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