LLVM 22.0.0git
DependenceAnalysis.cpp
Go to the documentation of this file.
1//===-- DependenceAnalysis.cpp - DA Implementation --------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// DependenceAnalysis is an LLVM pass that analyses dependences between memory
10// accesses. Currently, it is an (incomplete) implementation of the approach
11// described in
12//
13// Practical Dependence Testing
14// Goff, Kennedy, Tseng
15// PLDI 1991
16//
17// There's a single entry point that analyzes the dependence between a pair
18// of memory references in a function, returning either NULL, for no dependence,
19// or a more-or-less detailed description of the dependence between them.
20//
21// 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.
25//
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.
30//
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.
34//
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.
38//
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.
42//
43// Please note that this is work in progress and the interface is subject to
44// change.
45//
46//===----------------------------------------------------------------------===//
47// //
48// In memory of Ken Kennedy, 1945 - 2007 //
49// //
50//===----------------------------------------------------------------------===//
51
53#include "llvm/ADT/Statistic.h"
61#include "llvm/IR/Module.h"
64#include "llvm/Support/Debug.h"
67
68using namespace llvm;
69
70#define DEBUG_TYPE "da"
71
72//===----------------------------------------------------------------------===//
73// statistics
74
75STATISTIC(TotalArrayPairs, "Array pairs tested");
76STATISTIC(SeparableSubscriptPairs, "Separable subscript pairs");
77STATISTIC(CoupledSubscriptPairs, "Coupled subscript pairs");
78STATISTIC(NonlinearSubscriptPairs, "Nonlinear subscript pairs");
79STATISTIC(ZIVapplications, "ZIV applications");
80STATISTIC(ZIVindependence, "ZIV independence");
81STATISTIC(StrongSIVapplications, "Strong SIV applications");
82STATISTIC(StrongSIVsuccesses, "Strong SIV successes");
83STATISTIC(StrongSIVindependence, "Strong SIV independence");
84STATISTIC(WeakCrossingSIVapplications, "Weak-Crossing SIV applications");
85STATISTIC(WeakCrossingSIVsuccesses, "Weak-Crossing SIV successes");
86STATISTIC(WeakCrossingSIVindependence, "Weak-Crossing SIV independence");
87STATISTIC(ExactSIVapplications, "Exact SIV applications");
88STATISTIC(ExactSIVsuccesses, "Exact SIV successes");
89STATISTIC(ExactSIVindependence, "Exact SIV independence");
90STATISTIC(WeakZeroSIVapplications, "Weak-Zero SIV applications");
91STATISTIC(WeakZeroSIVsuccesses, "Weak-Zero SIV successes");
92STATISTIC(WeakZeroSIVindependence, "Weak-Zero SIV independence");
93STATISTIC(ExactRDIVapplications, "Exact RDIV applications");
94STATISTIC(ExactRDIVindependence, "Exact RDIV independence");
95STATISTIC(SymbolicRDIVapplications, "Symbolic RDIV applications");
96STATISTIC(SymbolicRDIVindependence, "Symbolic RDIV independence");
97STATISTIC(DeltaApplications, "Delta applications");
98STATISTIC(DeltaSuccesses, "Delta successes");
99STATISTIC(DeltaIndependence, "Delta independence");
100STATISTIC(DeltaPropagations, "Delta propagations");
101STATISTIC(GCDapplications, "GCD applications");
102STATISTIC(GCDsuccesses, "GCD successes");
103STATISTIC(GCDindependence, "GCD independence");
104STATISTIC(BanerjeeApplications, "Banerjee applications");
105STATISTIC(BanerjeeIndependence, "Banerjee independence");
106STATISTIC(BanerjeeSuccesses, "Banerjee successes");
107
108static cl::opt<bool>
109 Delinearize("da-delinearize", cl::init(true), cl::Hidden,
110 cl::desc("Try to delinearize array references."));
112 "da-disable-delinearization-checks", cl::Hidden,
113 cl::desc(
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."));
118
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."));
123
124//===----------------------------------------------------------------------===//
125// basics
126
129 auto &AA = FAM.getResult<AAManager>(F);
131 auto &LI = FAM.getResult<LoopAnalysis>(F);
132 return DependenceInfo(&F, &AA, &SE, &LI);
133}
134
135AnalysisKey DependenceAnalysis::Key;
136
138 "Dependence Analysis", true, true)
144
146
148 : FunctionPass(ID) {}
149
152}
153
155 auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
156 auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
157 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
158 info.reset(new DependenceInfo(&F, &AA, &SE, &LI));
159 return false;
160}
161
163
165
167 AU.setPreservesAll();
171}
172
173// Used to test the dependence analyzer.
174// Looks through the function, noting instructions that may access memory.
175// Calls depends() on every possible pair and prints out the result.
176// Ignores all other instructions.
178 ScalarEvolution &SE, bool NormalizeResults) {
179 auto *F = DA->getFunction();
180 for (inst_iterator SrcI = inst_begin(F), SrcE = inst_end(F); SrcI != SrcE;
181 ++SrcI) {
182 if (SrcI->mayReadOrWriteMemory()) {
183 for (inst_iterator DstI = SrcI, DstE = inst_end(F); DstI != DstE;
184 ++DstI) {
185 if (DstI->mayReadOrWriteMemory()) {
186 OS << "Src:" << *SrcI << " --> Dst:" << *DstI << "\n";
187 OS << " da analyze - ";
188 if (auto D = DA->depends(&*SrcI, &*DstI,
189 /*UnderRuntimeAssumptions=*/true)) {
190
191#ifndef NDEBUG
192 // Verify that the distance being zero is equivalent to the
193 // direction being EQ.
194 for (unsigned Level = 1; Level <= D->getLevels(); Level++) {
195 const SCEV *Distance = D->getDistance(Level);
196 bool IsDistanceZero = Distance && Distance->isZero();
197 bool IsDirectionEQ =
198 D->getDirection(Level) == Dependence::DVEntry::EQ;
199 assert(IsDistanceZero == IsDirectionEQ &&
200 "Inconsistent distance and direction.");
201 }
202#endif
203
204 // Normalize negative direction vectors if required by clients.
205 if (NormalizeResults && D->normalize(&SE))
206 OS << "normalized - ";
207 D->dump(OS);
208 for (unsigned Level = 1; Level <= D->getLevels(); Level++) {
209 if (D->isSplitable(Level)) {
210 OS << " da analyze - split level = " << Level;
211 OS << ", iteration = " << *DA->getSplitIteration(*D, Level);
212 OS << "!\n";
213 }
214 }
215 } else
216 OS << "none!\n";
217 }
218 }
219 }
220 }
221 SCEVUnionPredicate Assumptions = DA->getRuntimeAssumptions();
222 if (!Assumptions.isAlwaysTrue()) {
223 OS << "Runtime Assumptions:\n";
224 Assumptions.print(OS, 0);
225 }
226}
227
229 const Module *) const {
231 OS, info.get(), getAnalysis<ScalarEvolutionWrapperPass>().getSE(), false);
232}
233
236 OS << "Printing analysis 'Dependence Analysis' for function '" << F.getName()
237 << "':\n";
240 NormalizeResults);
241 return PreservedAnalyses::all();
242}
243
244//===----------------------------------------------------------------------===//
245// Dependence methods
246
247// Returns true if this is an input dependence.
250}
251
252// Returns true if this is an output dependence.
255}
256
257// Returns true if this is an flow (aka true) dependence.
258bool Dependence::isFlow() const {
260}
261
262// Returns true if this is an anti dependence.
263bool Dependence::isAnti() const {
265}
266
267// Returns true if a particular level is scalar; that is,
268// if no subscript in the source or destination mention the induction
269// variable associated with the loop at this level.
270// Leave this out of line, so it will serve as a virtual method anchor
271bool Dependence::isScalar(unsigned level) const { return false; }
272
273//===----------------------------------------------------------------------===//
274// FullDependence methods
275
277 const SCEVUnionPredicate &Assumes,
278 bool PossiblyLoopIndependent,
279 unsigned CommonLevels)
280 : Dependence(Source, Destination, Assumes), Levels(CommonLevels),
281 LoopIndependent(PossiblyLoopIndependent) {
282 Consistent = true;
283 if (CommonLevels)
284 DV = std::make_unique<DVEntry[]>(CommonLevels);
285}
286
287// FIXME: in some cases the meaning of a negative direction vector
288// may not be straightforward, e.g.,
289// for (int i = 0; i < 32; ++i) {
290// Src: A[i] = ...;
291// Dst: use(A[31 - i]);
292// }
293// The dependency is
294// flow { Src[i] -> Dst[31 - i] : when i >= 16 } and
295// anti { Dst[i] -> Src[31 - i] : when i < 16 },
296// -- hence a [<>].
297// As long as a dependence result contains '>' ('<>', '<=>', "*"), it
298// means that a reversed/normalized dependence needs to be considered
299// as well. Nevertheless, current isDirectionNegative() only returns
300// true with a '>' or '>=' dependency for ease of canonicalizing the
301// dependency vector, since the reverse of '<>', '<=>' and "*" is itself.
303 for (unsigned Level = 1; Level <= Levels; ++Level) {
304 unsigned char Direction = DV[Level - 1].Direction;
306 continue;
309 return true;
310 return false;
311 }
312 return false;
313}
314
316 if (!isDirectionNegative())
317 return false;
318
319 LLVM_DEBUG(dbgs() << "Before normalizing negative direction vectors:\n";
320 dump(dbgs()););
321 std::swap(Src, Dst);
322 for (unsigned Level = 1; Level <= Levels; ++Level) {
323 unsigned char Direction = DV[Level - 1].Direction;
324 // Reverse the direction vector, this means LT becomes GT
325 // and GT becomes LT.
326 unsigned char RevDirection = Direction & Dependence::DVEntry::EQ;
328 RevDirection |= Dependence::DVEntry::GT;
330 RevDirection |= Dependence::DVEntry::LT;
331 DV[Level - 1].Direction = RevDirection;
332 // Reverse the dependence distance as well.
333 if (DV[Level - 1].Distance != nullptr)
334 DV[Level - 1].Distance = SE->getNegativeSCEV(DV[Level - 1].Distance);
335 }
336
337 LLVM_DEBUG(dbgs() << "After normalizing negative direction vectors:\n";
338 dump(dbgs()););
339 return true;
340}
341
342// The rest are simple getters that hide the implementation.
343
344// getDirection - Returns the direction associated with a particular level.
345unsigned FullDependence::getDirection(unsigned Level) const {
346 assert(0 < Level && Level <= Levels && "Level out of range");
347 return DV[Level - 1].Direction;
348}
349
350// Returns the distance (or NULL) associated with a particular level.
351const SCEV *FullDependence::getDistance(unsigned Level) const {
352 assert(0 < Level && Level <= Levels && "Level out of range");
353 return DV[Level - 1].Distance;
354}
355
356// Returns true if a particular level is scalar; that is,
357// if no subscript in the source or destination mention the induction
358// variable associated with the loop at this level.
359bool FullDependence::isScalar(unsigned Level) const {
360 assert(0 < Level && Level <= Levels && "Level out of range");
361 return DV[Level - 1].Scalar;
362}
363
364// Returns true if peeling the first iteration from this loop
365// will break this dependence.
366bool FullDependence::isPeelFirst(unsigned Level) const {
367 assert(0 < Level && Level <= Levels && "Level out of range");
368 return DV[Level - 1].PeelFirst;
369}
370
371// Returns true if peeling the last iteration from this loop
372// will break this dependence.
373bool FullDependence::isPeelLast(unsigned Level) const {
374 assert(0 < Level && Level <= Levels && "Level out of range");
375 return DV[Level - 1].PeelLast;
376}
377
378// Returns true if splitting this loop will break the dependence.
379bool FullDependence::isSplitable(unsigned Level) const {
380 assert(0 < Level && Level <= Levels && "Level out of range");
381 return DV[Level - 1].Splitable;
382}
383
384//===----------------------------------------------------------------------===//
385// DependenceInfo::Constraint methods
386
387// If constraint is a point <X, Y>, returns X.
388// Otherwise assert.
389const SCEV *DependenceInfo::Constraint::getX() const {
390 assert(Kind == Point && "Kind should be Point");
391 return A;
392}
393
394// If constraint is a point <X, Y>, returns Y.
395// Otherwise assert.
396const SCEV *DependenceInfo::Constraint::getY() const {
397 assert(Kind == Point && "Kind should be Point");
398 return B;
399}
400
401// If constraint is a line AX + BY = C, returns A.
402// Otherwise assert.
403const SCEV *DependenceInfo::Constraint::getA() const {
404 assert((Kind == Line || Kind == Distance) &&
405 "Kind should be Line (or Distance)");
406 return A;
407}
408
409// If constraint is a line AX + BY = C, returns B.
410// Otherwise assert.
411const SCEV *DependenceInfo::Constraint::getB() const {
412 assert((Kind == Line || Kind == Distance) &&
413 "Kind should be Line (or Distance)");
414 return B;
415}
416
417// If constraint is a line AX + BY = C, returns C.
418// Otherwise assert.
419const SCEV *DependenceInfo::Constraint::getC() const {
420 assert((Kind == Line || Kind == Distance) &&
421 "Kind should be Line (or Distance)");
422 return C;
423}
424
425// If constraint is a distance, returns D.
426// Otherwise assert.
427const SCEV *DependenceInfo::Constraint::getD() const {
428 assert(Kind == Distance && "Kind should be Distance");
429 return SE->getNegativeSCEV(C);
430}
431
432// Returns the loop associated with this constraint.
433const Loop *DependenceInfo::Constraint::getAssociatedLoop() const {
434 assert((Kind == Distance || Kind == Line || Kind == Point) &&
435 "Kind should be Distance, Line, or Point");
436 return AssociatedLoop;
437}
438
439void DependenceInfo::Constraint::setPoint(const SCEV *X, const SCEV *Y,
440 const Loop *CurLoop) {
441 Kind = Point;
442 A = X;
443 B = Y;
444 AssociatedLoop = CurLoop;
445}
446
447void DependenceInfo::Constraint::setLine(const SCEV *AA, const SCEV *BB,
448 const SCEV *CC, const Loop *CurLoop) {
449 Kind = Line;
450 A = AA;
451 B = BB;
452 C = CC;
453 AssociatedLoop = CurLoop;
454}
455
456void DependenceInfo::Constraint::setDistance(const SCEV *D,
457 const Loop *CurLoop) {
458 Kind = Distance;
459 A = SE->getOne(D->getType());
460 B = SE->getNegativeSCEV(A);
461 C = SE->getNegativeSCEV(D);
462 AssociatedLoop = CurLoop;
463}
464
465void DependenceInfo::Constraint::setEmpty() { Kind = Empty; }
466
467void DependenceInfo::Constraint::setAny(ScalarEvolution *NewSE) {
468 SE = NewSE;
469 Kind = Any;
470}
471
472#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
473// For debugging purposes. Dumps the constraint out to OS.
474LLVM_DUMP_METHOD void DependenceInfo::Constraint::dump(raw_ostream &OS) const {
475 if (isEmpty())
476 OS << " Empty\n";
477 else if (isAny())
478 OS << " Any\n";
479 else if (isPoint())
480 OS << " Point is <" << *getX() << ", " << *getY() << ">\n";
481 else if (isDistance())
482 OS << " Distance is " << *getD() << " (" << *getA() << "*X + " << *getB()
483 << "*Y = " << *getC() << ")\n";
484 else if (isLine())
485 OS << " Line is " << *getA() << "*X + " << *getB() << "*Y = " << *getC()
486 << "\n";
487 else
488 llvm_unreachable("unknown constraint type in Constraint::dump");
489}
490#endif
491
492// Updates X with the intersection
493// of the Constraints X and Y. Returns true if X has changed.
494// Corresponds to Figure 4 from the paper
495//
496// Practical Dependence Testing
497// Goff, Kennedy, Tseng
498// PLDI 1991
499bool DependenceInfo::intersectConstraints(Constraint *X, const Constraint *Y) {
500 ++DeltaApplications;
501 LLVM_DEBUG(dbgs() << "\tintersect constraints\n");
502 LLVM_DEBUG(dbgs() << "\t X ="; X->dump(dbgs()));
503 LLVM_DEBUG(dbgs() << "\t Y ="; Y->dump(dbgs()));
504 assert(!Y->isPoint() && "Y must not be a Point");
505 if (X->isAny()) {
506 if (Y->isAny())
507 return false;
508 *X = *Y;
509 return true;
510 }
511 if (X->isEmpty())
512 return false;
513 if (Y->isEmpty()) {
514 X->setEmpty();
515 return true;
516 }
517
518 if (X->isDistance() && Y->isDistance()) {
519 LLVM_DEBUG(dbgs() << "\t intersect 2 distances\n");
520 if (isKnownPredicate(CmpInst::ICMP_EQ, X->getD(), Y->getD()))
521 return false;
522 if (isKnownPredicate(CmpInst::ICMP_NE, X->getD(), Y->getD())) {
523 X->setEmpty();
524 ++DeltaSuccesses;
525 return true;
526 }
527 // Hmmm, interesting situation.
528 // I guess if either is constant, keep it and ignore the other.
529 if (isa<SCEVConstant>(Y->getD())) {
530 *X = *Y;
531 return true;
532 }
533 return false;
534 }
535
536 // At this point, the pseudo-code in Figure 4 of the paper
537 // checks if (X->isPoint() && Y->isPoint()).
538 // This case can't occur in our implementation,
539 // since a Point can only arise as the result of intersecting
540 // two Line constraints, and the right-hand value, Y, is never
541 // the result of an intersection.
542 assert(!(X->isPoint() && Y->isPoint()) &&
543 "We shouldn't ever see X->isPoint() && Y->isPoint()");
544
545 if (X->isLine() && Y->isLine()) {
546 LLVM_DEBUG(dbgs() << "\t intersect 2 lines\n");
547 const SCEV *Prod1 = SE->getMulExpr(X->getA(), Y->getB());
548 const SCEV *Prod2 = SE->getMulExpr(X->getB(), Y->getA());
549 if (isKnownPredicate(CmpInst::ICMP_EQ, Prod1, Prod2)) {
550 // slopes are equal, so lines are parallel
551 LLVM_DEBUG(dbgs() << "\t\tsame slope\n");
552 Prod1 = SE->getMulExpr(X->getC(), Y->getB());
553 Prod2 = SE->getMulExpr(X->getB(), Y->getC());
554 if (isKnownPredicate(CmpInst::ICMP_EQ, Prod1, Prod2))
555 return false;
556 if (isKnownPredicate(CmpInst::ICMP_NE, Prod1, Prod2)) {
557 X->setEmpty();
558 ++DeltaSuccesses;
559 return true;
560 }
561 return false;
562 }
563 if (isKnownPredicate(CmpInst::ICMP_NE, Prod1, Prod2)) {
564 // slopes differ, so lines intersect
565 LLVM_DEBUG(dbgs() << "\t\tdifferent slopes\n");
566 const SCEV *C1B2 = SE->getMulExpr(X->getC(), Y->getB());
567 const SCEV *C1A2 = SE->getMulExpr(X->getC(), Y->getA());
568 const SCEV *C2B1 = SE->getMulExpr(Y->getC(), X->getB());
569 const SCEV *C2A1 = SE->getMulExpr(Y->getC(), X->getA());
570 const SCEV *A1B2 = SE->getMulExpr(X->getA(), Y->getB());
571 const SCEV *A2B1 = SE->getMulExpr(Y->getA(), X->getB());
572 const SCEVConstant *C1A2_C2A1 =
573 dyn_cast<SCEVConstant>(SE->getMinusSCEV(C1A2, C2A1));
574 const SCEVConstant *C1B2_C2B1 =
575 dyn_cast<SCEVConstant>(SE->getMinusSCEV(C1B2, C2B1));
576 const SCEVConstant *A1B2_A2B1 =
577 dyn_cast<SCEVConstant>(SE->getMinusSCEV(A1B2, A2B1));
578 const SCEVConstant *A2B1_A1B2 =
579 dyn_cast<SCEVConstant>(SE->getMinusSCEV(A2B1, A1B2));
580 if (!C1B2_C2B1 || !C1A2_C2A1 || !A1B2_A2B1 || !A2B1_A1B2)
581 return false;
582 APInt Xtop = C1B2_C2B1->getAPInt();
583 APInt Xbot = A1B2_A2B1->getAPInt();
584 APInt Ytop = C1A2_C2A1->getAPInt();
585 APInt Ybot = A2B1_A1B2->getAPInt();
586 LLVM_DEBUG(dbgs() << "\t\tXtop = " << Xtop << "\n");
587 LLVM_DEBUG(dbgs() << "\t\tXbot = " << Xbot << "\n");
588 LLVM_DEBUG(dbgs() << "\t\tYtop = " << Ytop << "\n");
589 LLVM_DEBUG(dbgs() << "\t\tYbot = " << Ybot << "\n");
590 APInt Xq = Xtop; // these need to be initialized, even
591 APInt Xr = Xtop; // though they're just going to be overwritten
592 APInt::sdivrem(Xtop, Xbot, Xq, Xr);
593 APInt Yq = Ytop;
594 APInt Yr = Ytop;
595 APInt::sdivrem(Ytop, Ybot, Yq, Yr);
596 if (Xr != 0 || Yr != 0) {
597 X->setEmpty();
598 ++DeltaSuccesses;
599 return true;
600 }
601 LLVM_DEBUG(dbgs() << "\t\tX = " << Xq << ", Y = " << Yq << "\n");
602 if (Xq.slt(0) || Yq.slt(0)) {
603 X->setEmpty();
604 ++DeltaSuccesses;
605 return true;
606 }
607 if (const SCEVConstant *CUB = collectConstantUpperBound(
608 X->getAssociatedLoop(), Prod1->getType())) {
609 const APInt &UpperBound = CUB->getAPInt();
610 LLVM_DEBUG(dbgs() << "\t\tupper bound = " << UpperBound << "\n");
611 if (Xq.sgt(UpperBound) || Yq.sgt(UpperBound)) {
612 X->setEmpty();
613 ++DeltaSuccesses;
614 return true;
615 }
616 }
617 X->setPoint(SE->getConstant(Xq), SE->getConstant(Yq),
618 X->getAssociatedLoop());
619 ++DeltaSuccesses;
620 return true;
621 }
622 return false;
623 }
624
625 // if (X->isLine() && Y->isPoint()) This case can't occur.
626 assert(!(X->isLine() && Y->isPoint()) && "This case should never occur");
627
628 if (X->isPoint() && Y->isLine()) {
629 LLVM_DEBUG(dbgs() << "\t intersect Point and Line\n");
630 const SCEV *A1X1 = SE->getMulExpr(Y->getA(), X->getX());
631 const SCEV *B1Y1 = SE->getMulExpr(Y->getB(), X->getY());
632 const SCEV *Sum = SE->getAddExpr(A1X1, B1Y1);
633 if (isKnownPredicate(CmpInst::ICMP_EQ, Sum, Y->getC()))
634 return false;
635 if (isKnownPredicate(CmpInst::ICMP_NE, Sum, Y->getC())) {
636 X->setEmpty();
637 ++DeltaSuccesses;
638 return true;
639 }
640 return false;
641 }
642
643 llvm_unreachable("shouldn't reach the end of Constraint intersection");
644 return false;
645}
646
647//===----------------------------------------------------------------------===//
648// DependenceInfo methods
649
650// For debugging purposes. Dumps a dependence to OS.
652 bool Splitable = false;
653 if (isConfused())
654 OS << "confused";
655 else {
656 if (isConsistent())
657 OS << "consistent ";
658 if (isFlow())
659 OS << "flow";
660 else if (isOutput())
661 OS << "output";
662 else if (isAnti())
663 OS << "anti";
664 else if (isInput())
665 OS << "input";
666 unsigned Levels = getLevels();
667 OS << " [";
668 for (unsigned II = 1; II <= Levels; ++II) {
669 if (isSplitable(II))
670 Splitable = true;
671 if (isPeelFirst(II))
672 OS << 'p';
673 const SCEV *Distance = getDistance(II);
674 if (Distance)
675 OS << *Distance;
676 else if (isScalar(II))
677 OS << "S";
678 else {
679 unsigned Direction = getDirection(II);
680 if (Direction == DVEntry::ALL)
681 OS << "*";
682 else {
684 OS << "<";
686 OS << "=";
688 OS << ">";
689 }
690 }
691 if (isPeelLast(II))
692 OS << 'p';
693 if (II < Levels)
694 OS << " ";
695 }
696 if (isLoopIndependent())
697 OS << "|<";
698 OS << "]";
699 if (Splitable)
700 OS << " splitable";
701 }
702 OS << "!\n";
703
705 if (!Assumptions.isAlwaysTrue()) {
706 OS << " Runtime Assumptions:\n";
707 Assumptions.print(OS, 2);
708 }
709}
710
711// Returns NoAlias/MayAliass/MustAlias for two memory locations based upon their
712// underlaying objects. If LocA and LocB are known to not alias (for any reason:
713// tbaa, non-overlapping regions etc), then it is known there is no dependecy.
714// Otherwise the underlying objects are checked to see if they point to
715// different identifiable objects.
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 =
723 MemoryLocation LocBS =
725 BatchAAResults BAA(*AA);
727
728 if (BAA.isNoAlias(LocAS, LocBS))
730
731 // Check the underlying objects are the same
732 const Value *AObj = getUnderlyingObject(LocA.Ptr);
733 const Value *BObj = getUnderlyingObject(LocB.Ptr);
734
735 // If the underlying objects are the same, they must alias
736 if (AObj == BObj)
738
739 // We may have hit the recursion limit for underlying objects, or have
740 // underlying objects where we don't know they will alias.
741 if (!isIdentifiedObject(AObj) || !isIdentifiedObject(BObj))
743
744 // Otherwise we know the objects are different and both identified objects so
745 // must not alias.
747}
748
749// Returns true if the load or store can be analyzed. Atomic and volatile
750// operations have properties which this analysis does not understand.
751static bool isLoadOrStore(const Instruction *I) {
752 if (const LoadInst *LI = dyn_cast<LoadInst>(I))
753 return LI->isUnordered();
754 else if (const StoreInst *SI = dyn_cast<StoreInst>(I))
755 return SI->isUnordered();
756 return false;
757}
758
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:
772// 0 - unused
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:
781// for (a = ...) {
782// for (b = ...) {
783// for (c = ...) {
784// for (d = ...) {
785// A[] = ...;
786// }
787// }
788// for (e = ...) {
789// for (f = ...) {
790// for (g = ...) {
791// ... = A[];
792// }
793// }
794// }
795// }
796// }
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
802// a - 1
803// b - 2 = CommonLevels
804// c - 3
805// d - 4 = SrcLevels
806// e - 5
807// f - 6
808// g - 7 = MaxLevels
809void 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();
821 SrcLevel--;
822 }
823 while (DstLevel > SrcLevel) {
824 DstLoop = DstLoop->getParentLoop();
825 DstLevel--;
826 }
827 while (SrcLoop != DstLoop) {
828 SrcLoop = SrcLoop->getParentLoop();
829 DstLoop = DstLoop->getParentLoop();
830 SrcLevel--;
831 }
832 CommonLevels = SrcLevel;
833 MaxLevels -= CommonLevels;
834}
835
836// Given one of the loops containing the source, return
837// its level index in our numbering scheme.
838unsigned DependenceInfo::mapSrcLoop(const Loop *SrcLoop) const {
839 return SrcLoop->getLoopDepth();
840}
841
842// Given one of the loops containing the destination,
843// return its level index in our numbering scheme.
844unsigned DependenceInfo::mapDstLoop(const Loop *DstLoop) const {
845 unsigned D = DstLoop->getLoopDepth();
846 if (D > CommonLevels)
847 // This tries to make sure that we assign unique numbers to src and dst when
848 // the memory accesses reside in different loops that have the same depth.
849 return D - CommonLevels + SrcLevels;
850 else
851 return D;
852}
853
854// Returns true if Expression is loop invariant in LoopNest.
855bool DependenceInfo::isLoopInvariant(const SCEV *Expression,
856 const Loop *LoopNest) const {
857 // Unlike ScalarEvolution::isLoopInvariant() we consider an access outside of
858 // any loop as invariant, because we only consier expression evaluation at a
859 // specific position (where the array access takes place), and not across the
860 // entire function.
861 if (!LoopNest)
862 return true;
863
864 // If the expression is invariant in the outermost loop of the loop nest, it
865 // is invariant anywhere in the loop nest.
867}
868
869// Finds the set of loops from the LoopNest that
870// have a level <= CommonLevels and are referred to by the SCEV Expression.
871void DependenceInfo::collectCommonLoops(const SCEV *Expression,
872 const Loop *LoopNest,
873 SmallBitVector &Loops) const {
874 while (LoopNest) {
875 unsigned Level = LoopNest->getLoopDepth();
876 if (Level <= CommonLevels && !SE->isLoopInvariant(Expression, LoopNest))
877 Loops.set(Level);
878 LoopNest = LoopNest->getParentLoop();
879 }
880}
881
882void DependenceInfo::unifySubscriptType(ArrayRef<Subscript *> Pairs) {
883
884 unsigned widestWidthSeen = 0;
885 Type *widestType;
886
887 // Go through each pair and find the widest bit to which we need
888 // to extend all of them.
889 for (Subscript *Pair : Pairs) {
890 const SCEV *Src = Pair->Src;
891 const SCEV *Dst = Pair->Dst;
892 IntegerType *SrcTy = dyn_cast<IntegerType>(Src->getType());
893 IntegerType *DstTy = dyn_cast<IntegerType>(Dst->getType());
894 if (SrcTy == nullptr || DstTy == nullptr) {
895 assert(SrcTy == DstTy &&
896 "This function only unify integer types and "
897 "expect Src and Dst share the same type otherwise.");
898 continue;
899 }
900 if (SrcTy->getBitWidth() > widestWidthSeen) {
901 widestWidthSeen = SrcTy->getBitWidth();
902 widestType = SrcTy;
903 }
904 if (DstTy->getBitWidth() > widestWidthSeen) {
905 widestWidthSeen = DstTy->getBitWidth();
906 widestType = DstTy;
907 }
908 }
909
910 assert(widestWidthSeen > 0);
911
912 // Now extend each pair to the widest seen.
913 for (Subscript *Pair : Pairs) {
914 const SCEV *Src = Pair->Src;
915 const SCEV *Dst = Pair->Dst;
916 IntegerType *SrcTy = dyn_cast<IntegerType>(Src->getType());
917 IntegerType *DstTy = dyn_cast<IntegerType>(Dst->getType());
918 if (SrcTy == nullptr || DstTy == nullptr) {
919 assert(SrcTy == DstTy &&
920 "This function only unify integer types and "
921 "expect Src and Dst share the same type otherwise.");
922 continue;
923 }
924 if (SrcTy->getBitWidth() < widestWidthSeen)
925 // Sign-extend Src to widestType
926 Pair->Src = SE->getSignExtendExpr(Src, widestType);
927 if (DstTy->getBitWidth() < widestWidthSeen) {
928 // Sign-extend Dst to widestType
929 Pair->Dst = SE->getSignExtendExpr(Dst, widestType);
930 }
931 }
932}
933
934// removeMatchingExtensions - Examines a subscript pair.
935// If the source and destination are identically sign (or zero)
936// extended, it strips off the extension in an effect to simplify
937// the actual analysis.
938void DependenceInfo::removeMatchingExtensions(Subscript *Pair) {
939 const SCEV *Src = Pair->Src;
940 const SCEV *Dst = Pair->Dst;
941 if ((isa<SCEVZeroExtendExpr>(Src) && isa<SCEVZeroExtendExpr>(Dst)) ||
942 (isa<SCEVSignExtendExpr>(Src) && isa<SCEVSignExtendExpr>(Dst))) {
943 const SCEVIntegralCastExpr *SrcCast = cast<SCEVIntegralCastExpr>(Src);
944 const SCEVIntegralCastExpr *DstCast = cast<SCEVIntegralCastExpr>(Dst);
945 const SCEV *SrcCastOp = SrcCast->getOperand();
946 const SCEV *DstCastOp = DstCast->getOperand();
947 if (SrcCastOp->getType() == DstCastOp->getType()) {
948 Pair->Src = SrcCastOp;
949 Pair->Dst = DstCastOp;
950 }
951 }
952}
953
954// Examine the scev and return true iff it's affine.
955// Collect any loops mentioned in the set of "Loops".
956bool DependenceInfo::checkSubscript(const SCEV *Expr, const Loop *LoopNest,
957 SmallBitVector &Loops, bool IsSrc) {
958 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
959 if (!AddRec)
960 return isLoopInvariant(Expr, LoopNest);
961
962 // The AddRec must depend on one of the containing loops. Otherwise,
963 // mapSrcLoop and mapDstLoop return indices outside the intended range. This
964 // can happen when a subscript in one loop references an IV from a sibling
965 // loop that could not be replaced with a concrete exit value by
966 // getSCEVAtScope.
967 const Loop *L = LoopNest;
968 while (L && AddRec->getLoop() != L)
969 L = L->getParentLoop();
970 if (!L)
971 return false;
972
973 const SCEV *Start = AddRec->getStart();
974 const SCEV *Step = AddRec->getStepRecurrence(*SE);
975 if (!isLoopInvariant(Step, LoopNest))
976 return false;
977 if (IsSrc)
978 Loops.set(mapSrcLoop(AddRec->getLoop()));
979 else
980 Loops.set(mapDstLoop(AddRec->getLoop()));
981 return checkSubscript(Start, LoopNest, Loops, IsSrc);
982}
983
984// Examine the scev and return true iff it's linear.
985// Collect any loops mentioned in the set of "Loops".
986bool DependenceInfo::checkSrcSubscript(const SCEV *Src, const Loop *LoopNest,
988 return checkSubscript(Src, LoopNest, Loops, true);
989}
990
991// Examine the scev and return true iff it's linear.
992// Collect any loops mentioned in the set of "Loops".
993bool DependenceInfo::checkDstSubscript(const SCEV *Dst, const Loop *LoopNest,
995 return checkSubscript(Dst, LoopNest, Loops, false);
996}
997
998// Examines the subscript pair (the Src and Dst SCEVs)
999// and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear.
1000// Collects the associated loops in a set.
1001DependenceInfo::Subscript::ClassificationKind
1002DependenceInfo::classifyPair(const SCEV *Src, const Loop *SrcLoopNest,
1003 const SCEV *Dst, const Loop *DstLoopNest,
1005 SmallBitVector SrcLoops(MaxLevels + 1);
1006 SmallBitVector DstLoops(MaxLevels + 1);
1007 if (!checkSrcSubscript(Src, SrcLoopNest, SrcLoops))
1008 return Subscript::NonLinear;
1009 if (!checkDstSubscript(Dst, DstLoopNest, DstLoops))
1010 return Subscript::NonLinear;
1011 Loops = SrcLoops;
1012 Loops |= DstLoops;
1013 unsigned N = Loops.count();
1014 if (N == 0)
1015 return Subscript::ZIV;
1016 if (N == 1)
1017 return Subscript::SIV;
1018 if (N == 2 && (SrcLoops.count() == 0 || DstLoops.count() == 0 ||
1019 (SrcLoops.count() == 1 && DstLoops.count() == 1)))
1020 return Subscript::RDIV;
1021 return Subscript::MIV;
1022}
1023
1024// A wrapper around SCEV::isKnownPredicate.
1025// Looks for cases where we're interested in comparing for equality.
1026// If both X and Y have been identically sign or zero extended,
1027// it strips off the (confusing) extensions before invoking
1028// SCEV::isKnownPredicate. Perhaps, someday, the ScalarEvolution package
1029// will be similarly updated.
1030//
1031// If SCEV::isKnownPredicate can't prove the predicate,
1032// we try simple subtraction, which seems to help in some cases
1033// involving symbolics.
1034bool DependenceInfo::isKnownPredicate(ICmpInst::Predicate Pred, const SCEV *X,
1035 const SCEV *Y) const {
1036 if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) {
1037 if ((isa<SCEVSignExtendExpr>(X) && isa<SCEVSignExtendExpr>(Y)) ||
1038 (isa<SCEVZeroExtendExpr>(X) && isa<SCEVZeroExtendExpr>(Y))) {
1039 const SCEVIntegralCastExpr *CX = cast<SCEVIntegralCastExpr>(X);
1040 const SCEVIntegralCastExpr *CY = cast<SCEVIntegralCastExpr>(Y);
1041 const SCEV *Xop = CX->getOperand();
1042 const SCEV *Yop = CY->getOperand();
1043 if (Xop->getType() == Yop->getType()) {
1044 X = Xop;
1045 Y = Yop;
1046 }
1047 }
1048 }
1049 if (SE->isKnownPredicate(Pred, X, Y))
1050 return true;
1051 // If SE->isKnownPredicate can't prove the condition,
1052 // we try the brute-force approach of subtracting
1053 // and testing the difference.
1054 // By testing with SE->isKnownPredicate first, we avoid
1055 // the possibility of overflow when the arguments are constants.
1056 const SCEV *Delta = SE->getMinusSCEV(X, Y);
1057 switch (Pred) {
1058 case CmpInst::ICMP_EQ:
1059 return Delta->isZero();
1060 case CmpInst::ICMP_NE:
1061 return SE->isKnownNonZero(Delta);
1062 case CmpInst::ICMP_SGE:
1063 return SE->isKnownNonNegative(Delta);
1064 case CmpInst::ICMP_SLE:
1065 return SE->isKnownNonPositive(Delta);
1066 case CmpInst::ICMP_SGT:
1067 return SE->isKnownPositive(Delta);
1068 case CmpInst::ICMP_SLT:
1069 return SE->isKnownNegative(Delta);
1070 default:
1071 llvm_unreachable("unexpected predicate in isKnownPredicate");
1072 }
1073}
1074
1075/// Compare to see if S is less than Size, using
1076///
1077/// isKnownNegative(S - Size)
1078///
1079/// with some extra checking if S is an AddRec and we can prove less-than using
1080/// the loop bounds.
1081bool DependenceInfo::isKnownLessThan(const SCEV *S, const SCEV *Size) const {
1082 // First unify to the same type
1083 auto *SType = dyn_cast<IntegerType>(S->getType());
1084 auto *SizeType = dyn_cast<IntegerType>(Size->getType());
1085 if (!SType || !SizeType)
1086 return false;
1087 Type *MaxType =
1088 (SType->getBitWidth() >= SizeType->getBitWidth()) ? SType : SizeType;
1089 S = SE->getTruncateOrZeroExtend(S, MaxType);
1090 Size = SE->getTruncateOrZeroExtend(Size, MaxType);
1091
1092 // Special check for addrecs using BE taken count
1093 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S))
1094 if (AddRec->isAffine() && AddRec->hasNoSignedWrap()) {
1095 const SCEV *BECount = SE->getBackedgeTakenCount(AddRec->getLoop());
1096 const SCEV *Start = AddRec->getStart();
1097 const SCEV *Step = AddRec->getStepRecurrence(*SE);
1098 const SCEV *End = AddRec->evaluateAtIteration(BECount, *SE);
1099 const SCEV *Diff0 = SE->getMinusSCEV(Start, Size);
1100 const SCEV *Diff1 = SE->getMinusSCEV(End, Size);
1101
1102 // If the value of Step is non-negative and the AddRec is non-wrap, it
1103 // reaches its maximum at the last iteration. So it's enouth to check
1104 // whether End - Size is negative.
1105 if (SE->isKnownNonNegative(Step) && SE->isKnownNegative(Diff1))
1106 return true;
1107
1108 // If the value of Step is non-positive and the AddRec is non-wrap, the
1109 // initial value is its maximum.
1110 if (SE->isKnownNonPositive(Step) && SE->isKnownNegative(Diff0))
1111 return true;
1112
1113 // Even if we don't know the sign of Step, either Start or End must be
1114 // the maximum value of the AddRec since it is non-wrap.
1115 if (SE->isKnownNegative(Diff0) && SE->isKnownNegative(Diff1))
1116 return true;
1117 }
1118
1119 // Check using normal isKnownNegative
1120 const SCEV *LimitedBound = SE->getMinusSCEV(S, Size);
1121 return SE->isKnownNegative(LimitedBound);
1122}
1123
1124bool DependenceInfo::isKnownNonNegative(const SCEV *S, const Value *Ptr) const {
1125 bool Inbounds = false;
1126 if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(Ptr))
1127 Inbounds = SrcGEP->isInBounds();
1128 if (Inbounds) {
1129 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
1130 if (AddRec->isAffine()) {
1131 // We know S is for Ptr, the operand on a load/store, so doesn't wrap.
1132 // If both parts are NonNegative, the end result will be NonNegative
1133 if (SE->isKnownNonNegative(AddRec->getStart()) &&
1134 SE->isKnownNonNegative(AddRec->getOperand(1)))
1135 return true;
1136 }
1137 }
1138 }
1139
1140 return SE->isKnownNonNegative(S);
1141}
1142
1143// All subscripts are all the same type.
1144// Loop bound may be smaller (e.g., a char).
1145// Should zero extend loop bound, since it's always >= 0.
1146// This routine collects upper bound and extends or truncates if needed.
1147// Truncating is safe when subscripts are known not to wrap. Cases without
1148// nowrap flags should have been rejected earlier.
1149// Return null if no bound available.
1150const SCEV *DependenceInfo::collectUpperBound(const Loop *L, Type *T) const {
1152 const SCEV *UB = SE->getBackedgeTakenCount(L);
1153 return SE->getTruncateOrZeroExtend(UB, T);
1154 }
1155 return nullptr;
1156}
1157
1158// Calls collectUpperBound(), then attempts to cast it to SCEVConstant.
1159// If the cast fails, returns NULL.
1160const SCEVConstant *DependenceInfo::collectConstantUpperBound(const Loop *L,
1161 Type *T) const {
1162 if (const SCEV *UB = collectUpperBound(L, T))
1163 return dyn_cast<SCEVConstant>(UB);
1164 return nullptr;
1165}
1166
1167// testZIV -
1168// When we have a pair of subscripts of the form [c1] and [c2],
1169// where c1 and c2 are both loop invariant, we attack it using
1170// the ZIV test. Basically, we test by comparing the two values,
1171// but there are actually three possible results:
1172// 1) the values are equal, so there's a dependence
1173// 2) the values are different, so there's no dependence
1174// 3) the values might be equal, so we have to assume a dependence.
1175//
1176// Return true if dependence disproved.
1177bool DependenceInfo::testZIV(const SCEV *Src, const SCEV *Dst,
1178 FullDependence &Result) const {
1179 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
1180 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
1181 ++ZIVapplications;
1182 if (isKnownPredicate(CmpInst::ICMP_EQ, Src, Dst)) {
1183 LLVM_DEBUG(dbgs() << " provably dependent\n");
1184 return false; // provably dependent
1185 }
1186 if (isKnownPredicate(CmpInst::ICMP_NE, Src, Dst)) {
1187 LLVM_DEBUG(dbgs() << " provably independent\n");
1188 ++ZIVindependence;
1189 return true; // provably independent
1190 }
1191 LLVM_DEBUG(dbgs() << " possibly dependent\n");
1192 Result.Consistent = false;
1193 return false; // possibly dependent
1194}
1195
1196// strongSIVtest -
1197// From the paper, Practical Dependence Testing, Section 4.2.1
1198//
1199// When we have a pair of subscripts of the form [c1 + a*i] and [c2 + a*i],
1200// where i is an induction variable, c1 and c2 are loop invariant,
1201// and a is a constant, we can solve it exactly using the Strong SIV test.
1202//
1203// Can prove independence. Failing that, can compute distance (and direction).
1204// In the presence of symbolic terms, we can sometimes make progress.
1205//
1206// If there's a dependence,
1207//
1208// c1 + a*i = c2 + a*i'
1209//
1210// The dependence distance is
1211//
1212// d = i' - i = (c1 - c2)/a
1213//
1214// A dependence only exists if d is an integer and abs(d) <= U, where U is the
1215// loop's upper bound. If a dependence exists, the dependence direction is
1216// defined as
1217//
1218// { < if d > 0
1219// direction = { = if d = 0
1220// { > if d < 0
1221//
1222// Return true if dependence disproved.
1223bool DependenceInfo::strongSIVtest(const SCEV *Coeff, const SCEV *SrcConst,
1224 const SCEV *DstConst, const Loop *CurLoop,
1225 unsigned Level, FullDependence &Result,
1226 Constraint &NewConstraint) const {
1227 LLVM_DEBUG(dbgs() << "\tStrong SIV test\n");
1228 LLVM_DEBUG(dbgs() << "\t Coeff = " << *Coeff);
1229 LLVM_DEBUG(dbgs() << ", " << *Coeff->getType() << "\n");
1230 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst);
1231 LLVM_DEBUG(dbgs() << ", " << *SrcConst->getType() << "\n");
1232 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst);
1233 LLVM_DEBUG(dbgs() << ", " << *DstConst->getType() << "\n");
1234 ++StrongSIVapplications;
1235 assert(0 < Level && Level <= CommonLevels && "level out of range");
1236 Level--;
1237
1238 const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst);
1239 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta);
1240 LLVM_DEBUG(dbgs() << ", " << *Delta->getType() << "\n");
1241
1242 // check that |Delta| < iteration count
1243 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
1244 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound);
1245 LLVM_DEBUG(dbgs() << ", " << *UpperBound->getType() << "\n");
1246 const SCEV *AbsDelta =
1247 SE->isKnownNonNegative(Delta) ? Delta : SE->getNegativeSCEV(Delta);
1248 const SCEV *AbsCoeff =
1249 SE->isKnownNonNegative(Coeff) ? Coeff : SE->getNegativeSCEV(Coeff);
1250 const SCEV *Product = SE->getMulExpr(UpperBound, AbsCoeff);
1251 if (isKnownPredicate(CmpInst::ICMP_SGT, AbsDelta, Product)) {
1252 // Distance greater than trip count - no dependence
1253 ++StrongSIVindependence;
1254 ++StrongSIVsuccesses;
1255 return true;
1256 }
1257 }
1258
1259 // Can we compute distance?
1260 if (isa<SCEVConstant>(Delta) && isa<SCEVConstant>(Coeff)) {
1261 APInt ConstDelta = cast<SCEVConstant>(Delta)->getAPInt();
1262 APInt ConstCoeff = cast<SCEVConstant>(Coeff)->getAPInt();
1263 APInt Distance = ConstDelta; // these need to be initialized
1264 APInt Remainder = ConstDelta;
1265 APInt::sdivrem(ConstDelta, ConstCoeff, Distance, Remainder);
1266 LLVM_DEBUG(dbgs() << "\t Distance = " << Distance << "\n");
1267 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
1268 // Make sure Coeff divides Delta exactly
1269 if (Remainder != 0) {
1270 // Coeff doesn't divide Distance, no dependence
1271 ++StrongSIVindependence;
1272 ++StrongSIVsuccesses;
1273 return true;
1274 }
1275 Result.DV[Level].Distance = SE->getConstant(Distance);
1276 NewConstraint.setDistance(SE->getConstant(Distance), CurLoop);
1277 if (Distance.sgt(0))
1278 Result.DV[Level].Direction &= Dependence::DVEntry::LT;
1279 else if (Distance.slt(0))
1280 Result.DV[Level].Direction &= Dependence::DVEntry::GT;
1281 else
1282 Result.DV[Level].Direction &= Dependence::DVEntry::EQ;
1283 ++StrongSIVsuccesses;
1284 } else if (Delta->isZero()) {
1285 // since 0/X == 0
1286 Result.DV[Level].Distance = Delta;
1287 NewConstraint.setDistance(Delta, CurLoop);
1288 Result.DV[Level].Direction &= Dependence::DVEntry::EQ;
1289 ++StrongSIVsuccesses;
1290 } else {
1291 if (Coeff->isOne()) {
1292 LLVM_DEBUG(dbgs() << "\t Distance = " << *Delta << "\n");
1293 Result.DV[Level].Distance = Delta; // since X/1 == X
1294 NewConstraint.setDistance(Delta, CurLoop);
1295 } else {
1296 Result.Consistent = false;
1297 NewConstraint.setLine(Coeff, SE->getNegativeSCEV(Coeff),
1298 SE->getNegativeSCEV(Delta), CurLoop);
1299 }
1300
1301 // maybe we can get a useful direction
1302 bool DeltaMaybeZero = !SE->isKnownNonZero(Delta);
1303 bool DeltaMaybePositive = !SE->isKnownNonPositive(Delta);
1304 bool DeltaMaybeNegative = !SE->isKnownNonNegative(Delta);
1305 bool CoeffMaybePositive = !SE->isKnownNonPositive(Coeff);
1306 bool CoeffMaybeNegative = !SE->isKnownNonNegative(Coeff);
1307 // The double negatives above are confusing.
1308 // It helps to read !SE->isKnownNonZero(Delta)
1309 // as "Delta might be Zero"
1310 unsigned NewDirection = Dependence::DVEntry::NONE;
1311 if ((DeltaMaybePositive && CoeffMaybePositive) ||
1312 (DeltaMaybeNegative && CoeffMaybeNegative))
1313 NewDirection = Dependence::DVEntry::LT;
1314 if (DeltaMaybeZero)
1315 NewDirection |= Dependence::DVEntry::EQ;
1316 if ((DeltaMaybeNegative && CoeffMaybePositive) ||
1317 (DeltaMaybePositive && CoeffMaybeNegative))
1318 NewDirection |= Dependence::DVEntry::GT;
1319 if (NewDirection < Result.DV[Level].Direction)
1320 ++StrongSIVsuccesses;
1321 Result.DV[Level].Direction &= NewDirection;
1322 }
1323 return false;
1324}
1325
1326// weakCrossingSIVtest -
1327// From the paper, Practical Dependence Testing, Section 4.2.2
1328//
1329// When we have a pair of subscripts of the form [c1 + a*i] and [c2 - a*i],
1330// where i is an induction variable, c1 and c2 are loop invariant,
1331// and a is a constant, we can solve it exactly using the
1332// Weak-Crossing SIV test.
1333//
1334// Given c1 + a*i = c2 - a*i', we can look for the intersection of
1335// the two lines, where i = i', yielding
1336//
1337// c1 + a*i = c2 - a*i
1338// 2a*i = c2 - c1
1339// i = (c2 - c1)/2a
1340//
1341// If i < 0, there is no dependence.
1342// If i > upperbound, there is no dependence.
1343// If i = 0 (i.e., if c1 = c2), there's a dependence with distance = 0.
1344// If i = upperbound, there's a dependence with distance = 0.
1345// If i is integral, there's a dependence (all directions).
1346// If the non-integer part = 1/2, there's a dependence (<> directions).
1347// Otherwise, there's no dependence.
1348//
1349// Can prove independence. Failing that,
1350// can sometimes refine the directions.
1351// Can determine iteration for splitting.
1352//
1353// Return true if dependence disproved.
1354bool DependenceInfo::weakCrossingSIVtest(
1355 const SCEV *Coeff, const SCEV *SrcConst, const SCEV *DstConst,
1356 const Loop *CurLoop, unsigned Level, FullDependence &Result,
1357 Constraint &NewConstraint, const SCEV *&SplitIter) const {
1358 LLVM_DEBUG(dbgs() << "\tWeak-Crossing SIV test\n");
1359 LLVM_DEBUG(dbgs() << "\t Coeff = " << *Coeff << "\n");
1360 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1361 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1362 ++WeakCrossingSIVapplications;
1363 assert(0 < Level && Level <= CommonLevels && "Level out of range");
1364 Level--;
1365 Result.Consistent = false;
1366 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1367 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1368 NewConstraint.setLine(Coeff, Coeff, Delta, CurLoop);
1369 if (Delta->isZero()) {
1370 Result.DV[Level].Direction &= ~Dependence::DVEntry::LT;
1371 Result.DV[Level].Direction &= ~Dependence::DVEntry::GT;
1372 ++WeakCrossingSIVsuccesses;
1373 if (!Result.DV[Level].Direction) {
1374 ++WeakCrossingSIVindependence;
1375 return true;
1376 }
1377 Result.DV[Level].Distance = Delta; // = 0
1378 return false;
1379 }
1380 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(Coeff);
1381 if (!ConstCoeff)
1382 return false;
1383
1384 Result.DV[Level].Splitable = true;
1385 if (SE->isKnownNegative(ConstCoeff)) {
1386 ConstCoeff = dyn_cast<SCEVConstant>(SE->getNegativeSCEV(ConstCoeff));
1387 assert(ConstCoeff &&
1388 "dynamic cast of negative of ConstCoeff should yield constant");
1389 Delta = SE->getNegativeSCEV(Delta);
1390 }
1391 assert(SE->isKnownPositive(ConstCoeff) && "ConstCoeff should be positive");
1392
1393 // compute SplitIter for use by DependenceInfo::getSplitIteration()
1394 SplitIter = SE->getUDivExpr(
1395 SE->getSMaxExpr(SE->getZero(Delta->getType()), Delta),
1396 SE->getMulExpr(SE->getConstant(Delta->getType(), 2), ConstCoeff));
1397 LLVM_DEBUG(dbgs() << "\t Split iter = " << *SplitIter << "\n");
1398
1399 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1400 if (!ConstDelta)
1401 return false;
1402
1403 // We're certain that ConstCoeff > 0; therefore,
1404 // if Delta < 0, then no dependence.
1405 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1406 LLVM_DEBUG(dbgs() << "\t ConstCoeff = " << *ConstCoeff << "\n");
1407 if (SE->isKnownNegative(Delta)) {
1408 // No dependence, Delta < 0
1409 ++WeakCrossingSIVindependence;
1410 ++WeakCrossingSIVsuccesses;
1411 return true;
1412 }
1413
1414 // We're certain that Delta > 0 and ConstCoeff > 0.
1415 // Check Delta/(2*ConstCoeff) against upper loop bound
1416 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
1417 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
1418 const SCEV *ConstantTwo = SE->getConstant(UpperBound->getType(), 2);
1419 const SCEV *ML =
1420 SE->getMulExpr(SE->getMulExpr(ConstCoeff, UpperBound), ConstantTwo);
1421 LLVM_DEBUG(dbgs() << "\t ML = " << *ML << "\n");
1422 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, ML)) {
1423 // Delta too big, no dependence
1424 ++WeakCrossingSIVindependence;
1425 ++WeakCrossingSIVsuccesses;
1426 return true;
1427 }
1428 if (isKnownPredicate(CmpInst::ICMP_EQ, Delta, ML)) {
1429 // i = i' = UB
1430 Result.DV[Level].Direction &= ~Dependence::DVEntry::LT;
1431 Result.DV[Level].Direction &= ~Dependence::DVEntry::GT;
1432 ++WeakCrossingSIVsuccesses;
1433 if (!Result.DV[Level].Direction) {
1434 ++WeakCrossingSIVindependence;
1435 return true;
1436 }
1437 Result.DV[Level].Splitable = false;
1438 Result.DV[Level].Distance = SE->getZero(Delta->getType());
1439 return false;
1440 }
1441 }
1442
1443 // check that Coeff divides Delta
1444 APInt APDelta = ConstDelta->getAPInt();
1445 APInt APCoeff = ConstCoeff->getAPInt();
1446 APInt Distance = APDelta; // these need to be initialzed
1447 APInt Remainder = APDelta;
1448 APInt::sdivrem(APDelta, APCoeff, Distance, Remainder);
1449 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
1450 if (Remainder != 0) {
1451 // Coeff doesn't divide Delta, no dependence
1452 ++WeakCrossingSIVindependence;
1453 ++WeakCrossingSIVsuccesses;
1454 return true;
1455 }
1456 LLVM_DEBUG(dbgs() << "\t Distance = " << Distance << "\n");
1457
1458 // if 2*Coeff doesn't divide Delta, then the equal direction isn't possible
1459 APInt Two = APInt(Distance.getBitWidth(), 2, true);
1460 Remainder = Distance.srem(Two);
1461 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
1462 if (Remainder != 0) {
1463 // Equal direction isn't possible
1464 Result.DV[Level].Direction &= ~Dependence::DVEntry::EQ;
1465 ++WeakCrossingSIVsuccesses;
1466 }
1467 return false;
1468}
1469
1470// Kirch's algorithm, from
1471//
1472// Optimizing Supercompilers for Supercomputers
1473// Michael Wolfe
1474// MIT Press, 1989
1475//
1476// Program 2.1, page 29.
1477// Computes the GCD of AM and BM.
1478// Also finds a solution to the equation ax - by = gcd(a, b).
1479// Returns true if dependence disproved; i.e., gcd does not divide Delta.
1480static bool findGCD(unsigned Bits, const APInt &AM, const APInt &BM,
1481 const APInt &Delta, APInt &G, APInt &X, APInt &Y) {
1482 APInt A0(Bits, 1, true), A1(Bits, 0, true);
1483 APInt B0(Bits, 0, true), B1(Bits, 1, true);
1484 APInt G0 = AM.abs();
1485 APInt G1 = BM.abs();
1486 APInt Q = G0; // these need to be initialized
1487 APInt R = G0;
1488 APInt::sdivrem(G0, G1, Q, R);
1489 while (R != 0) {
1490 // clang-format off
1491 APInt A2 = A0 - Q*A1; A0 = A1; A1 = A2;
1492 APInt B2 = B0 - Q*B1; B0 = B1; B1 = B2;
1493 G0 = G1; G1 = R;
1494 // clang-format on
1495 APInt::sdivrem(G0, G1, Q, R);
1496 }
1497 G = G1;
1498 LLVM_DEBUG(dbgs() << "\t GCD = " << G << "\n");
1499 X = AM.slt(0) ? -A1 : A1;
1500 Y = BM.slt(0) ? B1 : -B1;
1501
1502 // make sure gcd divides Delta
1503 R = Delta.srem(G);
1504 if (R != 0)
1505 return true; // gcd doesn't divide Delta, no dependence
1506 Q = Delta.sdiv(G);
1507 return false;
1508}
1509
1510static APInt floorOfQuotient(const APInt &A, const APInt &B) {
1511 APInt Q = A; // these need to be initialized
1512 APInt R = A;
1513 APInt::sdivrem(A, B, Q, R);
1514 if (R == 0)
1515 return Q;
1516 if ((A.sgt(0) && B.sgt(0)) || (A.slt(0) && B.slt(0)))
1517 return Q;
1518 else
1519 return Q - 1;
1520}
1521
1522static APInt ceilingOfQuotient(const APInt &A, const APInt &B) {
1523 APInt Q = A; // these need to be initialized
1524 APInt R = A;
1525 APInt::sdivrem(A, B, Q, R);
1526 if (R == 0)
1527 return Q;
1528 if ((A.sgt(0) && B.sgt(0)) || (A.slt(0) && B.slt(0)))
1529 return Q + 1;
1530 else
1531 return Q;
1532}
1533
1534/// Given an affine expression of the form A*k + B, where k is an arbitrary
1535/// integer, infer the possible range of k based on the known range of the
1536/// affine expression. If we know A*k + B is non-negative, i.e.,
1537///
1538/// A*k + B >= 0
1539///
1540/// we can derive the following inequalities for k when A is positive:
1541///
1542/// k >= -B / A
1543///
1544/// Since k is an integer, it means k is greater than or equal to the
1545/// ceil(-B / A).
1546///
1547/// If the upper bound of the affine expression \p UB is passed, the following
1548/// inequality can be derived as well:
1549///
1550/// A*k + B <= UB
1551///
1552/// which leads to:
1553///
1554/// k <= (UB - B) / A
1555///
1556/// Again, as k is an integer, it means k is less than or equal to the
1557/// floor((UB - B) / A).
1558///
1559/// The similar logic applies when A is negative, but the inequalities sign flip
1560/// while working with them.
1561///
1562/// Preconditions: \p A is non-zero, and we know A*k + B is non-negative.
1563static std::pair<std::optional<APInt>, std::optional<APInt>>
1565 const std::optional<APInt> &UB) {
1566 assert(A != 0 && "A must be non-zero");
1567 std::optional<APInt> TL, TU;
1568 if (A.sgt(0)) {
1569 TL = ceilingOfQuotient(-B, A);
1570 LLVM_DEBUG(dbgs() << "\t Possible TL = " << *TL << "\n");
1571 // New bound check - modification to Banerjee's e3 check
1572 if (UB) {
1573 // TODO?: Overflow check for UB - B
1574 TU = floorOfQuotient(*UB - B, A);
1575 LLVM_DEBUG(dbgs() << "\t Possible TU = " << *TU << "\n");
1576 }
1577 } else {
1578 TU = floorOfQuotient(-B, A);
1579 LLVM_DEBUG(dbgs() << "\t Possible TU = " << *TU << "\n");
1580 // New bound check - modification to Banerjee's e3 check
1581 if (UB) {
1582 // TODO?: Overflow check for UB - B
1583 TL = ceilingOfQuotient(*UB - B, A);
1584 LLVM_DEBUG(dbgs() << "\t Possible TL = " << *TL << "\n");
1585 }
1586 }
1587 return std::make_pair(TL, TU);
1588}
1589
1590// exactSIVtest -
1591// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*i],
1592// where i is an induction variable, c1 and c2 are loop invariant, and a1
1593// and a2 are constant, we can solve it exactly using an algorithm developed
1594// by Banerjee and Wolfe. See Algorithm 6.2.1 (case 2.5) in:
1595//
1596// Dependence Analysis for Supercomputing
1597// Utpal Banerjee
1598// Kluwer Academic Publishers, 1988
1599//
1600// It's slower than the specialized tests (strong SIV, weak-zero SIV, etc),
1601// so use them if possible. They're also a bit better with symbolics and,
1602// in the case of the strong SIV test, can compute Distances.
1603//
1604// Return true if dependence disproved.
1605//
1606// This is a modified version of the original Banerjee algorithm. The original
1607// only tested whether Dst depends on Src. This algorithm extends that and
1608// returns all the dependencies that exist between Dst and Src.
1609bool DependenceInfo::exactSIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
1610 const SCEV *SrcConst, const SCEV *DstConst,
1611 const Loop *CurLoop, unsigned Level,
1612 FullDependence &Result,
1613 Constraint &NewConstraint) const {
1614 LLVM_DEBUG(dbgs() << "\tExact SIV test\n");
1615 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n");
1616 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n");
1617 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1618 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1619 ++ExactSIVapplications;
1620 assert(0 < Level && Level <= CommonLevels && "Level out of range");
1621 Level--;
1622 Result.Consistent = false;
1623 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1624 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1625 NewConstraint.setLine(SrcCoeff, SE->getNegativeSCEV(DstCoeff), Delta,
1626 CurLoop);
1627 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1628 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1629 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1630 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
1631 return false;
1632
1633 // find gcd
1634 APInt G, X, Y;
1635 APInt AM = ConstSrcCoeff->getAPInt();
1636 APInt BM = ConstDstCoeff->getAPInt();
1637 APInt CM = ConstDelta->getAPInt();
1638 unsigned Bits = AM.getBitWidth();
1639 if (findGCD(Bits, AM, BM, CM, G, X, Y)) {
1640 // gcd doesn't divide Delta, no dependence
1641 ++ExactSIVindependence;
1642 ++ExactSIVsuccesses;
1643 return true;
1644 }
1645
1646 LLVM_DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n");
1647
1648 // since SCEV construction normalizes, LM = 0
1649 std::optional<APInt> UM;
1650 // UM is perhaps unavailable, let's check
1651 if (const SCEVConstant *CUB =
1652 collectConstantUpperBound(CurLoop, Delta->getType())) {
1653 UM = CUB->getAPInt();
1654 LLVM_DEBUG(dbgs() << "\t UM = " << *UM << "\n");
1655 }
1656
1659 APInt TC = CM.sdiv(G);
1660 APInt TX = X * TC;
1661 APInt TY = Y * TC;
1662 LLVM_DEBUG(dbgs() << "\t TC = " << TC << "\n");
1663 LLVM_DEBUG(dbgs() << "\t TX = " << TX << "\n");
1664 LLVM_DEBUG(dbgs() << "\t TY = " << TY << "\n");
1665
1666 APInt TB = BM.sdiv(G);
1667 APInt TA = AM.sdiv(G);
1668
1669 // At this point, we have the following equations:
1670 //
1671 // TA*i0 - TB*i1 = TC
1672 //
1673 // Also, we know that the all pairs of (i0, i1) can be expressed as:
1674 //
1675 // (TX + k*TB, TY + k*TA)
1676 //
1677 // where k is an arbitrary integer.
1678 auto [TL0, TU0] = inferDomainOfAffine(TB, TX, UM);
1679 auto [TL1, TU1] = inferDomainOfAffine(TA, TY, UM);
1680
1681 auto CreateVec = [](const std::optional<APInt> &V0,
1682 const std::optional<APInt> &V1) {
1684 if (V0)
1685 Vec.push_back(*V0);
1686 if (V1)
1687 Vec.push_back(*V1);
1688 return Vec;
1689 };
1690
1691 SmallVector<APInt, 2> TLVec = CreateVec(TL0, TL1);
1692 SmallVector<APInt, 2> TUVec = CreateVec(TU0, TU1);
1693
1694 LLVM_DEBUG(dbgs() << "\t TA = " << TA << "\n");
1695 LLVM_DEBUG(dbgs() << "\t TB = " << TB << "\n");
1696
1697 if (TLVec.empty() || TUVec.empty())
1698 return false;
1699 TL = APIntOps::smax(TLVec.front(), TLVec.back());
1700 TU = APIntOps::smin(TUVec.front(), TUVec.back());
1701 LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n");
1702 LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n");
1703
1704 if (TL.sgt(TU)) {
1705 ++ExactSIVindependence;
1706 ++ExactSIVsuccesses;
1707 return true;
1708 }
1709
1710 // explore directions
1711 unsigned NewDirection = Dependence::DVEntry::NONE;
1712 APInt LowerDistance, UpperDistance;
1713 if (TA.sgt(TB)) {
1714 LowerDistance = (TY - TX) + (TA - TB) * TL;
1715 UpperDistance = (TY - TX) + (TA - TB) * TU;
1716 } else {
1717 LowerDistance = (TY - TX) + (TA - TB) * TU;
1718 UpperDistance = (TY - TX) + (TA - TB) * TL;
1719 }
1720
1721 LLVM_DEBUG(dbgs() << "\t LowerDistance = " << LowerDistance << "\n");
1722 LLVM_DEBUG(dbgs() << "\t UpperDistance = " << UpperDistance << "\n");
1723
1724 APInt Zero(Bits, 0, true);
1725 if (LowerDistance.sle(Zero) && UpperDistance.sge(Zero)) {
1726 NewDirection |= Dependence::DVEntry::EQ;
1727 ++ExactSIVsuccesses;
1728 }
1729 if (LowerDistance.slt(0)) {
1730 NewDirection |= Dependence::DVEntry::GT;
1731 ++ExactSIVsuccesses;
1732 }
1733 if (UpperDistance.sgt(0)) {
1734 NewDirection |= Dependence::DVEntry::LT;
1735 ++ExactSIVsuccesses;
1736 }
1737
1738 // finished
1739 Result.DV[Level].Direction &= NewDirection;
1740 if (Result.DV[Level].Direction == Dependence::DVEntry::NONE)
1741 ++ExactSIVindependence;
1742 LLVM_DEBUG(dbgs() << "\t Result = ");
1743 LLVM_DEBUG(Result.dump(dbgs()));
1744 return Result.DV[Level].Direction == Dependence::DVEntry::NONE;
1745}
1746
1747// Return true if the divisor evenly divides the dividend.
1748static bool isRemainderZero(const SCEVConstant *Dividend,
1749 const SCEVConstant *Divisor) {
1750 const APInt &ConstDividend = Dividend->getAPInt();
1751 const APInt &ConstDivisor = Divisor->getAPInt();
1752 return ConstDividend.srem(ConstDivisor) == 0;
1753}
1754
1755// weakZeroSrcSIVtest -
1756// From the paper, Practical Dependence Testing, Section 4.2.2
1757//
1758// When we have a pair of subscripts of the form [c1] and [c2 + a*i],
1759// where i is an induction variable, c1 and c2 are loop invariant,
1760// and a is a constant, we can solve it exactly using the
1761// Weak-Zero SIV test.
1762//
1763// Given
1764//
1765// c1 = c2 + a*i
1766//
1767// we get
1768//
1769// (c1 - c2)/a = i
1770//
1771// If i is not an integer, there's no dependence.
1772// If i < 0 or > UB, there's no dependence.
1773// If i = 0, the direction is >= and peeling the
1774// 1st iteration will break the dependence.
1775// If i = UB, the direction is <= and peeling the
1776// last iteration will break the dependence.
1777// Otherwise, the direction is *.
1778//
1779// Can prove independence. Failing that, we can sometimes refine
1780// the directions. Can sometimes show that first or last
1781// iteration carries all the dependences (so worth peeling).
1782//
1783// (see also weakZeroDstSIVtest)
1784//
1785// Return true if dependence disproved.
1786bool DependenceInfo::weakZeroSrcSIVtest(const SCEV *DstCoeff,
1787 const SCEV *SrcConst,
1788 const SCEV *DstConst,
1789 const Loop *CurLoop, unsigned Level,
1790 FullDependence &Result,
1791 Constraint &NewConstraint) const {
1792 // For the WeakSIV test, it's possible the loop isn't common to
1793 // the Src and Dst loops. If it isn't, then there's no need to
1794 // record a direction.
1795 LLVM_DEBUG(dbgs() << "\tWeak-Zero (src) SIV test\n");
1796 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << "\n");
1797 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1798 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1799 ++WeakZeroSIVapplications;
1800 assert(0 < Level && Level <= MaxLevels && "Level out of range");
1801 Level--;
1802 Result.Consistent = false;
1803 const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst);
1804 NewConstraint.setLine(SE->getZero(Delta->getType()), DstCoeff, Delta,
1805 CurLoop);
1806 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1807 if (isKnownPredicate(CmpInst::ICMP_EQ, SrcConst, DstConst)) {
1808 if (Level < CommonLevels) {
1809 Result.DV[Level].Direction &= Dependence::DVEntry::GE;
1810 Result.DV[Level].PeelFirst = true;
1811 ++WeakZeroSIVsuccesses;
1812 }
1813 return false; // dependences caused by first iteration
1814 }
1815 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1816 if (!ConstCoeff)
1817 return false;
1818 const SCEV *AbsCoeff = SE->isKnownNegative(ConstCoeff)
1819 ? SE->getNegativeSCEV(ConstCoeff)
1820 : ConstCoeff;
1821 const SCEV *NewDelta =
1822 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
1823
1824 // check that Delta/SrcCoeff < iteration count
1825 // really check NewDelta < count*AbsCoeff
1826 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
1827 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
1828 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound);
1829 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) {
1830 ++WeakZeroSIVindependence;
1831 ++WeakZeroSIVsuccesses;
1832 return true;
1833 }
1834 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) {
1835 // dependences caused by last iteration
1836 if (Level < CommonLevels) {
1837 Result.DV[Level].Direction &= Dependence::DVEntry::LE;
1838 Result.DV[Level].PeelLast = true;
1839 ++WeakZeroSIVsuccesses;
1840 }
1841 return false;
1842 }
1843 }
1844
1845 // check that Delta/SrcCoeff >= 0
1846 // really check that NewDelta >= 0
1847 if (SE->isKnownNegative(NewDelta)) {
1848 // No dependence, newDelta < 0
1849 ++WeakZeroSIVindependence;
1850 ++WeakZeroSIVsuccesses;
1851 return true;
1852 }
1853
1854 // if SrcCoeff doesn't divide Delta, then no dependence
1855 if (isa<SCEVConstant>(Delta) &&
1856 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) {
1857 ++WeakZeroSIVindependence;
1858 ++WeakZeroSIVsuccesses;
1859 return true;
1860 }
1861 return false;
1862}
1863
1864// weakZeroDstSIVtest -
1865// From the paper, Practical Dependence Testing, Section 4.2.2
1866//
1867// When we have a pair of subscripts of the form [c1 + a*i] and [c2],
1868// where i is an induction variable, c1 and c2 are loop invariant,
1869// and a is a constant, we can solve it exactly using the
1870// Weak-Zero SIV test.
1871//
1872// Given
1873//
1874// c1 + a*i = c2
1875//
1876// we get
1877//
1878// i = (c2 - c1)/a
1879//
1880// If i is not an integer, there's no dependence.
1881// If i < 0 or > UB, there's no dependence.
1882// If i = 0, the direction is <= and peeling the
1883// 1st iteration will break the dependence.
1884// If i = UB, the direction is >= and peeling the
1885// last iteration will break the dependence.
1886// Otherwise, the direction is *.
1887//
1888// Can prove independence. Failing that, we can sometimes refine
1889// the directions. Can sometimes show that first or last
1890// iteration carries all the dependences (so worth peeling).
1891//
1892// (see also weakZeroSrcSIVtest)
1893//
1894// Return true if dependence disproved.
1895bool DependenceInfo::weakZeroDstSIVtest(const SCEV *SrcCoeff,
1896 const SCEV *SrcConst,
1897 const SCEV *DstConst,
1898 const Loop *CurLoop, unsigned Level,
1899 FullDependence &Result,
1900 Constraint &NewConstraint) const {
1901 // For the WeakSIV test, it's possible the loop isn't common to the
1902 // Src and Dst loops. If it isn't, then there's no need to record a direction.
1903 LLVM_DEBUG(dbgs() << "\tWeak-Zero (dst) SIV test\n");
1904 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << "\n");
1905 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1906 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1907 ++WeakZeroSIVapplications;
1908 assert(0 < Level && Level <= SrcLevels && "Level out of range");
1909 Level--;
1910 Result.Consistent = false;
1911 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1912 NewConstraint.setLine(SrcCoeff, SE->getZero(Delta->getType()), Delta,
1913 CurLoop);
1914 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1915 if (isKnownPredicate(CmpInst::ICMP_EQ, DstConst, SrcConst)) {
1916 if (Level < CommonLevels) {
1917 Result.DV[Level].Direction &= Dependence::DVEntry::LE;
1918 Result.DV[Level].PeelFirst = true;
1919 ++WeakZeroSIVsuccesses;
1920 }
1921 return false; // dependences caused by first iteration
1922 }
1923 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1924 if (!ConstCoeff)
1925 return false;
1926 const SCEV *AbsCoeff = SE->isKnownNegative(ConstCoeff)
1927 ? SE->getNegativeSCEV(ConstCoeff)
1928 : ConstCoeff;
1929 const SCEV *NewDelta =
1930 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
1931
1932 // check that Delta/SrcCoeff < iteration count
1933 // really check NewDelta < count*AbsCoeff
1934 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
1935 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
1936 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound);
1937 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) {
1938 ++WeakZeroSIVindependence;
1939 ++WeakZeroSIVsuccesses;
1940 return true;
1941 }
1942 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) {
1943 // dependences caused by last iteration
1944 if (Level < CommonLevels) {
1945 Result.DV[Level].Direction &= Dependence::DVEntry::GE;
1946 Result.DV[Level].PeelLast = true;
1947 ++WeakZeroSIVsuccesses;
1948 }
1949 return false;
1950 }
1951 }
1952
1953 // check that Delta/SrcCoeff >= 0
1954 // really check that NewDelta >= 0
1955 if (SE->isKnownNegative(NewDelta)) {
1956 // No dependence, newDelta < 0
1957 ++WeakZeroSIVindependence;
1958 ++WeakZeroSIVsuccesses;
1959 return true;
1960 }
1961
1962 // if SrcCoeff doesn't divide Delta, then no dependence
1963 if (isa<SCEVConstant>(Delta) &&
1964 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) {
1965 ++WeakZeroSIVindependence;
1966 ++WeakZeroSIVsuccesses;
1967 return true;
1968 }
1969 return false;
1970}
1971
1972// exactRDIVtest - Tests the RDIV subscript pair for dependence.
1973// Things of the form [c1 + a*i] and [c2 + b*j],
1974// where i and j are induction variable, c1 and c2 are loop invariant,
1975// and a and b are constants.
1976// Returns true if any possible dependence is disproved.
1977// Marks the result as inconsistent.
1978// Works in some cases that symbolicRDIVtest doesn't, and vice versa.
1979bool DependenceInfo::exactRDIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
1980 const SCEV *SrcConst, const SCEV *DstConst,
1981 const Loop *SrcLoop, const Loop *DstLoop,
1982 FullDependence &Result) const {
1983 LLVM_DEBUG(dbgs() << "\tExact RDIV test\n");
1984 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n");
1985 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n");
1986 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1987 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1988 ++ExactRDIVapplications;
1989 Result.Consistent = false;
1990 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1991 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1992 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1993 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1994 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1995 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
1996 return false;
1997
1998 // find gcd
1999 APInt G, X, Y;
2000 APInt AM = ConstSrcCoeff->getAPInt();
2001 APInt BM = ConstDstCoeff->getAPInt();
2002 APInt CM = ConstDelta->getAPInt();
2003 unsigned Bits = AM.getBitWidth();
2004 if (findGCD(Bits, AM, BM, CM, G, X, Y)) {
2005 // gcd doesn't divide Delta, no dependence
2006 ++ExactRDIVindependence;
2007 return true;
2008 }
2009
2010 LLVM_DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n");
2011
2012 // since SCEV construction seems to normalize, LM = 0
2013 std::optional<APInt> SrcUM;
2014 // SrcUM is perhaps unavailable, let's check
2015 if (const SCEVConstant *UpperBound =
2016 collectConstantUpperBound(SrcLoop, Delta->getType())) {
2017 SrcUM = UpperBound->getAPInt();
2018 LLVM_DEBUG(dbgs() << "\t SrcUM = " << *SrcUM << "\n");
2019 }
2020
2021 std::optional<APInt> DstUM;
2022 // UM is perhaps unavailable, let's check
2023 if (const SCEVConstant *UpperBound =
2024 collectConstantUpperBound(DstLoop, Delta->getType())) {
2025 DstUM = UpperBound->getAPInt();
2026 LLVM_DEBUG(dbgs() << "\t DstUM = " << *DstUM << "\n");
2027 }
2028
2031 APInt TC = CM.sdiv(G);
2032 APInt TX = X * TC;
2033 APInt TY = Y * TC;
2034 LLVM_DEBUG(dbgs() << "\t TC = " << TC << "\n");
2035 LLVM_DEBUG(dbgs() << "\t TX = " << TX << "\n");
2036 LLVM_DEBUG(dbgs() << "\t TY = " << TY << "\n");
2037
2038 APInt TB = BM.sdiv(G);
2039 APInt TA = AM.sdiv(G);
2040
2041 // At this point, we have the following equations:
2042 //
2043 // TA*i - TB*j = TC
2044 //
2045 // Also, we know that the all pairs of (i, j) can be expressed as:
2046 //
2047 // (TX + k*TB, TY + k*TA)
2048 //
2049 // where k is an arbitrary integer.
2050 auto [TL0, TU0] = inferDomainOfAffine(TB, TX, SrcUM);
2051 auto [TL1, TU1] = inferDomainOfAffine(TA, TY, DstUM);
2052
2053 LLVM_DEBUG(dbgs() << "\t TA = " << TA << "\n");
2054 LLVM_DEBUG(dbgs() << "\t TB = " << TB << "\n");
2055
2056 auto CreateVec = [](const std::optional<APInt> &V0,
2057 const std::optional<APInt> &V1) {
2059 if (V0)
2060 Vec.push_back(*V0);
2061 if (V1)
2062 Vec.push_back(*V1);
2063 return Vec;
2064 };
2065
2066 SmallVector<APInt, 2> TLVec = CreateVec(TL0, TL1);
2067 SmallVector<APInt, 2> TUVec = CreateVec(TU0, TU1);
2068 if (TLVec.empty() || TUVec.empty())
2069 return false;
2070
2071 TL = APIntOps::smax(TLVec.front(), TLVec.back());
2072 TU = APIntOps::smin(TUVec.front(), TUVec.back());
2073 LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n");
2074 LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n");
2075
2076 if (TL.sgt(TU))
2077 ++ExactRDIVindependence;
2078 return TL.sgt(TU);
2079}
2080
2081// symbolicRDIVtest -
2082// In Section 4.5 of the Practical Dependence Testing paper,the authors
2083// introduce a special case of Banerjee's Inequalities (also called the
2084// Extreme-Value Test) that can handle some of the SIV and RDIV cases,
2085// particularly cases with symbolics. Since it's only able to disprove
2086// dependence (not compute distances or directions), we'll use it as a
2087// fall back for the other tests.
2088//
2089// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
2090// where i and j are induction variables and c1 and c2 are loop invariants,
2091// we can use the symbolic tests to disprove some dependences, serving as a
2092// backup for the RDIV test. Note that i and j can be the same variable,
2093// letting this test serve as a backup for the various SIV tests.
2094//
2095// For a dependence to exist, c1 + a1*i must equal c2 + a2*j for some
2096// 0 <= i <= N1 and some 0 <= j <= N2, where N1 and N2 are the (normalized)
2097// loop bounds for the i and j loops, respectively. So, ...
2098//
2099// c1 + a1*i = c2 + a2*j
2100// a1*i - a2*j = c2 - c1
2101//
2102// To test for a dependence, we compute c2 - c1 and make sure it's in the
2103// range of the maximum and minimum possible values of a1*i - a2*j.
2104// Considering the signs of a1 and a2, we have 4 possible cases:
2105//
2106// 1) If a1 >= 0 and a2 >= 0, then
2107// a1*0 - a2*N2 <= c2 - c1 <= a1*N1 - a2*0
2108// -a2*N2 <= c2 - c1 <= a1*N1
2109//
2110// 2) If a1 >= 0 and a2 <= 0, then
2111// a1*0 - a2*0 <= c2 - c1 <= a1*N1 - a2*N2
2112// 0 <= c2 - c1 <= a1*N1 - a2*N2
2113//
2114// 3) If a1 <= 0 and a2 >= 0, then
2115// a1*N1 - a2*N2 <= c2 - c1 <= a1*0 - a2*0
2116// a1*N1 - a2*N2 <= c2 - c1 <= 0
2117//
2118// 4) If a1 <= 0 and a2 <= 0, then
2119// a1*N1 - a2*0 <= c2 - c1 <= a1*0 - a2*N2
2120// a1*N1 <= c2 - c1 <= -a2*N2
2121//
2122// return true if dependence disproved
2123bool DependenceInfo::symbolicRDIVtest(const SCEV *A1, const SCEV *A2,
2124 const SCEV *C1, const SCEV *C2,
2125 const Loop *Loop1,
2126 const Loop *Loop2) const {
2127 ++SymbolicRDIVapplications;
2128 LLVM_DEBUG(dbgs() << "\ttry symbolic RDIV test\n");
2129 LLVM_DEBUG(dbgs() << "\t A1 = " << *A1);
2130 LLVM_DEBUG(dbgs() << ", type = " << *A1->getType() << "\n");
2131 LLVM_DEBUG(dbgs() << "\t A2 = " << *A2 << "\n");
2132 LLVM_DEBUG(dbgs() << "\t C1 = " << *C1 << "\n");
2133 LLVM_DEBUG(dbgs() << "\t C2 = " << *C2 << "\n");
2134 const SCEV *N1 = collectUpperBound(Loop1, A1->getType());
2135 const SCEV *N2 = collectUpperBound(Loop2, A1->getType());
2136 LLVM_DEBUG(if (N1) dbgs() << "\t N1 = " << *N1 << "\n");
2137 LLVM_DEBUG(if (N2) dbgs() << "\t N2 = " << *N2 << "\n");
2138 const SCEV *C2_C1 = SE->getMinusSCEV(C2, C1);
2139 const SCEV *C1_C2 = SE->getMinusSCEV(C1, C2);
2140 LLVM_DEBUG(dbgs() << "\t C2 - C1 = " << *C2_C1 << "\n");
2141 LLVM_DEBUG(dbgs() << "\t C1 - C2 = " << *C1_C2 << "\n");
2142 if (SE->isKnownNonNegative(A1)) {
2143 if (SE->isKnownNonNegative(A2)) {
2144 // A1 >= 0 && A2 >= 0
2145 if (N1) {
2146 // make sure that c2 - c1 <= a1*N1
2147 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2148 LLVM_DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n");
2149 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1)) {
2150 ++SymbolicRDIVindependence;
2151 return true;
2152 }
2153 }
2154 if (N2) {
2155 // make sure that -a2*N2 <= c2 - c1, or a2*N2 >= c1 - c2
2156 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2157 LLVM_DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n");
2158 if (isKnownPredicate(CmpInst::ICMP_SLT, A2N2, C1_C2)) {
2159 ++SymbolicRDIVindependence;
2160 return true;
2161 }
2162 }
2163 } else if (SE->isKnownNonPositive(A2)) {
2164 // a1 >= 0 && a2 <= 0
2165 if (N1 && N2) {
2166 // make sure that c2 - c1 <= a1*N1 - a2*N2
2167 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2168 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2169 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2);
2170 LLVM_DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n");
2171 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1_A2N2)) {
2172 ++SymbolicRDIVindependence;
2173 return true;
2174 }
2175 }
2176 // make sure that 0 <= c2 - c1
2177 if (SE->isKnownNegative(C2_C1)) {
2178 ++SymbolicRDIVindependence;
2179 return true;
2180 }
2181 }
2182 } else if (SE->isKnownNonPositive(A1)) {
2183 if (SE->isKnownNonNegative(A2)) {
2184 // a1 <= 0 && a2 >= 0
2185 if (N1 && N2) {
2186 // make sure that a1*N1 - a2*N2 <= c2 - c1
2187 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2188 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2189 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2);
2190 LLVM_DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n");
2191 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1_A2N2, C2_C1)) {
2192 ++SymbolicRDIVindependence;
2193 return true;
2194 }
2195 }
2196 // make sure that c2 - c1 <= 0
2197 if (SE->isKnownPositive(C2_C1)) {
2198 ++SymbolicRDIVindependence;
2199 return true;
2200 }
2201 } else if (SE->isKnownNonPositive(A2)) {
2202 // a1 <= 0 && a2 <= 0
2203 if (N1) {
2204 // make sure that a1*N1 <= c2 - c1
2205 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2206 LLVM_DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n");
2207 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1, C2_C1)) {
2208 ++SymbolicRDIVindependence;
2209 return true;
2210 }
2211 }
2212 if (N2) {
2213 // make sure that c2 - c1 <= -a2*N2, or c1 - c2 >= a2*N2
2214 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2215 LLVM_DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n");
2216 if (isKnownPredicate(CmpInst::ICMP_SLT, C1_C2, A2N2)) {
2217 ++SymbolicRDIVindependence;
2218 return true;
2219 }
2220 }
2221 }
2222 }
2223 return false;
2224}
2225
2226// testSIV -
2227// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 - a2*i]
2228// where i is an induction variable, c1 and c2 are loop invariant, and a1 and
2229// a2 are constant, we attack it with an SIV test. While they can all be
2230// solved with the Exact SIV test, it's worthwhile to use simpler tests when
2231// they apply; they're cheaper and sometimes more precise.
2232//
2233// Return true if dependence disproved.
2234bool DependenceInfo::testSIV(const SCEV *Src, const SCEV *Dst, unsigned &Level,
2235 FullDependence &Result, Constraint &NewConstraint,
2236 const SCEV *&SplitIter) const {
2237 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
2238 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
2239 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
2240 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
2241 if (SrcAddRec && DstAddRec) {
2242 const SCEV *SrcConst = SrcAddRec->getStart();
2243 const SCEV *DstConst = DstAddRec->getStart();
2244 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2245 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE);
2246 const Loop *CurLoop = SrcAddRec->getLoop();
2247 assert(CurLoop == DstAddRec->getLoop() &&
2248 "both loops in SIV should be same");
2249 Level = mapSrcLoop(CurLoop);
2250 bool disproven;
2251 if (SrcCoeff == DstCoeff)
2252 disproven = strongSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop, Level,
2253 Result, NewConstraint);
2254 else if (SrcCoeff == SE->getNegativeSCEV(DstCoeff))
2255 disproven = weakCrossingSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop,
2256 Level, Result, NewConstraint, SplitIter);
2257 else
2258 disproven = exactSIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurLoop,
2259 Level, Result, NewConstraint);
2260 return disproven || gcdMIVtest(Src, Dst, Result) ||
2261 symbolicRDIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurLoop,
2262 CurLoop);
2263 }
2264 if (SrcAddRec) {
2265 const SCEV *SrcConst = SrcAddRec->getStart();
2266 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2267 const SCEV *DstConst = Dst;
2268 const Loop *CurLoop = SrcAddRec->getLoop();
2269 Level = mapSrcLoop(CurLoop);
2270 return weakZeroDstSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop, Level,
2271 Result, NewConstraint) ||
2272 gcdMIVtest(Src, Dst, Result);
2273 }
2274 if (DstAddRec) {
2275 const SCEV *DstConst = DstAddRec->getStart();
2276 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE);
2277 const SCEV *SrcConst = Src;
2278 const Loop *CurLoop = DstAddRec->getLoop();
2279 Level = mapDstLoop(CurLoop);
2280 return weakZeroSrcSIVtest(DstCoeff, SrcConst, DstConst, CurLoop, Level,
2281 Result, NewConstraint) ||
2282 gcdMIVtest(Src, Dst, Result);
2283 }
2284 llvm_unreachable("SIV test expected at least one AddRec");
2285 return false;
2286}
2287
2288// testRDIV -
2289// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
2290// where i and j are induction variables, c1 and c2 are loop invariant,
2291// and a1 and a2 are constant, we can solve it exactly with an easy adaptation
2292// of the Exact SIV test, the Restricted Double Index Variable (RDIV) test.
2293// It doesn't make sense to talk about distance or direction in this case,
2294// so there's no point in making special versions of the Strong SIV test or
2295// the Weak-crossing SIV test.
2296//
2297// With minor algebra, this test can also be used for things like
2298// [c1 + a1*i + a2*j][c2].
2299//
2300// Return true if dependence disproved.
2301bool DependenceInfo::testRDIV(const SCEV *Src, const SCEV *Dst,
2302 FullDependence &Result) const {
2303 // we have 3 possible situations here:
2304 // 1) [a*i + b] and [c*j + d]
2305 // 2) [a*i + c*j + b] and [d]
2306 // 3) [b] and [a*i + c*j + d]
2307 // We need to find what we've got and get organized
2308
2309 const SCEV *SrcConst, *DstConst;
2310 const SCEV *SrcCoeff, *DstCoeff;
2311 const Loop *SrcLoop, *DstLoop;
2312
2313 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
2314 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
2315 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
2316 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
2317 if (SrcAddRec && DstAddRec) {
2318 SrcConst = SrcAddRec->getStart();
2319 SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2320 SrcLoop = SrcAddRec->getLoop();
2321 DstConst = DstAddRec->getStart();
2322 DstCoeff = DstAddRec->getStepRecurrence(*SE);
2323 DstLoop = DstAddRec->getLoop();
2324 } else if (SrcAddRec) {
2325 if (const SCEVAddRecExpr *tmpAddRec =
2326 dyn_cast<SCEVAddRecExpr>(SrcAddRec->getStart())) {
2327 SrcConst = tmpAddRec->getStart();
2328 SrcCoeff = tmpAddRec->getStepRecurrence(*SE);
2329 SrcLoop = tmpAddRec->getLoop();
2330 DstConst = Dst;
2331 DstCoeff = SE->getNegativeSCEV(SrcAddRec->getStepRecurrence(*SE));
2332 DstLoop = SrcAddRec->getLoop();
2333 } else
2334 llvm_unreachable("RDIV reached by surprising SCEVs");
2335 } else if (DstAddRec) {
2336 if (const SCEVAddRecExpr *tmpAddRec =
2337 dyn_cast<SCEVAddRecExpr>(DstAddRec->getStart())) {
2338 DstConst = tmpAddRec->getStart();
2339 DstCoeff = tmpAddRec->getStepRecurrence(*SE);
2340 DstLoop = tmpAddRec->getLoop();
2341 SrcConst = Src;
2342 SrcCoeff = SE->getNegativeSCEV(DstAddRec->getStepRecurrence(*SE));
2343 SrcLoop = DstAddRec->getLoop();
2344 } else
2345 llvm_unreachable("RDIV reached by surprising SCEVs");
2346 } else
2347 llvm_unreachable("RDIV expected at least one AddRec");
2348 return exactRDIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, SrcLoop, DstLoop,
2349 Result) ||
2350 gcdMIVtest(Src, Dst, Result) ||
2351 symbolicRDIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, SrcLoop,
2352 DstLoop);
2353}
2354
2355// Tests the single-subscript MIV pair (Src and Dst) for dependence.
2356// Return true if dependence disproved.
2357// Can sometimes refine direction vectors.
2358bool DependenceInfo::testMIV(const SCEV *Src, const SCEV *Dst,
2359 const SmallBitVector &Loops,
2360 FullDependence &Result) const {
2361 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
2362 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
2363 Result.Consistent = false;
2364 return gcdMIVtest(Src, Dst, Result) ||
2365 banerjeeMIVtest(Src, Dst, Loops, Result);
2366}
2367
2368// Given a product, e.g., 10*X*Y, returns the first constant operand,
2369// in this case 10. If there is no constant part, returns std::nullopt.
2370static std::optional<APInt> getConstantPart(const SCEV *Expr) {
2371 if (const auto *Constant = dyn_cast<SCEVConstant>(Expr))
2372 return Constant->getAPInt();
2373 if (const auto *Product = dyn_cast<SCEVMulExpr>(Expr))
2374 if (const auto *Constant = dyn_cast<SCEVConstant>(Product->getOperand(0)))
2375 return Constant->getAPInt();
2376 return std::nullopt;
2377}
2378
2379bool DependenceInfo::accumulateCoefficientsGCD(const SCEV *Expr,
2380 const Loop *CurLoop,
2381 const SCEV *&CurLoopCoeff,
2382 APInt &RunningGCD) const {
2383 // If RunningGCD is already 1, exit early.
2384 // TODO: It might be better to continue the recursion to find CurLoopCoeff.
2385 if (RunningGCD == 1)
2386 return true;
2387
2388 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
2389 if (!AddRec) {
2390 assert(isLoopInvariant(Expr, CurLoop) &&
2391 "Expected loop invariant expression");
2392 return true;
2393 }
2394
2395 assert(AddRec->isAffine() && "Unexpected Expr");
2396 const SCEV *Start = AddRec->getStart();
2397 const SCEV *Step = AddRec->getStepRecurrence(*SE);
2398 if (AddRec->getLoop() == CurLoop) {
2399 CurLoopCoeff = Step;
2400 } else {
2401 std::optional<APInt> ConstCoeff = getConstantPart(Step);
2402
2403 // If the coefficient is the product of a constant and other stuff, we can
2404 // use the constant in the GCD computation.
2405 if (!ConstCoeff)
2406 return false;
2407
2408 // TODO: What happens if ConstCoeff is the "most negative" signed number
2409 // (e.g. -128 for 8 bit wide APInt)?
2410 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
2411 }
2412
2413 return accumulateCoefficientsGCD(Start, CurLoop, CurLoopCoeff, RunningGCD);
2414}
2415
2416//===----------------------------------------------------------------------===//
2417// gcdMIVtest -
2418// Tests an MIV subscript pair for dependence.
2419// Returns true if any possible dependence is disproved.
2420// Marks the result as inconsistent.
2421// Can sometimes disprove the equal direction for 1 or more loops,
2422// as discussed in Michael Wolfe's book,
2423// High Performance Compilers for Parallel Computing, page 235.
2424//
2425// We spend some effort (code!) to handle cases like
2426// [10*i + 5*N*j + 15*M + 6], where i and j are induction variables,
2427// but M and N are just loop-invariant variables.
2428// This should help us handle linearized subscripts;
2429// also makes this test a useful backup to the various SIV tests.
2430//
2431// It occurs to me that the presence of loop-invariant variables
2432// changes the nature of the test from "greatest common divisor"
2433// to "a common divisor".
2434bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst,
2435 FullDependence &Result) const {
2436 LLVM_DEBUG(dbgs() << "starting gcd\n");
2437 ++GCDapplications;
2438 unsigned BitWidth = SE->getTypeSizeInBits(Src->getType());
2439 APInt RunningGCD = APInt::getZero(BitWidth);
2440
2441 // Examine Src coefficients.
2442 // Compute running GCD and record source constant.
2443 // Because we're looking for the constant at the end of the chain,
2444 // we can't quit the loop just because the GCD == 1.
2445 const SCEV *Coefficients = Src;
2446 while (const SCEVAddRecExpr *AddRec =
2447 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2448 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2449 // If the coefficient is the product of a constant and other stuff,
2450 // we can use the constant in the GCD computation.
2451 std::optional<APInt> ConstCoeff = getConstantPart(Coeff);
2452 if (!ConstCoeff)
2453 return false;
2454 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
2455 Coefficients = AddRec->getStart();
2456 }
2457 const SCEV *SrcConst = Coefficients;
2458
2459 // Examine Dst coefficients.
2460 // Compute running GCD and record destination constant.
2461 // Because we're looking for the constant at the end of the chain,
2462 // we can't quit the loop just because the GCD == 1.
2463 Coefficients = Dst;
2464 while (const SCEVAddRecExpr *AddRec =
2465 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2466 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2467 // If the coefficient is the product of a constant and other stuff,
2468 // we can use the constant in the GCD computation.
2469 std::optional<APInt> ConstCoeff = getConstantPart(Coeff);
2470 if (!ConstCoeff)
2471 return false;
2472 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
2473 Coefficients = AddRec->getStart();
2474 }
2475 const SCEV *DstConst = Coefficients;
2476
2477 APInt ExtraGCD = APInt::getZero(BitWidth);
2478 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
2479 LLVM_DEBUG(dbgs() << " Delta = " << *Delta << "\n");
2480 const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Delta);
2481 if (const SCEVAddExpr *Sum = dyn_cast<SCEVAddExpr>(Delta)) {
2482 // If Delta is a sum of products, we may be able to make further progress.
2483 for (const SCEV *Operand : Sum->operands()) {
2484 if (isa<SCEVConstant>(Operand)) {
2485 assert(!Constant && "Surprised to find multiple constants");
2486 Constant = cast<SCEVConstant>(Operand);
2487 } else if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Operand)) {
2488 // Search for constant operand to participate in GCD;
2489 // If none found; return false.
2490 std::optional<APInt> ConstOp = getConstantPart(Product);
2491 if (!ConstOp)
2492 return false;
2493 ExtraGCD = APIntOps::GreatestCommonDivisor(ExtraGCD, ConstOp->abs());
2494 } else
2495 return false;
2496 }
2497 }
2498 if (!Constant)
2499 return false;
2500 APInt ConstDelta = cast<SCEVConstant>(Constant)->getAPInt();
2501 LLVM_DEBUG(dbgs() << " ConstDelta = " << ConstDelta << "\n");
2502 if (ConstDelta == 0)
2503 return false;
2504 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ExtraGCD);
2505 LLVM_DEBUG(dbgs() << " RunningGCD = " << RunningGCD << "\n");
2506 APInt Remainder = ConstDelta.srem(RunningGCD);
2507 if (Remainder != 0) {
2508 ++GCDindependence;
2509 return true;
2510 }
2511
2512 // Try to disprove equal directions.
2513 // For example, given a subscript pair [3*i + 2*j] and [i' + 2*j' - 1],
2514 // the code above can't disprove the dependence because the GCD = 1.
2515 // So we consider what happen if i = i' and what happens if j = j'.
2516 // If i = i', we can simplify the subscript to [2*i + 2*j] and [2*j' - 1],
2517 // which is infeasible, so we can disallow the = direction for the i level.
2518 // Setting j = j' doesn't help matters, so we end up with a direction vector
2519 // of [<>, *]
2520 //
2521 // Given A[5*i + 10*j*M + 9*M*N] and A[15*i + 20*j*M - 21*N*M + 5],
2522 // we need to remember that the constant part is 5 and the RunningGCD should
2523 // be initialized to ExtraGCD = 30.
2524 LLVM_DEBUG(dbgs() << " ExtraGCD = " << ExtraGCD << '\n');
2525
2526 bool Improved = false;
2527 Coefficients = Src;
2528 while (const SCEVAddRecExpr *AddRec =
2529 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2530 Coefficients = AddRec->getStart();
2531 const Loop *CurLoop = AddRec->getLoop();
2532 RunningGCD = ExtraGCD;
2533 const SCEV *SrcCoeff = AddRec->getStepRecurrence(*SE);
2534 const SCEV *DstCoeff = SE->getMinusSCEV(SrcCoeff, SrcCoeff);
2535
2536 if (!accumulateCoefficientsGCD(Src, CurLoop, SrcCoeff, RunningGCD) ||
2537 !accumulateCoefficientsGCD(Dst, CurLoop, DstCoeff, RunningGCD))
2538 return false;
2539
2540 Delta = SE->getMinusSCEV(SrcCoeff, DstCoeff);
2541 // If the coefficient is the product of a constant and other stuff,
2542 // we can use the constant in the GCD computation.
2543 std::optional<APInt> ConstCoeff = getConstantPart(Delta);
2544 if (!ConstCoeff)
2545 // The difference of the two coefficients might not be a product
2546 // or constant, in which case we give up on this direction.
2547 continue;
2548 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
2549 LLVM_DEBUG(dbgs() << "\tRunningGCD = " << RunningGCD << "\n");
2550 if (RunningGCD != 0) {
2551 Remainder = ConstDelta.srem(RunningGCD);
2552 LLVM_DEBUG(dbgs() << "\tRemainder = " << Remainder << "\n");
2553 if (Remainder != 0) {
2554 unsigned Level = mapSrcLoop(CurLoop);
2555 Result.DV[Level - 1].Direction &= ~Dependence::DVEntry::EQ;
2556 Improved = true;
2557 }
2558 }
2559 }
2560 if (Improved)
2561 ++GCDsuccesses;
2562 LLVM_DEBUG(dbgs() << "all done\n");
2563 return false;
2564}
2565
2566//===----------------------------------------------------------------------===//
2567// banerjeeMIVtest -
2568// Use Banerjee's Inequalities to test an MIV subscript pair.
2569// (Wolfe, in the race-car book, calls this the Extreme Value Test.)
2570// Generally follows the discussion in Section 2.5.2 of
2571//
2572// Optimizing Supercompilers for Supercomputers
2573// Michael Wolfe
2574//
2575// The inequalities given on page 25 are simplified in that loops are
2576// normalized so that the lower bound is always 0 and the stride is always 1.
2577// For example, Wolfe gives
2578//
2579// LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2580//
2581// where A_k is the coefficient of the kth index in the source subscript,
2582// B_k is the coefficient of the kth index in the destination subscript,
2583// U_k is the upper bound of the kth index, L_k is the lower bound of the Kth
2584// index, and N_k is the stride of the kth index. Since all loops are normalized
2585// by the SCEV package, N_k = 1 and L_k = 0, allowing us to simplify the
2586// equation to
2587//
2588// LB^<_k = (A^-_k - B_k)^- (U_k - 0 - 1) + (A_k - B_k)0 - B_k 1
2589// = (A^-_k - B_k)^- (U_k - 1) - B_k
2590//
2591// Similar simplifications are possible for the other equations.
2592//
2593// When we can't determine the number of iterations for a loop,
2594// we use NULL as an indicator for the worst case, infinity.
2595// When computing the upper bound, NULL denotes +inf;
2596// for the lower bound, NULL denotes -inf.
2597//
2598// Return true if dependence disproved.
2599bool DependenceInfo::banerjeeMIVtest(const SCEV *Src, const SCEV *Dst,
2600 const SmallBitVector &Loops,
2601 FullDependence &Result) const {
2602 LLVM_DEBUG(dbgs() << "starting Banerjee\n");
2603 ++BanerjeeApplications;
2604 LLVM_DEBUG(dbgs() << " Src = " << *Src << '\n');
2605 const SCEV *A0;
2606 CoefficientInfo *A = collectCoeffInfo(Src, true, A0);
2607 LLVM_DEBUG(dbgs() << " Dst = " << *Dst << '\n');
2608 const SCEV *B0;
2609 CoefficientInfo *B = collectCoeffInfo(Dst, false, B0);
2610 BoundInfo *Bound = new BoundInfo[MaxLevels + 1];
2611 const SCEV *Delta = SE->getMinusSCEV(B0, A0);
2612 LLVM_DEBUG(dbgs() << "\tDelta = " << *Delta << '\n');
2613
2614 // Compute bounds for all the * directions.
2615 LLVM_DEBUG(dbgs() << "\tBounds[*]\n");
2616 for (unsigned K = 1; K <= MaxLevels; ++K) {
2617 Bound[K].Iterations = A[K].Iterations ? A[K].Iterations : B[K].Iterations;
2618 Bound[K].Direction = Dependence::DVEntry::ALL;
2619 Bound[K].DirSet = Dependence::DVEntry::NONE;
2620 findBoundsALL(A, B, Bound, K);
2621#ifndef NDEBUG
2622 LLVM_DEBUG(dbgs() << "\t " << K << '\t');
2623 if (Bound[K].Lower[Dependence::DVEntry::ALL])
2624 LLVM_DEBUG(dbgs() << *Bound[K].Lower[Dependence::DVEntry::ALL] << '\t');
2625 else
2626 LLVM_DEBUG(dbgs() << "-inf\t");
2627 if (Bound[K].Upper[Dependence::DVEntry::ALL])
2628 LLVM_DEBUG(dbgs() << *Bound[K].Upper[Dependence::DVEntry::ALL] << '\n');
2629 else
2630 LLVM_DEBUG(dbgs() << "+inf\n");
2631#endif
2632 }
2633
2634 // Test the *, *, *, ... case.
2635 bool Disproved = false;
2636 if (testBounds(Dependence::DVEntry::ALL, 0, Bound, Delta)) {
2637 // Explore the direction vector hierarchy.
2638 unsigned DepthExpanded = 0;
2639 unsigned NewDeps =
2640 exploreDirections(1, A, B, Bound, Loops, DepthExpanded, Delta);
2641 if (NewDeps > 0) {
2642 bool Improved = false;
2643 for (unsigned K = 1; K <= CommonLevels; ++K) {
2644 if (Loops[K]) {
2645 unsigned Old = Result.DV[K - 1].Direction;
2646 Result.DV[K - 1].Direction = Old & Bound[K].DirSet;
2647 Improved |= Old != Result.DV[K - 1].Direction;
2648 if (!Result.DV[K - 1].Direction) {
2649 Improved = false;
2650 Disproved = true;
2651 break;
2652 }
2653 }
2654 }
2655 if (Improved)
2656 ++BanerjeeSuccesses;
2657 } else {
2658 ++BanerjeeIndependence;
2659 Disproved = true;
2660 }
2661 } else {
2662 ++BanerjeeIndependence;
2663 Disproved = true;
2664 }
2665 delete[] Bound;
2666 delete[] A;
2667 delete[] B;
2668 return Disproved;
2669}
2670
2671// Hierarchically expands the direction vector
2672// search space, combining the directions of discovered dependences
2673// in the DirSet field of Bound. Returns the number of distinct
2674// dependences discovered. If the dependence is disproved,
2675// it will return 0.
2676unsigned DependenceInfo::exploreDirections(unsigned Level, CoefficientInfo *A,
2677 CoefficientInfo *B, BoundInfo *Bound,
2678 const SmallBitVector &Loops,
2679 unsigned &DepthExpanded,
2680 const SCEV *Delta) const {
2681 // This algorithm has worst case complexity of O(3^n), where 'n' is the number
2682 // of common loop levels. To avoid excessive compile-time, pessimize all the
2683 // results and immediately return when the number of common levels is beyond
2684 // the given threshold.
2685 if (CommonLevels > MIVMaxLevelThreshold) {
2686 LLVM_DEBUG(dbgs() << "Number of common levels exceeded the threshold. MIV "
2687 "direction exploration is terminated.\n");
2688 for (unsigned K = 1; K <= CommonLevels; ++K)
2689 if (Loops[K])
2690 Bound[K].DirSet = Dependence::DVEntry::ALL;
2691 return 1;
2692 }
2693
2694 if (Level > CommonLevels) {
2695 // record result
2696 LLVM_DEBUG(dbgs() << "\t[");
2697 for (unsigned K = 1; K <= CommonLevels; ++K) {
2698 if (Loops[K]) {
2699 Bound[K].DirSet |= Bound[K].Direction;
2700#ifndef NDEBUG
2701 switch (Bound[K].Direction) {
2703 LLVM_DEBUG(dbgs() << " <");
2704 break;
2706 LLVM_DEBUG(dbgs() << " =");
2707 break;
2709 LLVM_DEBUG(dbgs() << " >");
2710 break;
2712 LLVM_DEBUG(dbgs() << " *");
2713 break;
2714 default:
2715 llvm_unreachable("unexpected Bound[K].Direction");
2716 }
2717#endif
2718 }
2719 }
2720 LLVM_DEBUG(dbgs() << " ]\n");
2721 return 1;
2722 }
2723 if (Loops[Level]) {
2724 if (Level > DepthExpanded) {
2725 DepthExpanded = Level;
2726 // compute bounds for <, =, > at current level
2727 findBoundsLT(A, B, Bound, Level);
2728 findBoundsGT(A, B, Bound, Level);
2729 findBoundsEQ(A, B, Bound, Level);
2730#ifndef NDEBUG
2731 LLVM_DEBUG(dbgs() << "\tBound for level = " << Level << '\n');
2732 LLVM_DEBUG(dbgs() << "\t <\t");
2733 if (Bound[Level].Lower[Dependence::DVEntry::LT])
2734 LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::LT]
2735 << '\t');
2736 else
2737 LLVM_DEBUG(dbgs() << "-inf\t");
2738 if (Bound[Level].Upper[Dependence::DVEntry::LT])
2739 LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::LT]
2740 << '\n');
2741 else
2742 LLVM_DEBUG(dbgs() << "+inf\n");
2743 LLVM_DEBUG(dbgs() << "\t =\t");
2744 if (Bound[Level].Lower[Dependence::DVEntry::EQ])
2745 LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::EQ]
2746 << '\t');
2747 else
2748 LLVM_DEBUG(dbgs() << "-inf\t");
2749 if (Bound[Level].Upper[Dependence::DVEntry::EQ])
2750 LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::EQ]
2751 << '\n');
2752 else
2753 LLVM_DEBUG(dbgs() << "+inf\n");
2754 LLVM_DEBUG(dbgs() << "\t >\t");
2755 if (Bound[Level].Lower[Dependence::DVEntry::GT])
2756 LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::GT]
2757 << '\t');
2758 else
2759 LLVM_DEBUG(dbgs() << "-inf\t");
2760 if (Bound[Level].Upper[Dependence::DVEntry::GT])
2761 LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::GT]
2762 << '\n');
2763 else
2764 LLVM_DEBUG(dbgs() << "+inf\n");
2765#endif
2766 }
2767
2768 unsigned NewDeps = 0;
2769
2770 // test bounds for <, *, *, ...
2771 if (testBounds(Dependence::DVEntry::LT, Level, Bound, Delta))
2772 NewDeps += exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
2773 Delta);
2774
2775 // Test bounds for =, *, *, ...
2776 if (testBounds(Dependence::DVEntry::EQ, Level, Bound, Delta))
2777 NewDeps += exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
2778 Delta);
2779
2780 // test bounds for >, *, *, ...
2781 if (testBounds(Dependence::DVEntry::GT, Level, Bound, Delta))
2782 NewDeps += exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
2783 Delta);
2784
2785 Bound[Level].Direction = Dependence::DVEntry::ALL;
2786 return NewDeps;
2787 } else
2788 return exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
2789 Delta);
2790}
2791
2792// Returns true iff the current bounds are plausible.
2793bool DependenceInfo::testBounds(unsigned char DirKind, unsigned Level,
2794 BoundInfo *Bound, const SCEV *Delta) const {
2795 Bound[Level].Direction = DirKind;
2796 if (const SCEV *LowerBound = getLowerBound(Bound))
2797 if (isKnownPredicate(CmpInst::ICMP_SGT, LowerBound, Delta))
2798 return false;
2799 if (const SCEV *UpperBound = getUpperBound(Bound))
2800 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, UpperBound))
2801 return false;
2802 return true;
2803}
2804
2805// Computes the upper and lower bounds for level K
2806// using the * direction. Records them in Bound.
2807// Wolfe gives the equations
2808//
2809// LB^*_k = (A^-_k - B^+_k)(U_k - L_k) + (A_k - B_k)L_k
2810// UB^*_k = (A^+_k - B^-_k)(U_k - L_k) + (A_k - B_k)L_k
2811//
2812// Since we normalize loops, we can simplify these equations to
2813//
2814// LB^*_k = (A^-_k - B^+_k)U_k
2815// UB^*_k = (A^+_k - B^-_k)U_k
2816//
2817// We must be careful to handle the case where the upper bound is unknown.
2818// Note that the lower bound is always <= 0
2819// and the upper bound is always >= 0.
2820void DependenceInfo::findBoundsALL(CoefficientInfo *A, CoefficientInfo *B,
2821 BoundInfo *Bound, unsigned K) const {
2822 Bound[K].Lower[Dependence::DVEntry::ALL] =
2823 nullptr; // Default value = -infinity.
2824 Bound[K].Upper[Dependence::DVEntry::ALL] =
2825 nullptr; // Default value = +infinity.
2826 if (Bound[K].Iterations) {
2827 Bound[K].Lower[Dependence::DVEntry::ALL] = SE->getMulExpr(
2828 SE->getMinusSCEV(A[K].NegPart, B[K].PosPart), Bound[K].Iterations);
2829 Bound[K].Upper[Dependence::DVEntry::ALL] = SE->getMulExpr(
2830 SE->getMinusSCEV(A[K].PosPart, B[K].NegPart), Bound[K].Iterations);
2831 } else {
2832 // If the difference is 0, we won't need to know the number of iterations.
2833 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].NegPart, B[K].PosPart))
2834 Bound[K].Lower[Dependence::DVEntry::ALL] =
2835 SE->getZero(A[K].Coeff->getType());
2836 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].PosPart, B[K].NegPart))
2837 Bound[K].Upper[Dependence::DVEntry::ALL] =
2838 SE->getZero(A[K].Coeff->getType());
2839 }
2840}
2841
2842// Computes the upper and lower bounds for level K
2843// using the = direction. Records them in Bound.
2844// Wolfe gives the equations
2845//
2846// LB^=_k = (A_k - B_k)^- (U_k - L_k) + (A_k - B_k)L_k
2847// UB^=_k = (A_k - B_k)^+ (U_k - L_k) + (A_k - B_k)L_k
2848//
2849// Since we normalize loops, we can simplify these equations to
2850//
2851// LB^=_k = (A_k - B_k)^- U_k
2852// UB^=_k = (A_k - B_k)^+ U_k
2853//
2854// We must be careful to handle the case where the upper bound is unknown.
2855// Note that the lower bound is always <= 0
2856// and the upper bound is always >= 0.
2857void DependenceInfo::findBoundsEQ(CoefficientInfo *A, CoefficientInfo *B,
2858 BoundInfo *Bound, unsigned K) const {
2859 Bound[K].Lower[Dependence::DVEntry::EQ] =
2860 nullptr; // Default value = -infinity.
2861 Bound[K].Upper[Dependence::DVEntry::EQ] =
2862 nullptr; // Default value = +infinity.
2863 if (Bound[K].Iterations) {
2864 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
2865 const SCEV *NegativePart = getNegativePart(Delta);
2866 Bound[K].Lower[Dependence::DVEntry::EQ] =
2867 SE->getMulExpr(NegativePart, Bound[K].Iterations);
2868 const SCEV *PositivePart = getPositivePart(Delta);
2869 Bound[K].Upper[Dependence::DVEntry::EQ] =
2870 SE->getMulExpr(PositivePart, Bound[K].Iterations);
2871 } else {
2872 // If the positive/negative part of the difference is 0,
2873 // we won't need to know the number of iterations.
2874 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
2875 const SCEV *NegativePart = getNegativePart(Delta);
2876 if (NegativePart->isZero())
2877 Bound[K].Lower[Dependence::DVEntry::EQ] = NegativePart; // Zero
2878 const SCEV *PositivePart = getPositivePart(Delta);
2879 if (PositivePart->isZero())
2880 Bound[K].Upper[Dependence::DVEntry::EQ] = PositivePart; // Zero
2881 }
2882}
2883
2884// Computes the upper and lower bounds for level K
2885// using the < direction. Records them in Bound.
2886// Wolfe gives the equations
2887//
2888// LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2889// UB^<_k = (A^+_k - B_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2890//
2891// Since we normalize loops, we can simplify these equations to
2892//
2893// LB^<_k = (A^-_k - B_k)^- (U_k - 1) - B_k
2894// UB^<_k = (A^+_k - B_k)^+ (U_k - 1) - B_k
2895//
2896// We must be careful to handle the case where the upper bound is unknown.
2897void DependenceInfo::findBoundsLT(CoefficientInfo *A, CoefficientInfo *B,
2898 BoundInfo *Bound, unsigned K) const {
2899 Bound[K].Lower[Dependence::DVEntry::LT] =
2900 nullptr; // Default value = -infinity.
2901 Bound[K].Upper[Dependence::DVEntry::LT] =
2902 nullptr; // Default value = +infinity.
2903 if (Bound[K].Iterations) {
2904 const SCEV *Iter_1 = SE->getMinusSCEV(
2905 Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
2906 const SCEV *NegPart =
2907 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
2908 Bound[K].Lower[Dependence::DVEntry::LT] =
2909 SE->getMinusSCEV(SE->getMulExpr(NegPart, Iter_1), B[K].Coeff);
2910 const SCEV *PosPart =
2911 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
2912 Bound[K].Upper[Dependence::DVEntry::LT] =
2913 SE->getMinusSCEV(SE->getMulExpr(PosPart, Iter_1), B[K].Coeff);
2914 } else {
2915 // If the positive/negative part of the difference is 0,
2916 // we won't need to know the number of iterations.
2917 const SCEV *NegPart =
2918 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
2919 if (NegPart->isZero())
2920 Bound[K].Lower[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
2921 const SCEV *PosPart =
2922 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
2923 if (PosPart->isZero())
2924 Bound[K].Upper[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
2925 }
2926}
2927
2928// Computes the upper and lower bounds for level K
2929// using the > direction. Records them in Bound.
2930// Wolfe gives the equations
2931//
2932// LB^>_k = (A_k - B^+_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
2933// UB^>_k = (A_k - B^-_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
2934//
2935// Since we normalize loops, we can simplify these equations to
2936//
2937// LB^>_k = (A_k - B^+_k)^- (U_k - 1) + A_k
2938// UB^>_k = (A_k - B^-_k)^+ (U_k - 1) + A_k
2939//
2940// We must be careful to handle the case where the upper bound is unknown.
2941void DependenceInfo::findBoundsGT(CoefficientInfo *A, CoefficientInfo *B,
2942 BoundInfo *Bound, unsigned K) const {
2943 Bound[K].Lower[Dependence::DVEntry::GT] =
2944 nullptr; // Default value = -infinity.
2945 Bound[K].Upper[Dependence::DVEntry::GT] =
2946 nullptr; // Default value = +infinity.
2947 if (Bound[K].Iterations) {
2948 const SCEV *Iter_1 = SE->getMinusSCEV(
2949 Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
2950 const SCEV *NegPart =
2951 getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
2952 Bound[K].Lower[Dependence::DVEntry::GT] =
2953 SE->getAddExpr(SE->getMulExpr(NegPart, Iter_1), A[K].Coeff);
2954 const SCEV *PosPart =
2955 getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
2956 Bound[K].Upper[Dependence::DVEntry::GT] =
2957 SE->getAddExpr(SE->getMulExpr(PosPart, Iter_1), A[K].Coeff);
2958 } else {
2959 // If the positive/negative part of the difference is 0,
2960 // we won't need to know the number of iterations.
2961 const SCEV *NegPart =
2962 getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
2963 if (NegPart->isZero())
2964 Bound[K].Lower[Dependence::DVEntry::GT] = A[K].Coeff;
2965 const SCEV *PosPart =
2966 getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
2967 if (PosPart->isZero())
2968 Bound[K].Upper[Dependence::DVEntry::GT] = A[K].Coeff;
2969 }
2970}
2971
2972// X^+ = max(X, 0)
2973const SCEV *DependenceInfo::getPositivePart(const SCEV *X) const {
2974 return SE->getSMaxExpr(X, SE->getZero(X->getType()));
2975}
2976
2977// X^- = min(X, 0)
2978const SCEV *DependenceInfo::getNegativePart(const SCEV *X) const {
2979 return SE->getSMinExpr(X, SE->getZero(X->getType()));
2980}
2981
2982// Walks through the subscript,
2983// collecting each coefficient, the associated loop bounds,
2984// and recording its positive and negative parts for later use.
2985DependenceInfo::CoefficientInfo *
2986DependenceInfo::collectCoeffInfo(const SCEV *Subscript, bool SrcFlag,
2987 const SCEV *&Constant) const {
2988 const SCEV *Zero = SE->getZero(Subscript->getType());
2989 CoefficientInfo *CI = new CoefficientInfo[MaxLevels + 1];
2990 for (unsigned K = 1; K <= MaxLevels; ++K) {
2991 CI[K].Coeff = Zero;
2992 CI[K].PosPart = Zero;
2993 CI[K].NegPart = Zero;
2994 CI[K].Iterations = nullptr;
2995 }
2996 while (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Subscript)) {
2997 const Loop *L = AddRec->getLoop();
2998 unsigned K = SrcFlag ? mapSrcLoop(L) : mapDstLoop(L);
2999 CI[K].Coeff = AddRec->getStepRecurrence(*SE);
3000 CI[K].PosPart = getPositivePart(CI[K].Coeff);
3001 CI[K].NegPart = getNegativePart(CI[K].Coeff);
3002 CI[K].Iterations = collectUpperBound(L, Subscript->getType());
3003 Subscript = AddRec->getStart();
3004 }
3005 Constant = Subscript;
3006#ifndef NDEBUG
3007 LLVM_DEBUG(dbgs() << "\tCoefficient Info\n");
3008 for (unsigned K = 1; K <= MaxLevels; ++K) {
3009 LLVM_DEBUG(dbgs() << "\t " << K << "\t" << *CI[K].Coeff);
3010 LLVM_DEBUG(dbgs() << "\tPos Part = ");
3011 LLVM_DEBUG(dbgs() << *CI[K].PosPart);
3012 LLVM_DEBUG(dbgs() << "\tNeg Part = ");
3013 LLVM_DEBUG(dbgs() << *CI[K].NegPart);
3014 LLVM_DEBUG(dbgs() << "\tUpper Bound = ");
3015 if (CI[K].Iterations)
3016 LLVM_DEBUG(dbgs() << *CI[K].Iterations);
3017 else
3018 LLVM_DEBUG(dbgs() << "+inf");
3019 LLVM_DEBUG(dbgs() << '\n');
3020 }
3021 LLVM_DEBUG(dbgs() << "\t Constant = " << *Subscript << '\n');
3022#endif
3023 return CI;
3024}
3025
3026// Looks through all the bounds info and
3027// computes the lower bound given the current direction settings
3028// at each level. If the lower bound for any level is -inf,
3029// the result is -inf.
3030const SCEV *DependenceInfo::getLowerBound(BoundInfo *Bound) const {
3031 const SCEV *Sum = Bound[1].Lower[Bound[1].Direction];
3032 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
3033 if (Bound[K].Lower[Bound[K].Direction])
3034 Sum = SE->getAddExpr(Sum, Bound[K].Lower[Bound[K].Direction]);
3035 else
3036 Sum = nullptr;
3037 }
3038 return Sum;
3039}
3040
3041// Looks through all the bounds info and
3042// computes the upper bound given the current direction settings
3043// at each level. If the upper bound at any level is +inf,
3044// the result is +inf.
3045const SCEV *DependenceInfo::getUpperBound(BoundInfo *Bound) const {
3046 const SCEV *Sum = Bound[1].Upper[Bound[1].Direction];
3047 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
3048 if (Bound[K].Upper[Bound[K].Direction])
3049 Sum = SE->getAddExpr(Sum, Bound[K].Upper[Bound[K].Direction]);
3050 else
3051 Sum = nullptr;
3052 }
3053 return Sum;
3054}
3055
3056//===----------------------------------------------------------------------===//
3057// Constraint manipulation for Delta test.
3058
3059// Given a linear SCEV,
3060// return the coefficient (the step)
3061// corresponding to the specified loop.
3062// If there isn't one, return 0.
3063// For example, given a*i + b*j + c*k, finding the coefficient
3064// corresponding to the j loop would yield b.
3065const SCEV *DependenceInfo::findCoefficient(const SCEV *Expr,
3066 const Loop *TargetLoop) const {
3067 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
3068 if (!AddRec)
3069 return SE->getZero(Expr->getType());
3070 if (AddRec->getLoop() == TargetLoop)
3071 return AddRec->getStepRecurrence(*SE);
3072 return findCoefficient(AddRec->getStart(), TargetLoop);
3073}
3074
3075// Given a linear SCEV,
3076// return the SCEV given by zeroing out the coefficient
3077// corresponding to the specified loop.
3078// For example, given a*i + b*j + c*k, zeroing the coefficient
3079// corresponding to the j loop would yield a*i + c*k.
3080const SCEV *DependenceInfo::zeroCoefficient(const SCEV *Expr,
3081 const Loop *TargetLoop) const {
3082 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
3083 if (!AddRec)
3084 return Expr; // ignore
3085 if (AddRec->getLoop() == TargetLoop)
3086 return AddRec->getStart();
3087 return SE->getAddRecExpr(zeroCoefficient(AddRec->getStart(), TargetLoop),
3088 AddRec->getStepRecurrence(*SE), AddRec->getLoop(),
3089 AddRec->getNoWrapFlags());
3090}
3091
3092// Given a linear SCEV Expr,
3093// return the SCEV given by adding some Value to the
3094// coefficient corresponding to the specified TargetLoop.
3095// For example, given a*i + b*j + c*k, adding 1 to the coefficient
3096// corresponding to the j loop would yield a*i + (b+1)*j + c*k.
3097const SCEV *DependenceInfo::addToCoefficient(const SCEV *Expr,
3098 const Loop *TargetLoop,
3099 const SCEV *Value) const {
3100 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
3101 if (!AddRec) // create a new addRec
3102 return SE->getAddRecExpr(Expr, Value, TargetLoop,
3103 SCEV::FlagAnyWrap); // Worst case, with no info.
3104 if (AddRec->getLoop() == TargetLoop) {
3105 const SCEV *Sum = SE->getAddExpr(AddRec->getStepRecurrence(*SE), Value);
3106 if (Sum->isZero())
3107 return AddRec->getStart();
3108 return SE->getAddRecExpr(AddRec->getStart(), Sum, AddRec->getLoop(),
3109 AddRec->getNoWrapFlags());
3110 }
3111 if (SE->isLoopInvariant(AddRec, TargetLoop))
3112 return SE->getAddRecExpr(AddRec, Value, TargetLoop, SCEV::FlagAnyWrap);
3113 return SE->getAddRecExpr(
3114 addToCoefficient(AddRec->getStart(), TargetLoop, Value),
3115 AddRec->getStepRecurrence(*SE), AddRec->getLoop(),
3116 AddRec->getNoWrapFlags());
3117}
3118
3119// Review the constraints, looking for opportunities
3120// to simplify a subscript pair (Src and Dst).
3121// Return true if some simplification occurs.
3122// If the simplification isn't exact (that is, if it is conservative
3123// in terms of dependence), set consistent to false.
3124// Corresponds to Figure 5 from the paper
3125//
3126// Practical Dependence Testing
3127// Goff, Kennedy, Tseng
3128// PLDI 1991
3129bool DependenceInfo::propagate(const SCEV *&Src, const SCEV *&Dst,
3131 SmallVectorImpl<Constraint> &Constraints,
3132 bool &Consistent) {
3133 bool Result = false;
3134 for (unsigned LI : Loops.set_bits()) {
3135 LLVM_DEBUG(dbgs() << "\t Constraint[" << LI << "] is");
3136 LLVM_DEBUG(Constraints[LI].dump(dbgs()));
3137 if (Constraints[LI].isDistance())
3138 Result |= propagateDistance(Src, Dst, Constraints[LI], Consistent);
3139 else if (Constraints[LI].isLine())
3140 Result |= propagateLine(Src, Dst, Constraints[LI], Consistent);
3141 else if (Constraints[LI].isPoint())
3142 Result |= propagatePoint(Src, Dst, Constraints[LI]);
3143 }
3144 return Result;
3145}
3146
3147// Attempt to propagate a distance
3148// constraint into a subscript pair (Src and Dst).
3149// Return true if some simplification occurs.
3150// If the simplification isn't exact (that is, if it is conservative
3151// in terms of dependence), set consistent to false.
3152bool DependenceInfo::propagateDistance(const SCEV *&Src, const SCEV *&Dst,
3153 Constraint &CurConstraint,
3154 bool &Consistent) {
3155 const Loop *CurLoop = CurConstraint.getAssociatedLoop();
3156 LLVM_DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n");
3157 const SCEV *A_K = findCoefficient(Src, CurLoop);
3158 if (A_K->isZero())
3159 return false;
3160 const SCEV *DA_K = SE->getMulExpr(A_K, CurConstraint.getD());
3161 Src = SE->getMinusSCEV(Src, DA_K);
3162 Src = zeroCoefficient(Src, CurLoop);
3163 LLVM_DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n");
3164 LLVM_DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n");
3165 Dst = addToCoefficient(Dst, CurLoop, SE->getNegativeSCEV(A_K));
3166 LLVM_DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n");
3167 if (!findCoefficient(Dst, CurLoop)->isZero())
3168 Consistent = false;
3169 return true;
3170}
3171
3172// Attempt to propagate a line
3173// constraint into a subscript pair (Src and Dst).
3174// Return true if some simplification occurs.
3175// If the simplification isn't exact (that is, if it is conservative
3176// in terms of dependence), set consistent to false.
3177bool DependenceInfo::propagateLine(const SCEV *&Src, const SCEV *&Dst,
3178 Constraint &CurConstraint,
3179 bool &Consistent) {
3180 const Loop *CurLoop = CurConstraint.getAssociatedLoop();
3181 const SCEV *A = CurConstraint.getA();
3182 const SCEV *B = CurConstraint.getB();
3183 const SCEV *C = CurConstraint.getC();
3184 LLVM_DEBUG(dbgs() << "\t\tA = " << *A << ", B = " << *B << ", C = " << *C
3185 << "\n");
3186 LLVM_DEBUG(dbgs() << "\t\tSrc = " << *Src << "\n");
3187 LLVM_DEBUG(dbgs() << "\t\tDst = " << *Dst << "\n");
3188 if (A->isZero()) {
3189 const SCEVConstant *Bconst = dyn_cast<SCEVConstant>(B);
3190 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3191 if (!Bconst || !Cconst)
3192 return false;
3193 APInt Beta = Bconst->getAPInt();
3194 APInt Charlie = Cconst->getAPInt();
3195 APInt CdivB = Charlie.sdiv(Beta);
3196 assert(Charlie.srem(Beta) == 0 && "C should be evenly divisible by B");
3197 const SCEV *AP_K = findCoefficient(Dst, CurLoop);
3198 Src = SE->getMinusSCEV(Src, SE->getMulExpr(AP_K, SE->getConstant(CdivB)));
3199 Dst = zeroCoefficient(Dst, CurLoop);
3200 if (!findCoefficient(Src, CurLoop)->isZero())
3201 Consistent = false;
3202 } else if (B->isZero()) {
3203 const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A);
3204 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3205 if (!Aconst || !Cconst)
3206 return false;
3207 APInt Alpha = Aconst->getAPInt();
3208 APInt Charlie = Cconst->getAPInt();
3209 APInt CdivA = Charlie.sdiv(Alpha);
3210 assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A");
3211 const SCEV *A_K = findCoefficient(Src, CurLoop);
3212 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA)));
3213 Src = zeroCoefficient(Src, CurLoop);
3214 if (!findCoefficient(Dst, CurLoop)->isZero())
3215 Consistent = false;
3216 } else if (isKnownPredicate(CmpInst::ICMP_EQ, A, B)) {
3217 const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A);
3218 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3219 if (!Aconst || !Cconst)
3220 return false;
3221 APInt Alpha = Aconst->getAPInt();
3222 APInt Charlie = Cconst->getAPInt();
3223 APInt CdivA = Charlie.sdiv(Alpha);
3224 assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A");
3225 const SCEV *A_K = findCoefficient(Src, CurLoop);
3226 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA)));
3227 Src = zeroCoefficient(Src, CurLoop);
3228 Dst = addToCoefficient(Dst, CurLoop, A_K);
3229 if (!findCoefficient(Dst, CurLoop)->isZero())
3230 Consistent = false;
3231 } else {
3232 // paper is incorrect here, or perhaps just misleading
3233 const SCEV *A_K = findCoefficient(Src, CurLoop);
3234 Src = SE->getMulExpr(Src, A);
3235 Dst = SE->getMulExpr(Dst, A);
3236 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, C));
3237 Src = zeroCoefficient(Src, CurLoop);
3238 Dst = addToCoefficient(Dst, CurLoop, SE->getMulExpr(A_K, B));
3239 if (!findCoefficient(Dst, CurLoop)->isZero())
3240 Consistent = false;
3241 }
3242 LLVM_DEBUG(dbgs() << "\t\tnew Src = " << *Src << "\n");
3243 LLVM_DEBUG(dbgs() << "\t\tnew Dst = " << *Dst << "\n");
3244 return true;
3245}
3246
3247// Attempt to propagate a point
3248// constraint into a subscript pair (Src and Dst).
3249// Return true if some simplification occurs.
3250bool DependenceInfo::propagatePoint(const SCEV *&Src, const SCEV *&Dst,
3251 Constraint &CurConstraint) {
3252 const Loop *CurLoop = CurConstraint.getAssociatedLoop();
3253 const SCEV *A_K = findCoefficient(Src, CurLoop);
3254 const SCEV *AP_K = findCoefficient(Dst, CurLoop);
3255 const SCEV *XA_K = SE->getMulExpr(A_K, CurConstraint.getX());
3256 const SCEV *YAP_K = SE->getMulExpr(AP_K, CurConstraint.getY());
3257 LLVM_DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n");
3258 Src = SE->getAddExpr(Src, SE->getMinusSCEV(XA_K, YAP_K));
3259 Src = zeroCoefficient(Src, CurLoop);
3260 LLVM_DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n");
3261 LLVM_DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n");
3262 Dst = zeroCoefficient(Dst, CurLoop);
3263 LLVM_DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n");
3264 return true;
3265}
3266
3267// Update direction vector entry based on the current constraint.
3268void DependenceInfo::updateDirection(Dependence::DVEntry &Level,
3269 const Constraint &CurConstraint) const {
3270 LLVM_DEBUG(dbgs() << "\tUpdate direction, constraint =");
3271 LLVM_DEBUG(CurConstraint.dump(dbgs()));
3272 if (CurConstraint.isAny())
3273 ; // use defaults
3274 else if (CurConstraint.isDistance()) {
3275 // this one is consistent, the others aren't
3276 Level.Scalar = false;
3277 Level.Distance = CurConstraint.getD();
3278 unsigned NewDirection = Dependence::DVEntry::NONE;
3279 if (!SE->isKnownNonZero(Level.Distance)) // if may be zero
3280 NewDirection = Dependence::DVEntry::EQ;
3281 if (!SE->isKnownNonPositive(Level.Distance)) // if may be positive
3282 NewDirection |= Dependence::DVEntry::LT;
3283 if (!SE->isKnownNonNegative(Level.Distance)) // if may be negative
3284 NewDirection |= Dependence::DVEntry::GT;
3285 Level.Direction &= NewDirection;
3286 } else if (CurConstraint.isLine()) {
3287 Level.Scalar = false;
3288 Level.Distance = nullptr;
3289 // direction should be accurate
3290 } else if (CurConstraint.isPoint()) {
3291 Level.Scalar = false;
3292 Level.Distance = nullptr;
3293 unsigned NewDirection = Dependence::DVEntry::NONE;
3294 if (!isKnownPredicate(CmpInst::ICMP_NE, CurConstraint.getY(),
3295 CurConstraint.getX()))
3296 // if X may be = Y
3297 NewDirection |= Dependence::DVEntry::EQ;
3298 if (!isKnownPredicate(CmpInst::ICMP_SLE, CurConstraint.getY(),
3299 CurConstraint.getX()))
3300 // if Y may be > X
3301 NewDirection |= Dependence::DVEntry::LT;
3302 if (!isKnownPredicate(CmpInst::ICMP_SGE, CurConstraint.getY(),
3303 CurConstraint.getX()))
3304 // if Y may be < X
3305 NewDirection |= Dependence::DVEntry::GT;
3306 Level.Direction &= NewDirection;
3307 } else
3308 llvm_unreachable("constraint has unexpected kind");
3309}
3310
3311/// Check if we can delinearize the subscripts. If the SCEVs representing the
3312/// source and destination array references are recurrences on a nested loop,
3313/// this function flattens the nested recurrences into separate recurrences
3314/// for each loop level.
3315bool DependenceInfo::tryDelinearize(Instruction *Src, Instruction *Dst,
3317 assert(isLoadOrStore(Src) && "instruction is not load or store");
3318 assert(isLoadOrStore(Dst) && "instruction is not load or store");
3319 Value *SrcPtr = getLoadStorePointerOperand(Src);
3320 Value *DstPtr = getLoadStorePointerOperand(Dst);
3321 Loop *SrcLoop = LI->getLoopFor(Src->getParent());
3322 Loop *DstLoop = LI->getLoopFor(Dst->getParent());
3323 const SCEV *SrcAccessFn = SE->getSCEVAtScope(SrcPtr, SrcLoop);
3324 const SCEV *DstAccessFn = SE->getSCEVAtScope(DstPtr, DstLoop);
3325 const SCEVUnknown *SrcBase =
3326 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
3327 const SCEVUnknown *DstBase =
3328 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
3329
3330 if (!SrcBase || !DstBase || SrcBase != DstBase)
3331 return false;
3332
3333 SmallVector<const SCEV *, 4> SrcSubscripts, DstSubscripts;
3334
3335 if (!tryDelinearizeFixedSize(Src, Dst, SrcAccessFn, DstAccessFn,
3336 SrcSubscripts, DstSubscripts) &&
3337 !tryDelinearizeParametricSize(Src, Dst, SrcAccessFn, DstAccessFn,
3338 SrcSubscripts, DstSubscripts))
3339 return false;
3340
3341 assert(isLoopInvariant(SrcBase, SrcLoop) &&
3342 isLoopInvariant(DstBase, DstLoop) &&
3343 "Expected SrcBase and DstBase to be loop invariant");
3344
3345 int Size = SrcSubscripts.size();
3346 LLVM_DEBUG({
3347 dbgs() << "\nSrcSubscripts: ";
3348 for (int I = 0; I < Size; I++)
3349 dbgs() << *SrcSubscripts[I];
3350 dbgs() << "\nDstSubscripts: ";
3351 for (int I = 0; I < Size; I++)
3352 dbgs() << *DstSubscripts[I];
3353 });
3354
3355 // The delinearization transforms a single-subscript MIV dependence test into
3356 // a multi-subscript SIV dependence test that is easier to compute. So we
3357 // resize Pair to contain as many pairs of subscripts as the delinearization
3358 // has found, and then initialize the pairs following the delinearization.
3359 Pair.resize(Size);
3360 for (int I = 0; I < Size; ++I) {
3361 Pair[I].Src = SrcSubscripts[I];
3362 Pair[I].Dst = DstSubscripts[I];
3363 unifySubscriptType(&Pair[I]);
3364 }
3365
3366 return true;
3367}
3368
3369/// Try to delinearize \p SrcAccessFn and \p DstAccessFn if the underlying
3370/// arrays accessed are fixed-size arrays. Return true if delinearization was
3371/// successful.
3372bool DependenceInfo::tryDelinearizeFixedSize(
3373 Instruction *Src, Instruction *Dst, const SCEV *SrcAccessFn,
3374 const SCEV *DstAccessFn, SmallVectorImpl<const SCEV *> &SrcSubscripts,
3375 SmallVectorImpl<const SCEV *> &DstSubscripts) {
3376 LLVM_DEBUG({
3377 const SCEVUnknown *SrcBase =
3378 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
3379 const SCEVUnknown *DstBase =
3380 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
3381 assert(SrcBase && DstBase && SrcBase == DstBase &&
3382 "expected src and dst scev unknowns to be equal");
3383 });
3384
3385 SmallVector<int, 4> SrcSizes;
3386 SmallVector<int, 4> DstSizes;
3387 if (!tryDelinearizeFixedSizeImpl(SE, Src, SrcAccessFn, SrcSubscripts,
3388 SrcSizes) ||
3389 !tryDelinearizeFixedSizeImpl(SE, Dst, DstAccessFn, DstSubscripts,
3390 DstSizes))
3391 return false;
3392
3393 // Check that the two size arrays are non-empty and equal in length and
3394 // value.
3395 if (SrcSizes.size() != DstSizes.size() ||
3396 !std::equal(SrcSizes.begin(), SrcSizes.end(), DstSizes.begin())) {
3397 SrcSubscripts.clear();
3398 DstSubscripts.clear();
3399 return false;
3400 }
3401
3402 assert(SrcSubscripts.size() == DstSubscripts.size() &&
3403 "Expected equal number of entries in the list of SrcSubscripts and "
3404 "DstSubscripts.");
3405
3406 Value *SrcPtr = getLoadStorePointerOperand(Src);
3407 Value *DstPtr = getLoadStorePointerOperand(Dst);
3408
3409 // In general we cannot safely assume that the subscripts recovered from GEPs
3410 // are in the range of values defined for their corresponding array
3411 // dimensions. For example some C language usage/interpretation make it
3412 // impossible to verify this at compile-time. As such we can only delinearize
3413 // iff the subscripts are positive and are less than the range of the
3414 // dimension.
3416 auto AllIndicesInRange = [&](SmallVector<int, 4> &DimensionSizes,
3418 Value *Ptr) {
3419 size_t SSize = Subscripts.size();
3420 for (size_t I = 1; I < SSize; ++I) {
3421 const SCEV *S = Subscripts[I];
3422 if (!isKnownNonNegative(S, Ptr))
3423 return false;
3424 if (auto *SType = dyn_cast<IntegerType>(S->getType())) {
3425 const SCEV *Range = SE->getConstant(
3426 ConstantInt::get(SType, DimensionSizes[I - 1], false));
3427 if (!isKnownLessThan(S, Range))
3428 return false;
3429 }
3430 }
3431 return true;
3432 };
3433
3434 if (!AllIndicesInRange(SrcSizes, SrcSubscripts, SrcPtr) ||
3435 !AllIndicesInRange(DstSizes, DstSubscripts, DstPtr)) {
3436 SrcSubscripts.clear();
3437 DstSubscripts.clear();
3438 return false;
3439 }
3440 }
3441 LLVM_DEBUG({
3442 dbgs() << "Delinearized subscripts of fixed-size array\n"
3443 << "SrcGEP:" << *SrcPtr << "\n"
3444 << "DstGEP:" << *DstPtr << "\n";
3445 });
3446 return true;
3447}
3448
3449bool DependenceInfo::tryDelinearizeParametricSize(
3450 Instruction *Src, Instruction *Dst, const SCEV *SrcAccessFn,
3451 const SCEV *DstAccessFn, SmallVectorImpl<const SCEV *> &SrcSubscripts,
3452 SmallVectorImpl<const SCEV *> &DstSubscripts) {
3453
3454 Value *SrcPtr = getLoadStorePointerOperand(Src);
3455 Value *DstPtr = getLoadStorePointerOperand(Dst);
3456 const SCEVUnknown *SrcBase =
3457 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
3458 const SCEVUnknown *DstBase =
3459 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
3460 assert(SrcBase && DstBase && SrcBase == DstBase &&
3461 "expected src and dst scev unknowns to be equal");
3462
3463 const SCEV *ElementSize = SE->getElementSize(Src);
3464 if (ElementSize != SE->getElementSize(Dst))
3465 return false;
3466
3467 const SCEV *SrcSCEV = SE->getMinusSCEV(SrcAccessFn, SrcBase);
3468 const SCEV *DstSCEV = SE->getMinusSCEV(DstAccessFn, DstBase);
3469
3470 const SCEVAddRecExpr *SrcAR = dyn_cast<SCEVAddRecExpr>(SrcSCEV);
3471 const SCEVAddRecExpr *DstAR = dyn_cast<SCEVAddRecExpr>(DstSCEV);
3472 if (!SrcAR || !DstAR || !SrcAR->isAffine() || !DstAR->isAffine())
3473 return false;
3474
3475 // First step: collect parametric terms in both array references.
3477 collectParametricTerms(*SE, SrcAR, Terms);
3478 collectParametricTerms(*SE, DstAR, Terms);
3479
3480 // Second step: find subscript sizes.
3482 findArrayDimensions(*SE, Terms, Sizes, ElementSize);
3483
3484 // Third step: compute the access functions for each subscript.
3485 computeAccessFunctions(*SE, SrcAR, SrcSubscripts, Sizes);
3486 computeAccessFunctions(*SE, DstAR, DstSubscripts, Sizes);
3487
3488 // Fail when there is only a subscript: that's a linearized access function.
3489 if (SrcSubscripts.size() < 2 || DstSubscripts.size() < 2 ||
3490 SrcSubscripts.size() != DstSubscripts.size())
3491 return false;
3492
3493 size_t Size = SrcSubscripts.size();
3494
3495 // Statically check that the array bounds are in-range. The first subscript we
3496 // don't have a size for and it cannot overflow into another subscript, so is
3497 // always safe. The others need to be 0 <= subscript[i] < bound, for both src
3498 // and dst.
3499 // FIXME: It may be better to record these sizes and add them as constraints
3500 // to the dependency checks.
3502 for (size_t I = 1; I < Size; ++I) {
3503 if (!isKnownNonNegative(SrcSubscripts[I], SrcPtr))
3504 return false;
3505
3506 if (!isKnownLessThan(SrcSubscripts[I], Sizes[I - 1]))
3507 return false;
3508
3509 if (!isKnownNonNegative(DstSubscripts[I], DstPtr))
3510 return false;
3511
3512 if (!isKnownLessThan(DstSubscripts[I], Sizes[I - 1]))
3513 return false;
3514 }
3515
3516 return true;
3517}
3518
3519//===----------------------------------------------------------------------===//
3520
3521#ifndef NDEBUG
3522// For debugging purposes, dump a small bit vector to dbgs().
3524 dbgs() << "{";
3525 for (unsigned VI : BV.set_bits()) {
3526 dbgs() << VI;
3527 if (BV.find_next(VI) >= 0)
3528 dbgs() << ' ';
3529 }
3530 dbgs() << "}\n";
3531}
3532#endif
3533
3536 // Check if the analysis itself has been invalidated.
3537 auto PAC = PA.getChecker<DependenceAnalysis>();
3538 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>())
3539 return true;
3540
3541 // Check transitive dependencies.
3542 return Inv.invalidate<AAManager>(F, PA) ||
3544 Inv.invalidate<LoopAnalysis>(F, PA);
3545}
3546
3548 return SCEVUnionPredicate(Assumptions, *SE);
3549}
3550
3551// depends -
3552// Returns NULL if there is no dependence.
3553// Otherwise, return a Dependence with as many details as possible.
3554// Corresponds to Section 3.1 in the paper
3555//
3556// Practical Dependence Testing
3557// Goff, Kennedy, Tseng
3558// PLDI 1991
3559//
3560// Care is required to keep the routine below, getSplitIteration(),
3561// up to date with respect to this routine.
3562std::unique_ptr<Dependence>
3564 bool UnderRuntimeAssumptions) {
3566 bool PossiblyLoopIndependent = true;
3567 if (Src == Dst)
3568 PossiblyLoopIndependent = false;
3569
3570 if (!(Src->mayReadOrWriteMemory() && Dst->mayReadOrWriteMemory()))
3571 // if both instructions don't reference memory, there's no dependence
3572 return nullptr;
3573
3574 if (!isLoadOrStore(Src) || !isLoadOrStore(Dst)) {
3575 // can only analyze simple loads and stores, i.e., no calls, invokes, etc.
3576 LLVM_DEBUG(dbgs() << "can only handle simple loads and stores\n");
3577 return std::make_unique<Dependence>(Src, Dst,
3578 SCEVUnionPredicate(Assume, *SE));
3579 }
3580
3581 const MemoryLocation &DstLoc = MemoryLocation::get(Dst);
3582 const MemoryLocation &SrcLoc = MemoryLocation::get(Src);
3583
3584 switch (underlyingObjectsAlias(AA, F->getDataLayout(), DstLoc, SrcLoc)) {
3587 // cannot analyse objects if we don't understand their aliasing.
3588 LLVM_DEBUG(dbgs() << "can't analyze may or partial alias\n");
3589 return std::make_unique<Dependence>(Src, Dst,
3590 SCEVUnionPredicate(Assume, *SE));
3592 // If the objects noalias, they are distinct, accesses are independent.
3593 LLVM_DEBUG(dbgs() << "no alias\n");
3594 return nullptr;
3596 break; // The underlying objects alias; test accesses for dependence.
3597 }
3598
3599 if (DstLoc.Size != SrcLoc.Size || !DstLoc.Size.isPrecise() ||
3600 !SrcLoc.Size.isPrecise()) {
3601 // The dependence test gets confused if the size of the memory accesses
3602 // differ.
3603 LLVM_DEBUG(dbgs() << "can't analyze must alias with different sizes\n");
3604 return std::make_unique<Dependence>(Src, Dst,
3605 SCEVUnionPredicate(Assume, *SE));
3606 }
3607
3608 Value *SrcPtr = getLoadStorePointerOperand(Src);
3609 Value *DstPtr = getLoadStorePointerOperand(Dst);
3610 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
3611 const SCEV *DstSCEV = SE->getSCEV(DstPtr);
3612 LLVM_DEBUG(dbgs() << " SrcSCEV = " << *SrcSCEV << "\n");
3613 LLVM_DEBUG(dbgs() << " DstSCEV = " << *DstSCEV << "\n");
3614 const SCEV *SrcBase = SE->getPointerBase(SrcSCEV);
3615 const SCEV *DstBase = SE->getPointerBase(DstSCEV);
3616 if (SrcBase != DstBase) {
3617 // If two pointers have different bases, trying to analyze indexes won't
3618 // work; we can't compare them to each other. This can happen, for example,
3619 // if one is produced by an LCSSA PHI node.
3620 //
3621 // We check this upfront so we don't crash in cases where getMinusSCEV()
3622 // returns a SCEVCouldNotCompute.
3623 LLVM_DEBUG(dbgs() << "can't analyze SCEV with different pointer base\n");
3624 return std::make_unique<Dependence>(Src, Dst,
3625 SCEVUnionPredicate(Assume, *SE));
3626 }
3627
3628 // Even if the base pointers are the same, they may not be loop-invariant. It
3629 // could lead to incorrect results, as we're analyzing loop-carried
3630 // dependencies. Src and Dst can be in different loops, so we need to check
3631 // the base pointer is invariant in both loops.
3632 Loop *SrcLoop = LI->getLoopFor(Src->getParent());
3633 Loop *DstLoop = LI->getLoopFor(Dst->getParent());
3634 if (!isLoopInvariant(SrcBase, SrcLoop) ||
3635 !isLoopInvariant(DstBase, DstLoop)) {
3636 LLVM_DEBUG(dbgs() << "The base pointer is not loop invariant.\n");
3637 return std::make_unique<Dependence>(Src, Dst,
3638 SCEVUnionPredicate(Assume, *SE));
3639 }
3640
3641 uint64_t EltSize = SrcLoc.Size.toRaw();
3642 const SCEV *SrcEv = SE->getMinusSCEV(SrcSCEV, SrcBase);
3643 const SCEV *DstEv = SE->getMinusSCEV(DstSCEV, DstBase);
3644
3645 // Check that memory access offsets are multiples of element sizes.
3646 if (!SE->isKnownMultipleOf(SrcEv, EltSize, Assume) ||
3647 !SE->isKnownMultipleOf(DstEv, EltSize, Assume)) {
3648 LLVM_DEBUG(dbgs() << "can't analyze SCEV with different offsets\n");
3649 return std::make_unique<Dependence>(Src, Dst,
3650 SCEVUnionPredicate(Assume, *SE));
3651 }
3652
3653 if (!Assume.empty()) {
3654 if (!UnderRuntimeAssumptions)
3655 return std::make_unique<Dependence>(Src, Dst,
3656 SCEVUnionPredicate(Assume, *SE));
3657 // Add non-redundant assumptions.
3658 unsigned N = Assumptions.size();
3659 for (const SCEVPredicate *P : Assume) {
3660 bool Implied = false;
3661 for (unsigned I = 0; I != N && !Implied; I++)
3662 if (Assumptions[I]->implies(P, *SE))
3663 Implied = true;
3664 if (!Implied)
3665 Assumptions.push_back(P);
3666 }
3667 }
3668
3669 establishNestingLevels(Src, Dst);
3670 LLVM_DEBUG(dbgs() << " common nesting levels = " << CommonLevels << "\n");
3671 LLVM_DEBUG(dbgs() << " maximum nesting levels = " << MaxLevels << "\n");
3672
3673 FullDependence Result(Src, Dst, SCEVUnionPredicate(Assume, *SE),
3674 PossiblyLoopIndependent, CommonLevels);
3675 ++TotalArrayPairs;
3676
3677 unsigned Pairs = 1;
3678 SmallVector<Subscript, 2> Pair(Pairs);
3679 Pair[0].Src = SrcSCEV;
3680 Pair[0].Dst = DstSCEV;
3681
3682 if (Delinearize) {
3683 if (tryDelinearize(Src, Dst, Pair)) {
3684 LLVM_DEBUG(dbgs() << " delinearized\n");
3685 Pairs = Pair.size();
3686 }
3687 }
3688
3689 for (unsigned P = 0; P < Pairs; ++P) {
3690 Pair[P].Loops.resize(MaxLevels + 1);
3691 Pair[P].GroupLoops.resize(MaxLevels + 1);
3692 Pair[P].Group.resize(Pairs);
3693 removeMatchingExtensions(&Pair[P]);
3694 Pair[P].Classification =
3695 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()), Pair[P].Dst,
3696 LI->getLoopFor(Dst->getParent()), Pair[P].Loops);
3697 Pair[P].GroupLoops = Pair[P].Loops;
3698 Pair[P].Group.set(P);
3699 LLVM_DEBUG(dbgs() << " subscript " << P << "\n");
3700 LLVM_DEBUG(dbgs() << "\tsrc = " << *Pair[P].Src << "\n");
3701 LLVM_DEBUG(dbgs() << "\tdst = " << *Pair[P].Dst << "\n");
3702 LLVM_DEBUG(dbgs() << "\tclass = " << Pair[P].Classification << "\n");
3703 LLVM_DEBUG(dbgs() << "\tloops = ");
3705 }
3706
3707 SmallBitVector Separable(Pairs);
3708 SmallBitVector Coupled(Pairs);
3709
3710 // Partition subscripts into separable and minimally-coupled groups
3711 // Algorithm in paper is algorithmically better;
3712 // this may be faster in practice. Check someday.
3713 //
3714 // Here's an example of how it works. Consider this code:
3715 //
3716 // for (i = ...) {
3717 // for (j = ...) {
3718 // for (k = ...) {
3719 // for (l = ...) {
3720 // for (m = ...) {
3721 // A[i][j][k][m] = ...;
3722 // ... = A[0][j][l][i + j];
3723 // }
3724 // }
3725 // }
3726 // }
3727 // }
3728 //
3729 // There are 4 subscripts here:
3730 // 0 [i] and [0]
3731 // 1 [j] and [j]
3732 // 2 [k] and [l]
3733 // 3 [m] and [i + j]
3734 //
3735 // We've already classified each subscript pair as ZIV, SIV, etc.,
3736 // and collected all the loops mentioned by pair P in Pair[P].Loops.
3737 // In addition, we've initialized Pair[P].GroupLoops to Pair[P].Loops
3738 // and set Pair[P].Group = {P}.
3739 //
3740 // Src Dst Classification Loops GroupLoops Group
3741 // 0 [i] [0] SIV {1} {1} {0}
3742 // 1 [j] [j] SIV {2} {2} {1}
3743 // 2 [k] [l] RDIV {3,4} {3,4} {2}
3744 // 3 [m] [i + j] MIV {1,2,5} {1,2,5} {3}
3745 //
3746 // For each subscript SI 0 .. 3, we consider each remaining subscript, SJ.
3747 // So, 0 is compared against 1, 2, and 3; 1 is compared against 2 and 3, etc.
3748 //
3749 // We begin by comparing 0 and 1. The intersection of the GroupLoops is empty.
3750 // Next, 0 and 2. Again, the intersection of their GroupLoops is empty.
3751 // Next 0 and 3. The intersection of their GroupLoop = {1}, not empty,
3752 // so Pair[3].Group = {0,3} and Done = false (that is, 0 will not be added
3753 // to either Separable or Coupled).
3754 //
3755 // Next, we consider 1 and 2. The intersection of the GroupLoops is empty.
3756 // Next, 1 and 3. The intersection of their GroupLoops = {2}, not empty,
3757 // so Pair[3].Group = {0, 1, 3} and Done = false.
3758 //
3759 // Next, we compare 2 against 3. The intersection of the GroupLoops is empty.
3760 // Since Done remains true, we add 2 to the set of Separable pairs.
3761 //
3762 // Finally, we consider 3. There's nothing to compare it with,
3763 // so Done remains true and we add it to the Coupled set.
3764 // Pair[3].Group = {0, 1, 3} and GroupLoops = {1, 2, 5}.
3765 //
3766 // In the end, we've got 1 separable subscript and 1 coupled group.
3767 for (unsigned SI = 0; SI < Pairs; ++SI) {
3768 if (Pair[SI].Classification == Subscript::NonLinear) {
3769 // ignore these, but collect loops for later
3770 ++NonlinearSubscriptPairs;
3771 collectCommonLoops(Pair[SI].Src, LI->getLoopFor(Src->getParent()),
3772 Pair[SI].Loops);
3773 collectCommonLoops(Pair[SI].Dst, LI->getLoopFor(Dst->getParent()),
3774 Pair[SI].Loops);
3775 Result.Consistent = false;
3776 } else if (Pair[SI].Classification == Subscript::ZIV) {
3777 // always separable
3778 Separable.set(SI);
3779 } else {
3780 // SIV, RDIV, or MIV, so check for coupled group
3781 bool Done = true;
3782 for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) {
3783 SmallBitVector Intersection = Pair[SI].GroupLoops;
3784 Intersection &= Pair[SJ].GroupLoops;
3785 if (Intersection.any()) {
3786 // accumulate set of all the loops in group
3787 Pair[SJ].GroupLoops |= Pair[SI].GroupLoops;
3788 // accumulate set of all subscripts in group
3789 Pair[SJ].Group |= Pair[SI].Group;
3790 Done = false;
3791 }
3792 }
3793 if (Done) {
3794 if (Pair[SI].Group.count() == 1) {
3795 Separable.set(SI);
3796 ++SeparableSubscriptPairs;
3797 } else {
3798 Coupled.set(SI);
3799 ++CoupledSubscriptPairs;
3800 }
3801 }
3802 }
3803 }
3804
3805 LLVM_DEBUG(dbgs() << " Separable = ");
3806 LLVM_DEBUG(dumpSmallBitVector(Separable));
3807 LLVM_DEBUG(dbgs() << " Coupled = ");
3809
3810 Constraint NewConstraint;
3811 NewConstraint.setAny(SE);
3812
3813 // test separable subscripts
3814 for (unsigned SI : Separable.set_bits()) {
3815 LLVM_DEBUG(dbgs() << "testing subscript " << SI);
3816 switch (Pair[SI].Classification) {
3817 case Subscript::ZIV:
3818 LLVM_DEBUG(dbgs() << ", ZIV\n");
3819 if (testZIV(Pair[SI].Src, Pair[SI].Dst, Result))
3820 return nullptr;
3821 break;
3822 case Subscript::SIV: {
3823 LLVM_DEBUG(dbgs() << ", SIV\n");
3824 unsigned Level;
3825 const SCEV *SplitIter = nullptr;
3826 if (testSIV(Pair[SI].Src, Pair[SI].Dst, Level, Result, NewConstraint,
3827 SplitIter))
3828 return nullptr;
3829 break;
3830 }
3831 case Subscript::RDIV:
3832 LLVM_DEBUG(dbgs() << ", RDIV\n");
3833 if (testRDIV(Pair[SI].Src, Pair[SI].Dst, Result))
3834 return nullptr;
3835 break;
3836 case Subscript::MIV:
3837 LLVM_DEBUG(dbgs() << ", MIV\n");
3838 if (testMIV(Pair[SI].Src, Pair[SI].Dst, Pair[SI].Loops, Result))
3839 return nullptr;
3840 break;
3841 default:
3842 llvm_unreachable("subscript has unexpected classification");
3843 }
3844 }
3845
3846 if (Coupled.count()) {
3847 // test coupled subscript groups
3848 LLVM_DEBUG(dbgs() << "starting on coupled subscripts\n");
3849 LLVM_DEBUG(dbgs() << "MaxLevels + 1 = " << MaxLevels + 1 << "\n");
3850 SmallVector<Constraint, 4> Constraints(MaxLevels + 1);
3851 for (unsigned II = 0; II <= MaxLevels; ++II)
3852 Constraints[II].setAny(SE);
3853 for (unsigned SI : Coupled.set_bits()) {
3854 LLVM_DEBUG(dbgs() << "testing subscript group " << SI << " { ");
3855 SmallBitVector Group(Pair[SI].Group);
3856 SmallBitVector Sivs(Pairs);
3857 SmallBitVector Mivs(Pairs);
3858 SmallBitVector ConstrainedLevels(MaxLevels + 1);
3859 SmallVector<Subscript *, 4> PairsInGroup;
3860 for (unsigned SJ : Group.set_bits()) {
3861 LLVM_DEBUG(dbgs() << SJ << " ");
3862 if (Pair[SJ].Classification == Subscript::SIV)
3863 Sivs.set(SJ);
3864 else
3865 Mivs.set(SJ);
3866 PairsInGroup.push_back(&Pair[SJ]);
3867 }
3868 unifySubscriptType(PairsInGroup);
3869 LLVM_DEBUG(dbgs() << "}\n");
3870 while (Sivs.any()) {
3871 bool Changed = false;
3872 for (unsigned SJ : Sivs.set_bits()) {
3873 LLVM_DEBUG(dbgs() << "testing subscript " << SJ << ", SIV\n");
3874 // SJ is an SIV subscript that's part of the current coupled group
3875 unsigned Level;
3876 const SCEV *SplitIter = nullptr;
3877 LLVM_DEBUG(dbgs() << "SIV\n");
3878 if (testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level, Result, NewConstraint,
3879 SplitIter))
3880 return nullptr;
3881 ConstrainedLevels.set(Level);
3882 if (intersectConstraints(&Constraints[Level], &NewConstraint)) {
3883 if (Constraints[Level].isEmpty()) {
3884 ++DeltaIndependence;
3885 return nullptr;
3886 }
3887 Changed = true;
3888 }
3889 Sivs.reset(SJ);
3890 }
3891 if (Changed) {
3892 // propagate, possibly creating new SIVs and ZIVs
3893 LLVM_DEBUG(dbgs() << " propagating\n");
3894 LLVM_DEBUG(dbgs() << "\tMivs = ");
3896 for (unsigned SJ : Mivs.set_bits()) {
3897 // SJ is an MIV subscript that's part of the current coupled group
3898 LLVM_DEBUG(dbgs() << "\tSJ = " << SJ << "\n");
3899 if (propagate(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops,
3900 Constraints, Result.Consistent)) {
3901 LLVM_DEBUG(dbgs() << "\t Changed\n");
3902 ++DeltaPropagations;
3903 Pair[SJ].Classification = classifyPair(
3904 Pair[SJ].Src, LI->getLoopFor(Src->getParent()), Pair[SJ].Dst,
3905 LI->getLoopFor(Dst->getParent()), Pair[SJ].Loops);
3906 switch (Pair[SJ].Classification) {
3907 case Subscript::ZIV:
3908 LLVM_DEBUG(dbgs() << "ZIV\n");
3909 if (testZIV(Pair[SJ].Src, Pair[SJ].Dst, Result))
3910 return nullptr;
3911 Mivs.reset(SJ);
3912 break;
3913 case Subscript::SIV:
3914 Sivs.set(SJ);
3915 Mivs.reset(SJ);
3916 break;
3917 case Subscript::RDIV:
3918 case Subscript::MIV:
3919 break;
3920 default:
3921 llvm_unreachable("bad subscript classification");
3922 }
3923 }
3924 }
3925 }
3926 }
3927
3928 // test & propagate remaining RDIVs
3929 for (unsigned SJ : Mivs.set_bits()) {
3930 if (Pair[SJ].Classification == Subscript::RDIV) {
3931 LLVM_DEBUG(dbgs() << "RDIV test\n");
3932 if (testRDIV(Pair[SJ].Src, Pair[SJ].Dst, Result))
3933 return nullptr;
3934 // I don't yet understand how to propagate RDIV results
3935 Mivs.reset(SJ);
3936 }
3937 }
3938
3939 // test remaining MIVs
3940 // This code is temporary.
3941 // Better to somehow test all remaining subscripts simultaneously.
3942 for (unsigned SJ : Mivs.set_bits()) {
3943 if (Pair[SJ].Classification == Subscript::MIV) {
3944 LLVM_DEBUG(dbgs() << "MIV test\n");
3945 if (testMIV(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops, Result))
3946 return nullptr;
3947 } else
3948 llvm_unreachable("expected only MIV subscripts at this point");
3949 }
3950
3951 // update Result.DV from constraint vector
3952 LLVM_DEBUG(dbgs() << " updating\n");
3953 for (unsigned SJ : ConstrainedLevels.set_bits()) {
3954 if (SJ > CommonLevels)
3955 break;
3956 updateDirection(Result.DV[SJ - 1], Constraints[SJ]);
3957 if (Result.DV[SJ - 1].Direction == Dependence::DVEntry::NONE)
3958 return nullptr;
3959 }
3960 }
3961 }
3962
3963 // Make sure the Scalar flags are set correctly.
3964 SmallBitVector CompleteLoops(MaxLevels + 1);
3965 for (unsigned SI = 0; SI < Pairs; ++SI)
3966 CompleteLoops |= Pair[SI].Loops;
3967 for (unsigned II = 1; II <= CommonLevels; ++II)
3968 if (CompleteLoops[II])
3969 Result.DV[II - 1].Scalar = false;
3970
3971 // Set the distance to zero if the direction is EQ.
3972 // TODO: Ideally, the distance should be set to 0 immediately simultaneously
3973 // with the corresponding direction being set to EQ.
3974 for (unsigned II = 1; II <= Result.getLevels(); ++II) {
3975 if (Result.getDirection(II) == Dependence::DVEntry::EQ) {
3976 if (Result.DV[II - 1].Distance == nullptr)
3977 Result.DV[II - 1].Distance = SE->getZero(SrcSCEV->getType());
3978 else
3979 assert(Result.DV[II - 1].Distance->isZero() &&
3980 "Inconsistency between distance and direction");
3981 }
3982
3983#ifndef NDEBUG
3984 // Check that the converse (i.e., if the distance is zero, then the
3985 // direction is EQ) holds.
3986 const SCEV *Distance = Result.getDistance(II);
3987 if (Distance && Distance->isZero())
3988 assert(Result.getDirection(II) == Dependence::DVEntry::EQ &&
3989 "Distance is zero, but direction is not EQ");
3990#endif
3991 }
3992
3993 if (PossiblyLoopIndependent) {
3994 // Make sure the LoopIndependent flag is set correctly.
3995 // All directions must include equal, otherwise no
3996 // loop-independent dependence is possible.
3997 for (unsigned II = 1; II <= CommonLevels; ++II) {
3998 if (!(Result.getDirection(II) & Dependence::DVEntry::EQ)) {
3999 Result.LoopIndependent = false;
4000 break;
4001 }
4002 }
4003 } else {
4004 // On the other hand, if all directions are equal and there's no
4005 // loop-independent dependence possible, then no dependence exists.
4006 bool AllEqual = true;
4007 for (unsigned II = 1; II <= CommonLevels; ++II) {
4008 if (Result.getDirection(II) != Dependence::DVEntry::EQ) {
4009 AllEqual = false;
4010 break;
4011 }
4012 }
4013 if (AllEqual)
4014 return nullptr;
4015 }
4016
4017 return std::make_unique<FullDependence>(std::move(Result));
4018}
4019
4020//===----------------------------------------------------------------------===//
4021// getSplitIteration -
4022// Rather than spend rarely-used space recording the splitting iteration
4023// during the Weak-Crossing SIV test, we re-compute it on demand.
4024// The re-computation is basically a repeat of the entire dependence test,
4025// though simplified since we know that the dependence exists.
4026// It's tedious, since we must go through all propagations, etc.
4027//
4028// Care is required to keep this code up to date with respect to the routine
4029// above, depends().
4030//
4031// Generally, the dependence analyzer will be used to build
4032// a dependence graph for a function (basically a map from instructions
4033// to dependences). Looking for cycles in the graph shows us loops
4034// that cannot be trivially vectorized/parallelized.
4035//
4036// We can try to improve the situation by examining all the dependences
4037// that make up the cycle, looking for ones we can break.
4038// Sometimes, peeling the first or last iteration of a loop will break
4039// dependences, and we've got flags for those possibilities.
4040// Sometimes, splitting a loop at some other iteration will do the trick,
4041// and we've got a flag for that case. Rather than waste the space to
4042// record the exact iteration (since we rarely know), we provide
4043// a method that calculates the iteration. It's a drag that it must work
4044// from scratch, but wonderful in that it's possible.
4045//
4046// Here's an example:
4047//
4048// for (i = 0; i < 10; i++)
4049// A[i] = ...
4050// ... = A[11 - i]
4051//
4052// There's a loop-carried flow dependence from the store to the load,
4053// found by the weak-crossing SIV test. The dependence will have a flag,
4054// indicating that the dependence can be broken by splitting the loop.
4055// Calling getSplitIteration will return 5.
4056// Splitting the loop breaks the dependence, like so:
4057//
4058// for (i = 0; i <= 5; i++)
4059// A[i] = ...
4060// ... = A[11 - i]
4061// for (i = 6; i < 10; i++)
4062// A[i] = ...
4063// ... = A[11 - i]
4064//
4065// breaks the dependence and allows us to vectorize/parallelize
4066// both loops.
4068 unsigned SplitLevel) {
4069 assert(Dep.isSplitable(SplitLevel) &&
4070 "Dep should be splitable at SplitLevel");
4071 Instruction *Src = Dep.getSrc();
4072 Instruction *Dst = Dep.getDst();
4073 assert(Src->mayReadFromMemory() || Src->mayWriteToMemory());
4074 assert(Dst->mayReadFromMemory() || Dst->mayWriteToMemory());
4075 assert(isLoadOrStore(Src));
4076 assert(isLoadOrStore(Dst));
4077 Value *SrcPtr = getLoadStorePointerOperand(Src);
4078 Value *DstPtr = getLoadStorePointerOperand(Dst);
4080 AA, F->getDataLayout(), MemoryLocation::get(Dst),
4082
4083 // establish loop nesting levels
4084 establishNestingLevels(Src, Dst);
4085
4086 FullDependence Result(Src, Dst, Dep.Assumptions, false, CommonLevels);
4087
4088 unsigned Pairs = 1;
4089 SmallVector<Subscript, 2> Pair(Pairs);
4090 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
4091 const SCEV *DstSCEV = SE->getSCEV(DstPtr);
4092 Pair[0].Src = SrcSCEV;
4093 Pair[0].Dst = DstSCEV;
4094
4095 if (Delinearize) {
4096 if (tryDelinearize(Src, Dst, Pair)) {
4097 LLVM_DEBUG(dbgs() << " delinearized\n");
4098 Pairs = Pair.size();
4099 }
4100 }
4101
4102 for (unsigned P = 0; P < Pairs; ++P) {
4103 Pair[P].Loops.resize(MaxLevels + 1);
4104 Pair[P].GroupLoops.resize(MaxLevels + 1);
4105 Pair[P].Group.resize(Pairs);
4106 removeMatchingExtensions(&Pair[P]);
4107 Pair[P].Classification =
4108 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()), Pair[P].Dst,
4109 LI->getLoopFor(Dst->getParent()), Pair[P].Loops);
4110 Pair[P].GroupLoops = Pair[P].Loops;
4111 Pair[P].Group.set(P);
4112 }
4113
4114 SmallBitVector Separable(Pairs);
4115 SmallBitVector Coupled(Pairs);
4116
4117 // partition subscripts into separable and minimally-coupled groups
4118 for (unsigned SI = 0; SI < Pairs; ++SI) {
4119 if (Pair[SI].Classification == Subscript::NonLinear) {
4120 // ignore these, but collect loops for later
4121 collectCommonLoops(Pair[SI].Src, LI->getLoopFor(Src->getParent()),
4122 Pair[SI].Loops);
4123 collectCommonLoops(Pair[SI].Dst, LI->getLoopFor(Dst->getParent()),
4124 Pair[SI].Loops);
4125 Result.Consistent = false;
4126 } else if (Pair[SI].Classification == Subscript::ZIV)
4127 Separable.set(SI);
4128 else {
4129 // SIV, RDIV, or MIV, so check for coupled group
4130 bool Done = true;
4131 for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) {
4132 SmallBitVector Intersection = Pair[SI].GroupLoops;
4133 Intersection &= Pair[SJ].GroupLoops;
4134 if (Intersection.any()) {
4135 // accumulate set of all the loops in group
4136 Pair[SJ].GroupLoops |= Pair[SI].GroupLoops;
4137 // accumulate set of all subscripts in group
4138 Pair[SJ].Group |= Pair[SI].Group;
4139 Done = false;
4140 }
4141 }
4142 if (Done) {
4143 if (Pair[SI].Group.count() == 1)
4144 Separable.set(SI);
4145 else
4146 Coupled.set(SI);
4147 }
4148 }
4149 }
4150
4151 Constraint NewConstraint;
4152 NewConstraint.setAny(SE);
4153
4154 // test separable subscripts
4155 for (unsigned SI : Separable.set_bits()) {
4156 switch (Pair[SI].Classification) {
4157 case Subscript::SIV: {
4158 unsigned Level;
4159 const SCEV *SplitIter = nullptr;
4160 (void)testSIV(Pair[SI].Src, Pair[SI].Dst, Level, Result, NewConstraint,
4161 SplitIter);
4162 if (Level == SplitLevel) {
4163 assert(SplitIter != nullptr);
4164 return SplitIter;
4165 }
4166 break;
4167 }
4168 case Subscript::ZIV:
4169 case Subscript::RDIV:
4170 case Subscript::MIV:
4171 break;
4172 default:
4173 llvm_unreachable("subscript has unexpected classification");
4174 }
4175 }
4176
4177 assert(!Coupled.empty() && "coupled expected non-empty");
4178
4179 // test coupled subscript groups
4180 SmallVector<Constraint, 4> Constraints(MaxLevels + 1);
4181 for (unsigned II = 0; II <= MaxLevels; ++II)
4182 Constraints[II].setAny(SE);
4183 for (unsigned SI : Coupled.set_bits()) {
4184 SmallBitVector Group(Pair[SI].Group);
4185 SmallBitVector Sivs(Pairs);
4186 SmallBitVector Mivs(Pairs);
4187 SmallBitVector ConstrainedLevels(MaxLevels + 1);
4188 for (unsigned SJ : Group.set_bits()) {
4189 if (Pair[SJ].Classification == Subscript::SIV)
4190 Sivs.set(SJ);
4191 else
4192 Mivs.set(SJ);
4193 }
4194 while (Sivs.any()) {
4195 bool Changed = false;
4196 for (unsigned SJ : Sivs.set_bits()) {
4197 // SJ is an SIV subscript that's part of the current coupled group
4198 unsigned Level;
4199 const SCEV *SplitIter = nullptr;
4200 (void)testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level, Result, NewConstraint,
4201 SplitIter);
4202 if (Level == SplitLevel && SplitIter)
4203 return SplitIter;
4204 ConstrainedLevels.set(Level);
4205 if (intersectConstraints(&Constraints[Level], &NewConstraint))
4206 Changed = true;
4207 Sivs.reset(SJ);
4208 }
4209 if (!Changed)
4210 continue;
4211 // propagate, possibly creating new SIVs and ZIVs
4212 for (unsigned SJ : Mivs.set_bits()) {
4213 // SJ is an MIV subscript that's part of the current coupled group
4214 if (!propagate(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops, Constraints,
4215 Result.Consistent))
4216 continue;
4217 Pair[SJ].Classification = classifyPair(
4218 Pair[SJ].Src, LI->getLoopFor(Src->getParent()), Pair[SJ].Dst,
4219 LI->getLoopFor(Dst->getParent()), Pair[SJ].Loops);
4220 switch (Pair[SJ].Classification) {
4221 case Subscript::ZIV:
4222 Mivs.reset(SJ);
4223 break;
4224 case Subscript::SIV:
4225 Sivs.set(SJ);
4226 Mivs.reset(SJ);
4227 break;
4228 case Subscript::RDIV:
4229 case Subscript::MIV:
4230 break;
4231 default:
4232 llvm_unreachable("bad subscript classification");
4233 }
4234 }
4235 }
4236 }
4237 llvm_unreachable("somehow reached end of routine");
4238}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
block Block Frequency Analysis
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:638
static bool isLoadOrStore(const Instruction *I)
static bool findGCD(unsigned Bits, const APInt &AM, const APInt &BM, const APInt &Delta, APInt &G, APInt &X, APInt &Y)
static void dumpSmallBitVector(SmallBitVector &BV)
static APInt ceilingOfQuotient(const APInt &A, const APInt &B)
static APInt floorOfQuotient(const APInt &A, const APInt &B)
static AliasResult underlyingObjectsAlias(AAResults *AA, const DataLayout &DL, const MemoryLocation &LocA, const MemoryLocation &LocB)
static std::pair< std::optional< APInt >, std::optional< APInt > > inferDomainOfAffine(const APInt &A, const APInt &B, const std::optional< APInt > &UB)
Given an affine expression of the form A*k + B, where k is an arbitrary integer, infer the possible r...
static std::optional< APInt > getConstantPart(const SCEV *Expr)
static bool isRemainderZero(const SCEVConstant *Dividend, const SCEVConstant *Divisor)
static cl::opt< bool > Delinearize("da-delinearize", cl::init(true), cl::Hidden, cl::desc("Try to delinearize array references."))
static void dumpExampleDependence(raw_ostream &OS, DependenceInfo *DA, ScalarEvolution &SE, bool NormalizeResults)
static cl::opt< unsigned > MIVMaxLevelThreshold("da-miv-max-level-threshold", cl::init(7), cl::Hidden, cl::desc("Maximum depth allowed for the recursive algorithm used to " "explore MIV direction vectors."))
static cl::opt< bool > DisableDelinearizationChecks("da-disable-delinearization-checks", cl::Hidden, cl::desc("Disable checks that try to statically verify validity of " "delinearized subscripts. Enabling this option may result in incorrect " "dependence vectors for languages that allow the subscript of one " "dimension to underflow or overflow into another dimension."))
uint64_t Size
bool End
Definition: ELF_riscv.cpp:480
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
Hexagon Hardware Loops
Module.h This file contains the declarations for the Module class.
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:546
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#define P(N)
FunctionAnalysisManager FAM
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:39
raw_pwrite_stream & OS
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
#define LLVM_DEBUG(...)
Definition: Debug.h:119
A manager for alias analyses.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
A private abstract base class describing the concept of an individual alias analysis implementation.
Class for arbitrary precision integers.
Definition: APInt.h:78
static LLVM_ABI void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Definition: APInt.cpp:1890
APInt abs() const
Get the absolute value.
Definition: APInt.h:1795
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition: APInt.h:1201
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1488
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:209
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1644
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition: APInt.h:1166
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:219
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition: APInt.cpp:1736
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1130
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:200
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition: APInt.h:1237
The possible results of an alias query.
Definition: AliasAnalysis.h:78
@ MayAlias
The two locations may or may not alias.
Definition: AliasAnalysis.h:99
@ NoAlias
The two locations do not alias at all.
Definition: AliasAnalysis.h:96
@ PartialAlias
The two locations alias, but only due to a partial overlap.
@ MustAlias
The two locations precisely alias each other.
This templated class represents "all analyses that operate over <a particular IR unit>" (e....
Definition: Analysis.h:50
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:294
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Trigger the invalidation of some other analysis pass if not already handled and return whether it was...
Definition: PassManager.h:312
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:412
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
AnalysisUsage & addRequiredTransitive()
Definition: Any.h:28
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
void enableCrossIterationMode()
Assume that values may come from different cycle iterations.
bool isNoAlias(const MemoryLocation &LocA, const MemoryLocation &LocB)
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:678
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:707
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:708
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:705
@ ICMP_EQ
equal
Definition: InstrTypes.h:699
@ ICMP_NE
not equal
Definition: InstrTypes.h:700
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:706
This is an important base class in LLVM.
Definition: Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
Legacy pass manager pass to access dependence information.
void getAnalysisUsage(AnalysisUsage &) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
void print(raw_ostream &, const Module *=nullptr) const override
print - Print out the internal state of the pass.
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
AnalysisPass to compute dependence information in a function.
LLVM_ABI Result run(Function &F, FunctionAnalysisManager &FAM)
DependenceInfo - This class is the main dependence-analysis driver.
LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle transitive invalidation when the cached analysis results go away.
LLVM_ABI const SCEV * getSplitIteration(const Dependence &Dep, unsigned Level)
getSplitIteration - Give a dependence that's splittable at some particular level, return the iteratio...
LLVM_ABI SCEVUnionPredicate getRuntimeAssumptions() const
getRuntimeAssumptions - Returns all the runtime assumptions under which the dependence test is valid.
LLVM_ABI std::unique_ptr< Dependence > depends(Instruction *Src, Instruction *Dst, bool UnderRuntimeAssumptions=false)
depends - Tests for a dependence between the Src and Dst instructions.
Dependence - This class represents a dependence between two memory memory references in a function.
virtual unsigned getDirection(unsigned Level) const
getDirection - Returns the direction associated with a particular level.
virtual bool isPeelFirst(unsigned Level) const
isPeelFirst - Returns true if peeling the first iteration from this loop will break this dependence.
Instruction * getDst() const
getDst - Returns the destination instruction for this dependence.
SCEVUnionPredicate getRuntimeAssumptions() const
getRuntimeAssumptions - Returns the runtime assumptions under which this Dependence relation is valid...
virtual bool isConfused() const
isConfused - Returns true if this dependence is confused (the compiler understands nothing and makes ...
virtual bool isSplitable(unsigned Level) const
isSplitable - Returns true if splitting this loop will break the dependence.
virtual const SCEV * getDistance(unsigned Level) const
getDistance - Returns the distance (or NULL) associated with a particular level.
virtual bool isScalar(unsigned Level) const
isScalar - Returns true if a particular level is scalar; that is, if no subscript in the source or de...
virtual bool isConsistent() const
isConsistent - Returns true if this dependence is consistent (occurs every time the source and destin...
virtual bool isPeelLast(unsigned Level) const
isPeelLast - Returns true if peeling the last iteration from this loop will break this dependence.
virtual unsigned getLevels() const
getLevels - Returns the number of common loops surrounding the source and destination of the dependen...
bool isFlow() const
isFlow - Returns true if this is a flow (aka true) dependence.
bool isInput() const
isInput - Returns true if this is an input dependence.
bool isAnti() const
isAnti - Returns true if this is an anti dependence.
Instruction * getSrc() const
getSrc - Returns the source instruction for this dependence.
virtual bool isLoopIndependent() const
isLoopIndependent - Returns true if this is a loop-independent dependence.
bool isOutput() const
isOutput - Returns true if this is an output dependence.
void dump(raw_ostream &OS) const
dump - For debugging purposes, dumps a dependence to OS.
Class representing an expression and its matching format.
FullDependence - This class represents a dependence between two memory references in a function.
unsigned getDirection(unsigned Level) const override
getDirection - Returns the direction associated with a particular level.
const SCEV * getDistance(unsigned Level) const override
getDistance - Returns the distance (or NULL) associated with a particular level.
FullDependence(Instruction *Source, Instruction *Destination, const SCEVUnionPredicate &Assumes, bool PossiblyLoopIndependent, unsigned Levels)
bool isDirectionNegative() const override
Check if the direction vector is negative.
bool isSplitable(unsigned Level) const override
isSplitable - Returns true if splitting the loop will break the dependence.
bool isScalar(unsigned Level) const override
isScalar - Returns true if a particular level is scalar; that is, if no subscript in the source or de...
bool isPeelLast(unsigned Level) const override
isPeelLast - Returns true if peeling the last iteration from this loop will break this dependence.
bool isPeelFirst(unsigned Level) const override
isPeelFirst - Returns true if peeling the first iteration from this loop will break this dependence.
bool normalize(ScalarEvolution *SE) override
If the direction vector is negative, normalize the direction vector to make it non-negative.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:314
const DataLayout & getDataLayout() const
Get the data layout of the module this function belongs to.
Definition: Function.cpp:363
LLVM_ABI bool mayWriteToMemory() const LLVM_READONLY
Return true if this instruction may modify memory.
LLVM_ABI bool mayReadFromMemory() const LLVM_READONLY
Return true if this instruction may read memory.
Class to represent integer types.
Definition: DerivedTypes.h:42
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:74
An instruction for reading from memory.
Definition: Instructions.h:180
bool isPrecise() const
uint64_t toRaw() const
Analysis pass that exposes the LoopInfo for a function.
Definition: LoopInfo.h:570
unsigned getLoopDepth() const
Return the nesting level of this loop.
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
unsigned getLoopDepth(const BlockT *BB) const
Return the loop nesting level of the specified block.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:597
This class represents a loop nest and can be used to query its properties.
Loop & getOutermostLoop() const
Return the outermost loop in the loop nest.
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:40
Representation for a specific memory location.
static LLVM_ABI MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
LocationSize Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known.
static MemoryLocation getBeforeOrAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location before or after Ptr, while remaining within the underl...
AAMDNodes AATags
The metadata nodes which describes the aliasing of the location (each member is null if that kind of ...
const Value * Ptr
The address of the start of the location.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:118
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition: Analysis.h:275
This node represents an addition of some number of SCEVs.
This node represents a polynomial recurrence on the trip count of the specified loop.
LLVM_ABI const SCEV * evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const
Return the value of this chain of recurrences at the specified iteration number.
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values.
const SCEV * getOperand() const
This class represents a constant integer value.
const APInt & getAPInt() const
This is the base class for unary integral cast operator classes.
This node represents multiplication of some number of SCEVs.
NoWrapFlags getNoWrapFlags(NoWrapFlags Mask=NoWrapMask) const
const SCEV * getOperand(unsigned i) const
This class represents an assumption made using SCEV expressions which can be checked at run-time.
This class represents a composition of other SCEV predicates, and is the class that most clients will...
void print(raw_ostream &OS, unsigned Depth) const override
Prints a textual representation of this predicate with an indentation of Depth.
bool isAlwaysTrue() const override
Implementation of the SCEVPredicate interface.
This means that we are dealing with an entirely unknown SCEV value, and only represent it as its LLVM...
This class represents an analyzed expression in the program.
LLVM_ABI ArrayRef< const SCEV * > operands() const
Return operands of this SCEV expression.
LLVM_ABI bool isOne() const
Return true if the expression is a constant one.
LLVM_ABI bool isZero() const
Return true if the expression is a constant zero.
LLVM_ABI Type * getType() const
Return the LLVM type of this SCEV expression.
Analysis pass that exposes the ScalarEvolution for a function.
The main scalar evolution driver.
LLVM_ABI bool isKnownNonNegative(const SCEV *S)
Test if the given expression is known to be non-negative.
LLVM_ABI const SCEV * getNegativeSCEV(const SCEV *V, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
Return the SCEV object corresponding to -V.
LLVM_ABI const SCEV * getSMaxExpr(const SCEV *LHS, const SCEV *RHS)
LLVM_ABI bool isKnownNonPositive(const SCEV *S)
Test if the given expression is known to be non-positive.
LLVM_ABI bool isKnownNegative(const SCEV *S)
Test if the given expression is known to be negative.
LLVM_ABI bool isKnownNonZero(const SCEV *S)
Test if the given expression is known to be non-zero.
LLVM_ABI const SCEV * getSCEVAtScope(const SCEV *S, const Loop *L)
Return a SCEV expression for the specified value at the specified scope in the program.
LLVM_ABI const SCEV * getSMinExpr(const SCEV *LHS, const SCEV *RHS)
LLVM_ABI const SCEV * getBackedgeTakenCount(const Loop *L, ExitCountKind Kind=Exact)
If the specified loop has a predictable backedge-taken count, return it, otherwise return a SCEVCould...
const SCEV * getZero(Type *Ty)
Return a SCEV for the constant 0 of a specific type.
LLVM_ABI uint64_t getTypeSizeInBits(Type *Ty) const
Return the size in bits of the specified type, for which isSCEVable must return true.
LLVM_ABI const SCEV * getConstant(ConstantInt *V)
LLVM_ABI const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
const SCEV * getOne(Type *Ty)
Return a SCEV for the constant 1 of a specific type.
LLVM_ABI bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
LLVM_ABI bool isKnownPositive(const SCEV *S)
Test if the given expression is known to be positive.
LLVM_ABI const SCEV * getAddRecExpr(const SCEV *Start, const SCEV *Step, const Loop *L, SCEV::NoWrapFlags Flags)
Get an add recurrence expression for the specified loop.
LLVM_ABI const SCEV * getUDivExpr(const SCEV *LHS, const SCEV *RHS)
Get a canonical unsigned division expression, or something simpler if possible.
LLVM_ABI const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
LLVM_ABI bool hasLoopInvariantBackedgeTakenCount(const Loop *L)
Return true if the specified loop has an analyzable loop-invariant backedge-taken count.
LLVM_ABI bool isKnownMultipleOf(const SCEV *S, uint64_t M, SmallVectorImpl< const SCEVPredicate * > &Assumptions)
Check that S is a multiple of M.
LLVM_ABI const SCEV * getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
LLVM_ABI const SCEV * getPointerBase(const SCEV *V)
Transitively follow the chain of pointer-type operands until reaching a SCEV that does not have a sin...
LLVM_ABI const SCEV * getMulExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
LLVM_ABI const SCEV * getElementSize(Instruction *Inst)
Return the size of an element read or written by Inst.
LLVM_ABI const SCEV * getTruncateOrZeroExtend(const SCEV *V, Type *Ty, unsigned Depth=0)
Return a SCEV corresponding to a conversion of the input value to the specified type.
LLVM_ABI const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
LLVM_ABI bool isKnownPredicate(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Test if the given expression is known to satisfy the condition described by Pred, LHS,...
This is a 'bitvector' (really, a variable-sized bit array), optimized for the case when the array is ...
SmallBitVector & set()
iterator_range< const_set_bits_iterator > set_bits() const
int find_next(unsigned Prev) const
Returns the index of the next set bit following the "Prev" bit.
bool any() const
Returns true if any bit is set.
bool empty() const
Tests whether there are no bits in this bitvector.
size_type count() const
Returns the number of bits which are set.
SmallBitVector & reset()
bool empty() const
Definition: SmallVector.h:82
size_t size() const
Definition: SmallVector.h:79
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
void resize(size_type N)
Definition: SmallVector.h:639
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
An instruction for storing to memory.
Definition: Instructions.h:296
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const APInt & smin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
Definition: APInt.h:2248
const APInt & smax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be signed.
Definition: APInt.h:2253
LLVM_ABI APInt GreatestCommonDivisor(APInt A, APInt B)
Compute GCD of two unsigned APInt values.
Definition: APInt.cpp:798
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ TB
TB - TwoByte - Set if this instruction has a two byte opcode, which starts with a 0x0F byte before th...
Definition: X86BaseInfo.h:735
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:444
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
void collectParametricTerms(ScalarEvolution &SE, const SCEV *Expr, SmallVectorImpl< const SCEV * > &Terms)
Collect parametric terms occurring in step expressions (first step of delinearization).
void findArrayDimensions(ScalarEvolution &SE, SmallVectorImpl< const SCEV * > &Terms, SmallVectorImpl< const SCEV * > &Sizes, const SCEV *ElementSize)
Compute the array dimensions Sizes from the set of Terms extracted from the memory access function of...
@ Done
Definition: Threading.h:60
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:129
void computeAccessFunctions(ScalarEvolution &SE, const SCEV *Expr, SmallVectorImpl< const SCEV * > &Subscripts, SmallVectorImpl< const SCEV * > &Sizes)
Return in Subscripts the access functions for each dimension in Sizes (third step of delinearization)...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:130
bool tryDelinearizeFixedSizeImpl(ScalarEvolution *SE, Instruction *Inst, const SCEV *AccessFn, SmallVectorImpl< const SCEV * > &Subscripts, SmallVectorImpl< int > &Sizes)
Implementation of fixed size array delinearization.
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:223
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
LLVM_ABI FunctionPass * createDependenceAnalysisWrapperPass()
createDependenceAnalysisPass - This creates an instance of the DependenceAnalysis wrapper pass.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:858
#define N
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:29
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
Dependence::DVEntry - Each level in the distance/direction vector has a direction (or perhaps a union...
Direction
An enum for the direction of the loop.
Definition: LoopInfo.h:217