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