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 // Special check for addrecs using BE taken count
1184 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S))
1185 if (AddRec->isAffine() && AddRec->hasNoSignedWrap()) {
1186 const SCEV *BECount = SE->getBackedgeTakenCount(AddRec->getLoop());
1187 const SCEV *Start = AddRec->getStart();
1188 const SCEV *Step = AddRec->getStepRecurrence(*SE);
1189 const SCEV *End = AddRec->evaluateAtIteration(BECount, *SE);
1190 const SCEV *Diff0 = SE->getMinusSCEV(Start, Size);
1191 const SCEV *Diff1 = SE->getMinusSCEV(End, Size);
1192
1193 // If the value of Step is non-negative and the AddRec is non-wrap, it
1194 // reaches its maximum at the last iteration. So it's enouth to check
1195 // whether End - Size is negative.
1196 if (SE->isKnownNonNegative(Step) && SE->isKnownNegative(Diff1))
1197 return true;
1198
1199 // If the value of Step is non-positive and the AddRec is non-wrap, the
1200 // initial value is its maximum.
1201 if (SE->isKnownNonPositive(Step) && SE->isKnownNegative(Diff0))
1202 return true;
1203
1204 // Even if we don't know the sign of Step, either Start or End must be
1205 // the maximum value of the AddRec since it is non-wrap.
1206 if (SE->isKnownNegative(Diff0) && SE->isKnownNegative(Diff1))
1207 return true;
1208 }
1209
1210 // Check using normal isKnownNegative
1211 const SCEV *LimitedBound = SE->getMinusSCEV(S, Size);
1212 return SE->isKnownNegative(LimitedBound);
1213}
1214
1215bool DependenceInfo::isKnownNonNegative(const SCEV *S, const Value *Ptr) const {
1216 bool Inbounds = false;
1217 if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(Ptr))
1218 Inbounds = SrcGEP->isInBounds();
1219 if (Inbounds) {
1220 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
1221 if (AddRec->isAffine()) {
1222 // We know S is for Ptr, the operand on a load/store, so doesn't wrap.
1223 // If both parts are NonNegative, the end result will be NonNegative
1224 if (SE->isKnownNonNegative(AddRec->getStart()) &&
1225 SE->isKnownNonNegative(AddRec->getOperand(1)))
1226 return true;
1227 }
1228 }
1229 }
1230
1231 return SE->isKnownNonNegative(S);
1232}
1233
1234// All subscripts are all the same type.
1235// Loop bound may be smaller (e.g., a char).
1236// Should zero extend loop bound, since it's always >= 0.
1237// This routine collects upper bound and extends or truncates if needed.
1238// Truncating is safe when subscripts are known not to wrap. Cases without
1239// nowrap flags should have been rejected earlier.
1240// Return null if no bound available.
1241const SCEV *DependenceInfo::collectUpperBound(const Loop *L, Type *T) const {
1242 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
1243 const SCEV *UB = SE->getBackedgeTakenCount(L);
1244 return SE->getTruncateOrZeroExtend(UB, T);
1245 }
1246 return nullptr;
1247}
1248
1249// Calls collectUpperBound(), then attempts to cast it to SCEVConstant.
1250// If the cast fails, returns NULL.
1251const SCEVConstant *DependenceInfo::collectConstantUpperBound(const Loop *L,
1252 Type *T) const {
1253 if (const SCEV *UB = collectUpperBound(L, T))
1254 return dyn_cast<SCEVConstant>(UB);
1255 return nullptr;
1256}
1257
1258/// Returns \p A - \p B if it guaranteed not to signed wrap. Otherwise returns
1259/// nullptr. \p A and \p B must have the same integer type.
1260static const SCEV *minusSCEVNoSignedOverflow(const SCEV *A, const SCEV *B,
1261 ScalarEvolution &SE) {
1262 if (SE.willNotOverflow(Instruction::Sub, /*Signed=*/true, A, B))
1263 return SE.getMinusSCEV(A, B);
1264 return nullptr;
1265}
1266
1267// testZIV -
1268// When we have a pair of subscripts of the form [c1] and [c2],
1269// where c1 and c2 are both loop invariant, we attack it using
1270// the ZIV test. Basically, we test by comparing the two values,
1271// but there are actually three possible results:
1272// 1) the values are equal, so there's a dependence
1273// 2) the values are different, so there's no dependence
1274// 3) the values might be equal, so we have to assume a dependence.
1275//
1276// Return true if dependence disproved.
1277bool DependenceInfo::testZIV(const SCEV *Src, const SCEV *Dst,
1278 FullDependence &Result) const {
1279 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
1280 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
1281 ++ZIVapplications;
1282 if (isKnownPredicate(CmpInst::ICMP_EQ, Src, Dst)) {
1283 LLVM_DEBUG(dbgs() << " provably dependent\n");
1284 return false; // provably dependent
1285 }
1286 if (isKnownPredicate(CmpInst::ICMP_NE, Src, Dst)) {
1287 LLVM_DEBUG(dbgs() << " provably independent\n");
1288 ++ZIVindependence;
1289 return true; // provably independent
1290 }
1291 LLVM_DEBUG(dbgs() << " possibly dependent\n");
1292 Result.Consistent = false;
1293 return false; // possibly dependent
1294}
1295
1296// strongSIVtest -
1297// From the paper, Practical Dependence Testing, Section 4.2.1
1298//
1299// When we have a pair of subscripts of the form [c1 + a*i] and [c2 + a*i],
1300// where i is an induction variable, c1 and c2 are loop invariant,
1301// and a is a constant, we can solve it exactly using the Strong SIV test.
1302//
1303// Can prove independence. Failing that, can compute distance (and direction).
1304// In the presence of symbolic terms, we can sometimes make progress.
1305//
1306// If there's a dependence,
1307//
1308// c1 + a*i = c2 + a*i'
1309//
1310// The dependence distance is
1311//
1312// d = i' - i = (c1 - c2)/a
1313//
1314// A dependence only exists if d is an integer and abs(d) <= U, where U is the
1315// loop's upper bound. If a dependence exists, the dependence direction is
1316// defined as
1317//
1318// { < if d > 0
1319// direction = { = if d = 0
1320// { > if d < 0
1321//
1322// Return true if dependence disproved.
1323bool DependenceInfo::strongSIVtest(const SCEV *Coeff, const SCEV *SrcConst,
1324 const SCEV *DstConst, const Loop *CurSrcLoop,
1325 const Loop *CurDstLoop, unsigned Level,
1326 FullDependence &Result,
1327 Constraint &NewConstraint) const {
1328 LLVM_DEBUG(dbgs() << "\tStrong SIV test\n");
1329 LLVM_DEBUG(dbgs() << "\t Coeff = " << *Coeff);
1330 LLVM_DEBUG(dbgs() << ", " << *Coeff->getType() << "\n");
1331 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst);
1332 LLVM_DEBUG(dbgs() << ", " << *SrcConst->getType() << "\n");
1333 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst);
1334 LLVM_DEBUG(dbgs() << ", " << *DstConst->getType() << "\n");
1335 ++StrongSIVapplications;
1336 assert(0 < Level && Level <= CommonLevels && "level out of range");
1337 Level--;
1338
1339 const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst);
1340 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta);
1341 LLVM_DEBUG(dbgs() << ", " << *Delta->getType() << "\n");
1342
1343 // check that |Delta| < iteration count
1344 if (const SCEV *UpperBound =
1345 collectUpperBound(CurSrcLoop, Delta->getType())) {
1346 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound);
1347 LLVM_DEBUG(dbgs() << ", " << *UpperBound->getType() << "\n");
1348 const SCEV *AbsDelta =
1349 SE->isKnownNonNegative(Delta) ? Delta : SE->getNegativeSCEV(Delta);
1350 const SCEV *AbsCoeff =
1351 SE->isKnownNonNegative(Coeff) ? Coeff : SE->getNegativeSCEV(Coeff);
1352 const SCEV *Product = SE->getMulExpr(UpperBound, AbsCoeff);
1353 if (isKnownPredicate(CmpInst::ICMP_SGT, AbsDelta, Product)) {
1354 // Distance greater than trip count - no dependence
1355 ++StrongSIVindependence;
1356 ++StrongSIVsuccesses;
1357 return true;
1358 }
1359 }
1360
1361 // Can we compute distance?
1362 if (isa<SCEVConstant>(Delta) && isa<SCEVConstant>(Coeff)) {
1363 APInt ConstDelta = cast<SCEVConstant>(Delta)->getAPInt();
1364 APInt ConstCoeff = cast<SCEVConstant>(Coeff)->getAPInt();
1365 APInt Distance = ConstDelta; // these need to be initialized
1366 APInt Remainder = ConstDelta;
1367 APInt::sdivrem(ConstDelta, ConstCoeff, Distance, Remainder);
1368 LLVM_DEBUG(dbgs() << "\t Distance = " << Distance << "\n");
1369 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
1370 // Make sure Coeff divides Delta exactly
1371 if (Remainder != 0) {
1372 // Coeff doesn't divide Distance, no dependence
1373 ++StrongSIVindependence;
1374 ++StrongSIVsuccesses;
1375 return true;
1376 }
1377 Result.DV[Level].Distance = SE->getConstant(Distance);
1378 NewConstraint.setDistance(SE->getConstant(Distance), CurSrcLoop,
1379 CurDstLoop);
1380 if (Distance.sgt(0))
1381 Result.DV[Level].Direction &= Dependence::DVEntry::LT;
1382 else if (Distance.slt(0))
1383 Result.DV[Level].Direction &= Dependence::DVEntry::GT;
1384 else
1385 Result.DV[Level].Direction &= Dependence::DVEntry::EQ;
1386 ++StrongSIVsuccesses;
1387 } else if (Delta->isZero()) {
1388 // since 0/X == 0
1389 Result.DV[Level].Distance = Delta;
1390 NewConstraint.setDistance(Delta, CurSrcLoop, CurDstLoop);
1391 Result.DV[Level].Direction &= Dependence::DVEntry::EQ;
1392 ++StrongSIVsuccesses;
1393 } else {
1394 if (Coeff->isOne()) {
1395 LLVM_DEBUG(dbgs() << "\t Distance = " << *Delta << "\n");
1396 Result.DV[Level].Distance = Delta; // since X/1 == X
1397 NewConstraint.setDistance(Delta, CurSrcLoop, CurDstLoop);
1398 } else {
1399 Result.Consistent = false;
1400 NewConstraint.setLine(Coeff, SE->getNegativeSCEV(Coeff),
1401 SE->getNegativeSCEV(Delta), CurSrcLoop, CurDstLoop);
1402 }
1403
1404 // maybe we can get a useful direction
1405 bool DeltaMaybeZero = !SE->isKnownNonZero(Delta);
1406 bool DeltaMaybePositive = !SE->isKnownNonPositive(Delta);
1407 bool DeltaMaybeNegative = !SE->isKnownNonNegative(Delta);
1408 bool CoeffMaybePositive = !SE->isKnownNonPositive(Coeff);
1409 bool CoeffMaybeNegative = !SE->isKnownNonNegative(Coeff);
1410 // The double negatives above are confusing.
1411 // It helps to read !SE->isKnownNonZero(Delta)
1412 // as "Delta might be Zero"
1413 unsigned NewDirection = Dependence::DVEntry::NONE;
1414 if ((DeltaMaybePositive && CoeffMaybePositive) ||
1415 (DeltaMaybeNegative && CoeffMaybeNegative))
1416 NewDirection = Dependence::DVEntry::LT;
1417 if (DeltaMaybeZero)
1418 NewDirection |= Dependence::DVEntry::EQ;
1419 if ((DeltaMaybeNegative && CoeffMaybePositive) ||
1420 (DeltaMaybePositive && CoeffMaybeNegative))
1421 NewDirection |= Dependence::DVEntry::GT;
1422 if (NewDirection < Result.DV[Level].Direction)
1423 ++StrongSIVsuccesses;
1424 Result.DV[Level].Direction &= NewDirection;
1425 }
1426 return false;
1427}
1428
1429// weakCrossingSIVtest -
1430// From the paper, Practical Dependence Testing, Section 4.2.2
1431//
1432// When we have a pair of subscripts of the form [c1 + a*i] and [c2 - a*i],
1433// where i is an induction variable, c1 and c2 are loop invariant,
1434// and a is a constant, we can solve it exactly using the
1435// Weak-Crossing SIV test.
1436//
1437// Given c1 + a*i = c2 - a*i', we can look for the intersection of
1438// the two lines, where i = i', yielding
1439//
1440// c1 + a*i = c2 - a*i
1441// 2a*i = c2 - c1
1442// i = (c2 - c1)/2a
1443//
1444// If i < 0, there is no dependence.
1445// If i > upperbound, there is no dependence.
1446// If i = 0 (i.e., if c1 = c2), there's a dependence with distance = 0.
1447// If i = upperbound, there's a dependence with distance = 0.
1448// If i is integral, there's a dependence (all directions).
1449// If the non-integer part = 1/2, there's a dependence (<> directions).
1450// Otherwise, there's no dependence.
1451//
1452// Can prove independence. Failing that,
1453// can sometimes refine the directions.
1454// Can determine iteration for splitting.
1455//
1456// Return true if dependence disproved.
1457bool DependenceInfo::weakCrossingSIVtest(
1458 const SCEV *Coeff, const SCEV *SrcConst, const SCEV *DstConst,
1459 const Loop *CurSrcLoop, const Loop *CurDstLoop, unsigned Level,
1460 FullDependence &Result, Constraint &NewConstraint,
1461 const SCEV *&SplitIter) const {
1462 LLVM_DEBUG(dbgs() << "\tWeak-Crossing SIV test\n");
1463 LLVM_DEBUG(dbgs() << "\t Coeff = " << *Coeff << "\n");
1464 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1465 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1466 ++WeakCrossingSIVapplications;
1467 assert(0 < Level && Level <= CommonLevels && "Level out of range");
1468 Level--;
1469 Result.Consistent = false;
1470 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1471 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1472 NewConstraint.setLine(Coeff, Coeff, Delta, CurSrcLoop, CurDstLoop);
1473 if (Delta->isZero()) {
1474 Result.DV[Level].Direction &= ~Dependence::DVEntry::LT;
1475 Result.DV[Level].Direction &= ~Dependence::DVEntry::GT;
1476 ++WeakCrossingSIVsuccesses;
1477 if (!Result.DV[Level].Direction) {
1478 ++WeakCrossingSIVindependence;
1479 return true;
1480 }
1481 Result.DV[Level].Distance = Delta; // = 0
1482 return false;
1483 }
1484 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(Coeff);
1485 if (!ConstCoeff)
1486 return false;
1487
1488 Result.DV[Level].Splitable = true;
1489 if (SE->isKnownNegative(ConstCoeff)) {
1490 ConstCoeff = dyn_cast<SCEVConstant>(SE->getNegativeSCEV(ConstCoeff));
1491 assert(ConstCoeff &&
1492 "dynamic cast of negative of ConstCoeff should yield constant");
1493 Delta = SE->getNegativeSCEV(Delta);
1494 }
1495 assert(SE->isKnownPositive(ConstCoeff) && "ConstCoeff should be positive");
1496
1497 // compute SplitIter for use by DependenceInfo::getSplitIteration()
1498 SplitIter = SE->getUDivExpr(
1499 SE->getSMaxExpr(SE->getZero(Delta->getType()), Delta),
1500 SE->getMulExpr(SE->getConstant(Delta->getType(), 2), ConstCoeff));
1501 LLVM_DEBUG(dbgs() << "\t Split iter = " << *SplitIter << "\n");
1502
1503 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1504 if (!ConstDelta)
1505 return false;
1506
1507 // We're certain that ConstCoeff > 0; therefore,
1508 // if Delta < 0, then no dependence.
1509 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1510 LLVM_DEBUG(dbgs() << "\t ConstCoeff = " << *ConstCoeff << "\n");
1511 if (SE->isKnownNegative(Delta)) {
1512 // No dependence, Delta < 0
1513 ++WeakCrossingSIVindependence;
1514 ++WeakCrossingSIVsuccesses;
1515 return true;
1516 }
1517
1518 // We're certain that Delta > 0 and ConstCoeff > 0.
1519 // Check Delta/(2*ConstCoeff) against upper loop bound
1520 if (const SCEV *UpperBound =
1521 collectUpperBound(CurSrcLoop, Delta->getType())) {
1522 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
1523 const SCEV *ConstantTwo = SE->getConstant(UpperBound->getType(), 2);
1524 const SCEV *ML =
1525 SE->getMulExpr(SE->getMulExpr(ConstCoeff, UpperBound), ConstantTwo);
1526 LLVM_DEBUG(dbgs() << "\t ML = " << *ML << "\n");
1527 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, ML)) {
1528 // Delta too big, no dependence
1529 ++WeakCrossingSIVindependence;
1530 ++WeakCrossingSIVsuccesses;
1531 return true;
1532 }
1533 if (isKnownPredicate(CmpInst::ICMP_EQ, Delta, ML)) {
1534 // i = i' = UB
1535 Result.DV[Level].Direction &= ~Dependence::DVEntry::LT;
1536 Result.DV[Level].Direction &= ~Dependence::DVEntry::GT;
1537 ++WeakCrossingSIVsuccesses;
1538 if (!Result.DV[Level].Direction) {
1539 ++WeakCrossingSIVindependence;
1540 return true;
1541 }
1542 Result.DV[Level].Splitable = false;
1543 Result.DV[Level].Distance = SE->getZero(Delta->getType());
1544 return false;
1545 }
1546 }
1547
1548 // check that Coeff divides Delta
1549 APInt APDelta = ConstDelta->getAPInt();
1550 APInt APCoeff = ConstCoeff->getAPInt();
1551 APInt Distance = APDelta; // these need to be initialzed
1552 APInt Remainder = APDelta;
1553 APInt::sdivrem(APDelta, APCoeff, Distance, Remainder);
1554 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
1555 if (Remainder != 0) {
1556 // Coeff doesn't divide Delta, no dependence
1557 ++WeakCrossingSIVindependence;
1558 ++WeakCrossingSIVsuccesses;
1559 return true;
1560 }
1561 LLVM_DEBUG(dbgs() << "\t Distance = " << Distance << "\n");
1562
1563 // if 2*Coeff doesn't divide Delta, then the equal direction isn't possible
1564 APInt Two = APInt(Distance.getBitWidth(), 2, true);
1565 Remainder = Distance.srem(Two);
1566 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
1567 if (Remainder != 0) {
1568 // Equal direction isn't possible
1569 Result.DV[Level].Direction &= ~Dependence::DVEntry::EQ;
1570 ++WeakCrossingSIVsuccesses;
1571 }
1572 return false;
1573}
1574
1575// Kirch's algorithm, from
1576//
1577// Optimizing Supercompilers for Supercomputers
1578// Michael Wolfe
1579// MIT Press, 1989
1580//
1581// Program 2.1, page 29.
1582// Computes the GCD of AM and BM.
1583// Also finds a solution to the equation ax - by = gcd(a, b).
1584// Returns true if dependence disproved; i.e., gcd does not divide Delta.
1585static bool findGCD(unsigned Bits, const APInt &AM, const APInt &BM,
1586 const APInt &Delta, APInt &G, APInt &X, APInt &Y) {
1587 APInt A0(Bits, 1, true), A1(Bits, 0, true);
1588 APInt B0(Bits, 0, true), B1(Bits, 1, true);
1589 APInt G0 = AM.abs();
1590 APInt G1 = BM.abs();
1591 APInt Q = G0; // these need to be initialized
1592 APInt R = G0;
1593 APInt::sdivrem(G0, G1, Q, R);
1594 while (R != 0) {
1595 // clang-format off
1596 APInt A2 = A0 - Q*A1; A0 = A1; A1 = A2;
1597 APInt B2 = B0 - Q*B1; B0 = B1; B1 = B2;
1598 G0 = G1; G1 = R;
1599 // clang-format on
1600 APInt::sdivrem(G0, G1, Q, R);
1601 }
1602 G = G1;
1603 LLVM_DEBUG(dbgs() << "\t GCD = " << G << "\n");
1604 X = AM.slt(0) ? -A1 : A1;
1605 Y = BM.slt(0) ? B1 : -B1;
1606
1607 // make sure gcd divides Delta
1608 R = Delta.srem(G);
1609 if (R != 0)
1610 return true; // gcd doesn't divide Delta, no dependence
1611 Q = Delta.sdiv(G);
1612 return false;
1613}
1614
1615static APInt floorOfQuotient(const APInt &A, const APInt &B) {
1616 APInt Q = A; // these need to be initialized
1617 APInt R = A;
1618 APInt::sdivrem(A, B, Q, R);
1619 if (R == 0)
1620 return Q;
1621 if ((A.sgt(0) && B.sgt(0)) || (A.slt(0) && B.slt(0)))
1622 return Q;
1623 else
1624 return Q - 1;
1625}
1626
1627static APInt ceilingOfQuotient(const APInt &A, const APInt &B) {
1628 APInt Q = A; // these need to be initialized
1629 APInt R = A;
1630 APInt::sdivrem(A, B, Q, R);
1631 if (R == 0)
1632 return Q;
1633 if ((A.sgt(0) && B.sgt(0)) || (A.slt(0) && B.slt(0)))
1634 return Q + 1;
1635 else
1636 return Q;
1637}
1638
1639/// Given an affine expression of the form A*k + B, where k is an arbitrary
1640/// integer, infer the possible range of k based on the known range of the
1641/// affine expression. If we know A*k + B is non-negative, i.e.,
1642///
1643/// A*k + B >= 0
1644///
1645/// we can derive the following inequalities for k when A is positive:
1646///
1647/// k >= -B / A
1648///
1649/// Since k is an integer, it means k is greater than or equal to the
1650/// ceil(-B / A).
1651///
1652/// If the upper bound of the affine expression \p UB is passed, the following
1653/// inequality can be derived as well:
1654///
1655/// A*k + B <= UB
1656///
1657/// which leads to:
1658///
1659/// k <= (UB - B) / A
1660///
1661/// Again, as k is an integer, it means k is less than or equal to the
1662/// floor((UB - B) / A).
1663///
1664/// The similar logic applies when A is negative, but the inequalities sign flip
1665/// while working with them.
1666///
1667/// Preconditions: \p A is non-zero, and we know A*k + B is non-negative.
1668static std::pair<std::optional<APInt>, std::optional<APInt>>
1670 const std::optional<APInt> &UB) {
1671 assert(A != 0 && "A must be non-zero");
1672 std::optional<APInt> TL, TU;
1673 if (A.sgt(0)) {
1674 TL = ceilingOfQuotient(-B, A);
1675 LLVM_DEBUG(dbgs() << "\t Possible TL = " << *TL << "\n");
1676 // New bound check - modification to Banerjee's e3 check
1677 if (UB) {
1678 // TODO?: Overflow check for UB - B
1679 TU = floorOfQuotient(*UB - B, A);
1680 LLVM_DEBUG(dbgs() << "\t Possible TU = " << *TU << "\n");
1681 }
1682 } else {
1683 TU = floorOfQuotient(-B, A);
1684 LLVM_DEBUG(dbgs() << "\t Possible TU = " << *TU << "\n");
1685 // New bound check - modification to Banerjee's e3 check
1686 if (UB) {
1687 // TODO?: Overflow check for UB - B
1688 TL = ceilingOfQuotient(*UB - B, A);
1689 LLVM_DEBUG(dbgs() << "\t Possible TL = " << *TL << "\n");
1690 }
1691 }
1692 return std::make_pair(TL, TU);
1693}
1694
1695// exactSIVtest -
1696// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*i],
1697// where i is an induction variable, c1 and c2 are loop invariant, and a1
1698// and a2 are constant, we can solve it exactly using an algorithm developed
1699// by Banerjee and Wolfe. See Algorithm 6.2.1 (case 2.5) in:
1700//
1701// Dependence Analysis for Supercomputing
1702// Utpal Banerjee
1703// Kluwer Academic Publishers, 1988
1704//
1705// It's slower than the specialized tests (strong SIV, weak-zero SIV, etc),
1706// so use them if possible. They're also a bit better with symbolics and,
1707// in the case of the strong SIV test, can compute Distances.
1708//
1709// Return true if dependence disproved.
1710//
1711// This is a modified version of the original Banerjee algorithm. The original
1712// only tested whether Dst depends on Src. This algorithm extends that and
1713// returns all the dependencies that exist between Dst and Src.
1714bool DependenceInfo::exactSIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
1715 const SCEV *SrcConst, const SCEV *DstConst,
1716 const Loop *CurSrcLoop,
1717 const Loop *CurDstLoop, unsigned Level,
1718 FullDependence &Result,
1719 Constraint &NewConstraint) const {
1720 LLVM_DEBUG(dbgs() << "\tExact SIV test\n");
1721 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n");
1722 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n");
1723 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1724 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1725 ++ExactSIVapplications;
1726 assert(0 < Level && Level <= CommonLevels && "Level out of range");
1727 Level--;
1728 Result.Consistent = false;
1729 const SCEV *Delta = minusSCEVNoSignedOverflow(DstConst, SrcConst, *SE);
1730 if (!Delta)
1731 return false;
1732 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1733 NewConstraint.setLine(SrcCoeff, SE->getNegativeSCEV(DstCoeff), Delta,
1734 CurSrcLoop, CurDstLoop);
1735 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1736 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1737 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1738 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
1739 return false;
1740
1741 // find gcd
1742 APInt G, X, Y;
1743 APInt AM = ConstSrcCoeff->getAPInt();
1744 APInt BM = ConstDstCoeff->getAPInt();
1745 APInt CM = ConstDelta->getAPInt();
1746 unsigned Bits = AM.getBitWidth();
1747 if (findGCD(Bits, AM, BM, CM, G, X, Y)) {
1748 // gcd doesn't divide Delta, no dependence
1749 ++ExactSIVindependence;
1750 ++ExactSIVsuccesses;
1751 return true;
1752 }
1753
1754 LLVM_DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n");
1755
1756 // since SCEV construction normalizes, LM = 0
1757 std::optional<APInt> UM;
1758 // UM is perhaps unavailable, let's check
1759 if (const SCEVConstant *CUB =
1760 collectConstantUpperBound(CurSrcLoop, Delta->getType())) {
1761 UM = CUB->getAPInt();
1762 LLVM_DEBUG(dbgs() << "\t UM = " << *UM << "\n");
1763 }
1764
1765 APInt TU(APInt::getSignedMaxValue(Bits));
1766 APInt TL(APInt::getSignedMinValue(Bits));
1767 APInt TC = CM.sdiv(G);
1768 APInt TX = X * TC;
1769 APInt TY = Y * TC;
1770 LLVM_DEBUG(dbgs() << "\t TC = " << TC << "\n");
1771 LLVM_DEBUG(dbgs() << "\t TX = " << TX << "\n");
1772 LLVM_DEBUG(dbgs() << "\t TY = " << TY << "\n");
1773
1774 APInt TB = BM.sdiv(G);
1775 APInt TA = AM.sdiv(G);
1776
1777 // At this point, we have the following equations:
1778 //
1779 // TA*i0 - TB*i1 = TC
1780 //
1781 // Also, we know that the all pairs of (i0, i1) can be expressed as:
1782 //
1783 // (TX + k*TB, TY + k*TA)
1784 //
1785 // where k is an arbitrary integer.
1786 auto [TL0, TU0] = inferDomainOfAffine(TB, TX, UM);
1787 auto [TL1, TU1] = inferDomainOfAffine(TA, TY, UM);
1788
1789 auto CreateVec = [](const std::optional<APInt> &V0,
1790 const std::optional<APInt> &V1) {
1792 if (V0)
1793 Vec.push_back(*V0);
1794 if (V1)
1795 Vec.push_back(*V1);
1796 return Vec;
1797 };
1798
1799 SmallVector<APInt, 2> TLVec = CreateVec(TL0, TL1);
1800 SmallVector<APInt, 2> TUVec = CreateVec(TU0, TU1);
1801
1802 LLVM_DEBUG(dbgs() << "\t TA = " << TA << "\n");
1803 LLVM_DEBUG(dbgs() << "\t TB = " << TB << "\n");
1804
1805 if (TLVec.empty() || TUVec.empty())
1806 return false;
1807 TL = APIntOps::smax(TLVec.front(), TLVec.back());
1808 TU = APIntOps::smin(TUVec.front(), TUVec.back());
1809 LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n");
1810 LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n");
1811
1812 if (TL.sgt(TU)) {
1813 ++ExactSIVindependence;
1814 ++ExactSIVsuccesses;
1815 return true;
1816 }
1817
1818 // explore directions
1819 unsigned NewDirection = Dependence::DVEntry::NONE;
1820 APInt LowerDistance, UpperDistance;
1821 // TODO: Overflow check may be needed.
1822 if (TA.sgt(TB)) {
1823 LowerDistance = (TY - TX) + (TA - TB) * TL;
1824 UpperDistance = (TY - TX) + (TA - TB) * TU;
1825 } else {
1826 LowerDistance = (TY - TX) + (TA - TB) * TU;
1827 UpperDistance = (TY - TX) + (TA - TB) * TL;
1828 }
1829
1830 LLVM_DEBUG(dbgs() << "\t LowerDistance = " << LowerDistance << "\n");
1831 LLVM_DEBUG(dbgs() << "\t UpperDistance = " << UpperDistance << "\n");
1832
1833 APInt Zero(Bits, 0, true);
1834 if (LowerDistance.sle(Zero) && UpperDistance.sge(Zero)) {
1835 NewDirection |= Dependence::DVEntry::EQ;
1836 ++ExactSIVsuccesses;
1837 }
1838 if (LowerDistance.slt(0)) {
1839 NewDirection |= Dependence::DVEntry::GT;
1840 ++ExactSIVsuccesses;
1841 }
1842 if (UpperDistance.sgt(0)) {
1843 NewDirection |= Dependence::DVEntry::LT;
1844 ++ExactSIVsuccesses;
1845 }
1846
1847 // finished
1848 Result.DV[Level].Direction &= NewDirection;
1849 if (Result.DV[Level].Direction == Dependence::DVEntry::NONE)
1850 ++ExactSIVindependence;
1851 LLVM_DEBUG(dbgs() << "\t Result = ");
1852 LLVM_DEBUG(Result.dump(dbgs()));
1853 return Result.DV[Level].Direction == Dependence::DVEntry::NONE;
1854}
1855
1856// Return true if the divisor evenly divides the dividend.
1857static bool isRemainderZero(const SCEVConstant *Dividend,
1858 const SCEVConstant *Divisor) {
1859 const APInt &ConstDividend = Dividend->getAPInt();
1860 const APInt &ConstDivisor = Divisor->getAPInt();
1861 return ConstDividend.srem(ConstDivisor) == 0;
1862}
1863
1864// weakZeroSrcSIVtest -
1865// From the paper, Practical Dependence Testing, Section 4.2.2
1866//
1867// When we have a pair of subscripts of the form [c1] and [c2 + a*i],
1868// where i is an induction variable, c1 and c2 are loop invariant,
1869// and a is a constant, we can solve it exactly using the
1870// Weak-Zero SIV test.
1871//
1872// Given
1873//
1874// c1 = c2 + a*i
1875//
1876// we get
1877//
1878// (c1 - c2)/a = i
1879//
1880// If i is not an integer, there's no dependence.
1881// If i < 0 or > UB, there's no dependence.
1882// If i = 0, the direction is >= and peeling the
1883// 1st iteration will break the dependence.
1884// If i = UB, the direction is <= and peeling the
1885// last iteration will break the dependence.
1886// Otherwise, the direction is *.
1887//
1888// Can prove independence. Failing that, we can sometimes refine
1889// the directions. Can sometimes show that first or last
1890// iteration carries all the dependences (so worth peeling).
1891//
1892// (see also weakZeroDstSIVtest)
1893//
1894// Return true if dependence disproved.
1895bool DependenceInfo::weakZeroSrcSIVtest(
1896 const SCEV *DstCoeff, const SCEV *SrcConst, const SCEV *DstConst,
1897 const Loop *CurSrcLoop, const Loop *CurDstLoop, unsigned Level,
1898 FullDependence &Result, Constraint &NewConstraint) const {
1899 // For the WeakSIV test, it's possible the loop isn't common to
1900 // the Src and Dst loops. If it isn't, then there's no need to
1901 // record a direction.
1902 LLVM_DEBUG(dbgs() << "\tWeak-Zero (src) SIV test\n");
1903 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << "\n");
1904 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1905 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1906 ++WeakZeroSIVapplications;
1907 assert(0 < Level && Level <= MaxLevels && "Level out of range");
1908 Level--;
1909 Result.Consistent = false;
1910 const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst);
1911 NewConstraint.setLine(SE->getZero(Delta->getType()), DstCoeff, Delta,
1912 CurSrcLoop, CurDstLoop);
1913 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1914 if (isKnownPredicate(CmpInst::ICMP_EQ, SrcConst, DstConst)) {
1915 if (Level < CommonLevels) {
1916 Result.DV[Level].Direction &= Dependence::DVEntry::GE;
1917 Result.DV[Level].PeelFirst = true;
1918 ++WeakZeroSIVsuccesses;
1919 }
1920 return false; // dependences caused by first iteration
1921 }
1922 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1923 if (!ConstCoeff)
1924 return false;
1925 const SCEV *AbsCoeff = SE->isKnownNegative(ConstCoeff)
1926 ? SE->getNegativeSCEV(ConstCoeff)
1927 : ConstCoeff;
1928 const SCEV *NewDelta =
1929 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
1930
1931 // check that Delta/SrcCoeff < iteration count
1932 // really check NewDelta < count*AbsCoeff
1933 if (const SCEV *UpperBound =
1934 collectUpperBound(CurSrcLoop, Delta->getType())) {
1935 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
1936 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound);
1937 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) {
1938 ++WeakZeroSIVindependence;
1939 ++WeakZeroSIVsuccesses;
1940 return true;
1941 }
1942 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) {
1943 // dependences caused by last iteration
1944 if (Level < CommonLevels) {
1945 Result.DV[Level].Direction &= Dependence::DVEntry::LE;
1946 Result.DV[Level].PeelLast = true;
1947 ++WeakZeroSIVsuccesses;
1948 }
1949 return false;
1950 }
1951 }
1952
1953 // check that Delta/SrcCoeff >= 0
1954 // really check that NewDelta >= 0
1955 if (SE->isKnownNegative(NewDelta)) {
1956 // No dependence, newDelta < 0
1957 ++WeakZeroSIVindependence;
1958 ++WeakZeroSIVsuccesses;
1959 return true;
1960 }
1961
1962 // if SrcCoeff doesn't divide Delta, then no dependence
1963 if (isa<SCEVConstant>(Delta) &&
1964 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) {
1965 ++WeakZeroSIVindependence;
1966 ++WeakZeroSIVsuccesses;
1967 return true;
1968 }
1969 return false;
1970}
1971
1972// weakZeroDstSIVtest -
1973// From the paper, Practical Dependence Testing, Section 4.2.2
1974//
1975// When we have a pair of subscripts of the form [c1 + a*i] and [c2],
1976// where i is an induction variable, c1 and c2 are loop invariant,
1977// and a is a constant, we can solve it exactly using the
1978// Weak-Zero SIV test.
1979//
1980// Given
1981//
1982// c1 + a*i = c2
1983//
1984// we get
1985//
1986// i = (c2 - c1)/a
1987//
1988// If i is not an integer, there's no dependence.
1989// If i < 0 or > UB, there's no dependence.
1990// If i = 0, the direction is <= and peeling the
1991// 1st iteration will break the dependence.
1992// If i = UB, the direction is >= and peeling the
1993// last iteration will break the dependence.
1994// Otherwise, the direction is *.
1995//
1996// Can prove independence. Failing that, we can sometimes refine
1997// the directions. Can sometimes show that first or last
1998// iteration carries all the dependences (so worth peeling).
1999//
2000// (see also weakZeroSrcSIVtest)
2001//
2002// Return true if dependence disproved.
2003bool DependenceInfo::weakZeroDstSIVtest(
2004 const SCEV *SrcCoeff, const SCEV *SrcConst, const SCEV *DstConst,
2005 const Loop *CurSrcLoop, const Loop *CurDstLoop, unsigned Level,
2006 FullDependence &Result, Constraint &NewConstraint) const {
2007 // For the WeakSIV test, it's possible the loop isn't common to the
2008 // Src and Dst loops. If it isn't, then there's no need to record a direction.
2009 LLVM_DEBUG(dbgs() << "\tWeak-Zero (dst) SIV test\n");
2010 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << "\n");
2011 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
2012 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
2013 ++WeakZeroSIVapplications;
2014 assert(0 < Level && Level <= SrcLevels && "Level out of range");
2015 Level--;
2016 Result.Consistent = false;
2017 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
2018 NewConstraint.setLine(SrcCoeff, SE->getZero(Delta->getType()), Delta,
2019 CurSrcLoop, CurDstLoop);
2020 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
2021 if (isKnownPredicate(CmpInst::ICMP_EQ, DstConst, SrcConst)) {
2022 if (Level < CommonLevels) {
2023 Result.DV[Level].Direction &= Dependence::DVEntry::LE;
2024 Result.DV[Level].PeelFirst = true;
2025 ++WeakZeroSIVsuccesses;
2026 }
2027 return false; // dependences caused by first iteration
2028 }
2029 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
2030 if (!ConstCoeff)
2031 return false;
2032 const SCEV *AbsCoeff = SE->isKnownNegative(ConstCoeff)
2033 ? SE->getNegativeSCEV(ConstCoeff)
2034 : ConstCoeff;
2035 const SCEV *NewDelta =
2036 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
2037
2038 // check that Delta/SrcCoeff < iteration count
2039 // really check NewDelta < count*AbsCoeff
2040 if (const SCEV *UpperBound =
2041 collectUpperBound(CurSrcLoop, Delta->getType())) {
2042 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
2043 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound);
2044 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) {
2045 ++WeakZeroSIVindependence;
2046 ++WeakZeroSIVsuccesses;
2047 return true;
2048 }
2049 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) {
2050 // dependences caused by last iteration
2051 if (Level < CommonLevels) {
2052 Result.DV[Level].Direction &= Dependence::DVEntry::GE;
2053 Result.DV[Level].PeelLast = true;
2054 ++WeakZeroSIVsuccesses;
2055 }
2056 return false;
2057 }
2058 }
2059
2060 // check that Delta/SrcCoeff >= 0
2061 // really check that NewDelta >= 0
2062 if (SE->isKnownNegative(NewDelta)) {
2063 // No dependence, newDelta < 0
2064 ++WeakZeroSIVindependence;
2065 ++WeakZeroSIVsuccesses;
2066 return true;
2067 }
2068
2069 // if SrcCoeff doesn't divide Delta, then no dependence
2070 if (isa<SCEVConstant>(Delta) &&
2071 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) {
2072 ++WeakZeroSIVindependence;
2073 ++WeakZeroSIVsuccesses;
2074 return true;
2075 }
2076 return false;
2077}
2078
2079// exactRDIVtest - Tests the RDIV subscript pair for dependence.
2080// Things of the form [c1 + a*i] and [c2 + b*j],
2081// where i and j are induction variable, c1 and c2 are loop invariant,
2082// and a and b are constants.
2083// Returns true if any possible dependence is disproved.
2084// Marks the result as inconsistent.
2085// Works in some cases that symbolicRDIVtest doesn't, and vice versa.
2086bool DependenceInfo::exactRDIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
2087 const SCEV *SrcConst, const SCEV *DstConst,
2088 const Loop *SrcLoop, const Loop *DstLoop,
2089 FullDependence &Result) const {
2091 return false;
2092 LLVM_DEBUG(dbgs() << "\tExact RDIV test\n");
2093 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n");
2094 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n");
2095 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
2096 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
2097 ++ExactRDIVapplications;
2098 Result.Consistent = false;
2099 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
2100 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
2101 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
2102 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
2103 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
2104 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
2105 return false;
2106
2107 // find gcd
2108 APInt G, X, Y;
2109 APInt AM = ConstSrcCoeff->getAPInt();
2110 APInt BM = ConstDstCoeff->getAPInt();
2111 APInt CM = ConstDelta->getAPInt();
2112 unsigned Bits = AM.getBitWidth();
2113 if (findGCD(Bits, AM, BM, CM, G, X, Y)) {
2114 // gcd doesn't divide Delta, no dependence
2115 ++ExactRDIVindependence;
2116 return true;
2117 }
2118
2119 LLVM_DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n");
2120
2121 // since SCEV construction seems to normalize, LM = 0
2122 std::optional<APInt> SrcUM;
2123 // SrcUM is perhaps unavailable, let's check
2124 if (const SCEVConstant *UpperBound =
2125 collectConstantUpperBound(SrcLoop, Delta->getType())) {
2126 SrcUM = UpperBound->getAPInt();
2127 LLVM_DEBUG(dbgs() << "\t SrcUM = " << *SrcUM << "\n");
2128 }
2129
2130 std::optional<APInt> DstUM;
2131 // UM is perhaps unavailable, let's check
2132 if (const SCEVConstant *UpperBound =
2133 collectConstantUpperBound(DstLoop, Delta->getType())) {
2134 DstUM = UpperBound->getAPInt();
2135 LLVM_DEBUG(dbgs() << "\t DstUM = " << *DstUM << "\n");
2136 }
2137
2138 APInt TU(APInt::getSignedMaxValue(Bits));
2139 APInt TL(APInt::getSignedMinValue(Bits));
2140 APInt TC = CM.sdiv(G);
2141 APInt TX = X * TC;
2142 APInt TY = Y * TC;
2143 LLVM_DEBUG(dbgs() << "\t TC = " << TC << "\n");
2144 LLVM_DEBUG(dbgs() << "\t TX = " << TX << "\n");
2145 LLVM_DEBUG(dbgs() << "\t TY = " << TY << "\n");
2146
2147 APInt TB = BM.sdiv(G);
2148 APInt TA = AM.sdiv(G);
2149
2150 // At this point, we have the following equations:
2151 //
2152 // TA*i - TB*j = TC
2153 //
2154 // Also, we know that the all pairs of (i, j) can be expressed as:
2155 //
2156 // (TX + k*TB, TY + k*TA)
2157 //
2158 // where k is an arbitrary integer.
2159 auto [TL0, TU0] = inferDomainOfAffine(TB, TX, SrcUM);
2160 auto [TL1, TU1] = inferDomainOfAffine(TA, TY, DstUM);
2161
2162 LLVM_DEBUG(dbgs() << "\t TA = " << TA << "\n");
2163 LLVM_DEBUG(dbgs() << "\t TB = " << TB << "\n");
2164
2165 auto CreateVec = [](const std::optional<APInt> &V0,
2166 const std::optional<APInt> &V1) {
2168 if (V0)
2169 Vec.push_back(*V0);
2170 if (V1)
2171 Vec.push_back(*V1);
2172 return Vec;
2173 };
2174
2175 SmallVector<APInt, 2> TLVec = CreateVec(TL0, TL1);
2176 SmallVector<APInt, 2> TUVec = CreateVec(TU0, TU1);
2177 if (TLVec.empty() || TUVec.empty())
2178 return false;
2179
2180 TL = APIntOps::smax(TLVec.front(), TLVec.back());
2181 TU = APIntOps::smin(TUVec.front(), TUVec.back());
2182 LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n");
2183 LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n");
2184
2185 if (TL.sgt(TU))
2186 ++ExactRDIVindependence;
2187 return TL.sgt(TU);
2188}
2189
2190// symbolicRDIVtest -
2191// In Section 4.5 of the Practical Dependence Testing paper,the authors
2192// introduce a special case of Banerjee's Inequalities (also called the
2193// Extreme-Value Test) that can handle some of the SIV and RDIV cases,
2194// particularly cases with symbolics. Since it's only able to disprove
2195// dependence (not compute distances or directions), we'll use it as a
2196// fall back for the other tests.
2197//
2198// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
2199// where i and j are induction variables and c1 and c2 are loop invariants,
2200// we can use the symbolic tests to disprove some dependences, serving as a
2201// backup for the RDIV test. Note that i and j can be the same variable,
2202// letting this test serve as a backup for the various SIV tests.
2203//
2204// For a dependence to exist, c1 + a1*i must equal c2 + a2*j for some
2205// 0 <= i <= N1 and some 0 <= j <= N2, where N1 and N2 are the (normalized)
2206// loop bounds for the i and j loops, respectively. So, ...
2207//
2208// c1 + a1*i = c2 + a2*j
2209// a1*i - a2*j = c2 - c1
2210//
2211// To test for a dependence, we compute c2 - c1 and make sure it's in the
2212// range of the maximum and minimum possible values of a1*i - a2*j.
2213// Considering the signs of a1 and a2, we have 4 possible cases:
2214//
2215// 1) If a1 >= 0 and a2 >= 0, then
2216// a1*0 - a2*N2 <= c2 - c1 <= a1*N1 - a2*0
2217// -a2*N2 <= c2 - c1 <= a1*N1
2218//
2219// 2) If a1 >= 0 and a2 <= 0, then
2220// a1*0 - a2*0 <= c2 - c1 <= a1*N1 - a2*N2
2221// 0 <= c2 - c1 <= a1*N1 - a2*N2
2222//
2223// 3) If a1 <= 0 and a2 >= 0, then
2224// a1*N1 - a2*N2 <= c2 - c1 <= a1*0 - a2*0
2225// a1*N1 - a2*N2 <= c2 - c1 <= 0
2226//
2227// 4) If a1 <= 0 and a2 <= 0, then
2228// a1*N1 - a2*0 <= c2 - c1 <= a1*0 - a2*N2
2229// a1*N1 <= c2 - c1 <= -a2*N2
2230//
2231// return true if dependence disproved
2232bool DependenceInfo::symbolicRDIVtest(const SCEV *A1, const SCEV *A2,
2233 const SCEV *C1, const SCEV *C2,
2234 const Loop *Loop1,
2235 const Loop *Loop2) const {
2237 return false;
2238 ++SymbolicRDIVapplications;
2239 LLVM_DEBUG(dbgs() << "\ttry symbolic RDIV test\n");
2240 LLVM_DEBUG(dbgs() << "\t A1 = " << *A1);
2241 LLVM_DEBUG(dbgs() << ", type = " << *A1->getType() << "\n");
2242 LLVM_DEBUG(dbgs() << "\t A2 = " << *A2 << "\n");
2243 LLVM_DEBUG(dbgs() << "\t C1 = " << *C1 << "\n");
2244 LLVM_DEBUG(dbgs() << "\t C2 = " << *C2 << "\n");
2245 const SCEV *N1 = collectUpperBound(Loop1, A1->getType());
2246 const SCEV *N2 = collectUpperBound(Loop2, A1->getType());
2247 LLVM_DEBUG(if (N1) dbgs() << "\t N1 = " << *N1 << "\n");
2248 LLVM_DEBUG(if (N2) dbgs() << "\t N2 = " << *N2 << "\n");
2249 const SCEV *C2_C1 = SE->getMinusSCEV(C2, C1);
2250 const SCEV *C1_C2 = SE->getMinusSCEV(C1, C2);
2251 LLVM_DEBUG(dbgs() << "\t C2 - C1 = " << *C2_C1 << "\n");
2252 LLVM_DEBUG(dbgs() << "\t C1 - C2 = " << *C1_C2 << "\n");
2253 if (SE->isKnownNonNegative(A1)) {
2254 if (SE->isKnownNonNegative(A2)) {
2255 // A1 >= 0 && A2 >= 0
2256 if (N1) {
2257 // make sure that c2 - c1 <= a1*N1
2258 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2259 LLVM_DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n");
2260 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1)) {
2261 ++SymbolicRDIVindependence;
2262 return true;
2263 }
2264 }
2265 if (N2) {
2266 // make sure that -a2*N2 <= c2 - c1, or a2*N2 >= c1 - c2
2267 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2268 LLVM_DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n");
2269 if (isKnownPredicate(CmpInst::ICMP_SLT, A2N2, C1_C2)) {
2270 ++SymbolicRDIVindependence;
2271 return true;
2272 }
2273 }
2274 } else if (SE->isKnownNonPositive(A2)) {
2275 // a1 >= 0 && a2 <= 0
2276 if (N1 && N2) {
2277 // make sure that c2 - c1 <= a1*N1 - a2*N2
2278 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2279 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2280 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2);
2281 LLVM_DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n");
2282 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1_A2N2)) {
2283 ++SymbolicRDIVindependence;
2284 return true;
2285 }
2286 }
2287 // make sure that 0 <= c2 - c1
2288 if (SE->isKnownNegative(C2_C1)) {
2289 ++SymbolicRDIVindependence;
2290 return true;
2291 }
2292 }
2293 } else if (SE->isKnownNonPositive(A1)) {
2294 if (SE->isKnownNonNegative(A2)) {
2295 // a1 <= 0 && a2 >= 0
2296 if (N1 && N2) {
2297 // make sure that a1*N1 - a2*N2 <= c2 - c1
2298 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2299 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2300 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2);
2301 LLVM_DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n");
2302 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1_A2N2, C2_C1)) {
2303 ++SymbolicRDIVindependence;
2304 return true;
2305 }
2306 }
2307 // make sure that c2 - c1 <= 0
2308 if (SE->isKnownPositive(C2_C1)) {
2309 ++SymbolicRDIVindependence;
2310 return true;
2311 }
2312 } else if (SE->isKnownNonPositive(A2)) {
2313 // a1 <= 0 && a2 <= 0
2314 if (N1) {
2315 // make sure that a1*N1 <= c2 - c1
2316 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2317 LLVM_DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n");
2318 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1, C2_C1)) {
2319 ++SymbolicRDIVindependence;
2320 return true;
2321 }
2322 }
2323 if (N2) {
2324 // make sure that c2 - c1 <= -a2*N2, or c1 - c2 >= a2*N2
2325 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2326 LLVM_DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n");
2327 if (isKnownPredicate(CmpInst::ICMP_SLT, C1_C2, A2N2)) {
2328 ++SymbolicRDIVindependence;
2329 return true;
2330 }
2331 }
2332 }
2333 }
2334 return false;
2335}
2336
2337// testSIV -
2338// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 - a2*i]
2339// where i is an induction variable, c1 and c2 are loop invariant, and a1 and
2340// a2 are constant, we attack it with an SIV test. While they can all be
2341// solved with the Exact SIV test, it's worthwhile to use simpler tests when
2342// they apply; they're cheaper and sometimes more precise.
2343//
2344// Return true if dependence disproved.
2345bool DependenceInfo::testSIV(const SCEV *Src, const SCEV *Dst, unsigned &Level,
2346 FullDependence &Result, Constraint &NewConstraint,
2347 const SCEV *&SplitIter) const {
2348 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
2349 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
2350 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
2351 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
2352 if (SrcAddRec && DstAddRec) {
2353 const SCEV *SrcConst = SrcAddRec->getStart();
2354 const SCEV *DstConst = DstAddRec->getStart();
2355 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2356 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE);
2357 const Loop *CurSrcLoop = SrcAddRec->getLoop();
2358 const Loop *CurDstLoop = DstAddRec->getLoop();
2359 assert(haveSameSD(CurSrcLoop, CurDstLoop) &&
2360 "Loops in the SIV test should have the same iteration space and "
2361 "depth");
2362 Level = mapSrcLoop(CurSrcLoop);
2363 bool disproven;
2364 if (SrcCoeff == DstCoeff)
2365 disproven = strongSIVtest(SrcCoeff, SrcConst, DstConst, CurSrcLoop,
2366 CurDstLoop, Level, Result, NewConstraint);
2367 else if (SrcCoeff == SE->getNegativeSCEV(DstCoeff))
2368 disproven = weakCrossingSIVtest(SrcCoeff, SrcConst, DstConst, CurSrcLoop,
2369 CurDstLoop, Level, Result, NewConstraint,
2370 SplitIter);
2371 else
2372 disproven =
2373 exactSIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurSrcLoop,
2374 CurDstLoop, Level, Result, NewConstraint);
2375 return disproven || gcdMIVtest(Src, Dst, Result) ||
2376 symbolicRDIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurSrcLoop,
2377 CurDstLoop);
2378 }
2379 if (SrcAddRec) {
2380 const SCEV *SrcConst = SrcAddRec->getStart();
2381 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2382 const SCEV *DstConst = Dst;
2383 const Loop *CurSrcLoop = SrcAddRec->getLoop();
2384 Level = mapSrcLoop(CurSrcLoop);
2385 return weakZeroDstSIVtest(SrcCoeff, SrcConst, DstConst, CurSrcLoop,
2386 CurSrcLoop, Level, Result, NewConstraint) ||
2387 gcdMIVtest(Src, Dst, Result);
2388 }
2389 if (DstAddRec) {
2390 const SCEV *DstConst = DstAddRec->getStart();
2391 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE);
2392 const SCEV *SrcConst = Src;
2393 const Loop *CurDstLoop = DstAddRec->getLoop();
2394 Level = mapDstLoop(CurDstLoop);
2395 return weakZeroSrcSIVtest(DstCoeff, SrcConst, DstConst, CurDstLoop,
2396 CurDstLoop, Level, Result, NewConstraint) ||
2397 gcdMIVtest(Src, Dst, Result);
2398 }
2399 llvm_unreachable("SIV test expected at least one AddRec");
2400 return false;
2401}
2402
2403// testRDIV -
2404// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
2405// where i and j are induction variables, c1 and c2 are loop invariant,
2406// and a1 and a2 are constant, we can solve it exactly with an easy adaptation
2407// of the Exact SIV test, the Restricted Double Index Variable (RDIV) test.
2408// It doesn't make sense to talk about distance or direction in this case,
2409// so there's no point in making special versions of the Strong SIV test or
2410// the Weak-crossing SIV test.
2411//
2412// With minor algebra, this test can also be used for things like
2413// [c1 + a1*i + a2*j][c2].
2414//
2415// Return true if dependence disproved.
2416bool DependenceInfo::testRDIV(const SCEV *Src, const SCEV *Dst,
2417 FullDependence &Result) const {
2418 // we have 3 possible situations here:
2419 // 1) [a*i + b] and [c*j + d]
2420 // 2) [a*i + c*j + b] and [d]
2421 // 3) [b] and [a*i + c*j + d]
2422 // We need to find what we've got and get organized
2423
2424 const SCEV *SrcConst, *DstConst;
2425 const SCEV *SrcCoeff, *DstCoeff;
2426 const Loop *SrcLoop, *DstLoop;
2427
2428 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
2429 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
2430 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
2431 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
2432 if (SrcAddRec && DstAddRec) {
2433 SrcConst = SrcAddRec->getStart();
2434 SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2435 SrcLoop = SrcAddRec->getLoop();
2436 DstConst = DstAddRec->getStart();
2437 DstCoeff = DstAddRec->getStepRecurrence(*SE);
2438 DstLoop = DstAddRec->getLoop();
2439 } else if (SrcAddRec) {
2440 if (const SCEVAddRecExpr *tmpAddRec =
2441 dyn_cast<SCEVAddRecExpr>(SrcAddRec->getStart())) {
2442 SrcConst = tmpAddRec->getStart();
2443 SrcCoeff = tmpAddRec->getStepRecurrence(*SE);
2444 SrcLoop = tmpAddRec->getLoop();
2445 DstConst = Dst;
2446 DstCoeff = SE->getNegativeSCEV(SrcAddRec->getStepRecurrence(*SE));
2447 DstLoop = SrcAddRec->getLoop();
2448 } else
2449 llvm_unreachable("RDIV reached by surprising SCEVs");
2450 } else if (DstAddRec) {
2451 if (const SCEVAddRecExpr *tmpAddRec =
2452 dyn_cast<SCEVAddRecExpr>(DstAddRec->getStart())) {
2453 DstConst = tmpAddRec->getStart();
2454 DstCoeff = tmpAddRec->getStepRecurrence(*SE);
2455 DstLoop = tmpAddRec->getLoop();
2456 SrcConst = Src;
2457 SrcCoeff = SE->getNegativeSCEV(DstAddRec->getStepRecurrence(*SE));
2458 SrcLoop = DstAddRec->getLoop();
2459 } else
2460 llvm_unreachable("RDIV reached by surprising SCEVs");
2461 } else
2462 llvm_unreachable("RDIV expected at least one AddRec");
2463 return exactRDIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, SrcLoop, DstLoop,
2464 Result) ||
2465 gcdMIVtest(Src, Dst, Result) ||
2466 symbolicRDIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, SrcLoop,
2467 DstLoop);
2468}
2469
2470// Tests the single-subscript MIV pair (Src and Dst) for dependence.
2471// Return true if dependence disproved.
2472// Can sometimes refine direction vectors.
2473bool DependenceInfo::testMIV(const SCEV *Src, const SCEV *Dst,
2474 const SmallBitVector &Loops,
2475 FullDependence &Result) const {
2476 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
2477 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
2478 Result.Consistent = false;
2479 return gcdMIVtest(Src, Dst, Result) ||
2480 banerjeeMIVtest(Src, Dst, Loops, Result);
2481}
2482
2483// Given a product, e.g., 10*X*Y, returns the first constant operand,
2484// in this case 10. If there is no constant part, returns std::nullopt.
2485static std::optional<APInt> getConstantPart(const SCEV *Expr) {
2486 if (const auto *Constant = dyn_cast<SCEVConstant>(Expr))
2487 return Constant->getAPInt();
2488 if (const auto *Product = dyn_cast<SCEVMulExpr>(Expr))
2489 if (const auto *Constant = dyn_cast<SCEVConstant>(Product->getOperand(0)))
2490 return Constant->getAPInt();
2491 return std::nullopt;
2492}
2493
2494bool DependenceInfo::accumulateCoefficientsGCD(const SCEV *Expr,
2495 const Loop *CurLoop,
2496 const SCEV *&CurLoopCoeff,
2497 APInt &RunningGCD) const {
2498 // If RunningGCD is already 1, exit early.
2499 // TODO: It might be better to continue the recursion to find CurLoopCoeff.
2500 if (RunningGCD == 1)
2501 return true;
2502
2503 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
2504 if (!AddRec) {
2505 assert(isLoopInvariant(Expr, CurLoop) &&
2506 "Expected loop invariant expression");
2507 return true;
2508 }
2509
2510 assert(AddRec->isAffine() && "Unexpected Expr");
2511 const SCEV *Start = AddRec->getStart();
2512 const SCEV *Step = AddRec->getStepRecurrence(*SE);
2513 if (AddRec->getLoop() == CurLoop) {
2514 CurLoopCoeff = Step;
2515 } else {
2516 std::optional<APInt> ConstCoeff = getConstantPart(Step);
2517
2518 // If the coefficient is the product of a constant and other stuff, we can
2519 // use the constant in the GCD computation.
2520 if (!ConstCoeff)
2521 return false;
2522
2523 // TODO: What happens if ConstCoeff is the "most negative" signed number
2524 // (e.g. -128 for 8 bit wide APInt)?
2525 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
2526 }
2527
2528 return accumulateCoefficientsGCD(Start, CurLoop, CurLoopCoeff, RunningGCD);
2529}
2530
2531//===----------------------------------------------------------------------===//
2532// gcdMIVtest -
2533// Tests an MIV subscript pair for dependence.
2534// Returns true if any possible dependence is disproved.
2535// Marks the result as inconsistent.
2536// Can sometimes disprove the equal direction for 1 or more loops,
2537// as discussed in Michael Wolfe's book,
2538// High Performance Compilers for Parallel Computing, page 235.
2539//
2540// We spend some effort (code!) to handle cases like
2541// [10*i + 5*N*j + 15*M + 6], where i and j are induction variables,
2542// but M and N are just loop-invariant variables.
2543// This should help us handle linearized subscripts;
2544// also makes this test a useful backup to the various SIV tests.
2545//
2546// It occurs to me that the presence of loop-invariant variables
2547// changes the nature of the test from "greatest common divisor"
2548// to "a common divisor".
2549bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst,
2550 FullDependence &Result) const {
2552 return false;
2553 LLVM_DEBUG(dbgs() << "starting gcd\n");
2554 ++GCDapplications;
2555 unsigned BitWidth = SE->getTypeSizeInBits(Src->getType());
2556 APInt RunningGCD = APInt::getZero(BitWidth);
2557
2558 // Examine Src coefficients.
2559 // Compute running GCD and record source constant.
2560 // Because we're looking for the constant at the end of the chain,
2561 // we can't quit the loop just because the GCD == 1.
2562 const SCEV *Coefficients = Src;
2563 while (const SCEVAddRecExpr *AddRec =
2564 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2565 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2566 // If the coefficient is the product of a constant and other stuff,
2567 // we can use the constant in the GCD computation.
2568 std::optional<APInt> ConstCoeff = getConstantPart(Coeff);
2569 if (!ConstCoeff)
2570 return false;
2571 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
2572 Coefficients = AddRec->getStart();
2573 }
2574 const SCEV *SrcConst = Coefficients;
2575
2576 // Examine Dst coefficients.
2577 // Compute running GCD and record destination constant.
2578 // Because we're looking for the constant at the end of the chain,
2579 // we can't quit the loop just because the GCD == 1.
2580 Coefficients = Dst;
2581 while (const SCEVAddRecExpr *AddRec =
2582 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2583 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2584 // If the coefficient is the product of a constant and other stuff,
2585 // we can use the constant in the GCD computation.
2586 std::optional<APInt> ConstCoeff = getConstantPart(Coeff);
2587 if (!ConstCoeff)
2588 return false;
2589 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
2590 Coefficients = AddRec->getStart();
2591 }
2592 const SCEV *DstConst = Coefficients;
2593
2594 APInt ExtraGCD = APInt::getZero(BitWidth);
2595 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
2596 LLVM_DEBUG(dbgs() << " Delta = " << *Delta << "\n");
2597 const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Delta);
2598 if (const SCEVAddExpr *Sum = dyn_cast<SCEVAddExpr>(Delta)) {
2599 // If Delta is a sum of products, we may be able to make further progress.
2600 for (const SCEV *Operand : Sum->operands()) {
2601 if (isa<SCEVConstant>(Operand)) {
2602 assert(!Constant && "Surprised to find multiple constants");
2603 Constant = cast<SCEVConstant>(Operand);
2604 } else if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Operand)) {
2605 // Search for constant operand to participate in GCD;
2606 // If none found; return false.
2607 std::optional<APInt> ConstOp = getConstantPart(Product);
2608 if (!ConstOp)
2609 return false;
2610 ExtraGCD = APIntOps::GreatestCommonDivisor(ExtraGCD, ConstOp->abs());
2611 } else
2612 return false;
2613 }
2614 }
2615 if (!Constant)
2616 return false;
2617 APInt ConstDelta = cast<SCEVConstant>(Constant)->getAPInt();
2618 LLVM_DEBUG(dbgs() << " ConstDelta = " << ConstDelta << "\n");
2619 if (ConstDelta == 0)
2620 return false;
2621 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ExtraGCD);
2622 LLVM_DEBUG(dbgs() << " RunningGCD = " << RunningGCD << "\n");
2623 APInt Remainder = ConstDelta.srem(RunningGCD);
2624 if (Remainder != 0) {
2625 ++GCDindependence;
2626 return true;
2627 }
2628
2629 // Try to disprove equal directions.
2630 // For example, given a subscript pair [3*i + 2*j] and [i' + 2*j' - 1],
2631 // the code above can't disprove the dependence because the GCD = 1.
2632 // So we consider what happen if i = i' and what happens if j = j'.
2633 // If i = i', we can simplify the subscript to [2*i + 2*j] and [2*j' - 1],
2634 // which is infeasible, so we can disallow the = direction for the i level.
2635 // Setting j = j' doesn't help matters, so we end up with a direction vector
2636 // of [<>, *]
2637 //
2638 // Given A[5*i + 10*j*M + 9*M*N] and A[15*i + 20*j*M - 21*N*M + 5],
2639 // we need to remember that the constant part is 5 and the RunningGCD should
2640 // be initialized to ExtraGCD = 30.
2641 LLVM_DEBUG(dbgs() << " ExtraGCD = " << ExtraGCD << '\n');
2642
2643 bool Improved = false;
2644 Coefficients = Src;
2645 while (const SCEVAddRecExpr *AddRec =
2646 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2647 Coefficients = AddRec->getStart();
2648 const Loop *CurLoop = AddRec->getLoop();
2649 RunningGCD = ExtraGCD;
2650 const SCEV *SrcCoeff = AddRec->getStepRecurrence(*SE);
2651 const SCEV *DstCoeff = SE->getMinusSCEV(SrcCoeff, SrcCoeff);
2652
2653 if (!accumulateCoefficientsGCD(Src, CurLoop, SrcCoeff, RunningGCD) ||
2654 !accumulateCoefficientsGCD(Dst, CurLoop, DstCoeff, RunningGCD))
2655 return false;
2656
2657 Delta = SE->getMinusSCEV(SrcCoeff, DstCoeff);
2658 // If the coefficient is the product of a constant and other stuff,
2659 // we can use the constant in the GCD computation.
2660 std::optional<APInt> ConstCoeff = getConstantPart(Delta);
2661 if (!ConstCoeff)
2662 // The difference of the two coefficients might not be a product
2663 // or constant, in which case we give up on this direction.
2664 continue;
2665 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
2666 LLVM_DEBUG(dbgs() << "\tRunningGCD = " << RunningGCD << "\n");
2667 if (RunningGCD != 0) {
2668 Remainder = ConstDelta.srem(RunningGCD);
2669 LLVM_DEBUG(dbgs() << "\tRemainder = " << Remainder << "\n");
2670 if (Remainder != 0) {
2671 unsigned Level = mapSrcLoop(CurLoop);
2672 Result.DV[Level - 1].Direction &= ~Dependence::DVEntry::EQ;
2673 Improved = true;
2674 }
2675 }
2676 }
2677 if (Improved)
2678 ++GCDsuccesses;
2679 LLVM_DEBUG(dbgs() << "all done\n");
2680 return false;
2681}
2682
2683//===----------------------------------------------------------------------===//
2684// banerjeeMIVtest -
2685// Use Banerjee's Inequalities to test an MIV subscript pair.
2686// (Wolfe, in the race-car book, calls this the Extreme Value Test.)
2687// Generally follows the discussion in Section 2.5.2 of
2688//
2689// Optimizing Supercompilers for Supercomputers
2690// Michael Wolfe
2691//
2692// The inequalities given on page 25 are simplified in that loops are
2693// normalized so that the lower bound is always 0 and the stride is always 1.
2694// For example, Wolfe gives
2695//
2696// LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2697//
2698// where A_k is the coefficient of the kth index in the source subscript,
2699// B_k is the coefficient of the kth index in the destination subscript,
2700// U_k is the upper bound of the kth index, L_k is the lower bound of the Kth
2701// index, and N_k is the stride of the kth index. Since all loops are normalized
2702// by the SCEV package, N_k = 1 and L_k = 0, allowing us to simplify the
2703// equation to
2704//
2705// LB^<_k = (A^-_k - B_k)^- (U_k - 0 - 1) + (A_k - B_k)0 - B_k 1
2706// = (A^-_k - B_k)^- (U_k - 1) - B_k
2707//
2708// Similar simplifications are possible for the other equations.
2709//
2710// When we can't determine the number of iterations for a loop,
2711// we use NULL as an indicator for the worst case, infinity.
2712// When computing the upper bound, NULL denotes +inf;
2713// for the lower bound, NULL denotes -inf.
2714//
2715// Return true if dependence disproved.
2716bool DependenceInfo::banerjeeMIVtest(const SCEV *Src, const SCEV *Dst,
2717 const SmallBitVector &Loops,
2718 FullDependence &Result) const {
2720 return false;
2721 LLVM_DEBUG(dbgs() << "starting Banerjee\n");
2722 ++BanerjeeApplications;
2723 LLVM_DEBUG(dbgs() << " Src = " << *Src << '\n');
2724 const SCEV *A0;
2725 CoefficientInfo *A = collectCoeffInfo(Src, true, A0);
2726 LLVM_DEBUG(dbgs() << " Dst = " << *Dst << '\n');
2727 const SCEV *B0;
2728 CoefficientInfo *B = collectCoeffInfo(Dst, false, B0);
2729 BoundInfo *Bound = new BoundInfo[MaxLevels + 1];
2730 const SCEV *Delta = SE->getMinusSCEV(B0, A0);
2731 LLVM_DEBUG(dbgs() << "\tDelta = " << *Delta << '\n');
2732
2733 // Compute bounds for all the * directions.
2734 LLVM_DEBUG(dbgs() << "\tBounds[*]\n");
2735 for (unsigned K = 1; K <= MaxLevels; ++K) {
2736 Bound[K].Iterations = A[K].Iterations ? A[K].Iterations : B[K].Iterations;
2737 Bound[K].Direction = Dependence::DVEntry::ALL;
2738 Bound[K].DirSet = Dependence::DVEntry::NONE;
2739 findBoundsALL(A, B, Bound, K);
2740#ifndef NDEBUG
2741 LLVM_DEBUG(dbgs() << "\t " << K << '\t');
2742 if (Bound[K].Lower[Dependence::DVEntry::ALL])
2743 LLVM_DEBUG(dbgs() << *Bound[K].Lower[Dependence::DVEntry::ALL] << '\t');
2744 else
2745 LLVM_DEBUG(dbgs() << "-inf\t");
2746 if (Bound[K].Upper[Dependence::DVEntry::ALL])
2747 LLVM_DEBUG(dbgs() << *Bound[K].Upper[Dependence::DVEntry::ALL] << '\n');
2748 else
2749 LLVM_DEBUG(dbgs() << "+inf\n");
2750#endif
2751 }
2752
2753 // Test the *, *, *, ... case.
2754 bool Disproved = false;
2755 if (testBounds(Dependence::DVEntry::ALL, 0, Bound, Delta)) {
2756 // Explore the direction vector hierarchy.
2757 unsigned DepthExpanded = 0;
2758 unsigned NewDeps =
2759 exploreDirections(1, A, B, Bound, Loops, DepthExpanded, Delta);
2760 if (NewDeps > 0) {
2761 bool Improved = false;
2762 for (unsigned K = 1; K <= CommonLevels; ++K) {
2763 if (Loops[K]) {
2764 unsigned Old = Result.DV[K - 1].Direction;
2765 Result.DV[K - 1].Direction = Old & Bound[K].DirSet;
2766 Improved |= Old != Result.DV[K - 1].Direction;
2767 if (!Result.DV[K - 1].Direction) {
2768 Improved = false;
2769 Disproved = true;
2770 break;
2771 }
2772 }
2773 }
2774 if (Improved)
2775 ++BanerjeeSuccesses;
2776 } else {
2777 ++BanerjeeIndependence;
2778 Disproved = true;
2779 }
2780 } else {
2781 ++BanerjeeIndependence;
2782 Disproved = true;
2783 }
2784 delete[] Bound;
2785 delete[] A;
2786 delete[] B;
2787 return Disproved;
2788}
2789
2790// Hierarchically expands the direction vector
2791// search space, combining the directions of discovered dependences
2792// in the DirSet field of Bound. Returns the number of distinct
2793// dependences discovered. If the dependence is disproved,
2794// it will return 0.
2795unsigned DependenceInfo::exploreDirections(unsigned Level, CoefficientInfo *A,
2796 CoefficientInfo *B, BoundInfo *Bound,
2797 const SmallBitVector &Loops,
2798 unsigned &DepthExpanded,
2799 const SCEV *Delta) const {
2800 // This algorithm has worst case complexity of O(3^n), where 'n' is the number
2801 // of common loop levels. To avoid excessive compile-time, pessimize all the
2802 // results and immediately return when the number of common levels is beyond
2803 // the given threshold.
2804 if (CommonLevels > MIVMaxLevelThreshold) {
2805 LLVM_DEBUG(dbgs() << "Number of common levels exceeded the threshold. MIV "
2806 "direction exploration is terminated.\n");
2807 for (unsigned K = 1; K <= CommonLevels; ++K)
2808 if (Loops[K])
2809 Bound[K].DirSet = Dependence::DVEntry::ALL;
2810 return 1;
2811 }
2812
2813 if (Level > CommonLevels) {
2814 // record result
2815 LLVM_DEBUG(dbgs() << "\t[");
2816 for (unsigned K = 1; K <= CommonLevels; ++K) {
2817 if (Loops[K]) {
2818 Bound[K].DirSet |= Bound[K].Direction;
2819#ifndef NDEBUG
2820 switch (Bound[K].Direction) {
2822 LLVM_DEBUG(dbgs() << " <");
2823 break;
2825 LLVM_DEBUG(dbgs() << " =");
2826 break;
2828 LLVM_DEBUG(dbgs() << " >");
2829 break;
2831 LLVM_DEBUG(dbgs() << " *");
2832 break;
2833 default:
2834 llvm_unreachable("unexpected Bound[K].Direction");
2835 }
2836#endif
2837 }
2838 }
2839 LLVM_DEBUG(dbgs() << " ]\n");
2840 return 1;
2841 }
2842 if (Loops[Level]) {
2843 if (Level > DepthExpanded) {
2844 DepthExpanded = Level;
2845 // compute bounds for <, =, > at current level
2846 findBoundsLT(A, B, Bound, Level);
2847 findBoundsGT(A, B, Bound, Level);
2848 findBoundsEQ(A, B, Bound, Level);
2849#ifndef NDEBUG
2850 LLVM_DEBUG(dbgs() << "\tBound for level = " << Level << '\n');
2851 LLVM_DEBUG(dbgs() << "\t <\t");
2852 if (Bound[Level].Lower[Dependence::DVEntry::LT])
2853 LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::LT]
2854 << '\t');
2855 else
2856 LLVM_DEBUG(dbgs() << "-inf\t");
2857 if (Bound[Level].Upper[Dependence::DVEntry::LT])
2858 LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::LT]
2859 << '\n');
2860 else
2861 LLVM_DEBUG(dbgs() << "+inf\n");
2862 LLVM_DEBUG(dbgs() << "\t =\t");
2863 if (Bound[Level].Lower[Dependence::DVEntry::EQ])
2864 LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::EQ]
2865 << '\t');
2866 else
2867 LLVM_DEBUG(dbgs() << "-inf\t");
2868 if (Bound[Level].Upper[Dependence::DVEntry::EQ])
2869 LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::EQ]
2870 << '\n');
2871 else
2872 LLVM_DEBUG(dbgs() << "+inf\n");
2873 LLVM_DEBUG(dbgs() << "\t >\t");
2874 if (Bound[Level].Lower[Dependence::DVEntry::GT])
2875 LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::GT]
2876 << '\t');
2877 else
2878 LLVM_DEBUG(dbgs() << "-inf\t");
2879 if (Bound[Level].Upper[Dependence::DVEntry::GT])
2880 LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::GT]
2881 << '\n');
2882 else
2883 LLVM_DEBUG(dbgs() << "+inf\n");
2884#endif
2885 }
2886
2887 unsigned NewDeps = 0;
2888
2889 // test bounds for <, *, *, ...
2890 if (testBounds(Dependence::DVEntry::LT, Level, Bound, Delta))
2891 NewDeps += exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
2892 Delta);
2893
2894 // Test bounds for =, *, *, ...
2895 if (testBounds(Dependence::DVEntry::EQ, Level, Bound, Delta))
2896 NewDeps += exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
2897 Delta);
2898
2899 // test bounds for >, *, *, ...
2900 if (testBounds(Dependence::DVEntry::GT, Level, Bound, Delta))
2901 NewDeps += exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
2902 Delta);
2903
2904 Bound[Level].Direction = Dependence::DVEntry::ALL;
2905 return NewDeps;
2906 } else
2907 return exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
2908 Delta);
2909}
2910
2911// Returns true iff the current bounds are plausible.
2912bool DependenceInfo::testBounds(unsigned char DirKind, unsigned Level,
2913 BoundInfo *Bound, const SCEV *Delta) const {
2914 Bound[Level].Direction = DirKind;
2915 if (const SCEV *LowerBound = getLowerBound(Bound))
2916 if (isKnownPredicate(CmpInst::ICMP_SGT, LowerBound, Delta))
2917 return false;
2918 if (const SCEV *UpperBound = getUpperBound(Bound))
2919 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, UpperBound))
2920 return false;
2921 return true;
2922}
2923
2924// Computes the upper and lower bounds for level K
2925// using the * direction. Records them in Bound.
2926// Wolfe gives the equations
2927//
2928// LB^*_k = (A^-_k - B^+_k)(U_k - L_k) + (A_k - B_k)L_k
2929// UB^*_k = (A^+_k - B^-_k)(U_k - L_k) + (A_k - B_k)L_k
2930//
2931// Since we normalize loops, we can simplify these equations to
2932//
2933// LB^*_k = (A^-_k - B^+_k)U_k
2934// UB^*_k = (A^+_k - B^-_k)U_k
2935//
2936// We must be careful to handle the case where the upper bound is unknown.
2937// Note that the lower bound is always <= 0
2938// and the upper bound is always >= 0.
2939void DependenceInfo::findBoundsALL(CoefficientInfo *A, CoefficientInfo *B,
2940 BoundInfo *Bound, unsigned K) const {
2941 Bound[K].Lower[Dependence::DVEntry::ALL] =
2942 nullptr; // Default value = -infinity.
2943 Bound[K].Upper[Dependence::DVEntry::ALL] =
2944 nullptr; // Default value = +infinity.
2945 if (Bound[K].Iterations) {
2946 Bound[K].Lower[Dependence::DVEntry::ALL] = SE->getMulExpr(
2947 SE->getMinusSCEV(A[K].NegPart, B[K].PosPart), Bound[K].Iterations);
2948 Bound[K].Upper[Dependence::DVEntry::ALL] = SE->getMulExpr(
2949 SE->getMinusSCEV(A[K].PosPart, B[K].NegPart), Bound[K].Iterations);
2950 } else {
2951 // If the difference is 0, we won't need to know the number of iterations.
2952 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].NegPart, B[K].PosPart))
2953 Bound[K].Lower[Dependence::DVEntry::ALL] =
2954 SE->getZero(A[K].Coeff->getType());
2955 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].PosPart, B[K].NegPart))
2956 Bound[K].Upper[Dependence::DVEntry::ALL] =
2957 SE->getZero(A[K].Coeff->getType());
2958 }
2959}
2960
2961// Computes the upper and lower bounds for level K
2962// using the = direction. Records them in Bound.
2963// Wolfe gives the equations
2964//
2965// LB^=_k = (A_k - B_k)^- (U_k - L_k) + (A_k - B_k)L_k
2966// UB^=_k = (A_k - B_k)^+ (U_k - L_k) + (A_k - B_k)L_k
2967//
2968// Since we normalize loops, we can simplify these equations to
2969//
2970// LB^=_k = (A_k - B_k)^- U_k
2971// UB^=_k = (A_k - B_k)^+ U_k
2972//
2973// We must be careful to handle the case where the upper bound is unknown.
2974// Note that the lower bound is always <= 0
2975// and the upper bound is always >= 0.
2976void DependenceInfo::findBoundsEQ(CoefficientInfo *A, CoefficientInfo *B,
2977 BoundInfo *Bound, unsigned K) const {
2978 Bound[K].Lower[Dependence::DVEntry::EQ] =
2979 nullptr; // Default value = -infinity.
2980 Bound[K].Upper[Dependence::DVEntry::EQ] =
2981 nullptr; // Default value = +infinity.
2982 if (Bound[K].Iterations) {
2983 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
2984 const SCEV *NegativePart = getNegativePart(Delta);
2985 Bound[K].Lower[Dependence::DVEntry::EQ] =
2986 SE->getMulExpr(NegativePart, Bound[K].Iterations);
2987 const SCEV *PositivePart = getPositivePart(Delta);
2988 Bound[K].Upper[Dependence::DVEntry::EQ] =
2989 SE->getMulExpr(PositivePart, Bound[K].Iterations);
2990 } else {
2991 // If the positive/negative part of the difference is 0,
2992 // we won't need to know the number of iterations.
2993 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
2994 const SCEV *NegativePart = getNegativePart(Delta);
2995 if (NegativePart->isZero())
2996 Bound[K].Lower[Dependence::DVEntry::EQ] = NegativePart; // Zero
2997 const SCEV *PositivePart = getPositivePart(Delta);
2998 if (PositivePart->isZero())
2999 Bound[K].Upper[Dependence::DVEntry::EQ] = PositivePart; // Zero
3000 }
3001}
3002
3003// Computes the upper and lower bounds for level K
3004// using the < direction. Records them in Bound.
3005// Wolfe gives the equations
3006//
3007// LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
3008// UB^<_k = (A^+_k - B_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
3009//
3010// Since we normalize loops, we can simplify these equations to
3011//
3012// LB^<_k = (A^-_k - B_k)^- (U_k - 1) - B_k
3013// UB^<_k = (A^+_k - B_k)^+ (U_k - 1) - B_k
3014//
3015// We must be careful to handle the case where the upper bound is unknown.
3016void DependenceInfo::findBoundsLT(CoefficientInfo *A, CoefficientInfo *B,
3017 BoundInfo *Bound, unsigned K) const {
3018 Bound[K].Lower[Dependence::DVEntry::LT] =
3019 nullptr; // Default value = -infinity.
3020 Bound[K].Upper[Dependence::DVEntry::LT] =
3021 nullptr; // Default value = +infinity.
3022 if (Bound[K].Iterations) {
3023 const SCEV *Iter_1 = SE->getMinusSCEV(
3024 Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
3025 const SCEV *NegPart =
3026 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
3027 Bound[K].Lower[Dependence::DVEntry::LT] =
3028 SE->getMinusSCEV(SE->getMulExpr(NegPart, Iter_1), B[K].Coeff);
3029 const SCEV *PosPart =
3030 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
3031 Bound[K].Upper[Dependence::DVEntry::LT] =
3032 SE->getMinusSCEV(SE->getMulExpr(PosPart, Iter_1), B[K].Coeff);
3033 } else {
3034 // If the positive/negative part of the difference is 0,
3035 // we won't need to know the number of iterations.
3036 const SCEV *NegPart =
3037 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
3038 if (NegPart->isZero())
3039 Bound[K].Lower[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
3040 const SCEV *PosPart =
3041 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
3042 if (PosPart->isZero())
3043 Bound[K].Upper[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
3044 }
3045}
3046
3047// Computes the upper and lower bounds for level K
3048// using the > direction. Records them in Bound.
3049// Wolfe gives the equations
3050//
3051// LB^>_k = (A_k - B^+_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
3052// UB^>_k = (A_k - B^-_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
3053//
3054// Since we normalize loops, we can simplify these equations to
3055//
3056// LB^>_k = (A_k - B^+_k)^- (U_k - 1) + A_k
3057// UB^>_k = (A_k - B^-_k)^+ (U_k - 1) + A_k
3058//
3059// We must be careful to handle the case where the upper bound is unknown.
3060void DependenceInfo::findBoundsGT(CoefficientInfo *A, CoefficientInfo *B,
3061 BoundInfo *Bound, unsigned K) const {
3062 Bound[K].Lower[Dependence::DVEntry::GT] =
3063 nullptr; // Default value = -infinity.
3064 Bound[K].Upper[Dependence::DVEntry::GT] =
3065 nullptr; // Default value = +infinity.
3066 if (Bound[K].Iterations) {
3067 const SCEV *Iter_1 = SE->getMinusSCEV(
3068 Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
3069 const SCEV *NegPart =
3070 getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
3071 Bound[K].Lower[Dependence::DVEntry::GT] =
3072 SE->getAddExpr(SE->getMulExpr(NegPart, Iter_1), A[K].Coeff);
3073 const SCEV *PosPart =
3074 getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
3075 Bound[K].Upper[Dependence::DVEntry::GT] =
3076 SE->getAddExpr(SE->getMulExpr(PosPart, Iter_1), A[K].Coeff);
3077 } else {
3078 // If the positive/negative part of the difference is 0,
3079 // we won't need to know the number of iterations.
3080 const SCEV *NegPart =
3081 getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
3082 if (NegPart->isZero())
3083 Bound[K].Lower[Dependence::DVEntry::GT] = A[K].Coeff;
3084 const SCEV *PosPart =
3085 getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
3086 if (PosPart->isZero())
3087 Bound[K].Upper[Dependence::DVEntry::GT] = A[K].Coeff;
3088 }
3089}
3090
3091// X^+ = max(X, 0)
3092const SCEV *DependenceInfo::getPositivePart(const SCEV *X) const {
3093 return SE->getSMaxExpr(X, SE->getZero(X->getType()));
3094}
3095
3096// X^- = min(X, 0)
3097const SCEV *DependenceInfo::getNegativePart(const SCEV *X) const {
3098 return SE->getSMinExpr(X, SE->getZero(X->getType()));
3099}
3100
3101// Walks through the subscript,
3102// collecting each coefficient, the associated loop bounds,
3103// and recording its positive and negative parts for later use.
3104DependenceInfo::CoefficientInfo *
3105DependenceInfo::collectCoeffInfo(const SCEV *Subscript, bool SrcFlag,
3106 const SCEV *&Constant) const {
3107 const SCEV *Zero = SE->getZero(Subscript->getType());
3108 CoefficientInfo *CI = new CoefficientInfo[MaxLevels + 1];
3109 for (unsigned K = 1; K <= MaxLevels; ++K) {
3110 CI[K].Coeff = Zero;
3111 CI[K].PosPart = Zero;
3112 CI[K].NegPart = Zero;
3113 CI[K].Iterations = nullptr;
3114 }
3115 while (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Subscript)) {
3116 const Loop *L = AddRec->getLoop();
3117 unsigned K = SrcFlag ? mapSrcLoop(L) : mapDstLoop(L);
3118 CI[K].Coeff = AddRec->getStepRecurrence(*SE);
3119 CI[K].PosPart = getPositivePart(CI[K].Coeff);
3120 CI[K].NegPart = getNegativePart(CI[K].Coeff);
3121 CI[K].Iterations = collectUpperBound(L, Subscript->getType());
3122 Subscript = AddRec->getStart();
3123 }
3124 Constant = Subscript;
3125#ifndef NDEBUG
3126 LLVM_DEBUG(dbgs() << "\tCoefficient Info\n");
3127 for (unsigned K = 1; K <= MaxLevels; ++K) {
3128 LLVM_DEBUG(dbgs() << "\t " << K << "\t" << *CI[K].Coeff);
3129 LLVM_DEBUG(dbgs() << "\tPos Part = ");
3130 LLVM_DEBUG(dbgs() << *CI[K].PosPart);
3131 LLVM_DEBUG(dbgs() << "\tNeg Part = ");
3132 LLVM_DEBUG(dbgs() << *CI[K].NegPart);
3133 LLVM_DEBUG(dbgs() << "\tUpper Bound = ");
3134 if (CI[K].Iterations)
3135 LLVM_DEBUG(dbgs() << *CI[K].Iterations);
3136 else
3137 LLVM_DEBUG(dbgs() << "+inf");
3138 LLVM_DEBUG(dbgs() << '\n');
3139 }
3140 LLVM_DEBUG(dbgs() << "\t Constant = " << *Subscript << '\n');
3141#endif
3142 return CI;
3143}
3144
3145// Looks through all the bounds info and
3146// computes the lower bound given the current direction settings
3147// at each level. If the lower bound for any level is -inf,
3148// the result is -inf.
3149const SCEV *DependenceInfo::getLowerBound(BoundInfo *Bound) const {
3150 const SCEV *Sum = Bound[1].Lower[Bound[1].Direction];
3151 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
3152 if (Bound[K].Lower[Bound[K].Direction])
3153 Sum = SE->getAddExpr(Sum, Bound[K].Lower[Bound[K].Direction]);
3154 else
3155 Sum = nullptr;
3156 }
3157 return Sum;
3158}
3159
3160// Looks through all the bounds info and
3161// computes the upper bound given the current direction settings
3162// at each level. If the upper bound at any level is +inf,
3163// the result is +inf.
3164const SCEV *DependenceInfo::getUpperBound(BoundInfo *Bound) const {
3165 const SCEV *Sum = Bound[1].Upper[Bound[1].Direction];
3166 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
3167 if (Bound[K].Upper[Bound[K].Direction])
3168 Sum = SE->getAddExpr(Sum, Bound[K].Upper[Bound[K].Direction]);
3169 else
3170 Sum = nullptr;
3171 }
3172 return Sum;
3173}
3174
3175//===----------------------------------------------------------------------===//
3176// Constraint manipulation for Delta test.
3177
3178// Given a linear SCEV,
3179// return the coefficient (the step)
3180// corresponding to the specified loop.
3181// If there isn't one, return 0.
3182// For example, given a*i + b*j + c*k, finding the coefficient
3183// corresponding to the j loop would yield b.
3184const SCEV *DependenceInfo::findCoefficient(const SCEV *Expr,
3185 const Loop *TargetLoop) const {
3186 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
3187 if (!AddRec)
3188 return SE->getZero(Expr->getType());
3189 if (AddRec->getLoop() == TargetLoop)
3190 return AddRec->getStepRecurrence(*SE);
3191 return findCoefficient(AddRec->getStart(), TargetLoop);
3192}
3193
3194// Given a linear SCEV,
3195// return the SCEV given by zeroing out the coefficient
3196// corresponding to the specified loop.
3197// For example, given a*i + b*j + c*k, zeroing the coefficient
3198// corresponding to the j loop would yield a*i + c*k.
3199const SCEV *DependenceInfo::zeroCoefficient(const SCEV *Expr,
3200 const Loop *TargetLoop) const {
3201 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
3202 if (!AddRec)
3203 return Expr; // ignore
3204 if (AddRec->getLoop() == TargetLoop)
3205 return AddRec->getStart();
3206 return SE->getAddRecExpr(zeroCoefficient(AddRec->getStart(), TargetLoop),
3207 AddRec->getStepRecurrence(*SE), AddRec->getLoop(),
3208 AddRec->getNoWrapFlags());
3209}
3210
3211// Given a linear SCEV Expr,
3212// return the SCEV given by adding some Value to the
3213// coefficient corresponding to the specified TargetLoop.
3214// For example, given a*i + b*j + c*k, adding 1 to the coefficient
3215// corresponding to the j loop would yield a*i + (b+1)*j + c*k.
3216const SCEV *DependenceInfo::addToCoefficient(const SCEV *Expr,
3217 const Loop *TargetLoop,
3218 const SCEV *Value) const {
3219 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
3220 if (!AddRec) // create a new addRec
3221 return SE->getAddRecExpr(Expr, Value, TargetLoop,
3222 SCEV::FlagAnyWrap); // Worst case, with no info.
3223 if (AddRec->getLoop() == TargetLoop) {
3224 const SCEV *Sum = SE->getAddExpr(AddRec->getStepRecurrence(*SE), Value);
3225 if (Sum->isZero())
3226 return AddRec->getStart();
3227 return SE->getAddRecExpr(AddRec->getStart(), Sum, AddRec->getLoop(),
3228 AddRec->getNoWrapFlags());
3229 }
3230 if (SE->isLoopInvariant(AddRec, TargetLoop))
3231 return SE->getAddRecExpr(AddRec, Value, TargetLoop, SCEV::FlagAnyWrap);
3232 return SE->getAddRecExpr(
3233 addToCoefficient(AddRec->getStart(), TargetLoop, Value),
3234 AddRec->getStepRecurrence(*SE), AddRec->getLoop(),
3235 AddRec->getNoWrapFlags());
3236}
3237
3238// Review the constraints, looking for opportunities
3239// to simplify a subscript pair (Src and Dst).
3240// Return true if some simplification occurs.
3241// If the simplification isn't exact (that is, if it is conservative
3242// in terms of dependence), set consistent to false.
3243// Corresponds to Figure 5 from the paper
3244//
3245// Practical Dependence Testing
3246// Goff, Kennedy, Tseng
3247// PLDI 1991
3248bool DependenceInfo::propagate(const SCEV *&Src, const SCEV *&Dst,
3249 SmallBitVector &Loops,
3250 SmallVectorImpl<Constraint> &Constraints,
3251 bool &Consistent) {
3252 bool Result = false;
3253 for (unsigned LI : Loops.set_bits()) {
3254 LLVM_DEBUG(dbgs() << "\t Constraint[" << LI << "] is");
3255 LLVM_DEBUG(Constraints[LI].dump(dbgs()));
3256 if (Constraints[LI].isDistance())
3257 Result |= propagateDistance(Src, Dst, Constraints[LI], Consistent);
3258 else if (Constraints[LI].isLine())
3259 Result |= propagateLine(Src, Dst, Constraints[LI], Consistent);
3260 else if (Constraints[LI].isPoint())
3261 Result |= propagatePoint(Src, Dst, Constraints[LI]);
3262 }
3263 return Result;
3264}
3265
3266// Attempt to propagate a distance
3267// constraint into a subscript pair (Src and Dst).
3268// Return true if some simplification occurs.
3269// If the simplification isn't exact (that is, if it is conservative
3270// in terms of dependence), set consistent to false.
3271bool DependenceInfo::propagateDistance(const SCEV *&Src, const SCEV *&Dst,
3272 Constraint &CurConstraint,
3273 bool &Consistent) {
3274 const Loop *CurSrcLoop = CurConstraint.getAssociatedSrcLoop();
3275 const Loop *CurDstLoop = CurConstraint.getAssociatedDstLoop();
3276 LLVM_DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n");
3277 const SCEV *A_K = findCoefficient(Src, CurSrcLoop);
3278 if (A_K->isZero())
3279 return false;
3280 const SCEV *DA_K = SE->getMulExpr(A_K, CurConstraint.getD());
3281 Src = SE->getMinusSCEV(Src, DA_K);
3282 Src = zeroCoefficient(Src, CurSrcLoop);
3283 LLVM_DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n");
3284 LLVM_DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n");
3285 Dst = addToCoefficient(Dst, CurDstLoop, SE->getNegativeSCEV(A_K));
3286 LLVM_DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n");
3287 if (!findCoefficient(Dst, CurDstLoop)->isZero())
3288 Consistent = false;
3289 return true;
3290}
3291
3292// Attempt to propagate a line
3293// constraint into a subscript pair (Src and Dst).
3294// Return true if some simplification occurs.
3295// If the simplification isn't exact (that is, if it is conservative
3296// in terms of dependence), set consistent to false.
3297bool DependenceInfo::propagateLine(const SCEV *&Src, const SCEV *&Dst,
3298 Constraint &CurConstraint,
3299 bool &Consistent) {
3300 const Loop *CurSrcLoop = CurConstraint.getAssociatedSrcLoop();
3301 const Loop *CurDstLoop = CurConstraint.getAssociatedDstLoop();
3302 const SCEV *A = CurConstraint.getA();
3303 const SCEV *B = CurConstraint.getB();
3304 const SCEV *C = CurConstraint.getC();
3305 LLVM_DEBUG(dbgs() << "\t\tA = " << *A << ", B = " << *B << ", C = " << *C
3306 << "\n");
3307 LLVM_DEBUG(dbgs() << "\t\tSrc = " << *Src << "\n");
3308 LLVM_DEBUG(dbgs() << "\t\tDst = " << *Dst << "\n");
3309 if (A->isZero()) {
3310 const SCEVConstant *Bconst = dyn_cast<SCEVConstant>(B);
3311 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3312 if (!Bconst || !Cconst)
3313 return false;
3314 APInt Beta = Bconst->getAPInt();
3315 APInt Charlie = Cconst->getAPInt();
3316 APInt CdivB = Charlie.sdiv(Beta);
3317 assert(Charlie.srem(Beta) == 0 && "C should be evenly divisible by B");
3318 const SCEV *AP_K = findCoefficient(Dst, CurDstLoop);
3319 Src = SE->getMinusSCEV(Src, SE->getMulExpr(AP_K, SE->getConstant(CdivB)));
3320 Dst = zeroCoefficient(Dst, CurDstLoop);
3321 if (!findCoefficient(Src, CurSrcLoop)->isZero())
3322 Consistent = false;
3323 } else if (B->isZero()) {
3324 const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A);
3325 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3326 if (!Aconst || !Cconst)
3327 return false;
3328 APInt Alpha = Aconst->getAPInt();
3329 APInt Charlie = Cconst->getAPInt();
3330 APInt CdivA = Charlie.sdiv(Alpha);
3331 assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A");
3332 const SCEV *A_K = findCoefficient(Src, CurSrcLoop);
3333 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA)));
3334 Src = zeroCoefficient(Src, CurSrcLoop);
3335 if (!findCoefficient(Dst, CurDstLoop)->isZero())
3336 Consistent = false;
3337 } else if (isKnownPredicate(CmpInst::ICMP_EQ, A, B)) {
3338 const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A);
3339 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
3340 if (!Aconst || !Cconst)
3341 return false;
3342 APInt Alpha = Aconst->getAPInt();
3343 APInt Charlie = Cconst->getAPInt();
3344 APInt CdivA = Charlie.sdiv(Alpha);
3345 assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A");
3346 const SCEV *A_K = findCoefficient(Src, CurSrcLoop);
3347 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA)));
3348 Src = zeroCoefficient(Src, CurSrcLoop);
3349 Dst = addToCoefficient(Dst, CurDstLoop, A_K);
3350 if (!findCoefficient(Dst, CurDstLoop)->isZero())
3351 Consistent = false;
3352 } else {
3353 // paper is incorrect here, or perhaps just misleading
3354 const SCEV *A_K = findCoefficient(Src, CurSrcLoop);
3355 Src = SE->getMulExpr(Src, A);
3356 Dst = SE->getMulExpr(Dst, A);
3357 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, C));
3358 Src = zeroCoefficient(Src, CurSrcLoop);
3359 Dst = addToCoefficient(Dst, CurDstLoop, SE->getMulExpr(A_K, B));
3360 if (!findCoefficient(Dst, CurDstLoop)->isZero())
3361 Consistent = false;
3362 }
3363 LLVM_DEBUG(dbgs() << "\t\tnew Src = " << *Src << "\n");
3364 LLVM_DEBUG(dbgs() << "\t\tnew Dst = " << *Dst << "\n");
3365 return true;
3366}
3367
3368// Attempt to propagate a point
3369// constraint into a subscript pair (Src and Dst).
3370// Return true if some simplification occurs.
3371bool DependenceInfo::propagatePoint(const SCEV *&Src, const SCEV *&Dst,
3372 Constraint &CurConstraint) {
3373 const Loop *CurSrcLoop = CurConstraint.getAssociatedSrcLoop();
3374 const Loop *CurDstLoop = CurConstraint.getAssociatedDstLoop();
3375 const SCEV *A_K = findCoefficient(Src, CurSrcLoop);
3376 const SCEV *AP_K = findCoefficient(Dst, CurDstLoop);
3377 const SCEV *XA_K = SE->getMulExpr(A_K, CurConstraint.getX());
3378 const SCEV *YAP_K = SE->getMulExpr(AP_K, CurConstraint.getY());
3379 LLVM_DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n");
3380 Src = SE->getAddExpr(Src, SE->getMinusSCEV(XA_K, YAP_K));
3381 Src = zeroCoefficient(Src, CurSrcLoop);
3382 LLVM_DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n");
3383 LLVM_DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n");
3384 Dst = zeroCoefficient(Dst, CurDstLoop);
3385 LLVM_DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n");
3386 return true;
3387}
3388
3389// Update direction vector entry based on the current constraint.
3390void DependenceInfo::updateDirection(Dependence::DVEntry &Level,
3391 const Constraint &CurConstraint) const {
3392 LLVM_DEBUG(dbgs() << "\tUpdate direction, constraint =");
3393 LLVM_DEBUG(CurConstraint.dump(dbgs()));
3394 if (CurConstraint.isAny())
3395 ; // use defaults
3396 else if (CurConstraint.isDistance()) {
3397 // this one is consistent, the others aren't
3398 Level.Scalar = false;
3399 Level.Distance = CurConstraint.getD();
3400 unsigned NewDirection = Dependence::DVEntry::NONE;
3401 if (!SE->isKnownNonZero(Level.Distance)) // if may be zero
3402 NewDirection = Dependence::DVEntry::EQ;
3403 if (!SE->isKnownNonPositive(Level.Distance)) // if may be positive
3404 NewDirection |= Dependence::DVEntry::LT;
3405 if (!SE->isKnownNonNegative(Level.Distance)) // if may be negative
3406 NewDirection |= Dependence::DVEntry::GT;
3407 Level.Direction &= NewDirection;
3408 } else if (CurConstraint.isLine()) {
3409 Level.Scalar = false;
3410 Level.Distance = nullptr;
3411 // direction should be accurate
3412 } else if (CurConstraint.isPoint()) {
3413 Level.Scalar = false;
3414 Level.Distance = nullptr;
3415 unsigned NewDirection = Dependence::DVEntry::NONE;
3416 if (!isKnownPredicate(CmpInst::ICMP_NE, CurConstraint.getY(),
3417 CurConstraint.getX()))
3418 // if X may be = Y
3419 NewDirection |= Dependence::DVEntry::EQ;
3420 if (!isKnownPredicate(CmpInst::ICMP_SLE, CurConstraint.getY(),
3421 CurConstraint.getX()))
3422 // if Y may be > X
3423 NewDirection |= Dependence::DVEntry::LT;
3424 if (!isKnownPredicate(CmpInst::ICMP_SGE, CurConstraint.getY(),
3425 CurConstraint.getX()))
3426 // if Y may be < X
3427 NewDirection |= Dependence::DVEntry::GT;
3428 Level.Direction &= NewDirection;
3429 } else
3430 llvm_unreachable("constraint has unexpected kind");
3431}
3432
3433/// Check if we can delinearize the subscripts. If the SCEVs representing the
3434/// source and destination array references are recurrences on a nested loop,
3435/// this function flattens the nested recurrences into separate recurrences
3436/// for each loop level.
3437bool DependenceInfo::tryDelinearize(Instruction *Src, Instruction *Dst,
3438 SmallVectorImpl<Subscript> &Pair) {
3439 assert(isLoadOrStore(Src) && "instruction is not load or store");
3440 assert(isLoadOrStore(Dst) && "instruction is not load or store");
3441 Value *SrcPtr = getLoadStorePointerOperand(Src);
3442 Value *DstPtr = getLoadStorePointerOperand(Dst);
3443 Loop *SrcLoop = LI->getLoopFor(Src->getParent());
3444 Loop *DstLoop = LI->getLoopFor(Dst->getParent());
3445 const SCEV *SrcAccessFn = SE->getSCEVAtScope(SrcPtr, SrcLoop);
3446 const SCEV *DstAccessFn = SE->getSCEVAtScope(DstPtr, DstLoop);
3447 const SCEVUnknown *SrcBase =
3448 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
3449 const SCEVUnknown *DstBase =
3450 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
3451
3452 if (!SrcBase || !DstBase || SrcBase != DstBase)
3453 return false;
3454
3455 SmallVector<const SCEV *, 4> SrcSubscripts, DstSubscripts;
3456
3457 if (!tryDelinearizeFixedSize(Src, Dst, SrcAccessFn, DstAccessFn,
3458 SrcSubscripts, DstSubscripts) &&
3459 !tryDelinearizeParametricSize(Src, Dst, SrcAccessFn, DstAccessFn,
3460 SrcSubscripts, DstSubscripts))
3461 return false;
3462
3463 assert(isLoopInvariant(SrcBase, SrcLoop) &&
3464 isLoopInvariant(DstBase, DstLoop) &&
3465 "Expected SrcBase and DstBase to be loop invariant");
3466
3467 int Size = SrcSubscripts.size();
3468 LLVM_DEBUG({
3469 dbgs() << "\nSrcSubscripts: ";
3470 for (int I = 0; I < Size; I++)
3471 dbgs() << *SrcSubscripts[I];
3472 dbgs() << "\nDstSubscripts: ";
3473 for (int I = 0; I < Size; I++)
3474 dbgs() << *DstSubscripts[I];
3475 });
3476
3477 // The delinearization transforms a single-subscript MIV dependence test into
3478 // a multi-subscript SIV dependence test that is easier to compute. So we
3479 // resize Pair to contain as many pairs of subscripts as the delinearization
3480 // has found, and then initialize the pairs following the delinearization.
3481 Pair.resize(Size);
3482 for (int I = 0; I < Size; ++I) {
3483 Pair[I].Src = SrcSubscripts[I];
3484 Pair[I].Dst = DstSubscripts[I];
3485 unifySubscriptType(&Pair[I]);
3486 }
3487
3488 return true;
3489}
3490
3491/// Try to delinearize \p SrcAccessFn and \p DstAccessFn if the underlying
3492/// arrays accessed are fixed-size arrays. Return true if delinearization was
3493/// successful.
3494bool DependenceInfo::tryDelinearizeFixedSize(
3495 Instruction *Src, Instruction *Dst, const SCEV *SrcAccessFn,
3496 const SCEV *DstAccessFn, SmallVectorImpl<const SCEV *> &SrcSubscripts,
3497 SmallVectorImpl<const SCEV *> &DstSubscripts) {
3498 LLVM_DEBUG({
3499 const SCEVUnknown *SrcBase =
3500 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
3501 const SCEVUnknown *DstBase =
3502 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
3503 assert(SrcBase && DstBase && SrcBase == DstBase &&
3504 "expected src and dst scev unknowns to be equal");
3505 });
3506
3507 SmallVector<int, 4> SrcSizes;
3508 SmallVector<int, 4> DstSizes;
3509 if (!tryDelinearizeFixedSizeImpl(SE, Src, SrcAccessFn, SrcSubscripts,
3510 SrcSizes) ||
3511 !tryDelinearizeFixedSizeImpl(SE, Dst, DstAccessFn, DstSubscripts,
3512 DstSizes))
3513 return false;
3514
3515 // Check that the two size arrays are non-empty and equal in length and
3516 // value.
3517 if (SrcSizes.size() != DstSizes.size() ||
3518 !std::equal(SrcSizes.begin(), SrcSizes.end(), DstSizes.begin())) {
3519 SrcSubscripts.clear();
3520 DstSubscripts.clear();
3521 return false;
3522 }
3523
3524 assert(SrcSubscripts.size() == DstSubscripts.size() &&
3525 "Expected equal number of entries in the list of SrcSubscripts and "
3526 "DstSubscripts.");
3527
3528 Value *SrcPtr = getLoadStorePointerOperand(Src);
3529 Value *DstPtr = getLoadStorePointerOperand(Dst);
3530
3531 // In general we cannot safely assume that the subscripts recovered from GEPs
3532 // are in the range of values defined for their corresponding array
3533 // dimensions. For example some C language usage/interpretation make it
3534 // impossible to verify this at compile-time. As such we can only delinearize
3535 // iff the subscripts are positive and are less than the range of the
3536 // dimension.
3538 auto AllIndicesInRange = [&](SmallVector<int, 4> &DimensionSizes,
3539 SmallVectorImpl<const SCEV *> &Subscripts,
3540 Value *Ptr) {
3541 size_t SSize = Subscripts.size();
3542 for (size_t I = 1; I < SSize; ++I) {
3543 const SCEV *S = Subscripts[I];
3544 if (!isKnownNonNegative(S, Ptr)) {
3545 LLVM_DEBUG({
3546 dbgs() << "Check failed: !isKnownNonNegative(S, Ptr)\n";
3547 dbgs() << " S: " << *S << "\n" << " Ptr: " << *Ptr << "\n";
3548 });
3549 return false;
3550 }
3551 if (auto *SType = dyn_cast<IntegerType>(S->getType())) {
3552 const SCEV *Range = SE->getConstant(
3553 ConstantInt::get(SType, DimensionSizes[I - 1], false));
3554 if (!isKnownLessThan(S, Range)) {
3555 LLVM_DEBUG({
3556 dbgs() << "Check failed: !isKnownLessThan(S, Range)\n";
3557 dbgs() << " S: " << *S << "\n"
3558 << " Range: " << *Range << "\n";
3559 });
3560 return false;
3561 }
3562 }
3563 }
3564 return true;
3565 };
3566
3567 if (!AllIndicesInRange(SrcSizes, SrcSubscripts, SrcPtr) ||
3568 !AllIndicesInRange(DstSizes, DstSubscripts, DstPtr)) {
3569 LLVM_DEBUG(dbgs() << "Check failed: AllIndicesInRange.\n");
3570 SrcSubscripts.clear();
3571 DstSubscripts.clear();
3572 return false;
3573 }
3574 }
3575 LLVM_DEBUG({
3576 dbgs() << "Delinearized subscripts of fixed-size array\n"
3577 << "SrcGEP:" << *SrcPtr << "\n"
3578 << "DstGEP:" << *DstPtr << "\n";
3579 });
3580 return true;
3581}
3582
3583bool DependenceInfo::tryDelinearizeParametricSize(
3584 Instruction *Src, Instruction *Dst, const SCEV *SrcAccessFn,
3585 const SCEV *DstAccessFn, SmallVectorImpl<const SCEV *> &SrcSubscripts,
3586 SmallVectorImpl<const SCEV *> &DstSubscripts) {
3587
3588 Value *SrcPtr = getLoadStorePointerOperand(Src);
3589 Value *DstPtr = getLoadStorePointerOperand(Dst);
3590 const SCEVUnknown *SrcBase =
3591 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
3592 const SCEVUnknown *DstBase =
3593 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
3594 assert(SrcBase && DstBase && SrcBase == DstBase &&
3595 "expected src and dst scev unknowns to be equal");
3596
3597 const SCEV *ElementSize = SE->getElementSize(Src);
3598 if (ElementSize != SE->getElementSize(Dst))
3599 return false;
3600
3601 const SCEV *SrcSCEV = SE->getMinusSCEV(SrcAccessFn, SrcBase);
3602 const SCEV *DstSCEV = SE->getMinusSCEV(DstAccessFn, DstBase);
3603
3604 const SCEVAddRecExpr *SrcAR = dyn_cast<SCEVAddRecExpr>(SrcSCEV);
3605 const SCEVAddRecExpr *DstAR = dyn_cast<SCEVAddRecExpr>(DstSCEV);
3606 if (!SrcAR || !DstAR || !SrcAR->isAffine() || !DstAR->isAffine())
3607 return false;
3608
3609 // First step: collect parametric terms in both array references.
3611 collectParametricTerms(*SE, SrcAR, Terms);
3612 collectParametricTerms(*SE, DstAR, Terms);
3613
3614 // Second step: find subscript sizes.
3616 findArrayDimensions(*SE, Terms, Sizes, ElementSize);
3617
3618 // Third step: compute the access functions for each subscript.
3619 computeAccessFunctions(*SE, SrcAR, SrcSubscripts, Sizes);
3620 computeAccessFunctions(*SE, DstAR, DstSubscripts, Sizes);
3621
3622 // Fail when there is only a subscript: that's a linearized access function.
3623 if (SrcSubscripts.size() < 2 || DstSubscripts.size() < 2 ||
3624 SrcSubscripts.size() != DstSubscripts.size())
3625 return false;
3626
3627 size_t Size = SrcSubscripts.size();
3628
3629 // Statically check that the array bounds are in-range. The first subscript we
3630 // don't have a size for and it cannot overflow into another subscript, so is
3631 // always safe. The others need to be 0 <= subscript[i] < bound, for both src
3632 // and dst.
3633 // FIXME: It may be better to record these sizes and add them as constraints
3634 // to the dependency checks.
3636 for (size_t I = 1; I < Size; ++I) {
3637 bool SNN = isKnownNonNegative(SrcSubscripts[I], SrcPtr);
3638 bool DNN = isKnownNonNegative(DstSubscripts[I], DstPtr);
3639 bool SLT = isKnownLessThan(SrcSubscripts[I], Sizes[I - 1]);
3640 bool DLT = isKnownLessThan(DstSubscripts[I], Sizes[I - 1]);
3641 if (SNN && DNN && SLT && DLT)
3642 continue;
3643
3644 LLVM_DEBUG({
3645 dbgs() << "Delinearization checks failed: can't prove the following\n";
3646 if (!SNN)
3647 dbgs() << " isKnownNonNegative(" << *SrcSubscripts[I] << ")\n";
3648 if (!DNN)
3649 dbgs() << " isKnownNonNegative(" << *DstSubscripts[I] << ")\n";
3650 if (!SLT)
3651 dbgs() << " isKnownLessThan(" << *SrcSubscripts[I] << ", "
3652 << *Sizes[I - 1] << ")\n";
3653 if (!DLT)
3654 dbgs() << " isKnownLessThan(" << *DstSubscripts[I] << ", "
3655 << *Sizes[I - 1] << ")\n";
3656 });
3657 return false;
3658 }
3659
3660 return true;
3661}
3662
3663//===----------------------------------------------------------------------===//
3664
3665#ifndef NDEBUG
3666// For debugging purposes, dump a small bit vector to dbgs().
3668 dbgs() << "{";
3669 for (unsigned VI : BV.set_bits()) {
3670 dbgs() << VI;
3671 if (BV.find_next(VI) >= 0)
3672 dbgs() << ' ';
3673 }
3674 dbgs() << "}\n";
3675}
3676#endif
3677
3679 FunctionAnalysisManager::Invalidator &Inv) {
3680 // Check if the analysis itself has been invalidated.
3681 auto PAC = PA.getChecker<DependenceAnalysis>();
3682 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>())
3683 return true;
3684
3685 // Check transitive dependencies.
3686 return Inv.invalidate<AAManager>(F, PA) ||
3687 Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) ||
3688 Inv.invalidate<LoopAnalysis>(F, PA);
3689}
3690
3694
3695// depends -
3696// Returns NULL if there is no dependence.
3697// Otherwise, return a Dependence with as many details as possible.
3698// Corresponds to Section 3.1 in the paper
3699//
3700// Practical Dependence Testing
3701// Goff, Kennedy, Tseng
3702// PLDI 1991
3703//
3704// Care is required to keep the routine below, getSplitIteration(),
3705// up to date with respect to this routine.
3706std::unique_ptr<Dependence>
3708 bool UnderRuntimeAssumptions) {
3710 bool PossiblyLoopIndependent = true;
3711 if (Src == Dst)
3712 PossiblyLoopIndependent = false;
3713
3714 if (!(Src->mayReadOrWriteMemory() && Dst->mayReadOrWriteMemory()))
3715 // if both instructions don't reference memory, there's no dependence
3716 return nullptr;
3717
3718 if (!isLoadOrStore(Src) || !isLoadOrStore(Dst)) {
3719 // can only analyze simple loads and stores, i.e., no calls, invokes, etc.
3720 LLVM_DEBUG(dbgs() << "can only handle simple loads and stores\n");
3721 return std::make_unique<Dependence>(Src, Dst,
3722 SCEVUnionPredicate(Assume, *SE));
3723 }
3724
3725 const MemoryLocation &DstLoc = MemoryLocation::get(Dst);
3726 const MemoryLocation &SrcLoc = MemoryLocation::get(Src);
3727
3728 switch (underlyingObjectsAlias(AA, F->getDataLayout(), DstLoc, SrcLoc)) {
3731 // cannot analyse objects if we don't understand their aliasing.
3732 LLVM_DEBUG(dbgs() << "can't analyze may or partial alias\n");
3733 return std::make_unique<Dependence>(Src, Dst,
3734 SCEVUnionPredicate(Assume, *SE));
3736 // If the objects noalias, they are distinct, accesses are independent.
3737 LLVM_DEBUG(dbgs() << "no alias\n");
3738 return nullptr;
3740 break; // The underlying objects alias; test accesses for dependence.
3741 }
3742
3743 if (DstLoc.Size != SrcLoc.Size || !DstLoc.Size.isPrecise() ||
3744 !SrcLoc.Size.isPrecise()) {
3745 // The dependence test gets confused if the size of the memory accesses
3746 // differ.
3747 LLVM_DEBUG(dbgs() << "can't analyze must alias with different sizes\n");
3748 return std::make_unique<Dependence>(Src, Dst,
3749 SCEVUnionPredicate(Assume, *SE));
3750 }
3751
3752 Value *SrcPtr = getLoadStorePointerOperand(Src);
3753 Value *DstPtr = getLoadStorePointerOperand(Dst);
3754 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
3755 const SCEV *DstSCEV = SE->getSCEV(DstPtr);
3756 LLVM_DEBUG(dbgs() << " SrcSCEV = " << *SrcSCEV << "\n");
3757 LLVM_DEBUG(dbgs() << " DstSCEV = " << *DstSCEV << "\n");
3758 const SCEV *SrcBase = SE->getPointerBase(SrcSCEV);
3759 const SCEV *DstBase = SE->getPointerBase(DstSCEV);
3760 if (SrcBase != DstBase) {
3761 // If two pointers have different bases, trying to analyze indexes won't
3762 // work; we can't compare them to each other. This can happen, for example,
3763 // if one is produced by an LCSSA PHI node.
3764 //
3765 // We check this upfront so we don't crash in cases where getMinusSCEV()
3766 // returns a SCEVCouldNotCompute.
3767 LLVM_DEBUG(dbgs() << "can't analyze SCEV with different pointer base\n");
3768 return std::make_unique<Dependence>(Src, Dst,
3769 SCEVUnionPredicate(Assume, *SE));
3770 }
3771
3772 // Even if the base pointers are the same, they may not be loop-invariant. It
3773 // could lead to incorrect results, as we're analyzing loop-carried
3774 // dependencies. Src and Dst can be in different loops, so we need to check
3775 // the base pointer is invariant in both loops.
3776 Loop *SrcLoop = LI->getLoopFor(Src->getParent());
3777 Loop *DstLoop = LI->getLoopFor(Dst->getParent());
3778 if (!isLoopInvariant(SrcBase, SrcLoop) ||
3779 !isLoopInvariant(DstBase, DstLoop)) {
3780 LLVM_DEBUG(dbgs() << "The base pointer is not loop invariant.\n");
3781 return std::make_unique<Dependence>(Src, Dst,
3782 SCEVUnionPredicate(Assume, *SE));
3783 }
3784
3785 uint64_t EltSize = SrcLoc.Size.toRaw();
3786 const SCEV *SrcEv = SE->getMinusSCEV(SrcSCEV, SrcBase);
3787 const SCEV *DstEv = SE->getMinusSCEV(DstSCEV, DstBase);
3788
3789 // Check that memory access offsets are multiples of element sizes.
3790 if (!SE->isKnownMultipleOf(SrcEv, EltSize, Assume) ||
3791 !SE->isKnownMultipleOf(DstEv, EltSize, Assume)) {
3792 LLVM_DEBUG(dbgs() << "can't analyze SCEV with different offsets\n");
3793 return std::make_unique<Dependence>(Src, Dst,
3794 SCEVUnionPredicate(Assume, *SE));
3795 }
3796
3797 if (!Assume.empty()) {
3798 if (!UnderRuntimeAssumptions)
3799 return std::make_unique<Dependence>(Src, Dst,
3800 SCEVUnionPredicate(Assume, *SE));
3801 // Add non-redundant assumptions.
3802 unsigned N = Assumptions.size();
3803 for (const SCEVPredicate *P : Assume) {
3804 bool Implied = false;
3805 for (unsigned I = 0; I != N && !Implied; I++)
3806 if (Assumptions[I]->implies(P, *SE))
3807 Implied = true;
3808 if (!Implied)
3809 Assumptions.push_back(P);
3810 }
3811 }
3812
3813 unsigned Pairs = 1;
3814 SmallVector<Subscript, 2> Pair(Pairs);
3815 Pair[0].Src = SrcEv;
3816 Pair[0].Dst = DstEv;
3817
3818 if (Delinearize) {
3819 if (tryDelinearize(Src, Dst, Pair)) {
3820 LLVM_DEBUG(dbgs() << " delinearized\n");
3821 Pairs = Pair.size();
3822 }
3823 }
3824
3825 // Establish loop nesting levels considering SameSD loops as common
3826 establishNestingLevels(Src, Dst);
3827
3828 LLVM_DEBUG(dbgs() << " common nesting levels = " << CommonLevels << "\n");
3829 LLVM_DEBUG(dbgs() << " maximum nesting levels = " << MaxLevels << "\n");
3830 LLVM_DEBUG(dbgs() << " SameSD nesting levels = " << SameSDLevels << "\n");
3831
3832 // Modify common levels to consider the SameSD levels in the tests
3833 CommonLevels += SameSDLevels;
3834 MaxLevels -= SameSDLevels;
3835 if (SameSDLevels > 0) {
3836 // Not all tests are handled yet over SameSD loops
3837 // Revoke if there are any tests other than ZIV, SIV or RDIV
3838 for (unsigned P = 0; P < Pairs; ++P) {
3840 Subscript::ClassificationKind TestClass =
3841 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()),
3842 Pair[P].Dst, LI->getLoopFor(Dst->getParent()), Loops);
3843
3844 if (TestClass != Subscript::ZIV && TestClass != Subscript::SIV &&
3845 TestClass != Subscript::RDIV) {
3846 // Revert the levels to not consider the SameSD levels
3847 CommonLevels -= SameSDLevels;
3848 MaxLevels += SameSDLevels;
3849 SameSDLevels = 0;
3850 break;
3851 }
3852 }
3853 }
3854
3855 if (SameSDLevels > 0)
3856 SameSDLoopsCount++;
3857
3858 FullDependence Result(Src, Dst, SCEVUnionPredicate(Assume, *SE),
3859 PossiblyLoopIndependent, CommonLevels);
3860 ++TotalArrayPairs;
3861
3862 for (unsigned P = 0; P < Pairs; ++P) {
3863 assert(Pair[P].Src->getType()->isIntegerTy() && "Src must be an integer");
3864 assert(Pair[P].Dst->getType()->isIntegerTy() && "Dst must be an integer");
3865 Pair[P].Loops.resize(MaxLevels + 1);
3866 Pair[P].GroupLoops.resize(MaxLevels + 1);
3867 Pair[P].Group.resize(Pairs);
3868 removeMatchingExtensions(&Pair[P]);
3869 Pair[P].Classification =
3870 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()), Pair[P].Dst,
3871 LI->getLoopFor(Dst->getParent()), Pair[P].Loops);
3872 Pair[P].GroupLoops = Pair[P].Loops;
3873 Pair[P].Group.set(P);
3874 LLVM_DEBUG(dbgs() << " subscript " << P << "\n");
3875 LLVM_DEBUG(dbgs() << "\tsrc = " << *Pair[P].Src << "\n");
3876 LLVM_DEBUG(dbgs() << "\tdst = " << *Pair[P].Dst << "\n");
3877 LLVM_DEBUG(dbgs() << "\tclass = " << Pair[P].Classification << "\n");
3878 LLVM_DEBUG(dbgs() << "\tloops = ");
3880 }
3881
3882 SmallBitVector Separable(Pairs);
3883 SmallBitVector Coupled(Pairs);
3884
3885 // Partition subscripts into separable and minimally-coupled groups
3886 // Algorithm in paper is algorithmically better;
3887 // this may be faster in practice. Check someday.
3888 //
3889 // Here's an example of how it works. Consider this code:
3890 //
3891 // for (i = ...) {
3892 // for (j = ...) {
3893 // for (k = ...) {
3894 // for (l = ...) {
3895 // for (m = ...) {
3896 // A[i][j][k][m] = ...;
3897 // ... = A[0][j][l][i + j];
3898 // }
3899 // }
3900 // }
3901 // }
3902 // }
3903 //
3904 // There are 4 subscripts here:
3905 // 0 [i] and [0]
3906 // 1 [j] and [j]
3907 // 2 [k] and [l]
3908 // 3 [m] and [i + j]
3909 //
3910 // We've already classified each subscript pair as ZIV, SIV, etc.,
3911 // and collected all the loops mentioned by pair P in Pair[P].Loops.
3912 // In addition, we've initialized Pair[P].GroupLoops to Pair[P].Loops
3913 // and set Pair[P].Group = {P}.
3914 //
3915 // Src Dst Classification Loops GroupLoops Group
3916 // 0 [i] [0] SIV {1} {1} {0}
3917 // 1 [j] [j] SIV {2} {2} {1}
3918 // 2 [k] [l] RDIV {3,4} {3,4} {2}
3919 // 3 [m] [i + j] MIV {1,2,5} {1,2,5} {3}
3920 //
3921 // For each subscript SI 0 .. 3, we consider each remaining subscript, SJ.
3922 // So, 0 is compared against 1, 2, and 3; 1 is compared against 2 and 3, etc.
3923 //
3924 // We begin by comparing 0 and 1. The intersection of the GroupLoops is empty.
3925 // Next, 0 and 2. Again, the intersection of their GroupLoops is empty.
3926 // Next 0 and 3. The intersection of their GroupLoop = {1}, not empty,
3927 // so Pair[3].Group = {0,3} and Done = false (that is, 0 will not be added
3928 // to either Separable or Coupled).
3929 //
3930 // Next, we consider 1 and 2. The intersection of the GroupLoops is empty.
3931 // Next, 1 and 3. The intersection of their GroupLoops = {2}, not empty,
3932 // so Pair[3].Group = {0, 1, 3} and Done = false.
3933 //
3934 // Next, we compare 2 against 3. The intersection of the GroupLoops is empty.
3935 // Since Done remains true, we add 2 to the set of Separable pairs.
3936 //
3937 // Finally, we consider 3. There's nothing to compare it with,
3938 // so Done remains true and we add it to the Coupled set.
3939 // Pair[3].Group = {0, 1, 3} and GroupLoops = {1, 2, 5}.
3940 //
3941 // In the end, we've got 1 separable subscript and 1 coupled group.
3942 for (unsigned SI = 0; SI < Pairs; ++SI) {
3943 if (Pair[SI].Classification == Subscript::NonLinear) {
3944 // ignore these, but collect loops for later
3945 ++NonlinearSubscriptPairs;
3946 collectCommonLoops(Pair[SI].Src, LI->getLoopFor(Src->getParent()),
3947 Pair[SI].Loops);
3948 collectCommonLoops(Pair[SI].Dst, LI->getLoopFor(Dst->getParent()),
3949 Pair[SI].Loops);
3950 Result.Consistent = false;
3951 } else if (Pair[SI].Classification == Subscript::ZIV) {
3952 // always separable
3953 Separable.set(SI);
3954 } else {
3955 // SIV, RDIV, or MIV, so check for coupled group
3956 bool Done = true;
3957 for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) {
3958 SmallBitVector Intersection = Pair[SI].GroupLoops;
3959 Intersection &= Pair[SJ].GroupLoops;
3960 if (Intersection.any()) {
3961 // accumulate set of all the loops in group
3962 Pair[SJ].GroupLoops |= Pair[SI].GroupLoops;
3963 // accumulate set of all subscripts in group
3964 Pair[SJ].Group |= Pair[SI].Group;
3965 Done = false;
3966 }
3967 }
3968 if (Done) {
3969 if (Pair[SI].Group.count() == 1) {
3970 Separable.set(SI);
3971 ++SeparableSubscriptPairs;
3972 } else {
3973 Coupled.set(SI);
3974 ++CoupledSubscriptPairs;
3975 }
3976 }
3977 }
3978 }
3979
3980 LLVM_DEBUG(dbgs() << " Separable = ");
3981 LLVM_DEBUG(dumpSmallBitVector(Separable));
3982 LLVM_DEBUG(dbgs() << " Coupled = ");
3984
3985 Constraint NewConstraint;
3986 NewConstraint.setAny(SE);
3987
3988 // test separable subscripts
3989 for (unsigned SI : Separable.set_bits()) {
3990 LLVM_DEBUG(dbgs() << "testing subscript " << SI);
3991 switch (Pair[SI].Classification) {
3992 case Subscript::ZIV:
3993 LLVM_DEBUG(dbgs() << ", ZIV\n");
3994 if (testZIV(Pair[SI].Src, Pair[SI].Dst, Result))
3995 return nullptr;
3996 break;
3997 case Subscript::SIV: {
3998 LLVM_DEBUG(dbgs() << ", SIV\n");
3999 unsigned Level;
4000 const SCEV *SplitIter = nullptr;
4001 if (testSIV(Pair[SI].Src, Pair[SI].Dst, Level, Result, NewConstraint,
4002 SplitIter))
4003 return nullptr;
4004 break;
4005 }
4006 case Subscript::RDIV:
4007 LLVM_DEBUG(dbgs() << ", RDIV\n");
4008 if (testRDIV(Pair[SI].Src, Pair[SI].Dst, Result))
4009 return nullptr;
4010 break;
4011 case Subscript::MIV:
4012 LLVM_DEBUG(dbgs() << ", MIV\n");
4013 if (testMIV(Pair[SI].Src, Pair[SI].Dst, Pair[SI].Loops, Result))
4014 return nullptr;
4015 break;
4016 default:
4017 llvm_unreachable("subscript has unexpected classification");
4018 }
4019 }
4020
4021 if (Coupled.count()) {
4022 // test coupled subscript groups
4023 LLVM_DEBUG(dbgs() << "starting on coupled subscripts\n");
4024 LLVM_DEBUG(dbgs() << "MaxLevels + 1 = " << MaxLevels + 1 << "\n");
4025 SmallVector<Constraint, 4> Constraints(MaxLevels + 1);
4026 for (unsigned II = 0; II <= MaxLevels; ++II)
4027 Constraints[II].setAny(SE);
4028 for (unsigned SI : Coupled.set_bits()) {
4029 LLVM_DEBUG(dbgs() << "testing subscript group " << SI << " { ");
4030 SmallBitVector Group(Pair[SI].Group);
4031 SmallBitVector Sivs(Pairs);
4032 SmallBitVector Mivs(Pairs);
4033 SmallBitVector ConstrainedLevels(MaxLevels + 1);
4034 SmallVector<Subscript *, 4> PairsInGroup;
4035 for (unsigned SJ : Group.set_bits()) {
4036 LLVM_DEBUG(dbgs() << SJ << " ");
4037 if (Pair[SJ].Classification == Subscript::SIV)
4038 Sivs.set(SJ);
4039 else
4040 Mivs.set(SJ);
4041 PairsInGroup.push_back(&Pair[SJ]);
4042 }
4043 unifySubscriptType(PairsInGroup);
4044 LLVM_DEBUG(dbgs() << "}\n");
4045 while (Sivs.any()) {
4046 bool Changed = false;
4047 for (unsigned SJ : Sivs.set_bits()) {
4048 LLVM_DEBUG(dbgs() << "testing subscript " << SJ << ", SIV\n");
4049 // SJ is an SIV subscript that's part of the current coupled group
4050 unsigned Level;
4051 const SCEV *SplitIter = nullptr;
4052 LLVM_DEBUG(dbgs() << "SIV\n");
4053 if (testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level, Result, NewConstraint,
4054 SplitIter))
4055 return nullptr;
4056 ConstrainedLevels.set(Level);
4057 if (intersectConstraints(&Constraints[Level], &NewConstraint)) {
4058 if (Constraints[Level].isEmpty()) {
4059 ++DeltaIndependence;
4060 return nullptr;
4061 }
4062 Changed = true;
4063 }
4064 Sivs.reset(SJ);
4065 }
4066 if (Changed) {
4067 // propagate, possibly creating new SIVs and ZIVs
4068 LLVM_DEBUG(dbgs() << " propagating\n");
4069 LLVM_DEBUG(dbgs() << "\tMivs = ");
4071 for (unsigned SJ : Mivs.set_bits()) {
4072 // SJ is an MIV subscript that's part of the current coupled group
4073 LLVM_DEBUG(dbgs() << "\tSJ = " << SJ << "\n");
4074 if (propagate(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops,
4075 Constraints, Result.Consistent)) {
4076 LLVM_DEBUG(dbgs() << "\t Changed\n");
4077 ++DeltaPropagations;
4078 Pair[SJ].Classification = classifyPair(
4079 Pair[SJ].Src, LI->getLoopFor(Src->getParent()), Pair[SJ].Dst,
4080 LI->getLoopFor(Dst->getParent()), Pair[SJ].Loops);
4081 switch (Pair[SJ].Classification) {
4082 case Subscript::ZIV:
4083 LLVM_DEBUG(dbgs() << "ZIV\n");
4084 if (testZIV(Pair[SJ].Src, Pair[SJ].Dst, Result))
4085 return nullptr;
4086 Mivs.reset(SJ);
4087 break;
4088 case Subscript::SIV:
4089 Sivs.set(SJ);
4090 Mivs.reset(SJ);
4091 break;
4092 case Subscript::RDIV:
4093 case Subscript::MIV:
4094 break;
4095 default:
4096 llvm_unreachable("bad subscript classification");
4097 }
4098 }
4099 }
4100 }
4101 }
4102
4103 // test & propagate remaining RDIVs
4104 for (unsigned SJ : Mivs.set_bits()) {
4105 if (Pair[SJ].Classification == Subscript::RDIV) {
4106 LLVM_DEBUG(dbgs() << "RDIV test\n");
4107 if (testRDIV(Pair[SJ].Src, Pair[SJ].Dst, Result))
4108 return nullptr;
4109 // I don't yet understand how to propagate RDIV results
4110 Mivs.reset(SJ);
4111 }
4112 }
4113
4114 // test remaining MIVs
4115 // This code is temporary.
4116 // Better to somehow test all remaining subscripts simultaneously.
4117 for (unsigned SJ : Mivs.set_bits()) {
4118 if (Pair[SJ].Classification == Subscript::MIV) {
4119 LLVM_DEBUG(dbgs() << "MIV test\n");
4120 if (testMIV(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops, Result))
4121 return nullptr;
4122 } else
4123 llvm_unreachable("expected only MIV subscripts at this point");
4124 }
4125
4126 // update Result.DV from constraint vector
4127 LLVM_DEBUG(dbgs() << " updating\n");
4128 for (unsigned SJ : ConstrainedLevels.set_bits()) {
4129 if (SJ > CommonLevels)
4130 break;
4131 updateDirection(Result.DV[SJ - 1], Constraints[SJ]);
4132 if (Result.DV[SJ - 1].Direction == Dependence::DVEntry::NONE)
4133 return nullptr;
4134 }
4135 }
4136 }
4137
4138 // Make sure the Scalar flags are set correctly.
4139 SmallBitVector CompleteLoops(MaxLevels + 1);
4140 for (unsigned SI = 0; SI < Pairs; ++SI)
4141 CompleteLoops |= Pair[SI].Loops;
4142 for (unsigned II = 1; II <= CommonLevels; ++II)
4143 if (CompleteLoops[II])
4144 Result.DV[II - 1].Scalar = false;
4145
4146 // Set the distance to zero if the direction is EQ.
4147 // TODO: Ideally, the distance should be set to 0 immediately simultaneously
4148 // with the corresponding direction being set to EQ.
4149 for (unsigned II = 1; II <= Result.getLevels(); ++II) {
4150 if (Result.getDirection(II) == Dependence::DVEntry::EQ) {
4151 if (Result.DV[II - 1].Distance == nullptr)
4152 Result.DV[II - 1].Distance = SE->getZero(SrcSCEV->getType());
4153 else
4154 assert(Result.DV[II - 1].Distance->isZero() &&
4155 "Inconsistency between distance and direction");
4156 }
4157
4158#ifndef NDEBUG
4159 // Check that the converse (i.e., if the distance is zero, then the
4160 // direction is EQ) holds.
4161 const SCEV *Distance = Result.getDistance(II);
4162 if (Distance && Distance->isZero())
4163 assert(Result.getDirection(II) == Dependence::DVEntry::EQ &&
4164 "Distance is zero, but direction is not EQ");
4165#endif
4166 }
4167
4168 if (SameSDLevels > 0) {
4169 // Extracting SameSD levels from the common levels
4170 // Reverting CommonLevels and MaxLevels to their original values
4171 assert(CommonLevels >= SameSDLevels);
4172 CommonLevels -= SameSDLevels;
4173 MaxLevels += SameSDLevels;
4174 std::unique_ptr<FullDependence::DVEntry[]> DV, DVSameSD;
4175 DV = std::make_unique<FullDependence::DVEntry[]>(CommonLevels);
4176 DVSameSD = std::make_unique<FullDependence::DVEntry[]>(SameSDLevels);
4177 for (unsigned Level = 0; Level < CommonLevels; ++Level)
4178 DV[Level] = Result.DV[Level];
4179 for (unsigned Level = 0; Level < SameSDLevels; ++Level)
4180 DVSameSD[Level] = Result.DV[CommonLevels + Level];
4181 Result.DV = std::move(DV);
4182 Result.DVSameSD = std::move(DVSameSD);
4183 Result.Levels = CommonLevels;
4184 Result.SameSDLevels = SameSDLevels;
4185 // Result is not consistent if it considers SameSD levels
4186 Result.Consistent = false;
4187 }
4188
4189 if (PossiblyLoopIndependent) {
4190 // Make sure the LoopIndependent flag is set correctly.
4191 // All directions must include equal, otherwise no
4192 // loop-independent dependence is possible.
4193 for (unsigned II = 1; II <= CommonLevels; ++II) {
4194 if (!(Result.getDirection(II) & Dependence::DVEntry::EQ)) {
4195 Result.LoopIndependent = false;
4196 break;
4197 }
4198 }
4199 } else {
4200 // On the other hand, if all directions are equal and there's no
4201 // loop-independent dependence possible, then no dependence exists.
4202 bool AllEqual = true;
4203 for (unsigned II = 1; II <= CommonLevels; ++II) {
4204 if (Result.getDirection(II) != Dependence::DVEntry::EQ) {
4205 AllEqual = false;
4206 break;
4207 }
4208 }
4209 if (AllEqual)
4210 return nullptr;
4211 }
4212
4213 return std::make_unique<FullDependence>(std::move(Result));
4214}
4215
4216//===----------------------------------------------------------------------===//
4217// getSplitIteration -
4218// Rather than spend rarely-used space recording the splitting iteration
4219// during the Weak-Crossing SIV test, we re-compute it on demand.
4220// The re-computation is basically a repeat of the entire dependence test,
4221// though simplified since we know that the dependence exists.
4222// It's tedious, since we must go through all propagations, etc.
4223//
4224// Care is required to keep this code up to date with respect to the routine
4225// above, depends().
4226//
4227// Generally, the dependence analyzer will be used to build
4228// a dependence graph for a function (basically a map from instructions
4229// to dependences). Looking for cycles in the graph shows us loops
4230// that cannot be trivially vectorized/parallelized.
4231//
4232// We can try to improve the situation by examining all the dependences
4233// that make up the cycle, looking for ones we can break.
4234// Sometimes, peeling the first or last iteration of a loop will break
4235// dependences, and we've got flags for those possibilities.
4236// Sometimes, splitting a loop at some other iteration will do the trick,
4237// and we've got a flag for that case. Rather than waste the space to
4238// record the exact iteration (since we rarely know), we provide
4239// a method that calculates the iteration. It's a drag that it must work
4240// from scratch, but wonderful in that it's possible.
4241//
4242// Here's an example:
4243//
4244// for (i = 0; i < 10; i++)
4245// A[i] = ...
4246// ... = A[11 - i]
4247//
4248// There's a loop-carried flow dependence from the store to the load,
4249// found by the weak-crossing SIV test. The dependence will have a flag,
4250// indicating that the dependence can be broken by splitting the loop.
4251// Calling getSplitIteration will return 5.
4252// Splitting the loop breaks the dependence, like so:
4253//
4254// for (i = 0; i <= 5; i++)
4255// A[i] = ...
4256// ... = A[11 - i]
4257// for (i = 6; i < 10; i++)
4258// A[i] = ...
4259// ... = A[11 - i]
4260//
4261// breaks the dependence and allows us to vectorize/parallelize
4262// both loops.
4264 unsigned SplitLevel) {
4265 assert(Dep.isSplitable(SplitLevel) &&
4266 "Dep should be splitable at SplitLevel");
4267 Instruction *Src = Dep.getSrc();
4268 Instruction *Dst = Dep.getDst();
4269 assert(Src->mayReadFromMemory() || Src->mayWriteToMemory());
4270 assert(Dst->mayReadFromMemory() || Dst->mayWriteToMemory());
4271 assert(isLoadOrStore(Src));
4272 assert(isLoadOrStore(Dst));
4273 Value *SrcPtr = getLoadStorePointerOperand(Src);
4274 Value *DstPtr = getLoadStorePointerOperand(Dst);
4276 AA, F->getDataLayout(), MemoryLocation::get(Dst),
4278
4279 // establish loop nesting levels
4280 establishNestingLevels(Src, Dst);
4281
4282 FullDependence Result(Src, Dst, Dep.Assumptions, false, CommonLevels);
4283
4284 unsigned Pairs = 1;
4285 SmallVector<Subscript, 2> Pair(Pairs);
4286 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
4287 const SCEV *DstSCEV = SE->getSCEV(DstPtr);
4288 Pair[0].Src = SE->removePointerBase(SrcSCEV);
4289 Pair[0].Dst = SE->removePointerBase(DstSCEV);
4290
4291 if (Delinearize) {
4292 if (tryDelinearize(Src, Dst, Pair)) {
4293 LLVM_DEBUG(dbgs() << " delinearized\n");
4294 Pairs = Pair.size();
4295 }
4296 }
4297
4298 for (unsigned P = 0; P < Pairs; ++P) {
4299 assert(Pair[P].Src->getType()->isIntegerTy() && "Src must be an integer");
4300 assert(Pair[P].Dst->getType()->isIntegerTy() && "Dst must be an integer");
4301 Pair[P].Loops.resize(MaxLevels + 1);
4302 Pair[P].GroupLoops.resize(MaxLevels + 1);
4303 Pair[P].Group.resize(Pairs);
4304 removeMatchingExtensions(&Pair[P]);
4305 Pair[P].Classification =
4306 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()), Pair[P].Dst,
4307 LI->getLoopFor(Dst->getParent()), Pair[P].Loops);
4308 Pair[P].GroupLoops = Pair[P].Loops;
4309 Pair[P].Group.set(P);
4310 }
4311
4312 SmallBitVector Separable(Pairs);
4313 SmallBitVector Coupled(Pairs);
4314
4315 // partition subscripts into separable and minimally-coupled groups
4316 for (unsigned SI = 0; SI < Pairs; ++SI) {
4317 if (Pair[SI].Classification == Subscript::NonLinear) {
4318 // ignore these, but collect loops for later
4319 collectCommonLoops(Pair[SI].Src, LI->getLoopFor(Src->getParent()),
4320 Pair[SI].Loops);
4321 collectCommonLoops(Pair[SI].Dst, LI->getLoopFor(Dst->getParent()),
4322 Pair[SI].Loops);
4323 Result.Consistent = false;
4324 } else if (Pair[SI].Classification == Subscript::ZIV)
4325 Separable.set(SI);
4326 else {
4327 // SIV, RDIV, or MIV, so check for coupled group
4328 bool Done = true;
4329 for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) {
4330 SmallBitVector Intersection = Pair[SI].GroupLoops;
4331 Intersection &= Pair[SJ].GroupLoops;
4332 if (Intersection.any()) {
4333 // accumulate set of all the loops in group
4334 Pair[SJ].GroupLoops |= Pair[SI].GroupLoops;
4335 // accumulate set of all subscripts in group
4336 Pair[SJ].Group |= Pair[SI].Group;
4337 Done = false;
4338 }
4339 }
4340 if (Done) {
4341 if (Pair[SI].Group.count() == 1)
4342 Separable.set(SI);
4343 else
4344 Coupled.set(SI);
4345 }
4346 }
4347 }
4348
4349 Constraint NewConstraint;
4350 NewConstraint.setAny(SE);
4351
4352 // test separable subscripts
4353 for (unsigned SI : Separable.set_bits()) {
4354 switch (Pair[SI].Classification) {
4355 case Subscript::SIV: {
4356 unsigned Level;
4357 const SCEV *SplitIter = nullptr;
4358 (void)testSIV(Pair[SI].Src, Pair[SI].Dst, Level, Result, NewConstraint,
4359 SplitIter);
4360 if (Level == SplitLevel) {
4361 assert(SplitIter != nullptr);
4362 return SplitIter;
4363 }
4364 break;
4365 }
4366 case Subscript::ZIV:
4367 case Subscript::RDIV:
4368 case Subscript::MIV:
4369 break;
4370 default:
4371 llvm_unreachable("subscript has unexpected classification");
4372 }
4373 }
4374
4375 assert(!Coupled.empty() && "coupled expected non-empty");
4376
4377 // test coupled subscript groups
4378 SmallVector<Constraint, 4> Constraints(MaxLevels + 1);
4379 for (unsigned II = 0; II <= MaxLevels; ++II)
4380 Constraints[II].setAny(SE);
4381 for (unsigned SI : Coupled.set_bits()) {
4382 SmallBitVector Group(Pair[SI].Group);
4383 SmallBitVector Sivs(Pairs);
4384 SmallBitVector Mivs(Pairs);
4385 SmallBitVector ConstrainedLevels(MaxLevels + 1);
4386 for (unsigned SJ : Group.set_bits()) {
4387 if (Pair[SJ].Classification == Subscript::SIV)
4388 Sivs.set(SJ);
4389 else
4390 Mivs.set(SJ);
4391 }
4392 while (Sivs.any()) {
4393 bool Changed = false;
4394 for (unsigned SJ : Sivs.set_bits()) {
4395 // SJ is an SIV subscript that's part of the current coupled group
4396 unsigned Level;
4397 const SCEV *SplitIter = nullptr;
4398 (void)testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level, Result, NewConstraint,
4399 SplitIter);
4400 if (Level == SplitLevel && SplitIter)
4401 return SplitIter;
4402 ConstrainedLevels.set(Level);
4403 if (intersectConstraints(&Constraints[Level], &NewConstraint))
4404 Changed = true;
4405 Sivs.reset(SJ);
4406 }
4407 if (!Changed)
4408 continue;
4409 // propagate, possibly creating new SIVs and ZIVs
4410 for (unsigned SJ : Mivs.set_bits()) {
4411 // SJ is an MIV subscript that's part of the current coupled group
4412 if (!propagate(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops, Constraints,
4413 Result.Consistent))
4414 continue;
4415 Pair[SJ].Classification = classifyPair(
4416 Pair[SJ].Src, LI->getLoopFor(Src->getParent()), Pair[SJ].Dst,
4417 LI->getLoopFor(Dst->getParent()), Pair[SJ].Loops);
4418 switch (Pair[SJ].Classification) {
4419 case Subscript::ZIV:
4420 Mivs.reset(SJ);
4421 break;
4422 case Subscript::SIV:
4423 Sivs.set(SJ);
4424 Mivs.reset(SJ);
4425 break;
4426 case Subscript::RDIV:
4427 case Subscript::MIV:
4428 break;
4429 default:
4430 llvm_unreachable("bad subscript classification");
4431 }
4432 }
4433 }
4434 }
4435 llvm_unreachable("somehow reached end of routine");
4436}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:638
static bool isLoadOrStore(const Instruction *I)
static bool findGCD(unsigned Bits, const APInt &AM, const APInt &BM, const APInt &Delta, APInt &G, APInt &X, APInt &Y)
static void dumpSmallBitVector(SmallBitVector &BV)
static APInt ceilingOfQuotient(const APInt &A, const APInt &B)
static APInt floorOfQuotient(const APInt &A, const APInt &B)
static const SCEV * minusSCEVNoSignedOverflow(const SCEV *A, const SCEV *B, ScalarEvolution &SE)
Returns A - B if it guaranteed not to signed wrap.
static AliasResult underlyingObjectsAlias(AAResults *AA, const DataLayout &DL, const MemoryLocation &LocA, const MemoryLocation &LocB)
static cl::opt< bool > RunSIVRoutinesOnly("da-run-siv-routines-only", cl::init(false), cl::ReallyHidden, cl::desc("Run only SIV routines and disable others (ZIV, RDIV, and MIV). " "The purpose is mainly to exclude the influence of those routines " "in regression tests for SIV routines."))
static std::pair< std::optional< APInt >, std::optional< APInt > > inferDomainOfAffine(const APInt &A, const APInt &B, const std::optional< APInt > &UB)
Given an affine expression of the form A*k + B, where k is an arbitrary integer, infer the possible r...
static std::optional< APInt > getConstantPart(const SCEV *Expr)
static bool isRemainderZero(const SCEVConstant *Dividend, const SCEVConstant *Divisor)
static cl::opt< bool > Delinearize("da-delinearize", cl::init(true), cl::Hidden, cl::desc("Try to delinearize array references."))
static void dumpExampleDependence(raw_ostream &OS, DependenceInfo *DA, ScalarEvolution &SE, bool NormalizeResults)
static cl::opt< unsigned > MIVMaxLevelThreshold("da-miv-max-level-threshold", cl::init(7), cl::Hidden, cl::desc("Maximum depth allowed for the recursive algorithm used to " "explore MIV direction vectors."))
static cl::opt< bool > DisableDelinearizationChecks("da-disable-delinearization-checks", cl::Hidden, cl::desc("Disable checks that try to statically verify validity of " "delinearized subscripts. Enabling this option may result in incorrect " "dependence vectors for languages that allow the subscript of one " "dimension to underflow or overflow into another dimension."))
Hexagon Hardware Loops
Module.h This file contains the declarations for the Module class.
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:539
Loop::LoopBounds::Direction Direction
Definition LoopInfo.cpp:231
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
#define G(x, y, z)
Definition MD5.cpp:56
#define T
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
static bool isInput(const ArrayRef< StringRef > &Prefixes, StringRef Arg)
Definition OptTable.cpp:146
#define P(N)
FunctionAnalysisManager FAM
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
LocallyHashedType DenseMapInfo< LocallyHashedType >::Empty
A manager for alias analyses.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
Class for arbitrary precision integers.
Definition APInt.h:78
static LLVM_ABI void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Definition APInt.cpp:1890
APInt abs() const
Get the absolute value.
Definition APInt.h:1795
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition APInt.h:1201
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1488
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:209
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1644
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1166
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:219
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1736
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition APInt.h:1130
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:200
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1237
The possible results of an alias query.
@ MayAlias
The two locations may or may not alias.
@ NoAlias
The two locations do not alias at all.
@ PartialAlias
The two locations alias, but only due to a partial overlap.
@ MustAlias
The two locations precisely alias each other.
This templated class represents "all analyses that operate over <aparticular IR unit>" (e....
Definition Analysis.h:50
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
AnalysisUsage & addRequiredTransitive()
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
void enableCrossIterationMode()
Assume that values may come from different cycle iterations.
bool isNoAlias(const MemoryLocation &LocA, const MemoryLocation &LocB)
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:678
@ ICMP_SLT
signed less than
Definition InstrTypes.h:707
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:708
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:705
@ ICMP_NE
not equal
Definition InstrTypes.h:700
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:706
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
Legacy pass manager pass to access dependence information.
void getAnalysisUsage(AnalysisUsage &) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
void print(raw_ostream &, const Module *=nullptr) const override
print - Print out the internal state of the pass.
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
AnalysisPass to compute dependence information in a function.
LLVM_ABI Result run(Function &F, FunctionAnalysisManager &FAM)
DependenceInfo - This class is the main dependence-analysis driver.
LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle transitive invalidation when the cached analysis results go away.
LLVM_ABI const SCEV * getSplitIteration(const Dependence &Dep, unsigned Level)
getSplitIteration - Give a dependence that's splittable at some particular level, return the iteratio...
LLVM_ABI SCEVUnionPredicate getRuntimeAssumptions() const
getRuntimeAssumptions - Returns all the runtime assumptions under which the dependence test is valid.
LLVM_ABI std::unique_ptr< Dependence > depends(Instruction *Src, Instruction *Dst, bool UnderRuntimeAssumptions=false)
depends - Tests for a dependence between the Src and Dst instructions.
Dependence - This class represents a dependence between two memory memory references in a function.
Instruction * getDst() const
getDst - Returns the destination instruction for this dependence.
Dependence(Dependence &&)=default
virtual bool isConfused() const
isConfused - Returns true if this dependence is confused (the compiler understands nothing and makes ...
virtual unsigned getSameSDLevels() const
getSameSDLevels - Returns the number of separate SameSD loops surrounding the source and destination ...
virtual const SCEV * getDistance(unsigned Level, bool SameSD=false) const
getDistance - Returns the distance (or NULL) associated with a particular common or SameSD level.
virtual bool isPeelLast(unsigned Level, bool SameSD=false) const
isPeelLast - Returns true if peeling the last iteration from this regular or SameSD loop level will b...
virtual bool isConsistent() const
isConsistent - Returns true if this dependence is consistent (occurs every time the source and destin...
virtual unsigned getLevels() const
getLevels - Returns the number of common loops surrounding the source and destination of the dependen...
virtual unsigned getDirection(unsigned Level, bool SameSD=false) const
getDirection - Returns the direction associated with a particular common or SameSD level.
virtual bool isScalar(unsigned Level, bool SameSD=false) const
isScalar - Returns true if a particular regular or SameSD level is scalar; that is,...
bool isFlow() const
isFlow - Returns true if this is a flow (aka true) dependence.
virtual bool isPeelFirst(unsigned Level, bool SameSD=false) const
isPeelFirst - Returns true if peeling the first iteration from this regular or SameSD loop level will...
bool isInput() const
isInput - Returns true if this is an input dependence.
bool isAnti() const
isAnti - Returns true if this is an anti dependence.
virtual bool isSplitable(unsigned Level, bool SameSD=false) const
isSplitable - Returns true if splitting the loop will break the dependence.
Instruction * getSrc() const
getSrc - Returns the source instruction for this dependence.
virtual bool isLoopIndependent() const
isLoopIndependent - Returns true if this is a loop-independent dependence.
bool isOutput() const
isOutput - Returns true if this is an output dependence.
void dumpImp(raw_ostream &OS, bool SameSD=false) const
dumpImp - For debugging purposes.
void dump(raw_ostream &OS) const
dump - For debugging purposes, dumps a dependence to OS.
virtual bool inSameSDLoops(unsigned Level) const
inSameSDLoops - Returns true if this level is an SameSD level, i.e., performed across two separate lo...
FullDependence - This class represents a dependence between two memory references in a function.
FullDependence(Instruction *Source, Instruction *Destination, const SCEVUnionPredicate &Assumes, bool PossiblyLoopIndependent, unsigned Levels)
unsigned getDirection(unsigned Level, bool SameSD=false) const override
getDirection - Returns the direction associated with a particular common or SameSD level.
bool isScalar(unsigned Level, bool SameSD=false) const override
isScalar - Returns true if a particular regular or SameSD level is scalar; that is,...
bool isDirectionNegative() const override
Check if the direction vector is negative.
const SCEV * getDistance(unsigned Level, bool SameSD=false) const override
getDistance - Returns the distance (or NULL) associated with a particular common or SameSD level.
bool isSplitable(unsigned Level, bool SameSD=false) const override
isSplitable - Returns true if splitting the loop will break the dependence.
bool isPeelLast(unsigned Level, bool SameSD=false) const override
isPeelLast - Returns true if peeling the last iteration from this regular or SameSD loop level will b...
bool isPeelFirst(unsigned Level, bool SameSD=false) const override
isPeelFirst - Returns true if peeling the first iteration from this regular or SameSD loop level will...
bool inSameSDLoops(unsigned Level) const override
inSameSDLoops - Returns true if this level is an SameSD level, i.e., performed across two separate lo...
DVEntry getDVEntry(unsigned Level, bool isSameSD) const
getDVEntry - Returns the DV entry associated with a regular or a SameSD level.
bool normalize(ScalarEvolution *SE) override
If the direction vector is negative, normalize the direction vector to make it non-negative.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
FunctionPass(char &pid)
Definition Pass.h:316
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
An instruction for reading from memory.
bool isPrecise() const
uint64_t toRaw() const
Analysis pass that exposes the LoopInfo for a function.
Definition LoopInfo.h:569
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
const LoopT * getOutermostLoop() const
Get the outermost loop in which this loop is contained.
unsigned getLoopDepth() const
Return the nesting level of this loop.
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
The legacy pass manager's analysis pass to compute loop information.
Definition LoopInfo.h:596
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Representation for a specific memory location.
static LLVM_ABI MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
LocationSize Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known.
static MemoryLocation getBeforeOrAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location before or after Ptr, while remaining within the underl...
AAMDNodes AATags
The metadata nodes which describes the aliasing of the location (each member is null if that kind of ...
const Value * Ptr
The address of the start of the location.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition Analysis.h:275
LLVM_ABI const SCEV * evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const
Return the value of this chain of recurrences at the specified iteration number.
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values.
const SCEV * getOperand() const
This class represents a constant integer value.
const APInt & getAPInt() const
NoWrapFlags getNoWrapFlags(NoWrapFlags Mask=NoWrapMask) const
const SCEV * getOperand(unsigned i) const
This class represents an assumption made using SCEV expressions which can be checked at run-time.
This class represents a composition of other SCEV predicates, and is the class that most clients will...
void print(raw_ostream &OS, unsigned Depth) const override
Prints a textual representation of this predicate with an indentation of Depth.
bool isAlwaysTrue() const override
Implementation of the SCEVPredicate interface.
This class represents an analyzed expression in the program.
LLVM_ABI ArrayRef< const SCEV * > operands() const
Return operands of this SCEV expression.
LLVM_ABI bool isOne() const
Return true if the expression is a constant one.
LLVM_ABI bool isZero() const
Return true if the expression is a constant zero.
LLVM_ABI Type * getType() const
Return the LLVM type of this SCEV expression.
Analysis pass that exposes the ScalarEvolution for a function.
The main scalar evolution driver.
LLVM_ABI const SCEV * getNegativeSCEV(const SCEV *V, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
Return the SCEV object corresponding to -V.
LLVM_ABI bool willNotOverflow(Instruction::BinaryOps BinOp, bool Signed, const SCEV *LHS, const SCEV *RHS, const Instruction *CtxI=nullptr)
Is operation BinOp between LHS and RHS provably does not have a signed/unsigned overflow (Signed)?
LLVM_ABI const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
This is a 'bitvector' (really, a variable-sized bit array), optimized for the case when the array is ...
SmallBitVector & set()
iterator_range< const_set_bits_iterator > set_bits() const
int find_next(unsigned Prev) const
Returns the index of the next set bit following the "Prev" bit.
bool any() const
Returns true if any bit is set.
bool empty() const
Tests whether there are no bits in this bitvector.
size_type count() const
Returns the number of bits which are set.
SmallBitVector & reset()
void resize(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract Attribute helper functions.
Definition Attributor.h:165
const APInt & smin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
Definition APInt.h:2248
const APInt & smax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be signed.
Definition APInt.h:2253
LLVM_ABI APInt GreatestCommonDivisor(APInt A, APInt B)
Compute GCD of two unsigned APInt values.
Definition APInt.cpp:798
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ TB
TB - TwoByte - Set if this instruction has a two byte opcode, which starts with a 0x0F byte before th...
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
InstIterator< SymbolTableList< BasicBlock >, Function::iterator, BasicBlock::iterator, Instruction > inst_iterator
void collectParametricTerms(ScalarEvolution &SE, const SCEV *Expr, SmallVectorImpl< const SCEV * > &Terms)
Collect parametric terms occurring in step expressions (first step of delinearization).
void findArrayDimensions(ScalarEvolution &SE, SmallVectorImpl< const SCEV * > &Terms, SmallVectorImpl< const SCEV * > &Sizes, const SCEV *ElementSize)
Compute the array dimensions Sizes from the set of Terms extracted from the memory access function of...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
@ Done
Definition Threading.h:60
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
inst_iterator inst_begin(Function *F)
void computeAccessFunctions(ScalarEvolution &SE, const SCEV *Expr, SmallVectorImpl< const SCEV * > &Subscripts, SmallVectorImpl< const SCEV * > &Sizes)
Return in Subscripts the access functions for each dimension in Sizes (third step of delinearization)...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
inst_iterator inst_end(Function *F)
bool tryDelinearizeFixedSizeImpl(ScalarEvolution *SE, Instruction *Inst, const SCEV *AccessFn, SmallVectorImpl< const SCEV * > &Subscripts, SmallVectorImpl< int > &Sizes)
Implementation of fixed size array delinearization.
ArrayRef(const T &OneElt) -> ArrayRef< T >
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
LLVM_ABI FunctionPass * createDependenceAnalysisWrapperPass()
createDependenceAnalysisPass - This creates an instance of the DependenceAnalysis wrapper pass.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:853
#define N
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
Dependence::DVEntry - Each level in the distance/direction vector has a direction (or perhaps a union...