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