LLVM 23.0.0git
DependenceAnalysis.cpp
Go to the documentation of this file.
1//===-- DependenceAnalysis.cpp - DA Implementation --------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// DependenceAnalysis is an LLVM pass that analyses dependences between memory
10// accesses. Currently, it is an (incomplete) implementation of the approach
11// described in
12//
13// Practical Dependence Testing
14// Goff, Kennedy, Tseng
15// PLDI 1991
16//
17// There's a single entry point that analyzes the dependence between a pair
18// of memory references in a function, returning either NULL, for no dependence,
19// or a more-or-less detailed description of the dependence between them.
20//
21// Since Clang linearizes some array subscripts, the dependence
22// analysis is using SCEV->delinearize to recover the representation of multiple
23// subscripts, and thus avoid the more expensive and less precise MIV tests. The
24// delinearization is controlled by the flag -da-delinearize.
25//
26// We should pay some careful attention to the possibility of integer overflow
27// in the implementation of the various tests. This could happen with Add,
28// Subtract, or Multiply, with both APInt's and SCEV's.
29//
30// Some non-linear subscript pairs can be handled by the GCD test
31// (and perhaps other tests).
32// Should explore how often these things occur.
33//
34// Finally, it seems like certain test cases expose weaknesses in the SCEV
35// simplification, especially in the handling of sign and zero extensions.
36// It could be useful to spend time exploring these.
37//
38// Please note that this is work in progress and the interface is subject to
39// change.
40//
41//===----------------------------------------------------------------------===//
42// //
43// In memory of Ken Kennedy, 1945 - 2007 //
44// //
45//===----------------------------------------------------------------------===//
46
48#include "llvm/ADT/Statistic.h"
56#include "llvm/IR/Module.h"
59#include "llvm/Support/Debug.h"
62
63using namespace llvm;
64
65#define DEBUG_TYPE "da"
66
67//===----------------------------------------------------------------------===//
68// statistics
69
70STATISTIC(TotalArrayPairs, "Array pairs tested");
71STATISTIC(NonlinearSubscriptPairs, "Nonlinear subscript pairs");
72STATISTIC(ZIVapplications, "ZIV applications");
73STATISTIC(ZIVindependence, "ZIV independence");
74STATISTIC(StrongSIVapplications, "Strong SIV applications");
75STATISTIC(StrongSIVsuccesses, "Strong SIV successes");
76STATISTIC(StrongSIVindependence, "Strong SIV independence");
77STATISTIC(WeakCrossingSIVapplications, "Weak-Crossing SIV applications");
78STATISTIC(WeakCrossingSIVsuccesses, "Weak-Crossing SIV successes");
79STATISTIC(WeakCrossingSIVindependence, "Weak-Crossing SIV independence");
80STATISTIC(ExactSIVapplications, "Exact SIV applications");
81STATISTIC(ExactSIVsuccesses, "Exact SIV successes");
82STATISTIC(ExactSIVindependence, "Exact SIV independence");
83STATISTIC(WeakZeroSIVapplications, "Weak-Zero SIV applications");
84STATISTIC(WeakZeroSIVsuccesses, "Weak-Zero SIV successes");
85STATISTIC(WeakZeroSIVindependence, "Weak-Zero SIV independence");
86STATISTIC(ExactRDIVapplications, "Exact RDIV applications");
87STATISTIC(ExactRDIVindependence, "Exact RDIV independence");
88STATISTIC(GCDapplications, "GCD applications");
89STATISTIC(GCDsuccesses, "GCD successes");
90STATISTIC(GCDindependence, "GCD independence");
91STATISTIC(BanerjeeApplications, "Banerjee applications");
92STATISTIC(BanerjeeIndependence, "Banerjee independence");
93STATISTIC(BanerjeeSuccesses, "Banerjee successes");
94STATISTIC(SameSDLoopsCount, "Loops with Same iteration Space and Depth");
95
96static cl::opt<bool>
97 Delinearize("da-delinearize", cl::init(true), cl::Hidden,
98 cl::desc("Try to delinearize array references."));
100 "da-disable-delinearization-checks", cl::Hidden,
101 cl::desc(
102 "Disable checks that try to statically verify validity of "
103 "delinearized subscripts. Enabling this option may result in incorrect "
104 "dependence vectors for languages that allow the subscript of one "
105 "dimension to underflow or overflow into another dimension."));
106
108 "da-miv-max-level-threshold", cl::init(7), cl::Hidden,
109 cl::desc("Maximum depth allowed for the recursive algorithm used to "
110 "explore MIV direction vectors."));
111
112namespace {
113
114/// Types of dependence test routines.
115enum class DependenceTestType {
116 Default, ///< All tests except BanerjeeMIV
117 All,
118 StrongSIV,
119 WeakCrossingSIV,
120 ExactSIV,
121 WeakZeroSIV,
122 ExactRDIV,
123 GCDMIV,
124 BanerjeeMIV,
125};
126
127} // anonymous namespace
128
130 "da-enable-dependence-test", cl::init(DependenceTestType::Default),
132 cl::desc("Run only specified dependence test routine and disable others. "
133 "The purpose is mainly to exclude the influence of other "
134 "dependence test routines in regression tests. If set to All, all "
135 "dependence test routines are enabled."),
136 cl::values(clEnumValN(DependenceTestType::Default, "default",
137 "Enable all dependence test routines except "
138 "Banerjee MIV (default)."),
139 clEnumValN(DependenceTestType::All, "all",
140 "Enable all dependence test routines."),
141 clEnumValN(DependenceTestType::StrongSIV, "strong-siv",
142 "Enable only Strong SIV test."),
143 clEnumValN(DependenceTestType::WeakCrossingSIV,
144 "weak-crossing-siv",
145 "Enable only Weak-Crossing SIV test."),
146 clEnumValN(DependenceTestType::ExactSIV, "exact-siv",
147 "Enable only Exact SIV test."),
148 clEnumValN(DependenceTestType::WeakZeroSIV, "weak-zero-siv",
149 "Enable only Weak-Zero SIV test."),
150 clEnumValN(DependenceTestType::ExactRDIV, "exact-rdiv",
151 "Enable only Exact RDIV test."),
152 clEnumValN(DependenceTestType::GCDMIV, "gcd-miv",
153 "Enable only GCD MIV test."),
154 clEnumValN(DependenceTestType::BanerjeeMIV, "banerjee-miv",
155 "Enable only Banerjee MIV test.")));
156
157//===----------------------------------------------------------------------===//
158// basics
159
162 auto &AA = FAM.getResult<AAManager>(F);
163 auto &SE = FAM.getResult<ScalarEvolutionAnalysis>(F);
164 auto &LI = FAM.getResult<LoopAnalysis>(F);
165 return DependenceInfo(&F, &AA, &SE, &LI);
166}
167
168AnalysisKey DependenceAnalysis::Key;
169
171 "Dependence Analysis", true, true)
177
179
182
186
188 auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
189 auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
190 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
191 info.reset(new DependenceInfo(&F, &AA, &SE, &LI));
192 return false;
193}
194
196
198
205
206namespace {
207
208/// A wrapper class for std::optional<APInt> that provides arithmetic operators
209/// with overflow checking in a signed sense. This allows us to omit inserting
210/// an overflow check at every arithmetic operation, which simplifies the code
211/// if the operations are chained like `a + b + c + ...`.
212///
213/// If an calculation overflows, the result becomes "invalid" which is
214/// internally represented by std::nullopt. If any operand of an arithmetic
215/// operation is "invalid", the result will also be "invalid".
216struct OverflowSafeSignedAPInt {
217 OverflowSafeSignedAPInt() : Value(std::nullopt) {}
218 OverflowSafeSignedAPInt(const APInt &V) : Value(V) {}
219 OverflowSafeSignedAPInt(const std::optional<APInt> &V) : Value(V) {}
220
221 OverflowSafeSignedAPInt operator+(const OverflowSafeSignedAPInt &RHS) const {
222 if (!Value || !RHS.Value)
223 return OverflowSafeSignedAPInt();
224 bool Overflow;
225 APInt Result = Value->sadd_ov(*RHS.Value, Overflow);
226 if (Overflow)
227 return OverflowSafeSignedAPInt();
228 return OverflowSafeSignedAPInt(Result);
229 }
230
231 OverflowSafeSignedAPInt operator+(int RHS) const {
232 if (!Value)
233 return OverflowSafeSignedAPInt();
234 return *this + fromInt(RHS);
235 }
236
237 OverflowSafeSignedAPInt operator-(const OverflowSafeSignedAPInt &RHS) const {
238 if (!Value || !RHS.Value)
239 return OverflowSafeSignedAPInt();
240 bool Overflow;
241 APInt Result = Value->ssub_ov(*RHS.Value, Overflow);
242 if (Overflow)
243 return OverflowSafeSignedAPInt();
244 return OverflowSafeSignedAPInt(Result);
245 }
246
247 OverflowSafeSignedAPInt operator-(int RHS) const {
248 if (!Value)
249 return OverflowSafeSignedAPInt();
250 return *this - fromInt(RHS);
251 }
252
253 OverflowSafeSignedAPInt operator*(const OverflowSafeSignedAPInt &RHS) const {
254 if (!Value || !RHS.Value)
255 return OverflowSafeSignedAPInt();
256 bool Overflow;
257 APInt Result = Value->smul_ov(*RHS.Value, Overflow);
258 if (Overflow)
259 return OverflowSafeSignedAPInt();
260 return OverflowSafeSignedAPInt(Result);
261 }
262
263 OverflowSafeSignedAPInt operator-() const {
264 if (!Value)
265 return OverflowSafeSignedAPInt();
266 if (Value->isMinSignedValue())
267 return OverflowSafeSignedAPInt();
268 return OverflowSafeSignedAPInt(-*Value);
269 }
270
271 operator bool() const { return Value.has_value(); }
272
273 bool operator!() const { return !Value.has_value(); }
274
275 const APInt &operator*() const {
276 assert(Value && "Value is not available.");
277 return *Value;
278 }
279
280 const APInt *operator->() const {
281 assert(Value && "Value is not available.");
282 return &*Value;
283 }
284
285private:
286 /// Underlying value. std::nullopt means "unknown". An arithmetic operation on
287 /// "unknown" always produces "unknown".
288 std::optional<APInt> Value;
289
290 OverflowSafeSignedAPInt fromInt(uint64_t V) const {
291 assert(Value && "Value is not available.");
292 return OverflowSafeSignedAPInt(
293 APInt(Value->getBitWidth(), V, /*isSigned=*/true));
294 }
295};
296
297} // anonymous namespace
298
299// Used to test the dependence analyzer.
300// Looks through the function, noting instructions that may access memory.
301// Calls depends() on every possible pair and prints out the result.
302// Ignores all other instructions.
304 ScalarEvolution &SE, LoopInfo &LI,
305 bool NormalizeResults) {
306 auto *F = DA->getFunction();
307
308 for (inst_iterator SrcI = inst_begin(F), SrcE = inst_end(F); SrcI != SrcE;
309 ++SrcI) {
310 if (SrcI->mayReadOrWriteMemory()) {
311 for (inst_iterator DstI = SrcI, DstE = inst_end(F); DstI != DstE;
312 ++DstI) {
313 if (DstI->mayReadOrWriteMemory()) {
314 OS << "Src:" << *SrcI << " --> Dst:" << *DstI << "\n";
315 OS << " da analyze - ";
316 if (auto D = DA->depends(&*SrcI, &*DstI,
317 /*UnderRuntimeAssumptions=*/true)) {
318
319#ifndef NDEBUG
320 // Verify that the distance being zero is equivalent to the
321 // direction being EQ.
322 for (unsigned Level = 1; Level <= D->getLevels(); Level++) {
323 const SCEV *Distance = D->getDistance(Level);
324 bool IsDistanceZero = Distance && Distance->isZero();
325 bool IsDirectionEQ =
326 D->getDirection(Level) == Dependence::DVEntry::EQ;
327 assert(IsDistanceZero == IsDirectionEQ &&
328 "Inconsistent distance and direction.");
329 }
330#endif
331
332 // Normalize negative direction vectors if required by clients.
333 if (NormalizeResults && D->normalize(&SE))
334 OS << "normalized - ";
335 D->dump(OS);
336 } else
337 OS << "none!\n";
338 }
339 }
340 }
341 }
342}
343
345 const Module *) const {
347 OS, info.get(), getAnalysis<ScalarEvolutionWrapperPass>().getSE(),
348 getAnalysis<LoopInfoWrapperPass>().getLoopInfo(), false);
349}
350
353 OS << "Printing analysis 'Dependence Analysis' for function '" << F.getName()
354 << "':\n";
356 FAM.getResult<ScalarEvolutionAnalysis>(F),
357 FAM.getResult<LoopAnalysis>(F), NormalizeResults);
358 return PreservedAnalyses::all();
359}
360
361//===----------------------------------------------------------------------===//
362// Dependence methods
363
364// Returns true if this is an input dependence.
366 return Src->mayReadFromMemory() && Dst->mayReadFromMemory();
367}
368
369// Returns true if this is an output dependence.
371 return Src->mayWriteToMemory() && Dst->mayWriteToMemory();
372}
373
374// Returns true if this is an flow (aka true) dependence.
375bool Dependence::isFlow() const {
376 return Src->mayWriteToMemory() && Dst->mayReadFromMemory();
377}
378
379// Returns true if this is an anti dependence.
380bool Dependence::isAnti() const {
381 return Src->mayReadFromMemory() && Dst->mayWriteToMemory();
382}
383
384// Returns true if a particular level is scalar; that is,
385// if no subscript in the source or destination mention the induction
386// variable associated with the loop at this level.
387// Leave this out of line, so it will serve as a virtual method anchor
388bool Dependence::isScalar(unsigned level, bool IsSameSD) const { return false; }
389
390//===----------------------------------------------------------------------===//
391// FullDependence methods
392
394 const SCEVUnionPredicate &Assumes,
395 bool PossiblyLoopIndependent,
396 unsigned CommonLevels)
397 : Dependence(Source, Destination, Assumes), Levels(CommonLevels),
398 LoopIndependent(PossiblyLoopIndependent) {
399 SameSDLevels = 0;
400 if (CommonLevels)
401 DV = std::make_unique<DVEntry[]>(CommonLevels);
402}
403
404// FIXME: in some cases the meaning of a negative direction vector
405// may not be straightforward, e.g.,
406// for (int i = 0; i < 32; ++i) {
407// Src: A[i] = ...;
408// Dst: use(A[31 - i]);
409// }
410// The dependency is
411// flow { Src[i] -> Dst[31 - i] : when i >= 16 } and
412// anti { Dst[i] -> Src[31 - i] : when i < 16 },
413// -- hence a [<>].
414// As long as a dependence result contains '>' ('<>', '<=>', "*"), it
415// means that a reversed/normalized dependence needs to be considered
416// as well. Nevertheless, current isDirectionNegative() only returns
417// true with a '>' or '>=' dependency for ease of canonicalizing the
418// dependency vector, since the reverse of '<>', '<=>' and "*" is itself.
420 for (unsigned Level = 1; Level <= Levels; ++Level) {
421 unsigned char Direction = DV[Level - 1].Direction;
422 if (Direction == Dependence::DVEntry::EQ)
423 continue;
424 if (Direction == Dependence::DVEntry::GT ||
425 Direction == Dependence::DVEntry::GE)
426 return true;
427 return false;
428 }
429 return false;
430}
431
433 std::swap(Src, Dst);
434 for (unsigned Level = 1; Level <= Levels; ++Level) {
435 unsigned char Direction = DV[Level - 1].Direction;
436 // Reverse the direction vector, this means LT becomes GT
437 // and GT becomes LT.
438 unsigned char RevDirection = Direction & Dependence::DVEntry::EQ;
439 if (Direction & Dependence::DVEntry::LT)
440 RevDirection |= Dependence::DVEntry::GT;
441 if (Direction & Dependence::DVEntry::GT)
442 RevDirection |= Dependence::DVEntry::LT;
443 DV[Level - 1].Direction = RevDirection;
444 // Reverse the dependence distance as well.
445 if (DV[Level - 1].Distance != nullptr)
446 DV[Level - 1].Distance = SE.getNegativeSCEV(DV[Level - 1].Distance);
447 }
448}
449
451 if (!isDirectionNegative())
452 return false;
453
454 LLVM_DEBUG(dbgs() << "Before normalizing negative direction vectors:\n";
455 dump(dbgs()););
456 negate(*SE);
457 LLVM_DEBUG(dbgs() << "After normalizing negative direction vectors:\n";
458 dump(dbgs()););
459 return true;
460}
461
462// The rest are simple getters that hide the implementation.
463
464// getDirection - Returns the direction associated with a particular common or
465// SameSD level.
466unsigned FullDependence::getDirection(unsigned Level, bool IsSameSD) const {
467 return getDVEntry(Level, IsSameSD).Direction;
468}
469
470// Returns the distance (or NULL) associated with a particular common or
471// SameSD level.
472const SCEV *FullDependence::getDistance(unsigned Level, bool IsSameSD) const {
473 return getDVEntry(Level, IsSameSD).Distance;
474}
475
476// Returns true if a particular regular or SameSD level is scalar; that is,
477// if no subscript in the source or destination mention the induction variable
478// associated with the loop at this level.
479bool FullDependence::isScalar(unsigned Level, bool IsSameSD) const {
480 return getDVEntry(Level, IsSameSD).Scalar;
481}
482
483// inSameSDLoops - Returns true if this level is an SameSD level, i.e.,
484// performed across two separate loop nests that have the Same iteration space
485// and Depth.
486bool FullDependence::inSameSDLoops(unsigned Level) const {
487 assert(0 < Level && Level <= static_cast<unsigned>(Levels) + SameSDLevels &&
488 "Level out of range");
489 return Level > Levels;
490}
491
492//===----------------------------------------------------------------------===//
493// DependenceInfo methods
494
495// For debugging purposes. Dumps a dependence to OS.
497 if (isConfused())
498 OS << "confused";
499 else {
500 if (isFlow())
501 OS << "flow";
502 else if (isOutput())
503 OS << "output";
504 else if (isAnti())
505 OS << "anti";
506 else if (isInput())
507 OS << "input";
508 dumpImp(OS);
509 unsigned SameSDLevels = getSameSDLevels();
510 if (SameSDLevels > 0) {
511 OS << " / assuming " << SameSDLevels << " loop level(s) fused: ";
512 dumpImp(OS, true);
513 }
514 }
515 OS << "!\n";
516
518 if (!Assumptions.isAlwaysTrue()) {
519 OS << " Runtime Assumptions:\n";
520 Assumptions.print(OS, 2);
521 }
522}
523
524// For debugging purposes. Dumps a dependence to OS with or without considering
525// the SameSD levels.
526void Dependence::dumpImp(raw_ostream &OS, bool IsSameSD) const {
527 unsigned Levels = getLevels();
528 unsigned SameSDLevels = getSameSDLevels();
529 bool OnSameSD = false;
530 unsigned LevelNum = Levels;
531 if (IsSameSD)
532 LevelNum += SameSDLevels;
533 OS << " [";
534 for (unsigned II = 1; II <= LevelNum; ++II) {
535 if (!OnSameSD && inSameSDLoops(II))
536 OnSameSD = true;
537 const SCEV *Distance = getDistance(II, OnSameSD);
538 if (Distance)
539 OS << *Distance;
540 else if (isScalar(II, OnSameSD))
541 OS << "S";
542 else {
543 unsigned Direction = getDirection(II, OnSameSD);
544 if (Direction == DVEntry::ALL)
545 OS << "*";
546 else {
547 if (Direction & DVEntry::LT)
548 OS << "<";
549 if (Direction & DVEntry::EQ)
550 OS << "=";
551 if (Direction & DVEntry::GT)
552 OS << ">";
553 }
554 }
555 if (II < LevelNum)
556 OS << " ";
557 }
558 if (isLoopIndependent())
559 OS << "|<";
560 OS << "]";
561}
562
563// Returns NoAlias/MayAliass/MustAlias for two memory locations based upon their
564// underlaying objects. If LocA and LocB are known to not alias (for any reason:
565// tbaa, non-overlapping regions etc), then it is known there is no dependecy.
566// Otherwise the underlying objects are checked to see if they point to
567// different identifiable objects.
569 const MemoryLocation &LocA,
570 const MemoryLocation &LocB) {
571 // Check the original locations (minus size) for noalias, which can happen for
572 // tbaa, incompatible underlying object locations, etc.
573 MemoryLocation LocAS =
575 MemoryLocation LocBS =
577 BatchAAResults BAA(*AA);
579
580 if (BAA.isNoAlias(LocAS, LocBS))
582
583 // Check the underlying objects are the same
584 const Value *AObj = getUnderlyingObject(LocA.Ptr);
585 const Value *BObj = getUnderlyingObject(LocB.Ptr);
586
587 // If the underlying objects are the same, they must alias
588 if (AObj == BObj)
590
591 // We may have hit the recursion limit for underlying objects, or have
592 // underlying objects where we don't know they will alias.
593 if (!isIdentifiedObject(AObj) || !isIdentifiedObject(BObj))
595
596 // Otherwise we know the objects are different and both identified objects so
597 // must not alias.
599}
600
601// Returns true if the load or store can be analyzed. Atomic and volatile
602// operations have properties which this analysis does not understand.
603static bool isLoadOrStore(const Instruction *I) {
604 if (const LoadInst *LI = dyn_cast<LoadInst>(I))
605 return LI->isUnordered();
606 else if (const StoreInst *SI = dyn_cast<StoreInst>(I))
607 return SI->isUnordered();
608 return false;
609}
610
611// Returns true if two loops have the Same iteration Space and Depth. To be
612// more specific, two loops have SameSD if they are in the same nesting
613// depth and have the same backedge count. SameSD stands for Same iteration
614// Space and Depth.
615bool DependenceInfo::haveSameSD(const Loop *SrcLoop,
616 const Loop *DstLoop) const {
617 if (SrcLoop == DstLoop)
618 return true;
619
620 if (SrcLoop->getLoopDepth() != DstLoop->getLoopDepth())
621 return false;
622
623 if (!SrcLoop || !SrcLoop->getLoopLatch() || !DstLoop ||
624 !DstLoop->getLoopLatch())
625 return false;
626
627 const SCEV *SrcUB = SE->getBackedgeTakenCount(SrcLoop);
628 const SCEV *DstUB = SE->getBackedgeTakenCount(DstLoop);
630 return false;
631
632 Type *WiderType = SE->getWiderType(SrcUB->getType(), DstUB->getType());
633 SrcUB = SE->getNoopOrZeroExtend(SrcUB, WiderType);
634 DstUB = SE->getNoopOrZeroExtend(DstUB, WiderType);
635
636 if (SrcUB == DstUB)
637 return true;
638
639 return false;
640}
641
642// Examines the loop nesting of the Src and Dst
643// instructions and establishes their shared loops. Sets the variables
644// CommonLevels, SrcLevels, and MaxLevels.
645// The source and destination instructions needn't be contained in the same
646// loop. The routine establishNestingLevels finds the level of most deeply
647// nested loop that contains them both, CommonLevels. An instruction that's
648// not contained in a loop is at level = 0. MaxLevels is equal to the level
649// of the source plus the level of the destination, minus CommonLevels.
650// This lets us allocate vectors MaxLevels in length, with room for every
651// distinct loop referenced in both the source and destination subscripts.
652// The variable SrcLevels is the nesting depth of the source instruction.
653// It's used to help calculate distinct loops referenced by the destination.
654// Here's the map from loops to levels:
655// 0 - unused
656// 1 - outermost common loop
657// ... - other common loops
658// CommonLevels - innermost common loop
659// ... - loops containing Src but not Dst
660// SrcLevels - innermost loop containing Src but not Dst
661// ... - loops containing Dst but not Src
662// MaxLevels - innermost loops containing Dst but not Src
663// Consider the follow code fragment:
664// for (a = ...) {
665// for (b = ...) {
666// for (c = ...) {
667// for (d = ...) {
668// A[] = ...;
669// }
670// }
671// for (e = ...) {
672// for (f = ...) {
673// for (g = ...) {
674// ... = A[];
675// }
676// }
677// }
678// }
679// }
680// If we're looking at the possibility of a dependence between the store
681// to A (the Src) and the load from A (the Dst), we'll note that they
682// have 2 loops in common, so CommonLevels will equal 2 and the direction
683// vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7.
684// A map from loop names to loop numbers would look like
685// a - 1
686// b - 2 = CommonLevels
687// c - 3
688// d - 4 = SrcLevels
689// e - 5
690// f - 6
691// g - 7 = MaxLevels
692// SameSDLevels counts the number of levels after common levels that are
693// not common but have the same iteration space and depth. Internally this
694// is checked using haveSameSD. Currently we only need to check for SameSD
695// levels up to one level after the common levels, and therefore SameSDLevels
696// will be either 0 or 1.
697// 1. Assume that in this code fragment, levels c and e have the same iteration
698// space and depth, but levels d and f does not. Then SameSDLevels is set to 1.
699// In that case the level numbers for the previous code look like
700// a - 1
701// b - 2
702// c,e - 3 = CommonLevels
703// d - 4 = SrcLevels
704// f - 5
705// g - 6 = MaxLevels
706void DependenceInfo::establishNestingLevels(const Instruction *Src,
707 const Instruction *Dst) {
708 const BasicBlock *SrcBlock = Src->getParent();
709 const BasicBlock *DstBlock = Dst->getParent();
710 unsigned SrcLevel = LI->getLoopDepth(SrcBlock);
711 unsigned DstLevel = LI->getLoopDepth(DstBlock);
712 const Loop *SrcLoop = LI->getLoopFor(SrcBlock);
713 const Loop *DstLoop = LI->getLoopFor(DstBlock);
714 SrcLevels = SrcLevel;
715 MaxLevels = SrcLevel + DstLevel;
716 SameSDLevels = 0;
717 while (SrcLevel > DstLevel) {
718 SrcLoop = SrcLoop->getParentLoop();
719 SrcLevel--;
720 }
721 while (DstLevel > SrcLevel) {
722 DstLoop = DstLoop->getParentLoop();
723 DstLevel--;
724 }
725
726 const Loop *SrcUncommonFrontier = nullptr, *DstUncommonFrontier = nullptr;
727 // Find the first uncommon level pair and check if the associated levels have
728 // the SameSD.
729 while (SrcLoop != DstLoop) {
730 SrcUncommonFrontier = SrcLoop;
731 DstUncommonFrontier = DstLoop;
732 SrcLoop = SrcLoop->getParentLoop();
733 DstLoop = DstLoop->getParentLoop();
734 SrcLevel--;
735 }
736 if (SrcUncommonFrontier && DstUncommonFrontier &&
737 haveSameSD(SrcUncommonFrontier, DstUncommonFrontier))
738 SameSDLevels = 1;
739 CommonLevels = SrcLevel;
740 MaxLevels -= CommonLevels;
741}
742
743// Given one of the loops containing the source, return
744// its level index in our numbering scheme.
745unsigned DependenceInfo::mapSrcLoop(const Loop *SrcLoop) const {
746 return SrcLoop->getLoopDepth();
747}
748
749// Given one of the loops containing the destination,
750// return its level index in our numbering scheme.
751unsigned DependenceInfo::mapDstLoop(const Loop *DstLoop) const {
752 unsigned D = DstLoop->getLoopDepth();
753 if (D > CommonLevels)
754 // This tries to make sure that we assign unique numbers to src and dst when
755 // the memory accesses reside in different loops that have the same depth.
756 return D - CommonLevels + SrcLevels;
757 else
758 return D;
759}
760
761// Returns true if Expression is loop invariant in LoopNest.
762bool DependenceInfo::isLoopInvariant(const SCEV *Expression,
763 const Loop *LoopNest) const {
764 // Unlike ScalarEvolution::isLoopInvariant() we consider an access outside of
765 // any loop as invariant, because we only consier expression evaluation at a
766 // specific position (where the array access takes place), and not across the
767 // entire function.
768 if (!LoopNest)
769 return true;
770
771 // If the expression is invariant in the outermost loop of the loop nest, it
772 // is invariant anywhere in the loop nest.
773 return SE->isLoopInvariant(Expression, LoopNest->getOutermostLoop());
774}
775
776// Finds the set of loops from the LoopNest that
777// have a level <= CommonLevels and are referred to by the SCEV Expression.
778void DependenceInfo::collectCommonLoops(const SCEV *Expression,
779 const Loop *LoopNest,
780 SmallBitVector &Loops) const {
781 while (LoopNest) {
782 unsigned Level = LoopNest->getLoopDepth();
783 if (Level <= CommonLevels && !SE->isLoopInvariant(Expression, LoopNest))
784 Loops.set(Level);
785 LoopNest = LoopNest->getParentLoop();
786 }
787}
788
789// Examine the scev and return true iff it's affine.
790// Collect any loops mentioned in the set of "Loops".
791bool DependenceInfo::checkSubscript(const SCEV *Expr, const Loop *LoopNest,
792 SmallBitVector &Loops, bool IsSrc) {
793 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
794 if (!AddRec)
795 return isLoopInvariant(Expr, LoopNest);
796
797 // The AddRec must depend on one of the containing loops. Otherwise,
798 // mapSrcLoop and mapDstLoop return indices outside the intended range. This
799 // can happen when a subscript in one loop references an IV from a sibling
800 // loop that could not be replaced with a concrete exit value by
801 // getSCEVAtScope.
802 const Loop *L = LoopNest;
803 while (L && AddRec->getLoop() != L)
804 L = L->getParentLoop();
805 if (!L)
806 return false;
807
808 if (!AddRec->hasNoSignedWrap())
809 return false;
810
811 const SCEV *Start = AddRec->getStart();
812 const SCEV *Step = AddRec->getStepRecurrence(*SE);
813 if (!isLoopInvariant(Step, LoopNest))
814 return false;
815 if (IsSrc)
816 Loops.set(mapSrcLoop(AddRec->getLoop()));
817 else
818 Loops.set(mapDstLoop(AddRec->getLoop()));
819 return checkSubscript(Start, LoopNest, Loops, IsSrc);
820}
821
822// Examine the scev and return true iff it's linear.
823// Collect any loops mentioned in the set of "Loops".
824bool DependenceInfo::checkSrcSubscript(const SCEV *Src, const Loop *LoopNest,
826 return checkSubscript(Src, LoopNest, Loops, true);
827}
828
829// Examine the scev and return true iff it's linear.
830// Collect any loops mentioned in the set of "Loops".
831bool DependenceInfo::checkDstSubscript(const SCEV *Dst, const Loop *LoopNest,
833 return checkSubscript(Dst, LoopNest, Loops, false);
834}
835
836// Examines the subscript pair (the Src and Dst SCEVs)
837// and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear.
838// Collects the associated loops in a set.
839DependenceInfo::Subscript::ClassificationKind
840DependenceInfo::classifyPair(const SCEV *Src, const Loop *SrcLoopNest,
841 const SCEV *Dst, const Loop *DstLoopNest,
843 SmallBitVector SrcLoops(MaxLevels + 1);
844 SmallBitVector DstLoops(MaxLevels + 1);
845 if (!checkSrcSubscript(Src, SrcLoopNest, SrcLoops))
846 return Subscript::NonLinear;
847 if (!checkDstSubscript(Dst, DstLoopNest, DstLoops))
848 return Subscript::NonLinear;
849 Loops = SrcLoops;
850 Loops |= DstLoops;
851 unsigned N = Loops.count();
852 if (N == 0)
853 return Subscript::ZIV;
854 if (N == 1)
855 return Subscript::SIV;
856 if (N == 2 && SrcLoops.count() == 1 && DstLoops.count() == 1)
857 return Subscript::RDIV;
858 return Subscript::MIV;
859}
860
861// All subscripts are all the same type.
862// Loop bound may be smaller (e.g., a char).
863// Should zero extend loop bound, since it's always >= 0.
864// This routine collects upper bound and extends or truncates if needed.
865// Truncating is safe when subscripts are known not to wrap. Cases without
866// nowrap flags should have been rejected earlier.
867// Return null if no bound available.
868const SCEV *DependenceInfo::collectUpperBound(const Loop *L, Type *T) const {
869 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
870 const SCEV *UB = SE->getBackedgeTakenCount(L);
871 return SE->getTruncateOrZeroExtend(UB, T);
872 }
873 return nullptr;
874}
875
876// Calls collectUpperBound(), then attempts to cast it to APInt.
877// If the cast fails, returns std::nullopt.
878std::optional<APInt>
879DependenceInfo::collectNonNegativeConstantUpperBound(const Loop *L,
880 Type *T) const {
881 if (const SCEV *UB = collectUpperBound(L, T))
882 if (auto *C = dyn_cast<SCEVConstant>(UB)) {
883 APInt Res = C->getAPInt();
884 if (Res.isNonNegative())
885 return Res;
886 }
887 return std::nullopt;
888}
889
890/// Returns \p A - \p B if it guaranteed not to signed wrap. Otherwise returns
891/// nullptr. \p A and \p B must have the same integer type.
892static const SCEV *minusSCEVNoSignedOverflow(const SCEV *A, const SCEV *B,
893 ScalarEvolution &SE) {
894 if (SE.willNotOverflow(Instruction::Sub, /*Signed=*/true, A, B))
895 return SE.getMinusSCEV(A, B);
896 return nullptr;
897}
898
899/// Returns true iff \p Test is enabled.
900static bool isDependenceTestEnabled(DependenceTestType Test) {
901 if (EnableDependenceTest == DependenceTestType::All)
902 return true;
903 // The Banerjee test is disabled by default because of correctness issues,
904 // but can be enabled with -da-enable-dependence-test=banerjee-miv or
905 // -da-enable-dependence-test=all.
906 if (EnableDependenceTest == DependenceTestType::Default)
907 return Test != DependenceTestType::BanerjeeMIV;
908 return EnableDependenceTest == Test;
909}
910
911// testZIV -
912// When we have a pair of subscripts of the form [c1] and [c2],
913// where c1 and c2 are both loop invariant, we attack it using
914// the ZIV test. Basically, we test by comparing the two values,
915// but there are actually three possible results:
916// 1) the values are equal, so there's a dependence
917// 2) the values are different, so there's no dependence
918// 3) the values might be equal, so we have to assume a dependence.
919//
920// Return true if dependence disproved.
921bool DependenceInfo::testZIV(const SCEV *Src, const SCEV *Dst,
922 FullDependence &Result) const {
923 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
924 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
925 ++ZIVapplications;
926 if (SE->isKnownPredicate(CmpInst::ICMP_EQ, Src, Dst)) {
927 LLVM_DEBUG(dbgs() << " provably dependent\n");
928 return false; // provably dependent
929 }
930 if (SE->isKnownPredicate(CmpInst::ICMP_NE, Src, Dst)) {
931 LLVM_DEBUG(dbgs() << " provably independent\n");
932 ++ZIVindependence;
933 return true; // provably independent
934 }
935 LLVM_DEBUG(dbgs() << " possibly dependent\n");
936 return false; // possibly dependent
937}
938
939// strongSIVtest -
940// From the paper, Practical Dependence Testing, Section 4.2.1
941//
942// When we have a pair of subscripts of the form [c1 + a*i] and [c2 + a*i],
943// where i is an induction variable, c1 and c2 are loop invariant,
944// and a is a constant, we can solve it exactly using the Strong SIV test.
945//
946// Can prove independence. Failing that, can compute distance (and direction).
947// In the presence of symbolic terms, we can sometimes make progress.
948//
949// If there's a dependence,
950//
951// c1 + a*i = c2 + a*i'
952//
953// The dependence distance is
954//
955// d = i' - i = (c1 - c2)/a
956//
957// A dependence only exists if d is an integer and abs(d) <= U, where U is the
958// loop's upper bound. If a dependence exists, the dependence direction is
959// defined as
960//
961// { < if d > 0
962// direction = { = if d = 0
963// { > if d < 0
964//
965// Return true if dependence disproved.
966bool DependenceInfo::strongSIVtest(const SCEVAddRecExpr *Src,
967 const SCEVAddRecExpr *Dst, unsigned Level,
968 FullDependence &Result,
969 bool UnderRuntimeAssumptions) {
970 if (!isDependenceTestEnabled(DependenceTestType::StrongSIV))
971 return false;
972
973 const SCEV *Coeff = Src->getStepRecurrence(*SE);
974 assert(Coeff == Dst->getStepRecurrence(*SE) &&
975 "Expecting same coefficient in Strong SIV test");
976 const SCEV *SrcConst = Src->getStart();
977 const SCEV *DstConst = Dst->getStart();
978 LLVM_DEBUG(dbgs() << "\tStrong SIV test\n");
979 LLVM_DEBUG(dbgs() << "\t Coeff = " << *Coeff);
980 LLVM_DEBUG(dbgs() << ", " << *Coeff->getType() << "\n");
981 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst);
982 LLVM_DEBUG(dbgs() << ", " << *SrcConst->getType() << "\n");
983 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst);
984 LLVM_DEBUG(dbgs() << ", " << *DstConst->getType() << "\n");
985 ++StrongSIVapplications;
986 assert(0 < Level && Level <= CommonLevels && "level out of range");
987 Level--;
988
989 const SCEV *Delta = minusSCEVNoSignedOverflow(SrcConst, DstConst, *SE);
990 if (!Delta)
991 return false;
992 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta);
993 LLVM_DEBUG(dbgs() << ", " << *Delta->getType() << "\n");
994
995 // Can we compute distance?
996 if (isa<SCEVConstant>(Delta) && isa<SCEVConstant>(Coeff)) {
997 APInt ConstDelta = cast<SCEVConstant>(Delta)->getAPInt();
998 APInt ConstCoeff = cast<SCEVConstant>(Coeff)->getAPInt();
999 APInt Distance = ConstDelta; // these need to be initialized
1000 APInt Remainder = ConstDelta;
1001 APInt::sdivrem(ConstDelta, ConstCoeff, Distance, Remainder);
1002 LLVM_DEBUG(dbgs() << "\t Distance = " << Distance << "\n");
1003 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
1004 // Make sure Coeff divides Delta exactly
1005 if (Remainder != 0) {
1006 // Coeff doesn't divide Distance, no dependence
1007 ++StrongSIVindependence;
1008 ++StrongSIVsuccesses;
1009 return true;
1010 }
1011 Result.DV[Level].Distance = SE->getConstant(Distance);
1012 if (Distance.sgt(0))
1013 Result.DV[Level].Direction &= Dependence::DVEntry::LT;
1014 else if (Distance.slt(0))
1015 Result.DV[Level].Direction &= Dependence::DVEntry::GT;
1016 else
1017 Result.DV[Level].Direction &= Dependence::DVEntry::EQ;
1018 ++StrongSIVsuccesses;
1019 } else if (Delta->isZero()) {
1020 // Check if coefficient could be zero. If so, 0/0 is undefined and we
1021 // cannot conclude that only same-iteration dependencies exist.
1022 // When coeff=0, all iterations access the same location.
1023 if (SE->isKnownNonZero(Coeff)) {
1024 LLVM_DEBUG(
1025 dbgs() << "\t Coefficient proven non-zero by SCEV analysis\n");
1026 } else {
1027 // Cannot prove at compile time, would need runtime assumption.
1028 if (UnderRuntimeAssumptions) {
1029 const SCEVPredicate *Pred = SE->getComparePredicate(
1030 ICmpInst::ICMP_NE, Coeff, SE->getZero(Coeff->getType()));
1031 Result.Assumptions = Result.Assumptions.getUnionWith(Pred, *SE);
1032 LLVM_DEBUG(dbgs() << "\t Added runtime assumption: " << *Coeff
1033 << " != 0\n");
1034 } else {
1035 // Cannot add runtime assumptions, this test cannot handle this case.
1036 // Let more complex tests try.
1037 LLVM_DEBUG(dbgs() << "\t Would need runtime assumption " << *Coeff
1038 << " != 0, but not allowed. Failing this test.\n");
1039 return false;
1040 }
1041 }
1042 // Since 0/X == 0 (where X is known non-zero or assumed non-zero).
1043 Result.DV[Level].Distance = Delta;
1044 Result.DV[Level].Direction &= Dependence::DVEntry::EQ;
1045 ++StrongSIVsuccesses;
1046 } else {
1047 if (Coeff->isOne()) {
1048 LLVM_DEBUG(dbgs() << "\t Distance = " << *Delta << "\n");
1049 Result.DV[Level].Distance = Delta; // since X/1 == X
1050 }
1051
1052 // maybe we can get a useful direction
1053 bool DeltaMaybeZero = !SE->isKnownNonZero(Delta);
1054 bool DeltaMaybePositive = !SE->isKnownNonPositive(Delta);
1055 bool DeltaMaybeNegative = !SE->isKnownNonNegative(Delta);
1056 bool CoeffMaybePositive = !SE->isKnownNonPositive(Coeff);
1057 bool CoeffMaybeNegative = !SE->isKnownNonNegative(Coeff);
1058 // The double negatives above are confusing.
1059 // It helps to read !SE->isKnownNonZero(Delta)
1060 // as "Delta might be Zero"
1061 unsigned NewDirection = Dependence::DVEntry::NONE;
1062 if ((DeltaMaybePositive && CoeffMaybePositive) ||
1063 (DeltaMaybeNegative && CoeffMaybeNegative))
1064 NewDirection = Dependence::DVEntry::LT;
1065 if (DeltaMaybeZero)
1066 NewDirection |= Dependence::DVEntry::EQ;
1067 if ((DeltaMaybeNegative && CoeffMaybePositive) ||
1068 (DeltaMaybePositive && CoeffMaybeNegative))
1069 NewDirection |= Dependence::DVEntry::GT;
1070 if (NewDirection < Result.DV[Level].Direction)
1071 ++StrongSIVsuccesses;
1072 Result.DV[Level].Direction &= NewDirection;
1073 }
1074 return false;
1075}
1076
1077// weakCrossingSIVtest -
1078// From the paper, Practical Dependence Testing, Section 4.2.2
1079//
1080// When we have a pair of subscripts of the form [c1 + a*i] and [c2 - a*i],
1081// where i is an induction variable, c1 and c2 are loop invariant,
1082// and a is a constant, we can solve it exactly using the
1083// Weak-Crossing SIV test.
1084//
1085// Given c1 + a*i = c2 - a*i', we can look for the intersection of
1086// the two lines, where i = i', yielding
1087//
1088// c1 + a*i = c2 - a*i
1089// 2a*i = c2 - c1
1090// i = (c2 - c1)/2a
1091//
1092// If i < 0, there is no dependence.
1093// If i > upperbound, there is no dependence.
1094// If i = 0 (i.e., if c1 = c2), there's a dependence with distance = 0.
1095// If i = upperbound, there's a dependence with distance = 0.
1096// If i is integral, there's a dependence (all directions).
1097// If the non-integer part = 1/2, there's a dependence (<> directions).
1098// Otherwise, there's no dependence.
1099//
1100// Can prove independence. Failing that,
1101// can sometimes refine the directions.
1102// Can determine iteration for splitting.
1103//
1104// Return true if dependence disproved.
1105bool DependenceInfo::weakCrossingSIVtest(const SCEVAddRecExpr *Src,
1106 const SCEVAddRecExpr *Dst,
1107 unsigned Level,
1108 FullDependence &Result) const {
1109 if (!isDependenceTestEnabled(DependenceTestType::WeakCrossingSIV))
1110 return false;
1111
1112 const SCEV *Coeff = Src->getStepRecurrence(*SE);
1113 const SCEV *SrcConst = Src->getStart();
1114 const SCEV *DstConst = Dst->getStart();
1115
1116 assert(Coeff == SE->getNegativeSCEV(Dst->getStepRecurrence(*SE)) &&
1117 "Unexpected input for weakCrossingSIVtest");
1118
1119 LLVM_DEBUG(dbgs() << "\tWeak-Crossing SIV test\n");
1120 LLVM_DEBUG(dbgs() << "\t Coeff = " << *Coeff << "\n");
1121 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1122 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1123 ++WeakCrossingSIVapplications;
1124 assert(0 < Level && Level <= CommonLevels && "Level out of range");
1125 Level--;
1126 const SCEV *Delta = minusSCEVNoSignedOverflow(DstConst, SrcConst, *SE);
1127 if (!Delta)
1128 return false;
1129
1130 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1131 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(Coeff);
1132 if (!ConstCoeff)
1133 return false;
1134
1135 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1136 if (!ConstDelta)
1137 return false;
1138
1139 ConstantRange SrcRange = SE->getSignedRange(Src);
1140 ConstantRange DstRange = SE->getSignedRange(Dst);
1141 LLVM_DEBUG(dbgs() << "\t SrcRange = " << SrcRange << "\n");
1142 LLVM_DEBUG(dbgs() << "\t DstRange = " << DstRange << "\n");
1143 if (SrcRange.intersectWith(DstRange).isSingleElement()) {
1144 // The ranges touch at exactly one value (i = i' = 0 or i = i' = BTC).
1145 Result.DV[Level].Direction &= ~Dependence::DVEntry::LT;
1146 Result.DV[Level].Direction &= ~Dependence::DVEntry::GT;
1147 ++WeakCrossingSIVsuccesses;
1148 if (!Result.DV[Level].Direction) {
1149 ++WeakCrossingSIVindependence;
1150 return true;
1151 }
1152 Result.DV[Level].Distance = SE->getZero(Delta->getType());
1153 return false;
1154 }
1155
1156 // check that Coeff divides Delta
1157 APInt APDelta = ConstDelta->getAPInt();
1158 APInt APCoeff = ConstCoeff->getAPInt();
1159 APInt Distance = APDelta; // these need to be initialzed
1160 APInt Remainder = APDelta;
1161 APInt::sdivrem(APDelta, APCoeff, Distance, Remainder);
1162 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
1163 if (Remainder != 0) {
1164 // Coeff doesn't divide Delta, no dependence
1165 ++WeakCrossingSIVindependence;
1166 ++WeakCrossingSIVsuccesses;
1167 return true;
1168 }
1169 LLVM_DEBUG(dbgs() << "\t Distance = " << Distance << "\n");
1170
1171 // if 2*Coeff doesn't divide Delta, then the equal direction isn't possible
1172 if (Distance[0]) {
1173 // Equal direction isn't possible
1174 Result.DV[Level].Direction &= ~Dependence::DVEntry::EQ;
1175 ++WeakCrossingSIVsuccesses;
1176 }
1177 return false;
1178}
1179
1180// Kirch's algorithm, from
1181//
1182// Optimizing Supercompilers for Supercomputers
1183// Michael Wolfe
1184// MIT Press, 1989
1185//
1186// Program 2.1, page 29.
1187// Computes the GCD of AM and BM.
1188// Also finds a solution to the equation ax - by = gcd(a, b).
1189// Returns true if dependence disproved; i.e., gcd does not divide Delta.
1190//
1191// We don't use OverflowSafeSignedAPInt here because it's known that this
1192// algorithm doesn't overflow.
1193static bool findGCD(unsigned Bits, const APInt &AM, const APInt &BM,
1194 const APInt &Delta, APInt &G, APInt &X, APInt &Y) {
1195 LLVM_DEBUG(dbgs() << "\t AM = " << AM << "\n");
1196 LLVM_DEBUG(dbgs() << "\t BM = " << BM << "\n");
1197 LLVM_DEBUG(dbgs() << "\t Delta = " << Delta << "\n");
1198 APInt A0(Bits, 1, true), A1(Bits, 0, true);
1199 APInt B0(Bits, 0, true), B1(Bits, 1, true);
1200 APInt G0 = AM.abs();
1201 APInt G1 = BM.abs();
1202 APInt Q = G0; // these need to be initialized
1203 APInt R = G0;
1204 APInt::sdivrem(G0, G1, Q, R);
1205 while (R != 0) {
1206 // clang-format off
1207 APInt A2 = A0 - Q*A1; A0 = A1; A1 = A2;
1208 APInt B2 = B0 - Q*B1; B0 = B1; B1 = B2;
1209 G0 = G1; G1 = R;
1210 // clang-format on
1211 APInt::sdivrem(G0, G1, Q, R);
1212 }
1213 G = G1;
1214 LLVM_DEBUG(dbgs() << "\t GCD = " << G << "\n");
1215 X = AM.slt(0) ? -A1 : A1;
1216 Y = BM.slt(0) ? B1 : -B1;
1217
1218 // make sure gcd divides Delta
1219 R = Delta.srem(G);
1220 if (R != 0)
1221 return true; // gcd doesn't divide Delta, no dependence
1222 Q = Delta.sdiv(G);
1223 return false;
1224}
1225
1226static OverflowSafeSignedAPInt
1227floorOfQuotient(const OverflowSafeSignedAPInt &OA,
1228 const OverflowSafeSignedAPInt &OB) {
1229 if (!OA || !OB)
1230 return OverflowSafeSignedAPInt();
1231
1232 APInt A = *OA;
1233 APInt B = *OB;
1234 APInt Q = A; // these need to be initialized
1235 APInt R = A;
1236 APInt::sdivrem(A, B, Q, R);
1237 if (R == 0)
1238 return Q;
1239 if ((A.sgt(0) && B.sgt(0)) || (A.slt(0) && B.slt(0)))
1240 return Q;
1241 return OverflowSafeSignedAPInt(Q) - 1;
1242}
1243
1244static OverflowSafeSignedAPInt
1245ceilingOfQuotient(const OverflowSafeSignedAPInt &OA,
1246 const OverflowSafeSignedAPInt &OB) {
1247 if (!OA || !OB)
1248 return OverflowSafeSignedAPInt();
1249
1250 APInt A = *OA;
1251 APInt B = *OB;
1252 APInt Q = A; // these need to be initialized
1253 APInt R = A;
1254 APInt::sdivrem(A, B, Q, R);
1255 if (R == 0)
1256 return Q;
1257 if ((A.sgt(0) && B.sgt(0)) || (A.slt(0) && B.slt(0)))
1258 return OverflowSafeSignedAPInt(Q) + 1;
1259 return Q;
1260}
1261
1262/// Given an affine expression of the form A*k + B, where k is an arbitrary
1263/// integer, infer the possible range of k based on the known range of the
1264/// affine expression. If we know A*k + B is non-negative, i.e.,
1265///
1266/// A*k + B >=s 0
1267///
1268/// we can derive the following inequalities for k when A is positive:
1269///
1270/// k >=s -B / A
1271///
1272/// Since k is an integer, it means k is greater than or equal to the
1273/// ceil(-B / A).
1274///
1275/// If the upper bound of the affine expression \p UB is passed, the following
1276/// inequality can be derived as well:
1277///
1278/// A*k + B <=s UB
1279///
1280/// which leads to:
1281///
1282/// k <=s (UB - B) / A
1283///
1284/// Again, as k is an integer, it means k is less than or equal to the
1285/// floor((UB - B) / A).
1286///
1287/// The similar logic applies when A is negative, but the inequalities sign flip
1288/// while working with them.
1289///
1290/// Preconditions: \p A is non-zero, and we know A*k + B and \p UB are
1291/// non-negative.
1292static std::pair<OverflowSafeSignedAPInt, OverflowSafeSignedAPInt>
1293inferDomainOfAffine(OverflowSafeSignedAPInt A, OverflowSafeSignedAPInt B,
1294 OverflowSafeSignedAPInt UB) {
1295 assert(A && B && "A and B must be available");
1296 assert(*A != 0 && "A must be non-zero");
1297 assert((!UB || UB->isNonNegative()) && "UB must be non-negative");
1298 OverflowSafeSignedAPInt TL, TU;
1299 if (A->sgt(0)) {
1300 TL = ceilingOfQuotient(-B, A);
1301 LLVM_DEBUG(if (TL) dbgs() << "\t Possible TL = " << *TL << "\n");
1302
1303 // New bound check - modification to Banerjee's e3 check
1304 TU = floorOfQuotient(UB - B, A);
1305 LLVM_DEBUG(if (TU) dbgs() << "\t Possible TU = " << *TU << "\n");
1306 } else {
1307 TU = floorOfQuotient(-B, A);
1308 LLVM_DEBUG(if (TU) dbgs() << "\t Possible TU = " << *TU << "\n");
1309
1310 // New bound check - modification to Banerjee's e3 check
1311 TL = ceilingOfQuotient(UB - B, A);
1312 LLVM_DEBUG(if (TL) dbgs() << "\t Possible TL = " << *TL << "\n");
1313 }
1314 return std::make_pair(TL, TU);
1315}
1316
1317// exactSIVtest -
1318// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*i],
1319// where i is an induction variable, c1 and c2 are loop invariant, and a1
1320// and a2 are constant, we can solve it exactly using an algorithm developed
1321// by Banerjee and Wolfe. See Algorithm 6.2.1 (case 2.5) in:
1322//
1323// Dependence Analysis for Supercomputing
1324// Utpal Banerjee
1325// Kluwer Academic Publishers, 1988
1326//
1327// It's slower than the specialized tests (strong SIV, weak-zero SIV, etc),
1328// so use them if possible. They're also a bit better with symbolics and,
1329// in the case of the strong SIV test, can compute Distances.
1330//
1331// Return true if dependence disproved.
1332//
1333// This is a modified version of the original Banerjee algorithm. The original
1334// only tested whether Dst depends on Src. This algorithm extends that and
1335// returns all the dependencies that exist between Dst and Src.
1336bool DependenceInfo::exactSIVtest(const SCEVAddRecExpr *Src,
1337 const SCEVAddRecExpr *Dst, unsigned Level,
1338 FullDependence &Result) const {
1339 if (!isDependenceTestEnabled(DependenceTestType::ExactSIV))
1340 return false;
1341
1342 LLVM_DEBUG(dbgs() << "\tExact SIV test\n");
1343 ++ExactSIVapplications;
1344 assert(0 < Level && Level <= CommonLevels && "Level out of range");
1345 Level--;
1346 bool Res = exactTestImpl(Src, Dst, Result, Level);
1347 if (Res) {
1348 ++ExactSIVsuccesses;
1349 ++ExactSIVindependence;
1350 }
1351 return Res;
1352}
1353
1354// Return true if the divisor evenly divides the dividend.
1355static bool isRemainderZero(const SCEVConstant *Dividend,
1356 const SCEVConstant *Divisor) {
1357 const APInt &ConstDividend = Dividend->getAPInt();
1358 const APInt &ConstDivisor = Divisor->getAPInt();
1359 return ConstDividend.srem(ConstDivisor) == 0;
1360}
1361
1362bool DependenceInfo::weakZeroSIVtestImpl(const SCEVAddRecExpr *AR,
1363 const SCEV *Const, unsigned Level,
1364 FullDependence &Result) const {
1365 const SCEV *ARCoeff = AR->getStepRecurrence(*SE);
1366 const SCEV *ARConst = AR->getStart();
1367
1368 if (Const == ARConst && SE->isKnownNonZero(ARCoeff)) {
1369 if (Level < CommonLevels) {
1370 Result.DV[Level].Direction &= Dependence::DVEntry::LE;
1371 ++WeakZeroSIVsuccesses;
1372 }
1373 return false; // dependences caused by first iteration
1374 }
1375
1376 const SCEV *Delta = minusSCEVNoSignedOverflow(Const, ARConst, *SE);
1377 if (!Delta)
1378 return false;
1379 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(ARCoeff);
1380 if (!ConstCoeff)
1381 return false;
1382
1383 if (const SCEV *UpperBound =
1384 collectUpperBound(AR->getLoop(), Delta->getType())) {
1385 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
1386 bool OverlapAtLast = [&] {
1387 if (!SE->isKnownNonZero(ConstCoeff))
1388 return false;
1389 const SCEV *Last = AR->evaluateAtIteration(UpperBound, *SE);
1390 return Last == Const;
1391 }();
1392 if (OverlapAtLast) {
1393 // dependences caused by last iteration
1394 if (Level < CommonLevels) {
1395 Result.DV[Level].Direction &= Dependence::DVEntry::GE;
1396 ++WeakZeroSIVsuccesses;
1397 }
1398 return false;
1399 }
1400 }
1401
1402 // if ARCoeff doesn't divide Delta, then no dependence
1403 if (isa<SCEVConstant>(Delta) &&
1404 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) {
1405 ++WeakZeroSIVindependence;
1406 ++WeakZeroSIVsuccesses;
1407 return true;
1408 }
1409 return false;
1410}
1411
1412// weakZeroSrcSIVtest -
1413// From the paper, Practical Dependence Testing, Section 4.2.2
1414//
1415// When we have a pair of subscripts of the form [c1] and [c2 + a*i],
1416// where i is an induction variable, c1 and c2 are loop invariant,
1417// and a is a constant, we can solve it exactly using the
1418// Weak-Zero SIV test.
1419//
1420// Given
1421//
1422// c1 = c2 + a*i
1423//
1424// we get
1425//
1426// (c1 - c2)/a = i
1427//
1428// If i is not an integer, there's no dependence.
1429// If i < 0 or > UB, there's no dependence.
1430// If i = 0, the direction is >=.
1431// If i = UB, the direction is <=.
1432// Otherwise, the direction is *.
1433//
1434// Can prove independence. Failing that, we can sometimes refine
1435// the directions. Can sometimes show that first or last
1436// iteration carries all the dependences (so worth peeling).
1437//
1438// (see also weakZeroDstSIVtest)
1439//
1440// Return true if dependence disproved.
1441bool DependenceInfo::weakZeroSrcSIVtest(const SCEV *SrcConst,
1442 const SCEVAddRecExpr *Dst,
1443 unsigned Level,
1444 FullDependence &Result) const {
1445 if (!isDependenceTestEnabled(DependenceTestType::WeakZeroSIV))
1446 return false;
1447
1448 // For the WeakSIV test, it's possible the loop isn't common to
1449 // the Src and Dst loops. If it isn't, then there's no need to
1450 // record a direction.
1451 [[maybe_unused]] const SCEV *DstCoeff = Dst->getStepRecurrence(*SE);
1452 [[maybe_unused]] const SCEV *DstConst = Dst->getStart();
1453 LLVM_DEBUG(dbgs() << "\tWeak-Zero (src) SIV test\n");
1454 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << "\n");
1455 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1456 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1457 ++WeakZeroSIVapplications;
1458 assert(0 < Level && Level <= MaxLevels && "Level out of range");
1459 Level--;
1460
1461 // We have analyzed a dependence from Src to Dst, so \c Result may represent a
1462 // dependence in that direction. However, \c weakZeroSIVtestImpl will analyze
1463 // a dependence from \c Dst to \c SrcConst. To keep the consistency, we need
1464 // to negate the current result before passing it to \c weakZeroSIVtestImpl,
1465 // and negate it back after that.
1466 Result.negate(*SE);
1467 bool Res = weakZeroSIVtestImpl(Dst, SrcConst, Level, Result);
1468 Result.negate(*SE);
1469 return Res;
1470}
1471
1472// weakZeroDstSIVtest -
1473// From the paper, Practical Dependence Testing, Section 4.2.2
1474//
1475// When we have a pair of subscripts of the form [c1 + a*i] and [c2],
1476// where i is an induction variable, c1 and c2 are loop invariant,
1477// and a is a constant, we can solve it exactly using the
1478// Weak-Zero SIV test.
1479//
1480// Given
1481//
1482// c1 + a*i = c2
1483//
1484// we get
1485//
1486// i = (c2 - c1)/a
1487//
1488// If i is not an integer, there's no dependence.
1489// If i < 0 or > UB, there's no dependence.
1490// If i = 0, the direction is <=.
1491// If i = UB, the direction is >=.
1492// Otherwise, the direction is *.
1493//
1494// Can prove independence. Failing that, we can sometimes refine
1495// the directions. Can sometimes show that first or last
1496// iteration carries all the dependences (so worth peeling).
1497//
1498// (see also weakZeroSrcSIVtest)
1499//
1500// Return true if dependence disproved.
1501bool DependenceInfo::weakZeroDstSIVtest(const SCEVAddRecExpr *Src,
1502 const SCEV *DstConst, unsigned Level,
1503 FullDependence &Result) const {
1504 if (!isDependenceTestEnabled(DependenceTestType::WeakZeroSIV))
1505 return false;
1506
1507 // For the WeakSIV test, it's possible the loop isn't common to the
1508 // Src and Dst loops. If it isn't, then there's no need to record a direction.
1509 [[maybe_unused]] const SCEV *SrcCoeff = Src->getStepRecurrence(*SE);
1510 [[maybe_unused]] const SCEV *SrcConst = Src->getStart();
1511 LLVM_DEBUG(dbgs() << "\tWeak-Zero (dst) SIV test\n");
1512 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << "\n");
1513 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1514 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1515 ++WeakZeroSIVapplications;
1516 assert(0 < Level && Level <= SrcLevels && "Level out of range");
1517 Level--;
1518
1519 return weakZeroSIVtestImpl(Src, DstConst, Level, Result);
1520}
1521
1522// exactRDIVtest - Tests the RDIV subscript pair for dependence.
1523// Things of the form [c1 + a*i] and [c2 + b*j],
1524// where i and j are induction variable, c1 and c2 are loop invariant,
1525// and a and b are constants.
1526// Returns true if any possible dependence is disproved.
1527// Works in some cases that symbolicRDIVtest doesn't, and vice versa.
1528bool DependenceInfo::exactRDIVtest(const SCEVAddRecExpr *Src,
1529 const SCEVAddRecExpr *Dst,
1530 FullDependence &Result) const {
1531 if (!isDependenceTestEnabled(DependenceTestType::ExactRDIV))
1532 return false;
1533
1534 LLVM_DEBUG(dbgs() << "\tExact RDIV test\n");
1535 ++ExactRDIVapplications;
1536 bool Res = exactTestImpl(Src, Dst, Result, std::nullopt);
1537 if (Res)
1538 ++ExactRDIVindependence;
1539 return Res;
1540}
1541
1542bool DependenceInfo::exactTestImpl(const SCEVAddRecExpr *Src,
1543 const SCEVAddRecExpr *Dst,
1544 FullDependence &Result,
1545 std::optional<unsigned> Level) const {
1546 const SCEV *SrcCoeff = Src->getStepRecurrence(*SE);
1547 const SCEV *SrcConst = Src->getStart();
1548 const SCEV *DstCoeff = Dst->getStepRecurrence(*SE);
1549 const SCEV *DstConst = Dst->getStart();
1550 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << "\n");
1551 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << "\n");
1552 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1553 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1554
1555 const SCEV *Delta = minusSCEVNoSignedOverflow(DstConst, SrcConst, *SE);
1556 if (!Delta)
1557 return false;
1558 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1559 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1560 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1561 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1562 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
1563 return false;
1564
1565 // find gcd
1566 APInt G, X, Y;
1567 APInt AM = ConstSrcCoeff->getAPInt();
1568 APInt BM = ConstDstCoeff->getAPInt();
1569 APInt CM = ConstDelta->getAPInt();
1570 unsigned Bits = AM.getBitWidth();
1571 if (findGCD(Bits, AM, BM, CM, G, X, Y)) {
1572 // gcd doesn't divide Delta, no dependence
1573 return true;
1574 }
1575
1576 LLVM_DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n");
1577
1578 // since SCEV construction seems to normalize, LM = 0
1579 std::optional<APInt> SrcUM =
1580 collectNonNegativeConstantUpperBound(Src->getLoop(), Delta->getType());
1581 if (SrcUM)
1582 LLVM_DEBUG(dbgs() << "\t SrcUM = " << *SrcUM << "\n");
1583
1584 std::optional<APInt> DstUM =
1585 collectNonNegativeConstantUpperBound(Dst->getLoop(), Delta->getType());
1586 if (DstUM)
1587 LLVM_DEBUG(dbgs() << "\t DstUM = " << *DstUM << "\n");
1588
1589 APInt TU(APInt::getSignedMaxValue(Bits));
1590 APInt TL(APInt::getSignedMinValue(Bits));
1591 APInt TC = CM.sdiv(G);
1592 APInt TX = X * TC;
1593 APInt TY = Y * TC;
1594 LLVM_DEBUG(dbgs() << "\t TC = " << TC << "\n");
1595 LLVM_DEBUG(dbgs() << "\t TX = " << TX << "\n");
1596 LLVM_DEBUG(dbgs() << "\t TY = " << TY << "\n");
1597
1598 APInt TB = BM.sdiv(G);
1599 APInt TA = AM.sdiv(G);
1600
1601 // At this point, we have the following equations:
1602 //
1603 // TA*i - TB*j = TC
1604 //
1605 // Also, we know that the all pairs of (i, j) can be expressed as:
1606 //
1607 // (TX + k*TB, TY + k*TA)
1608 //
1609 // where k is an arbitrary integer.
1610 auto [TL0, TU0] = inferDomainOfAffine(TB, TX, SrcUM);
1611 auto [TL1, TU1] = inferDomainOfAffine(TA, TY, DstUM);
1612
1613 LLVM_DEBUG(dbgs() << "\t TA = " << TA << "\n");
1614 LLVM_DEBUG(dbgs() << "\t TB = " << TB << "\n");
1615
1616 auto GetMaxOrMin = [](const OverflowSafeSignedAPInt &V0,
1617 const OverflowSafeSignedAPInt &V1,
1618 bool IsMin) -> std::optional<APInt> {
1619 if (V0 && V1)
1620 return IsMin ? APIntOps::smin(*V0, *V1) : APIntOps::smax(*V0, *V1);
1621 if (V0)
1622 return *V0;
1623 if (V1)
1624 return *V1;
1625 return std::nullopt;
1626 };
1627
1628 std::optional<APInt> OptTL = GetMaxOrMin(TL0, TL1, false);
1629 std::optional<APInt> OptTU = GetMaxOrMin(TU0, TU1, true);
1630 if (!OptTL || !OptTU)
1631 return false;
1632
1633 TL = std::move(*OptTL);
1634 TU = std::move(*OptTU);
1635 LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n");
1636 LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n");
1637
1638 if (TL.sgt(TU))
1639 return true;
1640
1641 if (!Level)
1642 return false;
1643 assert(SrcUM == DstUM && "Expecting same upper bound for Src and Dst");
1644
1645 // explore directions
1646 unsigned NewDirection = Dependence::DVEntry::NONE;
1647 OverflowSafeSignedAPInt LowerDistance, UpperDistance;
1648 OverflowSafeSignedAPInt OTY(TY), OTX(TX), OTA(TA), OTB(TB), OTL(TL), OTU(TU);
1649 // NOTE: It's unclear whether these calculations can overflow. At the moment,
1650 // we conservatively assume they can.
1651 if (TA.sgt(TB)) {
1652 LowerDistance = (OTY - OTX) + (OTA - OTB) * OTL;
1653 UpperDistance = (OTY - OTX) + (OTA - OTB) * OTU;
1654 } else {
1655 LowerDistance = (OTY - OTX) + (OTA - OTB) * OTU;
1656 UpperDistance = (OTY - OTX) + (OTA - OTB) * OTL;
1657 }
1658
1659 if (!LowerDistance || !UpperDistance)
1660 return false;
1661
1662 LLVM_DEBUG(dbgs() << "\t LowerDistance = " << *LowerDistance << "\n");
1663 LLVM_DEBUG(dbgs() << "\t UpperDistance = " << *UpperDistance << "\n");
1664
1665 if (LowerDistance->sle(0) && UpperDistance->sge(0))
1666 NewDirection |= Dependence::DVEntry::EQ;
1667 if (LowerDistance->slt(0))
1668 NewDirection |= Dependence::DVEntry::GT;
1669 if (UpperDistance->sgt(0))
1670 NewDirection |= Dependence::DVEntry::LT;
1671
1672 // finished
1673 Result.DV[*Level].Direction &= NewDirection;
1674 LLVM_DEBUG(dbgs() << "\t Result = ");
1675 LLVM_DEBUG(Result.dump(dbgs()));
1676 return Result.DV[*Level].Direction == Dependence::DVEntry::NONE;
1677}
1678
1679// testSIV -
1680// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 - a2*i]
1681// where i is an induction variable, c1 and c2 are loop invariant, and a1 and
1682// a2 are constant, we attack it with an SIV test. While they can all be
1683// solved with the Exact SIV test, it's worthwhile to use simpler tests when
1684// they apply; they're cheaper and sometimes more precise.
1685//
1686// Return true if dependence disproved.
1687bool DependenceInfo::testSIV(const SCEV *Src, const SCEV *Dst, unsigned &Level,
1688 FullDependence &Result,
1689 bool UnderRuntimeAssumptions) {
1690 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
1691 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
1692 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
1693 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
1694 if (SrcAddRec && DstAddRec) {
1695 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
1696 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE);
1697 const Loop *CurSrcLoop = SrcAddRec->getLoop();
1698 [[maybe_unused]] const Loop *CurDstLoop = DstAddRec->getLoop();
1699 assert(haveSameSD(CurSrcLoop, CurDstLoop) &&
1700 "Loops in the SIV test should have the same iteration space and "
1701 "depth");
1702 Level = mapSrcLoop(CurSrcLoop);
1703 bool disproven = false;
1704 if (SrcCoeff == DstCoeff)
1705 disproven = strongSIVtest(SrcAddRec, DstAddRec, Level, Result,
1706 UnderRuntimeAssumptions);
1707 else if (SrcCoeff == SE->getNegativeSCEV(DstCoeff))
1708 disproven = weakCrossingSIVtest(SrcAddRec, DstAddRec, Level, Result);
1709 return disproven || exactSIVtest(SrcAddRec, DstAddRec, Level, Result);
1710 }
1711 if (SrcAddRec) {
1712 const Loop *CurSrcLoop = SrcAddRec->getLoop();
1713 Level = mapSrcLoop(CurSrcLoop);
1714 return weakZeroDstSIVtest(SrcAddRec, Dst, Level, Result);
1715 }
1716 if (DstAddRec) {
1717 const Loop *CurDstLoop = DstAddRec->getLoop();
1718 Level = mapDstLoop(CurDstLoop);
1719 return weakZeroSrcSIVtest(Src, DstAddRec, Level, Result);
1720 }
1721 llvm_unreachable("SIV test expected at least one AddRec");
1722 return false;
1723}
1724
1725// testRDIV -
1726// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
1727// where i and j are induction variables, c1 and c2 are loop invariant,
1728// and a1 and a2 are constant, we can solve it exactly with an easy adaptation
1729// of the Exact SIV test, the Restricted Double Index Variable (RDIV) test.
1730// It doesn't make sense to talk about distance or direction in this case,
1731// so there's no point in making special versions of the Strong SIV test or
1732// the Weak-crossing SIV test.
1733//
1734// Return true if dependence disproved.
1735bool DependenceInfo::testRDIV(const SCEV *Src, const SCEV *Dst,
1736 FullDependence &Result) const {
1737 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
1738 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
1739 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
1740 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
1741 assert(SrcAddRec && DstAddRec && "Unexpected non-addrec input");
1742 return exactRDIVtest(SrcAddRec, DstAddRec, Result) ||
1743 gcdMIVtest(Src, Dst, Result);
1744}
1745
1746// Tests the single-subscript MIV pair (Src and Dst) for dependence.
1747// Return true if dependence disproved.
1748// Can sometimes refine direction vectors.
1749bool DependenceInfo::testMIV(const SCEV *Src, const SCEV *Dst,
1750 const SmallBitVector &Loops,
1751 FullDependence &Result) const {
1752 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
1753 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
1754 return gcdMIVtest(Src, Dst, Result) ||
1755 banerjeeMIVtest(Src, Dst, Loops, Result);
1756}
1757
1758/// Given a SCEVMulExpr, returns its first operand if its first operand is a
1759/// constant and the product doesn't overflow in a signed sense. Otherwise,
1760/// returns std::nullopt. For example, given (10 * X * Y)<nsw>, it returns 10.
1761/// Notably, if it doesn't have nsw, the multiplication may overflow, and if
1762/// so, it may not a multiple of 10.
1763static std::optional<APInt> getConstantCoefficient(const SCEV *Expr) {
1764 if (const auto *Constant = dyn_cast<SCEVConstant>(Expr))
1765 return Constant->getAPInt();
1766 if (const auto *Product = dyn_cast<SCEVMulExpr>(Expr))
1767 if (const auto *Constant = dyn_cast<SCEVConstant>(Product->getOperand(0)))
1768 if (Product->hasNoSignedWrap())
1769 return Constant->getAPInt();
1770 return std::nullopt;
1771}
1772
1773bool DependenceInfo::accumulateCoefficientsGCD(const SCEV *Expr,
1774 const Loop *CurLoop,
1775 const SCEV *&CurLoopCoeff,
1776 APInt &RunningGCD) const {
1777 // If RunningGCD is already 1, exit early.
1778 // TODO: It might be better to continue the recursion to find CurLoopCoeff.
1779 if (RunningGCD == 1)
1780 return true;
1781
1782 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
1783 if (!AddRec) {
1784 assert(isLoopInvariant(Expr, CurLoop) &&
1785 "Expected loop invariant expression");
1786 return true;
1787 }
1788
1789 assert(AddRec->isAffine() && "Unexpected Expr");
1790 const SCEV *Start = AddRec->getStart();
1791 const SCEV *Step = AddRec->getStepRecurrence(*SE);
1792 if (AddRec->getLoop() == CurLoop) {
1793 CurLoopCoeff = Step;
1794 } else {
1795 std::optional<APInt> ConstCoeff = getConstantCoefficient(Step);
1796
1797 // If the coefficient is the product of a constant and other stuff, we can
1798 // use the constant in the GCD computation.
1799 if (!ConstCoeff)
1800 return false;
1801
1802 // TODO: What happens if ConstCoeff is the "most negative" signed number
1803 // (e.g. -128 for 8 bit wide APInt)?
1804 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
1805 }
1806
1807 return accumulateCoefficientsGCD(Start, CurLoop, CurLoopCoeff, RunningGCD);
1808}
1809
1810/// Compute \p RunningGCD and return the start value of the innermost
1811/// \p SCEVAddRecExpr. In order to calculate the return value we do not
1812/// return immediately if it is proved that \p RunningGCD = 1.
1813static const SCEV *analyzeCoefficientsForGCD(const SCEV *Coefficients,
1814 APInt &RunningGCD,
1815 ScalarEvolution *SE) {
1816 while (const SCEVAddRecExpr *AddRec =
1817 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
1818 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
1819 // If the coefficient is the product of a constant and other stuff,
1820 // we can use the constant in the GCD computation.
1821 std::optional<APInt> ConstCoeff = getConstantCoefficient(Coeff);
1822 if (!ConstCoeff)
1823 return nullptr;
1824 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
1825 Coefficients = AddRec->getStart();
1826 }
1827 return Coefficients;
1828}
1829
1830//===----------------------------------------------------------------------===//
1831// gcdMIVtest -
1832// Tests an MIV subscript pair for dependence.
1833// Returns true if any possible dependence is disproved.
1834// Can sometimes disprove the equal direction for 1 or more loops,
1835// as discussed in Michael Wolfe's book,
1836// High Performance Compilers for Parallel Computing, page 235.
1837//
1838// We spend some effort (code!) to handle cases like
1839// [10*i + 5*N*j + 15*M + 6], where i and j are induction variables,
1840// but M and N are just loop-invariant variables.
1841// This should help us handle linearized subscripts;
1842// also makes this test a useful backup to the various SIV tests.
1843//
1844// It occurs to me that the presence of loop-invariant variables
1845// changes the nature of the test from "greatest common divisor"
1846// to "a common divisor".
1847bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst,
1848 FullDependence &Result) const {
1849 if (!isDependenceTestEnabled(DependenceTestType::GCDMIV))
1850 return false;
1851
1852 LLVM_DEBUG(dbgs() << "starting gcd\n");
1853 ++GCDapplications;
1854 unsigned BitWidth = SE->getTypeSizeInBits(Src->getType());
1855 APInt RunningGCD = APInt::getZero(BitWidth);
1856
1857 // Examine Src and dst coefficients.
1858 const SCEV *SrcConst = analyzeCoefficientsForGCD(Src, RunningGCD, SE);
1859 if (!SrcConst)
1860 return false;
1861 const SCEV *DstConst = analyzeCoefficientsForGCD(Dst, RunningGCD, SE);
1862 if (!DstConst)
1863 return false;
1864
1865 const SCEV *Delta = minusSCEVNoSignedOverflow(DstConst, SrcConst, *SE);
1866 if (!Delta)
1867 return false;
1868 LLVM_DEBUG(dbgs() << " Delta = " << *Delta << "\n");
1869 const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Delta);
1870 if (!Constant)
1871 return false;
1872 APInt ConstDelta = cast<SCEVConstant>(Constant)->getAPInt();
1873 LLVM_DEBUG(dbgs() << " ConstDelta = " << ConstDelta << "\n");
1874 if (ConstDelta == 0)
1875 return false;
1876 LLVM_DEBUG(dbgs() << " RunningGCD = " << RunningGCD << "\n");
1877 APInt Remainder = ConstDelta.srem(RunningGCD);
1878 if (Remainder != 0) {
1879 ++GCDindependence;
1880 return true;
1881 }
1882
1883 // Try to disprove equal directions.
1884 // For example, given a subscript pair [3*i + 2*j] and [i' + 2*j' - 1],
1885 // the code above can't disprove the dependence because the GCD = 1.
1886 // So we consider what happen if i = i' and what happens if j = j'.
1887 // If i = i', we can simplify the subscript to [2*i + 2*j] and [2*j' - 1],
1888 // which is infeasible, so we can disallow the = direction for the i level.
1889 // Setting j = j' doesn't help matters, so we end up with a direction vector
1890 // of [<>, *]
1891
1892 bool Improved = false;
1893 const SCEV *Coefficients = Src;
1894 while (const SCEVAddRecExpr *AddRec =
1895 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
1896 Coefficients = AddRec->getStart();
1897 const Loop *CurLoop = AddRec->getLoop();
1898 RunningGCD = 0;
1899 const SCEV *SrcCoeff = AddRec->getStepRecurrence(*SE);
1900 const SCEV *DstCoeff = SE->getMinusSCEV(SrcCoeff, SrcCoeff);
1901
1902 if (!accumulateCoefficientsGCD(Src, CurLoop, SrcCoeff, RunningGCD) ||
1903 !accumulateCoefficientsGCD(Dst, CurLoop, DstCoeff, RunningGCD))
1904 return false;
1905
1906 Delta = minusSCEVNoSignedOverflow(DstCoeff, SrcCoeff, *SE);
1907 if (!Delta)
1908 continue;
1909 // If the coefficient is the product of a constant and other stuff,
1910 // we can use the constant in the GCD computation.
1911 std::optional<APInt> ConstCoeff = getConstantCoefficient(Delta);
1912 if (!ConstCoeff)
1913 // The difference of the two coefficients might not be a product
1914 // or constant, in which case we give up on this direction.
1915 continue;
1916 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
1917 LLVM_DEBUG(dbgs() << "\tRunningGCD = " << RunningGCD << "\n");
1918 if (RunningGCD != 0) {
1919 Remainder = ConstDelta.srem(RunningGCD);
1920 LLVM_DEBUG(dbgs() << "\tRemainder = " << Remainder << "\n");
1921 if (Remainder != 0) {
1922 unsigned Level = mapSrcLoop(CurLoop);
1923 Result.DV[Level - 1].Direction &= ~Dependence::DVEntry::EQ;
1924 Improved = true;
1925 }
1926 }
1927 }
1928 if (Improved)
1929 ++GCDsuccesses;
1930 LLVM_DEBUG(dbgs() << "all done\n");
1931 return false;
1932}
1933
1934//===----------------------------------------------------------------------===//
1935// banerjeeMIVtest -
1936// Use Banerjee's Inequalities to test an MIV subscript pair.
1937// (Wolfe, in the race-car book, calls this the Extreme Value Test.)
1938// Generally follows the discussion in Section 2.5.2 of
1939//
1940// Optimizing Supercompilers for Supercomputers
1941// Michael Wolfe
1942//
1943// The inequalities given on page 25 are simplified in that loops are
1944// normalized so that the lower bound is always 0 and the stride is always 1.
1945// For example, Wolfe gives
1946//
1947// LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
1948//
1949// where A_k is the coefficient of the kth index in the source subscript,
1950// B_k is the coefficient of the kth index in the destination subscript,
1951// U_k is the upper bound of the kth index, L_k is the lower bound of the Kth
1952// index, and N_k is the stride of the kth index. Since all loops are normalized
1953// by the SCEV package, N_k = 1 and L_k = 0, allowing us to simplify the
1954// equation to
1955//
1956// LB^<_k = (A^-_k - B_k)^- (U_k - 0 - 1) + (A_k - B_k)0 - B_k 1
1957// = (A^-_k - B_k)^- (U_k - 1) - B_k
1958//
1959// Similar simplifications are possible for the other equations.
1960//
1961// When we can't determine the number of iterations for a loop,
1962// we use NULL as an indicator for the worst case, infinity.
1963// When computing the upper bound, NULL denotes +inf;
1964// for the lower bound, NULL denotes -inf.
1965//
1966// Return true if dependence disproved.
1967bool DependenceInfo::banerjeeMIVtest(const SCEV *Src, const SCEV *Dst,
1968 const SmallBitVector &Loops,
1969 FullDependence &Result) const {
1970 if (!isDependenceTestEnabled(DependenceTestType::BanerjeeMIV))
1971 return false;
1972
1973 LLVM_DEBUG(dbgs() << "starting Banerjee\n");
1974 ++BanerjeeApplications;
1975 LLVM_DEBUG(dbgs() << " Src = " << *Src << '\n');
1976 const SCEV *A0;
1978 collectCoeffInfo(Src, true, A0, A);
1979 LLVM_DEBUG(dbgs() << " Dst = " << *Dst << '\n');
1980 const SCEV *B0;
1982 collectCoeffInfo(Dst, false, B0, B);
1983 SmallVector<BoundInfo, 4> Bound(MaxLevels + 1);
1984 const SCEV *Delta = minusSCEVNoSignedOverflow(B0, A0, *SE);
1985 if (!Delta)
1986 return false;
1987 LLVM_DEBUG(dbgs() << "\tDelta = " << *Delta << '\n');
1988
1989 // Compute bounds for all the * directions.
1990 LLVM_DEBUG(dbgs() << "\tBounds[*]\n");
1991 for (unsigned K = 1; K <= MaxLevels; ++K) {
1992 Bound[K].Iterations = A[K].Iterations ? A[K].Iterations : B[K].Iterations;
1993 Bound[K].Direction = Dependence::DVEntry::ALL;
1994 Bound[K].DirSet = Dependence::DVEntry::NONE;
1995 findBoundsALL(A, B, Bound, K);
1996#ifndef NDEBUG
1997 LLVM_DEBUG(dbgs() << "\t " << K << '\t');
1998 if (Bound[K].Lower[Dependence::DVEntry::ALL])
1999 LLVM_DEBUG(dbgs() << *Bound[K].Lower[Dependence::DVEntry::ALL] << '\t');
2000 else
2001 LLVM_DEBUG(dbgs() << "-inf\t");
2002 if (Bound[K].Upper[Dependence::DVEntry::ALL])
2003 LLVM_DEBUG(dbgs() << *Bound[K].Upper[Dependence::DVEntry::ALL] << '\n');
2004 else
2005 LLVM_DEBUG(dbgs() << "+inf\n");
2006#endif
2007 }
2008
2009 // Test the *, *, *, ... case.
2010 bool Disproved = false;
2011 if (testBounds(Dependence::DVEntry::ALL, 0, Bound, Delta)) {
2012 // Explore the direction vector hierarchy.
2013 unsigned DepthExpanded = 0;
2014 unsigned NewDeps =
2015 exploreDirections(1, A, B, Bound, Loops, DepthExpanded, Delta);
2016 if (NewDeps > 0) {
2017 bool Improved = false;
2018 for (unsigned K = 1; K <= CommonLevels; ++K) {
2019 if (Loops[K]) {
2020 unsigned Old = Result.DV[K - 1].Direction;
2021 Result.DV[K - 1].Direction = Old & Bound[K].DirSet;
2022 Improved |= Old != Result.DV[K - 1].Direction;
2023 if (!Result.DV[K - 1].Direction) {
2024 Improved = false;
2025 Disproved = true;
2026 break;
2027 }
2028 }
2029 }
2030 if (Improved)
2031 ++BanerjeeSuccesses;
2032 } else {
2033 ++BanerjeeIndependence;
2034 Disproved = true;
2035 }
2036 } else {
2037 ++BanerjeeIndependence;
2038 Disproved = true;
2039 }
2040 return Disproved;
2041}
2042
2043// Hierarchically expands the direction vector
2044// search space, combining the directions of discovered dependences
2045// in the DirSet field of Bound. Returns the number of distinct
2046// dependences discovered. If the dependence is disproved,
2047// it will return 0.
2048unsigned DependenceInfo::exploreDirections(
2051 unsigned &DepthExpanded, const SCEV *Delta) const {
2052 // This algorithm has worst case complexity of O(3^n), where 'n' is the number
2053 // of common loop levels. To avoid excessive compile-time, pessimize all the
2054 // results and immediately return when the number of common levels is beyond
2055 // the given threshold.
2056 if (CommonLevels > MIVMaxLevelThreshold) {
2057 LLVM_DEBUG(dbgs() << "Number of common levels exceeded the threshold. MIV "
2058 "direction exploration is terminated.\n");
2059 for (unsigned K = 1; K <= CommonLevels; ++K)
2060 if (Loops[K])
2061 Bound[K].DirSet = Dependence::DVEntry::ALL;
2062 return 1;
2063 }
2064
2065 if (Level > CommonLevels) {
2066 // record result
2067 LLVM_DEBUG(dbgs() << "\t[");
2068 for (unsigned K = 1; K <= CommonLevels; ++K) {
2069 if (Loops[K]) {
2070 Bound[K].DirSet |= Bound[K].Direction;
2071#ifndef NDEBUG
2072 switch (Bound[K].Direction) {
2074 LLVM_DEBUG(dbgs() << " <");
2075 break;
2077 LLVM_DEBUG(dbgs() << " =");
2078 break;
2080 LLVM_DEBUG(dbgs() << " >");
2081 break;
2083 LLVM_DEBUG(dbgs() << " *");
2084 break;
2085 default:
2086 llvm_unreachable("unexpected Bound[K].Direction");
2087 }
2088#endif
2089 }
2090 }
2091 LLVM_DEBUG(dbgs() << " ]\n");
2092 return 1;
2093 }
2094 if (Loops[Level]) {
2095 if (Level > DepthExpanded) {
2096 DepthExpanded = Level;
2097 // compute bounds for <, =, > at current level
2098 findBoundsLT(A, B, Bound, Level);
2099 findBoundsGT(A, B, Bound, Level);
2100 findBoundsEQ(A, B, Bound, Level);
2101#ifndef NDEBUG
2102 LLVM_DEBUG(dbgs() << "\tBound for level = " << Level << '\n');
2103 LLVM_DEBUG(dbgs() << "\t <\t");
2104 if (Bound[Level].Lower[Dependence::DVEntry::LT])
2105 LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::LT]
2106 << '\t');
2107 else
2108 LLVM_DEBUG(dbgs() << "-inf\t");
2109 if (Bound[Level].Upper[Dependence::DVEntry::LT])
2110 LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::LT]
2111 << '\n');
2112 else
2113 LLVM_DEBUG(dbgs() << "+inf\n");
2114 LLVM_DEBUG(dbgs() << "\t =\t");
2115 if (Bound[Level].Lower[Dependence::DVEntry::EQ])
2116 LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::EQ]
2117 << '\t');
2118 else
2119 LLVM_DEBUG(dbgs() << "-inf\t");
2120 if (Bound[Level].Upper[Dependence::DVEntry::EQ])
2121 LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::EQ]
2122 << '\n');
2123 else
2124 LLVM_DEBUG(dbgs() << "+inf\n");
2125 LLVM_DEBUG(dbgs() << "\t >\t");
2126 if (Bound[Level].Lower[Dependence::DVEntry::GT])
2127 LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::GT]
2128 << '\t');
2129 else
2130 LLVM_DEBUG(dbgs() << "-inf\t");
2131 if (Bound[Level].Upper[Dependence::DVEntry::GT])
2132 LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::GT]
2133 << '\n');
2134 else
2135 LLVM_DEBUG(dbgs() << "+inf\n");
2136#endif
2137 }
2138
2139 unsigned NewDeps = 0;
2140
2141 // test bounds for <, *, *, ...
2142 if (testBounds(Dependence::DVEntry::LT, Level, Bound, Delta))
2143 NewDeps += exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
2144 Delta);
2145
2146 // Test bounds for =, *, *, ...
2147 if (testBounds(Dependence::DVEntry::EQ, Level, Bound, Delta))
2148 NewDeps += exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
2149 Delta);
2150
2151 // test bounds for >, *, *, ...
2152 if (testBounds(Dependence::DVEntry::GT, Level, Bound, Delta))
2153 NewDeps += exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
2154 Delta);
2155
2156 Bound[Level].Direction = Dependence::DVEntry::ALL;
2157 return NewDeps;
2158 } else
2159 return exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
2160 Delta);
2161}
2162
2163// Returns true iff the current bounds are plausible.
2164bool DependenceInfo::testBounds(unsigned char DirKind, unsigned Level,
2166 const SCEV *Delta) const {
2167 Bound[Level].Direction = DirKind;
2168 if (const SCEV *LowerBound = getLowerBound(Bound))
2169 if (SE->isKnownPredicate(CmpInst::ICMP_SGT, LowerBound, Delta))
2170 return false;
2171 if (const SCEV *UpperBound = getUpperBound(Bound))
2172 if (SE->isKnownPredicate(CmpInst::ICMP_SGT, Delta, UpperBound))
2173 return false;
2174 return true;
2175}
2176
2177// Computes the upper and lower bounds for level K
2178// using the * direction. Records them in Bound.
2179// Wolfe gives the equations
2180//
2181// LB^*_k = (A^-_k - B^+_k)(U_k - L_k) + (A_k - B_k)L_k
2182// UB^*_k = (A^+_k - B^-_k)(U_k - L_k) + (A_k - B_k)L_k
2183//
2184// Since we normalize loops, we can simplify these equations to
2185//
2186// LB^*_k = (A^-_k - B^+_k)U_k
2187// UB^*_k = (A^+_k - B^-_k)U_k
2188//
2189// We must be careful to handle the case where the upper bound is unknown.
2190// Note that the lower bound is always <= 0
2191// and the upper bound is always >= 0.
2192void DependenceInfo::findBoundsALL(ArrayRef<CoefficientInfo> A,
2195 unsigned K) const {
2196 Bound[K].Lower[Dependence::DVEntry::ALL] =
2197 nullptr; // Default value = -infinity.
2198 Bound[K].Upper[Dependence::DVEntry::ALL] =
2199 nullptr; // Default value = +infinity.
2200 if (Bound[K].Iterations) {
2201 Bound[K].Lower[Dependence::DVEntry::ALL] = SE->getMulExpr(
2202 SE->getMinusSCEV(A[K].NegPart, B[K].PosPart), Bound[K].Iterations);
2203 Bound[K].Upper[Dependence::DVEntry::ALL] = SE->getMulExpr(
2204 SE->getMinusSCEV(A[K].PosPart, B[K].NegPart), Bound[K].Iterations);
2205 } else {
2206 // If the difference is 0, we won't need to know the number of iterations.
2207 if (SE->isKnownPredicate(CmpInst::ICMP_EQ, A[K].NegPart, B[K].PosPart))
2208 Bound[K].Lower[Dependence::DVEntry::ALL] =
2209 SE->getZero(A[K].Coeff->getType());
2210 if (SE->isKnownPredicate(CmpInst::ICMP_EQ, A[K].PosPart, B[K].NegPart))
2211 Bound[K].Upper[Dependence::DVEntry::ALL] =
2212 SE->getZero(A[K].Coeff->getType());
2213 }
2214}
2215
2216// Computes the upper and lower bounds for level K
2217// using the = direction. Records them in Bound.
2218// Wolfe gives the equations
2219//
2220// LB^=_k = (A_k - B_k)^- (U_k - L_k) + (A_k - B_k)L_k
2221// UB^=_k = (A_k - B_k)^+ (U_k - L_k) + (A_k - B_k)L_k
2222//
2223// Since we normalize loops, we can simplify these equations to
2224//
2225// LB^=_k = (A_k - B_k)^- U_k
2226// UB^=_k = (A_k - B_k)^+ U_k
2227//
2228// We must be careful to handle the case where the upper bound is unknown.
2229// Note that the lower bound is always <= 0
2230// and the upper bound is always >= 0.
2231void DependenceInfo::findBoundsEQ(ArrayRef<CoefficientInfo> A,
2234 unsigned K) const {
2235 Bound[K].Lower[Dependence::DVEntry::EQ] =
2236 nullptr; // Default value = -infinity.
2237 Bound[K].Upper[Dependence::DVEntry::EQ] =
2238 nullptr; // Default value = +infinity.
2239 if (Bound[K].Iterations) {
2240 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
2241 const SCEV *NegativePart = getNegativePart(Delta);
2242 Bound[K].Lower[Dependence::DVEntry::EQ] =
2243 SE->getMulExpr(NegativePart, Bound[K].Iterations);
2244 const SCEV *PositivePart = getPositivePart(Delta);
2245 Bound[K].Upper[Dependence::DVEntry::EQ] =
2246 SE->getMulExpr(PositivePart, Bound[K].Iterations);
2247 } else {
2248 // If the positive/negative part of the difference is 0,
2249 // we won't need to know the number of iterations.
2250 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
2251 const SCEV *NegativePart = getNegativePart(Delta);
2252 if (NegativePart->isZero())
2253 Bound[K].Lower[Dependence::DVEntry::EQ] = NegativePart; // Zero
2254 const SCEV *PositivePart = getPositivePart(Delta);
2255 if (PositivePart->isZero())
2256 Bound[K].Upper[Dependence::DVEntry::EQ] = PositivePart; // Zero
2257 }
2258}
2259
2260// Computes the upper and lower bounds for level K
2261// using the < direction. Records them in Bound.
2262// Wolfe gives the equations
2263//
2264// LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2265// UB^<_k = (A^+_k - B_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2266//
2267// Since we normalize loops, we can simplify these equations to
2268//
2269// LB^<_k = (A^-_k - B_k)^- (U_k - 1) - B_k
2270// UB^<_k = (A^+_k - B_k)^+ (U_k - 1) - B_k
2271//
2272// We must be careful to handle the case where the upper bound is unknown.
2273void DependenceInfo::findBoundsLT(ArrayRef<CoefficientInfo> A,
2276 unsigned K) const {
2277 Bound[K].Lower[Dependence::DVEntry::LT] =
2278 nullptr; // Default value = -infinity.
2279 Bound[K].Upper[Dependence::DVEntry::LT] =
2280 nullptr; // Default value = +infinity.
2281 if (Bound[K].Iterations) {
2282 const SCEV *Iter_1 = SE->getMinusSCEV(
2283 Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
2284 const SCEV *NegPart =
2285 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
2286 Bound[K].Lower[Dependence::DVEntry::LT] =
2287 SE->getMinusSCEV(SE->getMulExpr(NegPart, Iter_1), B[K].Coeff);
2288 const SCEV *PosPart =
2289 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
2290 Bound[K].Upper[Dependence::DVEntry::LT] =
2291 SE->getMinusSCEV(SE->getMulExpr(PosPart, Iter_1), B[K].Coeff);
2292 } else {
2293 // If the positive/negative part of the difference is 0,
2294 // we won't need to know the number of iterations.
2295 const SCEV *NegPart =
2296 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
2297 if (NegPart->isZero())
2298 Bound[K].Lower[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
2299 const SCEV *PosPart =
2300 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
2301 if (PosPart->isZero())
2302 Bound[K].Upper[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
2303 }
2304}
2305
2306// Computes the upper and lower bounds for level K
2307// using the > direction. Records them in Bound.
2308// Wolfe gives the equations
2309//
2310// LB^>_k = (A_k - B^+_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
2311// UB^>_k = (A_k - B^-_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
2312//
2313// Since we normalize loops, we can simplify these equations to
2314//
2315// LB^>_k = (A_k - B^+_k)^- (U_k - 1) + A_k
2316// UB^>_k = (A_k - B^-_k)^+ (U_k - 1) + A_k
2317//
2318// We must be careful to handle the case where the upper bound is unknown.
2319void DependenceInfo::findBoundsGT(ArrayRef<CoefficientInfo> A,
2322 unsigned K) const {
2323 Bound[K].Lower[Dependence::DVEntry::GT] =
2324 nullptr; // Default value = -infinity.
2325 Bound[K].Upper[Dependence::DVEntry::GT] =
2326 nullptr; // Default value = +infinity.
2327 if (Bound[K].Iterations) {
2328 const SCEV *Iter_1 = SE->getMinusSCEV(
2329 Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
2330 const SCEV *NegPart =
2331 getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
2332 Bound[K].Lower[Dependence::DVEntry::GT] =
2333 SE->getAddExpr(SE->getMulExpr(NegPart, Iter_1), A[K].Coeff);
2334 const SCEV *PosPart =
2335 getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
2336 Bound[K].Upper[Dependence::DVEntry::GT] =
2337 SE->getAddExpr(SE->getMulExpr(PosPart, Iter_1), A[K].Coeff);
2338 } else {
2339 // If the positive/negative part of the difference is 0,
2340 // we won't need to know the number of iterations.
2341 const SCEV *NegPart =
2342 getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
2343 if (NegPart->isZero())
2344 Bound[K].Lower[Dependence::DVEntry::GT] = A[K].Coeff;
2345 const SCEV *PosPart =
2346 getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
2347 if (PosPart->isZero())
2348 Bound[K].Upper[Dependence::DVEntry::GT] = A[K].Coeff;
2349 }
2350}
2351
2352// X^+ = max(X, 0)
2353const SCEV *DependenceInfo::getPositivePart(const SCEV *X) const {
2354 return SE->getSMaxExpr(X, SE->getZero(X->getType()));
2355}
2356
2357// X^- = min(X, 0)
2358const SCEV *DependenceInfo::getNegativePart(const SCEV *X) const {
2359 return SE->getSMinExpr(X, SE->getZero(X->getType()));
2360}
2361
2362// Walks through the subscript,
2363// collecting each coefficient, the associated loop bounds,
2364// and recording its positive and negative parts for later use.
2365void DependenceInfo::collectCoeffInfo(
2366 const SCEV *Subscript, bool SrcFlag, const SCEV *&Constant,
2368 const SCEV *Zero = SE->getZero(Subscript->getType());
2369 CI.resize(MaxLevels + 1);
2370 for (unsigned K = 1; K <= MaxLevels; ++K) {
2371 CI[K].Coeff = Zero;
2372 CI[K].PosPart = Zero;
2373 CI[K].NegPart = Zero;
2374 CI[K].Iterations = nullptr;
2375 }
2376 while (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Subscript)) {
2377 const Loop *L = AddRec->getLoop();
2378 unsigned K = SrcFlag ? mapSrcLoop(L) : mapDstLoop(L);
2379 CI[K].Coeff = AddRec->getStepRecurrence(*SE);
2380 CI[K].PosPart = getPositivePart(CI[K].Coeff);
2381 CI[K].NegPart = getNegativePart(CI[K].Coeff);
2382 CI[K].Iterations = collectUpperBound(L, Subscript->getType());
2383 Subscript = AddRec->getStart();
2384 }
2385 Constant = Subscript;
2386#ifndef NDEBUG
2387 LLVM_DEBUG(dbgs() << "\tCoefficient Info\n");
2388 for (unsigned K = 1; K <= MaxLevels; ++K) {
2389 LLVM_DEBUG(dbgs() << "\t " << K << "\t" << *CI[K].Coeff);
2390 LLVM_DEBUG(dbgs() << "\tPos Part = ");
2391 LLVM_DEBUG(dbgs() << *CI[K].PosPart);
2392 LLVM_DEBUG(dbgs() << "\tNeg Part = ");
2393 LLVM_DEBUG(dbgs() << *CI[K].NegPart);
2394 LLVM_DEBUG(dbgs() << "\tUpper Bound = ");
2395 if (CI[K].Iterations)
2396 LLVM_DEBUG(dbgs() << *CI[K].Iterations);
2397 else
2398 LLVM_DEBUG(dbgs() << "+inf");
2399 LLVM_DEBUG(dbgs() << '\n');
2400 }
2401 LLVM_DEBUG(dbgs() << "\t Constant = " << *Subscript << '\n');
2402#endif
2403}
2404
2405// Looks through all the bounds info and
2406// computes the lower bound given the current direction settings
2407// at each level. If the lower bound for any level is -inf,
2408// the result is -inf.
2409const SCEV *DependenceInfo::getLowerBound(ArrayRef<BoundInfo> Bound) const {
2410 const SCEV *Sum = Bound[1].Lower[Bound[1].Direction];
2411 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
2412 if (Bound[K].Lower[Bound[K].Direction])
2413 Sum = SE->getAddExpr(Sum, Bound[K].Lower[Bound[K].Direction]);
2414 else
2415 Sum = nullptr;
2416 }
2417 return Sum;
2418}
2419
2420// Looks through all the bounds info and
2421// computes the upper bound given the current direction settings
2422// at each level. If the upper bound at any level is +inf,
2423// the result is +inf.
2424const SCEV *DependenceInfo::getUpperBound(ArrayRef<BoundInfo> Bound) const {
2425 const SCEV *Sum = Bound[1].Upper[Bound[1].Direction];
2426 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
2427 if (Bound[K].Upper[Bound[K].Direction])
2428 Sum = SE->getAddExpr(Sum, Bound[K].Upper[Bound[K].Direction]);
2429 else
2430 Sum = nullptr;
2431 }
2432 return Sum;
2433}
2434
2435/// Check if we can delinearize the subscripts. If the SCEVs representing the
2436/// source and destination array references are recurrences on a nested loop,
2437/// this function flattens the nested recurrences into separate recurrences
2438/// for each loop level.
2439bool DependenceInfo::tryDelinearize(Instruction *Src, Instruction *Dst,
2441 assert(isLoadOrStore(Src) && "instruction is not load or store");
2442 assert(isLoadOrStore(Dst) && "instruction is not load or store");
2443 Value *SrcPtr = getLoadStorePointerOperand(Src);
2444 Value *DstPtr = getLoadStorePointerOperand(Dst);
2445 Loop *SrcLoop = LI->getLoopFor(Src->getParent());
2446 Loop *DstLoop = LI->getLoopFor(Dst->getParent());
2447 const SCEV *SrcAccessFn = SE->getSCEVAtScope(SrcPtr, SrcLoop);
2448 const SCEV *DstAccessFn = SE->getSCEVAtScope(DstPtr, DstLoop);
2449 const SCEVUnknown *SrcBase =
2450 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
2451 const SCEVUnknown *DstBase =
2452 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
2453
2454 if (!SrcBase || !DstBase || SrcBase != DstBase)
2455 return false;
2456
2457 SmallVector<const SCEV *, 4> SrcSubscripts, DstSubscripts;
2458
2459 if (!tryDelinearizeFixedSize(Src, Dst, SrcAccessFn, DstAccessFn,
2460 SrcSubscripts, DstSubscripts) &&
2461 !tryDelinearizeParametricSize(Src, Dst, SrcAccessFn, DstAccessFn,
2462 SrcSubscripts, DstSubscripts))
2463 return false;
2464
2465 assert(isLoopInvariant(SrcBase, SrcLoop) &&
2466 isLoopInvariant(DstBase, DstLoop) &&
2467 "Expected SrcBase and DstBase to be loop invariant");
2468
2469 int Size = SrcSubscripts.size();
2470 LLVM_DEBUG({
2471 dbgs() << "\nSrcSubscripts: ";
2472 for (int I = 0; I < Size; I++)
2473 dbgs() << *SrcSubscripts[I];
2474 dbgs() << "\nDstSubscripts: ";
2475 for (int I = 0; I < Size; I++)
2476 dbgs() << *DstSubscripts[I];
2477 dbgs() << "\n";
2478 });
2479
2480 // The delinearization transforms a single-subscript MIV dependence test into
2481 // a multi-subscript SIV dependence test that is easier to compute. So we
2482 // resize Pair to contain as many pairs of subscripts as the delinearization
2483 // has found, and then initialize the pairs following the delinearization.
2484 Pair.resize(Size);
2485 for (int I = 0; I < Size; ++I) {
2486 Pair[I].Src = SrcSubscripts[I];
2487 Pair[I].Dst = DstSubscripts[I];
2488
2489 assert(Pair[I].Src->getType() == Pair[I].Dst->getType() &&
2490 "Unexpected different types for the subscripts");
2491 }
2492
2493 return true;
2494}
2495
2496/// Try to delinearize \p SrcAccessFn and \p DstAccessFn if the underlying
2497/// arrays accessed are fixed-size arrays. Return true if delinearization was
2498/// successful.
2499bool DependenceInfo::tryDelinearizeFixedSize(
2500 Instruction *Src, Instruction *Dst, const SCEV *SrcAccessFn,
2501 const SCEV *DstAccessFn, SmallVectorImpl<const SCEV *> &SrcSubscripts,
2502 SmallVectorImpl<const SCEV *> &DstSubscripts) {
2503 LLVM_DEBUG({
2504 const SCEVUnknown *SrcBase =
2505 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
2506 const SCEVUnknown *DstBase =
2507 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
2508 assert(SrcBase && DstBase && SrcBase == DstBase &&
2509 "expected src and dst scev unknowns to be equal");
2510 });
2511
2512 const SCEV *ElemSize = SE->getElementSize(Src);
2513 assert(ElemSize == SE->getElementSize(Dst) && "Different element sizes");
2514 SmallVector<const SCEV *, 4> SrcSizes, DstSizes;
2515 if (!delinearizeFixedSizeArray(*SE, SE->removePointerBase(SrcAccessFn),
2516 SrcSubscripts, SrcSizes, ElemSize) ||
2517 !delinearizeFixedSizeArray(*SE, SE->removePointerBase(DstAccessFn),
2518 DstSubscripts, DstSizes, ElemSize))
2519 return false;
2520
2521 // Check that the two size arrays are non-empty and equal in length and
2522 // value. SCEV expressions are uniqued, so we can compare pointers.
2523 if (SrcSizes.size() != DstSizes.size() ||
2524 !std::equal(SrcSizes.begin(), SrcSizes.end(), DstSizes.begin())) {
2525 SrcSubscripts.clear();
2526 DstSubscripts.clear();
2527 return false;
2528 }
2529
2530 assert(SrcSubscripts.size() == DstSubscripts.size() &&
2531 "Expected equal number of entries in the list of SrcSubscripts and "
2532 "DstSubscripts.");
2533
2534 // In general we cannot safely assume that the subscripts recovered from GEPs
2535 // are in the range of values defined for their corresponding array
2536 // dimensions. For example some C language usage/interpretation make it
2537 // impossible to verify this at compile-time. As such we can only delinearize
2538 // iff the subscripts are positive and are less than the range of the
2539 // dimension.
2541 if (!validateDelinearizationResult(*SE, SrcSizes, SrcSubscripts) ||
2542 !validateDelinearizationResult(*SE, DstSizes, DstSubscripts)) {
2543 SrcSubscripts.clear();
2544 DstSubscripts.clear();
2545 return false;
2546 }
2547 }
2548 LLVM_DEBUG({
2549 dbgs() << "Delinearized subscripts of fixed-size array\n"
2550 << "SrcGEP:" << *getLoadStorePointerOperand(Src) << "\n"
2551 << "DstGEP:" << *getLoadStorePointerOperand(Dst) << "\n";
2552 });
2553 return true;
2554}
2555
2556bool DependenceInfo::tryDelinearizeParametricSize(
2557 Instruction *Src, Instruction *Dst, const SCEV *SrcAccessFn,
2558 const SCEV *DstAccessFn, SmallVectorImpl<const SCEV *> &SrcSubscripts,
2559 SmallVectorImpl<const SCEV *> &DstSubscripts) {
2560
2561 const SCEVUnknown *SrcBase =
2562 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
2563 const SCEVUnknown *DstBase =
2564 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
2565 assert(SrcBase && DstBase && SrcBase == DstBase &&
2566 "expected src and dst scev unknowns to be equal");
2567
2568 const SCEV *ElementSize = SE->getElementSize(Src);
2569 if (ElementSize != SE->getElementSize(Dst))
2570 return false;
2571
2572 const SCEV *SrcSCEV = SE->getMinusSCEV(SrcAccessFn, SrcBase);
2573 const SCEV *DstSCEV = SE->getMinusSCEV(DstAccessFn, DstBase);
2574
2575 const SCEVAddRecExpr *SrcAR = dyn_cast<SCEVAddRecExpr>(SrcSCEV);
2576 const SCEVAddRecExpr *DstAR = dyn_cast<SCEVAddRecExpr>(DstSCEV);
2577 if (!SrcAR || !DstAR || !SrcAR->isAffine() || !DstAR->isAffine())
2578 return false;
2579
2580 // First step: collect parametric terms in both array references.
2582 collectParametricTerms(*SE, SrcAR, Terms);
2583 collectParametricTerms(*SE, DstAR, Terms);
2584
2585 // Second step: find subscript sizes.
2587 findArrayDimensions(*SE, Terms, Sizes, ElementSize);
2588
2589 // Third step: compute the access functions for each subscript.
2590 computeAccessFunctions(*SE, SrcAR, SrcSubscripts, Sizes);
2591 computeAccessFunctions(*SE, DstAR, DstSubscripts, Sizes);
2592
2593 // Fail when there is only a subscript: that's a linearized access function.
2594 if (SrcSubscripts.size() < 2 || DstSubscripts.size() < 2 ||
2595 SrcSubscripts.size() != DstSubscripts.size())
2596 return false;
2597
2598 // Statically check that the array bounds are in-range. The first subscript we
2599 // don't have a size for and it cannot overflow into another subscript, so is
2600 // always safe. The others need to be 0 <= subscript[i] < bound, for both src
2601 // and dst.
2602 // FIXME: It may be better to record these sizes and add them as constraints
2603 // to the dependency checks.
2605 if (!validateDelinearizationResult(*SE, Sizes, SrcSubscripts) ||
2606 !validateDelinearizationResult(*SE, Sizes, DstSubscripts))
2607 return false;
2608
2609 return true;
2610}
2611
2612//===----------------------------------------------------------------------===//
2613
2614#ifndef NDEBUG
2615// For debugging purposes, dump a small bit vector to dbgs().
2617 dbgs() << "{";
2618 for (unsigned VI : BV.set_bits()) {
2619 dbgs() << VI;
2620 if (BV.find_next(VI) >= 0)
2621 dbgs() << ' ';
2622 }
2623 dbgs() << "}\n";
2624}
2625#endif
2626
2628 FunctionAnalysisManager::Invalidator &Inv) {
2629 // Check if the analysis itself has been invalidated.
2630 auto PAC = PA.getChecker<DependenceAnalysis>();
2631 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>())
2632 return true;
2633
2634 // Check transitive dependencies.
2635 return Inv.invalidate<AAManager>(F, PA) ||
2636 Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) ||
2637 Inv.invalidate<LoopAnalysis>(F, PA);
2638}
2639
2640// depends -
2641// Returns NULL if there is no dependence.
2642// Otherwise, return a Dependence with as many details as possible.
2643// Corresponds to Section 3.1 in the paper
2644//
2645// Practical Dependence Testing
2646// Goff, Kennedy, Tseng
2647// PLDI 1991
2648//
2649std::unique_ptr<Dependence>
2651 bool UnderRuntimeAssumptions) {
2653 bool PossiblyLoopIndependent = true;
2654 if (Src == Dst)
2655 PossiblyLoopIndependent = false;
2656
2657 if (!(Src->mayReadOrWriteMemory() && Dst->mayReadOrWriteMemory()))
2658 // if both instructions don't reference memory, there's no dependence
2659 return nullptr;
2660
2661 if (!isLoadOrStore(Src) || !isLoadOrStore(Dst)) {
2662 // can only analyze simple loads and stores, i.e., no calls, invokes, etc.
2663 LLVM_DEBUG(dbgs() << "can only handle simple loads and stores\n");
2664 return std::make_unique<Dependence>(Src, Dst,
2665 SCEVUnionPredicate(Assume, *SE));
2666 }
2667
2668 const MemoryLocation &DstLoc = MemoryLocation::get(Dst);
2669 const MemoryLocation &SrcLoc = MemoryLocation::get(Src);
2670
2671 switch (underlyingObjectsAlias(AA, F->getDataLayout(), DstLoc, SrcLoc)) {
2674 // cannot analyse objects if we don't understand their aliasing.
2675 LLVM_DEBUG(dbgs() << "can't analyze may or partial alias\n");
2676 return std::make_unique<Dependence>(Src, Dst,
2677 SCEVUnionPredicate(Assume, *SE));
2679 // If the objects noalias, they are distinct, accesses are independent.
2680 LLVM_DEBUG(dbgs() << "no alias\n");
2681 return nullptr;
2683 break; // The underlying objects alias; test accesses for dependence.
2684 }
2685
2686 if (DstLoc.Size != SrcLoc.Size || !DstLoc.Size.isPrecise() ||
2687 !SrcLoc.Size.isPrecise()) {
2688 // The dependence test gets confused if the size of the memory accesses
2689 // differ.
2690 LLVM_DEBUG(dbgs() << "can't analyze must alias with different sizes\n");
2691 return std::make_unique<Dependence>(Src, Dst,
2692 SCEVUnionPredicate(Assume, *SE));
2693 }
2694
2695 Value *SrcPtr = getLoadStorePointerOperand(Src);
2696 Value *DstPtr = getLoadStorePointerOperand(Dst);
2697 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
2698 const SCEV *DstSCEV = SE->getSCEV(DstPtr);
2699 LLVM_DEBUG(dbgs() << " SrcSCEV = " << *SrcSCEV << "\n");
2700 LLVM_DEBUG(dbgs() << " DstSCEV = " << *DstSCEV << "\n");
2701 const SCEV *SrcBase = SE->getPointerBase(SrcSCEV);
2702 const SCEV *DstBase = SE->getPointerBase(DstSCEV);
2703 if (SrcBase != DstBase) {
2704 // If two pointers have different bases, trying to analyze indexes won't
2705 // work; we can't compare them to each other. This can happen, for example,
2706 // if one is produced by an LCSSA PHI node.
2707 //
2708 // We check this upfront so we don't crash in cases where getMinusSCEV()
2709 // returns a SCEVCouldNotCompute.
2710 LLVM_DEBUG(dbgs() << "can't analyze SCEV with different pointer base\n");
2711 return std::make_unique<Dependence>(Src, Dst,
2712 SCEVUnionPredicate(Assume, *SE));
2713 }
2714
2715 // Even if the base pointers are the same, they may not be loop-invariant. It
2716 // could lead to incorrect results, as we're analyzing loop-carried
2717 // dependencies. Src and Dst can be in different loops, so we need to check
2718 // the base pointer is invariant in both loops.
2719 Loop *SrcLoop = LI->getLoopFor(Src->getParent());
2720 Loop *DstLoop = LI->getLoopFor(Dst->getParent());
2721 if (!isLoopInvariant(SrcBase, SrcLoop) ||
2722 !isLoopInvariant(DstBase, DstLoop)) {
2723 LLVM_DEBUG(dbgs() << "The base pointer is not loop invariant.\n");
2724 return std::make_unique<Dependence>(Src, Dst,
2725 SCEVUnionPredicate(Assume, *SE));
2726 }
2727
2728 uint64_t EltSize = SrcLoc.Size.toRaw();
2729 const SCEV *SrcEv = SE->getMinusSCEV(SrcSCEV, SrcBase);
2730 const SCEV *DstEv = SE->getMinusSCEV(DstSCEV, DstBase);
2731
2732 // Check that memory access offsets are multiples of element sizes.
2733 if (!SE->isKnownMultipleOf(SrcEv, EltSize, Assume) ||
2734 !SE->isKnownMultipleOf(DstEv, EltSize, Assume)) {
2735 LLVM_DEBUG(dbgs() << "can't analyze SCEV with different offsets\n");
2736 return std::make_unique<Dependence>(Src, Dst,
2737 SCEVUnionPredicate(Assume, *SE));
2738 }
2739
2740 // Runtime assumptions needed but not allowed.
2741 if (!Assume.empty() && !UnderRuntimeAssumptions)
2742 return std::make_unique<Dependence>(Src, Dst,
2743 SCEVUnionPredicate(Assume, *SE));
2744
2745 unsigned Pairs = 1;
2746 SmallVector<Subscript, 2> Pair(Pairs);
2747 Pair[0].Src = SrcEv;
2748 Pair[0].Dst = DstEv;
2749 if (Delinearize) {
2750 if (tryDelinearize(Src, Dst, Pair)) {
2751 LLVM_DEBUG(dbgs() << " delinearized\n");
2752 Pairs = Pair.size();
2753 }
2754 }
2755
2756 // Establish loop nesting levels considering SameSD loops as common
2757 establishNestingLevels(Src, Dst);
2758
2759 LLVM_DEBUG(dbgs() << " common nesting levels = " << CommonLevels << "\n");
2760 LLVM_DEBUG(dbgs() << " maximum nesting levels = " << MaxLevels << "\n");
2761 LLVM_DEBUG(dbgs() << " SameSD nesting levels = " << SameSDLevels << "\n");
2762
2763 // Modify common levels to consider the SameSD levels in the tests
2764 CommonLevels += SameSDLevels;
2765 MaxLevels -= SameSDLevels;
2766 if (SameSDLevels > 0) {
2767 // Not all tests are handled yet over SameSD loops
2768 // Revoke if there are any tests other than ZIV, SIV or RDIV
2769 for (unsigned P = 0; P < Pairs; ++P) {
2771 Subscript::ClassificationKind TestClass =
2772 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()),
2773 Pair[P].Dst, LI->getLoopFor(Dst->getParent()), Loops);
2774
2775 if (TestClass != Subscript::ZIV && TestClass != Subscript::SIV &&
2776 TestClass != Subscript::RDIV) {
2777 // Revert the levels to not consider the SameSD levels
2778 CommonLevels -= SameSDLevels;
2779 MaxLevels += SameSDLevels;
2780 SameSDLevels = 0;
2781 break;
2782 }
2783 }
2784 }
2785
2786 if (SameSDLevels > 0)
2787 SameSDLoopsCount++;
2788
2789 FullDependence Result(Src, Dst, SCEVUnionPredicate(Assume, *SE),
2790 PossiblyLoopIndependent, CommonLevels);
2791 ++TotalArrayPairs;
2792
2793 for (unsigned P = 0; P < Pairs; ++P) {
2794 assert(Pair[P].Src->getType()->isIntegerTy() && "Src must be an integer");
2795 assert(Pair[P].Dst->getType()->isIntegerTy() && "Dst must be an integer");
2796 Pair[P].Loops.resize(MaxLevels + 1);
2797 Pair[P].GroupLoops.resize(MaxLevels + 1);
2798 Pair[P].Group.resize(Pairs);
2799 Pair[P].Classification =
2800 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()), Pair[P].Dst,
2801 LI->getLoopFor(Dst->getParent()), Pair[P].Loops);
2802 Pair[P].GroupLoops = Pair[P].Loops;
2803 Pair[P].Group.set(P);
2804 LLVM_DEBUG(dbgs() << " subscript " << P << "\n");
2805 LLVM_DEBUG(dbgs() << "\tsrc = " << *Pair[P].Src << "\n");
2806 LLVM_DEBUG(dbgs() << "\tdst = " << *Pair[P].Dst << "\n");
2807 LLVM_DEBUG(dbgs() << "\tclass = " << Pair[P].Classification << "\n");
2808 LLVM_DEBUG(dbgs() << "\tloops = ");
2810 }
2811
2812 // Test each subscript individually
2813 for (unsigned SI = 0; SI < Pairs; ++SI) {
2814 LLVM_DEBUG(dbgs() << "testing subscript " << SI);
2815
2816 // Attempt signed range test first.
2817 ConstantRange SrcRange = SE->getSignedRange(Pair[SI].Src);
2818 ConstantRange DstRange = SE->getSignedRange(Pair[SI].Dst);
2819 if (SrcRange.intersectWith(DstRange).isEmptySet())
2820 return nullptr;
2821
2822 switch (Pair[SI].Classification) {
2823 case Subscript::NonLinear:
2824 // ignore these, but collect loops for later
2825 ++NonlinearSubscriptPairs;
2826 collectCommonLoops(Pair[SI].Src, LI->getLoopFor(Src->getParent()),
2827 Pair[SI].Loops);
2828 collectCommonLoops(Pair[SI].Dst, LI->getLoopFor(Dst->getParent()),
2829 Pair[SI].Loops);
2830 break;
2831 case Subscript::ZIV:
2832 LLVM_DEBUG(dbgs() << ", ZIV\n");
2833 if (testZIV(Pair[SI].Src, Pair[SI].Dst, Result))
2834 return nullptr;
2835 break;
2836 case Subscript::SIV: {
2837 LLVM_DEBUG(dbgs() << ", SIV\n");
2838 unsigned Level;
2839 if (testSIV(Pair[SI].Src, Pair[SI].Dst, Level, Result,
2840 UnderRuntimeAssumptions))
2841 return nullptr;
2842 break;
2843 }
2844 case Subscript::RDIV:
2845 LLVM_DEBUG(dbgs() << ", RDIV\n");
2846 if (testRDIV(Pair[SI].Src, Pair[SI].Dst, Result))
2847 return nullptr;
2848 break;
2849 case Subscript::MIV:
2850 LLVM_DEBUG(dbgs() << ", MIV\n");
2851 if (testMIV(Pair[SI].Src, Pair[SI].Dst, Pair[SI].Loops, Result))
2852 return nullptr;
2853 break;
2854 }
2855 }
2856
2857 // Make sure the Scalar flags are set correctly.
2858 SmallBitVector CompleteLoops(MaxLevels + 1);
2859 for (unsigned SI = 0; SI < Pairs; ++SI)
2860 CompleteLoops |= Pair[SI].Loops;
2861 for (unsigned II = 1; II <= CommonLevels; ++II)
2862 if (CompleteLoops[II])
2863 Result.DV[II - 1].Scalar = false;
2864
2865 // Set the distance to zero if the direction is EQ.
2866 // TODO: Ideally, the distance should be set to 0 immediately simultaneously
2867 // with the corresponding direction being set to EQ.
2868 for (unsigned II = 1; II <= Result.getLevels(); ++II) {
2869 if (Result.getDirection(II) == Dependence::DVEntry::EQ) {
2870 if (Result.DV[II - 1].Distance == nullptr)
2871 Result.DV[II - 1].Distance = SE->getZero(SrcSCEV->getType());
2872 else
2873 assert(Result.DV[II - 1].Distance->isZero() &&
2874 "Inconsistency between distance and direction");
2875 }
2876
2877#ifndef NDEBUG
2878 // Check that the converse (i.e., if the distance is zero, then the
2879 // direction is EQ) holds.
2880 const SCEV *Distance = Result.getDistance(II);
2881 if (Distance && Distance->isZero())
2882 assert(Result.getDirection(II) == Dependence::DVEntry::EQ &&
2883 "Distance is zero, but direction is not EQ");
2884#endif
2885 }
2886
2887 if (SameSDLevels > 0) {
2888 // Extracting SameSD levels from the common levels
2889 // Reverting CommonLevels and MaxLevels to their original values
2890 assert(CommonLevels >= SameSDLevels);
2891 CommonLevels -= SameSDLevels;
2892 MaxLevels += SameSDLevels;
2893 std::unique_ptr<FullDependence::DVEntry[]> DV, DVSameSD;
2894 DV = std::make_unique<FullDependence::DVEntry[]>(CommonLevels);
2895 DVSameSD = std::make_unique<FullDependence::DVEntry[]>(SameSDLevels);
2896 for (unsigned Level = 0; Level < CommonLevels; ++Level)
2897 DV[Level] = Result.DV[Level];
2898 for (unsigned Level = 0; Level < SameSDLevels; ++Level)
2899 DVSameSD[Level] = Result.DV[CommonLevels + Level];
2900 Result.DV = std::move(DV);
2901 Result.DVSameSD = std::move(DVSameSD);
2902 Result.Levels = CommonLevels;
2903 Result.SameSDLevels = SameSDLevels;
2904 }
2905
2906 if (PossiblyLoopIndependent) {
2907 // Make sure the LoopIndependent flag is set correctly.
2908 // All directions must include equal, otherwise no
2909 // loop-independent dependence is possible.
2910 for (unsigned II = 1; II <= CommonLevels; ++II) {
2911 if (!(Result.getDirection(II) & Dependence::DVEntry::EQ)) {
2912 Result.LoopIndependent = false;
2913 break;
2914 }
2915 }
2916 } else {
2917 // On the other hand, if all directions are equal and there's no
2918 // loop-independent dependence possible, then no dependence exists.
2919 // However, if there are runtime assumptions, we must return the result.
2920 bool AllEqual = true;
2921 for (unsigned II = 1; II <= CommonLevels; ++II) {
2922 if (Result.getDirection(II) != Dependence::DVEntry::EQ) {
2923 AllEqual = false;
2924 break;
2925 }
2926 }
2927 if (AllEqual && Result.Assumptions.getPredicates().empty())
2928 return nullptr;
2929 }
2930
2931 return std::make_unique<FullDependence>(std::move(Result));
2932}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define X(NUM, ENUM, NAME)
Definition ELF.h:851
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
static bool isLoadOrStore(const Instruction *I)
static OverflowSafeSignedAPInt floorOfQuotient(const OverflowSafeSignedAPInt &OA, const OverflowSafeSignedAPInt &OB)
static void dumpExampleDependence(raw_ostream &OS, DependenceInfo *DA, ScalarEvolution &SE, LoopInfo &LI, bool NormalizeResults)
static OverflowSafeSignedAPInt ceilingOfQuotient(const OverflowSafeSignedAPInt &OA, const OverflowSafeSignedAPInt &OB)
static bool isDependenceTestEnabled(DependenceTestType Test)
Returns true iff Test is enabled.
static bool findGCD(unsigned Bits, const APInt &AM, const APInt &BM, const APInt &Delta, APInt &G, APInt &X, APInt &Y)
static void dumpSmallBitVector(SmallBitVector &BV)
static std::pair< OverflowSafeSignedAPInt, OverflowSafeSignedAPInt > inferDomainOfAffine(OverflowSafeSignedAPInt A, OverflowSafeSignedAPInt B, OverflowSafeSignedAPInt UB)
Given an affine expression of the form A*k + B, where k is an arbitrary integer, infer the possible r...
static const SCEV * minusSCEVNoSignedOverflow(const SCEV *A, const SCEV *B, ScalarEvolution &SE)
Returns A - B if it guaranteed not to signed wrap.
static AliasResult underlyingObjectsAlias(AAResults *AA, const DataLayout &DL, const MemoryLocation &LocA, const MemoryLocation &LocB)
static std::optional< APInt > getConstantCoefficient(const SCEV *Expr)
Given a SCEVMulExpr, returns its first operand if its first operand is a constant and the product doe...
static bool isRemainderZero(const SCEVConstant *Dividend, const SCEVConstant *Divisor)
static const SCEV * analyzeCoefficientsForGCD(const SCEV *Coefficients, APInt &RunningGCD, ScalarEvolution *SE)
Compute RunningGCD and return the start value of the innermost SCEVAddRecExpr.
static cl::opt< bool > Delinearize("da-delinearize", cl::init(true), cl::Hidden, cl::desc("Try to delinearize array references."))
static cl::opt< DependenceTestType > EnableDependenceTest("da-enable-dependence-test", cl::init(DependenceTestType::Default), cl::ReallyHidden, cl::desc("Run only specified dependence test routine and disable others. " "The purpose is mainly to exclude the influence of other " "dependence test routines in regression tests. If set to All, all " "dependence test routines are enabled."), cl::values(clEnumValN(DependenceTestType::Default, "default", "Enable all dependence test routines except " "Banerjee MIV (default)."), clEnumValN(DependenceTestType::All, "all", "Enable all dependence test routines."), clEnumValN(DependenceTestType::StrongSIV, "strong-siv", "Enable only Strong SIV test."), clEnumValN(DependenceTestType::WeakCrossingSIV, "weak-crossing-siv", "Enable only Weak-Crossing SIV test."), clEnumValN(DependenceTestType::ExactSIV, "exact-siv", "Enable only Exact SIV test."), clEnumValN(DependenceTestType::WeakZeroSIV, "weak-zero-siv", "Enable only Weak-Zero SIV test."), clEnumValN(DependenceTestType::ExactRDIV, "exact-rdiv", "Enable only Exact RDIV test."), clEnumValN(DependenceTestType::GCDMIV, "gcd-miv", "Enable only GCD MIV test."), clEnumValN(DependenceTestType::BanerjeeMIV, "banerjee-miv", "Enable only Banerjee MIV test.")))
static cl::opt< unsigned > MIVMaxLevelThreshold("da-miv-max-level-threshold", cl::init(7), cl::Hidden, cl::desc("Maximum depth allowed for the recursive algorithm used to " "explore MIV direction vectors."))
static cl::opt< bool > DisableDelinearizationChecks("da-disable-delinearization-checks", cl::Hidden, cl::desc("Disable checks that try to statically verify validity of " "delinearized subscripts. Enabling this option may result in incorrect " "dependence vectors for languages that allow the subscript of one " "dimension to underflow or overflow into another dimension."))
@ Default
Hexagon Hardware Loops
Module.h This file contains the declarations for the Module class.
Loop::LoopBounds::Direction Direction
Definition LoopInfo.cpp:253
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
#define T
uint64_t IntrinsicInst * II
#define P(N)
FunctionAnalysisManager FAM
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
Value * RHS
A manager for alias analyses.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
Class for arbitrary precision integers.
Definition APInt.h:78
static LLVM_ABI void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Definition APInt.cpp:1941
APInt abs() const
Get the absolute value.
Definition APInt.h:1818
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition APInt.h:1208
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1511
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1686
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1787
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition APInt.h:1137
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
The possible results of an alias query.
@ MayAlias
The two locations may or may not alias.
@ NoAlias
The two locations do not alias at all.
@ PartialAlias
The two locations alias, but only due to a partial overlap.
@ MustAlias
The two locations precisely alias each other.
This templated class represents "all analyses that operate over <aparticular IR unit>" (e....
Definition Analysis.h:50
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
AnalysisUsage & addRequiredTransitive()
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
void enableCrossIterationMode()
Assume that values may come from different cycle iterations.
bool isNoAlias(const MemoryLocation &LocA, const MemoryLocation &LocB)
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ ICMP_NE
not equal
Definition InstrTypes.h:698
This class represents a range of values.
LLVM_ABI bool isEmptySet() const
Return true if this set contains no members.
bool isSingleElement() const
Return true if this set contains exactly one member.
LLVM_ABI ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Legacy pass manager pass to access dependence information.
void getAnalysisUsage(AnalysisUsage &) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
void print(raw_ostream &, const Module *=nullptr) const override
print - Print out the internal state of the pass.
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
AnalysisPass to compute dependence information in a function.
LLVM_ABI Result run(Function &F, FunctionAnalysisManager &FAM)
DependenceInfo - This class is the main dependence-analysis driver.
LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle transitive invalidation when the cached analysis results go away.
LLVM_ABI std::unique_ptr< Dependence > depends(Instruction *Src, Instruction *Dst, bool UnderRuntimeAssumptions=false)
depends - Tests for a dependence between the Src and Dst instructions.
void dumpImp(raw_ostream &OS, bool IsSameSD=false) const
dumpImp - For debugging purposes.
Dependence(Dependence &&)=default
SCEVUnionPredicate getRuntimeAssumptions() const
getRuntimeAssumptions - Returns the runtime assumptions under which this Dependence relation is valid...
virtual bool isConfused() const
isConfused - Returns true if this dependence is confused (the compiler understands nothing and makes ...
virtual unsigned getSameSDLevels() const
getSameSDLevels - Returns the number of separate SameSD loops surrounding the source and destination ...
virtual const SCEV * getDistance(unsigned Level, bool SameSD=false) const
getDistance - Returns the distance (or NULL) associated with a particular common or SameSD level.
virtual unsigned getLevels() const
getLevels - Returns the number of common loops surrounding the source and destination of the dependen...
virtual unsigned getDirection(unsigned Level, bool SameSD=false) const
getDirection - Returns the direction associated with a particular common or SameSD level.
virtual bool isScalar(unsigned Level, bool SameSD=false) const
isScalar - Returns true if a particular regular or SameSD level is scalar; that is,...
bool isFlow() const
isFlow - Returns true if this is a flow (aka true) dependence.
bool isInput() const
isInput - Returns true if this is an input dependence.
bool isAnti() const
isAnti - Returns true if this is an anti dependence.
virtual bool isLoopIndependent() const
isLoopIndependent - Returns true if this is a loop-independent dependence.
bool isOutput() const
isOutput - Returns true if this is an output dependence.
void dump(raw_ostream &OS) const
dump - For debugging purposes, dumps a dependence to OS.
virtual bool inSameSDLoops(unsigned Level) const
inSameSDLoops - Returns true if this level is an SameSD level, i.e., performed across two separate lo...
Class representing an expression and its matching format.
FullDependence - This class represents a dependence between two memory references in a function.
FullDependence(Instruction *Source, Instruction *Destination, const SCEVUnionPredicate &Assumes, bool PossiblyLoopIndependent, unsigned Levels)
unsigned getDirection(unsigned Level, bool SameSD=false) const override
getDirection - Returns the direction associated with a particular common or SameSD level.
bool isScalar(unsigned Level, bool SameSD=false) const override
isScalar - Returns true if a particular regular or SameSD level is scalar; that is,...
bool isDirectionNegative() const override
Check if the direction vector is negative.
void negate(ScalarEvolution &SE) override
Negate the dependence by swapping the source and destination, and reversing the direction and distanc...
const SCEV * getDistance(unsigned Level, bool SameSD=false) const override
getDistance - Returns the distance (or NULL) associated with a particular common or SameSD level.
DVEntry getDVEntry(unsigned Level, bool IsSameSD) const
getDVEntry - Returns the DV entry associated with a regular or a SameSD level.
bool inSameSDLoops(unsigned Level) const override
inSameSDLoops - Returns true if this level is an SameSD level, i.e., performed across two separate lo...
bool normalize(ScalarEvolution *SE) override
If the direction vector is negative, normalize the direction vector to make it non-negative.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
FunctionPass(char &pid)
Definition Pass.h:316
An instruction for reading from memory.
bool isPrecise() const
uint64_t toRaw() const
Analysis pass that exposes the LoopInfo for a function.
Definition LoopInfo.h:589
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
const LoopT * getOutermostLoop() const
Get the outermost loop in which this loop is contained.
unsigned getLoopDepth() const
Return the nesting level of this loop.
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
The legacy pass manager's analysis pass to compute loop information.
Definition LoopInfo.h:616
This class represents a loop nest and can be used to query its properties.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Representation for a specific memory location.
static LLVM_ABI MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
LocationSize Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known.
static MemoryLocation getBeforeOrAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location before or after Ptr, while remaining within the underl...
AAMDNodes AATags
The metadata nodes which describes the aliasing of the location (each member is null if that kind of ...
const Value * Ptr
The address of the start of the location.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:298
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition Analysis.h:275
This node represents a polynomial recurrence on the trip count of the specified loop.
LLVM_ABI const SCEV * evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const
Return the value of this chain of recurrences at the specified iteration number.
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values.
SCEVUse getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
This class represents a constant integer value.
const APInt & getAPInt() const
This class represents a composition of other SCEV predicates, and is the class that most clients will...
This class represents an analyzed expression in the program.
LLVM_ABI bool isOne() const
Return true if the expression is a constant one.
LLVM_ABI bool isZero() const
Return true if the expression is a constant zero.
LLVM_ABI Type * getType() const
Return the LLVM type of this SCEV expression.
Analysis pass that exposes the ScalarEvolution for a function.
The main scalar evolution driver.
LLVM_ABI const SCEV * getNegativeSCEV(const SCEV *V, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
Return the SCEV object corresponding to -V.
LLVM_ABI bool willNotOverflow(Instruction::BinaryOps BinOp, bool Signed, const SCEV *LHS, const SCEV *RHS, const Instruction *CtxI=nullptr)
Is operation BinOp between LHS and RHS provably does not have a signed/unsigned overflow (Signed)?
LLVM_ABI const SCEV * getMinusSCEV(SCEVUse LHS, SCEVUse RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
This is a 'bitvector' (really, a variable-sized bit array), optimized for the case when the array is ...
iterator_range< const_set_bits_iterator > set_bits() const
int find_next(unsigned Prev) const
Returns the index of the next set bit following the "Prev" bit.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void resize(size_type N)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI Value(Type *Ty, unsigned scid)
Definition Value.cpp:53
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract Attribute helper functions.
Definition Attributor.h:165
const APInt & smin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
Definition APInt.h:2277
const APInt & smax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be signed.
Definition APInt.h:2282
LLVM_ABI APInt GreatestCommonDivisor(APInt A, APInt B)
Compute GCD of two unsigned APInt values.
Definition APInt.cpp:829
constexpr bool operator!(E Val)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ TB
TB - TwoByte - Set if this instruction has a two byte opcode, which starts with a 0x0F byte before th...
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
InstIterator< SymbolTableList< BasicBlock >, Function::iterator, BasicBlock::iterator, Instruction > inst_iterator
void collectParametricTerms(ScalarEvolution &SE, const SCEV *Expr, SmallVectorImpl< const SCEV * > &Terms)
Collect parametric terms occurring in step expressions (first step of delinearization).
void findArrayDimensions(ScalarEvolution &SE, SmallVectorImpl< const SCEV * > &Terms, SmallVectorImpl< const SCEV * > &Sizes, const SCEV *ElementSize)
Compute the array dimensions Sizes from the set of Terms extracted from the memory access function of...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
APInt operator*(APInt a, uint64_t RHS)
Definition APInt.h:2264
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
inst_iterator inst_begin(Function *F)
bool validateDelinearizationResult(ScalarEvolution &SE, ArrayRef< const SCEV * > Sizes, ArrayRef< const SCEV * > Subscripts)
Check that each subscript in Subscripts is within the corresponding size in Sizes.
void computeAccessFunctions(ScalarEvolution &SE, const SCEV *Expr, SmallVectorImpl< const SCEV * > &Subscripts, SmallVectorImpl< const SCEV * > &Sizes)
Return in Subscripts the access functions for each dimension in Sizes (third step of delinearization)...
bool delinearizeFixedSizeArray(ScalarEvolution &SE, const SCEV *Expr, SmallVectorImpl< const SCEV * > &Subscripts, SmallVectorImpl< const SCEV * > &Sizes, const SCEV *ElementSize)
Split this SCEVAddRecExpr into two vectors of SCEVs representing the subscripts and sizes of an acces...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
inst_iterator inst_end(Function *F)
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
APInt operator-(APInt)
Definition APInt.h:2217
APInt operator+(APInt a, const APInt &b)
Definition APInt.h:2222
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
LLVM_ABI FunctionPass * createDependenceAnalysisWrapperPass()
createDependenceAnalysisPass - This creates an instance of the DependenceAnalysis wrapper pass.
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
#define N
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
Dependence::DVEntry - Each level in the distance/direction vector has a direction (or perhaps a union...