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