1 //===-- DependenceAnalysis.cpp - DA Implementation --------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // DependenceAnalysis is an LLVM pass that analyses dependences between memory
10 // accesses. Currently, it is an (incomplete) implementation of the approach
13 // Practical Dependence Testing
14 // Goff, Kennedy, Tseng
17 // There's a single entry point that analyzes the dependence between a pair
18 // of memory references in a function, returning either NULL, for no dependence,
19 // or a more-or-less detailed description of the dependence between them.
21 // Currently, the implementation cannot propagate constraints between
22 // coupled RDIV subscripts and lacks a multi-subscript MIV test.
23 // Both of these are conservative weaknesses;
24 // that is, not a source of correctness problems.
26 // Since Clang linearizes some array subscripts, the dependence
27 // analysis is using SCEV->delinearize to recover the representation of multiple
28 // subscripts, and thus avoid the more expensive and less precise MIV tests. The
29 // delinearization is controlled by the flag -da-delinearize.
31 // We should pay some careful attention to the possibility of integer overflow
32 // in the implementation of the various tests. This could happen with Add,
33 // Subtract, or Multiply, with both APInt's and SCEV's.
35 // Some non-linear subscript pairs can be handled by the GCD test
36 // (and perhaps other tests).
37 // Should explore how often these things occur.
39 // Finally, it seems like certain test cases expose weaknesses in the SCEV
40 // simplification, especially in the handling of sign and zero extensions.
41 // It could be useful to spend time exploring these.
43 // Please note that this is work in progress and the interface is subject to
46 //===----------------------------------------------------------------------===//
48 // In memory of Ken Kennedy, 1945 - 2007 //
50 //===----------------------------------------------------------------------===//
52 #include "llvm/Analysis/DependenceAnalysis.h"
53 #include "llvm/ADT/Statistic.h"
54 #include "llvm/Analysis/AliasAnalysis.h"
55 #include "llvm/Analysis/Delinearization.h"
56 #include "llvm/Analysis/LoopInfo.h"
57 #include "llvm/Analysis/ScalarEvolution.h"
58 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
59 #include "llvm/Analysis/ValueTracking.h"
60 #include "llvm/IR/InstIterator.h"
61 #include "llvm/IR/Module.h"
62 #include "llvm/InitializePasses.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(true), cl::Hidden
,
110 cl::desc("Try to delinearize array references."));
111 static cl::opt
<bool> DisableDelinearizationChecks(
112 "da-disable-delinearization-checks", cl::Hidden
,
114 "Disable checks that try to statically verify validity of "
115 "delinearized subscripts. Enabling this option may result in incorrect "
116 "dependence vectors for languages that allow the subscript of one "
117 "dimension to underflow or overflow into another dimension."));
119 static cl::opt
<unsigned> MIVMaxLevelThreshold(
120 "da-miv-max-level-threshold", cl::init(7), cl::Hidden
,
121 cl::desc("Maximum depth allowed for the recursive algorithm used to "
122 "explore MIV direction vectors."));
124 //===----------------------------------------------------------------------===//
127 DependenceAnalysis::Result
128 DependenceAnalysis::run(Function
&F
, FunctionAnalysisManager
&FAM
) {
129 auto &AA
= FAM
.getResult
<AAManager
>(F
);
130 auto &SE
= FAM
.getResult
<ScalarEvolutionAnalysis
>(F
);
131 auto &LI
= FAM
.getResult
<LoopAnalysis
>(F
);
132 return DependenceInfo(&F
, &AA
, &SE
, &LI
);
135 AnalysisKey
DependenceAnalysis::Key
;
137 INITIALIZE_PASS_BEGIN(DependenceAnalysisWrapperPass
, "da",
138 "Dependence Analysis", true, true)
139 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass
)
140 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass
)
141 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass
)
142 INITIALIZE_PASS_END(DependenceAnalysisWrapperPass
, "da", "Dependence Analysis",
145 char DependenceAnalysisWrapperPass::ID
= 0;
147 DependenceAnalysisWrapperPass::DependenceAnalysisWrapperPass()
149 initializeDependenceAnalysisWrapperPassPass(*PassRegistry::getPassRegistry());
152 FunctionPass
*llvm::createDependenceAnalysisWrapperPass() {
153 return new DependenceAnalysisWrapperPass();
156 bool DependenceAnalysisWrapperPass::runOnFunction(Function
&F
) {
157 auto &AA
= getAnalysis
<AAResultsWrapperPass
>().getAAResults();
158 auto &SE
= getAnalysis
<ScalarEvolutionWrapperPass
>().getSE();
159 auto &LI
= getAnalysis
<LoopInfoWrapperPass
>().getLoopInfo();
160 info
.reset(new DependenceInfo(&F
, &AA
, &SE
, &LI
));
164 DependenceInfo
&DependenceAnalysisWrapperPass::getDI() const { return *info
; }
166 void DependenceAnalysisWrapperPass::releaseMemory() { info
.reset(); }
168 void DependenceAnalysisWrapperPass::getAnalysisUsage(AnalysisUsage
&AU
) const {
169 AU
.setPreservesAll();
170 AU
.addRequiredTransitive
<AAResultsWrapperPass
>();
171 AU
.addRequiredTransitive
<ScalarEvolutionWrapperPass
>();
172 AU
.addRequiredTransitive
<LoopInfoWrapperPass
>();
175 // Used to test the dependence analyzer.
176 // Looks through the function, noting instructions that may access memory.
177 // Calls depends() on every possible pair and prints out the result.
178 // Ignores all other instructions.
179 static void dumpExampleDependence(raw_ostream
&OS
, DependenceInfo
*DA
,
180 ScalarEvolution
&SE
, bool NormalizeResults
) {
181 auto *F
= DA
->getFunction();
182 for (inst_iterator SrcI
= inst_begin(F
), SrcE
= inst_end(F
); SrcI
!= SrcE
;
184 if (SrcI
->mayReadOrWriteMemory()) {
185 for (inst_iterator DstI
= SrcI
, DstE
= inst_end(F
);
186 DstI
!= DstE
; ++DstI
) {
187 if (DstI
->mayReadOrWriteMemory()) {
188 OS
<< "Src:" << *SrcI
<< " --> Dst:" << *DstI
<< "\n";
189 OS
<< " da analyze - ";
190 if (auto D
= DA
->depends(&*SrcI
, &*DstI
, true)) {
191 // Normalize negative direction vectors if required by clients.
192 if (NormalizeResults
&& D
->normalize(&SE
))
193 OS
<< "normalized - ";
195 for (unsigned Level
= 1; Level
<= D
->getLevels(); Level
++) {
196 if (D
->isSplitable(Level
)) {
197 OS
<< " da analyze - split level = " << Level
;
198 OS
<< ", iteration = " << *DA
->getSplitIteration(*D
, Level
);
211 void DependenceAnalysisWrapperPass::print(raw_ostream
&OS
,
212 const Module
*) const {
213 dumpExampleDependence(OS
, info
.get(),
214 getAnalysis
<ScalarEvolutionWrapperPass
>().getSE(), false);
218 DependenceAnalysisPrinterPass::run(Function
&F
, FunctionAnalysisManager
&FAM
) {
219 OS
<< "'Dependence Analysis' for function '" << F
.getName() << "':\n";
220 dumpExampleDependence(OS
, &FAM
.getResult
<DependenceAnalysis
>(F
),
221 FAM
.getResult
<ScalarEvolutionAnalysis
>(F
),
223 return PreservedAnalyses::all();
226 //===----------------------------------------------------------------------===//
227 // Dependence methods
229 // Returns true if this is an input dependence.
230 bool Dependence::isInput() const {
231 return Src
->mayReadFromMemory() && Dst
->mayReadFromMemory();
235 // Returns true if this is an output dependence.
236 bool Dependence::isOutput() const {
237 return Src
->mayWriteToMemory() && Dst
->mayWriteToMemory();
241 // Returns true if this is an flow (aka true) dependence.
242 bool Dependence::isFlow() const {
243 return Src
->mayWriteToMemory() && Dst
->mayReadFromMemory();
247 // Returns true if this is an anti dependence.
248 bool Dependence::isAnti() const {
249 return Src
->mayReadFromMemory() && Dst
->mayWriteToMemory();
253 // Returns true if a particular level is scalar; that is,
254 // if no subscript in the source or destination mention the induction
255 // variable associated with the loop at this level.
256 // Leave this out of line, so it will serve as a virtual method anchor
257 bool Dependence::isScalar(unsigned level
) const {
262 //===----------------------------------------------------------------------===//
263 // FullDependence methods
265 FullDependence::FullDependence(Instruction
*Source
, Instruction
*Destination
,
266 bool PossiblyLoopIndependent
,
267 unsigned CommonLevels
)
268 : Dependence(Source
, Destination
), Levels(CommonLevels
),
269 LoopIndependent(PossiblyLoopIndependent
) {
272 DV
= std::make_unique
<DVEntry
[]>(CommonLevels
);
275 // FIXME: in some cases the meaning of a negative direction vector
276 // may not be straightforward, e.g.,
277 // for (int i = 0; i < 32; ++i) {
279 // Dst: use(A[31 - i]);
282 // flow { Src[i] -> Dst[31 - i] : when i >= 16 } and
283 // anti { Dst[i] -> Src[31 - i] : when i < 16 },
285 // As long as a dependence result contains '>' ('<>', '<=>', "*"), it
286 // means that a reversed/normalized dependence needs to be considered
287 // as well. Nevertheless, current isDirectionNegative() only returns
288 // true with a '>' or '>=' dependency for ease of canonicalizing the
289 // dependency vector, since the reverse of '<>', '<=>' and "*" is itself.
290 bool FullDependence::isDirectionNegative() const {
291 for (unsigned Level
= 1; Level
<= Levels
; ++Level
) {
292 unsigned char Direction
= DV
[Level
- 1].Direction
;
293 if (Direction
== Dependence::DVEntry::EQ
)
295 if (Direction
== Dependence::DVEntry::GT
||
296 Direction
== Dependence::DVEntry::GE
)
303 bool FullDependence::normalize(ScalarEvolution
*SE
) {
304 if (!isDirectionNegative())
307 LLVM_DEBUG(dbgs() << "Before normalizing negative direction vectors:\n";
310 for (unsigned Level
= 1; Level
<= Levels
; ++Level
) {
311 unsigned char Direction
= DV
[Level
- 1].Direction
;
312 // Reverse the direction vector, this means LT becomes GT
313 // and GT becomes LT.
314 unsigned char RevDirection
= Direction
& Dependence::DVEntry::EQ
;
315 if (Direction
& Dependence::DVEntry::LT
)
316 RevDirection
|= Dependence::DVEntry::GT
;
317 if (Direction
& Dependence::DVEntry::GT
)
318 RevDirection
|= Dependence::DVEntry::LT
;
319 DV
[Level
- 1].Direction
= RevDirection
;
320 // Reverse the dependence distance as well.
321 if (DV
[Level
- 1].Distance
!= nullptr)
322 DV
[Level
- 1].Distance
=
323 SE
->getNegativeSCEV(DV
[Level
- 1].Distance
);
326 LLVM_DEBUG(dbgs() << "After normalizing negative direction vectors:\n";
331 // The rest are simple getters that hide the implementation.
333 // getDirection - Returns the direction associated with a particular level.
334 unsigned FullDependence::getDirection(unsigned Level
) const {
335 assert(0 < Level
&& Level
<= Levels
&& "Level out of range");
336 return DV
[Level
- 1].Direction
;
340 // Returns the distance (or NULL) associated with a particular level.
341 const SCEV
*FullDependence::getDistance(unsigned Level
) const {
342 assert(0 < Level
&& Level
<= Levels
&& "Level out of range");
343 return DV
[Level
- 1].Distance
;
347 // Returns true if a particular level is scalar; that is,
348 // if no subscript in the source or destination mention the induction
349 // variable associated with the loop at this level.
350 bool FullDependence::isScalar(unsigned Level
) const {
351 assert(0 < Level
&& Level
<= Levels
&& "Level out of range");
352 return DV
[Level
- 1].Scalar
;
356 // Returns true if peeling the first iteration from this loop
357 // will break this dependence.
358 bool FullDependence::isPeelFirst(unsigned Level
) const {
359 assert(0 < Level
&& Level
<= Levels
&& "Level out of range");
360 return DV
[Level
- 1].PeelFirst
;
364 // Returns true if peeling the last iteration from this loop
365 // will break this dependence.
366 bool FullDependence::isPeelLast(unsigned Level
) const {
367 assert(0 < Level
&& Level
<= Levels
&& "Level out of range");
368 return DV
[Level
- 1].PeelLast
;
372 // Returns true if splitting this loop will break the dependence.
373 bool FullDependence::isSplitable(unsigned Level
) const {
374 assert(0 < Level
&& Level
<= Levels
&& "Level out of range");
375 return DV
[Level
- 1].Splitable
;
379 //===----------------------------------------------------------------------===//
380 // DependenceInfo::Constraint methods
382 // If constraint is a point <X, Y>, returns X.
384 const SCEV
*DependenceInfo::Constraint::getX() const {
385 assert(Kind
== Point
&& "Kind should be Point");
390 // If constraint is a point <X, Y>, returns Y.
392 const SCEV
*DependenceInfo::Constraint::getY() const {
393 assert(Kind
== Point
&& "Kind should be Point");
398 // If constraint is a line AX + BY = C, returns A.
400 const SCEV
*DependenceInfo::Constraint::getA() const {
401 assert((Kind
== Line
|| Kind
== Distance
) &&
402 "Kind should be Line (or Distance)");
407 // If constraint is a line AX + BY = C, returns B.
409 const SCEV
*DependenceInfo::Constraint::getB() const {
410 assert((Kind
== Line
|| Kind
== Distance
) &&
411 "Kind should be Line (or Distance)");
416 // If constraint is a line AX + BY = C, returns C.
418 const SCEV
*DependenceInfo::Constraint::getC() const {
419 assert((Kind
== Line
|| Kind
== Distance
) &&
420 "Kind should be Line (or Distance)");
425 // If constraint is a distance, returns D.
427 const SCEV
*DependenceInfo::Constraint::getD() const {
428 assert(Kind
== Distance
&& "Kind should be Distance");
429 return SE
->getNegativeSCEV(C
);
433 // Returns the loop associated with this constraint.
434 const Loop
*DependenceInfo::Constraint::getAssociatedLoop() const {
435 assert((Kind
== Distance
|| Kind
== Line
|| Kind
== Point
) &&
436 "Kind should be Distance, Line, or Point");
437 return AssociatedLoop
;
440 void DependenceInfo::Constraint::setPoint(const SCEV
*X
, const SCEV
*Y
,
441 const Loop
*CurLoop
) {
445 AssociatedLoop
= CurLoop
;
448 void DependenceInfo::Constraint::setLine(const SCEV
*AA
, const SCEV
*BB
,
449 const SCEV
*CC
, const Loop
*CurLoop
) {
454 AssociatedLoop
= CurLoop
;
457 void DependenceInfo::Constraint::setDistance(const SCEV
*D
,
458 const Loop
*CurLoop
) {
460 A
= SE
->getOne(D
->getType());
461 B
= SE
->getNegativeSCEV(A
);
462 C
= SE
->getNegativeSCEV(D
);
463 AssociatedLoop
= CurLoop
;
466 void DependenceInfo::Constraint::setEmpty() { Kind
= Empty
; }
468 void DependenceInfo::Constraint::setAny(ScalarEvolution
*NewSE
) {
473 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
474 // For debugging purposes. Dumps the constraint out to OS.
475 LLVM_DUMP_METHOD
void DependenceInfo::Constraint::dump(raw_ostream
&OS
) const {
481 OS
<< " Point is <" << *getX() << ", " << *getY() << ">\n";
482 else if (isDistance())
483 OS
<< " Distance is " << *getD() <<
484 " (" << *getA() << "*X + " << *getB() << "*Y = " << *getC() << ")\n";
486 OS
<< " Line is " << *getA() << "*X + " <<
487 *getB() << "*Y = " << *getC() << "\n";
489 llvm_unreachable("unknown constraint type in Constraint::dump");
494 // Updates X with the intersection
495 // of the Constraints X and Y. Returns true if X has changed.
496 // Corresponds to Figure 4 from the paper
498 // Practical Dependence Testing
499 // Goff, Kennedy, Tseng
501 bool DependenceInfo::intersectConstraints(Constraint
*X
, const Constraint
*Y
) {
503 LLVM_DEBUG(dbgs() << "\tintersect constraints\n");
504 LLVM_DEBUG(dbgs() << "\t X ="; X
->dump(dbgs()));
505 LLVM_DEBUG(dbgs() << "\t Y ="; Y
->dump(dbgs()));
506 assert(!Y
->isPoint() && "Y must not be a Point");
520 if (X
->isDistance() && Y
->isDistance()) {
521 LLVM_DEBUG(dbgs() << "\t intersect 2 distances\n");
522 if (isKnownPredicate(CmpInst::ICMP_EQ
, X
->getD(), Y
->getD()))
524 if (isKnownPredicate(CmpInst::ICMP_NE
, X
->getD(), Y
->getD())) {
529 // Hmmm, interesting situation.
530 // I guess if either is constant, keep it and ignore the other.
531 if (isa
<SCEVConstant
>(Y
->getD())) {
538 // At this point, the pseudo-code in Figure 4 of the paper
539 // checks if (X->isPoint() && Y->isPoint()).
540 // This case can't occur in our implementation,
541 // since a Point can only arise as the result of intersecting
542 // two Line constraints, and the right-hand value, Y, is never
543 // the result of an intersection.
544 assert(!(X
->isPoint() && Y
->isPoint()) &&
545 "We shouldn't ever see X->isPoint() && Y->isPoint()");
547 if (X
->isLine() && Y
->isLine()) {
548 LLVM_DEBUG(dbgs() << "\t intersect 2 lines\n");
549 const SCEV
*Prod1
= SE
->getMulExpr(X
->getA(), Y
->getB());
550 const SCEV
*Prod2
= SE
->getMulExpr(X
->getB(), Y
->getA());
551 if (isKnownPredicate(CmpInst::ICMP_EQ
, Prod1
, Prod2
)) {
552 // slopes are equal, so lines are parallel
553 LLVM_DEBUG(dbgs() << "\t\tsame slope\n");
554 Prod1
= SE
->getMulExpr(X
->getC(), Y
->getB());
555 Prod2
= SE
->getMulExpr(X
->getB(), Y
->getC());
556 if (isKnownPredicate(CmpInst::ICMP_EQ
, Prod1
, Prod2
))
558 if (isKnownPredicate(CmpInst::ICMP_NE
, Prod1
, Prod2
)) {
565 if (isKnownPredicate(CmpInst::ICMP_NE
, Prod1
, Prod2
)) {
566 // slopes differ, so lines intersect
567 LLVM_DEBUG(dbgs() << "\t\tdifferent slopes\n");
568 const SCEV
*C1B2
= SE
->getMulExpr(X
->getC(), Y
->getB());
569 const SCEV
*C1A2
= SE
->getMulExpr(X
->getC(), Y
->getA());
570 const SCEV
*C2B1
= SE
->getMulExpr(Y
->getC(), X
->getB());
571 const SCEV
*C2A1
= SE
->getMulExpr(Y
->getC(), X
->getA());
572 const SCEV
*A1B2
= SE
->getMulExpr(X
->getA(), Y
->getB());
573 const SCEV
*A2B1
= SE
->getMulExpr(Y
->getA(), X
->getB());
574 const SCEVConstant
*C1A2_C2A1
=
575 dyn_cast
<SCEVConstant
>(SE
->getMinusSCEV(C1A2
, C2A1
));
576 const SCEVConstant
*C1B2_C2B1
=
577 dyn_cast
<SCEVConstant
>(SE
->getMinusSCEV(C1B2
, C2B1
));
578 const SCEVConstant
*A1B2_A2B1
=
579 dyn_cast
<SCEVConstant
>(SE
->getMinusSCEV(A1B2
, A2B1
));
580 const SCEVConstant
*A2B1_A1B2
=
581 dyn_cast
<SCEVConstant
>(SE
->getMinusSCEV(A2B1
, A1B2
));
582 if (!C1B2_C2B1
|| !C1A2_C2A1
||
583 !A1B2_A2B1
|| !A2B1_A1B2
)
585 APInt Xtop
= C1B2_C2B1
->getAPInt();
586 APInt Xbot
= A1B2_A2B1
->getAPInt();
587 APInt Ytop
= C1A2_C2A1
->getAPInt();
588 APInt Ybot
= A2B1_A1B2
->getAPInt();
589 LLVM_DEBUG(dbgs() << "\t\tXtop = " << Xtop
<< "\n");
590 LLVM_DEBUG(dbgs() << "\t\tXbot = " << Xbot
<< "\n");
591 LLVM_DEBUG(dbgs() << "\t\tYtop = " << Ytop
<< "\n");
592 LLVM_DEBUG(dbgs() << "\t\tYbot = " << Ybot
<< "\n");
593 APInt Xq
= Xtop
; // these need to be initialized, even
594 APInt Xr
= Xtop
; // though they're just going to be overwritten
595 APInt::sdivrem(Xtop
, Xbot
, Xq
, Xr
);
598 APInt::sdivrem(Ytop
, Ybot
, Yq
, Yr
);
599 if (Xr
!= 0 || Yr
!= 0) {
604 LLVM_DEBUG(dbgs() << "\t\tX = " << Xq
<< ", Y = " << Yq
<< "\n");
605 if (Xq
.slt(0) || Yq
.slt(0)) {
610 if (const SCEVConstant
*CUB
=
611 collectConstantUpperBound(X
->getAssociatedLoop(), Prod1
->getType())) {
612 const APInt
&UpperBound
= CUB
->getAPInt();
613 LLVM_DEBUG(dbgs() << "\t\tupper bound = " << UpperBound
<< "\n");
614 if (Xq
.sgt(UpperBound
) || Yq
.sgt(UpperBound
)) {
620 X
->setPoint(SE
->getConstant(Xq
),
622 X
->getAssociatedLoop());
629 // if (X->isLine() && Y->isPoint()) This case can't occur.
630 assert(!(X
->isLine() && Y
->isPoint()) && "This case should never occur");
632 if (X
->isPoint() && Y
->isLine()) {
633 LLVM_DEBUG(dbgs() << "\t intersect Point and Line\n");
634 const SCEV
*A1X1
= SE
->getMulExpr(Y
->getA(), X
->getX());
635 const SCEV
*B1Y1
= SE
->getMulExpr(Y
->getB(), X
->getY());
636 const SCEV
*Sum
= SE
->getAddExpr(A1X1
, B1Y1
);
637 if (isKnownPredicate(CmpInst::ICMP_EQ
, Sum
, Y
->getC()))
639 if (isKnownPredicate(CmpInst::ICMP_NE
, Sum
, Y
->getC())) {
647 llvm_unreachable("shouldn't reach the end of Constraint intersection");
652 //===----------------------------------------------------------------------===//
653 // DependenceInfo methods
655 // For debugging purposes. Dumps a dependence to OS.
656 void Dependence::dump(raw_ostream
&OS
) const {
657 bool Splitable
= false;
671 unsigned Levels
= getLevels();
673 for (unsigned II
= 1; II
<= Levels
; ++II
) {
678 const SCEV
*Distance
= getDistance(II
);
681 else if (isScalar(II
))
684 unsigned Direction
= getDirection(II
);
685 if (Direction
== DVEntry::ALL
)
688 if (Direction
& DVEntry::LT
)
690 if (Direction
& DVEntry::EQ
)
692 if (Direction
& DVEntry::GT
)
701 if (isLoopIndependent())
710 // Returns NoAlias/MayAliass/MustAlias for two memory locations based upon their
711 // underlaying objects. If LocA and LocB are known to not alias (for any reason:
712 // tbaa, non-overlapping regions etc), then it is known there is no dependecy.
713 // Otherwise the underlying objects are checked to see if they point to
714 // different identifiable objects.
715 static AliasResult
underlyingObjectsAlias(AAResults
*AA
,
716 const DataLayout
&DL
,
717 const MemoryLocation
&LocA
,
718 const MemoryLocation
&LocB
) {
719 // Check the original locations (minus size) for noalias, which can happen for
720 // tbaa, incompatible underlying object locations, etc.
721 MemoryLocation LocAS
=
722 MemoryLocation::getBeforeOrAfter(LocA
.Ptr
, LocA
.AATags
);
723 MemoryLocation LocBS
=
724 MemoryLocation::getBeforeOrAfter(LocB
.Ptr
, LocB
.AATags
);
725 if (AA
->isNoAlias(LocAS
, LocBS
))
726 return AliasResult::NoAlias
;
728 // Check the underlying objects are the same
729 const Value
*AObj
= getUnderlyingObject(LocA
.Ptr
);
730 const Value
*BObj
= getUnderlyingObject(LocB
.Ptr
);
732 // If the underlying objects are the same, they must alias
734 return AliasResult::MustAlias
;
736 // We may have hit the recursion limit for underlying objects, or have
737 // underlying objects where we don't know they will alias.
738 if (!isIdentifiedObject(AObj
) || !isIdentifiedObject(BObj
))
739 return AliasResult::MayAlias
;
741 // Otherwise we know the objects are different and both identified objects so
743 return AliasResult::NoAlias
;
747 // Returns true if the load or store can be analyzed. Atomic and volatile
748 // operations have properties which this analysis does not understand.
750 bool isLoadOrStore(const Instruction
*I
) {
751 if (const LoadInst
*LI
= dyn_cast
<LoadInst
>(I
))
752 return LI
->isUnordered();
753 else if (const StoreInst
*SI
= dyn_cast
<StoreInst
>(I
))
754 return SI
->isUnordered();
759 // Examines the loop nesting of the Src and Dst
760 // instructions and establishes their shared loops. Sets the variables
761 // CommonLevels, SrcLevels, and MaxLevels.
762 // The source and destination instructions needn't be contained in the same
763 // loop. The routine establishNestingLevels finds the level of most deeply
764 // nested loop that contains them both, CommonLevels. An instruction that's
765 // not contained in a loop is at level = 0. MaxLevels is equal to the level
766 // of the source plus the level of the destination, minus CommonLevels.
767 // This lets us allocate vectors MaxLevels in length, with room for every
768 // distinct loop referenced in both the source and destination subscripts.
769 // The variable SrcLevels is the nesting depth of the source instruction.
770 // It's used to help calculate distinct loops referenced by the destination.
771 // Here's the map from loops to levels:
773 // 1 - outermost common loop
774 // ... - other common loops
775 // CommonLevels - innermost common loop
776 // ... - loops containing Src but not Dst
777 // SrcLevels - innermost loop containing Src but not Dst
778 // ... - loops containing Dst but not Src
779 // MaxLevels - innermost loops containing Dst but not Src
780 // Consider the follow code fragment:
797 // If we're looking at the possibility of a dependence between the store
798 // to A (the Src) and the load from A (the Dst), we'll note that they
799 // have 2 loops in common, so CommonLevels will equal 2 and the direction
800 // vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7.
801 // A map from loop names to loop numbers would look like
803 // b - 2 = CommonLevels
809 void DependenceInfo::establishNestingLevels(const Instruction
*Src
,
810 const Instruction
*Dst
) {
811 const BasicBlock
*SrcBlock
= Src
->getParent();
812 const BasicBlock
*DstBlock
= Dst
->getParent();
813 unsigned SrcLevel
= LI
->getLoopDepth(SrcBlock
);
814 unsigned DstLevel
= LI
->getLoopDepth(DstBlock
);
815 const Loop
*SrcLoop
= LI
->getLoopFor(SrcBlock
);
816 const Loop
*DstLoop
= LI
->getLoopFor(DstBlock
);
817 SrcLevels
= SrcLevel
;
818 MaxLevels
= SrcLevel
+ DstLevel
;
819 while (SrcLevel
> DstLevel
) {
820 SrcLoop
= SrcLoop
->getParentLoop();
823 while (DstLevel
> SrcLevel
) {
824 DstLoop
= DstLoop
->getParentLoop();
827 while (SrcLoop
!= DstLoop
) {
828 SrcLoop
= SrcLoop
->getParentLoop();
829 DstLoop
= DstLoop
->getParentLoop();
832 CommonLevels
= SrcLevel
;
833 MaxLevels
-= CommonLevels
;
837 // Given one of the loops containing the source, return
838 // its level index in our numbering scheme.
839 unsigned DependenceInfo::mapSrcLoop(const Loop
*SrcLoop
) const {
840 return SrcLoop
->getLoopDepth();
844 // Given one of the loops containing the destination,
845 // return its level index in our numbering scheme.
846 unsigned DependenceInfo::mapDstLoop(const Loop
*DstLoop
) const {
847 unsigned D
= DstLoop
->getLoopDepth();
848 if (D
> CommonLevels
)
849 // This tries to make sure that we assign unique numbers to src and dst when
850 // the memory accesses reside in different loops that have the same depth.
851 return D
- CommonLevels
+ SrcLevels
;
857 // Returns true if Expression is loop invariant in LoopNest.
858 bool DependenceInfo::isLoopInvariant(const SCEV
*Expression
,
859 const Loop
*LoopNest
) const {
860 // Unlike ScalarEvolution::isLoopInvariant() we consider an access outside of
861 // any loop as invariant, because we only consier expression evaluation at a
862 // specific position (where the array access takes place), and not across the
867 // If the expression is invariant in the outermost loop of the loop nest, it
868 // is invariant anywhere in the loop nest.
869 return SE
->isLoopInvariant(Expression
, LoopNest
->getOutermostLoop());
874 // Finds the set of loops from the LoopNest that
875 // have a level <= CommonLevels and are referred to by the SCEV Expression.
876 void DependenceInfo::collectCommonLoops(const SCEV
*Expression
,
877 const Loop
*LoopNest
,
878 SmallBitVector
&Loops
) const {
880 unsigned Level
= LoopNest
->getLoopDepth();
881 if (Level
<= CommonLevels
&& !SE
->isLoopInvariant(Expression
, LoopNest
))
883 LoopNest
= LoopNest
->getParentLoop();
887 void DependenceInfo::unifySubscriptType(ArrayRef
<Subscript
*> Pairs
) {
889 unsigned widestWidthSeen
= 0;
892 // Go through each pair and find the widest bit to which we need
893 // to extend all of them.
894 for (Subscript
*Pair
: Pairs
) {
895 const SCEV
*Src
= Pair
->Src
;
896 const SCEV
*Dst
= Pair
->Dst
;
897 IntegerType
*SrcTy
= dyn_cast
<IntegerType
>(Src
->getType());
898 IntegerType
*DstTy
= dyn_cast
<IntegerType
>(Dst
->getType());
899 if (SrcTy
== nullptr || DstTy
== nullptr) {
900 assert(SrcTy
== DstTy
&& "This function only unify integer types and "
901 "expect Src and Dst share the same type "
905 if (SrcTy
->getBitWidth() > widestWidthSeen
) {
906 widestWidthSeen
= SrcTy
->getBitWidth();
909 if (DstTy
->getBitWidth() > widestWidthSeen
) {
910 widestWidthSeen
= DstTy
->getBitWidth();
916 assert(widestWidthSeen
> 0);
918 // Now extend each pair to the widest seen.
919 for (Subscript
*Pair
: Pairs
) {
920 const SCEV
*Src
= Pair
->Src
;
921 const SCEV
*Dst
= Pair
->Dst
;
922 IntegerType
*SrcTy
= dyn_cast
<IntegerType
>(Src
->getType());
923 IntegerType
*DstTy
= dyn_cast
<IntegerType
>(Dst
->getType());
924 if (SrcTy
== nullptr || DstTy
== nullptr) {
925 assert(SrcTy
== DstTy
&& "This function only unify integer types and "
926 "expect Src and Dst share the same type "
930 if (SrcTy
->getBitWidth() < widestWidthSeen
)
931 // Sign-extend Src to widestType
932 Pair
->Src
= SE
->getSignExtendExpr(Src
, widestType
);
933 if (DstTy
->getBitWidth() < widestWidthSeen
) {
934 // Sign-extend Dst to widestType
935 Pair
->Dst
= SE
->getSignExtendExpr(Dst
, widestType
);
940 // removeMatchingExtensions - Examines a subscript pair.
941 // If the source and destination are identically sign (or zero)
942 // extended, it strips off the extension in an effect to simplify
943 // the actual analysis.
944 void DependenceInfo::removeMatchingExtensions(Subscript
*Pair
) {
945 const SCEV
*Src
= Pair
->Src
;
946 const SCEV
*Dst
= Pair
->Dst
;
947 if ((isa
<SCEVZeroExtendExpr
>(Src
) && isa
<SCEVZeroExtendExpr
>(Dst
)) ||
948 (isa
<SCEVSignExtendExpr
>(Src
) && isa
<SCEVSignExtendExpr
>(Dst
))) {
949 const SCEVIntegralCastExpr
*SrcCast
= cast
<SCEVIntegralCastExpr
>(Src
);
950 const SCEVIntegralCastExpr
*DstCast
= cast
<SCEVIntegralCastExpr
>(Dst
);
951 const SCEV
*SrcCastOp
= SrcCast
->getOperand();
952 const SCEV
*DstCastOp
= DstCast
->getOperand();
953 if (SrcCastOp
->getType() == DstCastOp
->getType()) {
954 Pair
->Src
= SrcCastOp
;
955 Pair
->Dst
= DstCastOp
;
960 // Examine the scev and return true iff it's affine.
961 // Collect any loops mentioned in the set of "Loops".
962 bool DependenceInfo::checkSubscript(const SCEV
*Expr
, const Loop
*LoopNest
,
963 SmallBitVector
&Loops
, bool IsSrc
) {
964 const SCEVAddRecExpr
*AddRec
= dyn_cast
<SCEVAddRecExpr
>(Expr
);
966 return isLoopInvariant(Expr
, LoopNest
);
968 // The AddRec must depend on one of the containing loops. Otherwise,
969 // mapSrcLoop and mapDstLoop return indices outside the intended range. This
970 // can happen when a subscript in one loop references an IV from a sibling
971 // loop that could not be replaced with a concrete exit value by
973 const Loop
*L
= LoopNest
;
974 while (L
&& AddRec
->getLoop() != L
)
975 L
= L
->getParentLoop();
979 const SCEV
*Start
= AddRec
->getStart();
980 const SCEV
*Step
= AddRec
->getStepRecurrence(*SE
);
981 const SCEV
*UB
= SE
->getBackedgeTakenCount(AddRec
->getLoop());
982 if (!isa
<SCEVCouldNotCompute
>(UB
)) {
983 if (SE
->getTypeSizeInBits(Start
->getType()) <
984 SE
->getTypeSizeInBits(UB
->getType())) {
985 if (!AddRec
->getNoWrapFlags())
989 if (!isLoopInvariant(Step
, LoopNest
))
992 Loops
.set(mapSrcLoop(AddRec
->getLoop()));
994 Loops
.set(mapDstLoop(AddRec
->getLoop()));
995 return checkSubscript(Start
, LoopNest
, Loops
, IsSrc
);
998 // Examine the scev and return true iff it's linear.
999 // Collect any loops mentioned in the set of "Loops".
1000 bool DependenceInfo::checkSrcSubscript(const SCEV
*Src
, const Loop
*LoopNest
,
1001 SmallBitVector
&Loops
) {
1002 return checkSubscript(Src
, LoopNest
, Loops
, true);
1005 // Examine the scev and return true iff it's linear.
1006 // Collect any loops mentioned in the set of "Loops".
1007 bool DependenceInfo::checkDstSubscript(const SCEV
*Dst
, const Loop
*LoopNest
,
1008 SmallBitVector
&Loops
) {
1009 return checkSubscript(Dst
, LoopNest
, Loops
, false);
1013 // Examines the subscript pair (the Src and Dst SCEVs)
1014 // and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear.
1015 // Collects the associated loops in a set.
1016 DependenceInfo::Subscript::ClassificationKind
1017 DependenceInfo::classifyPair(const SCEV
*Src
, const Loop
*SrcLoopNest
,
1018 const SCEV
*Dst
, const Loop
*DstLoopNest
,
1019 SmallBitVector
&Loops
) {
1020 SmallBitVector
SrcLoops(MaxLevels
+ 1);
1021 SmallBitVector
DstLoops(MaxLevels
+ 1);
1022 if (!checkSrcSubscript(Src
, SrcLoopNest
, SrcLoops
))
1023 return Subscript::NonLinear
;
1024 if (!checkDstSubscript(Dst
, DstLoopNest
, DstLoops
))
1025 return Subscript::NonLinear
;
1028 unsigned N
= Loops
.count();
1030 return Subscript::ZIV
;
1032 return Subscript::SIV
;
1033 if (N
== 2 && (SrcLoops
.count() == 0 ||
1034 DstLoops
.count() == 0 ||
1035 (SrcLoops
.count() == 1 && DstLoops
.count() == 1)))
1036 return Subscript::RDIV
;
1037 return Subscript::MIV
;
1041 // A wrapper around SCEV::isKnownPredicate.
1042 // Looks for cases where we're interested in comparing for equality.
1043 // If both X and Y have been identically sign or zero extended,
1044 // it strips off the (confusing) extensions before invoking
1045 // SCEV::isKnownPredicate. Perhaps, someday, the ScalarEvolution package
1046 // will be similarly updated.
1048 // If SCEV::isKnownPredicate can't prove the predicate,
1049 // we try simple subtraction, which seems to help in some cases
1050 // involving symbolics.
1051 bool DependenceInfo::isKnownPredicate(ICmpInst::Predicate Pred
, const SCEV
*X
,
1052 const SCEV
*Y
) const {
1053 if (Pred
== CmpInst::ICMP_EQ
||
1054 Pred
== CmpInst::ICMP_NE
) {
1055 if ((isa
<SCEVSignExtendExpr
>(X
) &&
1056 isa
<SCEVSignExtendExpr
>(Y
)) ||
1057 (isa
<SCEVZeroExtendExpr
>(X
) &&
1058 isa
<SCEVZeroExtendExpr
>(Y
))) {
1059 const SCEVIntegralCastExpr
*CX
= cast
<SCEVIntegralCastExpr
>(X
);
1060 const SCEVIntegralCastExpr
*CY
= cast
<SCEVIntegralCastExpr
>(Y
);
1061 const SCEV
*Xop
= CX
->getOperand();
1062 const SCEV
*Yop
= CY
->getOperand();
1063 if (Xop
->getType() == Yop
->getType()) {
1069 if (SE
->isKnownPredicate(Pred
, X
, Y
))
1071 // If SE->isKnownPredicate can't prove the condition,
1072 // we try the brute-force approach of subtracting
1073 // and testing the difference.
1074 // By testing with SE->isKnownPredicate first, we avoid
1075 // the possibility of overflow when the arguments are constants.
1076 const SCEV
*Delta
= SE
->getMinusSCEV(X
, Y
);
1078 case CmpInst::ICMP_EQ
:
1079 return Delta
->isZero();
1080 case CmpInst::ICMP_NE
:
1081 return SE
->isKnownNonZero(Delta
);
1082 case CmpInst::ICMP_SGE
:
1083 return SE
->isKnownNonNegative(Delta
);
1084 case CmpInst::ICMP_SLE
:
1085 return SE
->isKnownNonPositive(Delta
);
1086 case CmpInst::ICMP_SGT
:
1087 return SE
->isKnownPositive(Delta
);
1088 case CmpInst::ICMP_SLT
:
1089 return SE
->isKnownNegative(Delta
);
1091 llvm_unreachable("unexpected predicate in isKnownPredicate");
1095 /// Compare to see if S is less than Size, using isKnownNegative(S - max(Size, 1))
1096 /// with some extra checking if S is an AddRec and we can prove less-than using
1097 /// the loop bounds.
1098 bool DependenceInfo::isKnownLessThan(const SCEV
*S
, const SCEV
*Size
) const {
1099 // First unify to the same type
1100 auto *SType
= dyn_cast
<IntegerType
>(S
->getType());
1101 auto *SizeType
= dyn_cast
<IntegerType
>(Size
->getType());
1102 if (!SType
|| !SizeType
)
1105 (SType
->getBitWidth() >= SizeType
->getBitWidth()) ? SType
: SizeType
;
1106 S
= SE
->getTruncateOrZeroExtend(S
, MaxType
);
1107 Size
= SE
->getTruncateOrZeroExtend(Size
, MaxType
);
1109 // Special check for addrecs using BE taken count
1110 const SCEV
*Bound
= SE
->getMinusSCEV(S
, Size
);
1111 if (const SCEVAddRecExpr
*AddRec
= dyn_cast
<SCEVAddRecExpr
>(Bound
)) {
1112 if (AddRec
->isAffine()) {
1113 const SCEV
*BECount
= SE
->getBackedgeTakenCount(AddRec
->getLoop());
1114 if (!isa
<SCEVCouldNotCompute
>(BECount
)) {
1115 const SCEV
*Limit
= AddRec
->evaluateAtIteration(BECount
, *SE
);
1116 if (SE
->isKnownNegative(Limit
))
1122 // Check using normal isKnownNegative
1123 const SCEV
*LimitedBound
=
1124 SE
->getMinusSCEV(S
, SE
->getSMaxExpr(Size
, SE
->getOne(Size
->getType())));
1125 return SE
->isKnownNegative(LimitedBound
);
1128 bool DependenceInfo::isKnownNonNegative(const SCEV
*S
, const Value
*Ptr
) const {
1129 bool Inbounds
= false;
1130 if (auto *SrcGEP
= dyn_cast
<GetElementPtrInst
>(Ptr
))
1131 Inbounds
= SrcGEP
->isInBounds();
1133 if (const SCEVAddRecExpr
*AddRec
= dyn_cast
<SCEVAddRecExpr
>(S
)) {
1134 if (AddRec
->isAffine()) {
1135 // We know S is for Ptr, the operand on a load/store, so doesn't wrap.
1136 // If both parts are NonNegative, the end result will be NonNegative
1137 if (SE
->isKnownNonNegative(AddRec
->getStart()) &&
1138 SE
->isKnownNonNegative(AddRec
->getOperand(1)))
1144 return SE
->isKnownNonNegative(S
);
1147 // All subscripts are all the same type.
1148 // Loop bound may be smaller (e.g., a char).
1149 // Should zero extend loop bound, since it's always >= 0.
1150 // This routine collects upper bound and extends or truncates if needed.
1151 // Truncating is safe when subscripts are known not to wrap. Cases without
1152 // nowrap flags should have been rejected earlier.
1153 // Return null if no bound available.
1154 const SCEV
*DependenceInfo::collectUpperBound(const Loop
*L
, Type
*T
) const {
1155 if (SE
->hasLoopInvariantBackedgeTakenCount(L
)) {
1156 const SCEV
*UB
= SE
->getBackedgeTakenCount(L
);
1157 return SE
->getTruncateOrZeroExtend(UB
, T
);
1163 // Calls collectUpperBound(), then attempts to cast it to SCEVConstant.
1164 // If the cast fails, returns NULL.
1165 const SCEVConstant
*DependenceInfo::collectConstantUpperBound(const Loop
*L
,
1167 if (const SCEV
*UB
= collectUpperBound(L
, T
))
1168 return dyn_cast
<SCEVConstant
>(UB
);
1174 // When we have a pair of subscripts of the form [c1] and [c2],
1175 // where c1 and c2 are both loop invariant, we attack it using
1176 // the ZIV test. Basically, we test by comparing the two values,
1177 // but there are actually three possible results:
1178 // 1) the values are equal, so there's a dependence
1179 // 2) the values are different, so there's no dependence
1180 // 3) the values might be equal, so we have to assume a dependence.
1182 // Return true if dependence disproved.
1183 bool DependenceInfo::testZIV(const SCEV
*Src
, const SCEV
*Dst
,
1184 FullDependence
&Result
) const {
1185 LLVM_DEBUG(dbgs() << " src = " << *Src
<< "\n");
1186 LLVM_DEBUG(dbgs() << " dst = " << *Dst
<< "\n");
1188 if (isKnownPredicate(CmpInst::ICMP_EQ
, Src
, Dst
)) {
1189 LLVM_DEBUG(dbgs() << " provably dependent\n");
1190 return false; // provably dependent
1192 if (isKnownPredicate(CmpInst::ICMP_NE
, Src
, Dst
)) {
1193 LLVM_DEBUG(dbgs() << " provably independent\n");
1195 return true; // provably independent
1197 LLVM_DEBUG(dbgs() << " possibly dependent\n");
1198 Result
.Consistent
= false;
1199 return false; // possibly dependent
1204 // From the paper, Practical Dependence Testing, Section 4.2.1
1206 // When we have a pair of subscripts of the form [c1 + a*i] and [c2 + a*i],
1207 // where i is an induction variable, c1 and c2 are loop invariant,
1208 // and a is a constant, we can solve it exactly using the Strong SIV test.
1210 // Can prove independence. Failing that, can compute distance (and direction).
1211 // In the presence of symbolic terms, we can sometimes make progress.
1213 // If there's a dependence,
1215 // c1 + a*i = c2 + a*i'
1217 // The dependence distance is
1219 // d = i' - i = (c1 - c2)/a
1221 // A dependence only exists if d is an integer and abs(d) <= U, where U is the
1222 // loop's upper bound. If a dependence exists, the dependence direction is
1226 // direction = { = if d = 0
1229 // Return true if dependence disproved.
1230 bool DependenceInfo::strongSIVtest(const SCEV
*Coeff
, const SCEV
*SrcConst
,
1231 const SCEV
*DstConst
, const Loop
*CurLoop
,
1232 unsigned Level
, FullDependence
&Result
,
1233 Constraint
&NewConstraint
) const {
1234 LLVM_DEBUG(dbgs() << "\tStrong SIV test\n");
1235 LLVM_DEBUG(dbgs() << "\t Coeff = " << *Coeff
);
1236 LLVM_DEBUG(dbgs() << ", " << *Coeff
->getType() << "\n");
1237 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst
);
1238 LLVM_DEBUG(dbgs() << ", " << *SrcConst
->getType() << "\n");
1239 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst
);
1240 LLVM_DEBUG(dbgs() << ", " << *DstConst
->getType() << "\n");
1241 ++StrongSIVapplications
;
1242 assert(0 < Level
&& Level
<= CommonLevels
&& "level out of range");
1245 const SCEV
*Delta
= SE
->getMinusSCEV(SrcConst
, DstConst
);
1246 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta
);
1247 LLVM_DEBUG(dbgs() << ", " << *Delta
->getType() << "\n");
1249 // check that |Delta| < iteration count
1250 if (const SCEV
*UpperBound
= collectUpperBound(CurLoop
, Delta
->getType())) {
1251 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound
);
1252 LLVM_DEBUG(dbgs() << ", " << *UpperBound
->getType() << "\n");
1253 const SCEV
*AbsDelta
=
1254 SE
->isKnownNonNegative(Delta
) ? Delta
: SE
->getNegativeSCEV(Delta
);
1255 const SCEV
*AbsCoeff
=
1256 SE
->isKnownNonNegative(Coeff
) ? Coeff
: SE
->getNegativeSCEV(Coeff
);
1257 const SCEV
*Product
= SE
->getMulExpr(UpperBound
, AbsCoeff
);
1258 if (isKnownPredicate(CmpInst::ICMP_SGT
, AbsDelta
, Product
)) {
1259 // Distance greater than trip count - no dependence
1260 ++StrongSIVindependence
;
1261 ++StrongSIVsuccesses
;
1266 // Can we compute distance?
1267 if (isa
<SCEVConstant
>(Delta
) && isa
<SCEVConstant
>(Coeff
)) {
1268 APInt ConstDelta
= cast
<SCEVConstant
>(Delta
)->getAPInt();
1269 APInt ConstCoeff
= cast
<SCEVConstant
>(Coeff
)->getAPInt();
1270 APInt Distance
= ConstDelta
; // these need to be initialized
1271 APInt Remainder
= ConstDelta
;
1272 APInt::sdivrem(ConstDelta
, ConstCoeff
, Distance
, Remainder
);
1273 LLVM_DEBUG(dbgs() << "\t Distance = " << Distance
<< "\n");
1274 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder
<< "\n");
1275 // Make sure Coeff divides Delta exactly
1276 if (Remainder
!= 0) {
1277 // Coeff doesn't divide Distance, no dependence
1278 ++StrongSIVindependence
;
1279 ++StrongSIVsuccesses
;
1282 Result
.DV
[Level
].Distance
= SE
->getConstant(Distance
);
1283 NewConstraint
.setDistance(SE
->getConstant(Distance
), CurLoop
);
1284 if (Distance
.sgt(0))
1285 Result
.DV
[Level
].Direction
&= Dependence::DVEntry::LT
;
1286 else if (Distance
.slt(0))
1287 Result
.DV
[Level
].Direction
&= Dependence::DVEntry::GT
;
1289 Result
.DV
[Level
].Direction
&= Dependence::DVEntry::EQ
;
1290 ++StrongSIVsuccesses
;
1292 else if (Delta
->isZero()) {
1294 Result
.DV
[Level
].Distance
= Delta
;
1295 NewConstraint
.setDistance(Delta
, CurLoop
);
1296 Result
.DV
[Level
].Direction
&= Dependence::DVEntry::EQ
;
1297 ++StrongSIVsuccesses
;
1300 if (Coeff
->isOne()) {
1301 LLVM_DEBUG(dbgs() << "\t Distance = " << *Delta
<< "\n");
1302 Result
.DV
[Level
].Distance
= Delta
; // since X/1 == X
1303 NewConstraint
.setDistance(Delta
, CurLoop
);
1306 Result
.Consistent
= false;
1307 NewConstraint
.setLine(Coeff
,
1308 SE
->getNegativeSCEV(Coeff
),
1309 SE
->getNegativeSCEV(Delta
), CurLoop
);
1312 // maybe we can get a useful direction
1313 bool DeltaMaybeZero
= !SE
->isKnownNonZero(Delta
);
1314 bool DeltaMaybePositive
= !SE
->isKnownNonPositive(Delta
);
1315 bool DeltaMaybeNegative
= !SE
->isKnownNonNegative(Delta
);
1316 bool CoeffMaybePositive
= !SE
->isKnownNonPositive(Coeff
);
1317 bool CoeffMaybeNegative
= !SE
->isKnownNonNegative(Coeff
);
1318 // The double negatives above are confusing.
1319 // It helps to read !SE->isKnownNonZero(Delta)
1320 // as "Delta might be Zero"
1321 unsigned NewDirection
= Dependence::DVEntry::NONE
;
1322 if ((DeltaMaybePositive
&& CoeffMaybePositive
) ||
1323 (DeltaMaybeNegative
&& CoeffMaybeNegative
))
1324 NewDirection
= Dependence::DVEntry::LT
;
1326 NewDirection
|= Dependence::DVEntry::EQ
;
1327 if ((DeltaMaybeNegative
&& CoeffMaybePositive
) ||
1328 (DeltaMaybePositive
&& CoeffMaybeNegative
))
1329 NewDirection
|= Dependence::DVEntry::GT
;
1330 if (NewDirection
< Result
.DV
[Level
].Direction
)
1331 ++StrongSIVsuccesses
;
1332 Result
.DV
[Level
].Direction
&= NewDirection
;
1338 // weakCrossingSIVtest -
1339 // From the paper, Practical Dependence Testing, Section 4.2.2
1341 // When we have a pair of subscripts of the form [c1 + a*i] and [c2 - a*i],
1342 // where i is an induction variable, c1 and c2 are loop invariant,
1343 // and a is a constant, we can solve it exactly using the
1344 // Weak-Crossing SIV test.
1346 // Given c1 + a*i = c2 - a*i', we can look for the intersection of
1347 // the two lines, where i = i', yielding
1349 // c1 + a*i = c2 - a*i
1353 // If i < 0, there is no dependence.
1354 // If i > upperbound, there is no dependence.
1355 // If i = 0 (i.e., if c1 = c2), there's a dependence with distance = 0.
1356 // If i = upperbound, there's a dependence with distance = 0.
1357 // If i is integral, there's a dependence (all directions).
1358 // If the non-integer part = 1/2, there's a dependence (<> directions).
1359 // Otherwise, there's no dependence.
1361 // Can prove independence. Failing that,
1362 // can sometimes refine the directions.
1363 // Can determine iteration for splitting.
1365 // Return true if dependence disproved.
1366 bool DependenceInfo::weakCrossingSIVtest(
1367 const SCEV
*Coeff
, const SCEV
*SrcConst
, const SCEV
*DstConst
,
1368 const Loop
*CurLoop
, unsigned Level
, FullDependence
&Result
,
1369 Constraint
&NewConstraint
, const SCEV
*&SplitIter
) const {
1370 LLVM_DEBUG(dbgs() << "\tWeak-Crossing SIV test\n");
1371 LLVM_DEBUG(dbgs() << "\t Coeff = " << *Coeff
<< "\n");
1372 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst
<< "\n");
1373 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst
<< "\n");
1374 ++WeakCrossingSIVapplications
;
1375 assert(0 < Level
&& Level
<= CommonLevels
&& "Level out of range");
1377 Result
.Consistent
= false;
1378 const SCEV
*Delta
= SE
->getMinusSCEV(DstConst
, SrcConst
);
1379 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta
<< "\n");
1380 NewConstraint
.setLine(Coeff
, Coeff
, Delta
, CurLoop
);
1381 if (Delta
->isZero()) {
1382 Result
.DV
[Level
].Direction
&= ~Dependence::DVEntry::LT
;
1383 Result
.DV
[Level
].Direction
&= ~Dependence::DVEntry::GT
;
1384 ++WeakCrossingSIVsuccesses
;
1385 if (!Result
.DV
[Level
].Direction
) {
1386 ++WeakCrossingSIVindependence
;
1389 Result
.DV
[Level
].Distance
= Delta
; // = 0
1392 const SCEVConstant
*ConstCoeff
= dyn_cast
<SCEVConstant
>(Coeff
);
1396 Result
.DV
[Level
].Splitable
= true;
1397 if (SE
->isKnownNegative(ConstCoeff
)) {
1398 ConstCoeff
= dyn_cast
<SCEVConstant
>(SE
->getNegativeSCEV(ConstCoeff
));
1399 assert(ConstCoeff
&&
1400 "dynamic cast of negative of ConstCoeff should yield constant");
1401 Delta
= SE
->getNegativeSCEV(Delta
);
1403 assert(SE
->isKnownPositive(ConstCoeff
) && "ConstCoeff should be positive");
1405 // compute SplitIter for use by DependenceInfo::getSplitIteration()
1406 SplitIter
= SE
->getUDivExpr(
1407 SE
->getSMaxExpr(SE
->getZero(Delta
->getType()), Delta
),
1408 SE
->getMulExpr(SE
->getConstant(Delta
->getType(), 2), ConstCoeff
));
1409 LLVM_DEBUG(dbgs() << "\t Split iter = " << *SplitIter
<< "\n");
1411 const SCEVConstant
*ConstDelta
= dyn_cast
<SCEVConstant
>(Delta
);
1415 // We're certain that ConstCoeff > 0; therefore,
1416 // if Delta < 0, then no dependence.
1417 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta
<< "\n");
1418 LLVM_DEBUG(dbgs() << "\t ConstCoeff = " << *ConstCoeff
<< "\n");
1419 if (SE
->isKnownNegative(Delta
)) {
1420 // No dependence, Delta < 0
1421 ++WeakCrossingSIVindependence
;
1422 ++WeakCrossingSIVsuccesses
;
1426 // We're certain that Delta > 0 and ConstCoeff > 0.
1427 // Check Delta/(2*ConstCoeff) against upper loop bound
1428 if (const SCEV
*UpperBound
= collectUpperBound(CurLoop
, Delta
->getType())) {
1429 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound
<< "\n");
1430 const SCEV
*ConstantTwo
= SE
->getConstant(UpperBound
->getType(), 2);
1431 const SCEV
*ML
= SE
->getMulExpr(SE
->getMulExpr(ConstCoeff
, UpperBound
),
1433 LLVM_DEBUG(dbgs() << "\t ML = " << *ML
<< "\n");
1434 if (isKnownPredicate(CmpInst::ICMP_SGT
, Delta
, ML
)) {
1435 // Delta too big, no dependence
1436 ++WeakCrossingSIVindependence
;
1437 ++WeakCrossingSIVsuccesses
;
1440 if (isKnownPredicate(CmpInst::ICMP_EQ
, Delta
, ML
)) {
1442 Result
.DV
[Level
].Direction
&= ~Dependence::DVEntry::LT
;
1443 Result
.DV
[Level
].Direction
&= ~Dependence::DVEntry::GT
;
1444 ++WeakCrossingSIVsuccesses
;
1445 if (!Result
.DV
[Level
].Direction
) {
1446 ++WeakCrossingSIVindependence
;
1449 Result
.DV
[Level
].Splitable
= false;
1450 Result
.DV
[Level
].Distance
= SE
->getZero(Delta
->getType());
1455 // check that Coeff divides Delta
1456 APInt APDelta
= ConstDelta
->getAPInt();
1457 APInt APCoeff
= ConstCoeff
->getAPInt();
1458 APInt Distance
= APDelta
; // these need to be initialzed
1459 APInt Remainder
= APDelta
;
1460 APInt::sdivrem(APDelta
, APCoeff
, Distance
, Remainder
);
1461 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder
<< "\n");
1462 if (Remainder
!= 0) {
1463 // Coeff doesn't divide Delta, no dependence
1464 ++WeakCrossingSIVindependence
;
1465 ++WeakCrossingSIVsuccesses
;
1468 LLVM_DEBUG(dbgs() << "\t Distance = " << Distance
<< "\n");
1470 // if 2*Coeff doesn't divide Delta, then the equal direction isn't possible
1471 APInt Two
= APInt(Distance
.getBitWidth(), 2, true);
1472 Remainder
= Distance
.srem(Two
);
1473 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder
<< "\n");
1474 if (Remainder
!= 0) {
1475 // Equal direction isn't possible
1476 Result
.DV
[Level
].Direction
&= ~Dependence::DVEntry::EQ
;
1477 ++WeakCrossingSIVsuccesses
;
1483 // Kirch's algorithm, from
1485 // Optimizing Supercompilers for Supercomputers
1489 // Program 2.1, page 29.
1490 // Computes the GCD of AM and BM.
1491 // Also finds a solution to the equation ax - by = gcd(a, b).
1492 // Returns true if dependence disproved; i.e., gcd does not divide Delta.
1493 static bool findGCD(unsigned Bits
, const APInt
&AM
, const APInt
&BM
,
1494 const APInt
&Delta
, APInt
&G
, APInt
&X
, APInt
&Y
) {
1495 APInt
A0(Bits
, 1, true), A1(Bits
, 0, true);
1496 APInt
B0(Bits
, 0, true), B1(Bits
, 1, true);
1497 APInt G0
= AM
.abs();
1498 APInt G1
= BM
.abs();
1499 APInt Q
= G0
; // these need to be initialized
1501 APInt::sdivrem(G0
, G1
, Q
, R
);
1503 APInt A2
= A0
- Q
*A1
; A0
= A1
; A1
= A2
;
1504 APInt B2
= B0
- Q
*B1
; B0
= B1
; B1
= B2
;
1506 APInt::sdivrem(G0
, G1
, Q
, R
);
1509 LLVM_DEBUG(dbgs() << "\t GCD = " << G
<< "\n");
1510 X
= AM
.slt(0) ? -A1
: A1
;
1511 Y
= BM
.slt(0) ? B1
: -B1
;
1513 // make sure gcd divides Delta
1516 return true; // gcd doesn't divide Delta, no dependence
1521 static APInt
floorOfQuotient(const APInt
&A
, const APInt
&B
) {
1522 APInt Q
= A
; // these need to be initialized
1524 APInt::sdivrem(A
, B
, Q
, R
);
1527 if ((A
.sgt(0) && B
.sgt(0)) ||
1528 (A
.slt(0) && B
.slt(0)))
1534 static APInt
ceilingOfQuotient(const APInt
&A
, const APInt
&B
) {
1535 APInt Q
= A
; // these need to be initialized
1537 APInt::sdivrem(A
, B
, Q
, R
);
1540 if ((A
.sgt(0) && B
.sgt(0)) ||
1541 (A
.slt(0) && B
.slt(0)))
1548 // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*i],
1549 // where i is an induction variable, c1 and c2 are loop invariant, and a1
1550 // and a2 are constant, we can solve it exactly using an algorithm developed
1551 // by Banerjee and Wolfe. See Algorithm 6.2.1 (case 2.5) in:
1553 // Dependence Analysis for Supercomputing
1555 // Kluwer Academic Publishers, 1988
1557 // It's slower than the specialized tests (strong SIV, weak-zero SIV, etc),
1558 // so use them if possible. They're also a bit better with symbolics and,
1559 // in the case of the strong SIV test, can compute Distances.
1561 // Return true if dependence disproved.
1563 // This is a modified version of the original Banerjee algorithm. The original
1564 // only tested whether Dst depends on Src. This algorithm extends that and
1565 // returns all the dependencies that exist between Dst and Src.
1566 bool DependenceInfo::exactSIVtest(const SCEV
*SrcCoeff
, const SCEV
*DstCoeff
,
1567 const SCEV
*SrcConst
, const SCEV
*DstConst
,
1568 const Loop
*CurLoop
, unsigned Level
,
1569 FullDependence
&Result
,
1570 Constraint
&NewConstraint
) const {
1571 LLVM_DEBUG(dbgs() << "\tExact SIV test\n");
1572 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff
<< " = AM\n");
1573 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff
<< " = BM\n");
1574 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst
<< "\n");
1575 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst
<< "\n");
1576 ++ExactSIVapplications
;
1577 assert(0 < Level
&& Level
<= CommonLevels
&& "Level out of range");
1579 Result
.Consistent
= false;
1580 const SCEV
*Delta
= SE
->getMinusSCEV(DstConst
, SrcConst
);
1581 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta
<< "\n");
1582 NewConstraint
.setLine(SrcCoeff
, SE
->getNegativeSCEV(DstCoeff
), Delta
,
1584 const SCEVConstant
*ConstDelta
= dyn_cast
<SCEVConstant
>(Delta
);
1585 const SCEVConstant
*ConstSrcCoeff
= dyn_cast
<SCEVConstant
>(SrcCoeff
);
1586 const SCEVConstant
*ConstDstCoeff
= dyn_cast
<SCEVConstant
>(DstCoeff
);
1587 if (!ConstDelta
|| !ConstSrcCoeff
|| !ConstDstCoeff
)
1592 APInt AM
= ConstSrcCoeff
->getAPInt();
1593 APInt BM
= ConstDstCoeff
->getAPInt();
1594 APInt CM
= ConstDelta
->getAPInt();
1595 unsigned Bits
= AM
.getBitWidth();
1596 if (findGCD(Bits
, AM
, BM
, CM
, G
, X
, Y
)) {
1597 // gcd doesn't divide Delta, no dependence
1598 ++ExactSIVindependence
;
1599 ++ExactSIVsuccesses
;
1603 LLVM_DEBUG(dbgs() << "\t X = " << X
<< ", Y = " << Y
<< "\n");
1605 // since SCEV construction normalizes, LM = 0
1606 APInt
UM(Bits
, 1, true);
1607 bool UMValid
= false;
1608 // UM is perhaps unavailable, let's check
1609 if (const SCEVConstant
*CUB
=
1610 collectConstantUpperBound(CurLoop
, Delta
->getType())) {
1611 UM
= CUB
->getAPInt();
1612 LLVM_DEBUG(dbgs() << "\t UM = " << UM
<< "\n");
1616 APInt
TU(APInt::getSignedMaxValue(Bits
));
1617 APInt
TL(APInt::getSignedMinValue(Bits
));
1618 APInt TC
= CM
.sdiv(G
);
1621 LLVM_DEBUG(dbgs() << "\t TC = " << TC
<< "\n");
1622 LLVM_DEBUG(dbgs() << "\t TX = " << TX
<< "\n");
1623 LLVM_DEBUG(dbgs() << "\t TY = " << TY
<< "\n");
1625 SmallVector
<APInt
, 2> TLVec
, TUVec
;
1626 APInt TB
= BM
.sdiv(G
);
1628 TLVec
.push_back(ceilingOfQuotient(-TX
, TB
));
1629 LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec
.back() << "\n");
1630 // New bound check - modification to Banerjee's e3 check
1632 TUVec
.push_back(floorOfQuotient(UM
- TX
, TB
));
1633 LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec
.back() << "\n");
1636 TUVec
.push_back(floorOfQuotient(-TX
, TB
));
1637 LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec
.back() << "\n");
1638 // New bound check - modification to Banerjee's e3 check
1640 TLVec
.push_back(ceilingOfQuotient(UM
- TX
, TB
));
1641 LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec
.back() << "\n");
1645 APInt TA
= AM
.sdiv(G
);
1648 TUVec
.push_back(floorOfQuotient(UM
- TY
, TA
));
1649 LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec
.back() << "\n");
1651 // New bound check - modification to Banerjee's e3 check
1652 TLVec
.push_back(ceilingOfQuotient(-TY
, TA
));
1653 LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec
.back() << "\n");
1656 TLVec
.push_back(ceilingOfQuotient(UM
- TY
, TA
));
1657 LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec
.back() << "\n");
1659 // New bound check - modification to Banerjee's e3 check
1660 TUVec
.push_back(floorOfQuotient(-TY
, TA
));
1661 LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec
.back() << "\n");
1664 LLVM_DEBUG(dbgs() << "\t TA = " << TA
<< "\n");
1665 LLVM_DEBUG(dbgs() << "\t TB = " << TB
<< "\n");
1667 if (TLVec
.empty() || TUVec
.empty())
1669 TL
= APIntOps::smax(TLVec
.front(), TLVec
.back());
1670 TU
= APIntOps::smin(TUVec
.front(), TUVec
.back());
1671 LLVM_DEBUG(dbgs() << "\t TL = " << TL
<< "\n");
1672 LLVM_DEBUG(dbgs() << "\t TU = " << TU
<< "\n");
1675 ++ExactSIVindependence
;
1676 ++ExactSIVsuccesses
;
1680 // explore directions
1681 unsigned NewDirection
= Dependence::DVEntry::NONE
;
1682 APInt LowerDistance
, UpperDistance
;
1684 LowerDistance
= (TY
- TX
) + (TA
- TB
) * TL
;
1685 UpperDistance
= (TY
- TX
) + (TA
- TB
) * TU
;
1687 LowerDistance
= (TY
- TX
) + (TA
- TB
) * TU
;
1688 UpperDistance
= (TY
- TX
) + (TA
- TB
) * TL
;
1691 LLVM_DEBUG(dbgs() << "\t LowerDistance = " << LowerDistance
<< "\n");
1692 LLVM_DEBUG(dbgs() << "\t UpperDistance = " << UpperDistance
<< "\n");
1694 APInt
Zero(Bits
, 0, true);
1695 if (LowerDistance
.sle(Zero
) && UpperDistance
.sge(Zero
)) {
1696 NewDirection
|= Dependence::DVEntry::EQ
;
1697 ++ExactSIVsuccesses
;
1699 if (LowerDistance
.slt(0)) {
1700 NewDirection
|= Dependence::DVEntry::GT
;
1701 ++ExactSIVsuccesses
;
1703 if (UpperDistance
.sgt(0)) {
1704 NewDirection
|= Dependence::DVEntry::LT
;
1705 ++ExactSIVsuccesses
;
1709 Result
.DV
[Level
].Direction
&= NewDirection
;
1710 if (Result
.DV
[Level
].Direction
== Dependence::DVEntry::NONE
)
1711 ++ExactSIVindependence
;
1712 LLVM_DEBUG(dbgs() << "\t Result = ");
1713 LLVM_DEBUG(Result
.dump(dbgs()));
1714 return Result
.DV
[Level
].Direction
== Dependence::DVEntry::NONE
;
1718 // Return true if the divisor evenly divides the dividend.
1720 bool isRemainderZero(const SCEVConstant
*Dividend
,
1721 const SCEVConstant
*Divisor
) {
1722 const APInt
&ConstDividend
= Dividend
->getAPInt();
1723 const APInt
&ConstDivisor
= Divisor
->getAPInt();
1724 return ConstDividend
.srem(ConstDivisor
) == 0;
1728 // weakZeroSrcSIVtest -
1729 // From the paper, Practical Dependence Testing, Section 4.2.2
1731 // When we have a pair of subscripts of the form [c1] and [c2 + a*i],
1732 // where i is an induction variable, c1 and c2 are loop invariant,
1733 // and a is a constant, we can solve it exactly using the
1734 // Weak-Zero SIV test.
1744 // If i is not an integer, there's no dependence.
1745 // If i < 0 or > UB, there's no dependence.
1746 // If i = 0, the direction is >= and peeling the
1747 // 1st iteration will break the dependence.
1748 // If i = UB, the direction is <= and peeling the
1749 // last iteration will break the dependence.
1750 // Otherwise, the direction is *.
1752 // Can prove independence. Failing that, we can sometimes refine
1753 // the directions. Can sometimes show that first or last
1754 // iteration carries all the dependences (so worth peeling).
1756 // (see also weakZeroDstSIVtest)
1758 // Return true if dependence disproved.
1759 bool DependenceInfo::weakZeroSrcSIVtest(const SCEV
*DstCoeff
,
1760 const SCEV
*SrcConst
,
1761 const SCEV
*DstConst
,
1762 const Loop
*CurLoop
, unsigned Level
,
1763 FullDependence
&Result
,
1764 Constraint
&NewConstraint
) const {
1765 // For the WeakSIV test, it's possible the loop isn't common to
1766 // the Src and Dst loops. If it isn't, then there's no need to
1767 // record a direction.
1768 LLVM_DEBUG(dbgs() << "\tWeak-Zero (src) SIV test\n");
1769 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff
<< "\n");
1770 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst
<< "\n");
1771 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst
<< "\n");
1772 ++WeakZeroSIVapplications
;
1773 assert(0 < Level
&& Level
<= MaxLevels
&& "Level out of range");
1775 Result
.Consistent
= false;
1776 const SCEV
*Delta
= SE
->getMinusSCEV(SrcConst
, DstConst
);
1777 NewConstraint
.setLine(SE
->getZero(Delta
->getType()), DstCoeff
, Delta
,
1779 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta
<< "\n");
1780 if (isKnownPredicate(CmpInst::ICMP_EQ
, SrcConst
, DstConst
)) {
1781 if (Level
< CommonLevels
) {
1782 Result
.DV
[Level
].Direction
&= Dependence::DVEntry::GE
;
1783 Result
.DV
[Level
].PeelFirst
= true;
1784 ++WeakZeroSIVsuccesses
;
1786 return false; // dependences caused by first iteration
1788 const SCEVConstant
*ConstCoeff
= dyn_cast
<SCEVConstant
>(DstCoeff
);
1791 const SCEV
*AbsCoeff
=
1792 SE
->isKnownNegative(ConstCoeff
) ?
1793 SE
->getNegativeSCEV(ConstCoeff
) : ConstCoeff
;
1794 const SCEV
*NewDelta
=
1795 SE
->isKnownNegative(ConstCoeff
) ? SE
->getNegativeSCEV(Delta
) : Delta
;
1797 // check that Delta/SrcCoeff < iteration count
1798 // really check NewDelta < count*AbsCoeff
1799 if (const SCEV
*UpperBound
= collectUpperBound(CurLoop
, Delta
->getType())) {
1800 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound
<< "\n");
1801 const SCEV
*Product
= SE
->getMulExpr(AbsCoeff
, UpperBound
);
1802 if (isKnownPredicate(CmpInst::ICMP_SGT
, NewDelta
, Product
)) {
1803 ++WeakZeroSIVindependence
;
1804 ++WeakZeroSIVsuccesses
;
1807 if (isKnownPredicate(CmpInst::ICMP_EQ
, NewDelta
, Product
)) {
1808 // dependences caused by last iteration
1809 if (Level
< CommonLevels
) {
1810 Result
.DV
[Level
].Direction
&= Dependence::DVEntry::LE
;
1811 Result
.DV
[Level
].PeelLast
= true;
1812 ++WeakZeroSIVsuccesses
;
1818 // check that Delta/SrcCoeff >= 0
1819 // really check that NewDelta >= 0
1820 if (SE
->isKnownNegative(NewDelta
)) {
1821 // No dependence, newDelta < 0
1822 ++WeakZeroSIVindependence
;
1823 ++WeakZeroSIVsuccesses
;
1827 // if SrcCoeff doesn't divide Delta, then no dependence
1828 if (isa
<SCEVConstant
>(Delta
) &&
1829 !isRemainderZero(cast
<SCEVConstant
>(Delta
), ConstCoeff
)) {
1830 ++WeakZeroSIVindependence
;
1831 ++WeakZeroSIVsuccesses
;
1838 // weakZeroDstSIVtest -
1839 // From the paper, Practical Dependence Testing, Section 4.2.2
1841 // When we have a pair of subscripts of the form [c1 + a*i] and [c2],
1842 // where i is an induction variable, c1 and c2 are loop invariant,
1843 // and a is a constant, we can solve it exactly using the
1844 // Weak-Zero SIV test.
1854 // If i is not an integer, there's no dependence.
1855 // If i < 0 or > UB, there's no dependence.
1856 // If i = 0, the direction is <= and peeling the
1857 // 1st iteration will break the dependence.
1858 // If i = UB, the direction is >= and peeling the
1859 // last iteration will break the dependence.
1860 // Otherwise, the direction is *.
1862 // Can prove independence. Failing that, we can sometimes refine
1863 // the directions. Can sometimes show that first or last
1864 // iteration carries all the dependences (so worth peeling).
1866 // (see also weakZeroSrcSIVtest)
1868 // Return true if dependence disproved.
1869 bool DependenceInfo::weakZeroDstSIVtest(const SCEV
*SrcCoeff
,
1870 const SCEV
*SrcConst
,
1871 const SCEV
*DstConst
,
1872 const Loop
*CurLoop
, unsigned Level
,
1873 FullDependence
&Result
,
1874 Constraint
&NewConstraint
) const {
1875 // For the WeakSIV test, it's possible the loop isn't common to the
1876 // Src and Dst loops. If it isn't, then there's no need to record a direction.
1877 LLVM_DEBUG(dbgs() << "\tWeak-Zero (dst) SIV test\n");
1878 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff
<< "\n");
1879 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst
<< "\n");
1880 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst
<< "\n");
1881 ++WeakZeroSIVapplications
;
1882 assert(0 < Level
&& Level
<= SrcLevels
&& "Level out of range");
1884 Result
.Consistent
= false;
1885 const SCEV
*Delta
= SE
->getMinusSCEV(DstConst
, SrcConst
);
1886 NewConstraint
.setLine(SrcCoeff
, SE
->getZero(Delta
->getType()), Delta
,
1888 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta
<< "\n");
1889 if (isKnownPredicate(CmpInst::ICMP_EQ
, DstConst
, SrcConst
)) {
1890 if (Level
< CommonLevels
) {
1891 Result
.DV
[Level
].Direction
&= Dependence::DVEntry::LE
;
1892 Result
.DV
[Level
].PeelFirst
= true;
1893 ++WeakZeroSIVsuccesses
;
1895 return false; // dependences caused by first iteration
1897 const SCEVConstant
*ConstCoeff
= dyn_cast
<SCEVConstant
>(SrcCoeff
);
1900 const SCEV
*AbsCoeff
=
1901 SE
->isKnownNegative(ConstCoeff
) ?
1902 SE
->getNegativeSCEV(ConstCoeff
) : ConstCoeff
;
1903 const SCEV
*NewDelta
=
1904 SE
->isKnownNegative(ConstCoeff
) ? SE
->getNegativeSCEV(Delta
) : Delta
;
1906 // check that Delta/SrcCoeff < iteration count
1907 // really check NewDelta < count*AbsCoeff
1908 if (const SCEV
*UpperBound
= collectUpperBound(CurLoop
, Delta
->getType())) {
1909 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound
<< "\n");
1910 const SCEV
*Product
= SE
->getMulExpr(AbsCoeff
, UpperBound
);
1911 if (isKnownPredicate(CmpInst::ICMP_SGT
, NewDelta
, Product
)) {
1912 ++WeakZeroSIVindependence
;
1913 ++WeakZeroSIVsuccesses
;
1916 if (isKnownPredicate(CmpInst::ICMP_EQ
, NewDelta
, Product
)) {
1917 // dependences caused by last iteration
1918 if (Level
< CommonLevels
) {
1919 Result
.DV
[Level
].Direction
&= Dependence::DVEntry::GE
;
1920 Result
.DV
[Level
].PeelLast
= true;
1921 ++WeakZeroSIVsuccesses
;
1927 // check that Delta/SrcCoeff >= 0
1928 // really check that NewDelta >= 0
1929 if (SE
->isKnownNegative(NewDelta
)) {
1930 // No dependence, newDelta < 0
1931 ++WeakZeroSIVindependence
;
1932 ++WeakZeroSIVsuccesses
;
1936 // if SrcCoeff doesn't divide Delta, then no dependence
1937 if (isa
<SCEVConstant
>(Delta
) &&
1938 !isRemainderZero(cast
<SCEVConstant
>(Delta
), ConstCoeff
)) {
1939 ++WeakZeroSIVindependence
;
1940 ++WeakZeroSIVsuccesses
;
1947 // exactRDIVtest - Tests the RDIV subscript pair for dependence.
1948 // Things of the form [c1 + a*i] and [c2 + b*j],
1949 // where i and j are induction variable, c1 and c2 are loop invariant,
1950 // and a and b are constants.
1951 // Returns true if any possible dependence is disproved.
1952 // Marks the result as inconsistent.
1953 // Works in some cases that symbolicRDIVtest doesn't, and vice versa.
1954 bool DependenceInfo::exactRDIVtest(const SCEV
*SrcCoeff
, const SCEV
*DstCoeff
,
1955 const SCEV
*SrcConst
, const SCEV
*DstConst
,
1956 const Loop
*SrcLoop
, const Loop
*DstLoop
,
1957 FullDependence
&Result
) const {
1958 LLVM_DEBUG(dbgs() << "\tExact RDIV test\n");
1959 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff
<< " = AM\n");
1960 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff
<< " = BM\n");
1961 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst
<< "\n");
1962 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst
<< "\n");
1963 ++ExactRDIVapplications
;
1964 Result
.Consistent
= false;
1965 const SCEV
*Delta
= SE
->getMinusSCEV(DstConst
, SrcConst
);
1966 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta
<< "\n");
1967 const SCEVConstant
*ConstDelta
= dyn_cast
<SCEVConstant
>(Delta
);
1968 const SCEVConstant
*ConstSrcCoeff
= dyn_cast
<SCEVConstant
>(SrcCoeff
);
1969 const SCEVConstant
*ConstDstCoeff
= dyn_cast
<SCEVConstant
>(DstCoeff
);
1970 if (!ConstDelta
|| !ConstSrcCoeff
|| !ConstDstCoeff
)
1975 APInt AM
= ConstSrcCoeff
->getAPInt();
1976 APInt BM
= ConstDstCoeff
->getAPInt();
1977 APInt CM
= ConstDelta
->getAPInt();
1978 unsigned Bits
= AM
.getBitWidth();
1979 if (findGCD(Bits
, AM
, BM
, CM
, G
, X
, Y
)) {
1980 // gcd doesn't divide Delta, no dependence
1981 ++ExactRDIVindependence
;
1985 LLVM_DEBUG(dbgs() << "\t X = " << X
<< ", Y = " << Y
<< "\n");
1987 // since SCEV construction seems to normalize, LM = 0
1988 APInt
SrcUM(Bits
, 1, true);
1989 bool SrcUMvalid
= false;
1990 // SrcUM is perhaps unavailable, let's check
1991 if (const SCEVConstant
*UpperBound
=
1992 collectConstantUpperBound(SrcLoop
, Delta
->getType())) {
1993 SrcUM
= UpperBound
->getAPInt();
1994 LLVM_DEBUG(dbgs() << "\t SrcUM = " << SrcUM
<< "\n");
1998 APInt
DstUM(Bits
, 1, true);
1999 bool DstUMvalid
= false;
2000 // UM is perhaps unavailable, let's check
2001 if (const SCEVConstant
*UpperBound
=
2002 collectConstantUpperBound(DstLoop
, Delta
->getType())) {
2003 DstUM
= UpperBound
->getAPInt();
2004 LLVM_DEBUG(dbgs() << "\t DstUM = " << DstUM
<< "\n");
2008 APInt
TU(APInt::getSignedMaxValue(Bits
));
2009 APInt
TL(APInt::getSignedMinValue(Bits
));
2010 APInt TC
= CM
.sdiv(G
);
2013 LLVM_DEBUG(dbgs() << "\t TC = " << TC
<< "\n");
2014 LLVM_DEBUG(dbgs() << "\t TX = " << TX
<< "\n");
2015 LLVM_DEBUG(dbgs() << "\t TY = " << TY
<< "\n");
2017 SmallVector
<APInt
, 2> TLVec
, TUVec
;
2018 APInt TB
= BM
.sdiv(G
);
2020 TLVec
.push_back(ceilingOfQuotient(-TX
, TB
));
2021 LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec
.back() << "\n");
2023 TUVec
.push_back(floorOfQuotient(SrcUM
- TX
, TB
));
2024 LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec
.back() << "\n");
2027 TUVec
.push_back(floorOfQuotient(-TX
, TB
));
2028 LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec
.back() << "\n");
2030 TLVec
.push_back(ceilingOfQuotient(SrcUM
- TX
, TB
));
2031 LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec
.back() << "\n");
2035 APInt TA
= AM
.sdiv(G
);
2037 TLVec
.push_back(ceilingOfQuotient(-TY
, TA
));
2038 LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec
.back() << "\n");
2040 TUVec
.push_back(floorOfQuotient(DstUM
- TY
, TA
));
2041 LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec
.back() << "\n");
2044 TUVec
.push_back(floorOfQuotient(-TY
, TA
));
2045 LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec
.back() << "\n");
2047 TLVec
.push_back(ceilingOfQuotient(DstUM
- TY
, TA
));
2048 LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec
.back() << "\n");
2052 if (TLVec
.empty() || TUVec
.empty())
2055 LLVM_DEBUG(dbgs() << "\t TA = " << TA
<< "\n");
2056 LLVM_DEBUG(dbgs() << "\t TB = " << TB
<< "\n");
2058 TL
= APIntOps::smax(TLVec
.front(), TLVec
.back());
2059 TU
= APIntOps::smin(TUVec
.front(), TUVec
.back());
2060 LLVM_DEBUG(dbgs() << "\t TL = " << TL
<< "\n");
2061 LLVM_DEBUG(dbgs() << "\t TU = " << TU
<< "\n");
2064 ++ExactRDIVindependence
;
2069 // symbolicRDIVtest -
2070 // In Section 4.5 of the Practical Dependence Testing paper,the authors
2071 // introduce a special case of Banerjee's Inequalities (also called the
2072 // Extreme-Value Test) that can handle some of the SIV and RDIV cases,
2073 // particularly cases with symbolics. Since it's only able to disprove
2074 // dependence (not compute distances or directions), we'll use it as a
2075 // fall back for the other tests.
2077 // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
2078 // where i and j are induction variables and c1 and c2 are loop invariants,
2079 // we can use the symbolic tests to disprove some dependences, serving as a
2080 // backup for the RDIV test. Note that i and j can be the same variable,
2081 // letting this test serve as a backup for the various SIV tests.
2083 // For a dependence to exist, c1 + a1*i must equal c2 + a2*j for some
2084 // 0 <= i <= N1 and some 0 <= j <= N2, where N1 and N2 are the (normalized)
2085 // loop bounds for the i and j loops, respectively. So, ...
2087 // c1 + a1*i = c2 + a2*j
2088 // a1*i - a2*j = c2 - c1
2090 // To test for a dependence, we compute c2 - c1 and make sure it's in the
2091 // range of the maximum and minimum possible values of a1*i - a2*j.
2092 // Considering the signs of a1 and a2, we have 4 possible cases:
2094 // 1) If a1 >= 0 and a2 >= 0, then
2095 // a1*0 - a2*N2 <= c2 - c1 <= a1*N1 - a2*0
2096 // -a2*N2 <= c2 - c1 <= a1*N1
2098 // 2) If a1 >= 0 and a2 <= 0, then
2099 // a1*0 - a2*0 <= c2 - c1 <= a1*N1 - a2*N2
2100 // 0 <= c2 - c1 <= a1*N1 - a2*N2
2102 // 3) If a1 <= 0 and a2 >= 0, then
2103 // a1*N1 - a2*N2 <= c2 - c1 <= a1*0 - a2*0
2104 // a1*N1 - a2*N2 <= c2 - c1 <= 0
2106 // 4) If a1 <= 0 and a2 <= 0, then
2107 // a1*N1 - a2*0 <= c2 - c1 <= a1*0 - a2*N2
2108 // a1*N1 <= c2 - c1 <= -a2*N2
2110 // return true if dependence disproved
2111 bool DependenceInfo::symbolicRDIVtest(const SCEV
*A1
, const SCEV
*A2
,
2112 const SCEV
*C1
, const SCEV
*C2
,
2114 const Loop
*Loop2
) const {
2115 ++SymbolicRDIVapplications
;
2116 LLVM_DEBUG(dbgs() << "\ttry symbolic RDIV test\n");
2117 LLVM_DEBUG(dbgs() << "\t A1 = " << *A1
);
2118 LLVM_DEBUG(dbgs() << ", type = " << *A1
->getType() << "\n");
2119 LLVM_DEBUG(dbgs() << "\t A2 = " << *A2
<< "\n");
2120 LLVM_DEBUG(dbgs() << "\t C1 = " << *C1
<< "\n");
2121 LLVM_DEBUG(dbgs() << "\t C2 = " << *C2
<< "\n");
2122 const SCEV
*N1
= collectUpperBound(Loop1
, A1
->getType());
2123 const SCEV
*N2
= collectUpperBound(Loop2
, A1
->getType());
2124 LLVM_DEBUG(if (N1
) dbgs() << "\t N1 = " << *N1
<< "\n");
2125 LLVM_DEBUG(if (N2
) dbgs() << "\t N2 = " << *N2
<< "\n");
2126 const SCEV
*C2_C1
= SE
->getMinusSCEV(C2
, C1
);
2127 const SCEV
*C1_C2
= SE
->getMinusSCEV(C1
, C2
);
2128 LLVM_DEBUG(dbgs() << "\t C2 - C1 = " << *C2_C1
<< "\n");
2129 LLVM_DEBUG(dbgs() << "\t C1 - C2 = " << *C1_C2
<< "\n");
2130 if (SE
->isKnownNonNegative(A1
)) {
2131 if (SE
->isKnownNonNegative(A2
)) {
2132 // A1 >= 0 && A2 >= 0
2134 // make sure that c2 - c1 <= a1*N1
2135 const SCEV
*A1N1
= SE
->getMulExpr(A1
, N1
);
2136 LLVM_DEBUG(dbgs() << "\t A1*N1 = " << *A1N1
<< "\n");
2137 if (isKnownPredicate(CmpInst::ICMP_SGT
, C2_C1
, A1N1
)) {
2138 ++SymbolicRDIVindependence
;
2143 // make sure that -a2*N2 <= c2 - c1, or a2*N2 >= c1 - c2
2144 const SCEV
*A2N2
= SE
->getMulExpr(A2
, N2
);
2145 LLVM_DEBUG(dbgs() << "\t A2*N2 = " << *A2N2
<< "\n");
2146 if (isKnownPredicate(CmpInst::ICMP_SLT
, A2N2
, C1_C2
)) {
2147 ++SymbolicRDIVindependence
;
2152 else if (SE
->isKnownNonPositive(A2
)) {
2153 // a1 >= 0 && a2 <= 0
2155 // make sure that c2 - c1 <= a1*N1 - a2*N2
2156 const SCEV
*A1N1
= SE
->getMulExpr(A1
, N1
);
2157 const SCEV
*A2N2
= SE
->getMulExpr(A2
, N2
);
2158 const SCEV
*A1N1_A2N2
= SE
->getMinusSCEV(A1N1
, A2N2
);
2159 LLVM_DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2
<< "\n");
2160 if (isKnownPredicate(CmpInst::ICMP_SGT
, C2_C1
, A1N1_A2N2
)) {
2161 ++SymbolicRDIVindependence
;
2165 // make sure that 0 <= c2 - c1
2166 if (SE
->isKnownNegative(C2_C1
)) {
2167 ++SymbolicRDIVindependence
;
2172 else if (SE
->isKnownNonPositive(A1
)) {
2173 if (SE
->isKnownNonNegative(A2
)) {
2174 // a1 <= 0 && a2 >= 0
2176 // make sure that a1*N1 - a2*N2 <= c2 - c1
2177 const SCEV
*A1N1
= SE
->getMulExpr(A1
, N1
);
2178 const SCEV
*A2N2
= SE
->getMulExpr(A2
, N2
);
2179 const SCEV
*A1N1_A2N2
= SE
->getMinusSCEV(A1N1
, A2N2
);
2180 LLVM_DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2
<< "\n");
2181 if (isKnownPredicate(CmpInst::ICMP_SGT
, A1N1_A2N2
, C2_C1
)) {
2182 ++SymbolicRDIVindependence
;
2186 // make sure that c2 - c1 <= 0
2187 if (SE
->isKnownPositive(C2_C1
)) {
2188 ++SymbolicRDIVindependence
;
2192 else if (SE
->isKnownNonPositive(A2
)) {
2193 // a1 <= 0 && a2 <= 0
2195 // make sure that a1*N1 <= c2 - c1
2196 const SCEV
*A1N1
= SE
->getMulExpr(A1
, N1
);
2197 LLVM_DEBUG(dbgs() << "\t A1*N1 = " << *A1N1
<< "\n");
2198 if (isKnownPredicate(CmpInst::ICMP_SGT
, A1N1
, C2_C1
)) {
2199 ++SymbolicRDIVindependence
;
2204 // make sure that c2 - c1 <= -a2*N2, or c1 - c2 >= a2*N2
2205 const SCEV
*A2N2
= SE
->getMulExpr(A2
, N2
);
2206 LLVM_DEBUG(dbgs() << "\t A2*N2 = " << *A2N2
<< "\n");
2207 if (isKnownPredicate(CmpInst::ICMP_SLT
, C1_C2
, A2N2
)) {
2208 ++SymbolicRDIVindependence
;
2219 // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 - a2*i]
2220 // where i is an induction variable, c1 and c2 are loop invariant, and a1 and
2221 // a2 are constant, we attack it with an SIV test. While they can all be
2222 // solved with the Exact SIV test, it's worthwhile to use simpler tests when
2223 // they apply; they're cheaper and sometimes more precise.
2225 // Return true if dependence disproved.
2226 bool DependenceInfo::testSIV(const SCEV
*Src
, const SCEV
*Dst
, unsigned &Level
,
2227 FullDependence
&Result
, Constraint
&NewConstraint
,
2228 const SCEV
*&SplitIter
) const {
2229 LLVM_DEBUG(dbgs() << " src = " << *Src
<< "\n");
2230 LLVM_DEBUG(dbgs() << " dst = " << *Dst
<< "\n");
2231 const SCEVAddRecExpr
*SrcAddRec
= dyn_cast
<SCEVAddRecExpr
>(Src
);
2232 const SCEVAddRecExpr
*DstAddRec
= dyn_cast
<SCEVAddRecExpr
>(Dst
);
2233 if (SrcAddRec
&& DstAddRec
) {
2234 const SCEV
*SrcConst
= SrcAddRec
->getStart();
2235 const SCEV
*DstConst
= DstAddRec
->getStart();
2236 const SCEV
*SrcCoeff
= SrcAddRec
->getStepRecurrence(*SE
);
2237 const SCEV
*DstCoeff
= DstAddRec
->getStepRecurrence(*SE
);
2238 const Loop
*CurLoop
= SrcAddRec
->getLoop();
2239 assert(CurLoop
== DstAddRec
->getLoop() &&
2240 "both loops in SIV should be same");
2241 Level
= mapSrcLoop(CurLoop
);
2243 if (SrcCoeff
== DstCoeff
)
2244 disproven
= strongSIVtest(SrcCoeff
, SrcConst
, DstConst
, CurLoop
,
2245 Level
, Result
, NewConstraint
);
2246 else if (SrcCoeff
== SE
->getNegativeSCEV(DstCoeff
))
2247 disproven
= weakCrossingSIVtest(SrcCoeff
, SrcConst
, DstConst
, CurLoop
,
2248 Level
, Result
, NewConstraint
, SplitIter
);
2250 disproven
= exactSIVtest(SrcCoeff
, DstCoeff
, SrcConst
, DstConst
, CurLoop
,
2251 Level
, Result
, NewConstraint
);
2253 gcdMIVtest(Src
, Dst
, Result
) ||
2254 symbolicRDIVtest(SrcCoeff
, DstCoeff
, SrcConst
, DstConst
, CurLoop
, CurLoop
);
2257 const SCEV
*SrcConst
= SrcAddRec
->getStart();
2258 const SCEV
*SrcCoeff
= SrcAddRec
->getStepRecurrence(*SE
);
2259 const SCEV
*DstConst
= Dst
;
2260 const Loop
*CurLoop
= SrcAddRec
->getLoop();
2261 Level
= mapSrcLoop(CurLoop
);
2262 return weakZeroDstSIVtest(SrcCoeff
, SrcConst
, DstConst
, CurLoop
,
2263 Level
, Result
, NewConstraint
) ||
2264 gcdMIVtest(Src
, Dst
, Result
);
2267 const SCEV
*DstConst
= DstAddRec
->getStart();
2268 const SCEV
*DstCoeff
= DstAddRec
->getStepRecurrence(*SE
);
2269 const SCEV
*SrcConst
= Src
;
2270 const Loop
*CurLoop
= DstAddRec
->getLoop();
2271 Level
= mapDstLoop(CurLoop
);
2272 return weakZeroSrcSIVtest(DstCoeff
, SrcConst
, DstConst
,
2273 CurLoop
, Level
, Result
, NewConstraint
) ||
2274 gcdMIVtest(Src
, Dst
, Result
);
2276 llvm_unreachable("SIV test expected at least one AddRec");
2282 // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
2283 // where i and j are induction variables, c1 and c2 are loop invariant,
2284 // and a1 and a2 are constant, we can solve it exactly with an easy adaptation
2285 // of the Exact SIV test, the Restricted Double Index Variable (RDIV) test.
2286 // It doesn't make sense to talk about distance or direction in this case,
2287 // so there's no point in making special versions of the Strong SIV test or
2288 // the Weak-crossing SIV test.
2290 // With minor algebra, this test can also be used for things like
2291 // [c1 + a1*i + a2*j][c2].
2293 // Return true if dependence disproved.
2294 bool DependenceInfo::testRDIV(const SCEV
*Src
, const SCEV
*Dst
,
2295 FullDependence
&Result
) const {
2296 // we have 3 possible situations here:
2297 // 1) [a*i + b] and [c*j + d]
2298 // 2) [a*i + c*j + b] and [d]
2299 // 3) [b] and [a*i + c*j + d]
2300 // We need to find what we've got and get organized
2302 const SCEV
*SrcConst
, *DstConst
;
2303 const SCEV
*SrcCoeff
, *DstCoeff
;
2304 const Loop
*SrcLoop
, *DstLoop
;
2306 LLVM_DEBUG(dbgs() << " src = " << *Src
<< "\n");
2307 LLVM_DEBUG(dbgs() << " dst = " << *Dst
<< "\n");
2308 const SCEVAddRecExpr
*SrcAddRec
= dyn_cast
<SCEVAddRecExpr
>(Src
);
2309 const SCEVAddRecExpr
*DstAddRec
= dyn_cast
<SCEVAddRecExpr
>(Dst
);
2310 if (SrcAddRec
&& DstAddRec
) {
2311 SrcConst
= SrcAddRec
->getStart();
2312 SrcCoeff
= SrcAddRec
->getStepRecurrence(*SE
);
2313 SrcLoop
= SrcAddRec
->getLoop();
2314 DstConst
= DstAddRec
->getStart();
2315 DstCoeff
= DstAddRec
->getStepRecurrence(*SE
);
2316 DstLoop
= DstAddRec
->getLoop();
2318 else if (SrcAddRec
) {
2319 if (const SCEVAddRecExpr
*tmpAddRec
=
2320 dyn_cast
<SCEVAddRecExpr
>(SrcAddRec
->getStart())) {
2321 SrcConst
= tmpAddRec
->getStart();
2322 SrcCoeff
= tmpAddRec
->getStepRecurrence(*SE
);
2323 SrcLoop
= tmpAddRec
->getLoop();
2325 DstCoeff
= SE
->getNegativeSCEV(SrcAddRec
->getStepRecurrence(*SE
));
2326 DstLoop
= SrcAddRec
->getLoop();
2329 llvm_unreachable("RDIV reached by surprising SCEVs");
2331 else if (DstAddRec
) {
2332 if (const SCEVAddRecExpr
*tmpAddRec
=
2333 dyn_cast
<SCEVAddRecExpr
>(DstAddRec
->getStart())) {
2334 DstConst
= tmpAddRec
->getStart();
2335 DstCoeff
= tmpAddRec
->getStepRecurrence(*SE
);
2336 DstLoop
= tmpAddRec
->getLoop();
2338 SrcCoeff
= SE
->getNegativeSCEV(DstAddRec
->getStepRecurrence(*SE
));
2339 SrcLoop
= DstAddRec
->getLoop();
2342 llvm_unreachable("RDIV reached by surprising SCEVs");
2345 llvm_unreachable("RDIV expected at least one AddRec");
2346 return exactRDIVtest(SrcCoeff
, DstCoeff
,
2350 gcdMIVtest(Src
, Dst
, Result
) ||
2351 symbolicRDIVtest(SrcCoeff
, DstCoeff
,
2357 // Tests the single-subscript MIV pair (Src and Dst) for dependence.
2358 // Return true if dependence disproved.
2359 // Can sometimes refine direction vectors.
2360 bool DependenceInfo::testMIV(const SCEV
*Src
, const SCEV
*Dst
,
2361 const SmallBitVector
&Loops
,
2362 FullDependence
&Result
) const {
2363 LLVM_DEBUG(dbgs() << " src = " << *Src
<< "\n");
2364 LLVM_DEBUG(dbgs() << " dst = " << *Dst
<< "\n");
2365 Result
.Consistent
= false;
2366 return gcdMIVtest(Src
, Dst
, Result
) ||
2367 banerjeeMIVtest(Src
, Dst
, Loops
, Result
);
2371 // Given a product, e.g., 10*X*Y, returns the first constant operand,
2372 // in this case 10. If there is no constant part, returns NULL.
2374 const SCEVConstant
*getConstantPart(const SCEV
*Expr
) {
2375 if (const auto *Constant
= dyn_cast
<SCEVConstant
>(Expr
))
2377 else if (const auto *Product
= dyn_cast
<SCEVMulExpr
>(Expr
))
2378 if (const auto *Constant
= dyn_cast
<SCEVConstant
>(Product
->getOperand(0)))
2384 //===----------------------------------------------------------------------===//
2386 // Tests an MIV subscript pair for dependence.
2387 // Returns true if any possible dependence is disproved.
2388 // Marks the result as inconsistent.
2389 // Can sometimes disprove the equal direction for 1 or more loops,
2390 // as discussed in Michael Wolfe's book,
2391 // High Performance Compilers for Parallel Computing, page 235.
2393 // We spend some effort (code!) to handle cases like
2394 // [10*i + 5*N*j + 15*M + 6], where i and j are induction variables,
2395 // but M and N are just loop-invariant variables.
2396 // This should help us handle linearized subscripts;
2397 // also makes this test a useful backup to the various SIV tests.
2399 // It occurs to me that the presence of loop-invariant variables
2400 // changes the nature of the test from "greatest common divisor"
2401 // to "a common divisor".
2402 bool DependenceInfo::gcdMIVtest(const SCEV
*Src
, const SCEV
*Dst
,
2403 FullDependence
&Result
) const {
2404 LLVM_DEBUG(dbgs() << "starting gcd\n");
2406 unsigned BitWidth
= SE
->getTypeSizeInBits(Src
->getType());
2407 APInt RunningGCD
= APInt::getZero(BitWidth
);
2409 // Examine Src coefficients.
2410 // Compute running GCD and record source constant.
2411 // Because we're looking for the constant at the end of the chain,
2412 // we can't quit the loop just because the GCD == 1.
2413 const SCEV
*Coefficients
= Src
;
2414 while (const SCEVAddRecExpr
*AddRec
=
2415 dyn_cast
<SCEVAddRecExpr
>(Coefficients
)) {
2416 const SCEV
*Coeff
= AddRec
->getStepRecurrence(*SE
);
2417 // If the coefficient is the product of a constant and other stuff,
2418 // we can use the constant in the GCD computation.
2419 const auto *Constant
= getConstantPart(Coeff
);
2422 APInt ConstCoeff
= Constant
->getAPInt();
2423 RunningGCD
= APIntOps::GreatestCommonDivisor(RunningGCD
, ConstCoeff
.abs());
2424 Coefficients
= AddRec
->getStart();
2426 const SCEV
*SrcConst
= Coefficients
;
2428 // Examine Dst coefficients.
2429 // Compute running GCD and record destination constant.
2430 // Because we're looking for the constant at the end of the chain,
2431 // we can't quit the loop just because the GCD == 1.
2433 while (const SCEVAddRecExpr
*AddRec
=
2434 dyn_cast
<SCEVAddRecExpr
>(Coefficients
)) {
2435 const SCEV
*Coeff
= AddRec
->getStepRecurrence(*SE
);
2436 // If the coefficient is the product of a constant and other stuff,
2437 // we can use the constant in the GCD computation.
2438 const auto *Constant
= getConstantPart(Coeff
);
2441 APInt ConstCoeff
= Constant
->getAPInt();
2442 RunningGCD
= APIntOps::GreatestCommonDivisor(RunningGCD
, ConstCoeff
.abs());
2443 Coefficients
= AddRec
->getStart();
2445 const SCEV
*DstConst
= Coefficients
;
2447 APInt ExtraGCD
= APInt::getZero(BitWidth
);
2448 const SCEV
*Delta
= SE
->getMinusSCEV(DstConst
, SrcConst
);
2449 LLVM_DEBUG(dbgs() << " Delta = " << *Delta
<< "\n");
2450 const SCEVConstant
*Constant
= dyn_cast
<SCEVConstant
>(Delta
);
2451 if (const SCEVAddExpr
*Sum
= dyn_cast
<SCEVAddExpr
>(Delta
)) {
2452 // If Delta is a sum of products, we may be able to make further progress.
2453 for (const SCEV
*Operand
: Sum
->operands()) {
2454 if (isa
<SCEVConstant
>(Operand
)) {
2455 assert(!Constant
&& "Surprised to find multiple constants");
2456 Constant
= cast
<SCEVConstant
>(Operand
);
2458 else if (const SCEVMulExpr
*Product
= dyn_cast
<SCEVMulExpr
>(Operand
)) {
2459 // Search for constant operand to participate in GCD;
2460 // If none found; return false.
2461 const SCEVConstant
*ConstOp
= getConstantPart(Product
);
2464 APInt ConstOpValue
= ConstOp
->getAPInt();
2465 ExtraGCD
= APIntOps::GreatestCommonDivisor(ExtraGCD
,
2466 ConstOpValue
.abs());
2474 APInt ConstDelta
= cast
<SCEVConstant
>(Constant
)->getAPInt();
2475 LLVM_DEBUG(dbgs() << " ConstDelta = " << ConstDelta
<< "\n");
2476 if (ConstDelta
== 0)
2478 RunningGCD
= APIntOps::GreatestCommonDivisor(RunningGCD
, ExtraGCD
);
2479 LLVM_DEBUG(dbgs() << " RunningGCD = " << RunningGCD
<< "\n");
2480 APInt Remainder
= ConstDelta
.srem(RunningGCD
);
2481 if (Remainder
!= 0) {
2486 // Try to disprove equal directions.
2487 // For example, given a subscript pair [3*i + 2*j] and [i' + 2*j' - 1],
2488 // the code above can't disprove the dependence because the GCD = 1.
2489 // So we consider what happen if i = i' and what happens if j = j'.
2490 // If i = i', we can simplify the subscript to [2*i + 2*j] and [2*j' - 1],
2491 // which is infeasible, so we can disallow the = direction for the i level.
2492 // Setting j = j' doesn't help matters, so we end up with a direction vector
2495 // Given A[5*i + 10*j*M + 9*M*N] and A[15*i + 20*j*M - 21*N*M + 5],
2496 // we need to remember that the constant part is 5 and the RunningGCD should
2497 // be initialized to ExtraGCD = 30.
2498 LLVM_DEBUG(dbgs() << " ExtraGCD = " << ExtraGCD
<< '\n');
2500 bool Improved
= false;
2502 while (const SCEVAddRecExpr
*AddRec
=
2503 dyn_cast
<SCEVAddRecExpr
>(Coefficients
)) {
2504 Coefficients
= AddRec
->getStart();
2505 const Loop
*CurLoop
= AddRec
->getLoop();
2506 RunningGCD
= ExtraGCD
;
2507 const SCEV
*SrcCoeff
= AddRec
->getStepRecurrence(*SE
);
2508 const SCEV
*DstCoeff
= SE
->getMinusSCEV(SrcCoeff
, SrcCoeff
);
2509 const SCEV
*Inner
= Src
;
2510 while (RunningGCD
!= 1 && isa
<SCEVAddRecExpr
>(Inner
)) {
2511 AddRec
= cast
<SCEVAddRecExpr
>(Inner
);
2512 const SCEV
*Coeff
= AddRec
->getStepRecurrence(*SE
);
2513 if (CurLoop
== AddRec
->getLoop())
2514 ; // SrcCoeff == Coeff
2516 // If the coefficient is the product of a constant and other stuff,
2517 // we can use the constant in the GCD computation.
2518 Constant
= getConstantPart(Coeff
);
2521 APInt ConstCoeff
= Constant
->getAPInt();
2522 RunningGCD
= APIntOps::GreatestCommonDivisor(RunningGCD
, ConstCoeff
.abs());
2524 Inner
= AddRec
->getStart();
2527 while (RunningGCD
!= 1 && isa
<SCEVAddRecExpr
>(Inner
)) {
2528 AddRec
= cast
<SCEVAddRecExpr
>(Inner
);
2529 const SCEV
*Coeff
= AddRec
->getStepRecurrence(*SE
);
2530 if (CurLoop
== AddRec
->getLoop())
2533 // If the coefficient is the product of a constant and other stuff,
2534 // we can use the constant in the GCD computation.
2535 Constant
= getConstantPart(Coeff
);
2538 APInt ConstCoeff
= Constant
->getAPInt();
2539 RunningGCD
= APIntOps::GreatestCommonDivisor(RunningGCD
, ConstCoeff
.abs());
2541 Inner
= AddRec
->getStart();
2543 Delta
= SE
->getMinusSCEV(SrcCoeff
, DstCoeff
);
2544 // If the coefficient is the product of a constant and other stuff,
2545 // we can use the constant in the GCD computation.
2546 Constant
= getConstantPart(Delta
);
2548 // The difference of the two coefficients might not be a product
2549 // or constant, in which case we give up on this direction.
2551 APInt ConstCoeff
= Constant
->getAPInt();
2552 RunningGCD
= APIntOps::GreatestCommonDivisor(RunningGCD
, ConstCoeff
.abs());
2553 LLVM_DEBUG(dbgs() << "\tRunningGCD = " << RunningGCD
<< "\n");
2554 if (RunningGCD
!= 0) {
2555 Remainder
= ConstDelta
.srem(RunningGCD
);
2556 LLVM_DEBUG(dbgs() << "\tRemainder = " << Remainder
<< "\n");
2557 if (Remainder
!= 0) {
2558 unsigned Level
= mapSrcLoop(CurLoop
);
2559 Result
.DV
[Level
- 1].Direction
&= ~Dependence::DVEntry::EQ
;
2566 LLVM_DEBUG(dbgs() << "all done\n");
2571 //===----------------------------------------------------------------------===//
2572 // banerjeeMIVtest -
2573 // Use Banerjee's Inequalities to test an MIV subscript pair.
2574 // (Wolfe, in the race-car book, calls this the Extreme Value Test.)
2575 // Generally follows the discussion in Section 2.5.2 of
2577 // Optimizing Supercompilers for Supercomputers
2580 // The inequalities given on page 25 are simplified in that loops are
2581 // normalized so that the lower bound is always 0 and the stride is always 1.
2582 // For example, Wolfe gives
2584 // LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2586 // where A_k is the coefficient of the kth index in the source subscript,
2587 // B_k is the coefficient of the kth index in the destination subscript,
2588 // U_k is the upper bound of the kth index, L_k is the lower bound of the Kth
2589 // index, and N_k is the stride of the kth index. Since all loops are normalized
2590 // by the SCEV package, N_k = 1 and L_k = 0, allowing us to simplify the
2593 // LB^<_k = (A^-_k - B_k)^- (U_k - 0 - 1) + (A_k - B_k)0 - B_k 1
2594 // = (A^-_k - B_k)^- (U_k - 1) - B_k
2596 // Similar simplifications are possible for the other equations.
2598 // When we can't determine the number of iterations for a loop,
2599 // we use NULL as an indicator for the worst case, infinity.
2600 // When computing the upper bound, NULL denotes +inf;
2601 // for the lower bound, NULL denotes -inf.
2603 // Return true if dependence disproved.
2604 bool DependenceInfo::banerjeeMIVtest(const SCEV
*Src
, const SCEV
*Dst
,
2605 const SmallBitVector
&Loops
,
2606 FullDependence
&Result
) const {
2607 LLVM_DEBUG(dbgs() << "starting Banerjee\n");
2608 ++BanerjeeApplications
;
2609 LLVM_DEBUG(dbgs() << " Src = " << *Src
<< '\n');
2611 CoefficientInfo
*A
= collectCoeffInfo(Src
, true, A0
);
2612 LLVM_DEBUG(dbgs() << " Dst = " << *Dst
<< '\n');
2614 CoefficientInfo
*B
= collectCoeffInfo(Dst
, false, B0
);
2615 BoundInfo
*Bound
= new BoundInfo
[MaxLevels
+ 1];
2616 const SCEV
*Delta
= SE
->getMinusSCEV(B0
, A0
);
2617 LLVM_DEBUG(dbgs() << "\tDelta = " << *Delta
<< '\n');
2619 // Compute bounds for all the * directions.
2620 LLVM_DEBUG(dbgs() << "\tBounds[*]\n");
2621 for (unsigned K
= 1; K
<= MaxLevels
; ++K
) {
2622 Bound
[K
].Iterations
= A
[K
].Iterations
? A
[K
].Iterations
: B
[K
].Iterations
;
2623 Bound
[K
].Direction
= Dependence::DVEntry::ALL
;
2624 Bound
[K
].DirSet
= Dependence::DVEntry::NONE
;
2625 findBoundsALL(A
, B
, Bound
, K
);
2627 LLVM_DEBUG(dbgs() << "\t " << K
<< '\t');
2628 if (Bound
[K
].Lower
[Dependence::DVEntry::ALL
])
2629 LLVM_DEBUG(dbgs() << *Bound
[K
].Lower
[Dependence::DVEntry::ALL
] << '\t');
2631 LLVM_DEBUG(dbgs() << "-inf\t");
2632 if (Bound
[K
].Upper
[Dependence::DVEntry::ALL
])
2633 LLVM_DEBUG(dbgs() << *Bound
[K
].Upper
[Dependence::DVEntry::ALL
] << '\n');
2635 LLVM_DEBUG(dbgs() << "+inf\n");
2639 // Test the *, *, *, ... case.
2640 bool Disproved
= false;
2641 if (testBounds(Dependence::DVEntry::ALL
, 0, Bound
, Delta
)) {
2642 // Explore the direction vector hierarchy.
2643 unsigned DepthExpanded
= 0;
2644 unsigned NewDeps
= exploreDirections(1, A
, B
, Bound
,
2645 Loops
, DepthExpanded
, Delta
);
2647 bool Improved
= false;
2648 for (unsigned K
= 1; K
<= CommonLevels
; ++K
) {
2650 unsigned Old
= Result
.DV
[K
- 1].Direction
;
2651 Result
.DV
[K
- 1].Direction
= Old
& Bound
[K
].DirSet
;
2652 Improved
|= Old
!= Result
.DV
[K
- 1].Direction
;
2653 if (!Result
.DV
[K
- 1].Direction
) {
2661 ++BanerjeeSuccesses
;
2664 ++BanerjeeIndependence
;
2669 ++BanerjeeIndependence
;
2679 // Hierarchically expands the direction vector
2680 // search space, combining the directions of discovered dependences
2681 // in the DirSet field of Bound. Returns the number of distinct
2682 // dependences discovered. If the dependence is disproved,
2683 // it will return 0.
2684 unsigned DependenceInfo::exploreDirections(unsigned Level
, CoefficientInfo
*A
,
2685 CoefficientInfo
*B
, BoundInfo
*Bound
,
2686 const SmallBitVector
&Loops
,
2687 unsigned &DepthExpanded
,
2688 const SCEV
*Delta
) const {
2689 // This algorithm has worst case complexity of O(3^n), where 'n' is the number
2690 // of common loop levels. To avoid excessive compile-time, pessimize all the
2691 // results and immediately return when the number of common levels is beyond
2692 // the given threshold.
2693 if (CommonLevels
> MIVMaxLevelThreshold
) {
2694 LLVM_DEBUG(dbgs() << "Number of common levels exceeded the threshold. MIV "
2695 "direction exploration is terminated.\n");
2696 for (unsigned K
= 1; K
<= CommonLevels
; ++K
)
2698 Bound
[K
].DirSet
= Dependence::DVEntry::ALL
;
2702 if (Level
> CommonLevels
) {
2704 LLVM_DEBUG(dbgs() << "\t[");
2705 for (unsigned K
= 1; K
<= CommonLevels
; ++K
) {
2707 Bound
[K
].DirSet
|= Bound
[K
].Direction
;
2709 switch (Bound
[K
].Direction
) {
2710 case Dependence::DVEntry::LT
:
2711 LLVM_DEBUG(dbgs() << " <");
2713 case Dependence::DVEntry::EQ
:
2714 LLVM_DEBUG(dbgs() << " =");
2716 case Dependence::DVEntry::GT
:
2717 LLVM_DEBUG(dbgs() << " >");
2719 case Dependence::DVEntry::ALL
:
2720 LLVM_DEBUG(dbgs() << " *");
2723 llvm_unreachable("unexpected Bound[K].Direction");
2728 LLVM_DEBUG(dbgs() << " ]\n");
2732 if (Level
> DepthExpanded
) {
2733 DepthExpanded
= Level
;
2734 // compute bounds for <, =, > at current level
2735 findBoundsLT(A
, B
, Bound
, Level
);
2736 findBoundsGT(A
, B
, Bound
, Level
);
2737 findBoundsEQ(A
, B
, Bound
, Level
);
2739 LLVM_DEBUG(dbgs() << "\tBound for level = " << Level
<< '\n');
2740 LLVM_DEBUG(dbgs() << "\t <\t");
2741 if (Bound
[Level
].Lower
[Dependence::DVEntry::LT
])
2742 LLVM_DEBUG(dbgs() << *Bound
[Level
].Lower
[Dependence::DVEntry::LT
]
2745 LLVM_DEBUG(dbgs() << "-inf\t");
2746 if (Bound
[Level
].Upper
[Dependence::DVEntry::LT
])
2747 LLVM_DEBUG(dbgs() << *Bound
[Level
].Upper
[Dependence::DVEntry::LT
]
2750 LLVM_DEBUG(dbgs() << "+inf\n");
2751 LLVM_DEBUG(dbgs() << "\t =\t");
2752 if (Bound
[Level
].Lower
[Dependence::DVEntry::EQ
])
2753 LLVM_DEBUG(dbgs() << *Bound
[Level
].Lower
[Dependence::DVEntry::EQ
]
2756 LLVM_DEBUG(dbgs() << "-inf\t");
2757 if (Bound
[Level
].Upper
[Dependence::DVEntry::EQ
])
2758 LLVM_DEBUG(dbgs() << *Bound
[Level
].Upper
[Dependence::DVEntry::EQ
]
2761 LLVM_DEBUG(dbgs() << "+inf\n");
2762 LLVM_DEBUG(dbgs() << "\t >\t");
2763 if (Bound
[Level
].Lower
[Dependence::DVEntry::GT
])
2764 LLVM_DEBUG(dbgs() << *Bound
[Level
].Lower
[Dependence::DVEntry::GT
]
2767 LLVM_DEBUG(dbgs() << "-inf\t");
2768 if (Bound
[Level
].Upper
[Dependence::DVEntry::GT
])
2769 LLVM_DEBUG(dbgs() << *Bound
[Level
].Upper
[Dependence::DVEntry::GT
]
2772 LLVM_DEBUG(dbgs() << "+inf\n");
2776 unsigned NewDeps
= 0;
2778 // test bounds for <, *, *, ...
2779 if (testBounds(Dependence::DVEntry::LT
, Level
, Bound
, Delta
))
2780 NewDeps
+= exploreDirections(Level
+ 1, A
, B
, Bound
,
2781 Loops
, DepthExpanded
, Delta
);
2783 // Test bounds for =, *, *, ...
2784 if (testBounds(Dependence::DVEntry::EQ
, Level
, Bound
, Delta
))
2785 NewDeps
+= exploreDirections(Level
+ 1, A
, B
, Bound
,
2786 Loops
, DepthExpanded
, Delta
);
2788 // test bounds for >, *, *, ...
2789 if (testBounds(Dependence::DVEntry::GT
, Level
, Bound
, Delta
))
2790 NewDeps
+= exploreDirections(Level
+ 1, A
, B
, Bound
,
2791 Loops
, DepthExpanded
, Delta
);
2793 Bound
[Level
].Direction
= Dependence::DVEntry::ALL
;
2797 return exploreDirections(Level
+ 1, A
, B
, Bound
, Loops
, DepthExpanded
, Delta
);
2801 // Returns true iff the current bounds are plausible.
2802 bool DependenceInfo::testBounds(unsigned char DirKind
, unsigned Level
,
2803 BoundInfo
*Bound
, const SCEV
*Delta
) const {
2804 Bound
[Level
].Direction
= DirKind
;
2805 if (const SCEV
*LowerBound
= getLowerBound(Bound
))
2806 if (isKnownPredicate(CmpInst::ICMP_SGT
, LowerBound
, Delta
))
2808 if (const SCEV
*UpperBound
= getUpperBound(Bound
))
2809 if (isKnownPredicate(CmpInst::ICMP_SGT
, Delta
, UpperBound
))
2815 // Computes the upper and lower bounds for level K
2816 // using the * direction. Records them in Bound.
2817 // Wolfe gives the equations
2819 // LB^*_k = (A^-_k - B^+_k)(U_k - L_k) + (A_k - B_k)L_k
2820 // UB^*_k = (A^+_k - B^-_k)(U_k - L_k) + (A_k - B_k)L_k
2822 // Since we normalize loops, we can simplify these equations to
2824 // LB^*_k = (A^-_k - B^+_k)U_k
2825 // UB^*_k = (A^+_k - B^-_k)U_k
2827 // We must be careful to handle the case where the upper bound is unknown.
2828 // Note that the lower bound is always <= 0
2829 // and the upper bound is always >= 0.
2830 void DependenceInfo::findBoundsALL(CoefficientInfo
*A
, CoefficientInfo
*B
,
2831 BoundInfo
*Bound
, unsigned K
) const {
2832 Bound
[K
].Lower
[Dependence::DVEntry::ALL
] = nullptr; // Default value = -infinity.
2833 Bound
[K
].Upper
[Dependence::DVEntry::ALL
] = nullptr; // Default value = +infinity.
2834 if (Bound
[K
].Iterations
) {
2835 Bound
[K
].Lower
[Dependence::DVEntry::ALL
] =
2836 SE
->getMulExpr(SE
->getMinusSCEV(A
[K
].NegPart
, B
[K
].PosPart
),
2837 Bound
[K
].Iterations
);
2838 Bound
[K
].Upper
[Dependence::DVEntry::ALL
] =
2839 SE
->getMulExpr(SE
->getMinusSCEV(A
[K
].PosPart
, B
[K
].NegPart
),
2840 Bound
[K
].Iterations
);
2843 // If the difference is 0, we won't need to know the number of iterations.
2844 if (isKnownPredicate(CmpInst::ICMP_EQ
, A
[K
].NegPart
, B
[K
].PosPart
))
2845 Bound
[K
].Lower
[Dependence::DVEntry::ALL
] =
2846 SE
->getZero(A
[K
].Coeff
->getType());
2847 if (isKnownPredicate(CmpInst::ICMP_EQ
, A
[K
].PosPart
, B
[K
].NegPart
))
2848 Bound
[K
].Upper
[Dependence::DVEntry::ALL
] =
2849 SE
->getZero(A
[K
].Coeff
->getType());
2854 // Computes the upper and lower bounds for level K
2855 // using the = direction. Records them in Bound.
2856 // Wolfe gives the equations
2858 // LB^=_k = (A_k - B_k)^- (U_k - L_k) + (A_k - B_k)L_k
2859 // UB^=_k = (A_k - B_k)^+ (U_k - L_k) + (A_k - B_k)L_k
2861 // Since we normalize loops, we can simplify these equations to
2863 // LB^=_k = (A_k - B_k)^- U_k
2864 // UB^=_k = (A_k - B_k)^+ U_k
2866 // We must be careful to handle the case where the upper bound is unknown.
2867 // Note that the lower bound is always <= 0
2868 // and the upper bound is always >= 0.
2869 void DependenceInfo::findBoundsEQ(CoefficientInfo
*A
, CoefficientInfo
*B
,
2870 BoundInfo
*Bound
, unsigned K
) const {
2871 Bound
[K
].Lower
[Dependence::DVEntry::EQ
] = nullptr; // Default value = -infinity.
2872 Bound
[K
].Upper
[Dependence::DVEntry::EQ
] = nullptr; // Default value = +infinity.
2873 if (Bound
[K
].Iterations
) {
2874 const SCEV
*Delta
= SE
->getMinusSCEV(A
[K
].Coeff
, B
[K
].Coeff
);
2875 const SCEV
*NegativePart
= getNegativePart(Delta
);
2876 Bound
[K
].Lower
[Dependence::DVEntry::EQ
] =
2877 SE
->getMulExpr(NegativePart
, Bound
[K
].Iterations
);
2878 const SCEV
*PositivePart
= getPositivePart(Delta
);
2879 Bound
[K
].Upper
[Dependence::DVEntry::EQ
] =
2880 SE
->getMulExpr(PositivePart
, Bound
[K
].Iterations
);
2883 // If the positive/negative part of the difference is 0,
2884 // we won't need to know the number of iterations.
2885 const SCEV
*Delta
= SE
->getMinusSCEV(A
[K
].Coeff
, B
[K
].Coeff
);
2886 const SCEV
*NegativePart
= getNegativePart(Delta
);
2887 if (NegativePart
->isZero())
2888 Bound
[K
].Lower
[Dependence::DVEntry::EQ
] = NegativePart
; // Zero
2889 const SCEV
*PositivePart
= getPositivePart(Delta
);
2890 if (PositivePart
->isZero())
2891 Bound
[K
].Upper
[Dependence::DVEntry::EQ
] = PositivePart
; // Zero
2896 // Computes the upper and lower bounds for level K
2897 // using the < direction. Records them in Bound.
2898 // Wolfe gives the equations
2900 // LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2901 // UB^<_k = (A^+_k - B_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2903 // Since we normalize loops, we can simplify these equations to
2905 // LB^<_k = (A^-_k - B_k)^- (U_k - 1) - B_k
2906 // UB^<_k = (A^+_k - B_k)^+ (U_k - 1) - B_k
2908 // We must be careful to handle the case where the upper bound is unknown.
2909 void DependenceInfo::findBoundsLT(CoefficientInfo
*A
, CoefficientInfo
*B
,
2910 BoundInfo
*Bound
, unsigned K
) const {
2911 Bound
[K
].Lower
[Dependence::DVEntry::LT
] = nullptr; // Default value = -infinity.
2912 Bound
[K
].Upper
[Dependence::DVEntry::LT
] = nullptr; // Default value = +infinity.
2913 if (Bound
[K
].Iterations
) {
2914 const SCEV
*Iter_1
= SE
->getMinusSCEV(
2915 Bound
[K
].Iterations
, SE
->getOne(Bound
[K
].Iterations
->getType()));
2916 const SCEV
*NegPart
=
2917 getNegativePart(SE
->getMinusSCEV(A
[K
].NegPart
, B
[K
].Coeff
));
2918 Bound
[K
].Lower
[Dependence::DVEntry::LT
] =
2919 SE
->getMinusSCEV(SE
->getMulExpr(NegPart
, Iter_1
), B
[K
].Coeff
);
2920 const SCEV
*PosPart
=
2921 getPositivePart(SE
->getMinusSCEV(A
[K
].PosPart
, B
[K
].Coeff
));
2922 Bound
[K
].Upper
[Dependence::DVEntry::LT
] =
2923 SE
->getMinusSCEV(SE
->getMulExpr(PosPart
, Iter_1
), B
[K
].Coeff
);
2926 // If the positive/negative part of the difference is 0,
2927 // we won't need to know the number of iterations.
2928 const SCEV
*NegPart
=
2929 getNegativePart(SE
->getMinusSCEV(A
[K
].NegPart
, B
[K
].Coeff
));
2930 if (NegPart
->isZero())
2931 Bound
[K
].Lower
[Dependence::DVEntry::LT
] = SE
->getNegativeSCEV(B
[K
].Coeff
);
2932 const SCEV
*PosPart
=
2933 getPositivePart(SE
->getMinusSCEV(A
[K
].PosPart
, B
[K
].Coeff
));
2934 if (PosPart
->isZero())
2935 Bound
[K
].Upper
[Dependence::DVEntry::LT
] = SE
->getNegativeSCEV(B
[K
].Coeff
);
2940 // Computes the upper and lower bounds for level K
2941 // using the > direction. Records them in Bound.
2942 // Wolfe gives the equations
2944 // LB^>_k = (A_k - B^+_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
2945 // UB^>_k = (A_k - B^-_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
2947 // Since we normalize loops, we can simplify these equations to
2949 // LB^>_k = (A_k - B^+_k)^- (U_k - 1) + A_k
2950 // UB^>_k = (A_k - B^-_k)^+ (U_k - 1) + A_k
2952 // We must be careful to handle the case where the upper bound is unknown.
2953 void DependenceInfo::findBoundsGT(CoefficientInfo
*A
, CoefficientInfo
*B
,
2954 BoundInfo
*Bound
, unsigned K
) const {
2955 Bound
[K
].Lower
[Dependence::DVEntry::GT
] = nullptr; // Default value = -infinity.
2956 Bound
[K
].Upper
[Dependence::DVEntry::GT
] = nullptr; // Default value = +infinity.
2957 if (Bound
[K
].Iterations
) {
2958 const SCEV
*Iter_1
= SE
->getMinusSCEV(
2959 Bound
[K
].Iterations
, SE
->getOne(Bound
[K
].Iterations
->getType()));
2960 const SCEV
*NegPart
=
2961 getNegativePart(SE
->getMinusSCEV(A
[K
].Coeff
, B
[K
].PosPart
));
2962 Bound
[K
].Lower
[Dependence::DVEntry::GT
] =
2963 SE
->getAddExpr(SE
->getMulExpr(NegPart
, Iter_1
), A
[K
].Coeff
);
2964 const SCEV
*PosPart
=
2965 getPositivePart(SE
->getMinusSCEV(A
[K
].Coeff
, B
[K
].NegPart
));
2966 Bound
[K
].Upper
[Dependence::DVEntry::GT
] =
2967 SE
->getAddExpr(SE
->getMulExpr(PosPart
, Iter_1
), A
[K
].Coeff
);
2970 // If the positive/negative part of the difference is 0,
2971 // we won't need to know the number of iterations.
2972 const SCEV
*NegPart
= getNegativePart(SE
->getMinusSCEV(A
[K
].Coeff
, B
[K
].PosPart
));
2973 if (NegPart
->isZero())
2974 Bound
[K
].Lower
[Dependence::DVEntry::GT
] = A
[K
].Coeff
;
2975 const SCEV
*PosPart
= getPositivePart(SE
->getMinusSCEV(A
[K
].Coeff
, B
[K
].NegPart
));
2976 if (PosPart
->isZero())
2977 Bound
[K
].Upper
[Dependence::DVEntry::GT
] = A
[K
].Coeff
;
2983 const SCEV
*DependenceInfo::getPositivePart(const SCEV
*X
) const {
2984 return SE
->getSMaxExpr(X
, SE
->getZero(X
->getType()));
2989 const SCEV
*DependenceInfo::getNegativePart(const SCEV
*X
) const {
2990 return SE
->getSMinExpr(X
, SE
->getZero(X
->getType()));
2994 // Walks through the subscript,
2995 // collecting each coefficient, the associated loop bounds,
2996 // and recording its positive and negative parts for later use.
2997 DependenceInfo::CoefficientInfo
*
2998 DependenceInfo::collectCoeffInfo(const SCEV
*Subscript
, bool SrcFlag
,
2999 const SCEV
*&Constant
) const {
3000 const SCEV
*Zero
= SE
->getZero(Subscript
->getType());
3001 CoefficientInfo
*CI
= new CoefficientInfo
[MaxLevels
+ 1];
3002 for (unsigned K
= 1; K
<= MaxLevels
; ++K
) {
3004 CI
[K
].PosPart
= Zero
;
3005 CI
[K
].NegPart
= Zero
;
3006 CI
[K
].Iterations
= nullptr;
3008 while (const SCEVAddRecExpr
*AddRec
= dyn_cast
<SCEVAddRecExpr
>(Subscript
)) {
3009 const Loop
*L
= AddRec
->getLoop();
3010 unsigned K
= SrcFlag
? mapSrcLoop(L
) : mapDstLoop(L
);
3011 CI
[K
].Coeff
= AddRec
->getStepRecurrence(*SE
);
3012 CI
[K
].PosPart
= getPositivePart(CI
[K
].Coeff
);
3013 CI
[K
].NegPart
= getNegativePart(CI
[K
].Coeff
);
3014 CI
[K
].Iterations
= collectUpperBound(L
, Subscript
->getType());
3015 Subscript
= AddRec
->getStart();
3017 Constant
= Subscript
;
3019 LLVM_DEBUG(dbgs() << "\tCoefficient Info\n");
3020 for (unsigned K
= 1; K
<= MaxLevels
; ++K
) {
3021 LLVM_DEBUG(dbgs() << "\t " << K
<< "\t" << *CI
[K
].Coeff
);
3022 LLVM_DEBUG(dbgs() << "\tPos Part = ");
3023 LLVM_DEBUG(dbgs() << *CI
[K
].PosPart
);
3024 LLVM_DEBUG(dbgs() << "\tNeg Part = ");
3025 LLVM_DEBUG(dbgs() << *CI
[K
].NegPart
);
3026 LLVM_DEBUG(dbgs() << "\tUpper Bound = ");
3027 if (CI
[K
].Iterations
)
3028 LLVM_DEBUG(dbgs() << *CI
[K
].Iterations
);
3030 LLVM_DEBUG(dbgs() << "+inf");
3031 LLVM_DEBUG(dbgs() << '\n');
3033 LLVM_DEBUG(dbgs() << "\t Constant = " << *Subscript
<< '\n');
3039 // Looks through all the bounds info and
3040 // computes the lower bound given the current direction settings
3041 // at each level. If the lower bound for any level is -inf,
3042 // the result is -inf.
3043 const SCEV
*DependenceInfo::getLowerBound(BoundInfo
*Bound
) const {
3044 const SCEV
*Sum
= Bound
[1].Lower
[Bound
[1].Direction
];
3045 for (unsigned K
= 2; Sum
&& K
<= MaxLevels
; ++K
) {
3046 if (Bound
[K
].Lower
[Bound
[K
].Direction
])
3047 Sum
= SE
->getAddExpr(Sum
, Bound
[K
].Lower
[Bound
[K
].Direction
]);
3055 // Looks through all the bounds info and
3056 // computes the upper bound given the current direction settings
3057 // at each level. If the upper bound at any level is +inf,
3058 // the result is +inf.
3059 const SCEV
*DependenceInfo::getUpperBound(BoundInfo
*Bound
) const {
3060 const SCEV
*Sum
= Bound
[1].Upper
[Bound
[1].Direction
];
3061 for (unsigned K
= 2; Sum
&& K
<= MaxLevels
; ++K
) {
3062 if (Bound
[K
].Upper
[Bound
[K
].Direction
])
3063 Sum
= SE
->getAddExpr(Sum
, Bound
[K
].Upper
[Bound
[K
].Direction
]);
3071 //===----------------------------------------------------------------------===//
3072 // Constraint manipulation for Delta test.
3074 // Given a linear SCEV,
3075 // return the coefficient (the step)
3076 // corresponding to the specified loop.
3077 // If there isn't one, return 0.
3078 // For example, given a*i + b*j + c*k, finding the coefficient
3079 // corresponding to the j loop would yield b.
3080 const SCEV
*DependenceInfo::findCoefficient(const SCEV
*Expr
,
3081 const Loop
*TargetLoop
) const {
3082 const SCEVAddRecExpr
*AddRec
= dyn_cast
<SCEVAddRecExpr
>(Expr
);
3084 return SE
->getZero(Expr
->getType());
3085 if (AddRec
->getLoop() == TargetLoop
)
3086 return AddRec
->getStepRecurrence(*SE
);
3087 return findCoefficient(AddRec
->getStart(), TargetLoop
);
3091 // Given a linear SCEV,
3092 // return the SCEV given by zeroing out the coefficient
3093 // corresponding to the specified loop.
3094 // For example, given a*i + b*j + c*k, zeroing the coefficient
3095 // corresponding to the j loop would yield a*i + c*k.
3096 const SCEV
*DependenceInfo::zeroCoefficient(const SCEV
*Expr
,
3097 const Loop
*TargetLoop
) const {
3098 const SCEVAddRecExpr
*AddRec
= dyn_cast
<SCEVAddRecExpr
>(Expr
);
3100 return Expr
; // ignore
3101 if (AddRec
->getLoop() == TargetLoop
)
3102 return AddRec
->getStart();
3103 return SE
->getAddRecExpr(zeroCoefficient(AddRec
->getStart(), TargetLoop
),
3104 AddRec
->getStepRecurrence(*SE
),
3106 AddRec
->getNoWrapFlags());
3110 // Given a linear SCEV Expr,
3111 // return the SCEV given by adding some Value to the
3112 // coefficient corresponding to the specified TargetLoop.
3113 // For example, given a*i + b*j + c*k, adding 1 to the coefficient
3114 // corresponding to the j loop would yield a*i + (b+1)*j + c*k.
3115 const SCEV
*DependenceInfo::addToCoefficient(const SCEV
*Expr
,
3116 const Loop
*TargetLoop
,
3117 const SCEV
*Value
) const {
3118 const SCEVAddRecExpr
*AddRec
= dyn_cast
<SCEVAddRecExpr
>(Expr
);
3119 if (!AddRec
) // create a new addRec
3120 return SE
->getAddRecExpr(Expr
,
3123 SCEV::FlagAnyWrap
); // Worst case, with no info.
3124 if (AddRec
->getLoop() == TargetLoop
) {
3125 const SCEV
*Sum
= SE
->getAddExpr(AddRec
->getStepRecurrence(*SE
), Value
);
3127 return AddRec
->getStart();
3128 return SE
->getAddRecExpr(AddRec
->getStart(),
3131 AddRec
->getNoWrapFlags());
3133 if (SE
->isLoopInvariant(AddRec
, TargetLoop
))
3134 return SE
->getAddRecExpr(AddRec
, Value
, TargetLoop
, SCEV::FlagAnyWrap
);
3135 return SE
->getAddRecExpr(
3136 addToCoefficient(AddRec
->getStart(), TargetLoop
, Value
),
3137 AddRec
->getStepRecurrence(*SE
), AddRec
->getLoop(),
3138 AddRec
->getNoWrapFlags());
3142 // Review the constraints, looking for opportunities
3143 // to simplify a subscript pair (Src and Dst).
3144 // Return true if some simplification occurs.
3145 // If the simplification isn't exact (that is, if it is conservative
3146 // in terms of dependence), set consistent to false.
3147 // Corresponds to Figure 5 from the paper
3149 // Practical Dependence Testing
3150 // Goff, Kennedy, Tseng
3152 bool DependenceInfo::propagate(const SCEV
*&Src
, const SCEV
*&Dst
,
3153 SmallBitVector
&Loops
,
3154 SmallVectorImpl
<Constraint
> &Constraints
,
3156 bool Result
= false;
3157 for (unsigned LI
: Loops
.set_bits()) {
3158 LLVM_DEBUG(dbgs() << "\t Constraint[" << LI
<< "] is");
3159 LLVM_DEBUG(Constraints
[LI
].dump(dbgs()));
3160 if (Constraints
[LI
].isDistance())
3161 Result
|= propagateDistance(Src
, Dst
, Constraints
[LI
], Consistent
);
3162 else if (Constraints
[LI
].isLine())
3163 Result
|= propagateLine(Src
, Dst
, Constraints
[LI
], Consistent
);
3164 else if (Constraints
[LI
].isPoint())
3165 Result
|= propagatePoint(Src
, Dst
, Constraints
[LI
]);
3171 // Attempt to propagate a distance
3172 // constraint into a subscript pair (Src and Dst).
3173 // Return true if some simplification occurs.
3174 // If the simplification isn't exact (that is, if it is conservative
3175 // in terms of dependence), set consistent to false.
3176 bool DependenceInfo::propagateDistance(const SCEV
*&Src
, const SCEV
*&Dst
,
3177 Constraint
&CurConstraint
,
3179 const Loop
*CurLoop
= CurConstraint
.getAssociatedLoop();
3180 LLVM_DEBUG(dbgs() << "\t\tSrc is " << *Src
<< "\n");
3181 const SCEV
*A_K
= findCoefficient(Src
, CurLoop
);
3184 const SCEV
*DA_K
= SE
->getMulExpr(A_K
, CurConstraint
.getD());
3185 Src
= SE
->getMinusSCEV(Src
, DA_K
);
3186 Src
= zeroCoefficient(Src
, CurLoop
);
3187 LLVM_DEBUG(dbgs() << "\t\tnew Src is " << *Src
<< "\n");
3188 LLVM_DEBUG(dbgs() << "\t\tDst is " << *Dst
<< "\n");
3189 Dst
= addToCoefficient(Dst
, CurLoop
, SE
->getNegativeSCEV(A_K
));
3190 LLVM_DEBUG(dbgs() << "\t\tnew Dst is " << *Dst
<< "\n");
3191 if (!findCoefficient(Dst
, CurLoop
)->isZero())
3197 // Attempt to propagate a line
3198 // constraint into a subscript pair (Src and Dst).
3199 // Return true if some simplification occurs.
3200 // If the simplification isn't exact (that is, if it is conservative
3201 // in terms of dependence), set consistent to false.
3202 bool DependenceInfo::propagateLine(const SCEV
*&Src
, const SCEV
*&Dst
,
3203 Constraint
&CurConstraint
,
3205 const Loop
*CurLoop
= CurConstraint
.getAssociatedLoop();
3206 const SCEV
*A
= CurConstraint
.getA();
3207 const SCEV
*B
= CurConstraint
.getB();
3208 const SCEV
*C
= CurConstraint
.getC();
3209 LLVM_DEBUG(dbgs() << "\t\tA = " << *A
<< ", B = " << *B
<< ", C = " << *C
3211 LLVM_DEBUG(dbgs() << "\t\tSrc = " << *Src
<< "\n");
3212 LLVM_DEBUG(dbgs() << "\t\tDst = " << *Dst
<< "\n");
3214 const SCEVConstant
*Bconst
= dyn_cast
<SCEVConstant
>(B
);
3215 const SCEVConstant
*Cconst
= dyn_cast
<SCEVConstant
>(C
);
3216 if (!Bconst
|| !Cconst
) return false;
3217 APInt Beta
= Bconst
->getAPInt();
3218 APInt Charlie
= Cconst
->getAPInt();
3219 APInt CdivB
= Charlie
.sdiv(Beta
);
3220 assert(Charlie
.srem(Beta
) == 0 && "C should be evenly divisible by B");
3221 const SCEV
*AP_K
= findCoefficient(Dst
, CurLoop
);
3222 // Src = SE->getAddExpr(Src, SE->getMulExpr(AP_K, SE->getConstant(CdivB)));
3223 Src
= SE
->getMinusSCEV(Src
, SE
->getMulExpr(AP_K
, SE
->getConstant(CdivB
)));
3224 Dst
= zeroCoefficient(Dst
, CurLoop
);
3225 if (!findCoefficient(Src
, CurLoop
)->isZero())
3228 else if (B
->isZero()) {
3229 const SCEVConstant
*Aconst
= dyn_cast
<SCEVConstant
>(A
);
3230 const SCEVConstant
*Cconst
= dyn_cast
<SCEVConstant
>(C
);
3231 if (!Aconst
|| !Cconst
) return false;
3232 APInt Alpha
= Aconst
->getAPInt();
3233 APInt Charlie
= Cconst
->getAPInt();
3234 APInt CdivA
= Charlie
.sdiv(Alpha
);
3235 assert(Charlie
.srem(Alpha
) == 0 && "C should be evenly divisible by A");
3236 const SCEV
*A_K
= findCoefficient(Src
, CurLoop
);
3237 Src
= SE
->getAddExpr(Src
, SE
->getMulExpr(A_K
, SE
->getConstant(CdivA
)));
3238 Src
= zeroCoefficient(Src
, CurLoop
);
3239 if (!findCoefficient(Dst
, CurLoop
)->isZero())
3242 else if (isKnownPredicate(CmpInst::ICMP_EQ
, A
, B
)) {
3243 const SCEVConstant
*Aconst
= dyn_cast
<SCEVConstant
>(A
);
3244 const SCEVConstant
*Cconst
= dyn_cast
<SCEVConstant
>(C
);
3245 if (!Aconst
|| !Cconst
) return false;
3246 APInt Alpha
= Aconst
->getAPInt();
3247 APInt Charlie
= Cconst
->getAPInt();
3248 APInt CdivA
= Charlie
.sdiv(Alpha
);
3249 assert(Charlie
.srem(Alpha
) == 0 && "C should be evenly divisible by A");
3250 const SCEV
*A_K
= findCoefficient(Src
, CurLoop
);
3251 Src
= SE
->getAddExpr(Src
, SE
->getMulExpr(A_K
, SE
->getConstant(CdivA
)));
3252 Src
= zeroCoefficient(Src
, CurLoop
);
3253 Dst
= addToCoefficient(Dst
, CurLoop
, A_K
);
3254 if (!findCoefficient(Dst
, CurLoop
)->isZero())
3258 // paper is incorrect here, or perhaps just misleading
3259 const SCEV
*A_K
= findCoefficient(Src
, CurLoop
);
3260 Src
= SE
->getMulExpr(Src
, A
);
3261 Dst
= SE
->getMulExpr(Dst
, A
);
3262 Src
= SE
->getAddExpr(Src
, SE
->getMulExpr(A_K
, C
));
3263 Src
= zeroCoefficient(Src
, CurLoop
);
3264 Dst
= addToCoefficient(Dst
, CurLoop
, SE
->getMulExpr(A_K
, B
));
3265 if (!findCoefficient(Dst
, CurLoop
)->isZero())
3268 LLVM_DEBUG(dbgs() << "\t\tnew Src = " << *Src
<< "\n");
3269 LLVM_DEBUG(dbgs() << "\t\tnew Dst = " << *Dst
<< "\n");
3274 // Attempt to propagate a point
3275 // constraint into a subscript pair (Src and Dst).
3276 // Return true if some simplification occurs.
3277 bool DependenceInfo::propagatePoint(const SCEV
*&Src
, const SCEV
*&Dst
,
3278 Constraint
&CurConstraint
) {
3279 const Loop
*CurLoop
= CurConstraint
.getAssociatedLoop();
3280 const SCEV
*A_K
= findCoefficient(Src
, CurLoop
);
3281 const SCEV
*AP_K
= findCoefficient(Dst
, CurLoop
);
3282 const SCEV
*XA_K
= SE
->getMulExpr(A_K
, CurConstraint
.getX());
3283 const SCEV
*YAP_K
= SE
->getMulExpr(AP_K
, CurConstraint
.getY());
3284 LLVM_DEBUG(dbgs() << "\t\tSrc is " << *Src
<< "\n");
3285 Src
= SE
->getAddExpr(Src
, SE
->getMinusSCEV(XA_K
, YAP_K
));
3286 Src
= zeroCoefficient(Src
, CurLoop
);
3287 LLVM_DEBUG(dbgs() << "\t\tnew Src is " << *Src
<< "\n");
3288 LLVM_DEBUG(dbgs() << "\t\tDst is " << *Dst
<< "\n");
3289 Dst
= zeroCoefficient(Dst
, CurLoop
);
3290 LLVM_DEBUG(dbgs() << "\t\tnew Dst is " << *Dst
<< "\n");
3295 // Update direction vector entry based on the current constraint.
3296 void DependenceInfo::updateDirection(Dependence::DVEntry
&Level
,
3297 const Constraint
&CurConstraint
) const {
3298 LLVM_DEBUG(dbgs() << "\tUpdate direction, constraint =");
3299 LLVM_DEBUG(CurConstraint
.dump(dbgs()));
3300 if (CurConstraint
.isAny())
3302 else if (CurConstraint
.isDistance()) {
3303 // this one is consistent, the others aren't
3304 Level
.Scalar
= false;
3305 Level
.Distance
= CurConstraint
.getD();
3306 unsigned NewDirection
= Dependence::DVEntry::NONE
;
3307 if (!SE
->isKnownNonZero(Level
.Distance
)) // if may be zero
3308 NewDirection
= Dependence::DVEntry::EQ
;
3309 if (!SE
->isKnownNonPositive(Level
.Distance
)) // if may be positive
3310 NewDirection
|= Dependence::DVEntry::LT
;
3311 if (!SE
->isKnownNonNegative(Level
.Distance
)) // if may be negative
3312 NewDirection
|= Dependence::DVEntry::GT
;
3313 Level
.Direction
&= NewDirection
;
3315 else if (CurConstraint
.isLine()) {
3316 Level
.Scalar
= false;
3317 Level
.Distance
= nullptr;
3318 // direction should be accurate
3320 else if (CurConstraint
.isPoint()) {
3321 Level
.Scalar
= false;
3322 Level
.Distance
= nullptr;
3323 unsigned NewDirection
= Dependence::DVEntry::NONE
;
3324 if (!isKnownPredicate(CmpInst::ICMP_NE
,
3325 CurConstraint
.getY(),
3326 CurConstraint
.getX()))
3328 NewDirection
|= Dependence::DVEntry::EQ
;
3329 if (!isKnownPredicate(CmpInst::ICMP_SLE
,
3330 CurConstraint
.getY(),
3331 CurConstraint
.getX()))
3333 NewDirection
|= Dependence::DVEntry::LT
;
3334 if (!isKnownPredicate(CmpInst::ICMP_SGE
,
3335 CurConstraint
.getY(),
3336 CurConstraint
.getX()))
3338 NewDirection
|= Dependence::DVEntry::GT
;
3339 Level
.Direction
&= NewDirection
;
3342 llvm_unreachable("constraint has unexpected kind");
3345 /// Check if we can delinearize the subscripts. If the SCEVs representing the
3346 /// source and destination array references are recurrences on a nested loop,
3347 /// this function flattens the nested recurrences into separate recurrences
3348 /// for each loop level.
3349 bool DependenceInfo::tryDelinearize(Instruction
*Src
, Instruction
*Dst
,
3350 SmallVectorImpl
<Subscript
> &Pair
) {
3351 assert(isLoadOrStore(Src
) && "instruction is not load or store");
3352 assert(isLoadOrStore(Dst
) && "instruction is not load or store");
3353 Value
*SrcPtr
= getLoadStorePointerOperand(Src
);
3354 Value
*DstPtr
= getLoadStorePointerOperand(Dst
);
3355 Loop
*SrcLoop
= LI
->getLoopFor(Src
->getParent());
3356 Loop
*DstLoop
= LI
->getLoopFor(Dst
->getParent());
3357 const SCEV
*SrcAccessFn
= SE
->getSCEVAtScope(SrcPtr
, SrcLoop
);
3358 const SCEV
*DstAccessFn
= SE
->getSCEVAtScope(DstPtr
, DstLoop
);
3359 const SCEVUnknown
*SrcBase
=
3360 dyn_cast
<SCEVUnknown
>(SE
->getPointerBase(SrcAccessFn
));
3361 const SCEVUnknown
*DstBase
=
3362 dyn_cast
<SCEVUnknown
>(SE
->getPointerBase(DstAccessFn
));
3364 if (!SrcBase
|| !DstBase
|| SrcBase
!= DstBase
)
3367 SmallVector
<const SCEV
*, 4> SrcSubscripts
, DstSubscripts
;
3369 if (!tryDelinearizeFixedSize(Src
, Dst
, SrcAccessFn
, DstAccessFn
,
3370 SrcSubscripts
, DstSubscripts
) &&
3371 !tryDelinearizeParametricSize(Src
, Dst
, SrcAccessFn
, DstAccessFn
,
3372 SrcSubscripts
, DstSubscripts
))
3375 int Size
= SrcSubscripts
.size();
3377 dbgs() << "\nSrcSubscripts: ";
3378 for (int I
= 0; I
< Size
; I
++)
3379 dbgs() << *SrcSubscripts
[I
];
3380 dbgs() << "\nDstSubscripts: ";
3381 for (int I
= 0; I
< Size
; I
++)
3382 dbgs() << *DstSubscripts
[I
];
3385 // The delinearization transforms a single-subscript MIV dependence test into
3386 // a multi-subscript SIV dependence test that is easier to compute. So we
3387 // resize Pair to contain as many pairs of subscripts as the delinearization
3388 // has found, and then initialize the pairs following the delinearization.
3390 for (int I
= 0; I
< Size
; ++I
) {
3391 Pair
[I
].Src
= SrcSubscripts
[I
];
3392 Pair
[I
].Dst
= DstSubscripts
[I
];
3393 unifySubscriptType(&Pair
[I
]);
3399 /// Try to delinearize \p SrcAccessFn and \p DstAccessFn if the underlying
3400 /// arrays accessed are fixed-size arrays. Return true if delinearization was
3402 bool DependenceInfo::tryDelinearizeFixedSize(
3403 Instruction
*Src
, Instruction
*Dst
, const SCEV
*SrcAccessFn
,
3404 const SCEV
*DstAccessFn
, SmallVectorImpl
<const SCEV
*> &SrcSubscripts
,
3405 SmallVectorImpl
<const SCEV
*> &DstSubscripts
) {
3407 const SCEVUnknown
*SrcBase
=
3408 dyn_cast
<SCEVUnknown
>(SE
->getPointerBase(SrcAccessFn
));
3409 const SCEVUnknown
*DstBase
=
3410 dyn_cast
<SCEVUnknown
>(SE
->getPointerBase(DstAccessFn
));
3411 assert(SrcBase
&& DstBase
&& SrcBase
== DstBase
&&
3412 "expected src and dst scev unknowns to be equal");
3415 SmallVector
<int, 4> SrcSizes
;
3416 SmallVector
<int, 4> DstSizes
;
3417 if (!tryDelinearizeFixedSizeImpl(SE
, Src
, SrcAccessFn
, SrcSubscripts
,
3419 !tryDelinearizeFixedSizeImpl(SE
, Dst
, DstAccessFn
, DstSubscripts
,
3423 // Check that the two size arrays are non-empty and equal in length and
3425 if (SrcSizes
.size() != DstSizes
.size() ||
3426 !std::equal(SrcSizes
.begin(), SrcSizes
.end(), DstSizes
.begin())) {
3427 SrcSubscripts
.clear();
3428 DstSubscripts
.clear();
3432 assert(SrcSubscripts
.size() == DstSubscripts
.size() &&
3433 "Expected equal number of entries in the list of SrcSubscripts and "
3436 Value
*SrcPtr
= getLoadStorePointerOperand(Src
);
3437 Value
*DstPtr
= getLoadStorePointerOperand(Dst
);
3439 // In general we cannot safely assume that the subscripts recovered from GEPs
3440 // are in the range of values defined for their corresponding array
3441 // dimensions. For example some C language usage/interpretation make it
3442 // impossible to verify this at compile-time. As such we can only delinearize
3443 // iff the subscripts are positive and are less than the range of the
3445 if (!DisableDelinearizationChecks
) {
3446 auto AllIndicesInRange
= [&](SmallVector
<int, 4> &DimensionSizes
,
3447 SmallVectorImpl
<const SCEV
*> &Subscripts
,
3449 size_t SSize
= Subscripts
.size();
3450 for (size_t I
= 1; I
< SSize
; ++I
) {
3451 const SCEV
*S
= Subscripts
[I
];
3452 if (!isKnownNonNegative(S
, Ptr
))
3454 if (auto *SType
= dyn_cast
<IntegerType
>(S
->getType())) {
3455 const SCEV
*Range
= SE
->getConstant(
3456 ConstantInt::get(SType
, DimensionSizes
[I
- 1], false));
3457 if (!isKnownLessThan(S
, Range
))
3464 if (!AllIndicesInRange(SrcSizes
, SrcSubscripts
, SrcPtr
) ||
3465 !AllIndicesInRange(DstSizes
, DstSubscripts
, DstPtr
)) {
3466 SrcSubscripts
.clear();
3467 DstSubscripts
.clear();
3472 dbgs() << "Delinearized subscripts of fixed-size array\n"
3473 << "SrcGEP:" << *SrcPtr
<< "\n"
3474 << "DstGEP:" << *DstPtr
<< "\n";
3479 bool DependenceInfo::tryDelinearizeParametricSize(
3480 Instruction
*Src
, Instruction
*Dst
, const SCEV
*SrcAccessFn
,
3481 const SCEV
*DstAccessFn
, SmallVectorImpl
<const SCEV
*> &SrcSubscripts
,
3482 SmallVectorImpl
<const SCEV
*> &DstSubscripts
) {
3484 Value
*SrcPtr
= getLoadStorePointerOperand(Src
);
3485 Value
*DstPtr
= getLoadStorePointerOperand(Dst
);
3486 const SCEVUnknown
*SrcBase
=
3487 dyn_cast
<SCEVUnknown
>(SE
->getPointerBase(SrcAccessFn
));
3488 const SCEVUnknown
*DstBase
=
3489 dyn_cast
<SCEVUnknown
>(SE
->getPointerBase(DstAccessFn
));
3490 assert(SrcBase
&& DstBase
&& SrcBase
== DstBase
&&
3491 "expected src and dst scev unknowns to be equal");
3493 const SCEV
*ElementSize
= SE
->getElementSize(Src
);
3494 if (ElementSize
!= SE
->getElementSize(Dst
))
3497 const SCEV
*SrcSCEV
= SE
->getMinusSCEV(SrcAccessFn
, SrcBase
);
3498 const SCEV
*DstSCEV
= SE
->getMinusSCEV(DstAccessFn
, DstBase
);
3500 const SCEVAddRecExpr
*SrcAR
= dyn_cast
<SCEVAddRecExpr
>(SrcSCEV
);
3501 const SCEVAddRecExpr
*DstAR
= dyn_cast
<SCEVAddRecExpr
>(DstSCEV
);
3502 if (!SrcAR
|| !DstAR
|| !SrcAR
->isAffine() || !DstAR
->isAffine())
3505 // First step: collect parametric terms in both array references.
3506 SmallVector
<const SCEV
*, 4> Terms
;
3507 collectParametricTerms(*SE
, SrcAR
, Terms
);
3508 collectParametricTerms(*SE
, DstAR
, Terms
);
3510 // Second step: find subscript sizes.
3511 SmallVector
<const SCEV
*, 4> Sizes
;
3512 findArrayDimensions(*SE
, Terms
, Sizes
, ElementSize
);
3514 // Third step: compute the access functions for each subscript.
3515 computeAccessFunctions(*SE
, SrcAR
, SrcSubscripts
, Sizes
);
3516 computeAccessFunctions(*SE
, DstAR
, DstSubscripts
, Sizes
);
3518 // Fail when there is only a subscript: that's a linearized access function.
3519 if (SrcSubscripts
.size() < 2 || DstSubscripts
.size() < 2 ||
3520 SrcSubscripts
.size() != DstSubscripts
.size())
3523 size_t Size
= SrcSubscripts
.size();
3525 // Statically check that the array bounds are in-range. The first subscript we
3526 // don't have a size for and it cannot overflow into another subscript, so is
3527 // always safe. The others need to be 0 <= subscript[i] < bound, for both src
3529 // FIXME: It may be better to record these sizes and add them as constraints
3530 // to the dependency checks.
3531 if (!DisableDelinearizationChecks
)
3532 for (size_t I
= 1; I
< Size
; ++I
) {
3533 if (!isKnownNonNegative(SrcSubscripts
[I
], SrcPtr
))
3536 if (!isKnownLessThan(SrcSubscripts
[I
], Sizes
[I
- 1]))
3539 if (!isKnownNonNegative(DstSubscripts
[I
], DstPtr
))
3542 if (!isKnownLessThan(DstSubscripts
[I
], Sizes
[I
- 1]))
3549 //===----------------------------------------------------------------------===//
3552 // For debugging purposes, dump a small bit vector to dbgs().
3553 static void dumpSmallBitVector(SmallBitVector
&BV
) {
3555 for (unsigned VI
: BV
.set_bits()) {
3557 if (BV
.find_next(VI
) >= 0)
3564 bool DependenceInfo::invalidate(Function
&F
, const PreservedAnalyses
&PA
,
3565 FunctionAnalysisManager::Invalidator
&Inv
) {
3566 // Check if the analysis itself has been invalidated.
3567 auto PAC
= PA
.getChecker
<DependenceAnalysis
>();
3568 if (!PAC
.preserved() && !PAC
.preservedSet
<AllAnalysesOn
<Function
>>())
3571 // Check transitive dependencies.
3572 return Inv
.invalidate
<AAManager
>(F
, PA
) ||
3573 Inv
.invalidate
<ScalarEvolutionAnalysis
>(F
, PA
) ||
3574 Inv
.invalidate
<LoopAnalysis
>(F
, PA
);
3578 // Returns NULL if there is no dependence.
3579 // Otherwise, return a Dependence with as many details as possible.
3580 // Corresponds to Section 3.1 in the paper
3582 // Practical Dependence Testing
3583 // Goff, Kennedy, Tseng
3586 // Care is required to keep the routine below, getSplitIteration(),
3587 // up to date with respect to this routine.
3588 std::unique_ptr
<Dependence
>
3589 DependenceInfo::depends(Instruction
*Src
, Instruction
*Dst
,
3590 bool PossiblyLoopIndependent
) {
3592 PossiblyLoopIndependent
= false;
3594 if (!(Src
->mayReadOrWriteMemory() && Dst
->mayReadOrWriteMemory()))
3595 // if both instructions don't reference memory, there's no dependence
3598 if (!isLoadOrStore(Src
) || !isLoadOrStore(Dst
)) {
3599 // can only analyze simple loads and stores, i.e., no calls, invokes, etc.
3600 LLVM_DEBUG(dbgs() << "can only handle simple loads and stores\n");
3601 return std::make_unique
<Dependence
>(Src
, Dst
);
3604 assert(isLoadOrStore(Src
) && "instruction is not load or store");
3605 assert(isLoadOrStore(Dst
) && "instruction is not load or store");
3606 Value
*SrcPtr
= getLoadStorePointerOperand(Src
);
3607 Value
*DstPtr
= getLoadStorePointerOperand(Dst
);
3609 switch (underlyingObjectsAlias(AA
, F
->getDataLayout(),
3610 MemoryLocation::get(Dst
),
3611 MemoryLocation::get(Src
))) {
3612 case AliasResult::MayAlias
:
3613 case AliasResult::PartialAlias
:
3614 // cannot analyse objects if we don't understand their aliasing.
3615 LLVM_DEBUG(dbgs() << "can't analyze may or partial alias\n");
3616 return std::make_unique
<Dependence
>(Src
, Dst
);
3617 case AliasResult::NoAlias
:
3618 // If the objects noalias, they are distinct, accesses are independent.
3619 LLVM_DEBUG(dbgs() << "no alias\n");
3621 case AliasResult::MustAlias
:
3622 break; // The underlying objects alias; test accesses for dependence.
3625 // establish loop nesting levels
3626 establishNestingLevels(Src
, Dst
);
3627 LLVM_DEBUG(dbgs() << " common nesting levels = " << CommonLevels
<< "\n");
3628 LLVM_DEBUG(dbgs() << " maximum nesting levels = " << MaxLevels
<< "\n");
3630 FullDependence
Result(Src
, Dst
, PossiblyLoopIndependent
, CommonLevels
);
3634 SmallVector
<Subscript
, 2> Pair(Pairs
);
3635 const SCEV
*SrcSCEV
= SE
->getSCEV(SrcPtr
);
3636 const SCEV
*DstSCEV
= SE
->getSCEV(DstPtr
);
3637 LLVM_DEBUG(dbgs() << " SrcSCEV = " << *SrcSCEV
<< "\n");
3638 LLVM_DEBUG(dbgs() << " DstSCEV = " << *DstSCEV
<< "\n");
3639 if (SE
->getPointerBase(SrcSCEV
) != SE
->getPointerBase(DstSCEV
)) {
3640 // If two pointers have different bases, trying to analyze indexes won't
3641 // work; we can't compare them to each other. This can happen, for example,
3642 // if one is produced by an LCSSA PHI node.
3644 // We check this upfront so we don't crash in cases where getMinusSCEV()
3645 // returns a SCEVCouldNotCompute.
3646 LLVM_DEBUG(dbgs() << "can't analyze SCEV with different pointer base\n");
3647 return std::make_unique
<Dependence
>(Src
, Dst
);
3649 Pair
[0].Src
= SrcSCEV
;
3650 Pair
[0].Dst
= DstSCEV
;
3653 if (tryDelinearize(Src
, Dst
, Pair
)) {
3654 LLVM_DEBUG(dbgs() << " delinearized\n");
3655 Pairs
= Pair
.size();
3659 for (unsigned P
= 0; P
< Pairs
; ++P
) {
3660 Pair
[P
].Loops
.resize(MaxLevels
+ 1);
3661 Pair
[P
].GroupLoops
.resize(MaxLevels
+ 1);
3662 Pair
[P
].Group
.resize(Pairs
);
3663 removeMatchingExtensions(&Pair
[P
]);
3664 Pair
[P
].Classification
=
3665 classifyPair(Pair
[P
].Src
, LI
->getLoopFor(Src
->getParent()),
3666 Pair
[P
].Dst
, LI
->getLoopFor(Dst
->getParent()),
3668 Pair
[P
].GroupLoops
= Pair
[P
].Loops
;
3669 Pair
[P
].Group
.set(P
);
3670 LLVM_DEBUG(dbgs() << " subscript " << P
<< "\n");
3671 LLVM_DEBUG(dbgs() << "\tsrc = " << *Pair
[P
].Src
<< "\n");
3672 LLVM_DEBUG(dbgs() << "\tdst = " << *Pair
[P
].Dst
<< "\n");
3673 LLVM_DEBUG(dbgs() << "\tclass = " << Pair
[P
].Classification
<< "\n");
3674 LLVM_DEBUG(dbgs() << "\tloops = ");
3675 LLVM_DEBUG(dumpSmallBitVector(Pair
[P
].Loops
));
3678 SmallBitVector
Separable(Pairs
);
3679 SmallBitVector
Coupled(Pairs
);
3681 // Partition subscripts into separable and minimally-coupled groups
3682 // Algorithm in paper is algorithmically better;
3683 // this may be faster in practice. Check someday.
3685 // Here's an example of how it works. Consider this code:
3692 // A[i][j][k][m] = ...;
3693 // ... = A[0][j][l][i + j];
3700 // There are 4 subscripts here:
3704 // 3 [m] and [i + j]
3706 // We've already classified each subscript pair as ZIV, SIV, etc.,
3707 // and collected all the loops mentioned by pair P in Pair[P].Loops.
3708 // In addition, we've initialized Pair[P].GroupLoops to Pair[P].Loops
3709 // and set Pair[P].Group = {P}.
3711 // Src Dst Classification Loops GroupLoops Group
3712 // 0 [i] [0] SIV {1} {1} {0}
3713 // 1 [j] [j] SIV {2} {2} {1}
3714 // 2 [k] [l] RDIV {3,4} {3,4} {2}
3715 // 3 [m] [i + j] MIV {1,2,5} {1,2,5} {3}
3717 // For each subscript SI 0 .. 3, we consider each remaining subscript, SJ.
3718 // So, 0 is compared against 1, 2, and 3; 1 is compared against 2 and 3, etc.
3720 // We begin by comparing 0 and 1. The intersection of the GroupLoops is empty.
3721 // Next, 0 and 2. Again, the intersection of their GroupLoops is empty.
3722 // Next 0 and 3. The intersection of their GroupLoop = {1}, not empty,
3723 // so Pair[3].Group = {0,3} and Done = false (that is, 0 will not be added
3724 // to either Separable or Coupled).
3726 // Next, we consider 1 and 2. The intersection of the GroupLoops is empty.
3727 // Next, 1 and 3. The intersection of their GroupLoops = {2}, not empty,
3728 // so Pair[3].Group = {0, 1, 3} and Done = false.
3730 // Next, we compare 2 against 3. The intersection of the GroupLoops is empty.
3731 // Since Done remains true, we add 2 to the set of Separable pairs.
3733 // Finally, we consider 3. There's nothing to compare it with,
3734 // so Done remains true and we add it to the Coupled set.
3735 // Pair[3].Group = {0, 1, 3} and GroupLoops = {1, 2, 5}.
3737 // In the end, we've got 1 separable subscript and 1 coupled group.
3738 for (unsigned SI
= 0; SI
< Pairs
; ++SI
) {
3739 if (Pair
[SI
].Classification
== Subscript::NonLinear
) {
3740 // ignore these, but collect loops for later
3741 ++NonlinearSubscriptPairs
;
3742 collectCommonLoops(Pair
[SI
].Src
,
3743 LI
->getLoopFor(Src
->getParent()),
3745 collectCommonLoops(Pair
[SI
].Dst
,
3746 LI
->getLoopFor(Dst
->getParent()),
3748 Result
.Consistent
= false;
3749 } else if (Pair
[SI
].Classification
== Subscript::ZIV
) {
3754 // SIV, RDIV, or MIV, so check for coupled group
3756 for (unsigned SJ
= SI
+ 1; SJ
< Pairs
; ++SJ
) {
3757 SmallBitVector Intersection
= Pair
[SI
].GroupLoops
;
3758 Intersection
&= Pair
[SJ
].GroupLoops
;
3759 if (Intersection
.any()) {
3760 // accumulate set of all the loops in group
3761 Pair
[SJ
].GroupLoops
|= Pair
[SI
].GroupLoops
;
3762 // accumulate set of all subscripts in group
3763 Pair
[SJ
].Group
|= Pair
[SI
].Group
;
3768 if (Pair
[SI
].Group
.count() == 1) {
3770 ++SeparableSubscriptPairs
;
3774 ++CoupledSubscriptPairs
;
3780 LLVM_DEBUG(dbgs() << " Separable = ");
3781 LLVM_DEBUG(dumpSmallBitVector(Separable
));
3782 LLVM_DEBUG(dbgs() << " Coupled = ");
3783 LLVM_DEBUG(dumpSmallBitVector(Coupled
));
3785 Constraint NewConstraint
;
3786 NewConstraint
.setAny(SE
);
3788 // test separable subscripts
3789 for (unsigned SI
: Separable
.set_bits()) {
3790 LLVM_DEBUG(dbgs() << "testing subscript " << SI
);
3791 switch (Pair
[SI
].Classification
) {
3792 case Subscript::ZIV
:
3793 LLVM_DEBUG(dbgs() << ", ZIV\n");
3794 if (testZIV(Pair
[SI
].Src
, Pair
[SI
].Dst
, Result
))
3797 case Subscript::SIV
: {
3798 LLVM_DEBUG(dbgs() << ", SIV\n");
3800 const SCEV
*SplitIter
= nullptr;
3801 if (testSIV(Pair
[SI
].Src
, Pair
[SI
].Dst
, Level
, Result
, NewConstraint
,
3806 case Subscript::RDIV
:
3807 LLVM_DEBUG(dbgs() << ", RDIV\n");
3808 if (testRDIV(Pair
[SI
].Src
, Pair
[SI
].Dst
, Result
))
3811 case Subscript::MIV
:
3812 LLVM_DEBUG(dbgs() << ", MIV\n");
3813 if (testMIV(Pair
[SI
].Src
, Pair
[SI
].Dst
, Pair
[SI
].Loops
, Result
))
3817 llvm_unreachable("subscript has unexpected classification");
3821 if (Coupled
.count()) {
3822 // test coupled subscript groups
3823 LLVM_DEBUG(dbgs() << "starting on coupled subscripts\n");
3824 LLVM_DEBUG(dbgs() << "MaxLevels + 1 = " << MaxLevels
+ 1 << "\n");
3825 SmallVector
<Constraint
, 4> Constraints(MaxLevels
+ 1);
3826 for (unsigned II
= 0; II
<= MaxLevels
; ++II
)
3827 Constraints
[II
].setAny(SE
);
3828 for (unsigned SI
: Coupled
.set_bits()) {
3829 LLVM_DEBUG(dbgs() << "testing subscript group " << SI
<< " { ");
3830 SmallBitVector
Group(Pair
[SI
].Group
);
3831 SmallBitVector
Sivs(Pairs
);
3832 SmallBitVector
Mivs(Pairs
);
3833 SmallBitVector
ConstrainedLevels(MaxLevels
+ 1);
3834 SmallVector
<Subscript
*, 4> PairsInGroup
;
3835 for (unsigned SJ
: Group
.set_bits()) {
3836 LLVM_DEBUG(dbgs() << SJ
<< " ");
3837 if (Pair
[SJ
].Classification
== Subscript::SIV
)
3841 PairsInGroup
.push_back(&Pair
[SJ
]);
3843 unifySubscriptType(PairsInGroup
);
3844 LLVM_DEBUG(dbgs() << "}\n");
3845 while (Sivs
.any()) {
3846 bool Changed
= false;
3847 for (unsigned SJ
: Sivs
.set_bits()) {
3848 LLVM_DEBUG(dbgs() << "testing subscript " << SJ
<< ", SIV\n");
3849 // SJ is an SIV subscript that's part of the current coupled group
3851 const SCEV
*SplitIter
= nullptr;
3852 LLVM_DEBUG(dbgs() << "SIV\n");
3853 if (testSIV(Pair
[SJ
].Src
, Pair
[SJ
].Dst
, Level
, Result
, NewConstraint
,
3856 ConstrainedLevels
.set(Level
);
3857 if (intersectConstraints(&Constraints
[Level
], &NewConstraint
)) {
3858 if (Constraints
[Level
].isEmpty()) {
3859 ++DeltaIndependence
;
3867 // propagate, possibly creating new SIVs and ZIVs
3868 LLVM_DEBUG(dbgs() << " propagating\n");
3869 LLVM_DEBUG(dbgs() << "\tMivs = ");
3870 LLVM_DEBUG(dumpSmallBitVector(Mivs
));
3871 for (unsigned SJ
: Mivs
.set_bits()) {
3872 // SJ is an MIV subscript that's part of the current coupled group
3873 LLVM_DEBUG(dbgs() << "\tSJ = " << SJ
<< "\n");
3874 if (propagate(Pair
[SJ
].Src
, Pair
[SJ
].Dst
, Pair
[SJ
].Loops
,
3875 Constraints
, Result
.Consistent
)) {
3876 LLVM_DEBUG(dbgs() << "\t Changed\n");
3877 ++DeltaPropagations
;
3878 Pair
[SJ
].Classification
=
3879 classifyPair(Pair
[SJ
].Src
, LI
->getLoopFor(Src
->getParent()),
3880 Pair
[SJ
].Dst
, LI
->getLoopFor(Dst
->getParent()),
3882 switch (Pair
[SJ
].Classification
) {
3883 case Subscript::ZIV
:
3884 LLVM_DEBUG(dbgs() << "ZIV\n");
3885 if (testZIV(Pair
[SJ
].Src
, Pair
[SJ
].Dst
, Result
))
3889 case Subscript::SIV
:
3893 case Subscript::RDIV
:
3894 case Subscript::MIV
:
3897 llvm_unreachable("bad subscript classification");
3904 // test & propagate remaining RDIVs
3905 for (unsigned SJ
: Mivs
.set_bits()) {
3906 if (Pair
[SJ
].Classification
== Subscript::RDIV
) {
3907 LLVM_DEBUG(dbgs() << "RDIV test\n");
3908 if (testRDIV(Pair
[SJ
].Src
, Pair
[SJ
].Dst
, Result
))
3910 // I don't yet understand how to propagate RDIV results
3915 // test remaining MIVs
3916 // This code is temporary.
3917 // Better to somehow test all remaining subscripts simultaneously.
3918 for (unsigned SJ
: Mivs
.set_bits()) {
3919 if (Pair
[SJ
].Classification
== Subscript::MIV
) {
3920 LLVM_DEBUG(dbgs() << "MIV test\n");
3921 if (testMIV(Pair
[SJ
].Src
, Pair
[SJ
].Dst
, Pair
[SJ
].Loops
, Result
))
3925 llvm_unreachable("expected only MIV subscripts at this point");
3928 // update Result.DV from constraint vector
3929 LLVM_DEBUG(dbgs() << " updating\n");
3930 for (unsigned SJ
: ConstrainedLevels
.set_bits()) {
3931 if (SJ
> CommonLevels
)
3933 updateDirection(Result
.DV
[SJ
- 1], Constraints
[SJ
]);
3934 if (Result
.DV
[SJ
- 1].Direction
== Dependence::DVEntry::NONE
)
3940 // Make sure the Scalar flags are set correctly.
3941 SmallBitVector
CompleteLoops(MaxLevels
+ 1);
3942 for (unsigned SI
= 0; SI
< Pairs
; ++SI
)
3943 CompleteLoops
|= Pair
[SI
].Loops
;
3944 for (unsigned II
= 1; II
<= CommonLevels
; ++II
)
3945 if (CompleteLoops
[II
])
3946 Result
.DV
[II
- 1].Scalar
= false;
3948 if (PossiblyLoopIndependent
) {
3949 // Make sure the LoopIndependent flag is set correctly.
3950 // All directions must include equal, otherwise no
3951 // loop-independent dependence is possible.
3952 for (unsigned II
= 1; II
<= CommonLevels
; ++II
) {
3953 if (!(Result
.getDirection(II
) & Dependence::DVEntry::EQ
)) {
3954 Result
.LoopIndependent
= false;
3960 // On the other hand, if all directions are equal and there's no
3961 // loop-independent dependence possible, then no dependence exists.
3962 bool AllEqual
= true;
3963 for (unsigned II
= 1; II
<= CommonLevels
; ++II
) {
3964 if (Result
.getDirection(II
) != Dependence::DVEntry::EQ
) {
3973 return std::make_unique
<FullDependence
>(std::move(Result
));
3976 //===----------------------------------------------------------------------===//
3977 // getSplitIteration -
3978 // Rather than spend rarely-used space recording the splitting iteration
3979 // during the Weak-Crossing SIV test, we re-compute it on demand.
3980 // The re-computation is basically a repeat of the entire dependence test,
3981 // though simplified since we know that the dependence exists.
3982 // It's tedious, since we must go through all propagations, etc.
3984 // Care is required to keep this code up to date with respect to the routine
3985 // above, depends().
3987 // Generally, the dependence analyzer will be used to build
3988 // a dependence graph for a function (basically a map from instructions
3989 // to dependences). Looking for cycles in the graph shows us loops
3990 // that cannot be trivially vectorized/parallelized.
3992 // We can try to improve the situation by examining all the dependences
3993 // that make up the cycle, looking for ones we can break.
3994 // Sometimes, peeling the first or last iteration of a loop will break
3995 // dependences, and we've got flags for those possibilities.
3996 // Sometimes, splitting a loop at some other iteration will do the trick,
3997 // and we've got a flag for that case. Rather than waste the space to
3998 // record the exact iteration (since we rarely know), we provide
3999 // a method that calculates the iteration. It's a drag that it must work
4000 // from scratch, but wonderful in that it's possible.
4002 // Here's an example:
4004 // for (i = 0; i < 10; i++)
4008 // There's a loop-carried flow dependence from the store to the load,
4009 // found by the weak-crossing SIV test. The dependence will have a flag,
4010 // indicating that the dependence can be broken by splitting the loop.
4011 // Calling getSplitIteration will return 5.
4012 // Splitting the loop breaks the dependence, like so:
4014 // for (i = 0; i <= 5; i++)
4017 // for (i = 6; i < 10; i++)
4021 // breaks the dependence and allows us to vectorize/parallelize
4023 const SCEV
*DependenceInfo::getSplitIteration(const Dependence
&Dep
,
4024 unsigned SplitLevel
) {
4025 assert(Dep
.isSplitable(SplitLevel
) &&
4026 "Dep should be splitable at SplitLevel");
4027 Instruction
*Src
= Dep
.getSrc();
4028 Instruction
*Dst
= Dep
.getDst();
4029 assert(Src
->mayReadFromMemory() || Src
->mayWriteToMemory());
4030 assert(Dst
->mayReadFromMemory() || Dst
->mayWriteToMemory());
4031 assert(isLoadOrStore(Src
));
4032 assert(isLoadOrStore(Dst
));
4033 Value
*SrcPtr
= getLoadStorePointerOperand(Src
);
4034 Value
*DstPtr
= getLoadStorePointerOperand(Dst
);
4035 assert(underlyingObjectsAlias(
4036 AA
, F
->getDataLayout(), MemoryLocation::get(Dst
),
4037 MemoryLocation::get(Src
)) == AliasResult::MustAlias
);
4039 // establish loop nesting levels
4040 establishNestingLevels(Src
, Dst
);
4042 FullDependence
Result(Src
, Dst
, false, CommonLevels
);
4045 SmallVector
<Subscript
, 2> Pair(Pairs
);
4046 const SCEV
*SrcSCEV
= SE
->getSCEV(SrcPtr
);
4047 const SCEV
*DstSCEV
= SE
->getSCEV(DstPtr
);
4048 Pair
[0].Src
= SrcSCEV
;
4049 Pair
[0].Dst
= DstSCEV
;
4052 if (tryDelinearize(Src
, Dst
, Pair
)) {
4053 LLVM_DEBUG(dbgs() << " delinearized\n");
4054 Pairs
= Pair
.size();
4058 for (unsigned P
= 0; P
< Pairs
; ++P
) {
4059 Pair
[P
].Loops
.resize(MaxLevels
+ 1);
4060 Pair
[P
].GroupLoops
.resize(MaxLevels
+ 1);
4061 Pair
[P
].Group
.resize(Pairs
);
4062 removeMatchingExtensions(&Pair
[P
]);
4063 Pair
[P
].Classification
=
4064 classifyPair(Pair
[P
].Src
, LI
->getLoopFor(Src
->getParent()),
4065 Pair
[P
].Dst
, LI
->getLoopFor(Dst
->getParent()),
4067 Pair
[P
].GroupLoops
= Pair
[P
].Loops
;
4068 Pair
[P
].Group
.set(P
);
4071 SmallBitVector
Separable(Pairs
);
4072 SmallBitVector
Coupled(Pairs
);
4074 // partition subscripts into separable and minimally-coupled groups
4075 for (unsigned SI
= 0; SI
< Pairs
; ++SI
) {
4076 if (Pair
[SI
].Classification
== Subscript::NonLinear
) {
4077 // ignore these, but collect loops for later
4078 collectCommonLoops(Pair
[SI
].Src
,
4079 LI
->getLoopFor(Src
->getParent()),
4081 collectCommonLoops(Pair
[SI
].Dst
,
4082 LI
->getLoopFor(Dst
->getParent()),
4084 Result
.Consistent
= false;
4086 else if (Pair
[SI
].Classification
== Subscript::ZIV
)
4089 // SIV, RDIV, or MIV, so check for coupled group
4091 for (unsigned SJ
= SI
+ 1; SJ
< Pairs
; ++SJ
) {
4092 SmallBitVector Intersection
= Pair
[SI
].GroupLoops
;
4093 Intersection
&= Pair
[SJ
].GroupLoops
;
4094 if (Intersection
.any()) {
4095 // accumulate set of all the loops in group
4096 Pair
[SJ
].GroupLoops
|= Pair
[SI
].GroupLoops
;
4097 // accumulate set of all subscripts in group
4098 Pair
[SJ
].Group
|= Pair
[SI
].Group
;
4103 if (Pair
[SI
].Group
.count() == 1)
4111 Constraint NewConstraint
;
4112 NewConstraint
.setAny(SE
);
4114 // test separable subscripts
4115 for (unsigned SI
: Separable
.set_bits()) {
4116 switch (Pair
[SI
].Classification
) {
4117 case Subscript::SIV
: {
4119 const SCEV
*SplitIter
= nullptr;
4120 (void) testSIV(Pair
[SI
].Src
, Pair
[SI
].Dst
, Level
,
4121 Result
, NewConstraint
, SplitIter
);
4122 if (Level
== SplitLevel
) {
4123 assert(SplitIter
!= nullptr);
4128 case Subscript::ZIV
:
4129 case Subscript::RDIV
:
4130 case Subscript::MIV
:
4133 llvm_unreachable("subscript has unexpected classification");
4137 if (Coupled
.count()) {
4138 // test coupled subscript groups
4139 SmallVector
<Constraint
, 4> Constraints(MaxLevels
+ 1);
4140 for (unsigned II
= 0; II
<= MaxLevels
; ++II
)
4141 Constraints
[II
].setAny(SE
);
4142 for (unsigned SI
: Coupled
.set_bits()) {
4143 SmallBitVector
Group(Pair
[SI
].Group
);
4144 SmallBitVector
Sivs(Pairs
);
4145 SmallBitVector
Mivs(Pairs
);
4146 SmallBitVector
ConstrainedLevels(MaxLevels
+ 1);
4147 for (unsigned SJ
: Group
.set_bits()) {
4148 if (Pair
[SJ
].Classification
== Subscript::SIV
)
4153 while (Sivs
.any()) {
4154 bool Changed
= false;
4155 for (unsigned SJ
: Sivs
.set_bits()) {
4156 // SJ is an SIV subscript that's part of the current coupled group
4158 const SCEV
*SplitIter
= nullptr;
4159 (void) testSIV(Pair
[SJ
].Src
, Pair
[SJ
].Dst
, Level
,
4160 Result
, NewConstraint
, SplitIter
);
4161 if (Level
== SplitLevel
&& SplitIter
)
4163 ConstrainedLevels
.set(Level
);
4164 if (intersectConstraints(&Constraints
[Level
], &NewConstraint
))
4169 // propagate, possibly creating new SIVs and ZIVs
4170 for (unsigned SJ
: Mivs
.set_bits()) {
4171 // SJ is an MIV subscript that's part of the current coupled group
4172 if (propagate(Pair
[SJ
].Src
, Pair
[SJ
].Dst
,
4173 Pair
[SJ
].Loops
, Constraints
, Result
.Consistent
)) {
4174 Pair
[SJ
].Classification
=
4175 classifyPair(Pair
[SJ
].Src
, LI
->getLoopFor(Src
->getParent()),
4176 Pair
[SJ
].Dst
, LI
->getLoopFor(Dst
->getParent()),
4178 switch (Pair
[SJ
].Classification
) {
4179 case Subscript::ZIV
:
4182 case Subscript::SIV
:
4186 case Subscript::RDIV
:
4187 case Subscript::MIV
:
4190 llvm_unreachable("bad subscript classification");
4198 llvm_unreachable("somehow reached end of routine");