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