1 //===-- DependenceAnalysis.cpp - DA Implementation --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // DependenceAnalysis is an LLVM pass that analyses dependences between memory
11 // accesses. Currently, it is an (incomplete) implementation of the approach
14 // Practical Dependence Testing
15 // Goff, Kennedy, Tseng
18 // There's a single entry point that analyzes the dependence between a pair
19 // of memory references in a function, returning either NULL, for no dependence,
20 // or a more-or-less detailed description of the dependence between them.
22 // Currently, the implementation cannot propagate constraints between
23 // coupled RDIV subscripts and lacks a multi-subscript MIV test.
24 // Both of these are conservative weaknesses;
25 // that is, not a source of correctness problems.
27 // The implementation depends on the GEP instruction to differentiate
28 // subscripts. Since Clang linearizes some array subscripts, the dependence
29 // analysis is using SCEV->delinearize to recover the representation of multiple
30 // subscripts, and thus avoid the more expensive and less precise MIV tests. The
31 // delinearization is controlled by the flag -da-delinearize.
33 // We should pay some careful attention to the possibility of integer overflow
34 // in the implementation of the various tests. This could happen with Add,
35 // Subtract, or Multiply, with both APInt's and SCEV's.
37 // Some non-linear subscript pairs can be handled by the GCD test
38 // (and perhaps other tests).
39 // Should explore how often these things occur.
41 // Finally, it seems like certain test cases expose weaknesses in the SCEV
42 // simplification, especially in the handling of sign and zero extensions.
43 // It could be useful to spend time exploring these.
45 // Please note that this is work in progress and the interface is subject to
48 //===----------------------------------------------------------------------===//
50 // In memory of Ken Kennedy, 1945 - 2007 //
52 //===----------------------------------------------------------------------===//
54 #include "llvm/Analysis/DependenceAnalysis.h"
55 #include "llvm/ADT/Statistic.h"
56 #include "llvm/Analysis/AliasAnalysis.h"
57 #include "llvm/Analysis/LoopInfo.h"
58 #include "llvm/Analysis/ScalarEvolution.h"
59 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
60 #include "llvm/Analysis/ValueTracking.h"
61 #include "llvm/IR/InstIterator.h"
62 #include "llvm/IR/Operator.h"
63 #include "llvm/Support/CommandLine.h"
64 #include "llvm/Support/Debug.h"
65 #include "llvm/Support/ErrorHandling.h"
66 #include "llvm/Support/raw_ostream.h"
70 #define DEBUG_TYPE "da"
72 //===----------------------------------------------------------------------===//
75 STATISTIC(TotalArrayPairs, "Array pairs tested");
76 STATISTIC(SeparableSubscriptPairs, "Separable subscript pairs");
77 STATISTIC(CoupledSubscriptPairs, "Coupled subscript pairs");
78 STATISTIC(NonlinearSubscriptPairs, "Nonlinear subscript pairs");
79 STATISTIC(ZIVapplications, "ZIV applications");
80 STATISTIC(ZIVindependence, "ZIV independence");
81 STATISTIC(StrongSIVapplications, "Strong SIV applications");
82 STATISTIC(StrongSIVsuccesses, "Strong SIV successes");
83 STATISTIC(StrongSIVindependence, "Strong SIV independence");
84 STATISTIC(WeakCrossingSIVapplications, "Weak-Crossing SIV applications");
85 STATISTIC(WeakCrossingSIVsuccesses, "Weak-Crossing SIV successes");
86 STATISTIC(WeakCrossingSIVindependence, "Weak-Crossing SIV independence");
87 STATISTIC(ExactSIVapplications, "Exact SIV applications");
88 STATISTIC(ExactSIVsuccesses, "Exact SIV successes");
89 STATISTIC(ExactSIVindependence, "Exact SIV independence");
90 STATISTIC(WeakZeroSIVapplications, "Weak-Zero SIV applications");
91 STATISTIC(WeakZeroSIVsuccesses, "Weak-Zero SIV successes");
92 STATISTIC(WeakZeroSIVindependence, "Weak-Zero SIV independence");
93 STATISTIC(ExactRDIVapplications, "Exact RDIV applications");
94 STATISTIC(ExactRDIVindependence, "Exact RDIV independence");
95 STATISTIC(SymbolicRDIVapplications, "Symbolic RDIV applications");
96 STATISTIC(SymbolicRDIVindependence, "Symbolic RDIV independence");
97 STATISTIC(DeltaApplications, "Delta applications");
98 STATISTIC(DeltaSuccesses, "Delta successes");
99 STATISTIC(DeltaIndependence, "Delta independence");
100 STATISTIC(DeltaPropagations, "Delta propagations");
101 STATISTIC(GCDapplications, "GCD applications");
102 STATISTIC(GCDsuccesses, "GCD successes");
103 STATISTIC(GCDindependence, "GCD independence");
104 STATISTIC(BanerjeeApplications, "Banerjee applications");
105 STATISTIC(BanerjeeIndependence, "Banerjee independence");
106 STATISTIC(BanerjeeSuccesses, "Banerjee successes");
109 Delinearize("da-delinearize", cl::init(false), cl::Hidden, cl::ZeroOrMore,
110 cl::desc("Try to delinearize array references."));
112 //===----------------------------------------------------------------------===//
115 INITIALIZE_PASS_BEGIN(DependenceAnalysis, "da",
116 "Dependence Analysis", true, true)
117 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
118 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
119 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
120 INITIALIZE_PASS_END(DependenceAnalysis, "da",
121 "Dependence Analysis", true, true)
123 char DependenceAnalysis::ID = 0;
126 FunctionPass *llvm::createDependenceAnalysisPass() {
127 return new DependenceAnalysis();
131 bool DependenceAnalysis::runOnFunction(Function &F) {
133 AA = &getAnalysis<AliasAnalysis>();
134 SE = &getAnalysis<ScalarEvolution>();
135 LI = &getAnalysis<LoopInfo>();
140 void DependenceAnalysis::releaseMemory() {
144 void DependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
145 AU.setPreservesAll();
146 AU.addRequiredTransitive<AliasAnalysis>();
147 AU.addRequiredTransitive<ScalarEvolution>();
148 AU.addRequiredTransitive<LoopInfo>();
152 // Used to test the dependence analyzer.
153 // Looks through the function, noting loads and stores.
154 // Calls depends() on every possible pair and prints out the result.
155 // Ignores all other instructions.
157 void dumpExampleDependence(raw_ostream &OS, Function *F,
158 DependenceAnalysis *DA) {
159 for (inst_iterator SrcI = inst_begin(F), SrcE = inst_end(F);
160 SrcI != SrcE; ++SrcI) {
161 if (isa<StoreInst>(*SrcI) || isa<LoadInst>(*SrcI)) {
162 for (inst_iterator DstI = SrcI, DstE = inst_end(F);
163 DstI != DstE; ++DstI) {
164 if (isa<StoreInst>(*DstI) || isa<LoadInst>(*DstI)) {
165 OS << "da analyze - ";
166 if (auto D = DA->depends(&*SrcI, &*DstI, true)) {
168 for (unsigned Level = 1; Level <= D->getLevels(); Level++) {
169 if (D->isSplitable(Level)) {
170 OS << "da analyze - split level = " << Level;
171 OS << ", iteration = " << *DA->getSplitIteration(*D, Level);
185 void DependenceAnalysis::print(raw_ostream &OS, const Module*) const {
186 dumpExampleDependence(OS, F, const_cast<DependenceAnalysis *>(this));
189 //===----------------------------------------------------------------------===//
190 // Dependence methods
192 // Returns true if this is an input dependence.
193 bool Dependence::isInput() const {
194 return Src->mayReadFromMemory() && Dst->mayReadFromMemory();
198 // Returns true if this is an output dependence.
199 bool Dependence::isOutput() const {
200 return Src->mayWriteToMemory() && Dst->mayWriteToMemory();
204 // Returns true if this is an flow (aka true) dependence.
205 bool Dependence::isFlow() const {
206 return Src->mayWriteToMemory() && Dst->mayReadFromMemory();
210 // Returns true if this is an anti dependence.
211 bool Dependence::isAnti() const {
212 return Src->mayReadFromMemory() && Dst->mayWriteToMemory();
216 // Returns true if a particular level is scalar; that is,
217 // if no subscript in the source or destination mention the induction
218 // variable associated with the loop at this level.
219 // Leave this out of line, so it will serve as a virtual method anchor
220 bool Dependence::isScalar(unsigned level) const {
225 //===----------------------------------------------------------------------===//
226 // FullDependence methods
228 FullDependence::FullDependence(Instruction *Source,
229 Instruction *Destination,
230 bool PossiblyLoopIndependent,
231 unsigned CommonLevels) :
232 Dependence(Source, Destination),
233 Levels(CommonLevels),
234 LoopIndependent(PossiblyLoopIndependent) {
236 DV = CommonLevels ? new DVEntry[CommonLevels] : nullptr;
239 // The rest are simple getters that hide the implementation.
241 // getDirection - Returns the direction associated with a particular level.
242 unsigned FullDependence::getDirection(unsigned Level) const {
243 assert(0 < Level && Level <= Levels && "Level out of range");
244 return DV[Level - 1].Direction;
248 // Returns the distance (or NULL) associated with a particular level.
249 const SCEV *FullDependence::getDistance(unsigned Level) const {
250 assert(0 < Level && Level <= Levels && "Level out of range");
251 return DV[Level - 1].Distance;
255 // Returns true if a particular level is scalar; that is,
256 // if no subscript in the source or destination mention the induction
257 // variable associated with the loop at this level.
258 bool FullDependence::isScalar(unsigned Level) const {
259 assert(0 < Level && Level <= Levels && "Level out of range");
260 return DV[Level - 1].Scalar;
264 // Returns true if peeling the first iteration from this loop
265 // will break this dependence.
266 bool FullDependence::isPeelFirst(unsigned Level) const {
267 assert(0 < Level && Level <= Levels && "Level out of range");
268 return DV[Level - 1].PeelFirst;
272 // Returns true if peeling the last iteration from this loop
273 // will break this dependence.
274 bool FullDependence::isPeelLast(unsigned Level) const {
275 assert(0 < Level && Level <= Levels && "Level out of range");
276 return DV[Level - 1].PeelLast;
280 // Returns true if splitting this loop will break the dependence.
281 bool FullDependence::isSplitable(unsigned Level) const {
282 assert(0 < Level && Level <= Levels && "Level out of range");
283 return DV[Level - 1].Splitable;
287 //===----------------------------------------------------------------------===//
288 // DependenceAnalysis::Constraint methods
290 // If constraint is a point <X, Y>, returns X.
292 const SCEV *DependenceAnalysis::Constraint::getX() const {
293 assert(Kind == Point && "Kind should be Point");
298 // If constraint is a point <X, Y>, returns Y.
300 const SCEV *DependenceAnalysis::Constraint::getY() const {
301 assert(Kind == Point && "Kind should be Point");
306 // If constraint is a line AX + BY = C, returns A.
308 const SCEV *DependenceAnalysis::Constraint::getA() const {
309 assert((Kind == Line || Kind == Distance) &&
310 "Kind should be Line (or Distance)");
315 // If constraint is a line AX + BY = C, returns B.
317 const SCEV *DependenceAnalysis::Constraint::getB() const {
318 assert((Kind == Line || Kind == Distance) &&
319 "Kind should be Line (or Distance)");
324 // If constraint is a line AX + BY = C, returns C.
326 const SCEV *DependenceAnalysis::Constraint::getC() const {
327 assert((Kind == Line || Kind == Distance) &&
328 "Kind should be Line (or Distance)");
333 // If constraint is a distance, returns D.
335 const SCEV *DependenceAnalysis::Constraint::getD() const {
336 assert(Kind == Distance && "Kind should be Distance");
337 return SE->getNegativeSCEV(C);
341 // Returns the loop associated with this constraint.
342 const Loop *DependenceAnalysis::Constraint::getAssociatedLoop() const {
343 assert((Kind == Distance || Kind == Line || Kind == Point) &&
344 "Kind should be Distance, Line, or Point");
345 return AssociatedLoop;
349 void DependenceAnalysis::Constraint::setPoint(const SCEV *X,
351 const Loop *CurLoop) {
355 AssociatedLoop = CurLoop;
359 void DependenceAnalysis::Constraint::setLine(const SCEV *AA,
362 const Loop *CurLoop) {
367 AssociatedLoop = CurLoop;
371 void DependenceAnalysis::Constraint::setDistance(const SCEV *D,
372 const Loop *CurLoop) {
374 A = SE->getConstant(D->getType(), 1);
375 B = SE->getNegativeSCEV(A);
376 C = SE->getNegativeSCEV(D);
377 AssociatedLoop = CurLoop;
381 void DependenceAnalysis::Constraint::setEmpty() {
386 void DependenceAnalysis::Constraint::setAny(ScalarEvolution *NewSE) {
392 // For debugging purposes. Dumps the constraint out to OS.
393 void DependenceAnalysis::Constraint::dump(raw_ostream &OS) const {
399 OS << " Point is <" << *getX() << ", " << *getY() << ">\n";
400 else if (isDistance())
401 OS << " Distance is " << *getD() <<
402 " (" << *getA() << "*X + " << *getB() << "*Y = " << *getC() << ")\n";
404 OS << " Line is " << *getA() << "*X + " <<
405 *getB() << "*Y = " << *getC() << "\n";
407 llvm_unreachable("unknown constraint type in Constraint::dump");
411 // Updates X with the intersection
412 // of the Constraints X and Y. Returns true if X has changed.
413 // Corresponds to Figure 4 from the paper
415 // Practical Dependence Testing
416 // Goff, Kennedy, Tseng
418 bool DependenceAnalysis::intersectConstraints(Constraint *X,
419 const Constraint *Y) {
421 DEBUG(dbgs() << "\tintersect constraints\n");
422 DEBUG(dbgs() << "\t X ="; X->dump(dbgs()));
423 DEBUG(dbgs() << "\t Y ="; Y->dump(dbgs()));
424 assert(!Y->isPoint() && "Y must not be a Point");
438 if (X->isDistance() && Y->isDistance()) {
439 DEBUG(dbgs() << "\t intersect 2 distances\n");
440 if (isKnownPredicate(CmpInst::ICMP_EQ, X->getD(), Y->getD()))
442 if (isKnownPredicate(CmpInst::ICMP_NE, X->getD(), Y->getD())) {
447 // Hmmm, interesting situation.
448 // I guess if either is constant, keep it and ignore the other.
449 if (isa<SCEVConstant>(Y->getD())) {
456 // At this point, the pseudo-code in Figure 4 of the paper
457 // checks if (X->isPoint() && Y->isPoint()).
458 // This case can't occur in our implementation,
459 // since a Point can only arise as the result of intersecting
460 // two Line constraints, and the right-hand value, Y, is never
461 // the result of an intersection.
462 assert(!(X->isPoint() && Y->isPoint()) &&
463 "We shouldn't ever see X->isPoint() && Y->isPoint()");
465 if (X->isLine() && Y->isLine()) {
466 DEBUG(dbgs() << "\t intersect 2 lines\n");
467 const SCEV *Prod1 = SE->getMulExpr(X->getA(), Y->getB());
468 const SCEV *Prod2 = SE->getMulExpr(X->getB(), Y->getA());
469 if (isKnownPredicate(CmpInst::ICMP_EQ, Prod1, Prod2)) {
470 // slopes are equal, so lines are parallel
471 DEBUG(dbgs() << "\t\tsame slope\n");
472 Prod1 = SE->getMulExpr(X->getC(), Y->getB());
473 Prod2 = SE->getMulExpr(X->getB(), Y->getC());
474 if (isKnownPredicate(CmpInst::ICMP_EQ, Prod1, Prod2))
476 if (isKnownPredicate(CmpInst::ICMP_NE, Prod1, Prod2)) {
483 if (isKnownPredicate(CmpInst::ICMP_NE, Prod1, Prod2)) {
484 // slopes differ, so lines intersect
485 DEBUG(dbgs() << "\t\tdifferent slopes\n");
486 const SCEV *C1B2 = SE->getMulExpr(X->getC(), Y->getB());
487 const SCEV *C1A2 = SE->getMulExpr(X->getC(), Y->getA());
488 const SCEV *C2B1 = SE->getMulExpr(Y->getC(), X->getB());
489 const SCEV *C2A1 = SE->getMulExpr(Y->getC(), X->getA());
490 const SCEV *A1B2 = SE->getMulExpr(X->getA(), Y->getB());
491 const SCEV *A2B1 = SE->getMulExpr(Y->getA(), X->getB());
492 const SCEVConstant *C1A2_C2A1 =
493 dyn_cast<SCEVConstant>(SE->getMinusSCEV(C1A2, C2A1));
494 const SCEVConstant *C1B2_C2B1 =
495 dyn_cast<SCEVConstant>(SE->getMinusSCEV(C1B2, C2B1));
496 const SCEVConstant *A1B2_A2B1 =
497 dyn_cast<SCEVConstant>(SE->getMinusSCEV(A1B2, A2B1));
498 const SCEVConstant *A2B1_A1B2 =
499 dyn_cast<SCEVConstant>(SE->getMinusSCEV(A2B1, A1B2));
500 if (!C1B2_C2B1 || !C1A2_C2A1 ||
501 !A1B2_A2B1 || !A2B1_A1B2)
503 APInt Xtop = C1B2_C2B1->getValue()->getValue();
504 APInt Xbot = A1B2_A2B1->getValue()->getValue();
505 APInt Ytop = C1A2_C2A1->getValue()->getValue();
506 APInt Ybot = A2B1_A1B2->getValue()->getValue();
507 DEBUG(dbgs() << "\t\tXtop = " << Xtop << "\n");
508 DEBUG(dbgs() << "\t\tXbot = " << Xbot << "\n");
509 DEBUG(dbgs() << "\t\tYtop = " << Ytop << "\n");
510 DEBUG(dbgs() << "\t\tYbot = " << Ybot << "\n");
511 APInt Xq = Xtop; // these need to be initialized, even
512 APInt Xr = Xtop; // though they're just going to be overwritten
513 APInt::sdivrem(Xtop, Xbot, Xq, Xr);
516 APInt::sdivrem(Ytop, Ybot, Yq, Yr);
517 if (Xr != 0 || Yr != 0) {
522 DEBUG(dbgs() << "\t\tX = " << Xq << ", Y = " << Yq << "\n");
523 if (Xq.slt(0) || Yq.slt(0)) {
528 if (const SCEVConstant *CUB =
529 collectConstantUpperBound(X->getAssociatedLoop(), Prod1->getType())) {
530 APInt UpperBound = CUB->getValue()->getValue();
531 DEBUG(dbgs() << "\t\tupper bound = " << UpperBound << "\n");
532 if (Xq.sgt(UpperBound) || Yq.sgt(UpperBound)) {
538 X->setPoint(SE->getConstant(Xq),
540 X->getAssociatedLoop());
547 // if (X->isLine() && Y->isPoint()) This case can't occur.
548 assert(!(X->isLine() && Y->isPoint()) && "This case should never occur");
550 if (X->isPoint() && Y->isLine()) {
551 DEBUG(dbgs() << "\t intersect Point and Line\n");
552 const SCEV *A1X1 = SE->getMulExpr(Y->getA(), X->getX());
553 const SCEV *B1Y1 = SE->getMulExpr(Y->getB(), X->getY());
554 const SCEV *Sum = SE->getAddExpr(A1X1, B1Y1);
555 if (isKnownPredicate(CmpInst::ICMP_EQ, Sum, Y->getC()))
557 if (isKnownPredicate(CmpInst::ICMP_NE, Sum, Y->getC())) {
565 llvm_unreachable("shouldn't reach the end of Constraint intersection");
570 //===----------------------------------------------------------------------===//
571 // DependenceAnalysis methods
573 // For debugging purposes. Dumps a dependence to OS.
574 void Dependence::dump(raw_ostream &OS) const {
575 bool Splitable = false;
589 unsigned Levels = getLevels();
591 for (unsigned II = 1; II <= Levels; ++II) {
596 const SCEV *Distance = getDistance(II);
599 else if (isScalar(II))
602 unsigned Direction = getDirection(II);
603 if (Direction == DVEntry::ALL)
606 if (Direction & DVEntry::LT)
608 if (Direction & DVEntry::EQ)
610 if (Direction & DVEntry::GT)
619 if (isLoopIndependent())
631 AliasAnalysis::AliasResult underlyingObjectsAlias(AliasAnalysis *AA,
634 const Value *AObj = GetUnderlyingObject(A);
635 const Value *BObj = GetUnderlyingObject(B);
636 return AA->alias(AObj, AA->getTypeStoreSize(AObj->getType()),
637 BObj, AA->getTypeStoreSize(BObj->getType()));
641 // Returns true if the load or store can be analyzed. Atomic and volatile
642 // operations have properties which this analysis does not understand.
644 bool isLoadOrStore(const Instruction *I) {
645 if (const LoadInst *LI = dyn_cast<LoadInst>(I))
646 return LI->isUnordered();
647 else if (const StoreInst *SI = dyn_cast<StoreInst>(I))
648 return SI->isUnordered();
654 Value *getPointerOperand(Instruction *I) {
655 if (LoadInst *LI = dyn_cast<LoadInst>(I))
656 return LI->getPointerOperand();
657 if (StoreInst *SI = dyn_cast<StoreInst>(I))
658 return SI->getPointerOperand();
659 llvm_unreachable("Value is not load or store instruction");
664 // Examines the loop nesting of the Src and Dst
665 // instructions and establishes their shared loops. Sets the variables
666 // CommonLevels, SrcLevels, and MaxLevels.
667 // The source and destination instructions needn't be contained in the same
668 // loop. The routine establishNestingLevels finds the level of most deeply
669 // nested loop that contains them both, CommonLevels. An instruction that's
670 // not contained in a loop is at level = 0. MaxLevels is equal to the level
671 // of the source plus the level of the destination, minus CommonLevels.
672 // This lets us allocate vectors MaxLevels in length, with room for every
673 // distinct loop referenced in both the source and destination subscripts.
674 // The variable SrcLevels is the nesting depth of the source instruction.
675 // It's used to help calculate distinct loops referenced by the destination.
676 // Here's the map from loops to levels:
678 // 1 - outermost common loop
679 // ... - other common loops
680 // CommonLevels - innermost common loop
681 // ... - loops containing Src but not Dst
682 // SrcLevels - innermost loop containing Src but not Dst
683 // ... - loops containing Dst but not Src
684 // MaxLevels - innermost loops containing Dst but not Src
685 // Consider the follow code fragment:
702 // If we're looking at the possibility of a dependence between the store
703 // to A (the Src) and the load from A (the Dst), we'll note that they
704 // have 2 loops in common, so CommonLevels will equal 2 and the direction
705 // vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7.
706 // A map from loop names to loop numbers would look like
708 // b - 2 = CommonLevels
714 void DependenceAnalysis::establishNestingLevels(const Instruction *Src,
715 const Instruction *Dst) {
716 const BasicBlock *SrcBlock = Src->getParent();
717 const BasicBlock *DstBlock = Dst->getParent();
718 unsigned SrcLevel = LI->getLoopDepth(SrcBlock);
719 unsigned DstLevel = LI->getLoopDepth(DstBlock);
720 const Loop *SrcLoop = LI->getLoopFor(SrcBlock);
721 const Loop *DstLoop = LI->getLoopFor(DstBlock);
722 SrcLevels = SrcLevel;
723 MaxLevels = SrcLevel + DstLevel;
724 while (SrcLevel > DstLevel) {
725 SrcLoop = SrcLoop->getParentLoop();
728 while (DstLevel > SrcLevel) {
729 DstLoop = DstLoop->getParentLoop();
732 while (SrcLoop != DstLoop) {
733 SrcLoop = SrcLoop->getParentLoop();
734 DstLoop = DstLoop->getParentLoop();
737 CommonLevels = SrcLevel;
738 MaxLevels -= CommonLevels;
742 // Given one of the loops containing the source, return
743 // its level index in our numbering scheme.
744 unsigned DependenceAnalysis::mapSrcLoop(const Loop *SrcLoop) const {
745 return SrcLoop->getLoopDepth();
749 // Given one of the loops containing the destination,
750 // return its level index in our numbering scheme.
751 unsigned DependenceAnalysis::mapDstLoop(const Loop *DstLoop) const {
752 unsigned D = DstLoop->getLoopDepth();
753 if (D > CommonLevels)
754 return D - CommonLevels + SrcLevels;
760 // Returns true if Expression is loop invariant in LoopNest.
761 bool DependenceAnalysis::isLoopInvariant(const SCEV *Expression,
762 const Loop *LoopNest) const {
765 return SE->isLoopInvariant(Expression, LoopNest) &&
766 isLoopInvariant(Expression, LoopNest->getParentLoop());
771 // Finds the set of loops from the LoopNest that
772 // have a level <= CommonLevels and are referred to by the SCEV Expression.
773 void DependenceAnalysis::collectCommonLoops(const SCEV *Expression,
774 const Loop *LoopNest,
775 SmallBitVector &Loops) const {
777 unsigned Level = LoopNest->getLoopDepth();
778 if (Level <= CommonLevels && !SE->isLoopInvariant(Expression, LoopNest))
780 LoopNest = LoopNest->getParentLoop();
785 // removeMatchingExtensions - Examines a subscript pair.
786 // If the source and destination are identically sign (or zero)
787 // extended, it strips off the extension in an effect to simplify
788 // the actual analysis.
789 void DependenceAnalysis::removeMatchingExtensions(Subscript *Pair) {
790 const SCEV *Src = Pair->Src;
791 const SCEV *Dst = Pair->Dst;
792 if ((isa<SCEVZeroExtendExpr>(Src) && isa<SCEVZeroExtendExpr>(Dst)) ||
793 (isa<SCEVSignExtendExpr>(Src) && isa<SCEVSignExtendExpr>(Dst))) {
794 const SCEVCastExpr *SrcCast = cast<SCEVCastExpr>(Src);
795 const SCEVCastExpr *DstCast = cast<SCEVCastExpr>(Dst);
796 if (SrcCast->getType() == DstCast->getType()) {
797 Pair->Src = SrcCast->getOperand();
798 Pair->Dst = DstCast->getOperand();
804 // Examine the scev and return true iff it's linear.
805 // Collect any loops mentioned in the set of "Loops".
806 bool DependenceAnalysis::checkSrcSubscript(const SCEV *Src,
807 const Loop *LoopNest,
808 SmallBitVector &Loops) {
809 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Src);
811 return isLoopInvariant(Src, LoopNest);
812 const SCEV *Start = AddRec->getStart();
813 const SCEV *Step = AddRec->getStepRecurrence(*SE);
814 if (!isLoopInvariant(Step, LoopNest))
816 Loops.set(mapSrcLoop(AddRec->getLoop()));
817 return checkSrcSubscript(Start, LoopNest, Loops);
822 // Examine the scev and return true iff it's linear.
823 // Collect any loops mentioned in the set of "Loops".
824 bool DependenceAnalysis::checkDstSubscript(const SCEV *Dst,
825 const Loop *LoopNest,
826 SmallBitVector &Loops) {
827 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Dst);
829 return isLoopInvariant(Dst, LoopNest);
830 const SCEV *Start = AddRec->getStart();
831 const SCEV *Step = AddRec->getStepRecurrence(*SE);
832 if (!isLoopInvariant(Step, LoopNest))
834 Loops.set(mapDstLoop(AddRec->getLoop()));
835 return checkDstSubscript(Start, LoopNest, Loops);
839 // Examines the subscript pair (the Src and Dst SCEVs)
840 // and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear.
841 // Collects the associated loops in a set.
842 DependenceAnalysis::Subscript::ClassificationKind
843 DependenceAnalysis::classifyPair(const SCEV *Src, const Loop *SrcLoopNest,
844 const SCEV *Dst, const Loop *DstLoopNest,
845 SmallBitVector &Loops) {
846 SmallBitVector SrcLoops(MaxLevels + 1);
847 SmallBitVector DstLoops(MaxLevels + 1);
848 if (!checkSrcSubscript(Src, SrcLoopNest, SrcLoops))
849 return Subscript::NonLinear;
850 if (!checkDstSubscript(Dst, DstLoopNest, DstLoops))
851 return Subscript::NonLinear;
854 unsigned N = Loops.count();
856 return Subscript::ZIV;
858 return Subscript::SIV;
859 if (N == 2 && (SrcLoops.count() == 0 ||
860 DstLoops.count() == 0 ||
861 (SrcLoops.count() == 1 && DstLoops.count() == 1)))
862 return Subscript::RDIV;
863 return Subscript::MIV;
867 // A wrapper around SCEV::isKnownPredicate.
868 // Looks for cases where we're interested in comparing for equality.
869 // If both X and Y have been identically sign or zero extended,
870 // it strips off the (confusing) extensions before invoking
871 // SCEV::isKnownPredicate. Perhaps, someday, the ScalarEvolution package
872 // will be similarly updated.
874 // If SCEV::isKnownPredicate can't prove the predicate,
875 // we try simple subtraction, which seems to help in some cases
876 // involving symbolics.
877 bool DependenceAnalysis::isKnownPredicate(ICmpInst::Predicate Pred,
879 const SCEV *Y) const {
880 if (Pred == CmpInst::ICMP_EQ ||
881 Pred == CmpInst::ICMP_NE) {
882 if ((isa<SCEVSignExtendExpr>(X) &&
883 isa<SCEVSignExtendExpr>(Y)) ||
884 (isa<SCEVZeroExtendExpr>(X) &&
885 isa<SCEVZeroExtendExpr>(Y))) {
886 const SCEVCastExpr *CX = cast<SCEVCastExpr>(X);
887 const SCEVCastExpr *CY = cast<SCEVCastExpr>(Y);
888 const SCEV *Xop = CX->getOperand();
889 const SCEV *Yop = CY->getOperand();
890 if (Xop->getType() == Yop->getType()) {
896 if (SE->isKnownPredicate(Pred, X, Y))
898 // If SE->isKnownPredicate can't prove the condition,
899 // we try the brute-force approach of subtracting
900 // and testing the difference.
901 // By testing with SE->isKnownPredicate first, we avoid
902 // the possibility of overflow when the arguments are constants.
903 const SCEV *Delta = SE->getMinusSCEV(X, Y);
905 case CmpInst::ICMP_EQ:
906 return Delta->isZero();
907 case CmpInst::ICMP_NE:
908 return SE->isKnownNonZero(Delta);
909 case CmpInst::ICMP_SGE:
910 return SE->isKnownNonNegative(Delta);
911 case CmpInst::ICMP_SLE:
912 return SE->isKnownNonPositive(Delta);
913 case CmpInst::ICMP_SGT:
914 return SE->isKnownPositive(Delta);
915 case CmpInst::ICMP_SLT:
916 return SE->isKnownNegative(Delta);
918 llvm_unreachable("unexpected predicate in isKnownPredicate");
923 // All subscripts are all the same type.
924 // Loop bound may be smaller (e.g., a char).
925 // Should zero extend loop bound, since it's always >= 0.
926 // This routine collects upper bound and extends if needed.
927 // Return null if no bound available.
928 const SCEV *DependenceAnalysis::collectUpperBound(const Loop *L,
930 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
931 const SCEV *UB = SE->getBackedgeTakenCount(L);
932 return SE->getNoopOrZeroExtend(UB, T);
938 // Calls collectUpperBound(), then attempts to cast it to SCEVConstant.
939 // If the cast fails, returns NULL.
940 const SCEVConstant *DependenceAnalysis::collectConstantUpperBound(const Loop *L,
943 if (const SCEV *UB = collectUpperBound(L, T))
944 return dyn_cast<SCEVConstant>(UB);
950 // When we have a pair of subscripts of the form [c1] and [c2],
951 // where c1 and c2 are both loop invariant, we attack it using
952 // the ZIV test. Basically, we test by comparing the two values,
953 // but there are actually three possible results:
954 // 1) the values are equal, so there's a dependence
955 // 2) the values are different, so there's no dependence
956 // 3) the values might be equal, so we have to assume a dependence.
958 // Return true if dependence disproved.
959 bool DependenceAnalysis::testZIV(const SCEV *Src,
961 FullDependence &Result) const {
962 DEBUG(dbgs() << " src = " << *Src << "\n");
963 DEBUG(dbgs() << " dst = " << *Dst << "\n");
965 if (isKnownPredicate(CmpInst::ICMP_EQ, Src, Dst)) {
966 DEBUG(dbgs() << " provably dependent\n");
967 return false; // provably dependent
969 if (isKnownPredicate(CmpInst::ICMP_NE, Src, Dst)) {
970 DEBUG(dbgs() << " provably independent\n");
972 return true; // provably independent
974 DEBUG(dbgs() << " possibly dependent\n");
975 Result.Consistent = false;
976 return false; // possibly dependent
981 // From the paper, Practical Dependence Testing, Section 4.2.1
983 // When we have a pair of subscripts of the form [c1 + a*i] and [c2 + a*i],
984 // where i is an induction variable, c1 and c2 are loop invariant,
985 // and a is a constant, we can solve it exactly using the Strong SIV test.
987 // Can prove independence. Failing that, can compute distance (and direction).
988 // In the presence of symbolic terms, we can sometimes make progress.
990 // If there's a dependence,
992 // c1 + a*i = c2 + a*i'
994 // The dependence distance is
996 // d = i' - i = (c1 - c2)/a
998 // A dependence only exists if d is an integer and abs(d) <= U, where U is the
999 // loop's upper bound. If a dependence exists, the dependence direction is
1003 // direction = { = if d = 0
1006 // Return true if dependence disproved.
1007 bool DependenceAnalysis::strongSIVtest(const SCEV *Coeff,
1008 const SCEV *SrcConst,
1009 const SCEV *DstConst,
1010 const Loop *CurLoop,
1012 FullDependence &Result,
1013 Constraint &NewConstraint) const {
1014 DEBUG(dbgs() << "\tStrong SIV test\n");
1015 DEBUG(dbgs() << "\t Coeff = " << *Coeff);
1016 DEBUG(dbgs() << ", " << *Coeff->getType() << "\n");
1017 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst);
1018 DEBUG(dbgs() << ", " << *SrcConst->getType() << "\n");
1019 DEBUG(dbgs() << "\t DstConst = " << *DstConst);
1020 DEBUG(dbgs() << ", " << *DstConst->getType() << "\n");
1021 ++StrongSIVapplications;
1022 assert(0 < Level && Level <= CommonLevels && "level out of range");
1025 const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst);
1026 DEBUG(dbgs() << "\t Delta = " << *Delta);
1027 DEBUG(dbgs() << ", " << *Delta->getType() << "\n");
1029 // check that |Delta| < iteration count
1030 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
1031 DEBUG(dbgs() << "\t UpperBound = " << *UpperBound);
1032 DEBUG(dbgs() << ", " << *UpperBound->getType() << "\n");
1033 const SCEV *AbsDelta =
1034 SE->isKnownNonNegative(Delta) ? Delta : SE->getNegativeSCEV(Delta);
1035 const SCEV *AbsCoeff =
1036 SE->isKnownNonNegative(Coeff) ? Coeff : SE->getNegativeSCEV(Coeff);
1037 const SCEV *Product = SE->getMulExpr(UpperBound, AbsCoeff);
1038 if (isKnownPredicate(CmpInst::ICMP_SGT, AbsDelta, Product)) {
1039 // Distance greater than trip count - no dependence
1040 ++StrongSIVindependence;
1041 ++StrongSIVsuccesses;
1046 // Can we compute distance?
1047 if (isa<SCEVConstant>(Delta) && isa<SCEVConstant>(Coeff)) {
1048 APInt ConstDelta = cast<SCEVConstant>(Delta)->getValue()->getValue();
1049 APInt ConstCoeff = cast<SCEVConstant>(Coeff)->getValue()->getValue();
1050 APInt Distance = ConstDelta; // these need to be initialized
1051 APInt Remainder = ConstDelta;
1052 APInt::sdivrem(ConstDelta, ConstCoeff, Distance, Remainder);
1053 DEBUG(dbgs() << "\t Distance = " << Distance << "\n");
1054 DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
1055 // Make sure Coeff divides Delta exactly
1056 if (Remainder != 0) {
1057 // Coeff doesn't divide Distance, no dependence
1058 ++StrongSIVindependence;
1059 ++StrongSIVsuccesses;
1062 Result.DV[Level].Distance = SE->getConstant(Distance);
1063 NewConstraint.setDistance(SE->getConstant(Distance), CurLoop);
1064 if (Distance.sgt(0))
1065 Result.DV[Level].Direction &= Dependence::DVEntry::LT;
1066 else if (Distance.slt(0))
1067 Result.DV[Level].Direction &= Dependence::DVEntry::GT;
1069 Result.DV[Level].Direction &= Dependence::DVEntry::EQ;
1070 ++StrongSIVsuccesses;
1072 else if (Delta->isZero()) {
1074 Result.DV[Level].Distance = Delta;
1075 NewConstraint.setDistance(Delta, CurLoop);
1076 Result.DV[Level].Direction &= Dependence::DVEntry::EQ;
1077 ++StrongSIVsuccesses;
1080 if (Coeff->isOne()) {
1081 DEBUG(dbgs() << "\t Distance = " << *Delta << "\n");
1082 Result.DV[Level].Distance = Delta; // since X/1 == X
1083 NewConstraint.setDistance(Delta, CurLoop);
1086 Result.Consistent = false;
1087 NewConstraint.setLine(Coeff,
1088 SE->getNegativeSCEV(Coeff),
1089 SE->getNegativeSCEV(Delta), CurLoop);
1092 // maybe we can get a useful direction
1093 bool DeltaMaybeZero = !SE->isKnownNonZero(Delta);
1094 bool DeltaMaybePositive = !SE->isKnownNonPositive(Delta);
1095 bool DeltaMaybeNegative = !SE->isKnownNonNegative(Delta);
1096 bool CoeffMaybePositive = !SE->isKnownNonPositive(Coeff);
1097 bool CoeffMaybeNegative = !SE->isKnownNonNegative(Coeff);
1098 // The double negatives above are confusing.
1099 // It helps to read !SE->isKnownNonZero(Delta)
1100 // as "Delta might be Zero"
1101 unsigned NewDirection = Dependence::DVEntry::NONE;
1102 if ((DeltaMaybePositive && CoeffMaybePositive) ||
1103 (DeltaMaybeNegative && CoeffMaybeNegative))
1104 NewDirection = Dependence::DVEntry::LT;
1106 NewDirection |= Dependence::DVEntry::EQ;
1107 if ((DeltaMaybeNegative && CoeffMaybePositive) ||
1108 (DeltaMaybePositive && CoeffMaybeNegative))
1109 NewDirection |= Dependence::DVEntry::GT;
1110 if (NewDirection < Result.DV[Level].Direction)
1111 ++StrongSIVsuccesses;
1112 Result.DV[Level].Direction &= NewDirection;
1118 // weakCrossingSIVtest -
1119 // From the paper, Practical Dependence Testing, Section 4.2.2
1121 // When we have a pair of subscripts of the form [c1 + a*i] and [c2 - a*i],
1122 // where i is an induction variable, c1 and c2 are loop invariant,
1123 // and a is a constant, we can solve it exactly using the
1124 // Weak-Crossing SIV test.
1126 // Given c1 + a*i = c2 - a*i', we can look for the intersection of
1127 // the two lines, where i = i', yielding
1129 // c1 + a*i = c2 - a*i
1133 // If i < 0, there is no dependence.
1134 // If i > upperbound, there is no dependence.
1135 // If i = 0 (i.e., if c1 = c2), there's a dependence with distance = 0.
1136 // If i = upperbound, there's a dependence with distance = 0.
1137 // If i is integral, there's a dependence (all directions).
1138 // If the non-integer part = 1/2, there's a dependence (<> directions).
1139 // Otherwise, there's no dependence.
1141 // Can prove independence. Failing that,
1142 // can sometimes refine the directions.
1143 // Can determine iteration for splitting.
1145 // Return true if dependence disproved.
1146 bool DependenceAnalysis::weakCrossingSIVtest(const SCEV *Coeff,
1147 const SCEV *SrcConst,
1148 const SCEV *DstConst,
1149 const Loop *CurLoop,
1151 FullDependence &Result,
1152 Constraint &NewConstraint,
1153 const SCEV *&SplitIter) const {
1154 DEBUG(dbgs() << "\tWeak-Crossing SIV test\n");
1155 DEBUG(dbgs() << "\t Coeff = " << *Coeff << "\n");
1156 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1157 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1158 ++WeakCrossingSIVapplications;
1159 assert(0 < Level && Level <= CommonLevels && "Level out of range");
1161 Result.Consistent = false;
1162 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1163 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1164 NewConstraint.setLine(Coeff, Coeff, Delta, CurLoop);
1165 if (Delta->isZero()) {
1166 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::LT);
1167 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::GT);
1168 ++WeakCrossingSIVsuccesses;
1169 if (!Result.DV[Level].Direction) {
1170 ++WeakCrossingSIVindependence;
1173 Result.DV[Level].Distance = Delta; // = 0
1176 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(Coeff);
1180 Result.DV[Level].Splitable = true;
1181 if (SE->isKnownNegative(ConstCoeff)) {
1182 ConstCoeff = dyn_cast<SCEVConstant>(SE->getNegativeSCEV(ConstCoeff));
1183 assert(ConstCoeff &&
1184 "dynamic cast of negative of ConstCoeff should yield constant");
1185 Delta = SE->getNegativeSCEV(Delta);
1187 assert(SE->isKnownPositive(ConstCoeff) && "ConstCoeff should be positive");
1189 // compute SplitIter for use by DependenceAnalysis::getSplitIteration()
1191 SE->getUDivExpr(SE->getSMaxExpr(SE->getConstant(Delta->getType(), 0),
1193 SE->getMulExpr(SE->getConstant(Delta->getType(), 2),
1195 DEBUG(dbgs() << "\t Split iter = " << *SplitIter << "\n");
1197 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1201 // We're certain that ConstCoeff > 0; therefore,
1202 // if Delta < 0, then no dependence.
1203 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1204 DEBUG(dbgs() << "\t ConstCoeff = " << *ConstCoeff << "\n");
1205 if (SE->isKnownNegative(Delta)) {
1206 // No dependence, Delta < 0
1207 ++WeakCrossingSIVindependence;
1208 ++WeakCrossingSIVsuccesses;
1212 // We're certain that Delta > 0 and ConstCoeff > 0.
1213 // Check Delta/(2*ConstCoeff) against upper loop bound
1214 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
1215 DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
1216 const SCEV *ConstantTwo = SE->getConstant(UpperBound->getType(), 2);
1217 const SCEV *ML = SE->getMulExpr(SE->getMulExpr(ConstCoeff, UpperBound),
1219 DEBUG(dbgs() << "\t ML = " << *ML << "\n");
1220 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, ML)) {
1221 // Delta too big, no dependence
1222 ++WeakCrossingSIVindependence;
1223 ++WeakCrossingSIVsuccesses;
1226 if (isKnownPredicate(CmpInst::ICMP_EQ, Delta, ML)) {
1228 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::LT);
1229 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::GT);
1230 ++WeakCrossingSIVsuccesses;
1231 if (!Result.DV[Level].Direction) {
1232 ++WeakCrossingSIVindependence;
1235 Result.DV[Level].Splitable = false;
1236 Result.DV[Level].Distance = SE->getConstant(Delta->getType(), 0);
1241 // check that Coeff divides Delta
1242 APInt APDelta = ConstDelta->getValue()->getValue();
1243 APInt APCoeff = ConstCoeff->getValue()->getValue();
1244 APInt Distance = APDelta; // these need to be initialzed
1245 APInt Remainder = APDelta;
1246 APInt::sdivrem(APDelta, APCoeff, Distance, Remainder);
1247 DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
1248 if (Remainder != 0) {
1249 // Coeff doesn't divide Delta, no dependence
1250 ++WeakCrossingSIVindependence;
1251 ++WeakCrossingSIVsuccesses;
1254 DEBUG(dbgs() << "\t Distance = " << Distance << "\n");
1256 // if 2*Coeff doesn't divide Delta, then the equal direction isn't possible
1257 APInt Two = APInt(Distance.getBitWidth(), 2, true);
1258 Remainder = Distance.srem(Two);
1259 DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
1260 if (Remainder != 0) {
1261 // Equal direction isn't possible
1262 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::EQ);
1263 ++WeakCrossingSIVsuccesses;
1269 // Kirch's algorithm, from
1271 // Optimizing Supercompilers for Supercomputers
1275 // Program 2.1, page 29.
1276 // Computes the GCD of AM and BM.
1277 // Also finds a solution to the equation ax - by = gcd(a, b).
1278 // Returns true if dependence disproved; i.e., gcd does not divide Delta.
1280 bool findGCD(unsigned Bits, APInt AM, APInt BM, APInt Delta,
1281 APInt &G, APInt &X, APInt &Y) {
1282 APInt A0(Bits, 1, true), A1(Bits, 0, true);
1283 APInt B0(Bits, 0, true), B1(Bits, 1, true);
1284 APInt G0 = AM.abs();
1285 APInt G1 = BM.abs();
1286 APInt Q = G0; // these need to be initialized
1288 APInt::sdivrem(G0, G1, Q, R);
1290 APInt A2 = A0 - Q*A1; A0 = A1; A1 = A2;
1291 APInt B2 = B0 - Q*B1; B0 = B1; B1 = B2;
1293 APInt::sdivrem(G0, G1, Q, R);
1296 DEBUG(dbgs() << "\t GCD = " << G << "\n");
1297 X = AM.slt(0) ? -A1 : A1;
1298 Y = BM.slt(0) ? B1 : -B1;
1300 // make sure gcd divides Delta
1303 return true; // gcd doesn't divide Delta, no dependence
1312 APInt floorOfQuotient(APInt A, APInt B) {
1313 APInt Q = A; // these need to be initialized
1315 APInt::sdivrem(A, B, Q, R);
1318 if ((A.sgt(0) && B.sgt(0)) ||
1319 (A.slt(0) && B.slt(0)))
1327 APInt ceilingOfQuotient(APInt A, APInt B) {
1328 APInt Q = A; // these need to be initialized
1330 APInt::sdivrem(A, B, Q, R);
1333 if ((A.sgt(0) && B.sgt(0)) ||
1334 (A.slt(0) && B.slt(0)))
1342 APInt maxAPInt(APInt A, APInt B) {
1343 return A.sgt(B) ? A : B;
1348 APInt minAPInt(APInt A, APInt B) {
1349 return A.slt(B) ? A : B;
1354 // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*i],
1355 // where i is an induction variable, c1 and c2 are loop invariant, and a1
1356 // and a2 are constant, we can solve it exactly using an algorithm developed
1357 // by Banerjee and Wolfe. See Section 2.5.3 in
1359 // Optimizing Supercompilers for Supercomputers
1363 // It's slower than the specialized tests (strong SIV, weak-zero SIV, etc),
1364 // so use them if possible. They're also a bit better with symbolics and,
1365 // in the case of the strong SIV test, can compute Distances.
1367 // Return true if dependence disproved.
1368 bool DependenceAnalysis::exactSIVtest(const SCEV *SrcCoeff,
1369 const SCEV *DstCoeff,
1370 const SCEV *SrcConst,
1371 const SCEV *DstConst,
1372 const Loop *CurLoop,
1374 FullDependence &Result,
1375 Constraint &NewConstraint) const {
1376 DEBUG(dbgs() << "\tExact SIV test\n");
1377 DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n");
1378 DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n");
1379 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1380 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1381 ++ExactSIVapplications;
1382 assert(0 < Level && Level <= CommonLevels && "Level out of range");
1384 Result.Consistent = false;
1385 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1386 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1387 NewConstraint.setLine(SrcCoeff, SE->getNegativeSCEV(DstCoeff),
1389 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1390 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1391 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1392 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
1397 APInt AM = ConstSrcCoeff->getValue()->getValue();
1398 APInt BM = ConstDstCoeff->getValue()->getValue();
1399 unsigned Bits = AM.getBitWidth();
1400 if (findGCD(Bits, AM, BM, ConstDelta->getValue()->getValue(), G, X, Y)) {
1401 // gcd doesn't divide Delta, no dependence
1402 ++ExactSIVindependence;
1403 ++ExactSIVsuccesses;
1407 DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n");
1409 // since SCEV construction normalizes, LM = 0
1410 APInt UM(Bits, 1, true);
1411 bool UMvalid = false;
1412 // UM is perhaps unavailable, let's check
1413 if (const SCEVConstant *CUB =
1414 collectConstantUpperBound(CurLoop, Delta->getType())) {
1415 UM = CUB->getValue()->getValue();
1416 DEBUG(dbgs() << "\t UM = " << UM << "\n");
1420 APInt TU(APInt::getSignedMaxValue(Bits));
1421 APInt TL(APInt::getSignedMinValue(Bits));
1423 // test(BM/G, LM-X) and test(-BM/G, X-UM)
1424 APInt TMUL = BM.sdiv(G);
1426 TL = maxAPInt(TL, ceilingOfQuotient(-X, TMUL));
1427 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1429 TU = minAPInt(TU, floorOfQuotient(UM - X, TMUL));
1430 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1434 TU = minAPInt(TU, floorOfQuotient(-X, TMUL));
1435 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1437 TL = maxAPInt(TL, ceilingOfQuotient(UM - X, TMUL));
1438 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1442 // test(AM/G, LM-Y) and test(-AM/G, Y-UM)
1445 TL = maxAPInt(TL, ceilingOfQuotient(-Y, TMUL));
1446 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1448 TU = minAPInt(TU, floorOfQuotient(UM - Y, TMUL));
1449 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1453 TU = minAPInt(TU, floorOfQuotient(-Y, TMUL));
1454 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1456 TL = maxAPInt(TL, ceilingOfQuotient(UM - Y, TMUL));
1457 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1461 ++ExactSIVindependence;
1462 ++ExactSIVsuccesses;
1466 // explore directions
1467 unsigned NewDirection = Dependence::DVEntry::NONE;
1470 APInt SaveTU(TU); // save these
1472 DEBUG(dbgs() << "\t exploring LT direction\n");
1475 TL = maxAPInt(TL, ceilingOfQuotient(X - Y + 1, TMUL));
1476 DEBUG(dbgs() << "\t\t TL = " << TL << "\n");
1479 TU = minAPInt(TU, floorOfQuotient(X - Y + 1, TMUL));
1480 DEBUG(dbgs() << "\t\t TU = " << TU << "\n");
1483 NewDirection |= Dependence::DVEntry::LT;
1484 ++ExactSIVsuccesses;
1488 TU = SaveTU; // restore
1490 DEBUG(dbgs() << "\t exploring EQ direction\n");
1492 TL = maxAPInt(TL, ceilingOfQuotient(X - Y, TMUL));
1493 DEBUG(dbgs() << "\t\t TL = " << TL << "\n");
1496 TU = minAPInt(TU, floorOfQuotient(X - Y, TMUL));
1497 DEBUG(dbgs() << "\t\t TU = " << TU << "\n");
1501 TL = maxAPInt(TL, ceilingOfQuotient(Y - X, TMUL));
1502 DEBUG(dbgs() << "\t\t TL = " << TL << "\n");
1505 TU = minAPInt(TU, floorOfQuotient(Y - X, TMUL));
1506 DEBUG(dbgs() << "\t\t TU = " << TU << "\n");
1509 NewDirection |= Dependence::DVEntry::EQ;
1510 ++ExactSIVsuccesses;
1514 TU = SaveTU; // restore
1516 DEBUG(dbgs() << "\t exploring GT direction\n");
1518 TL = maxAPInt(TL, ceilingOfQuotient(Y - X + 1, TMUL));
1519 DEBUG(dbgs() << "\t\t TL = " << TL << "\n");
1522 TU = minAPInt(TU, floorOfQuotient(Y - X + 1, TMUL));
1523 DEBUG(dbgs() << "\t\t TU = " << TU << "\n");
1526 NewDirection |= Dependence::DVEntry::GT;
1527 ++ExactSIVsuccesses;
1531 Result.DV[Level].Direction &= NewDirection;
1532 if (Result.DV[Level].Direction == Dependence::DVEntry::NONE)
1533 ++ExactSIVindependence;
1534 return Result.DV[Level].Direction == Dependence::DVEntry::NONE;
1539 // Return true if the divisor evenly divides the dividend.
1541 bool isRemainderZero(const SCEVConstant *Dividend,
1542 const SCEVConstant *Divisor) {
1543 APInt ConstDividend = Dividend->getValue()->getValue();
1544 APInt ConstDivisor = Divisor->getValue()->getValue();
1545 return ConstDividend.srem(ConstDivisor) == 0;
1549 // weakZeroSrcSIVtest -
1550 // From the paper, Practical Dependence Testing, Section 4.2.2
1552 // When we have a pair of subscripts of the form [c1] and [c2 + a*i],
1553 // where i is an induction variable, c1 and c2 are loop invariant,
1554 // and a is a constant, we can solve it exactly using the
1555 // Weak-Zero SIV test.
1565 // If i is not an integer, there's no dependence.
1566 // If i < 0 or > UB, there's no dependence.
1567 // If i = 0, the direction is <= and peeling the
1568 // 1st iteration will break the dependence.
1569 // If i = UB, the direction is >= and peeling the
1570 // last iteration will break the dependence.
1571 // Otherwise, the direction is *.
1573 // Can prove independence. Failing that, we can sometimes refine
1574 // the directions. Can sometimes show that first or last
1575 // iteration carries all the dependences (so worth peeling).
1577 // (see also weakZeroDstSIVtest)
1579 // Return true if dependence disproved.
1580 bool DependenceAnalysis::weakZeroSrcSIVtest(const SCEV *DstCoeff,
1581 const SCEV *SrcConst,
1582 const SCEV *DstConst,
1583 const Loop *CurLoop,
1585 FullDependence &Result,
1586 Constraint &NewConstraint) const {
1587 // For the WeakSIV test, it's possible the loop isn't common to
1588 // the Src and Dst loops. If it isn't, then there's no need to
1589 // record a direction.
1590 DEBUG(dbgs() << "\tWeak-Zero (src) SIV test\n");
1591 DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << "\n");
1592 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1593 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1594 ++WeakZeroSIVapplications;
1595 assert(0 < Level && Level <= MaxLevels && "Level out of range");
1597 Result.Consistent = false;
1598 const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst);
1599 NewConstraint.setLine(SE->getConstant(Delta->getType(), 0),
1600 DstCoeff, Delta, CurLoop);
1601 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1602 if (isKnownPredicate(CmpInst::ICMP_EQ, SrcConst, DstConst)) {
1603 if (Level < CommonLevels) {
1604 Result.DV[Level].Direction &= Dependence::DVEntry::LE;
1605 Result.DV[Level].PeelFirst = true;
1606 ++WeakZeroSIVsuccesses;
1608 return false; // dependences caused by first iteration
1610 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1613 const SCEV *AbsCoeff =
1614 SE->isKnownNegative(ConstCoeff) ?
1615 SE->getNegativeSCEV(ConstCoeff) : ConstCoeff;
1616 const SCEV *NewDelta =
1617 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
1619 // check that Delta/SrcCoeff < iteration count
1620 // really check NewDelta < count*AbsCoeff
1621 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
1622 DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
1623 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound);
1624 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) {
1625 ++WeakZeroSIVindependence;
1626 ++WeakZeroSIVsuccesses;
1629 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) {
1630 // dependences caused by last iteration
1631 if (Level < CommonLevels) {
1632 Result.DV[Level].Direction &= Dependence::DVEntry::GE;
1633 Result.DV[Level].PeelLast = true;
1634 ++WeakZeroSIVsuccesses;
1640 // check that Delta/SrcCoeff >= 0
1641 // really check that NewDelta >= 0
1642 if (SE->isKnownNegative(NewDelta)) {
1643 // No dependence, newDelta < 0
1644 ++WeakZeroSIVindependence;
1645 ++WeakZeroSIVsuccesses;
1649 // if SrcCoeff doesn't divide Delta, then no dependence
1650 if (isa<SCEVConstant>(Delta) &&
1651 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) {
1652 ++WeakZeroSIVindependence;
1653 ++WeakZeroSIVsuccesses;
1660 // weakZeroDstSIVtest -
1661 // From the paper, Practical Dependence Testing, Section 4.2.2
1663 // When we have a pair of subscripts of the form [c1 + a*i] and [c2],
1664 // where i is an induction variable, c1 and c2 are loop invariant,
1665 // and a is a constant, we can solve it exactly using the
1666 // Weak-Zero SIV test.
1676 // If i is not an integer, there's no dependence.
1677 // If i < 0 or > UB, there's no dependence.
1678 // If i = 0, the direction is <= and peeling the
1679 // 1st iteration will break the dependence.
1680 // If i = UB, the direction is >= and peeling the
1681 // last iteration will break the dependence.
1682 // Otherwise, the direction is *.
1684 // Can prove independence. Failing that, we can sometimes refine
1685 // the directions. Can sometimes show that first or last
1686 // iteration carries all the dependences (so worth peeling).
1688 // (see also weakZeroSrcSIVtest)
1690 // Return true if dependence disproved.
1691 bool DependenceAnalysis::weakZeroDstSIVtest(const SCEV *SrcCoeff,
1692 const SCEV *SrcConst,
1693 const SCEV *DstConst,
1694 const Loop *CurLoop,
1696 FullDependence &Result,
1697 Constraint &NewConstraint) const {
1698 // For the WeakSIV test, it's possible the loop isn't common to the
1699 // Src and Dst loops. If it isn't, then there's no need to record a direction.
1700 DEBUG(dbgs() << "\tWeak-Zero (dst) SIV test\n");
1701 DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << "\n");
1702 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1703 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1704 ++WeakZeroSIVapplications;
1705 assert(0 < Level && Level <= SrcLevels && "Level out of range");
1707 Result.Consistent = false;
1708 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1709 NewConstraint.setLine(SrcCoeff, SE->getConstant(Delta->getType(), 0),
1711 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1712 if (isKnownPredicate(CmpInst::ICMP_EQ, DstConst, SrcConst)) {
1713 if (Level < CommonLevels) {
1714 Result.DV[Level].Direction &= Dependence::DVEntry::LE;
1715 Result.DV[Level].PeelFirst = true;
1716 ++WeakZeroSIVsuccesses;
1718 return false; // dependences caused by first iteration
1720 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1723 const SCEV *AbsCoeff =
1724 SE->isKnownNegative(ConstCoeff) ?
1725 SE->getNegativeSCEV(ConstCoeff) : ConstCoeff;
1726 const SCEV *NewDelta =
1727 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
1729 // check that Delta/SrcCoeff < iteration count
1730 // really check NewDelta < count*AbsCoeff
1731 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
1732 DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
1733 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound);
1734 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) {
1735 ++WeakZeroSIVindependence;
1736 ++WeakZeroSIVsuccesses;
1739 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) {
1740 // dependences caused by last iteration
1741 if (Level < CommonLevels) {
1742 Result.DV[Level].Direction &= Dependence::DVEntry::GE;
1743 Result.DV[Level].PeelLast = true;
1744 ++WeakZeroSIVsuccesses;
1750 // check that Delta/SrcCoeff >= 0
1751 // really check that NewDelta >= 0
1752 if (SE->isKnownNegative(NewDelta)) {
1753 // No dependence, newDelta < 0
1754 ++WeakZeroSIVindependence;
1755 ++WeakZeroSIVsuccesses;
1759 // if SrcCoeff doesn't divide Delta, then no dependence
1760 if (isa<SCEVConstant>(Delta) &&
1761 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) {
1762 ++WeakZeroSIVindependence;
1763 ++WeakZeroSIVsuccesses;
1770 // exactRDIVtest - Tests the RDIV subscript pair for dependence.
1771 // Things of the form [c1 + a*i] and [c2 + b*j],
1772 // where i and j are induction variable, c1 and c2 are loop invariant,
1773 // and a and b are constants.
1774 // Returns true if any possible dependence is disproved.
1775 // Marks the result as inconsistent.
1776 // Works in some cases that symbolicRDIVtest doesn't, and vice versa.
1777 bool DependenceAnalysis::exactRDIVtest(const SCEV *SrcCoeff,
1778 const SCEV *DstCoeff,
1779 const SCEV *SrcConst,
1780 const SCEV *DstConst,
1781 const Loop *SrcLoop,
1782 const Loop *DstLoop,
1783 FullDependence &Result) const {
1784 DEBUG(dbgs() << "\tExact RDIV test\n");
1785 DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n");
1786 DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n");
1787 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1788 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1789 ++ExactRDIVapplications;
1790 Result.Consistent = false;
1791 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1792 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1793 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1794 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1795 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1796 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
1801 APInt AM = ConstSrcCoeff->getValue()->getValue();
1802 APInt BM = ConstDstCoeff->getValue()->getValue();
1803 unsigned Bits = AM.getBitWidth();
1804 if (findGCD(Bits, AM, BM, ConstDelta->getValue()->getValue(), G, X, Y)) {
1805 // gcd doesn't divide Delta, no dependence
1806 ++ExactRDIVindependence;
1810 DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n");
1812 // since SCEV construction seems to normalize, LM = 0
1813 APInt SrcUM(Bits, 1, true);
1814 bool SrcUMvalid = false;
1815 // SrcUM is perhaps unavailable, let's check
1816 if (const SCEVConstant *UpperBound =
1817 collectConstantUpperBound(SrcLoop, Delta->getType())) {
1818 SrcUM = UpperBound->getValue()->getValue();
1819 DEBUG(dbgs() << "\t SrcUM = " << SrcUM << "\n");
1823 APInt DstUM(Bits, 1, true);
1824 bool DstUMvalid = false;
1825 // UM is perhaps unavailable, let's check
1826 if (const SCEVConstant *UpperBound =
1827 collectConstantUpperBound(DstLoop, Delta->getType())) {
1828 DstUM = UpperBound->getValue()->getValue();
1829 DEBUG(dbgs() << "\t DstUM = " << DstUM << "\n");
1833 APInt TU(APInt::getSignedMaxValue(Bits));
1834 APInt TL(APInt::getSignedMinValue(Bits));
1836 // test(BM/G, LM-X) and test(-BM/G, X-UM)
1837 APInt TMUL = BM.sdiv(G);
1839 TL = maxAPInt(TL, ceilingOfQuotient(-X, TMUL));
1840 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1842 TU = minAPInt(TU, floorOfQuotient(SrcUM - X, TMUL));
1843 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1847 TU = minAPInt(TU, floorOfQuotient(-X, TMUL));
1848 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1850 TL = maxAPInt(TL, ceilingOfQuotient(SrcUM - X, TMUL));
1851 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1855 // test(AM/G, LM-Y) and test(-AM/G, Y-UM)
1858 TL = maxAPInt(TL, ceilingOfQuotient(-Y, TMUL));
1859 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1861 TU = minAPInt(TU, floorOfQuotient(DstUM - Y, TMUL));
1862 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1866 TU = minAPInt(TU, floorOfQuotient(-Y, TMUL));
1867 DEBUG(dbgs() << "\t TU = " << TU << "\n");
1869 TL = maxAPInt(TL, ceilingOfQuotient(DstUM - Y, TMUL));
1870 DEBUG(dbgs() << "\t TL = " << TL << "\n");
1874 ++ExactRDIVindependence;
1879 // symbolicRDIVtest -
1880 // In Section 4.5 of the Practical Dependence Testing paper,the authors
1881 // introduce a special case of Banerjee's Inequalities (also called the
1882 // Extreme-Value Test) that can handle some of the SIV and RDIV cases,
1883 // particularly cases with symbolics. Since it's only able to disprove
1884 // dependence (not compute distances or directions), we'll use it as a
1885 // fall back for the other tests.
1887 // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
1888 // where i and j are induction variables and c1 and c2 are loop invariants,
1889 // we can use the symbolic tests to disprove some dependences, serving as a
1890 // backup for the RDIV test. Note that i and j can be the same variable,
1891 // letting this test serve as a backup for the various SIV tests.
1893 // For a dependence to exist, c1 + a1*i must equal c2 + a2*j for some
1894 // 0 <= i <= N1 and some 0 <= j <= N2, where N1 and N2 are the (normalized)
1895 // loop bounds for the i and j loops, respectively. So, ...
1897 // c1 + a1*i = c2 + a2*j
1898 // a1*i - a2*j = c2 - c1
1900 // To test for a dependence, we compute c2 - c1 and make sure it's in the
1901 // range of the maximum and minimum possible values of a1*i - a2*j.
1902 // Considering the signs of a1 and a2, we have 4 possible cases:
1904 // 1) If a1 >= 0 and a2 >= 0, then
1905 // a1*0 - a2*N2 <= c2 - c1 <= a1*N1 - a2*0
1906 // -a2*N2 <= c2 - c1 <= a1*N1
1908 // 2) If a1 >= 0 and a2 <= 0, then
1909 // a1*0 - a2*0 <= c2 - c1 <= a1*N1 - a2*N2
1910 // 0 <= c2 - c1 <= a1*N1 - a2*N2
1912 // 3) If a1 <= 0 and a2 >= 0, then
1913 // a1*N1 - a2*N2 <= c2 - c1 <= a1*0 - a2*0
1914 // a1*N1 - a2*N2 <= c2 - c1 <= 0
1916 // 4) If a1 <= 0 and a2 <= 0, then
1917 // a1*N1 - a2*0 <= c2 - c1 <= a1*0 - a2*N2
1918 // a1*N1 <= c2 - c1 <= -a2*N2
1920 // return true if dependence disproved
1921 bool DependenceAnalysis::symbolicRDIVtest(const SCEV *A1,
1926 const Loop *Loop2) const {
1927 ++SymbolicRDIVapplications;
1928 DEBUG(dbgs() << "\ttry symbolic RDIV test\n");
1929 DEBUG(dbgs() << "\t A1 = " << *A1);
1930 DEBUG(dbgs() << ", type = " << *A1->getType() << "\n");
1931 DEBUG(dbgs() << "\t A2 = " << *A2 << "\n");
1932 DEBUG(dbgs() << "\t C1 = " << *C1 << "\n");
1933 DEBUG(dbgs() << "\t C2 = " << *C2 << "\n");
1934 const SCEV *N1 = collectUpperBound(Loop1, A1->getType());
1935 const SCEV *N2 = collectUpperBound(Loop2, A1->getType());
1936 DEBUG(if (N1) dbgs() << "\t N1 = " << *N1 << "\n");
1937 DEBUG(if (N2) dbgs() << "\t N2 = " << *N2 << "\n");
1938 const SCEV *C2_C1 = SE->getMinusSCEV(C2, C1);
1939 const SCEV *C1_C2 = SE->getMinusSCEV(C1, C2);
1940 DEBUG(dbgs() << "\t C2 - C1 = " << *C2_C1 << "\n");
1941 DEBUG(dbgs() << "\t C1 - C2 = " << *C1_C2 << "\n");
1942 if (SE->isKnownNonNegative(A1)) {
1943 if (SE->isKnownNonNegative(A2)) {
1944 // A1 >= 0 && A2 >= 0
1946 // make sure that c2 - c1 <= a1*N1
1947 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
1948 DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n");
1949 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1)) {
1950 ++SymbolicRDIVindependence;
1955 // make sure that -a2*N2 <= c2 - c1, or a2*N2 >= c1 - c2
1956 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
1957 DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n");
1958 if (isKnownPredicate(CmpInst::ICMP_SLT, A2N2, C1_C2)) {
1959 ++SymbolicRDIVindependence;
1964 else if (SE->isKnownNonPositive(A2)) {
1965 // a1 >= 0 && a2 <= 0
1967 // make sure that c2 - c1 <= a1*N1 - a2*N2
1968 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
1969 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
1970 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2);
1971 DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n");
1972 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1_A2N2)) {
1973 ++SymbolicRDIVindependence;
1977 // make sure that 0 <= c2 - c1
1978 if (SE->isKnownNegative(C2_C1)) {
1979 ++SymbolicRDIVindependence;
1984 else if (SE->isKnownNonPositive(A1)) {
1985 if (SE->isKnownNonNegative(A2)) {
1986 // a1 <= 0 && a2 >= 0
1988 // make sure that a1*N1 - a2*N2 <= c2 - c1
1989 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
1990 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
1991 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2);
1992 DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n");
1993 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1_A2N2, C2_C1)) {
1994 ++SymbolicRDIVindependence;
1998 // make sure that c2 - c1 <= 0
1999 if (SE->isKnownPositive(C2_C1)) {
2000 ++SymbolicRDIVindependence;
2004 else if (SE->isKnownNonPositive(A2)) {
2005 // a1 <= 0 && a2 <= 0
2007 // make sure that a1*N1 <= c2 - c1
2008 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2009 DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n");
2010 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1, C2_C1)) {
2011 ++SymbolicRDIVindependence;
2016 // make sure that c2 - c1 <= -a2*N2, or c1 - c2 >= a2*N2
2017 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2018 DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n");
2019 if (isKnownPredicate(CmpInst::ICMP_SLT, C1_C2, A2N2)) {
2020 ++SymbolicRDIVindependence;
2031 // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 - a2*i]
2032 // where i is an induction variable, c1 and c2 are loop invariant, and a1 and
2033 // a2 are constant, we attack it with an SIV test. While they can all be
2034 // solved with the Exact SIV test, it's worthwhile to use simpler tests when
2035 // they apply; they're cheaper and sometimes more precise.
2037 // Return true if dependence disproved.
2038 bool DependenceAnalysis::testSIV(const SCEV *Src,
2041 FullDependence &Result,
2042 Constraint &NewConstraint,
2043 const SCEV *&SplitIter) const {
2044 DEBUG(dbgs() << " src = " << *Src << "\n");
2045 DEBUG(dbgs() << " dst = " << *Dst << "\n");
2046 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
2047 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
2048 if (SrcAddRec && DstAddRec) {
2049 const SCEV *SrcConst = SrcAddRec->getStart();
2050 const SCEV *DstConst = DstAddRec->getStart();
2051 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2052 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE);
2053 const Loop *CurLoop = SrcAddRec->getLoop();
2054 assert(CurLoop == DstAddRec->getLoop() &&
2055 "both loops in SIV should be same");
2056 Level = mapSrcLoop(CurLoop);
2058 if (SrcCoeff == DstCoeff)
2059 disproven = strongSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop,
2060 Level, Result, NewConstraint);
2061 else if (SrcCoeff == SE->getNegativeSCEV(DstCoeff))
2062 disproven = weakCrossingSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop,
2063 Level, Result, NewConstraint, SplitIter);
2065 disproven = exactSIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurLoop,
2066 Level, Result, NewConstraint);
2068 gcdMIVtest(Src, Dst, Result) ||
2069 symbolicRDIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurLoop, CurLoop);
2072 const SCEV *SrcConst = SrcAddRec->getStart();
2073 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2074 const SCEV *DstConst = Dst;
2075 const Loop *CurLoop = SrcAddRec->getLoop();
2076 Level = mapSrcLoop(CurLoop);
2077 return weakZeroDstSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop,
2078 Level, Result, NewConstraint) ||
2079 gcdMIVtest(Src, Dst, Result);
2082 const SCEV *DstConst = DstAddRec->getStart();
2083 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE);
2084 const SCEV *SrcConst = Src;
2085 const Loop *CurLoop = DstAddRec->getLoop();
2086 Level = mapDstLoop(CurLoop);
2087 return weakZeroSrcSIVtest(DstCoeff, SrcConst, DstConst,
2088 CurLoop, Level, Result, NewConstraint) ||
2089 gcdMIVtest(Src, Dst, Result);
2091 llvm_unreachable("SIV test expected at least one AddRec");
2097 // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
2098 // where i and j are induction variables, c1 and c2 are loop invariant,
2099 // and a1 and a2 are constant, we can solve it exactly with an easy adaptation
2100 // of the Exact SIV test, the Restricted Double Index Variable (RDIV) test.
2101 // It doesn't make sense to talk about distance or direction in this case,
2102 // so there's no point in making special versions of the Strong SIV test or
2103 // the Weak-crossing SIV test.
2105 // With minor algebra, this test can also be used for things like
2106 // [c1 + a1*i + a2*j][c2].
2108 // Return true if dependence disproved.
2109 bool DependenceAnalysis::testRDIV(const SCEV *Src,
2111 FullDependence &Result) const {
2112 // we have 3 possible situations here:
2113 // 1) [a*i + b] and [c*j + d]
2114 // 2) [a*i + c*j + b] and [d]
2115 // 3) [b] and [a*i + c*j + d]
2116 // We need to find what we've got and get organized
2118 const SCEV *SrcConst, *DstConst;
2119 const SCEV *SrcCoeff, *DstCoeff;
2120 const Loop *SrcLoop, *DstLoop;
2122 DEBUG(dbgs() << " src = " << *Src << "\n");
2123 DEBUG(dbgs() << " dst = " << *Dst << "\n");
2124 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
2125 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
2126 if (SrcAddRec && DstAddRec) {
2127 SrcConst = SrcAddRec->getStart();
2128 SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2129 SrcLoop = SrcAddRec->getLoop();
2130 DstConst = DstAddRec->getStart();
2131 DstCoeff = DstAddRec->getStepRecurrence(*SE);
2132 DstLoop = DstAddRec->getLoop();
2134 else if (SrcAddRec) {
2135 if (const SCEVAddRecExpr *tmpAddRec =
2136 dyn_cast<SCEVAddRecExpr>(SrcAddRec->getStart())) {
2137 SrcConst = tmpAddRec->getStart();
2138 SrcCoeff = tmpAddRec->getStepRecurrence(*SE);
2139 SrcLoop = tmpAddRec->getLoop();
2141 DstCoeff = SE->getNegativeSCEV(SrcAddRec->getStepRecurrence(*SE));
2142 DstLoop = SrcAddRec->getLoop();
2145 llvm_unreachable("RDIV reached by surprising SCEVs");
2147 else if (DstAddRec) {
2148 if (const SCEVAddRecExpr *tmpAddRec =
2149 dyn_cast<SCEVAddRecExpr>(DstAddRec->getStart())) {
2150 DstConst = tmpAddRec->getStart();
2151 DstCoeff = tmpAddRec->getStepRecurrence(*SE);
2152 DstLoop = tmpAddRec->getLoop();
2154 SrcCoeff = SE->getNegativeSCEV(DstAddRec->getStepRecurrence(*SE));
2155 SrcLoop = DstAddRec->getLoop();
2158 llvm_unreachable("RDIV reached by surprising SCEVs");
2161 llvm_unreachable("RDIV expected at least one AddRec");
2162 return exactRDIVtest(SrcCoeff, DstCoeff,
2166 gcdMIVtest(Src, Dst, Result) ||
2167 symbolicRDIVtest(SrcCoeff, DstCoeff,
2173 // Tests the single-subscript MIV pair (Src and Dst) for dependence.
2174 // Return true if dependence disproved.
2175 // Can sometimes refine direction vectors.
2176 bool DependenceAnalysis::testMIV(const SCEV *Src,
2178 const SmallBitVector &Loops,
2179 FullDependence &Result) const {
2180 DEBUG(dbgs() << " src = " << *Src << "\n");
2181 DEBUG(dbgs() << " dst = " << *Dst << "\n");
2182 Result.Consistent = false;
2183 return gcdMIVtest(Src, Dst, Result) ||
2184 banerjeeMIVtest(Src, Dst, Loops, Result);
2188 // Given a product, e.g., 10*X*Y, returns the first constant operand,
2189 // in this case 10. If there is no constant part, returns NULL.
2191 const SCEVConstant *getConstantPart(const SCEVMulExpr *Product) {
2192 for (unsigned Op = 0, Ops = Product->getNumOperands(); Op < Ops; Op++) {
2193 if (const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Product->getOperand(Op)))
2200 //===----------------------------------------------------------------------===//
2202 // Tests an MIV subscript pair for dependence.
2203 // Returns true if any possible dependence is disproved.
2204 // Marks the result as inconsistent.
2205 // Can sometimes disprove the equal direction for 1 or more loops,
2206 // as discussed in Michael Wolfe's book,
2207 // High Performance Compilers for Parallel Computing, page 235.
2209 // We spend some effort (code!) to handle cases like
2210 // [10*i + 5*N*j + 15*M + 6], where i and j are induction variables,
2211 // but M and N are just loop-invariant variables.
2212 // This should help us handle linearized subscripts;
2213 // also makes this test a useful backup to the various SIV tests.
2215 // It occurs to me that the presence of loop-invariant variables
2216 // changes the nature of the test from "greatest common divisor"
2217 // to "a common divisor".
2218 bool DependenceAnalysis::gcdMIVtest(const SCEV *Src,
2220 FullDependence &Result) const {
2221 DEBUG(dbgs() << "starting gcd\n");
2223 unsigned BitWidth = SE->getTypeSizeInBits(Src->getType());
2224 APInt RunningGCD = APInt::getNullValue(BitWidth);
2226 // Examine Src coefficients.
2227 // Compute running GCD and record source constant.
2228 // Because we're looking for the constant at the end of the chain,
2229 // we can't quit the loop just because the GCD == 1.
2230 const SCEV *Coefficients = Src;
2231 while (const SCEVAddRecExpr *AddRec =
2232 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2233 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2234 const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Coeff);
2235 if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Coeff))
2236 // If the coefficient is the product of a constant and other stuff,
2237 // we can use the constant in the GCD computation.
2238 Constant = getConstantPart(Product);
2241 APInt ConstCoeff = Constant->getValue()->getValue();
2242 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2243 Coefficients = AddRec->getStart();
2245 const SCEV *SrcConst = Coefficients;
2247 // Examine Dst coefficients.
2248 // Compute running GCD and record destination constant.
2249 // Because we're looking for the constant at the end of the chain,
2250 // we can't quit the loop just because the GCD == 1.
2252 while (const SCEVAddRecExpr *AddRec =
2253 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2254 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2255 const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Coeff);
2256 if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Coeff))
2257 // If the coefficient is the product of a constant and other stuff,
2258 // we can use the constant in the GCD computation.
2259 Constant = getConstantPart(Product);
2262 APInt ConstCoeff = Constant->getValue()->getValue();
2263 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2264 Coefficients = AddRec->getStart();
2266 const SCEV *DstConst = Coefficients;
2268 APInt ExtraGCD = APInt::getNullValue(BitWidth);
2269 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
2270 DEBUG(dbgs() << " Delta = " << *Delta << "\n");
2271 const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Delta);
2272 if (const SCEVAddExpr *Sum = dyn_cast<SCEVAddExpr>(Delta)) {
2273 // If Delta is a sum of products, we may be able to make further progress.
2274 for (unsigned Op = 0, Ops = Sum->getNumOperands(); Op < Ops; Op++) {
2275 const SCEV *Operand = Sum->getOperand(Op);
2276 if (isa<SCEVConstant>(Operand)) {
2277 assert(!Constant && "Surprised to find multiple constants");
2278 Constant = cast<SCEVConstant>(Operand);
2280 else if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Operand)) {
2281 // Search for constant operand to participate in GCD;
2282 // If none found; return false.
2283 const SCEVConstant *ConstOp = getConstantPart(Product);
2286 APInt ConstOpValue = ConstOp->getValue()->getValue();
2287 ExtraGCD = APIntOps::GreatestCommonDivisor(ExtraGCD,
2288 ConstOpValue.abs());
2296 APInt ConstDelta = cast<SCEVConstant>(Constant)->getValue()->getValue();
2297 DEBUG(dbgs() << " ConstDelta = " << ConstDelta << "\n");
2298 if (ConstDelta == 0)
2300 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ExtraGCD);
2301 DEBUG(dbgs() << " RunningGCD = " << RunningGCD << "\n");
2302 APInt Remainder = ConstDelta.srem(RunningGCD);
2303 if (Remainder != 0) {
2308 // Try to disprove equal directions.
2309 // For example, given a subscript pair [3*i + 2*j] and [i' + 2*j' - 1],
2310 // the code above can't disprove the dependence because the GCD = 1.
2311 // So we consider what happen if i = i' and what happens if j = j'.
2312 // If i = i', we can simplify the subscript to [2*i + 2*j] and [2*j' - 1],
2313 // which is infeasible, so we can disallow the = direction for the i level.
2314 // Setting j = j' doesn't help matters, so we end up with a direction vector
2317 // Given A[5*i + 10*j*M + 9*M*N] and A[15*i + 20*j*M - 21*N*M + 5],
2318 // we need to remember that the constant part is 5 and the RunningGCD should
2319 // be initialized to ExtraGCD = 30.
2320 DEBUG(dbgs() << " ExtraGCD = " << ExtraGCD << '\n');
2322 bool Improved = false;
2324 while (const SCEVAddRecExpr *AddRec =
2325 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2326 Coefficients = AddRec->getStart();
2327 const Loop *CurLoop = AddRec->getLoop();
2328 RunningGCD = ExtraGCD;
2329 const SCEV *SrcCoeff = AddRec->getStepRecurrence(*SE);
2330 const SCEV *DstCoeff = SE->getMinusSCEV(SrcCoeff, SrcCoeff);
2331 const SCEV *Inner = Src;
2332 while (RunningGCD != 1 && isa<SCEVAddRecExpr>(Inner)) {
2333 AddRec = cast<SCEVAddRecExpr>(Inner);
2334 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2335 if (CurLoop == AddRec->getLoop())
2336 ; // SrcCoeff == Coeff
2338 if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Coeff))
2339 // If the coefficient is the product of a constant and other stuff,
2340 // we can use the constant in the GCD computation.
2341 Constant = getConstantPart(Product);
2343 Constant = cast<SCEVConstant>(Coeff);
2344 APInt ConstCoeff = Constant->getValue()->getValue();
2345 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2347 Inner = AddRec->getStart();
2350 while (RunningGCD != 1 && isa<SCEVAddRecExpr>(Inner)) {
2351 AddRec = cast<SCEVAddRecExpr>(Inner);
2352 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2353 if (CurLoop == AddRec->getLoop())
2356 if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Coeff))
2357 // If the coefficient is the product of a constant and other stuff,
2358 // we can use the constant in the GCD computation.
2359 Constant = getConstantPart(Product);
2361 Constant = cast<SCEVConstant>(Coeff);
2362 APInt ConstCoeff = Constant->getValue()->getValue();
2363 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2365 Inner = AddRec->getStart();
2367 Delta = SE->getMinusSCEV(SrcCoeff, DstCoeff);
2368 if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Delta))
2369 // If the coefficient is the product of a constant and other stuff,
2370 // we can use the constant in the GCD computation.
2371 Constant = getConstantPart(Product);
2372 else if (isa<SCEVConstant>(Delta))
2373 Constant = cast<SCEVConstant>(Delta);
2375 // The difference of the two coefficients might not be a product
2376 // or constant, in which case we give up on this direction.
2379 APInt ConstCoeff = Constant->getValue()->getValue();
2380 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
2381 DEBUG(dbgs() << "\tRunningGCD = " << RunningGCD << "\n");
2382 if (RunningGCD != 0) {
2383 Remainder = ConstDelta.srem(RunningGCD);
2384 DEBUG(dbgs() << "\tRemainder = " << Remainder << "\n");
2385 if (Remainder != 0) {
2386 unsigned Level = mapSrcLoop(CurLoop);
2387 Result.DV[Level - 1].Direction &= unsigned(~Dependence::DVEntry::EQ);
2394 DEBUG(dbgs() << "all done\n");
2399 //===----------------------------------------------------------------------===//
2400 // banerjeeMIVtest -
2401 // Use Banerjee's Inequalities to test an MIV subscript pair.
2402 // (Wolfe, in the race-car book, calls this the Extreme Value Test.)
2403 // Generally follows the discussion in Section 2.5.2 of
2405 // Optimizing Supercompilers for Supercomputers
2408 // The inequalities given on page 25 are simplified in that loops are
2409 // normalized so that the lower bound is always 0 and the stride is always 1.
2410 // For example, Wolfe gives
2412 // LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2414 // where A_k is the coefficient of the kth index in the source subscript,
2415 // B_k is the coefficient of the kth index in the destination subscript,
2416 // U_k is the upper bound of the kth index, L_k is the lower bound of the Kth
2417 // index, and N_k is the stride of the kth index. Since all loops are normalized
2418 // by the SCEV package, N_k = 1 and L_k = 0, allowing us to simplify the
2421 // LB^<_k = (A^-_k - B_k)^- (U_k - 0 - 1) + (A_k - B_k)0 - B_k 1
2422 // = (A^-_k - B_k)^- (U_k - 1) - B_k
2424 // Similar simplifications are possible for the other equations.
2426 // When we can't determine the number of iterations for a loop,
2427 // we use NULL as an indicator for the worst case, infinity.
2428 // When computing the upper bound, NULL denotes +inf;
2429 // for the lower bound, NULL denotes -inf.
2431 // Return true if dependence disproved.
2432 bool DependenceAnalysis::banerjeeMIVtest(const SCEV *Src,
2434 const SmallBitVector &Loops,
2435 FullDependence &Result) const {
2436 DEBUG(dbgs() << "starting Banerjee\n");
2437 ++BanerjeeApplications;
2438 DEBUG(dbgs() << " Src = " << *Src << '\n');
2440 CoefficientInfo *A = collectCoeffInfo(Src, true, A0);
2441 DEBUG(dbgs() << " Dst = " << *Dst << '\n');
2443 CoefficientInfo *B = collectCoeffInfo(Dst, false, B0);
2444 BoundInfo *Bound = new BoundInfo[MaxLevels + 1];
2445 const SCEV *Delta = SE->getMinusSCEV(B0, A0);
2446 DEBUG(dbgs() << "\tDelta = " << *Delta << '\n');
2448 // Compute bounds for all the * directions.
2449 DEBUG(dbgs() << "\tBounds[*]\n");
2450 for (unsigned K = 1; K <= MaxLevels; ++K) {
2451 Bound[K].Iterations = A[K].Iterations ? A[K].Iterations : B[K].Iterations;
2452 Bound[K].Direction = Dependence::DVEntry::ALL;
2453 Bound[K].DirSet = Dependence::DVEntry::NONE;
2454 findBoundsALL(A, B, Bound, K);
2456 DEBUG(dbgs() << "\t " << K << '\t');
2457 if (Bound[K].Lower[Dependence::DVEntry::ALL])
2458 DEBUG(dbgs() << *Bound[K].Lower[Dependence::DVEntry::ALL] << '\t');
2460 DEBUG(dbgs() << "-inf\t");
2461 if (Bound[K].Upper[Dependence::DVEntry::ALL])
2462 DEBUG(dbgs() << *Bound[K].Upper[Dependence::DVEntry::ALL] << '\n');
2464 DEBUG(dbgs() << "+inf\n");
2468 // Test the *, *, *, ... case.
2469 bool Disproved = false;
2470 if (testBounds(Dependence::DVEntry::ALL, 0, Bound, Delta)) {
2471 // Explore the direction vector hierarchy.
2472 unsigned DepthExpanded = 0;
2473 unsigned NewDeps = exploreDirections(1, A, B, Bound,
2474 Loops, DepthExpanded, Delta);
2476 bool Improved = false;
2477 for (unsigned K = 1; K <= CommonLevels; ++K) {
2479 unsigned Old = Result.DV[K - 1].Direction;
2480 Result.DV[K - 1].Direction = Old & Bound[K].DirSet;
2481 Improved |= Old != Result.DV[K - 1].Direction;
2482 if (!Result.DV[K - 1].Direction) {
2490 ++BanerjeeSuccesses;
2493 ++BanerjeeIndependence;
2498 ++BanerjeeIndependence;
2508 // Hierarchically expands the direction vector
2509 // search space, combining the directions of discovered dependences
2510 // in the DirSet field of Bound. Returns the number of distinct
2511 // dependences discovered. If the dependence is disproved,
2512 // it will return 0.
2513 unsigned DependenceAnalysis::exploreDirections(unsigned Level,
2517 const SmallBitVector &Loops,
2518 unsigned &DepthExpanded,
2519 const SCEV *Delta) const {
2520 if (Level > CommonLevels) {
2522 DEBUG(dbgs() << "\t[");
2523 for (unsigned K = 1; K <= CommonLevels; ++K) {
2525 Bound[K].DirSet |= Bound[K].Direction;
2527 switch (Bound[K].Direction) {
2528 case Dependence::DVEntry::LT:
2529 DEBUG(dbgs() << " <");
2531 case Dependence::DVEntry::EQ:
2532 DEBUG(dbgs() << " =");
2534 case Dependence::DVEntry::GT:
2535 DEBUG(dbgs() << " >");
2537 case Dependence::DVEntry::ALL:
2538 DEBUG(dbgs() << " *");
2541 llvm_unreachable("unexpected Bound[K].Direction");
2546 DEBUG(dbgs() << " ]\n");
2550 if (Level > DepthExpanded) {
2551 DepthExpanded = Level;
2552 // compute bounds for <, =, > at current level
2553 findBoundsLT(A, B, Bound, Level);
2554 findBoundsGT(A, B, Bound, Level);
2555 findBoundsEQ(A, B, Bound, Level);
2557 DEBUG(dbgs() << "\tBound for level = " << Level << '\n');
2558 DEBUG(dbgs() << "\t <\t");
2559 if (Bound[Level].Lower[Dependence::DVEntry::LT])
2560 DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::LT] << '\t');
2562 DEBUG(dbgs() << "-inf\t");
2563 if (Bound[Level].Upper[Dependence::DVEntry::LT])
2564 DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::LT] << '\n');
2566 DEBUG(dbgs() << "+inf\n");
2567 DEBUG(dbgs() << "\t =\t");
2568 if (Bound[Level].Lower[Dependence::DVEntry::EQ])
2569 DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::EQ] << '\t');
2571 DEBUG(dbgs() << "-inf\t");
2572 if (Bound[Level].Upper[Dependence::DVEntry::EQ])
2573 DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::EQ] << '\n');
2575 DEBUG(dbgs() << "+inf\n");
2576 DEBUG(dbgs() << "\t >\t");
2577 if (Bound[Level].Lower[Dependence::DVEntry::GT])
2578 DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::GT] << '\t');
2580 DEBUG(dbgs() << "-inf\t");
2581 if (Bound[Level].Upper[Dependence::DVEntry::GT])
2582 DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::GT] << '\n');
2584 DEBUG(dbgs() << "+inf\n");
2588 unsigned NewDeps = 0;
2590 // test bounds for <, *, *, ...
2591 if (testBounds(Dependence::DVEntry::LT, Level, Bound, Delta))
2592 NewDeps += exploreDirections(Level + 1, A, B, Bound,
2593 Loops, DepthExpanded, Delta);
2595 // Test bounds for =, *, *, ...
2596 if (testBounds(Dependence::DVEntry::EQ, Level, Bound, Delta))
2597 NewDeps += exploreDirections(Level + 1, A, B, Bound,
2598 Loops, DepthExpanded, Delta);
2600 // test bounds for >, *, *, ...
2601 if (testBounds(Dependence::DVEntry::GT, Level, Bound, Delta))
2602 NewDeps += exploreDirections(Level + 1, A, B, Bound,
2603 Loops, DepthExpanded, Delta);
2605 Bound[Level].Direction = Dependence::DVEntry::ALL;
2609 return exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded, Delta);
2613 // Returns true iff the current bounds are plausible.
2614 bool DependenceAnalysis::testBounds(unsigned char DirKind,
2617 const SCEV *Delta) const {
2618 Bound[Level].Direction = DirKind;
2619 if (const SCEV *LowerBound = getLowerBound(Bound))
2620 if (isKnownPredicate(CmpInst::ICMP_SGT, LowerBound, Delta))
2622 if (const SCEV *UpperBound = getUpperBound(Bound))
2623 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, UpperBound))
2629 // Computes the upper and lower bounds for level K
2630 // using the * direction. Records them in Bound.
2631 // Wolfe gives the equations
2633 // LB^*_k = (A^-_k - B^+_k)(U_k - L_k) + (A_k - B_k)L_k
2634 // UB^*_k = (A^+_k - B^-_k)(U_k - L_k) + (A_k - B_k)L_k
2636 // Since we normalize loops, we can simplify these equations to
2638 // LB^*_k = (A^-_k - B^+_k)U_k
2639 // UB^*_k = (A^+_k - B^-_k)U_k
2641 // We must be careful to handle the case where the upper bound is unknown.
2642 // Note that the lower bound is always <= 0
2643 // and the upper bound is always >= 0.
2644 void DependenceAnalysis::findBoundsALL(CoefficientInfo *A,
2648 Bound[K].Lower[Dependence::DVEntry::ALL] = nullptr; // Default value = -infinity.
2649 Bound[K].Upper[Dependence::DVEntry::ALL] = nullptr; // Default value = +infinity.
2650 if (Bound[K].Iterations) {
2651 Bound[K].Lower[Dependence::DVEntry::ALL] =
2652 SE->getMulExpr(SE->getMinusSCEV(A[K].NegPart, B[K].PosPart),
2653 Bound[K].Iterations);
2654 Bound[K].Upper[Dependence::DVEntry::ALL] =
2655 SE->getMulExpr(SE->getMinusSCEV(A[K].PosPart, B[K].NegPart),
2656 Bound[K].Iterations);
2659 // If the difference is 0, we won't need to know the number of iterations.
2660 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].NegPart, B[K].PosPart))
2661 Bound[K].Lower[Dependence::DVEntry::ALL] =
2662 SE->getConstant(A[K].Coeff->getType(), 0);
2663 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].PosPart, B[K].NegPart))
2664 Bound[K].Upper[Dependence::DVEntry::ALL] =
2665 SE->getConstant(A[K].Coeff->getType(), 0);
2670 // Computes the upper and lower bounds for level K
2671 // using the = direction. Records them in Bound.
2672 // Wolfe gives the equations
2674 // LB^=_k = (A_k - B_k)^- (U_k - L_k) + (A_k - B_k)L_k
2675 // UB^=_k = (A_k - B_k)^+ (U_k - L_k) + (A_k - B_k)L_k
2677 // Since we normalize loops, we can simplify these equations to
2679 // LB^=_k = (A_k - B_k)^- U_k
2680 // UB^=_k = (A_k - B_k)^+ U_k
2682 // We must be careful to handle the case where the upper bound is unknown.
2683 // Note that the lower bound is always <= 0
2684 // and the upper bound is always >= 0.
2685 void DependenceAnalysis::findBoundsEQ(CoefficientInfo *A,
2689 Bound[K].Lower[Dependence::DVEntry::EQ] = nullptr; // Default value = -infinity.
2690 Bound[K].Upper[Dependence::DVEntry::EQ] = nullptr; // Default value = +infinity.
2691 if (Bound[K].Iterations) {
2692 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
2693 const SCEV *NegativePart = getNegativePart(Delta);
2694 Bound[K].Lower[Dependence::DVEntry::EQ] =
2695 SE->getMulExpr(NegativePart, Bound[K].Iterations);
2696 const SCEV *PositivePart = getPositivePart(Delta);
2697 Bound[K].Upper[Dependence::DVEntry::EQ] =
2698 SE->getMulExpr(PositivePart, Bound[K].Iterations);
2701 // If the positive/negative part of the difference is 0,
2702 // we won't need to know the number of iterations.
2703 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
2704 const SCEV *NegativePart = getNegativePart(Delta);
2705 if (NegativePart->isZero())
2706 Bound[K].Lower[Dependence::DVEntry::EQ] = NegativePart; // Zero
2707 const SCEV *PositivePart = getPositivePart(Delta);
2708 if (PositivePart->isZero())
2709 Bound[K].Upper[Dependence::DVEntry::EQ] = PositivePart; // Zero
2714 // Computes the upper and lower bounds for level K
2715 // using the < direction. Records them in Bound.
2716 // Wolfe gives the equations
2718 // LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2719 // UB^<_k = (A^+_k - B_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2721 // Since we normalize loops, we can simplify these equations to
2723 // LB^<_k = (A^-_k - B_k)^- (U_k - 1) - B_k
2724 // UB^<_k = (A^+_k - B_k)^+ (U_k - 1) - B_k
2726 // We must be careful to handle the case where the upper bound is unknown.
2727 void DependenceAnalysis::findBoundsLT(CoefficientInfo *A,
2731 Bound[K].Lower[Dependence::DVEntry::LT] = nullptr; // Default value = -infinity.
2732 Bound[K].Upper[Dependence::DVEntry::LT] = nullptr; // Default value = +infinity.
2733 if (Bound[K].Iterations) {
2734 const SCEV *Iter_1 =
2735 SE->getMinusSCEV(Bound[K].Iterations,
2736 SE->getConstant(Bound[K].Iterations->getType(), 1));
2737 const SCEV *NegPart =
2738 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
2739 Bound[K].Lower[Dependence::DVEntry::LT] =
2740 SE->getMinusSCEV(SE->getMulExpr(NegPart, Iter_1), B[K].Coeff);
2741 const SCEV *PosPart =
2742 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
2743 Bound[K].Upper[Dependence::DVEntry::LT] =
2744 SE->getMinusSCEV(SE->getMulExpr(PosPart, Iter_1), B[K].Coeff);
2747 // If the positive/negative part of the difference is 0,
2748 // we won't need to know the number of iterations.
2749 const SCEV *NegPart =
2750 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
2751 if (NegPart->isZero())
2752 Bound[K].Lower[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
2753 const SCEV *PosPart =
2754 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
2755 if (PosPart->isZero())
2756 Bound[K].Upper[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
2761 // Computes the upper and lower bounds for level K
2762 // using the > direction. Records them in Bound.
2763 // Wolfe gives the equations
2765 // LB^>_k = (A_k - B^+_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
2766 // UB^>_k = (A_k - B^-_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
2768 // Since we normalize loops, we can simplify these equations to
2770 // LB^>_k = (A_k - B^+_k)^- (U_k - 1) + A_k
2771 // UB^>_k = (A_k - B^-_k)^+ (U_k - 1) + A_k
2773 // We must be careful to handle the case where the upper bound is unknown.
2774 void DependenceAnalysis::findBoundsGT(CoefficientInfo *A,
2778 Bound[K].Lower[Dependence::DVEntry::GT] = nullptr; // Default value = -infinity.
2779 Bound[K].Upper[Dependence::DVEntry::GT] = nullptr; // Default value = +infinity.
2780 if (Bound[K].Iterations) {
2781 const SCEV *Iter_1 =
2782 SE->getMinusSCEV(Bound[K].Iterations,
2783 SE->getConstant(Bound[K].Iterations->getType(), 1));
2784 const SCEV *NegPart =
2785 getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
2786 Bound[K].Lower[Dependence::DVEntry::GT] =
2787 SE->getAddExpr(SE->getMulExpr(NegPart, Iter_1), A[K].Coeff);
2788 const SCEV *PosPart =
2789 getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
2790 Bound[K].Upper[Dependence::DVEntry::GT] =
2791 SE->getAddExpr(SE->getMulExpr(PosPart, Iter_1), A[K].Coeff);
2794 // If the positive/negative part of the difference is 0,
2795 // we won't need to know the number of iterations.
2796 const SCEV *NegPart = getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
2797 if (NegPart->isZero())
2798 Bound[K].Lower[Dependence::DVEntry::GT] = A[K].Coeff;
2799 const SCEV *PosPart = getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
2800 if (PosPart->isZero())
2801 Bound[K].Upper[Dependence::DVEntry::GT] = A[K].Coeff;
2807 const SCEV *DependenceAnalysis::getPositivePart(const SCEV *X) const {
2808 return SE->getSMaxExpr(X, SE->getConstant(X->getType(), 0));
2813 const SCEV *DependenceAnalysis::getNegativePart(const SCEV *X) const {
2814 return SE->getSMinExpr(X, SE->getConstant(X->getType(), 0));
2818 // Walks through the subscript,
2819 // collecting each coefficient, the associated loop bounds,
2820 // and recording its positive and negative parts for later use.
2821 DependenceAnalysis::CoefficientInfo *
2822 DependenceAnalysis::collectCoeffInfo(const SCEV *Subscript,
2824 const SCEV *&Constant) const {
2825 const SCEV *Zero = SE->getConstant(Subscript->getType(), 0);
2826 CoefficientInfo *CI = new CoefficientInfo[MaxLevels + 1];
2827 for (unsigned K = 1; K <= MaxLevels; ++K) {
2829 CI[K].PosPart = Zero;
2830 CI[K].NegPart = Zero;
2831 CI[K].Iterations = nullptr;
2833 while (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Subscript)) {
2834 const Loop *L = AddRec->getLoop();
2835 unsigned K = SrcFlag ? mapSrcLoop(L) : mapDstLoop(L);
2836 CI[K].Coeff = AddRec->getStepRecurrence(*SE);
2837 CI[K].PosPart = getPositivePart(CI[K].Coeff);
2838 CI[K].NegPart = getNegativePart(CI[K].Coeff);
2839 CI[K].Iterations = collectUpperBound(L, Subscript->getType());
2840 Subscript = AddRec->getStart();
2842 Constant = Subscript;
2844 DEBUG(dbgs() << "\tCoefficient Info\n");
2845 for (unsigned K = 1; K <= MaxLevels; ++K) {
2846 DEBUG(dbgs() << "\t " << K << "\t" << *CI[K].Coeff);
2847 DEBUG(dbgs() << "\tPos Part = ");
2848 DEBUG(dbgs() << *CI[K].PosPart);
2849 DEBUG(dbgs() << "\tNeg Part = ");
2850 DEBUG(dbgs() << *CI[K].NegPart);
2851 DEBUG(dbgs() << "\tUpper Bound = ");
2852 if (CI[K].Iterations)
2853 DEBUG(dbgs() << *CI[K].Iterations);
2855 DEBUG(dbgs() << "+inf");
2856 DEBUG(dbgs() << '\n');
2858 DEBUG(dbgs() << "\t Constant = " << *Subscript << '\n');
2864 // Looks through all the bounds info and
2865 // computes the lower bound given the current direction settings
2866 // at each level. If the lower bound for any level is -inf,
2867 // the result is -inf.
2868 const SCEV *DependenceAnalysis::getLowerBound(BoundInfo *Bound) const {
2869 const SCEV *Sum = Bound[1].Lower[Bound[1].Direction];
2870 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
2871 if (Bound[K].Lower[Bound[K].Direction])
2872 Sum = SE->getAddExpr(Sum, Bound[K].Lower[Bound[K].Direction]);
2880 // Looks through all the bounds info and
2881 // computes the upper bound given the current direction settings
2882 // at each level. If the upper bound at any level is +inf,
2883 // the result is +inf.
2884 const SCEV *DependenceAnalysis::getUpperBound(BoundInfo *Bound) const {
2885 const SCEV *Sum = Bound[1].Upper[Bound[1].Direction];
2886 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
2887 if (Bound[K].Upper[Bound[K].Direction])
2888 Sum = SE->getAddExpr(Sum, Bound[K].Upper[Bound[K].Direction]);
2896 //===----------------------------------------------------------------------===//
2897 // Constraint manipulation for Delta test.
2899 // Given a linear SCEV,
2900 // return the coefficient (the step)
2901 // corresponding to the specified loop.
2902 // If there isn't one, return 0.
2903 // For example, given a*i + b*j + c*k, zeroing the coefficient
2904 // corresponding to the j loop would yield b.
2905 const SCEV *DependenceAnalysis::findCoefficient(const SCEV *Expr,
2906 const Loop *TargetLoop) const {
2907 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
2909 return SE->getConstant(Expr->getType(), 0);
2910 if (AddRec->getLoop() == TargetLoop)
2911 return AddRec->getStepRecurrence(*SE);
2912 return findCoefficient(AddRec->getStart(), TargetLoop);
2916 // Given a linear SCEV,
2917 // return the SCEV given by zeroing out the coefficient
2918 // corresponding to the specified loop.
2919 // For example, given a*i + b*j + c*k, zeroing the coefficient
2920 // corresponding to the j loop would yield a*i + c*k.
2921 const SCEV *DependenceAnalysis::zeroCoefficient(const SCEV *Expr,
2922 const Loop *TargetLoop) const {
2923 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
2925 return Expr; // ignore
2926 if (AddRec->getLoop() == TargetLoop)
2927 return AddRec->getStart();
2928 return SE->getAddRecExpr(zeroCoefficient(AddRec->getStart(), TargetLoop),
2929 AddRec->getStepRecurrence(*SE),
2931 AddRec->getNoWrapFlags());
2935 // Given a linear SCEV Expr,
2936 // return the SCEV given by adding some Value to the
2937 // coefficient corresponding to the specified TargetLoop.
2938 // For example, given a*i + b*j + c*k, adding 1 to the coefficient
2939 // corresponding to the j loop would yield a*i + (b+1)*j + c*k.
2940 const SCEV *DependenceAnalysis::addToCoefficient(const SCEV *Expr,
2941 const Loop *TargetLoop,
2942 const SCEV *Value) const {
2943 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
2944 if (!AddRec) // create a new addRec
2945 return SE->getAddRecExpr(Expr,
2948 SCEV::FlagAnyWrap); // Worst case, with no info.
2949 if (AddRec->getLoop() == TargetLoop) {
2950 const SCEV *Sum = SE->getAddExpr(AddRec->getStepRecurrence(*SE), Value);
2952 return AddRec->getStart();
2953 return SE->getAddRecExpr(AddRec->getStart(),
2956 AddRec->getNoWrapFlags());
2958 if (SE->isLoopInvariant(AddRec, TargetLoop))
2959 return SE->getAddRecExpr(AddRec, Value, TargetLoop, SCEV::FlagAnyWrap);
2960 return SE->getAddRecExpr(
2961 addToCoefficient(AddRec->getStart(), TargetLoop, Value),
2962 AddRec->getStepRecurrence(*SE), AddRec->getLoop(),
2963 AddRec->getNoWrapFlags());
2967 // Review the constraints, looking for opportunities
2968 // to simplify a subscript pair (Src and Dst).
2969 // Return true if some simplification occurs.
2970 // If the simplification isn't exact (that is, if it is conservative
2971 // in terms of dependence), set consistent to false.
2972 // Corresponds to Figure 5 from the paper
2974 // Practical Dependence Testing
2975 // Goff, Kennedy, Tseng
2977 bool DependenceAnalysis::propagate(const SCEV *&Src,
2979 SmallBitVector &Loops,
2980 SmallVectorImpl<Constraint> &Constraints,
2982 bool Result = false;
2983 for (int LI = Loops.find_first(); LI >= 0; LI = Loops.find_next(LI)) {
2984 DEBUG(dbgs() << "\t Constraint[" << LI << "] is");
2985 DEBUG(Constraints[LI].dump(dbgs()));
2986 if (Constraints[LI].isDistance())
2987 Result |= propagateDistance(Src, Dst, Constraints[LI], Consistent);
2988 else if (Constraints[LI].isLine())
2989 Result |= propagateLine(Src, Dst, Constraints[LI], Consistent);
2990 else if (Constraints[LI].isPoint())
2991 Result |= propagatePoint(Src, Dst, Constraints[LI]);
2997 // Attempt to propagate a distance
2998 // constraint into a subscript pair (Src and Dst).
2999 // Return true if some simplification occurs.
3000 // If the simplification isn't exact (that is, if it is conservative
3001 // in terms of dependence), set consistent to false.
3002 bool DependenceAnalysis::propagateDistance(const SCEV *&Src,
3004 Constraint &CurConstraint,
3006 const Loop *CurLoop = CurConstraint.getAssociatedLoop();
3007 DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n");
3008 const SCEV *A_K = findCoefficient(Src, CurLoop);
3011 const SCEV *DA_K = SE->getMulExpr(A_K, CurConstraint.getD());
3012 Src = SE->getMinusSCEV(Src, DA_K);
3013 Src = zeroCoefficient(Src, CurLoop);
3014 DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n");
3015 DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n");
3016 Dst = addToCoefficient(Dst, CurLoop, SE->getNegativeSCEV(A_K));
3017 DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n");
3018 if (!findCoefficient(Dst, CurLoop)->isZero())
3024 // Attempt to propagate a line
3025 // constraint into a subscript pair (Src and Dst).
3026 // Return true if some simplification occurs.
3027 // If the simplification isn't exact (that is, if it is conservative
3028 // in terms of dependence), set consistent to false.
3029 bool DependenceAnalysis::propagateLine(const SCEV *&Src,
3031 Constraint &CurConstraint,
3033 const Loop *CurLoop = CurConstraint.getAssociatedLoop();
3034 const SCEV *A = CurConstraint.getA();
3035 const SCEV *B = CurConstraint.getB();
3036 const SCEV *C = CurConstraint.getC();
3037 DEBUG(dbgs() << "\t\tA = " << *A << ", B = " << *B << ", C = " << *C << "\n");
3038 DEBUG(dbgs() << "\t\tSrc = " << *Src << "\n");
3039 DEBUG(dbgs() << "\t\tDst = " << *Dst << "\n");
3041 const SCEVConstant *Bconst = dyn_cast<SCEVConstant>(B);
3042 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3043 if (!Bconst || !Cconst) return false;
3044 APInt Beta = Bconst->getValue()->getValue();
3045 APInt Charlie = Cconst->getValue()->getValue();
3046 APInt CdivB = Charlie.sdiv(Beta);
3047 assert(Charlie.srem(Beta) == 0 && "C should be evenly divisible by B");
3048 const SCEV *AP_K = findCoefficient(Dst, CurLoop);
3049 // Src = SE->getAddExpr(Src, SE->getMulExpr(AP_K, SE->getConstant(CdivB)));
3050 Src = SE->getMinusSCEV(Src, SE->getMulExpr(AP_K, SE->getConstant(CdivB)));
3051 Dst = zeroCoefficient(Dst, CurLoop);
3052 if (!findCoefficient(Src, CurLoop)->isZero())
3055 else if (B->isZero()) {
3056 const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A);
3057 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3058 if (!Aconst || !Cconst) return false;
3059 APInt Alpha = Aconst->getValue()->getValue();
3060 APInt Charlie = Cconst->getValue()->getValue();
3061 APInt CdivA = Charlie.sdiv(Alpha);
3062 assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A");
3063 const SCEV *A_K = findCoefficient(Src, CurLoop);
3064 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA)));
3065 Src = zeroCoefficient(Src, CurLoop);
3066 if (!findCoefficient(Dst, CurLoop)->isZero())
3069 else if (isKnownPredicate(CmpInst::ICMP_EQ, A, B)) {
3070 const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A);
3071 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3072 if (!Aconst || !Cconst) return false;
3073 APInt Alpha = Aconst->getValue()->getValue();
3074 APInt Charlie = Cconst->getValue()->getValue();
3075 APInt CdivA = Charlie.sdiv(Alpha);
3076 assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A");
3077 const SCEV *A_K = findCoefficient(Src, CurLoop);
3078 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA)));
3079 Src = zeroCoefficient(Src, CurLoop);
3080 Dst = addToCoefficient(Dst, CurLoop, A_K);
3081 if (!findCoefficient(Dst, CurLoop)->isZero())
3085 // paper is incorrect here, or perhaps just misleading
3086 const SCEV *A_K = findCoefficient(Src, CurLoop);
3087 Src = SE->getMulExpr(Src, A);
3088 Dst = SE->getMulExpr(Dst, A);
3089 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, C));
3090 Src = zeroCoefficient(Src, CurLoop);
3091 Dst = addToCoefficient(Dst, CurLoop, SE->getMulExpr(A_K, B));
3092 if (!findCoefficient(Dst, CurLoop)->isZero())
3095 DEBUG(dbgs() << "\t\tnew Src = " << *Src << "\n");
3096 DEBUG(dbgs() << "\t\tnew Dst = " << *Dst << "\n");
3101 // Attempt to propagate a point
3102 // constraint into a subscript pair (Src and Dst).
3103 // Return true if some simplification occurs.
3104 bool DependenceAnalysis::propagatePoint(const SCEV *&Src,
3106 Constraint &CurConstraint) {
3107 const Loop *CurLoop = CurConstraint.getAssociatedLoop();
3108 const SCEV *A_K = findCoefficient(Src, CurLoop);
3109 const SCEV *AP_K = findCoefficient(Dst, CurLoop);
3110 const SCEV *XA_K = SE->getMulExpr(A_K, CurConstraint.getX());
3111 const SCEV *YAP_K = SE->getMulExpr(AP_K, CurConstraint.getY());
3112 DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n");
3113 Src = SE->getAddExpr(Src, SE->getMinusSCEV(XA_K, YAP_K));
3114 Src = zeroCoefficient(Src, CurLoop);
3115 DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n");
3116 DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n");
3117 Dst = zeroCoefficient(Dst, CurLoop);
3118 DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n");
3123 // Update direction vector entry based on the current constraint.
3124 void DependenceAnalysis::updateDirection(Dependence::DVEntry &Level,
3125 const Constraint &CurConstraint
3127 DEBUG(dbgs() << "\tUpdate direction, constraint =");
3128 DEBUG(CurConstraint.dump(dbgs()));
3129 if (CurConstraint.isAny())
3131 else if (CurConstraint.isDistance()) {
3132 // this one is consistent, the others aren't
3133 Level.Scalar = false;
3134 Level.Distance = CurConstraint.getD();
3135 unsigned NewDirection = Dependence::DVEntry::NONE;
3136 if (!SE->isKnownNonZero(Level.Distance)) // if may be zero
3137 NewDirection = Dependence::DVEntry::EQ;
3138 if (!SE->isKnownNonPositive(Level.Distance)) // if may be positive
3139 NewDirection |= Dependence::DVEntry::LT;
3140 if (!SE->isKnownNonNegative(Level.Distance)) // if may be negative
3141 NewDirection |= Dependence::DVEntry::GT;
3142 Level.Direction &= NewDirection;
3144 else if (CurConstraint.isLine()) {
3145 Level.Scalar = false;
3146 Level.Distance = nullptr;
3147 // direction should be accurate
3149 else if (CurConstraint.isPoint()) {
3150 Level.Scalar = false;
3151 Level.Distance = nullptr;
3152 unsigned NewDirection = Dependence::DVEntry::NONE;
3153 if (!isKnownPredicate(CmpInst::ICMP_NE,
3154 CurConstraint.getY(),
3155 CurConstraint.getX()))
3157 NewDirection |= Dependence::DVEntry::EQ;
3158 if (!isKnownPredicate(CmpInst::ICMP_SLE,
3159 CurConstraint.getY(),
3160 CurConstraint.getX()))
3162 NewDirection |= Dependence::DVEntry::LT;
3163 if (!isKnownPredicate(CmpInst::ICMP_SGE,
3164 CurConstraint.getY(),
3165 CurConstraint.getX()))
3167 NewDirection |= Dependence::DVEntry::GT;
3168 Level.Direction &= NewDirection;
3171 llvm_unreachable("constraint has unexpected kind");
3174 /// Check if we can delinearize the subscripts. If the SCEVs representing the
3175 /// source and destination array references are recurrences on a nested loop,
3176 /// this function flattens the nested recurrences into separate recurrences
3177 /// for each loop level.
3178 bool DependenceAnalysis::tryDelinearize(const SCEV *SrcSCEV,
3179 const SCEV *DstSCEV,
3180 SmallVectorImpl<Subscript> &Pair,
3181 const SCEV *ElementSize) const {
3182 const SCEVUnknown *SrcBase =
3183 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcSCEV));
3184 const SCEVUnknown *DstBase =
3185 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstSCEV));
3187 if (!SrcBase || !DstBase || SrcBase != DstBase)
3190 SrcSCEV = SE->getMinusSCEV(SrcSCEV, SrcBase);
3191 DstSCEV = SE->getMinusSCEV(DstSCEV, DstBase);
3193 const SCEVAddRecExpr *SrcAR = dyn_cast<SCEVAddRecExpr>(SrcSCEV);
3194 const SCEVAddRecExpr *DstAR = dyn_cast<SCEVAddRecExpr>(DstSCEV);
3195 if (!SrcAR || !DstAR || !SrcAR->isAffine() || !DstAR->isAffine())
3198 // First step: collect parametric terms in both array references.
3199 SmallVector<const SCEV *, 4> Terms;
3200 SrcAR->collectParametricTerms(*SE, Terms);
3201 DstAR->collectParametricTerms(*SE, Terms);
3203 // Second step: find subscript sizes.
3204 SmallVector<const SCEV *, 4> Sizes;
3205 SE->findArrayDimensions(Terms, Sizes, ElementSize);
3207 // Third step: compute the access functions for each subscript.
3208 SmallVector<const SCEV *, 4> SrcSubscripts, DstSubscripts;
3209 SrcAR->computeAccessFunctions(*SE, SrcSubscripts, Sizes);
3210 DstAR->computeAccessFunctions(*SE, DstSubscripts, Sizes);
3212 // Fail when there is only a subscript: that's a linearized access function.
3213 if (SrcSubscripts.size() < 2 || DstSubscripts.size() < 2 ||
3214 SrcSubscripts.size() != DstSubscripts.size())
3217 int size = SrcSubscripts.size();
3220 dbgs() << "\nSrcSubscripts: ";
3221 for (int i = 0; i < size; i++)
3222 dbgs() << *SrcSubscripts[i];
3223 dbgs() << "\nDstSubscripts: ";
3224 for (int i = 0; i < size; i++)
3225 dbgs() << *DstSubscripts[i];
3228 // The delinearization transforms a single-subscript MIV dependence test into
3229 // a multi-subscript SIV dependence test that is easier to compute. So we
3230 // resize Pair to contain as many pairs of subscripts as the delinearization
3231 // has found, and then initialize the pairs following the delinearization.
3233 for (int i = 0; i < size; ++i) {
3234 Pair[i].Src = SrcSubscripts[i];
3235 Pair[i].Dst = DstSubscripts[i];
3237 // FIXME: we should record the bounds SrcSizes[i] and DstSizes[i] that the
3238 // delinearization has found, and add these constraints to the dependence
3239 // check to avoid memory accesses overflow from one dimension into another.
3240 // This is related to the problem of determining the existence of data
3241 // dependences in array accesses using a different number of subscripts: in
3242 // C one can access an array A[100][100]; as A[0][9999], *A[9999], etc.
3248 //===----------------------------------------------------------------------===//
3251 // For debugging purposes, dump a small bit vector to dbgs().
3252 static void dumpSmallBitVector(SmallBitVector &BV) {
3254 for (int VI = BV.find_first(); VI >= 0; VI = BV.find_next(VI)) {
3256 if (BV.find_next(VI) >= 0)
3265 // Returns NULL if there is no dependence.
3266 // Otherwise, return a Dependence with as many details as possible.
3267 // Corresponds to Section 3.1 in the paper
3269 // Practical Dependence Testing
3270 // Goff, Kennedy, Tseng
3273 // Care is required to keep the routine below, getSplitIteration(),
3274 // up to date with respect to this routine.
3275 std::unique_ptr<Dependence>
3276 DependenceAnalysis::depends(Instruction *Src, Instruction *Dst,
3277 bool PossiblyLoopIndependent) {
3279 PossiblyLoopIndependent = false;
3281 if ((!Src->mayReadFromMemory() && !Src->mayWriteToMemory()) ||
3282 (!Dst->mayReadFromMemory() && !Dst->mayWriteToMemory()))
3283 // if both instructions don't reference memory, there's no dependence
3286 if (!isLoadOrStore(Src) || !isLoadOrStore(Dst)) {
3287 // can only analyze simple loads and stores, i.e., no calls, invokes, etc.
3288 DEBUG(dbgs() << "can only handle simple loads and stores\n");
3289 return make_unique<Dependence>(Src, Dst);
3292 Value *SrcPtr = getPointerOperand(Src);
3293 Value *DstPtr = getPointerOperand(Dst);
3295 switch (underlyingObjectsAlias(AA, DstPtr, SrcPtr)) {
3296 case AliasAnalysis::MayAlias:
3297 case AliasAnalysis::PartialAlias:
3298 // cannot analyse objects if we don't understand their aliasing.
3299 DEBUG(dbgs() << "can't analyze may or partial alias\n");
3300 return make_unique<Dependence>(Src, Dst);
3301 case AliasAnalysis::NoAlias:
3302 // If the objects noalias, they are distinct, accesses are independent.
3303 DEBUG(dbgs() << "no alias\n");
3305 case AliasAnalysis::MustAlias:
3306 break; // The underlying objects alias; test accesses for dependence.
3309 // establish loop nesting levels
3310 establishNestingLevels(Src, Dst);
3311 DEBUG(dbgs() << " common nesting levels = " << CommonLevels << "\n");
3312 DEBUG(dbgs() << " maximum nesting levels = " << MaxLevels << "\n");
3314 FullDependence Result(Src, Dst, PossiblyLoopIndependent, CommonLevels);
3317 // See if there are GEPs we can use.
3318 bool UsefulGEP = false;
3319 GEPOperator *SrcGEP = dyn_cast<GEPOperator>(SrcPtr);
3320 GEPOperator *DstGEP = dyn_cast<GEPOperator>(DstPtr);
3321 if (SrcGEP && DstGEP &&
3322 SrcGEP->getPointerOperandType() == DstGEP->getPointerOperandType()) {
3323 const SCEV *SrcPtrSCEV = SE->getSCEV(SrcGEP->getPointerOperand());
3324 const SCEV *DstPtrSCEV = SE->getSCEV(DstGEP->getPointerOperand());
3325 DEBUG(dbgs() << " SrcPtrSCEV = " << *SrcPtrSCEV << "\n");
3326 DEBUG(dbgs() << " DstPtrSCEV = " << *DstPtrSCEV << "\n");
3329 isLoopInvariant(SrcPtrSCEV, LI->getLoopFor(Src->getParent())) &&
3330 isLoopInvariant(DstPtrSCEV, LI->getLoopFor(Dst->getParent()));
3332 unsigned Pairs = UsefulGEP ? SrcGEP->idx_end() - SrcGEP->idx_begin() : 1;
3333 SmallVector<Subscript, 4> Pair(Pairs);
3335 DEBUG(dbgs() << " using GEPs\n");
3337 for (GEPOperator::const_op_iterator SrcIdx = SrcGEP->idx_begin(),
3338 SrcEnd = SrcGEP->idx_end(),
3339 DstIdx = DstGEP->idx_begin();
3341 ++SrcIdx, ++DstIdx, ++P) {
3342 Pair[P].Src = SE->getSCEV(*SrcIdx);
3343 Pair[P].Dst = SE->getSCEV(*DstIdx);
3347 DEBUG(dbgs() << " ignoring GEPs\n");
3348 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
3349 const SCEV *DstSCEV = SE->getSCEV(DstPtr);
3350 DEBUG(dbgs() << " SrcSCEV = " << *SrcSCEV << "\n");
3351 DEBUG(dbgs() << " DstSCEV = " << *DstSCEV << "\n");
3352 Pair[0].Src = SrcSCEV;
3353 Pair[0].Dst = DstSCEV;
3356 if (Delinearize && Pairs == 1 && CommonLevels > 1 &&
3357 tryDelinearize(Pair[0].Src, Pair[0].Dst, Pair, SE->getElementSize(Src))) {
3358 DEBUG(dbgs() << " delinerized GEP\n");
3359 Pairs = Pair.size();
3362 for (unsigned P = 0; P < Pairs; ++P) {
3363 Pair[P].Loops.resize(MaxLevels + 1);
3364 Pair[P].GroupLoops.resize(MaxLevels + 1);
3365 Pair[P].Group.resize(Pairs);
3366 removeMatchingExtensions(&Pair[P]);
3367 Pair[P].Classification =
3368 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()),
3369 Pair[P].Dst, LI->getLoopFor(Dst->getParent()),
3371 Pair[P].GroupLoops = Pair[P].Loops;
3372 Pair[P].Group.set(P);
3373 DEBUG(dbgs() << " subscript " << P << "\n");
3374 DEBUG(dbgs() << "\tsrc = " << *Pair[P].Src << "\n");
3375 DEBUG(dbgs() << "\tdst = " << *Pair[P].Dst << "\n");
3376 DEBUG(dbgs() << "\tclass = " << Pair[P].Classification << "\n");
3377 DEBUG(dbgs() << "\tloops = ");
3378 DEBUG(dumpSmallBitVector(Pair[P].Loops));
3381 SmallBitVector Separable(Pairs);
3382 SmallBitVector Coupled(Pairs);
3384 // Partition subscripts into separable and minimally-coupled groups
3385 // Algorithm in paper is algorithmically better;
3386 // this may be faster in practice. Check someday.
3388 // Here's an example of how it works. Consider this code:
3395 // A[i][j][k][m] = ...;
3396 // ... = A[0][j][l][i + j];
3403 // There are 4 subscripts here:
3407 // 3 [m] and [i + j]
3409 // We've already classified each subscript pair as ZIV, SIV, etc.,
3410 // and collected all the loops mentioned by pair P in Pair[P].Loops.
3411 // In addition, we've initialized Pair[P].GroupLoops to Pair[P].Loops
3412 // and set Pair[P].Group = {P}.
3414 // Src Dst Classification Loops GroupLoops Group
3415 // 0 [i] [0] SIV {1} {1} {0}
3416 // 1 [j] [j] SIV {2} {2} {1}
3417 // 2 [k] [l] RDIV {3,4} {3,4} {2}
3418 // 3 [m] [i + j] MIV {1,2,5} {1,2,5} {3}
3420 // For each subscript SI 0 .. 3, we consider each remaining subscript, SJ.
3421 // So, 0 is compared against 1, 2, and 3; 1 is compared against 2 and 3, etc.
3423 // We begin by comparing 0 and 1. The intersection of the GroupLoops is empty.
3424 // Next, 0 and 2. Again, the intersection of their GroupLoops is empty.
3425 // Next 0 and 3. The intersection of their GroupLoop = {1}, not empty,
3426 // so Pair[3].Group = {0,3} and Done = false (that is, 0 will not be added
3427 // to either Separable or Coupled).
3429 // Next, we consider 1 and 2. The intersection of the GroupLoops is empty.
3430 // Next, 1 and 3. The intersectionof their GroupLoops = {2}, not empty,
3431 // so Pair[3].Group = {0, 1, 3} and Done = false.
3433 // Next, we compare 2 against 3. The intersection of the GroupLoops is empty.
3434 // Since Done remains true, we add 2 to the set of Separable pairs.
3436 // Finally, we consider 3. There's nothing to compare it with,
3437 // so Done remains true and we add it to the Coupled set.
3438 // Pair[3].Group = {0, 1, 3} and GroupLoops = {1, 2, 5}.
3440 // In the end, we've got 1 separable subscript and 1 coupled group.
3441 for (unsigned SI = 0; SI < Pairs; ++SI) {
3442 if (Pair[SI].Classification == Subscript::NonLinear) {
3443 // ignore these, but collect loops for later
3444 ++NonlinearSubscriptPairs;
3445 collectCommonLoops(Pair[SI].Src,
3446 LI->getLoopFor(Src->getParent()),
3448 collectCommonLoops(Pair[SI].Dst,
3449 LI->getLoopFor(Dst->getParent()),
3451 Result.Consistent = false;
3453 else if (Pair[SI].Classification == Subscript::ZIV) {
3458 // SIV, RDIV, or MIV, so check for coupled group
3460 for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) {
3461 SmallBitVector Intersection = Pair[SI].GroupLoops;
3462 Intersection &= Pair[SJ].GroupLoops;
3463 if (Intersection.any()) {
3464 // accumulate set of all the loops in group
3465 Pair[SJ].GroupLoops |= Pair[SI].GroupLoops;
3466 // accumulate set of all subscripts in group
3467 Pair[SJ].Group |= Pair[SI].Group;
3472 if (Pair[SI].Group.count() == 1) {
3474 ++SeparableSubscriptPairs;
3478 ++CoupledSubscriptPairs;
3484 DEBUG(dbgs() << " Separable = ");
3485 DEBUG(dumpSmallBitVector(Separable));
3486 DEBUG(dbgs() << " Coupled = ");
3487 DEBUG(dumpSmallBitVector(Coupled));
3489 Constraint NewConstraint;
3490 NewConstraint.setAny(SE);
3492 // test separable subscripts
3493 for (int SI = Separable.find_first(); SI >= 0; SI = Separable.find_next(SI)) {
3494 DEBUG(dbgs() << "testing subscript " << SI);
3495 switch (Pair[SI].Classification) {
3496 case Subscript::ZIV:
3497 DEBUG(dbgs() << ", ZIV\n");
3498 if (testZIV(Pair[SI].Src, Pair[SI].Dst, Result))
3501 case Subscript::SIV: {
3502 DEBUG(dbgs() << ", SIV\n");
3504 const SCEV *SplitIter = nullptr;
3505 if (testSIV(Pair[SI].Src, Pair[SI].Dst, Level,
3506 Result, NewConstraint, SplitIter))
3510 case Subscript::RDIV:
3511 DEBUG(dbgs() << ", RDIV\n");
3512 if (testRDIV(Pair[SI].Src, Pair[SI].Dst, Result))
3515 case Subscript::MIV:
3516 DEBUG(dbgs() << ", MIV\n");
3517 if (testMIV(Pair[SI].Src, Pair[SI].Dst, Pair[SI].Loops, Result))
3521 llvm_unreachable("subscript has unexpected classification");
3525 if (Coupled.count()) {
3526 // test coupled subscript groups
3527 DEBUG(dbgs() << "starting on coupled subscripts\n");
3528 DEBUG(dbgs() << "MaxLevels + 1 = " << MaxLevels + 1 << "\n");
3529 SmallVector<Constraint, 4> Constraints(MaxLevels + 1);
3530 for (unsigned II = 0; II <= MaxLevels; ++II)
3531 Constraints[II].setAny(SE);
3532 for (int SI = Coupled.find_first(); SI >= 0; SI = Coupled.find_next(SI)) {
3533 DEBUG(dbgs() << "testing subscript group " << SI << " { ");
3534 SmallBitVector Group(Pair[SI].Group);
3535 SmallBitVector Sivs(Pairs);
3536 SmallBitVector Mivs(Pairs);
3537 SmallBitVector ConstrainedLevels(MaxLevels + 1);
3538 for (int SJ = Group.find_first(); SJ >= 0; SJ = Group.find_next(SJ)) {
3539 DEBUG(dbgs() << SJ << " ");
3540 if (Pair[SJ].Classification == Subscript::SIV)
3545 DEBUG(dbgs() << "}\n");
3546 while (Sivs.any()) {
3547 bool Changed = false;
3548 for (int SJ = Sivs.find_first(); SJ >= 0; SJ = Sivs.find_next(SJ)) {
3549 DEBUG(dbgs() << "testing subscript " << SJ << ", SIV\n");
3550 // SJ is an SIV subscript that's part of the current coupled group
3552 const SCEV *SplitIter = nullptr;
3553 DEBUG(dbgs() << "SIV\n");
3554 if (testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level,
3555 Result, NewConstraint, SplitIter))
3557 ConstrainedLevels.set(Level);
3558 if (intersectConstraints(&Constraints[Level], &NewConstraint)) {
3559 if (Constraints[Level].isEmpty()) {
3560 ++DeltaIndependence;
3568 // propagate, possibly creating new SIVs and ZIVs
3569 DEBUG(dbgs() << " propagating\n");
3570 DEBUG(dbgs() << "\tMivs = ");
3571 DEBUG(dumpSmallBitVector(Mivs));
3572 for (int SJ = Mivs.find_first(); SJ >= 0; SJ = Mivs.find_next(SJ)) {
3573 // SJ is an MIV subscript that's part of the current coupled group
3574 DEBUG(dbgs() << "\tSJ = " << SJ << "\n");
3575 if (propagate(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops,
3576 Constraints, Result.Consistent)) {
3577 DEBUG(dbgs() << "\t Changed\n");
3578 ++DeltaPropagations;
3579 Pair[SJ].Classification =
3580 classifyPair(Pair[SJ].Src, LI->getLoopFor(Src->getParent()),
3581 Pair[SJ].Dst, LI->getLoopFor(Dst->getParent()),
3583 switch (Pair[SJ].Classification) {
3584 case Subscript::ZIV:
3585 DEBUG(dbgs() << "ZIV\n");
3586 if (testZIV(Pair[SJ].Src, Pair[SJ].Dst, Result))
3590 case Subscript::SIV:
3594 case Subscript::RDIV:
3595 case Subscript::MIV:
3598 llvm_unreachable("bad subscript classification");
3605 // test & propagate remaining RDIVs
3606 for (int SJ = Mivs.find_first(); SJ >= 0; SJ = Mivs.find_next(SJ)) {
3607 if (Pair[SJ].Classification == Subscript::RDIV) {
3608 DEBUG(dbgs() << "RDIV test\n");
3609 if (testRDIV(Pair[SJ].Src, Pair[SJ].Dst, Result))
3611 // I don't yet understand how to propagate RDIV results
3616 // test remaining MIVs
3617 // This code is temporary.
3618 // Better to somehow test all remaining subscripts simultaneously.
3619 for (int SJ = Mivs.find_first(); SJ >= 0; SJ = Mivs.find_next(SJ)) {
3620 if (Pair[SJ].Classification == Subscript::MIV) {
3621 DEBUG(dbgs() << "MIV test\n");
3622 if (testMIV(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops, Result))
3626 llvm_unreachable("expected only MIV subscripts at this point");
3629 // update Result.DV from constraint vector
3630 DEBUG(dbgs() << " updating\n");
3631 for (int SJ = ConstrainedLevels.find_first();
3632 SJ >= 0; SJ = ConstrainedLevels.find_next(SJ)) {
3633 updateDirection(Result.DV[SJ - 1], Constraints[SJ]);
3634 if (Result.DV[SJ - 1].Direction == Dependence::DVEntry::NONE)
3640 // Make sure the Scalar flags are set correctly.
3641 SmallBitVector CompleteLoops(MaxLevels + 1);
3642 for (unsigned SI = 0; SI < Pairs; ++SI)
3643 CompleteLoops |= Pair[SI].Loops;
3644 for (unsigned II = 1; II <= CommonLevels; ++II)
3645 if (CompleteLoops[II])
3646 Result.DV[II - 1].Scalar = false;
3648 if (PossiblyLoopIndependent) {
3649 // Make sure the LoopIndependent flag is set correctly.
3650 // All directions must include equal, otherwise no
3651 // loop-independent dependence is possible.
3652 for (unsigned II = 1; II <= CommonLevels; ++II) {
3653 if (!(Result.getDirection(II) & Dependence::DVEntry::EQ)) {
3654 Result.LoopIndependent = false;
3660 // On the other hand, if all directions are equal and there's no
3661 // loop-independent dependence possible, then no dependence exists.
3662 bool AllEqual = true;
3663 for (unsigned II = 1; II <= CommonLevels; ++II) {
3664 if (Result.getDirection(II) != Dependence::DVEntry::EQ) {
3673 auto Final = make_unique<FullDependence>(Result);
3674 Result.DV = nullptr;
3675 return std::move(Final);
3680 //===----------------------------------------------------------------------===//
3681 // getSplitIteration -
3682 // Rather than spend rarely-used space recording the splitting iteration
3683 // during the Weak-Crossing SIV test, we re-compute it on demand.
3684 // The re-computation is basically a repeat of the entire dependence test,
3685 // though simplified since we know that the dependence exists.
3686 // It's tedious, since we must go through all propagations, etc.
3688 // Care is required to keep this code up to date with respect to the routine
3689 // above, depends().
3691 // Generally, the dependence analyzer will be used to build
3692 // a dependence graph for a function (basically a map from instructions
3693 // to dependences). Looking for cycles in the graph shows us loops
3694 // that cannot be trivially vectorized/parallelized.
3696 // We can try to improve the situation by examining all the dependences
3697 // that make up the cycle, looking for ones we can break.
3698 // Sometimes, peeling the first or last iteration of a loop will break
3699 // dependences, and we've got flags for those possibilities.
3700 // Sometimes, splitting a loop at some other iteration will do the trick,
3701 // and we've got a flag for that case. Rather than waste the space to
3702 // record the exact iteration (since we rarely know), we provide
3703 // a method that calculates the iteration. It's a drag that it must work
3704 // from scratch, but wonderful in that it's possible.
3706 // Here's an example:
3708 // for (i = 0; i < 10; i++)
3712 // There's a loop-carried flow dependence from the store to the load,
3713 // found by the weak-crossing SIV test. The dependence will have a flag,
3714 // indicating that the dependence can be broken by splitting the loop.
3715 // Calling getSplitIteration will return 5.
3716 // Splitting the loop breaks the dependence, like so:
3718 // for (i = 0; i <= 5; i++)
3721 // for (i = 6; i < 10; i++)
3725 // breaks the dependence and allows us to vectorize/parallelize
3727 const SCEV *DependenceAnalysis::getSplitIteration(const Dependence &Dep,
3728 unsigned SplitLevel) {
3729 assert(Dep.isSplitable(SplitLevel) &&
3730 "Dep should be splitable at SplitLevel");
3731 Instruction *Src = Dep.getSrc();
3732 Instruction *Dst = Dep.getDst();
3733 assert(Src->mayReadFromMemory() || Src->mayWriteToMemory());
3734 assert(Dst->mayReadFromMemory() || Dst->mayWriteToMemory());
3735 assert(isLoadOrStore(Src));
3736 assert(isLoadOrStore(Dst));
3737 Value *SrcPtr = getPointerOperand(Src);
3738 Value *DstPtr = getPointerOperand(Dst);
3739 assert(underlyingObjectsAlias(AA, DstPtr, SrcPtr) ==
3740 AliasAnalysis::MustAlias);
3742 // establish loop nesting levels
3743 establishNestingLevels(Src, Dst);
3745 FullDependence Result(Src, Dst, false, CommonLevels);
3747 // See if there are GEPs we can use.
3748 bool UsefulGEP = false;
3749 GEPOperator *SrcGEP = dyn_cast<GEPOperator>(SrcPtr);
3750 GEPOperator *DstGEP = dyn_cast<GEPOperator>(DstPtr);
3751 if (SrcGEP && DstGEP &&
3752 SrcGEP->getPointerOperandType() == DstGEP->getPointerOperandType()) {
3753 const SCEV *SrcPtrSCEV = SE->getSCEV(SrcGEP->getPointerOperand());
3754 const SCEV *DstPtrSCEV = SE->getSCEV(DstGEP->getPointerOperand());
3756 isLoopInvariant(SrcPtrSCEV, LI->getLoopFor(Src->getParent())) &&
3757 isLoopInvariant(DstPtrSCEV, LI->getLoopFor(Dst->getParent()));
3759 unsigned Pairs = UsefulGEP ? SrcGEP->idx_end() - SrcGEP->idx_begin() : 1;
3760 SmallVector<Subscript, 4> Pair(Pairs);
3763 for (GEPOperator::const_op_iterator SrcIdx = SrcGEP->idx_begin(),
3764 SrcEnd = SrcGEP->idx_end(),
3765 DstIdx = DstGEP->idx_begin();
3767 ++SrcIdx, ++DstIdx, ++P) {
3768 Pair[P].Src = SE->getSCEV(*SrcIdx);
3769 Pair[P].Dst = SE->getSCEV(*DstIdx);
3773 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
3774 const SCEV *DstSCEV = SE->getSCEV(DstPtr);
3775 Pair[0].Src = SrcSCEV;
3776 Pair[0].Dst = DstSCEV;
3779 if (Delinearize && Pairs == 1 && CommonLevels > 1 &&
3780 tryDelinearize(Pair[0].Src, Pair[0].Dst, Pair, SE->getElementSize(Src))) {
3781 DEBUG(dbgs() << " delinerized GEP\n");
3782 Pairs = Pair.size();
3785 for (unsigned P = 0; P < Pairs; ++P) {
3786 Pair[P].Loops.resize(MaxLevels + 1);
3787 Pair[P].GroupLoops.resize(MaxLevels + 1);
3788 Pair[P].Group.resize(Pairs);
3789 removeMatchingExtensions(&Pair[P]);
3790 Pair[P].Classification =
3791 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()),
3792 Pair[P].Dst, LI->getLoopFor(Dst->getParent()),
3794 Pair[P].GroupLoops = Pair[P].Loops;
3795 Pair[P].Group.set(P);
3798 SmallBitVector Separable(Pairs);
3799 SmallBitVector Coupled(Pairs);
3801 // partition subscripts into separable and minimally-coupled groups
3802 for (unsigned SI = 0; SI < Pairs; ++SI) {
3803 if (Pair[SI].Classification == Subscript::NonLinear) {
3804 // ignore these, but collect loops for later
3805 collectCommonLoops(Pair[SI].Src,
3806 LI->getLoopFor(Src->getParent()),
3808 collectCommonLoops(Pair[SI].Dst,
3809 LI->getLoopFor(Dst->getParent()),
3811 Result.Consistent = false;
3813 else if (Pair[SI].Classification == Subscript::ZIV)
3816 // SIV, RDIV, or MIV, so check for coupled group
3818 for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) {
3819 SmallBitVector Intersection = Pair[SI].GroupLoops;
3820 Intersection &= Pair[SJ].GroupLoops;
3821 if (Intersection.any()) {
3822 // accumulate set of all the loops in group
3823 Pair[SJ].GroupLoops |= Pair[SI].GroupLoops;
3824 // accumulate set of all subscripts in group
3825 Pair[SJ].Group |= Pair[SI].Group;
3830 if (Pair[SI].Group.count() == 1)
3838 Constraint NewConstraint;
3839 NewConstraint.setAny(SE);
3841 // test separable subscripts
3842 for (int SI = Separable.find_first(); SI >= 0; SI = Separable.find_next(SI)) {
3843 switch (Pair[SI].Classification) {
3844 case Subscript::SIV: {
3846 const SCEV *SplitIter = nullptr;
3847 (void) testSIV(Pair[SI].Src, Pair[SI].Dst, Level,
3848 Result, NewConstraint, SplitIter);
3849 if (Level == SplitLevel) {
3850 assert(SplitIter != nullptr);
3855 case Subscript::ZIV:
3856 case Subscript::RDIV:
3857 case Subscript::MIV:
3860 llvm_unreachable("subscript has unexpected classification");
3864 if (Coupled.count()) {
3865 // test coupled subscript groups
3866 SmallVector<Constraint, 4> Constraints(MaxLevels + 1);
3867 for (unsigned II = 0; II <= MaxLevels; ++II)
3868 Constraints[II].setAny(SE);
3869 for (int SI = Coupled.find_first(); SI >= 0; SI = Coupled.find_next(SI)) {
3870 SmallBitVector Group(Pair[SI].Group);
3871 SmallBitVector Sivs(Pairs);
3872 SmallBitVector Mivs(Pairs);
3873 SmallBitVector ConstrainedLevels(MaxLevels + 1);
3874 for (int SJ = Group.find_first(); SJ >= 0; SJ = Group.find_next(SJ)) {
3875 if (Pair[SJ].Classification == Subscript::SIV)
3880 while (Sivs.any()) {
3881 bool Changed = false;
3882 for (int SJ = Sivs.find_first(); SJ >= 0; SJ = Sivs.find_next(SJ)) {
3883 // SJ is an SIV subscript that's part of the current coupled group
3885 const SCEV *SplitIter = nullptr;
3886 (void) testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level,
3887 Result, NewConstraint, SplitIter);
3888 if (Level == SplitLevel && SplitIter)
3890 ConstrainedLevels.set(Level);
3891 if (intersectConstraints(&Constraints[Level], &NewConstraint))
3896 // propagate, possibly creating new SIVs and ZIVs
3897 for (int SJ = Mivs.find_first(); SJ >= 0; SJ = Mivs.find_next(SJ)) {
3898 // SJ is an MIV subscript that's part of the current coupled group
3899 if (propagate(Pair[SJ].Src, Pair[SJ].Dst,
3900 Pair[SJ].Loops, Constraints, Result.Consistent)) {
3901 Pair[SJ].Classification =
3902 classifyPair(Pair[SJ].Src, LI->getLoopFor(Src->getParent()),
3903 Pair[SJ].Dst, LI->getLoopFor(Dst->getParent()),
3905 switch (Pair[SJ].Classification) {
3906 case Subscript::ZIV:
3909 case Subscript::SIV:
3913 case Subscript::RDIV:
3914 case Subscript::MIV:
3917 llvm_unreachable("bad subscript classification");
3925 llvm_unreachable("somehow reached end of routine");