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