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// Since Clang linearizes some array subscripts, the dependence
22// analysis is using SCEV->delinearize to recover the representation of multiple
23// subscripts, and thus avoid the more expensive and less precise MIV tests. The
24// delinearization is controlled by the flag -da-delinearize.
25//
26// We should pay some careful attention to the possibility of integer overflow
27// in the implementation of the various tests. This could happen with Add,
28// Subtract, or Multiply, with both APInt's and SCEV's.
29//
30// Some non-linear subscript pairs can be handled by the GCD test
31// (and perhaps other tests).
32// Should explore how often these things occur.
33//
34// Finally, it seems like certain test cases expose weaknesses in the SCEV
35// simplification, especially in the handling of sign and zero extensions.
36// It could be useful to spend time exploring these.
37//
38// Please note that this is work in progress and the interface is subject to
39// change.
40//
41//===----------------------------------------------------------------------===//
42// //
43// In memory of Ken Kennedy, 1945 - 2007 //
44// //
45//===----------------------------------------------------------------------===//
46
48#include "llvm/ADT/Statistic.h"
56#include "llvm/IR/Module.h"
59#include "llvm/Support/Debug.h"
62
63using namespace llvm;
64
65#define DEBUG_TYPE "da"
66
67//===----------------------------------------------------------------------===//
68// statistics
69
70STATISTIC(TotalArrayPairs, "Array pairs tested");
71STATISTIC(NonlinearSubscriptPairs, "Nonlinear subscript pairs");
72STATISTIC(ZIVapplications, "ZIV applications");
73STATISTIC(ZIVindependence, "ZIV independence");
74STATISTIC(StrongSIVapplications, "Strong SIV applications");
75STATISTIC(StrongSIVsuccesses, "Strong SIV successes");
76STATISTIC(StrongSIVindependence, "Strong SIV independence");
77STATISTIC(WeakCrossingSIVapplications, "Weak-Crossing SIV applications");
78STATISTIC(WeakCrossingSIVsuccesses, "Weak-Crossing SIV successes");
79STATISTIC(WeakCrossingSIVindependence, "Weak-Crossing SIV independence");
80STATISTIC(ExactSIVapplications, "Exact SIV applications");
81STATISTIC(ExactSIVsuccesses, "Exact SIV successes");
82STATISTIC(ExactSIVindependence, "Exact SIV independence");
83STATISTIC(WeakZeroSIVapplications, "Weak-Zero SIV applications");
84STATISTIC(WeakZeroSIVsuccesses, "Weak-Zero SIV successes");
85STATISTIC(WeakZeroSIVindependence, "Weak-Zero SIV independence");
86STATISTIC(ExactRDIVapplications, "Exact RDIV applications");
87STATISTIC(ExactRDIVindependence, "Exact RDIV independence");
88STATISTIC(SymbolicRDIVapplications, "Symbolic RDIV applications");
89STATISTIC(SymbolicRDIVindependence, "Symbolic RDIV independence");
90STATISTIC(GCDapplications, "GCD applications");
91STATISTIC(GCDsuccesses, "GCD successes");
92STATISTIC(GCDindependence, "GCD independence");
93STATISTIC(BanerjeeApplications, "Banerjee applications");
94STATISTIC(BanerjeeIndependence, "Banerjee independence");
95STATISTIC(BanerjeeSuccesses, "Banerjee successes");
96STATISTIC(SameSDLoopsCount, "Loops with Same iteration Space and Depth");
97
98static cl::opt<bool>
99 Delinearize("da-delinearize", cl::init(true), cl::Hidden,
100 cl::desc("Try to delinearize array references."));
102 "da-disable-delinearization-checks", cl::Hidden,
103 cl::desc(
104 "Disable checks that try to statically verify validity of "
105 "delinearized subscripts. Enabling this option may result in incorrect "
106 "dependence vectors for languages that allow the subscript of one "
107 "dimension to underflow or overflow into another dimension."));
108
110 "da-miv-max-level-threshold", cl::init(7), cl::Hidden,
111 cl::desc("Maximum depth allowed for the recursive algorithm used to "
112 "explore MIV direction vectors."));
113
114namespace {
115
116/// Types of dependence test routines.
117enum class DependenceTestType {
118 All,
119 StrongSIV,
120 WeakCrossingSIV,
121 ExactSIV,
122 WeakZeroSIV,
123 ExactRDIV,
124 SymbolicRDIV,
125 GCDMIV,
126 BanerjeeMIV,
127};
128
129} // anonymous namespace
130
132 "da-enable-dependence-test", cl::init(DependenceTestType::All),
134 cl::desc("Run only specified dependence test routine and disable others. "
135 "The purpose is mainly to exclude the influence of other "
136 "dependence test routines in regression tests. If set to All, all "
137 "dependence test routines are enabled."),
138 cl::values(clEnumValN(DependenceTestType::All, "all",
139 "Enable all dependence test routines."),
140 clEnumValN(DependenceTestType::StrongSIV, "strong-siv",
141 "Enable only Strong SIV test."),
142 clEnumValN(DependenceTestType::WeakCrossingSIV,
143 "weak-crossing-siv",
144 "Enable only Weak-Crossing SIV test."),
145 clEnumValN(DependenceTestType::ExactSIV, "exact-siv",
146 "Enable only Exact SIV test."),
147 clEnumValN(DependenceTestType::WeakZeroSIV, "weak-zero-siv",
148 "Enable only Weak-Zero SIV test."),
149 clEnumValN(DependenceTestType::ExactRDIV, "exact-rdiv",
150 "Enable only Exact RDIV test."),
151 clEnumValN(DependenceTestType::SymbolicRDIV, "symbolic-rdiv",
152 "Enable only Symbolic RDIV test."),
153 clEnumValN(DependenceTestType::GCDMIV, "gcd-miv",
154 "Enable only GCD MIV test."),
155 clEnumValN(DependenceTestType::BanerjeeMIV, "banerjee-miv",
156 "Enable only Banerjee MIV test.")));
157
158// TODO: This flag is disabled by default because it is still under development.
159// Enable it or delete this flag when the feature is ready.
161 "da-enable-monotonicity-check", cl::init(false), cl::Hidden,
162 cl::desc("Check if the subscripts are monotonic. If it's not, dependence "
163 "is reported as unknown."));
164
166 "da-dump-monotonicity-report", cl::init(false), cl::Hidden,
167 cl::desc(
168 "When printing analysis, dump the results of monotonicity checks."));
169
170//===----------------------------------------------------------------------===//
171// basics
172
175 auto &AA = FAM.getResult<AAManager>(F);
176 auto &SE = FAM.getResult<ScalarEvolutionAnalysis>(F);
177 auto &LI = FAM.getResult<LoopAnalysis>(F);
178 return DependenceInfo(&F, &AA, &SE, &LI);
179}
180
181AnalysisKey DependenceAnalysis::Key;
182
184 "Dependence Analysis", true, true)
190
192
195
199
201 auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
202 auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
203 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
204 info.reset(new DependenceInfo(&F, &AA, &SE, &LI));
205 return false;
206}
207
209
211
218
219namespace {
220
221/// The property of monotonicity of a SCEV. To define the monotonicity, assume
222/// a SCEV defined within N-nested loops. Let i_k denote the iteration number
223/// of the k-th loop. Then we can regard the SCEV as an N-ary function:
224///
225/// F(i_1, i_2, ..., i_N)
226///
227/// The domain of i_k is the closed range [0, BTC_k], where BTC_k is the
228/// backedge-taken count of the k-th loop.
229///
230/// A function F is said to be "monotonically increasing with respect to the
231/// k-th loop" if x <= y implies the following condition:
232///
233/// F(i_1, ..., i_{k-1}, x, i_{k+1}, ..., i_N) <=
234/// F(i_1, ..., i_{k-1}, y, i_{k+1}, ..., i_N)
235///
236/// where i_1, ..., i_{k-1}, i_{k+1}, ..., i_N, x, and y are elements of their
237/// respective domains.
238///
239/// Likewise F is "monotonically decreasing with respect to the k-th loop"
240/// if x <= y implies
241///
242/// F(i_1, ..., i_{k-1}, x, i_{k+1}, ..., i_N) >=
243/// F(i_1, ..., i_{k-1}, y, i_{k+1}, ..., i_N)
244///
245/// A function F that is monotonically increasing or decreasing with respect to
246/// the k-th loop is simply called "monotonic with respect to k-th loop".
247///
248/// A function F is said to be "multivariate monotonic" when it is monotonic
249/// with respect to all of the N loops.
250///
251/// Since integer comparison can be either signed or unsigned, we need to
252/// distinguish monotonicity in the signed sense from that in the unsigned
253/// sense. Note that the inequality "x <= y" merely indicates loop progression
254/// and is not affected by the difference between signed and unsigned order.
255///
256/// Currently we only consider monotonicity in a signed sense.
257enum class SCEVMonotonicityType {
258 /// We don't know anything about the monotonicity of the SCEV.
259 Unknown,
260
261 /// The SCEV is loop-invariant with respect to the outermost loop. In other
262 /// words, the function F corresponding to the SCEV is a constant function.
263 Invariant,
264
265 /// The function F corresponding to the SCEV is multivariate monotonic in a
266 /// signed sense. Note that the multivariate monotonic function may also be a
267 /// constant function. The order employed in the definition of monotonicity
268 /// is not strict order.
269 MultivariateSignedMonotonic,
270};
271
272struct SCEVMonotonicity {
273 SCEVMonotonicity(SCEVMonotonicityType Type,
274 const SCEV *FailurePoint = nullptr);
275
276 SCEVMonotonicityType getType() const { return Type; }
277
278 const SCEV *getFailurePoint() const { return FailurePoint; }
279
280 bool isUnknown() const { return Type == SCEVMonotonicityType::Unknown; }
281
282 void print(raw_ostream &OS, unsigned Depth) const;
283
284private:
285 SCEVMonotonicityType Type;
286
287 /// The subexpression that caused Unknown. Mainly for debugging purpose.
288 const SCEV *FailurePoint;
289};
290
291/// Check the monotonicity of a SCEV. Since dependence tests (SIV, MIV, etc.)
292/// assume that subscript expressions are (multivariate) monotonic, we need to
293/// verify this property before applying those tests. Violating this assumption
294/// may cause them to produce incorrect results.
295struct SCEVMonotonicityChecker
296 : public SCEVVisitor<SCEVMonotonicityChecker, SCEVMonotonicity> {
297
298 SCEVMonotonicityChecker(ScalarEvolution *SE) : SE(SE) {}
299
300 /// Check the monotonicity of \p Expr. \p Expr must be integer type. If \p
301 /// OutermostLoop is not null, \p Expr must be defined in \p OutermostLoop or
302 /// one of its nested loops.
303 SCEVMonotonicity checkMonotonicity(const SCEV *Expr,
304 const Loop *OutermostLoop);
305
306private:
307 ScalarEvolution *SE;
308
309 /// The outermost loop that DA is analyzing.
310 const Loop *OutermostLoop;
311
312 /// A helper to classify \p Expr as either Invariant or Unknown.
313 SCEVMonotonicity invariantOrUnknown(const SCEV *Expr);
314
315 /// Return true if \p Expr is loop-invariant with respect to the outermost
316 /// loop.
317 bool isLoopInvariant(const SCEV *Expr) const;
318
319 /// A helper to create an Unknown SCEVMonotonicity.
320 SCEVMonotonicity createUnknown(const SCEV *FailurePoint) {
321 return SCEVMonotonicity(SCEVMonotonicityType::Unknown, FailurePoint);
322 }
323
324 SCEVMonotonicity visitAddRecExpr(const SCEVAddRecExpr *Expr);
325
326 SCEVMonotonicity visitConstant(const SCEVConstant *) {
327 return SCEVMonotonicity(SCEVMonotonicityType::Invariant);
328 }
329 SCEVMonotonicity visitVScale(const SCEVVScale *) {
330 return SCEVMonotonicity(SCEVMonotonicityType::Invariant);
331 }
332
333 // TODO: Handle more cases.
334 SCEVMonotonicity visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
335 return invariantOrUnknown(Expr);
336 }
337 SCEVMonotonicity visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
338 return invariantOrUnknown(Expr);
339 }
340 SCEVMonotonicity visitAddExpr(const SCEVAddExpr *Expr) {
341 return invariantOrUnknown(Expr);
342 }
343 SCEVMonotonicity visitMulExpr(const SCEVMulExpr *Expr) {
344 return invariantOrUnknown(Expr);
345 }
346 SCEVMonotonicity visitPtrToIntExpr(const SCEVPtrToIntExpr *Expr) {
347 return invariantOrUnknown(Expr);
348 }
349 SCEVMonotonicity visitTruncateExpr(const SCEVTruncateExpr *Expr) {
350 return invariantOrUnknown(Expr);
351 }
352 SCEVMonotonicity visitUDivExpr(const SCEVUDivExpr *Expr) {
353 return invariantOrUnknown(Expr);
354 }
355 SCEVMonotonicity visitSMaxExpr(const SCEVSMaxExpr *Expr) {
356 return invariantOrUnknown(Expr);
357 }
358 SCEVMonotonicity visitUMaxExpr(const SCEVUMaxExpr *Expr) {
359 return invariantOrUnknown(Expr);
360 }
361 SCEVMonotonicity visitSMinExpr(const SCEVSMinExpr *Expr) {
362 return invariantOrUnknown(Expr);
363 }
364 SCEVMonotonicity visitUMinExpr(const SCEVUMinExpr *Expr) {
365 return invariantOrUnknown(Expr);
366 }
367 SCEVMonotonicity visitSequentialUMinExpr(const SCEVSequentialUMinExpr *Expr) {
368 return invariantOrUnknown(Expr);
369 }
370 SCEVMonotonicity visitUnknown(const SCEVUnknown *Expr) {
371 return invariantOrUnknown(Expr);
372 }
373 SCEVMonotonicity visitCouldNotCompute(const SCEVCouldNotCompute *Expr) {
374 return invariantOrUnknown(Expr);
375 }
376
377 friend struct SCEVVisitor<SCEVMonotonicityChecker, SCEVMonotonicity>;
378};
379
380} // anonymous namespace
381
382// Used to test the dependence analyzer.
383// Looks through the function, noting instructions that may access memory.
384// Calls depends() on every possible pair and prints out the result.
385// Ignores all other instructions.
387 ScalarEvolution &SE, LoopInfo &LI,
388 bool NormalizeResults) {
389 auto *F = DA->getFunction();
390
392 SCEVMonotonicityChecker Checker(&SE);
393 OS << "Monotonicity check:\n";
394 for (Instruction &Inst : instructions(F)) {
395 if (!isa<LoadInst>(Inst) && !isa<StoreInst>(Inst))
396 continue;
397 Value *Ptr = getLoadStorePointerOperand(&Inst);
398 const Loop *L = LI.getLoopFor(Inst.getParent());
399 const Loop *OutermostLoop = L ? L->getOutermostLoop() : nullptr;
400 const SCEV *PtrSCEV = SE.getSCEVAtScope(Ptr, L);
401 const SCEV *AccessFn = SE.removePointerBase(PtrSCEV);
402 SCEVMonotonicity Mon = Checker.checkMonotonicity(AccessFn, OutermostLoop);
403 OS.indent(2) << "Inst: " << Inst << "\n";
404 OS.indent(4) << "Expr: " << *AccessFn << "\n";
405 Mon.print(OS, 4);
406 }
407 OS << "\n";
408 }
409
410 for (inst_iterator SrcI = inst_begin(F), SrcE = inst_end(F); SrcI != SrcE;
411 ++SrcI) {
412 if (SrcI->mayReadOrWriteMemory()) {
413 for (inst_iterator DstI = SrcI, DstE = inst_end(F); DstI != DstE;
414 ++DstI) {
415 if (DstI->mayReadOrWriteMemory()) {
416 OS << "Src:" << *SrcI << " --> Dst:" << *DstI << "\n";
417 OS << " da analyze - ";
418 if (auto D = DA->depends(&*SrcI, &*DstI,
419 /*UnderRuntimeAssumptions=*/true)) {
420
421#ifndef NDEBUG
422 // Verify that the distance being zero is equivalent to the
423 // direction being EQ.
424 for (unsigned Level = 1; Level <= D->getLevels(); Level++) {
425 const SCEV *Distance = D->getDistance(Level);
426 bool IsDistanceZero = Distance && Distance->isZero();
427 bool IsDirectionEQ =
428 D->getDirection(Level) == Dependence::DVEntry::EQ;
429 assert(IsDistanceZero == IsDirectionEQ &&
430 "Inconsistent distance and direction.");
431 }
432#endif
433
434 // Normalize negative direction vectors if required by clients.
435 if (NormalizeResults && D->normalize(&SE))
436 OS << "normalized - ";
437 D->dump(OS);
438 } else
439 OS << "none!\n";
440 }
441 }
442 }
443 }
444}
445
447 const Module *) const {
449 OS, info.get(), getAnalysis<ScalarEvolutionWrapperPass>().getSE(),
450 getAnalysis<LoopInfoWrapperPass>().getLoopInfo(), false);
451}
452
455 OS << "Printing analysis 'Dependence Analysis' for function '" << F.getName()
456 << "':\n";
458 FAM.getResult<ScalarEvolutionAnalysis>(F),
459 FAM.getResult<LoopAnalysis>(F), NormalizeResults);
460 return PreservedAnalyses::all();
461}
462
463//===----------------------------------------------------------------------===//
464// Dependence methods
465
466// Returns true if this is an input dependence.
468 return Src->mayReadFromMemory() && Dst->mayReadFromMemory();
469}
470
471// Returns true if this is an output dependence.
473 return Src->mayWriteToMemory() && Dst->mayWriteToMemory();
474}
475
476// Returns true if this is an flow (aka true) dependence.
477bool Dependence::isFlow() const {
478 return Src->mayWriteToMemory() && Dst->mayReadFromMemory();
479}
480
481// Returns true if this is an anti dependence.
482bool Dependence::isAnti() const {
483 return Src->mayReadFromMemory() && Dst->mayWriteToMemory();
484}
485
486// Returns true if a particular level is scalar; that is,
487// if no subscript in the source or destination mention the induction
488// variable associated with the loop at this level.
489// Leave this out of line, so it will serve as a virtual method anchor
490bool Dependence::isScalar(unsigned level, bool IsSameSD) const { return false; }
491
492//===----------------------------------------------------------------------===//
493// FullDependence methods
494
496 const SCEVUnionPredicate &Assumes,
497 bool PossiblyLoopIndependent,
498 unsigned CommonLevels)
499 : Dependence(Source, Destination, Assumes), Levels(CommonLevels),
500 LoopIndependent(PossiblyLoopIndependent) {
501 Consistent = true;
502 SameSDLevels = 0;
503 if (CommonLevels)
504 DV = std::make_unique<DVEntry[]>(CommonLevels);
505}
506
507// FIXME: in some cases the meaning of a negative direction vector
508// may not be straightforward, e.g.,
509// for (int i = 0; i < 32; ++i) {
510// Src: A[i] = ...;
511// Dst: use(A[31 - i]);
512// }
513// The dependency is
514// flow { Src[i] -> Dst[31 - i] : when i >= 16 } and
515// anti { Dst[i] -> Src[31 - i] : when i < 16 },
516// -- hence a [<>].
517// As long as a dependence result contains '>' ('<>', '<=>', "*"), it
518// means that a reversed/normalized dependence needs to be considered
519// as well. Nevertheless, current isDirectionNegative() only returns
520// true with a '>' or '>=' dependency for ease of canonicalizing the
521// dependency vector, since the reverse of '<>', '<=>' and "*" is itself.
523 for (unsigned Level = 1; Level <= Levels; ++Level) {
524 unsigned char Direction = DV[Level - 1].Direction;
525 if (Direction == Dependence::DVEntry::EQ)
526 continue;
527 if (Direction == Dependence::DVEntry::GT ||
528 Direction == Dependence::DVEntry::GE)
529 return true;
530 return false;
531 }
532 return false;
533}
534
536 if (!isDirectionNegative())
537 return false;
538
539 LLVM_DEBUG(dbgs() << "Before normalizing negative direction vectors:\n";
540 dump(dbgs()););
541 std::swap(Src, Dst);
542 for (unsigned Level = 1; Level <= Levels; ++Level) {
543 unsigned char Direction = DV[Level - 1].Direction;
544 // Reverse the direction vector, this means LT becomes GT
545 // and GT becomes LT.
546 unsigned char RevDirection = Direction & Dependence::DVEntry::EQ;
547 if (Direction & Dependence::DVEntry::LT)
548 RevDirection |= Dependence::DVEntry::GT;
549 if (Direction & Dependence::DVEntry::GT)
550 RevDirection |= Dependence::DVEntry::LT;
551 DV[Level - 1].Direction = RevDirection;
552 // Reverse the dependence distance as well.
553 if (DV[Level - 1].Distance != nullptr)
554 DV[Level - 1].Distance = SE->getNegativeSCEV(DV[Level - 1].Distance);
555 }
556
557 LLVM_DEBUG(dbgs() << "After normalizing negative direction vectors:\n";
558 dump(dbgs()););
559 return true;
560}
561
562// The rest are simple getters that hide the implementation.
563
564// getDirection - Returns the direction associated with a particular common or
565// SameSD level.
566unsigned FullDependence::getDirection(unsigned Level, bool IsSameSD) const {
567 return getDVEntry(Level, IsSameSD).Direction;
568}
569
570// Returns the distance (or NULL) associated with a particular common or
571// SameSD level.
572const SCEV *FullDependence::getDistance(unsigned Level, bool IsSameSD) const {
573 return getDVEntry(Level, IsSameSD).Distance;
574}
575
576// Returns true if a particular regular or SameSD level is scalar; that is,
577// if no subscript in the source or destination mention the induction variable
578// associated with the loop at this level.
579bool FullDependence::isScalar(unsigned Level, bool IsSameSD) const {
580 return getDVEntry(Level, IsSameSD).Scalar;
581}
582
583// Returns true if peeling the first iteration from this regular or SameSD
584// loop level will break this dependence.
585bool FullDependence::isPeelFirst(unsigned Level, bool IsSameSD) const {
586 return getDVEntry(Level, IsSameSD).PeelFirst;
587}
588
589// Returns true if peeling the last iteration from this regular or SameSD
590// loop level will break this dependence.
591bool FullDependence::isPeelLast(unsigned Level, bool IsSameSD) const {
592 return getDVEntry(Level, IsSameSD).PeelLast;
593}
594
595// inSameSDLoops - Returns true if this level is an SameSD level, i.e.,
596// performed across two separate loop nests that have the Same iteration space
597// and Depth.
598bool FullDependence::inSameSDLoops(unsigned Level) const {
599 assert(0 < Level && Level <= static_cast<unsigned>(Levels) + SameSDLevels &&
600 "Level out of range");
601 return Level > Levels;
602}
603
604//===----------------------------------------------------------------------===//
605// SCEVMonotonicity
606
607SCEVMonotonicity::SCEVMonotonicity(SCEVMonotonicityType Type,
608 const SCEV *FailurePoint)
609 : Type(Type), FailurePoint(FailurePoint) {
610 assert(
611 ((Type == SCEVMonotonicityType::Unknown) == (FailurePoint != nullptr)) &&
612 "FailurePoint must be provided iff Type is Unknown");
613}
614
615void SCEVMonotonicity::print(raw_ostream &OS, unsigned Depth) const {
616 OS.indent(Depth) << "Monotonicity: ";
617 switch (Type) {
618 case SCEVMonotonicityType::Unknown:
619 assert(FailurePoint && "FailurePoint must be provided for Unknown");
620 OS << "Unknown\n";
621 OS.indent(Depth) << "Reason: " << *FailurePoint << "\n";
622 break;
623 case SCEVMonotonicityType::Invariant:
624 OS << "Invariant\n";
625 break;
626 case SCEVMonotonicityType::MultivariateSignedMonotonic:
627 OS << "MultivariateSignedMonotonic\n";
628 break;
629 }
630}
631
632bool SCEVMonotonicityChecker::isLoopInvariant(const SCEV *Expr) const {
633 return !OutermostLoop || SE->isLoopInvariant(Expr, OutermostLoop);
634}
635
636SCEVMonotonicity SCEVMonotonicityChecker::invariantOrUnknown(const SCEV *Expr) {
637 if (isLoopInvariant(Expr))
638 return SCEVMonotonicity(SCEVMonotonicityType::Invariant);
639 return createUnknown(Expr);
640}
641
642SCEVMonotonicity
643SCEVMonotonicityChecker::checkMonotonicity(const SCEV *Expr,
644 const Loop *OutermostLoop) {
645 assert((!OutermostLoop || OutermostLoop->isOutermost()) &&
646 "OutermostLoop must be outermost");
647 assert(Expr->getType()->isIntegerTy() && "Expr must be integer type");
648 this->OutermostLoop = OutermostLoop;
649 return visit(Expr);
650}
651
652/// We only care about an affine AddRec at the moment. For an affine AddRec,
653/// the monotonicity can be inferred from its nowrap property. For example, let
654/// X and Y be loop-invariant, and assume Y is non-negative. An AddRec
655/// {X,+.Y}<nsw> implies:
656///
657/// X <=s (X + Y) <=s ((X + Y) + Y) <=s ...
658///
659/// Thus, we can conclude that the AddRec is monotonically increasing with
660/// respect to the associated loop in a signed sense. The similar reasoning
661/// applies when Y is non-positive, leading to a monotonically decreasing
662/// AddRec.
663SCEVMonotonicity
664SCEVMonotonicityChecker::visitAddRecExpr(const SCEVAddRecExpr *Expr) {
665 if (!Expr->isAffine() || !Expr->hasNoSignedWrap())
666 return createUnknown(Expr);
667
668 const SCEV *Start = Expr->getStart();
669 const SCEV *Step = Expr->getStepRecurrence(*SE);
670
671 SCEVMonotonicity StartMon = visit(Start);
672 if (StartMon.isUnknown())
673 return StartMon;
674
675 if (!isLoopInvariant(Step))
676 return createUnknown(Expr);
677
678 return SCEVMonotonicity(SCEVMonotonicityType::MultivariateSignedMonotonic);
679}
680
681//===----------------------------------------------------------------------===//
682// DependenceInfo methods
683
684// For debugging purposes. Dumps a dependence to OS.
686 if (isConfused())
687 OS << "confused";
688 else {
689 if (isConsistent())
690 OS << "consistent ";
691 if (isFlow())
692 OS << "flow";
693 else if (isOutput())
694 OS << "output";
695 else if (isAnti())
696 OS << "anti";
697 else if (isInput())
698 OS << "input";
699 dumpImp(OS);
700 unsigned SameSDLevels = getSameSDLevels();
701 if (SameSDLevels > 0) {
702 OS << " / assuming " << SameSDLevels << " loop level(s) fused: ";
703 dumpImp(OS, true);
704 }
705 }
706 OS << "!\n";
707
709 if (!Assumptions.isAlwaysTrue()) {
710 OS << " Runtime Assumptions:\n";
711 Assumptions.print(OS, 2);
712 }
713}
714
715// For debugging purposes. Dumps a dependence to OS with or without considering
716// the SameSD levels.
717void Dependence::dumpImp(raw_ostream &OS, bool IsSameSD) const {
718 unsigned Levels = getLevels();
719 unsigned SameSDLevels = getSameSDLevels();
720 bool OnSameSD = false;
721 unsigned LevelNum = Levels;
722 if (IsSameSD)
723 LevelNum += SameSDLevels;
724 OS << " [";
725 for (unsigned II = 1; II <= LevelNum; ++II) {
726 if (!OnSameSD && inSameSDLoops(II))
727 OnSameSD = true;
728 if (isPeelFirst(II, OnSameSD))
729 OS << 'p';
730 const SCEV *Distance = getDistance(II, OnSameSD);
731 if (Distance)
732 OS << *Distance;
733 else if (isScalar(II, OnSameSD))
734 OS << "S";
735 else {
736 unsigned Direction = getDirection(II, OnSameSD);
737 if (Direction == DVEntry::ALL)
738 OS << "*";
739 else {
740 if (Direction & DVEntry::LT)
741 OS << "<";
742 if (Direction & DVEntry::EQ)
743 OS << "=";
744 if (Direction & DVEntry::GT)
745 OS << ">";
746 }
747 }
748 if (isPeelLast(II, OnSameSD))
749 OS << 'p';
750 if (II < LevelNum)
751 OS << " ";
752 }
753 if (isLoopIndependent())
754 OS << "|<";
755 OS << "]";
756}
757
758// Returns NoAlias/MayAliass/MustAlias for two memory locations based upon their
759// underlaying objects. If LocA and LocB are known to not alias (for any reason:
760// tbaa, non-overlapping regions etc), then it is known there is no dependecy.
761// Otherwise the underlying objects are checked to see if they point to
762// different identifiable objects.
764 const MemoryLocation &LocA,
765 const MemoryLocation &LocB) {
766 // Check the original locations (minus size) for noalias, which can happen for
767 // tbaa, incompatible underlying object locations, etc.
768 MemoryLocation LocAS =
770 MemoryLocation LocBS =
772 BatchAAResults BAA(*AA);
774
775 if (BAA.isNoAlias(LocAS, LocBS))
777
778 // Check the underlying objects are the same
779 const Value *AObj = getUnderlyingObject(LocA.Ptr);
780 const Value *BObj = getUnderlyingObject(LocB.Ptr);
781
782 // If the underlying objects are the same, they must alias
783 if (AObj == BObj)
785
786 // We may have hit the recursion limit for underlying objects, or have
787 // underlying objects where we don't know they will alias.
788 if (!isIdentifiedObject(AObj) || !isIdentifiedObject(BObj))
790
791 // Otherwise we know the objects are different and both identified objects so
792 // must not alias.
794}
795
796// Returns true if the load or store can be analyzed. Atomic and volatile
797// operations have properties which this analysis does not understand.
798static bool isLoadOrStore(const Instruction *I) {
799 if (const LoadInst *LI = dyn_cast<LoadInst>(I))
800 return LI->isUnordered();
801 else if (const StoreInst *SI = dyn_cast<StoreInst>(I))
802 return SI->isUnordered();
803 return false;
804}
805
806// Returns true if two loops have the Same iteration Space and Depth. To be
807// more specific, two loops have SameSD if they are in the same nesting
808// depth and have the same backedge count. SameSD stands for Same iteration
809// Space and Depth.
810bool DependenceInfo::haveSameSD(const Loop *SrcLoop,
811 const Loop *DstLoop) const {
812 if (SrcLoop == DstLoop)
813 return true;
814
815 if (SrcLoop->getLoopDepth() != DstLoop->getLoopDepth())
816 return false;
817
818 if (!SrcLoop || !SrcLoop->getLoopLatch() || !DstLoop ||
819 !DstLoop->getLoopLatch())
820 return false;
821
822 const SCEV *SrcUB = nullptr, *DstUP = nullptr;
823 if (SE->hasLoopInvariantBackedgeTakenCount(SrcLoop))
824 SrcUB = SE->getBackedgeTakenCount(SrcLoop);
825 if (SE->hasLoopInvariantBackedgeTakenCount(DstLoop))
826 DstUP = SE->getBackedgeTakenCount(DstLoop);
827
828 if (SrcUB != nullptr && DstUP != nullptr) {
829 Type *WiderType = SE->getWiderType(SrcUB->getType(), DstUP->getType());
830 SrcUB = SE->getNoopOrZeroExtend(SrcUB, WiderType);
831 DstUP = SE->getNoopOrZeroExtend(DstUP, WiderType);
832
833 if (SE->isKnownPredicate(ICmpInst::ICMP_EQ, SrcUB, DstUP))
834 return true;
835 }
836
837 return false;
838}
839
840// Examines the loop nesting of the Src and Dst
841// instructions and establishes their shared loops. Sets the variables
842// CommonLevels, SrcLevels, and MaxLevels.
843// The source and destination instructions needn't be contained in the same
844// loop. The routine establishNestingLevels finds the level of most deeply
845// nested loop that contains them both, CommonLevels. An instruction that's
846// not contained in a loop is at level = 0. MaxLevels is equal to the level
847// of the source plus the level of the destination, minus CommonLevels.
848// This lets us allocate vectors MaxLevels in length, with room for every
849// distinct loop referenced in both the source and destination subscripts.
850// The variable SrcLevels is the nesting depth of the source instruction.
851// It's used to help calculate distinct loops referenced by the destination.
852// Here's the map from loops to levels:
853// 0 - unused
854// 1 - outermost common loop
855// ... - other common loops
856// CommonLevels - innermost common loop
857// ... - loops containing Src but not Dst
858// SrcLevels - innermost loop containing Src but not Dst
859// ... - loops containing Dst but not Src
860// MaxLevels - innermost loops containing Dst but not Src
861// Consider the follow code fragment:
862// for (a = ...) {
863// for (b = ...) {
864// for (c = ...) {
865// for (d = ...) {
866// A[] = ...;
867// }
868// }
869// for (e = ...) {
870// for (f = ...) {
871// for (g = ...) {
872// ... = A[];
873// }
874// }
875// }
876// }
877// }
878// If we're looking at the possibility of a dependence between the store
879// to A (the Src) and the load from A (the Dst), we'll note that they
880// have 2 loops in common, so CommonLevels will equal 2 and the direction
881// vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7.
882// A map from loop names to loop numbers would look like
883// a - 1
884// b - 2 = CommonLevels
885// c - 3
886// d - 4 = SrcLevels
887// e - 5
888// f - 6
889// g - 7 = MaxLevels
890// SameSDLevels counts the number of levels after common levels that are
891// not common but have the same iteration space and depth. Internally this
892// is checked using haveSameSD. Assume that in this code fragment, levels c and
893// e have the same iteration space and depth, but levels d and f does not. Then
894// SameSDLevels is set to 1. In that case the level numbers for the previous
895// code look like
896// a - 1
897// b - 2
898// c,e - 3 = CommonLevels
899// d - 4 = SrcLevels
900// f - 5
901// g - 6 = MaxLevels
902void DependenceInfo::establishNestingLevels(const Instruction *Src,
903 const Instruction *Dst) {
904 const BasicBlock *SrcBlock = Src->getParent();
905 const BasicBlock *DstBlock = Dst->getParent();
906 unsigned SrcLevel = LI->getLoopDepth(SrcBlock);
907 unsigned DstLevel = LI->getLoopDepth(DstBlock);
908 const Loop *SrcLoop = LI->getLoopFor(SrcBlock);
909 const Loop *DstLoop = LI->getLoopFor(DstBlock);
910 SrcLevels = SrcLevel;
911 MaxLevels = SrcLevel + DstLevel;
912 SameSDLevels = 0;
913 while (SrcLevel > DstLevel) {
914 SrcLoop = SrcLoop->getParentLoop();
915 SrcLevel--;
916 }
917 while (DstLevel > SrcLevel) {
918 DstLoop = DstLoop->getParentLoop();
919 DstLevel--;
920 }
921
922 // find the first common level and count the SameSD levels leading to it
923 while (SrcLoop != DstLoop) {
924 SameSDLevels++;
925 if (!haveSameSD(SrcLoop, DstLoop))
926 SameSDLevels = 0;
927 SrcLoop = SrcLoop->getParentLoop();
928 DstLoop = DstLoop->getParentLoop();
929 SrcLevel--;
930 }
931 CommonLevels = SrcLevel;
932 MaxLevels -= CommonLevels;
933}
934
935// Given one of the loops containing the source, return
936// its level index in our numbering scheme.
937unsigned DependenceInfo::mapSrcLoop(const Loop *SrcLoop) const {
938 return SrcLoop->getLoopDepth();
939}
940
941// Given one of the loops containing the destination,
942// return its level index in our numbering scheme.
943unsigned DependenceInfo::mapDstLoop(const Loop *DstLoop) const {
944 unsigned D = DstLoop->getLoopDepth();
945 if (D > CommonLevels)
946 // This tries to make sure that we assign unique numbers to src and dst when
947 // the memory accesses reside in different loops that have the same depth.
948 return D - CommonLevels + SrcLevels;
949 else
950 return D;
951}
952
953// Returns true if Expression is loop invariant in LoopNest.
954bool DependenceInfo::isLoopInvariant(const SCEV *Expression,
955 const Loop *LoopNest) const {
956 // Unlike ScalarEvolution::isLoopInvariant() we consider an access outside of
957 // any loop as invariant, because we only consier expression evaluation at a
958 // specific position (where the array access takes place), and not across the
959 // entire function.
960 if (!LoopNest)
961 return true;
962
963 // If the expression is invariant in the outermost loop of the loop nest, it
964 // is invariant anywhere in the loop nest.
965 return SE->isLoopInvariant(Expression, LoopNest->getOutermostLoop());
966}
967
968// Finds the set of loops from the LoopNest that
969// have a level <= CommonLevels and are referred to by the SCEV Expression.
970void DependenceInfo::collectCommonLoops(const SCEV *Expression,
971 const Loop *LoopNest,
972 SmallBitVector &Loops) const {
973 while (LoopNest) {
974 unsigned Level = LoopNest->getLoopDepth();
975 if (Level <= CommonLevels && !SE->isLoopInvariant(Expression, LoopNest))
976 Loops.set(Level);
977 LoopNest = LoopNest->getParentLoop();
978 }
979}
980
981void DependenceInfo::unifySubscriptType(ArrayRef<Subscript *> Pairs) {
982
983 unsigned widestWidthSeen = 0;
984 Type *widestType;
985
986 // Go through each pair and find the widest bit to which we need
987 // to extend all of them.
988 for (Subscript *Pair : Pairs) {
989 const SCEV *Src = Pair->Src;
990 const SCEV *Dst = Pair->Dst;
991 IntegerType *SrcTy = dyn_cast<IntegerType>(Src->getType());
992 IntegerType *DstTy = dyn_cast<IntegerType>(Dst->getType());
993 if (SrcTy == nullptr || DstTy == nullptr) {
994 assert(SrcTy == DstTy &&
995 "This function only unify integer types and "
996 "expect Src and Dst share the same type otherwise.");
997 continue;
998 }
999 if (SrcTy->getBitWidth() > widestWidthSeen) {
1000 widestWidthSeen = SrcTy->getBitWidth();
1001 widestType = SrcTy;
1002 }
1003 if (DstTy->getBitWidth() > widestWidthSeen) {
1004 widestWidthSeen = DstTy->getBitWidth();
1005 widestType = DstTy;
1006 }
1007 }
1008
1009 assert(widestWidthSeen > 0);
1010
1011 // Now extend each pair to the widest seen.
1012 for (Subscript *Pair : Pairs) {
1013 const SCEV *Src = Pair->Src;
1014 const SCEV *Dst = Pair->Dst;
1015 IntegerType *SrcTy = dyn_cast<IntegerType>(Src->getType());
1016 IntegerType *DstTy = dyn_cast<IntegerType>(Dst->getType());
1017 if (SrcTy == nullptr || DstTy == nullptr) {
1018 assert(SrcTy == DstTy &&
1019 "This function only unify integer types and "
1020 "expect Src and Dst share the same type otherwise.");
1021 continue;
1022 }
1023 if (SrcTy->getBitWidth() < widestWidthSeen)
1024 // Sign-extend Src to widestType
1025 Pair->Src = SE->getSignExtendExpr(Src, widestType);
1026 if (DstTy->getBitWidth() < widestWidthSeen) {
1027 // Sign-extend Dst to widestType
1028 Pair->Dst = SE->getSignExtendExpr(Dst, widestType);
1029 }
1030 }
1031}
1032
1033// removeMatchingExtensions - Examines a subscript pair.
1034// If the source and destination are identically sign (or zero)
1035// extended, it strips off the extension in an effect to simplify
1036// the actual analysis.
1037void DependenceInfo::removeMatchingExtensions(Subscript *Pair) {
1038 const SCEV *Src = Pair->Src;
1039 const SCEV *Dst = Pair->Dst;
1042 const SCEVIntegralCastExpr *SrcCast = cast<SCEVIntegralCastExpr>(Src);
1043 const SCEVIntegralCastExpr *DstCast = cast<SCEVIntegralCastExpr>(Dst);
1044 const SCEV *SrcCastOp = SrcCast->getOperand();
1045 const SCEV *DstCastOp = DstCast->getOperand();
1046 if (SrcCastOp->getType() == DstCastOp->getType()) {
1047 Pair->Src = SrcCastOp;
1048 Pair->Dst = DstCastOp;
1049 }
1050 }
1051}
1052
1053// Examine the scev and return true iff it's affine.
1054// Collect any loops mentioned in the set of "Loops".
1055bool DependenceInfo::checkSubscript(const SCEV *Expr, const Loop *LoopNest,
1056 SmallBitVector &Loops, bool IsSrc) {
1057 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
1058 if (!AddRec)
1059 return isLoopInvariant(Expr, LoopNest);
1060
1061 // The AddRec must depend on one of the containing loops. Otherwise,
1062 // mapSrcLoop and mapDstLoop return indices outside the intended range. This
1063 // can happen when a subscript in one loop references an IV from a sibling
1064 // loop that could not be replaced with a concrete exit value by
1065 // getSCEVAtScope.
1066 const Loop *L = LoopNest;
1067 while (L && AddRec->getLoop() != L)
1068 L = L->getParentLoop();
1069 if (!L)
1070 return false;
1071
1072 const SCEV *Start = AddRec->getStart();
1073 const SCEV *Step = AddRec->getStepRecurrence(*SE);
1074 if (!isLoopInvariant(Step, LoopNest))
1075 return false;
1076 if (IsSrc)
1077 Loops.set(mapSrcLoop(AddRec->getLoop()));
1078 else
1079 Loops.set(mapDstLoop(AddRec->getLoop()));
1080 return checkSubscript(Start, LoopNest, Loops, IsSrc);
1081}
1082
1083// Examine the scev and return true iff it's linear.
1084// Collect any loops mentioned in the set of "Loops".
1085bool DependenceInfo::checkSrcSubscript(const SCEV *Src, const Loop *LoopNest,
1087 return checkSubscript(Src, LoopNest, Loops, true);
1088}
1089
1090// Examine the scev and return true iff it's linear.
1091// Collect any loops mentioned in the set of "Loops".
1092bool DependenceInfo::checkDstSubscript(const SCEV *Dst, const Loop *LoopNest,
1094 return checkSubscript(Dst, LoopNest, Loops, false);
1095}
1096
1097// Examines the subscript pair (the Src and Dst SCEVs)
1098// and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear.
1099// Collects the associated loops in a set.
1100DependenceInfo::Subscript::ClassificationKind
1101DependenceInfo::classifyPair(const SCEV *Src, const Loop *SrcLoopNest,
1102 const SCEV *Dst, const Loop *DstLoopNest,
1104 SmallBitVector SrcLoops(MaxLevels + 1);
1105 SmallBitVector DstLoops(MaxLevels + 1);
1106 if (!checkSrcSubscript(Src, SrcLoopNest, SrcLoops))
1107 return Subscript::NonLinear;
1108 if (!checkDstSubscript(Dst, DstLoopNest, DstLoops))
1109 return Subscript::NonLinear;
1110 Loops = SrcLoops;
1111 Loops |= DstLoops;
1112 unsigned N = Loops.count();
1113 if (N == 0)
1114 return Subscript::ZIV;
1115 if (N == 1)
1116 return Subscript::SIV;
1117 if (N == 2 && (SrcLoops.count() == 0 || DstLoops.count() == 0 ||
1118 (SrcLoops.count() == 1 && DstLoops.count() == 1)))
1119 return Subscript::RDIV;
1120 return Subscript::MIV;
1121}
1122
1123// A wrapper around SCEV::isKnownPredicate.
1124// Looks for cases where we're interested in comparing for equality.
1125// If both X and Y have been identically sign or zero extended,
1126// it strips off the (confusing) extensions before invoking
1127// SCEV::isKnownPredicate. Perhaps, someday, the ScalarEvolution package
1128// will be similarly updated.
1129//
1130// If SCEV::isKnownPredicate can't prove the predicate,
1131// we try simple subtraction, which seems to help in some cases
1132// involving symbolics.
1133bool DependenceInfo::isKnownPredicate(ICmpInst::Predicate Pred, const SCEV *X,
1134 const SCEV *Y) const {
1135 if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) {
1138 const SCEVIntegralCastExpr *CX = cast<SCEVIntegralCastExpr>(X);
1139 const SCEVIntegralCastExpr *CY = cast<SCEVIntegralCastExpr>(Y);
1140 const SCEV *Xop = CX->getOperand();
1141 const SCEV *Yop = CY->getOperand();
1142 if (Xop->getType() == Yop->getType()) {
1143 X = Xop;
1144 Y = Yop;
1145 }
1146 }
1147 }
1148 if (SE->isKnownPredicate(Pred, X, Y))
1149 return true;
1150 // If SE->isKnownPredicate can't prove the condition,
1151 // we try the brute-force approach of subtracting
1152 // and testing the difference.
1153 // By testing with SE->isKnownPredicate first, we avoid
1154 // the possibility of overflow when the arguments are constants.
1155 const SCEV *Delta = SE->getMinusSCEV(X, Y);
1156 switch (Pred) {
1157 case CmpInst::ICMP_EQ:
1158 return Delta->isZero();
1159 case CmpInst::ICMP_NE:
1160 return SE->isKnownNonZero(Delta);
1161 case CmpInst::ICMP_SGE:
1162 return SE->isKnownNonNegative(Delta);
1163 case CmpInst::ICMP_SLE:
1164 return SE->isKnownNonPositive(Delta);
1165 case CmpInst::ICMP_SGT:
1166 return SE->isKnownPositive(Delta);
1167 case CmpInst::ICMP_SLT:
1168 return SE->isKnownNegative(Delta);
1169 default:
1170 llvm_unreachable("unexpected predicate in isKnownPredicate");
1171 }
1172}
1173
1174// All subscripts are all the same type.
1175// Loop bound may be smaller (e.g., a char).
1176// Should zero extend loop bound, since it's always >= 0.
1177// This routine collects upper bound and extends or truncates if needed.
1178// Truncating is safe when subscripts are known not to wrap. Cases without
1179// nowrap flags should have been rejected earlier.
1180// Return null if no bound available.
1181const SCEV *DependenceInfo::collectUpperBound(const Loop *L, Type *T) const {
1182 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
1183 const SCEV *UB = SE->getBackedgeTakenCount(L);
1184 return SE->getTruncateOrZeroExtend(UB, T);
1185 }
1186 return nullptr;
1187}
1188
1189// Calls collectUpperBound(), then attempts to cast it to SCEVConstant.
1190// If the cast fails, returns NULL.
1191const SCEVConstant *DependenceInfo::collectConstantUpperBound(const Loop *L,
1192 Type *T) const {
1193 if (const SCEV *UB = collectUpperBound(L, T))
1194 return dyn_cast<SCEVConstant>(UB);
1195 return nullptr;
1196}
1197
1198/// Returns \p A - \p B if it guaranteed not to signed wrap. Otherwise returns
1199/// nullptr. \p A and \p B must have the same integer type.
1200static const SCEV *minusSCEVNoSignedOverflow(const SCEV *A, const SCEV *B,
1201 ScalarEvolution &SE) {
1202 if (SE.willNotOverflow(Instruction::Sub, /*Signed=*/true, A, B))
1203 return SE.getMinusSCEV(A, B);
1204 return nullptr;
1205}
1206
1207/// Returns \p A * \p B if it guaranteed not to signed wrap. Otherwise returns
1208/// nullptr. \p A and \p B must have the same integer type.
1209static const SCEV *mulSCEVNoSignedOverflow(const SCEV *A, const SCEV *B,
1210 ScalarEvolution &SE) {
1211 if (SE.willNotOverflow(Instruction::Mul, /*Signed=*/true, A, B))
1212 return SE.getMulExpr(A, B);
1213 return nullptr;
1214}
1215
1216/// Returns the absolute value of \p A. In the context of dependence analysis,
1217/// we need an absolute value in a mathematical sense. If \p A is the signed
1218/// minimum value, we cannot represent it unless extending the original type.
1219/// Thus if we cannot prove that \p A is not the signed minimum value, returns
1220/// nullptr.
1222 IntegerType *Ty = cast<IntegerType>(A->getType());
1223 if (!Ty)
1224 return nullptr;
1225
1226 const SCEV *SMin =
1227 SE.getConstant(APInt::getSignedMinValue(Ty->getBitWidth()));
1229 return nullptr;
1230 return SE.getAbsExpr(A, /*IsNSW=*/true);
1231}
1232
1233/// Returns true iff \p Test is enabled.
1234static bool isDependenceTestEnabled(DependenceTestType Test) {
1235 if (EnableDependenceTest == DependenceTestType::All)
1236 return true;
1237 return EnableDependenceTest == Test;
1238}
1239
1240// testZIV -
1241// When we have a pair of subscripts of the form [c1] and [c2],
1242// where c1 and c2 are both loop invariant, we attack it using
1243// the ZIV test. Basically, we test by comparing the two values,
1244// but there are actually three possible results:
1245// 1) the values are equal, so there's a dependence
1246// 2) the values are different, so there's no dependence
1247// 3) the values might be equal, so we have to assume a dependence.
1248//
1249// Return true if dependence disproved.
1250bool DependenceInfo::testZIV(const SCEV *Src, const SCEV *Dst,
1251 FullDependence &Result) const {
1252 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
1253 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
1254 ++ZIVapplications;
1255 if (isKnownPredicate(CmpInst::ICMP_EQ, Src, Dst)) {
1256 LLVM_DEBUG(dbgs() << " provably dependent\n");
1257 return false; // provably dependent
1258 }
1259 if (isKnownPredicate(CmpInst::ICMP_NE, Src, Dst)) {
1260 LLVM_DEBUG(dbgs() << " provably independent\n");
1261 ++ZIVindependence;
1262 return true; // provably independent
1263 }
1264 LLVM_DEBUG(dbgs() << " possibly dependent\n");
1265 Result.Consistent = false;
1266 return false; // possibly dependent
1267}
1268
1269// strongSIVtest -
1270// From the paper, Practical Dependence Testing, Section 4.2.1
1271//
1272// When we have a pair of subscripts of the form [c1 + a*i] and [c2 + a*i],
1273// where i is an induction variable, c1 and c2 are loop invariant,
1274// and a is a constant, we can solve it exactly using the Strong SIV test.
1275//
1276// Can prove independence. Failing that, can compute distance (and direction).
1277// In the presence of symbolic terms, we can sometimes make progress.
1278//
1279// If there's a dependence,
1280//
1281// c1 + a*i = c2 + a*i'
1282//
1283// The dependence distance is
1284//
1285// d = i' - i = (c1 - c2)/a
1286//
1287// A dependence only exists if d is an integer and abs(d) <= U, where U is the
1288// loop's upper bound. If a dependence exists, the dependence direction is
1289// defined as
1290//
1291// { < if d > 0
1292// direction = { = if d = 0
1293// { > if d < 0
1294//
1295// Return true if dependence disproved.
1296bool DependenceInfo::strongSIVtest(const SCEV *Coeff, const SCEV *SrcConst,
1297 const SCEV *DstConst, const Loop *CurSrcLoop,
1298 const Loop *CurDstLoop, unsigned Level,
1299 FullDependence &Result,
1300 bool UnderRuntimeAssumptions) {
1301 if (!isDependenceTestEnabled(DependenceTestType::StrongSIV))
1302 return false;
1303
1304 LLVM_DEBUG(dbgs() << "\tStrong SIV test\n");
1305 LLVM_DEBUG(dbgs() << "\t Coeff = " << *Coeff);
1306 LLVM_DEBUG(dbgs() << ", " << *Coeff->getType() << "\n");
1307 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst);
1308 LLVM_DEBUG(dbgs() << ", " << *SrcConst->getType() << "\n");
1309 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst);
1310 LLVM_DEBUG(dbgs() << ", " << *DstConst->getType() << "\n");
1311 ++StrongSIVapplications;
1312 assert(0 < Level && Level <= CommonLevels && "level out of range");
1313 Level--;
1314
1315 const SCEV *Delta = minusSCEVNoSignedOverflow(SrcConst, DstConst, *SE);
1316 if (!Delta) {
1317 Result.Consistent = false;
1318 return false;
1319 }
1320 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta);
1321 LLVM_DEBUG(dbgs() << ", " << *Delta->getType() << "\n");
1322
1323 // check that |Delta| < iteration count
1324 bool IsDeltaLarge = [&] {
1325 const SCEV *UpperBound = collectUpperBound(CurSrcLoop, Delta->getType());
1326 if (!UpperBound)
1327 return false;
1328
1329 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound);
1330 LLVM_DEBUG(dbgs() << ", " << *UpperBound->getType() << "\n");
1331 const SCEV *AbsDelta = absSCEVNoSignedOverflow(Delta, *SE);
1332 const SCEV *AbsCoeff = absSCEVNoSignedOverflow(Coeff, *SE);
1333 if (!AbsDelta || !AbsCoeff)
1334 return false;
1335 const SCEV *Product = mulSCEVNoSignedOverflow(UpperBound, AbsCoeff, *SE);
1336 if (!Product)
1337 return false;
1338 return isKnownPredicate(CmpInst::ICMP_SGT, AbsDelta, Product);
1339 }();
1340 if (IsDeltaLarge) {
1341 // Distance greater than trip count - no dependence
1342 ++StrongSIVindependence;
1343 ++StrongSIVsuccesses;
1344 return true;
1345 }
1346
1347 // Can we compute distance?
1348 if (isa<SCEVConstant>(Delta) && isa<SCEVConstant>(Coeff)) {
1349 APInt ConstDelta = cast<SCEVConstant>(Delta)->getAPInt();
1350 APInt ConstCoeff = cast<SCEVConstant>(Coeff)->getAPInt();
1351 APInt Distance = ConstDelta; // these need to be initialized
1352 APInt Remainder = ConstDelta;
1353 APInt::sdivrem(ConstDelta, ConstCoeff, Distance, Remainder);
1354 LLVM_DEBUG(dbgs() << "\t Distance = " << Distance << "\n");
1355 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
1356 // Make sure Coeff divides Delta exactly
1357 if (Remainder != 0) {
1358 // Coeff doesn't divide Distance, no dependence
1359 ++StrongSIVindependence;
1360 ++StrongSIVsuccesses;
1361 return true;
1362 }
1363 Result.DV[Level].Distance = SE->getConstant(Distance);
1364 if (Distance.sgt(0))
1365 Result.DV[Level].Direction &= Dependence::DVEntry::LT;
1366 else if (Distance.slt(0))
1367 Result.DV[Level].Direction &= Dependence::DVEntry::GT;
1368 else
1369 Result.DV[Level].Direction &= Dependence::DVEntry::EQ;
1370 ++StrongSIVsuccesses;
1371 } else if (Delta->isZero()) {
1372 // Check if coefficient could be zero. If so, 0/0 is undefined and we
1373 // cannot conclude that only same-iteration dependencies exist.
1374 // When coeff=0, all iterations access the same location.
1375 if (SE->isKnownNonZero(Coeff)) {
1376 LLVM_DEBUG(
1377 dbgs() << "\t Coefficient proven non-zero by SCEV analysis\n");
1378 } else {
1379 // Cannot prove at compile time, would need runtime assumption.
1380 if (UnderRuntimeAssumptions) {
1381 const SCEVPredicate *Pred = SE->getComparePredicate(
1382 ICmpInst::ICMP_NE, Coeff, SE->getZero(Coeff->getType()));
1383 Result.Assumptions = Result.Assumptions.getUnionWith(Pred, *SE);
1384 LLVM_DEBUG(dbgs() << "\t Added runtime assumption: " << *Coeff
1385 << " != 0\n");
1386 } else {
1387 // Cannot add runtime assumptions, this test cannot handle this case.
1388 // Let more complex tests try.
1389 LLVM_DEBUG(dbgs() << "\t Would need runtime assumption " << *Coeff
1390 << " != 0, but not allowed. Failing this test.\n");
1391 return false;
1392 }
1393 }
1394 // Since 0/X == 0 (where X is known non-zero or assumed non-zero).
1395 Result.DV[Level].Distance = Delta;
1396 Result.DV[Level].Direction &= Dependence::DVEntry::EQ;
1397 ++StrongSIVsuccesses;
1398 } else {
1399 if (Coeff->isOne()) {
1400 LLVM_DEBUG(dbgs() << "\t Distance = " << *Delta << "\n");
1401 Result.DV[Level].Distance = Delta; // since X/1 == X
1402 } else {
1403 Result.Consistent = false;
1404 }
1405
1406 // maybe we can get a useful direction
1407 bool DeltaMaybeZero = !SE->isKnownNonZero(Delta);
1408 bool DeltaMaybePositive = !SE->isKnownNonPositive(Delta);
1409 bool DeltaMaybeNegative = !SE->isKnownNonNegative(Delta);
1410 bool CoeffMaybePositive = !SE->isKnownNonPositive(Coeff);
1411 bool CoeffMaybeNegative = !SE->isKnownNonNegative(Coeff);
1412 // The double negatives above are confusing.
1413 // It helps to read !SE->isKnownNonZero(Delta)
1414 // as "Delta might be Zero"
1415 unsigned NewDirection = Dependence::DVEntry::NONE;
1416 if ((DeltaMaybePositive && CoeffMaybePositive) ||
1417 (DeltaMaybeNegative && CoeffMaybeNegative))
1418 NewDirection = Dependence::DVEntry::LT;
1419 if (DeltaMaybeZero)
1420 NewDirection |= Dependence::DVEntry::EQ;
1421 if ((DeltaMaybeNegative && CoeffMaybePositive) ||
1422 (DeltaMaybePositive && CoeffMaybeNegative))
1423 NewDirection |= Dependence::DVEntry::GT;
1424 if (NewDirection < Result.DV[Level].Direction)
1425 ++StrongSIVsuccesses;
1426 Result.DV[Level].Direction &= NewDirection;
1427 }
1428 return false;
1429}
1430
1431// weakCrossingSIVtest -
1432// From the paper, Practical Dependence Testing, Section 4.2.2
1433//
1434// When we have a pair of subscripts of the form [c1 + a*i] and [c2 - a*i],
1435// where i is an induction variable, c1 and c2 are loop invariant,
1436// and a is a constant, we can solve it exactly using the
1437// Weak-Crossing SIV test.
1438//
1439// Given c1 + a*i = c2 - a*i', we can look for the intersection of
1440// the two lines, where i = i', yielding
1441//
1442// c1 + a*i = c2 - a*i
1443// 2a*i = c2 - c1
1444// i = (c2 - c1)/2a
1445//
1446// If i < 0, there is no dependence.
1447// If i > upperbound, there is no dependence.
1448// If i = 0 (i.e., if c1 = c2), there's a dependence with distance = 0.
1449// If i = upperbound, there's a dependence with distance = 0.
1450// If i is integral, there's a dependence (all directions).
1451// If the non-integer part = 1/2, there's a dependence (<> directions).
1452// Otherwise, there's no dependence.
1453//
1454// Can prove independence. Failing that,
1455// can sometimes refine the directions.
1456// Can determine iteration for splitting.
1457//
1458// Return true if dependence disproved.
1459bool DependenceInfo::weakCrossingSIVtest(const SCEV *Coeff,
1460 const SCEV *SrcConst,
1461 const SCEV *DstConst,
1462 const Loop *CurSrcLoop,
1463 const Loop *CurDstLoop, unsigned Level,
1464 FullDependence &Result) const {
1465 if (!isDependenceTestEnabled(DependenceTestType::WeakCrossingSIV))
1466 return false;
1467
1468 LLVM_DEBUG(dbgs() << "\tWeak-Crossing SIV test\n");
1469 LLVM_DEBUG(dbgs() << "\t Coeff = " << *Coeff << "\n");
1470 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1471 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1472 ++WeakCrossingSIVapplications;
1473 assert(0 < Level && Level <= CommonLevels && "Level out of range");
1474 Level--;
1475 Result.Consistent = false;
1476 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1477 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1478 if (Delta->isZero()) {
1479 Result.DV[Level].Direction &= ~Dependence::DVEntry::LT;
1480 Result.DV[Level].Direction &= ~Dependence::DVEntry::GT;
1481 ++WeakCrossingSIVsuccesses;
1482 if (!Result.DV[Level].Direction) {
1483 ++WeakCrossingSIVindependence;
1484 return true;
1485 }
1486 Result.DV[Level].Distance = Delta; // = 0
1487 return false;
1488 }
1489 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(Coeff);
1490 if (!ConstCoeff)
1491 return false;
1492
1493 if (SE->isKnownNegative(ConstCoeff)) {
1494 ConstCoeff = dyn_cast<SCEVConstant>(SE->getNegativeSCEV(ConstCoeff));
1495 assert(ConstCoeff &&
1496 "dynamic cast of negative of ConstCoeff should yield constant");
1497 Delta = SE->getNegativeSCEV(Delta);
1498 }
1499 assert(SE->isKnownPositive(ConstCoeff) && "ConstCoeff should be positive");
1500
1501 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1502 if (!ConstDelta)
1503 return false;
1504
1505 // We're certain that ConstCoeff > 0; therefore,
1506 // if Delta < 0, then no dependence.
1507 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1508 LLVM_DEBUG(dbgs() << "\t ConstCoeff = " << *ConstCoeff << "\n");
1509 if (SE->isKnownNegative(Delta)) {
1510 // No dependence, Delta < 0
1511 ++WeakCrossingSIVindependence;
1512 ++WeakCrossingSIVsuccesses;
1513 return true;
1514 }
1515
1516 // We're certain that Delta > 0 and ConstCoeff > 0.
1517 // Check Delta/(2*ConstCoeff) against upper loop bound
1518 if (const SCEV *UpperBound =
1519 collectUpperBound(CurSrcLoop, Delta->getType())) {
1520 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
1521 const SCEV *ConstantTwo = SE->getConstant(UpperBound->getType(), 2);
1522 const SCEV *ML =
1523 SE->getMulExpr(SE->getMulExpr(ConstCoeff, UpperBound), ConstantTwo);
1524 LLVM_DEBUG(dbgs() << "\t ML = " << *ML << "\n");
1525 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, ML)) {
1526 // Delta too big, no dependence
1527 ++WeakCrossingSIVindependence;
1528 ++WeakCrossingSIVsuccesses;
1529 return true;
1530 }
1531 if (isKnownPredicate(CmpInst::ICMP_EQ, Delta, ML)) {
1532 // i = i' = UB
1533 Result.DV[Level].Direction &= ~Dependence::DVEntry::LT;
1534 Result.DV[Level].Direction &= ~Dependence::DVEntry::GT;
1535 ++WeakCrossingSIVsuccesses;
1536 if (!Result.DV[Level].Direction) {
1537 ++WeakCrossingSIVindependence;
1538 return true;
1539 }
1540 Result.DV[Level].Distance = SE->getZero(Delta->getType());
1541 return false;
1542 }
1543 }
1544
1545 // check that Coeff divides Delta
1546 APInt APDelta = ConstDelta->getAPInt();
1547 APInt APCoeff = ConstCoeff->getAPInt();
1548 APInt Distance = APDelta; // these need to be initialzed
1549 APInt Remainder = APDelta;
1550 APInt::sdivrem(APDelta, APCoeff, Distance, Remainder);
1551 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
1552 if (Remainder != 0) {
1553 // Coeff doesn't divide Delta, no dependence
1554 ++WeakCrossingSIVindependence;
1555 ++WeakCrossingSIVsuccesses;
1556 return true;
1557 }
1558 LLVM_DEBUG(dbgs() << "\t Distance = " << Distance << "\n");
1559
1560 // if 2*Coeff doesn't divide Delta, then the equal direction isn't possible
1561 APInt Two = APInt(Distance.getBitWidth(), 2, true);
1562 Remainder = Distance.srem(Two);
1563 LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
1564 if (Remainder != 0) {
1565 // Equal direction isn't possible
1566 Result.DV[Level].Direction &= ~Dependence::DVEntry::EQ;
1567 ++WeakCrossingSIVsuccesses;
1568 }
1569 return false;
1570}
1571
1572// Kirch's algorithm, from
1573//
1574// Optimizing Supercompilers for Supercomputers
1575// Michael Wolfe
1576// MIT Press, 1989
1577//
1578// Program 2.1, page 29.
1579// Computes the GCD of AM and BM.
1580// Also finds a solution to the equation ax - by = gcd(a, b).
1581// Returns true if dependence disproved; i.e., gcd does not divide Delta.
1582static bool findGCD(unsigned Bits, const APInt &AM, const APInt &BM,
1583 const APInt &Delta, APInt &G, APInt &X, APInt &Y) {
1584 APInt A0(Bits, 1, true), A1(Bits, 0, true);
1585 APInt B0(Bits, 0, true), B1(Bits, 1, true);
1586 APInt G0 = AM.abs();
1587 APInt G1 = BM.abs();
1588 APInt Q = G0; // these need to be initialized
1589 APInt R = G0;
1590 APInt::sdivrem(G0, G1, Q, R);
1591 while (R != 0) {
1592 // clang-format off
1593 APInt A2 = A0 - Q*A1; A0 = A1; A1 = A2;
1594 APInt B2 = B0 - Q*B1; B0 = B1; B1 = B2;
1595 G0 = G1; G1 = R;
1596 // clang-format on
1597 APInt::sdivrem(G0, G1, Q, R);
1598 }
1599 G = G1;
1600 LLVM_DEBUG(dbgs() << "\t GCD = " << G << "\n");
1601 X = AM.slt(0) ? -A1 : A1;
1602 Y = BM.slt(0) ? B1 : -B1;
1603
1604 // make sure gcd divides Delta
1605 R = Delta.srem(G);
1606 if (R != 0)
1607 return true; // gcd doesn't divide Delta, no dependence
1608 Q = Delta.sdiv(G);
1609 return false;
1610}
1611
1612static APInt floorOfQuotient(const APInt &A, const APInt &B) {
1613 APInt Q = A; // these need to be initialized
1614 APInt R = A;
1615 APInt::sdivrem(A, B, Q, R);
1616 if (R == 0)
1617 return Q;
1618 if ((A.sgt(0) && B.sgt(0)) || (A.slt(0) && B.slt(0)))
1619 return Q;
1620 else
1621 return Q - 1;
1622}
1623
1624static APInt ceilingOfQuotient(const APInt &A, const APInt &B) {
1625 APInt Q = A; // these need to be initialized
1626 APInt R = A;
1627 APInt::sdivrem(A, B, Q, R);
1628 if (R == 0)
1629 return Q;
1630 if ((A.sgt(0) && B.sgt(0)) || (A.slt(0) && B.slt(0)))
1631 return Q + 1;
1632 else
1633 return Q;
1634}
1635
1636/// Given an affine expression of the form A*k + B, where k is an arbitrary
1637/// integer, infer the possible range of k based on the known range of the
1638/// affine expression. If we know A*k + B is non-negative, i.e.,
1639///
1640/// A*k + B >= 0
1641///
1642/// we can derive the following inequalities for k when A is positive:
1643///
1644/// k >= -B / A
1645///
1646/// Since k is an integer, it means k is greater than or equal to the
1647/// ceil(-B / A).
1648///
1649/// If the upper bound of the affine expression \p UB is passed, the following
1650/// inequality can be derived as well:
1651///
1652/// A*k + B <= UB
1653///
1654/// which leads to:
1655///
1656/// k <= (UB - B) / A
1657///
1658/// Again, as k is an integer, it means k is less than or equal to the
1659/// floor((UB - B) / A).
1660///
1661/// The similar logic applies when A is negative, but the inequalities sign flip
1662/// while working with them.
1663///
1664/// Preconditions: \p A is non-zero, and we know A*k + B is non-negative.
1665static std::pair<std::optional<APInt>, std::optional<APInt>>
1667 const std::optional<APInt> &UB) {
1668 assert(A != 0 && "A must be non-zero");
1669 std::optional<APInt> TL, TU;
1670 if (A.sgt(0)) {
1671 TL = ceilingOfQuotient(-B, A);
1672 LLVM_DEBUG(dbgs() << "\t Possible TL = " << *TL << "\n");
1673 // New bound check - modification to Banerjee's e3 check
1674 if (UB) {
1675 // TODO?: Overflow check for UB - B
1676 TU = floorOfQuotient(*UB - B, A);
1677 LLVM_DEBUG(dbgs() << "\t Possible TU = " << *TU << "\n");
1678 }
1679 } else {
1680 TU = floorOfQuotient(-B, A);
1681 LLVM_DEBUG(dbgs() << "\t Possible TU = " << *TU << "\n");
1682 // New bound check - modification to Banerjee's e3 check
1683 if (UB) {
1684 // TODO?: Overflow check for UB - B
1685 TL = ceilingOfQuotient(*UB - B, A);
1686 LLVM_DEBUG(dbgs() << "\t Possible TL = " << *TL << "\n");
1687 }
1688 }
1689 return std::make_pair(TL, TU);
1690}
1691
1692// exactSIVtest -
1693// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*i],
1694// where i is an induction variable, c1 and c2 are loop invariant, and a1
1695// and a2 are constant, we can solve it exactly using an algorithm developed
1696// by Banerjee and Wolfe. See Algorithm 6.2.1 (case 2.5) in:
1697//
1698// Dependence Analysis for Supercomputing
1699// Utpal Banerjee
1700// Kluwer Academic Publishers, 1988
1701//
1702// It's slower than the specialized tests (strong SIV, weak-zero SIV, etc),
1703// so use them if possible. They're also a bit better with symbolics and,
1704// in the case of the strong SIV test, can compute Distances.
1705//
1706// Return true if dependence disproved.
1707//
1708// This is a modified version of the original Banerjee algorithm. The original
1709// only tested whether Dst depends on Src. This algorithm extends that and
1710// returns all the dependencies that exist between Dst and Src.
1711bool DependenceInfo::exactSIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
1712 const SCEV *SrcConst, const SCEV *DstConst,
1713 const Loop *CurSrcLoop,
1714 const Loop *CurDstLoop, unsigned Level,
1715 FullDependence &Result) const {
1716 if (!isDependenceTestEnabled(DependenceTestType::ExactSIV))
1717 return false;
1718
1719 LLVM_DEBUG(dbgs() << "\tExact SIV test\n");
1720 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n");
1721 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n");
1722 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1723 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1724 ++ExactSIVapplications;
1725 assert(0 < Level && Level <= CommonLevels && "Level out of range");
1726 Level--;
1727 Result.Consistent = false;
1728 const SCEV *Delta = minusSCEVNoSignedOverflow(DstConst, SrcConst, *SE);
1729 if (!Delta)
1730 return false;
1731 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1732 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
1733 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
1734 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1735 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
1736 return false;
1737
1738 // find gcd
1739 APInt G, X, Y;
1740 APInt AM = ConstSrcCoeff->getAPInt();
1741 APInt BM = ConstDstCoeff->getAPInt();
1742 APInt CM = ConstDelta->getAPInt();
1743 unsigned Bits = AM.getBitWidth();
1744 if (findGCD(Bits, AM, BM, CM, G, X, Y)) {
1745 // gcd doesn't divide Delta, no dependence
1746 ++ExactSIVindependence;
1747 ++ExactSIVsuccesses;
1748 return true;
1749 }
1750
1751 LLVM_DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n");
1752
1753 // since SCEV construction normalizes, LM = 0
1754 std::optional<APInt> UM;
1755 // UM is perhaps unavailable, let's check
1756 if (const SCEVConstant *CUB =
1757 collectConstantUpperBound(CurSrcLoop, Delta->getType())) {
1758 UM = CUB->getAPInt();
1759 LLVM_DEBUG(dbgs() << "\t UM = " << *UM << "\n");
1760 }
1761
1762 APInt TU(APInt::getSignedMaxValue(Bits));
1763 APInt TL(APInt::getSignedMinValue(Bits));
1764 APInt TC = CM.sdiv(G);
1765 APInt TX = X * TC;
1766 APInt TY = Y * TC;
1767 LLVM_DEBUG(dbgs() << "\t TC = " << TC << "\n");
1768 LLVM_DEBUG(dbgs() << "\t TX = " << TX << "\n");
1769 LLVM_DEBUG(dbgs() << "\t TY = " << TY << "\n");
1770
1771 APInt TB = BM.sdiv(G);
1772 APInt TA = AM.sdiv(G);
1773
1774 // At this point, we have the following equations:
1775 //
1776 // TA*i0 - TB*i1 = TC
1777 //
1778 // Also, we know that the all pairs of (i0, i1) can be expressed as:
1779 //
1780 // (TX + k*TB, TY + k*TA)
1781 //
1782 // where k is an arbitrary integer.
1783 auto [TL0, TU0] = inferDomainOfAffine(TB, TX, UM);
1784 auto [TL1, TU1] = inferDomainOfAffine(TA, TY, UM);
1785
1786 auto CreateVec = [](const std::optional<APInt> &V0,
1787 const std::optional<APInt> &V1) {
1789 if (V0)
1790 Vec.push_back(*V0);
1791 if (V1)
1792 Vec.push_back(*V1);
1793 return Vec;
1794 };
1795
1796 SmallVector<APInt, 2> TLVec = CreateVec(TL0, TL1);
1797 SmallVector<APInt, 2> TUVec = CreateVec(TU0, TU1);
1798
1799 LLVM_DEBUG(dbgs() << "\t TA = " << TA << "\n");
1800 LLVM_DEBUG(dbgs() << "\t TB = " << TB << "\n");
1801
1802 if (TLVec.empty() || TUVec.empty())
1803 return false;
1804 TL = APIntOps::smax(TLVec.front(), TLVec.back());
1805 TU = APIntOps::smin(TUVec.front(), TUVec.back());
1806 LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n");
1807 LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n");
1808
1809 if (TL.sgt(TU)) {
1810 ++ExactSIVindependence;
1811 ++ExactSIVsuccesses;
1812 return true;
1813 }
1814
1815 // explore directions
1816 unsigned NewDirection = Dependence::DVEntry::NONE;
1817 APInt LowerDistance, UpperDistance;
1818 // TODO: Overflow check may be needed.
1819 if (TA.sgt(TB)) {
1820 LowerDistance = (TY - TX) + (TA - TB) * TL;
1821 UpperDistance = (TY - TX) + (TA - TB) * TU;
1822 } else {
1823 LowerDistance = (TY - TX) + (TA - TB) * TU;
1824 UpperDistance = (TY - TX) + (TA - TB) * TL;
1825 }
1826
1827 LLVM_DEBUG(dbgs() << "\t LowerDistance = " << LowerDistance << "\n");
1828 LLVM_DEBUG(dbgs() << "\t UpperDistance = " << UpperDistance << "\n");
1829
1830 APInt Zero(Bits, 0, true);
1831 if (LowerDistance.sle(Zero) && UpperDistance.sge(Zero)) {
1832 NewDirection |= Dependence::DVEntry::EQ;
1833 ++ExactSIVsuccesses;
1834 }
1835 if (LowerDistance.slt(0)) {
1836 NewDirection |= Dependence::DVEntry::GT;
1837 ++ExactSIVsuccesses;
1838 }
1839 if (UpperDistance.sgt(0)) {
1840 NewDirection |= Dependence::DVEntry::LT;
1841 ++ExactSIVsuccesses;
1842 }
1843
1844 // finished
1845 Result.DV[Level].Direction &= NewDirection;
1846 if (Result.DV[Level].Direction == Dependence::DVEntry::NONE)
1847 ++ExactSIVindependence;
1848 LLVM_DEBUG(dbgs() << "\t Result = ");
1849 LLVM_DEBUG(Result.dump(dbgs()));
1850 return Result.DV[Level].Direction == Dependence::DVEntry::NONE;
1851}
1852
1853// Return true if the divisor evenly divides the dividend.
1854static bool isRemainderZero(const SCEVConstant *Dividend,
1855 const SCEVConstant *Divisor) {
1856 const APInt &ConstDividend = Dividend->getAPInt();
1857 const APInt &ConstDivisor = Divisor->getAPInt();
1858 return ConstDividend.srem(ConstDivisor) == 0;
1859}
1860
1861// weakZeroSrcSIVtest -
1862// From the paper, Practical Dependence Testing, Section 4.2.2
1863//
1864// When we have a pair of subscripts of the form [c1] and [c2 + a*i],
1865// where i is an induction variable, c1 and c2 are loop invariant,
1866// and a is a constant, we can solve it exactly using the
1867// Weak-Zero SIV test.
1868//
1869// Given
1870//
1871// c1 = c2 + a*i
1872//
1873// we get
1874//
1875// (c1 - c2)/a = i
1876//
1877// If i is not an integer, there's no dependence.
1878// If i < 0 or > UB, there's no dependence.
1879// If i = 0, the direction is >= and peeling the
1880// 1st iteration will break the dependence.
1881// If i = UB, the direction is <= and peeling the
1882// last iteration will break the dependence.
1883// Otherwise, the direction is *.
1884//
1885// Can prove independence. Failing that, we can sometimes refine
1886// the directions. Can sometimes show that first or last
1887// iteration carries all the dependences (so worth peeling).
1888//
1889// (see also weakZeroDstSIVtest)
1890//
1891// Return true if dependence disproved.
1892bool DependenceInfo::weakZeroSrcSIVtest(const SCEV *DstCoeff,
1893 const SCEV *SrcConst,
1894 const SCEV *DstConst,
1895 const Loop *CurSrcLoop,
1896 const Loop *CurDstLoop, unsigned Level,
1897 FullDependence &Result) const {
1898 if (!isDependenceTestEnabled(DependenceTestType::WeakZeroSIV))
1899 return false;
1900
1901 // For the WeakSIV test, it's possible the loop isn't common to
1902 // the Src and Dst loops. If it isn't, then there's no need to
1903 // record a direction.
1904 LLVM_DEBUG(dbgs() << "\tWeak-Zero (src) SIV test\n");
1905 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << "\n");
1906 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
1907 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
1908 ++WeakZeroSIVapplications;
1909 assert(0 < Level && Level <= MaxLevels && "Level out of range");
1910 Level--;
1911 Result.Consistent = false;
1912 const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst);
1913 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
1914 if (isKnownPredicate(CmpInst::ICMP_EQ, SrcConst, DstConst)) {
1915 if (Level < CommonLevels) {
1916 Result.DV[Level].Direction &= Dependence::DVEntry::GE;
1917 Result.DV[Level].PeelFirst = true;
1918 ++WeakZeroSIVsuccesses;
1919 }
1920 return false; // dependences caused by first iteration
1921 }
1922 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
1923 if (!ConstCoeff)
1924 return false;
1925
1926 // Since ConstCoeff is constant, !isKnownNegative means it's non-negative.
1927 // TODO: Bail out if it's a signed minimum value.
1928 const SCEV *AbsCoeff = SE->isKnownNegative(ConstCoeff)
1929 ? SE->getNegativeSCEV(ConstCoeff)
1930 : ConstCoeff;
1931 const SCEV *NewDelta =
1932 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
1933
1934 // check that Delta/SrcCoeff < iteration count
1935 // really check NewDelta < count*AbsCoeff
1936 if (const SCEV *UpperBound =
1937 collectUpperBound(CurSrcLoop, Delta->getType())) {
1938 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
1939 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound);
1940 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) {
1941 ++WeakZeroSIVindependence;
1942 ++WeakZeroSIVsuccesses;
1943 return true;
1944 }
1945 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) {
1946 // dependences caused by last iteration
1947 if (Level < CommonLevels) {
1948 Result.DV[Level].Direction &= Dependence::DVEntry::LE;
1949 Result.DV[Level].PeelLast = true;
1950 ++WeakZeroSIVsuccesses;
1951 }
1952 return false;
1953 }
1954 }
1955
1956 // check that Delta/SrcCoeff >= 0
1957 // really check that NewDelta >= 0
1958 if (SE->isKnownNegative(NewDelta)) {
1959 // No dependence, newDelta < 0
1960 ++WeakZeroSIVindependence;
1961 ++WeakZeroSIVsuccesses;
1962 return true;
1963 }
1964
1965 // if SrcCoeff doesn't divide Delta, then no dependence
1966 if (isa<SCEVConstant>(Delta) &&
1967 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) {
1968 ++WeakZeroSIVindependence;
1969 ++WeakZeroSIVsuccesses;
1970 return true;
1971 }
1972 return false;
1973}
1974
1975// weakZeroDstSIVtest -
1976// From the paper, Practical Dependence Testing, Section 4.2.2
1977//
1978// When we have a pair of subscripts of the form [c1 + a*i] and [c2],
1979// where i is an induction variable, c1 and c2 are loop invariant,
1980// and a is a constant, we can solve it exactly using the
1981// Weak-Zero SIV test.
1982//
1983// Given
1984//
1985// c1 + a*i = c2
1986//
1987// we get
1988//
1989// i = (c2 - c1)/a
1990//
1991// If i is not an integer, there's no dependence.
1992// If i < 0 or > UB, there's no dependence.
1993// If i = 0, the direction is <= and peeling the
1994// 1st iteration will break the dependence.
1995// If i = UB, the direction is >= and peeling the
1996// last iteration will break the dependence.
1997// Otherwise, the direction is *.
1998//
1999// Can prove independence. Failing that, we can sometimes refine
2000// the directions. Can sometimes show that first or last
2001// iteration carries all the dependences (so worth peeling).
2002//
2003// (see also weakZeroSrcSIVtest)
2004//
2005// Return true if dependence disproved.
2006bool DependenceInfo::weakZeroDstSIVtest(const SCEV *SrcCoeff,
2007 const SCEV *SrcConst,
2008 const SCEV *DstConst,
2009 const Loop *CurSrcLoop,
2010 const Loop *CurDstLoop, unsigned Level,
2011 FullDependence &Result) const {
2012 if (!isDependenceTestEnabled(DependenceTestType::WeakZeroSIV))
2013 return false;
2014
2015 // For the WeakSIV test, it's possible the loop isn't common to the
2016 // Src and Dst loops. If it isn't, then there's no need to record a direction.
2017 LLVM_DEBUG(dbgs() << "\tWeak-Zero (dst) SIV test\n");
2018 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << "\n");
2019 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
2020 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
2021 ++WeakZeroSIVapplications;
2022 assert(0 < Level && Level <= SrcLevels && "Level out of range");
2023 Level--;
2024 Result.Consistent = false;
2025 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
2026 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
2027 if (isKnownPredicate(CmpInst::ICMP_EQ, DstConst, SrcConst)) {
2028 if (Level < CommonLevels) {
2029 Result.DV[Level].Direction &= Dependence::DVEntry::LE;
2030 Result.DV[Level].PeelFirst = true;
2031 ++WeakZeroSIVsuccesses;
2032 }
2033 return false; // dependences caused by first iteration
2034 }
2035 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
2036 if (!ConstCoeff)
2037 return false;
2038
2039 // Since ConstCoeff is constant, !isKnownNegative means it's non-negative.
2040 // TODO: Bail out if it's a signed minimum value.
2041 const SCEV *AbsCoeff = SE->isKnownNegative(ConstCoeff)
2042 ? SE->getNegativeSCEV(ConstCoeff)
2043 : ConstCoeff;
2044 const SCEV *NewDelta =
2045 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
2046
2047 // check that Delta/SrcCoeff < iteration count
2048 // really check NewDelta < count*AbsCoeff
2049 if (const SCEV *UpperBound =
2050 collectUpperBound(CurSrcLoop, Delta->getType())) {
2051 LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
2052 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound);
2053 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) {
2054 ++WeakZeroSIVindependence;
2055 ++WeakZeroSIVsuccesses;
2056 return true;
2057 }
2058 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) {
2059 // dependences caused by last iteration
2060 if (Level < CommonLevels) {
2061 Result.DV[Level].Direction &= Dependence::DVEntry::GE;
2062 Result.DV[Level].PeelLast = true;
2063 ++WeakZeroSIVsuccesses;
2064 }
2065 return false;
2066 }
2067 }
2068
2069 // check that Delta/SrcCoeff >= 0
2070 // really check that NewDelta >= 0
2071 if (SE->isKnownNegative(NewDelta)) {
2072 // No dependence, newDelta < 0
2073 ++WeakZeroSIVindependence;
2074 ++WeakZeroSIVsuccesses;
2075 return true;
2076 }
2077
2078 // if SrcCoeff doesn't divide Delta, then no dependence
2079 if (isa<SCEVConstant>(Delta) &&
2080 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) {
2081 ++WeakZeroSIVindependence;
2082 ++WeakZeroSIVsuccesses;
2083 return true;
2084 }
2085 return false;
2086}
2087
2088// exactRDIVtest - Tests the RDIV subscript pair for dependence.
2089// Things of the form [c1 + a*i] and [c2 + b*j],
2090// where i and j are induction variable, c1 and c2 are loop invariant,
2091// and a and b are constants.
2092// Returns true if any possible dependence is disproved.
2093// Marks the result as inconsistent.
2094// Works in some cases that symbolicRDIVtest doesn't, and vice versa.
2095bool DependenceInfo::exactRDIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
2096 const SCEV *SrcConst, const SCEV *DstConst,
2097 const Loop *SrcLoop, const Loop *DstLoop,
2098 FullDependence &Result) const {
2099 if (!isDependenceTestEnabled(DependenceTestType::ExactRDIV))
2100 return false;
2101
2102 LLVM_DEBUG(dbgs() << "\tExact RDIV test\n");
2103 LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n");
2104 LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n");
2105 LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
2106 LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
2107 ++ExactRDIVapplications;
2108 Result.Consistent = false;
2109 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
2110 LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
2111 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
2112 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
2113 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
2114 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
2115 return false;
2116
2117 // find gcd
2118 APInt G, X, Y;
2119 APInt AM = ConstSrcCoeff->getAPInt();
2120 APInt BM = ConstDstCoeff->getAPInt();
2121 APInt CM = ConstDelta->getAPInt();
2122 unsigned Bits = AM.getBitWidth();
2123 if (findGCD(Bits, AM, BM, CM, G, X, Y)) {
2124 // gcd doesn't divide Delta, no dependence
2125 ++ExactRDIVindependence;
2126 return true;
2127 }
2128
2129 LLVM_DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n");
2130
2131 // since SCEV construction seems to normalize, LM = 0
2132 std::optional<APInt> SrcUM;
2133 // SrcUM is perhaps unavailable, let's check
2134 if (const SCEVConstant *UpperBound =
2135 collectConstantUpperBound(SrcLoop, Delta->getType())) {
2136 SrcUM = UpperBound->getAPInt();
2137 LLVM_DEBUG(dbgs() << "\t SrcUM = " << *SrcUM << "\n");
2138 }
2139
2140 std::optional<APInt> DstUM;
2141 // UM is perhaps unavailable, let's check
2142 if (const SCEVConstant *UpperBound =
2143 collectConstantUpperBound(DstLoop, Delta->getType())) {
2144 DstUM = UpperBound->getAPInt();
2145 LLVM_DEBUG(dbgs() << "\t DstUM = " << *DstUM << "\n");
2146 }
2147
2148 APInt TU(APInt::getSignedMaxValue(Bits));
2149 APInt TL(APInt::getSignedMinValue(Bits));
2150 APInt TC = CM.sdiv(G);
2151 APInt TX = X * TC;
2152 APInt TY = Y * TC;
2153 LLVM_DEBUG(dbgs() << "\t TC = " << TC << "\n");
2154 LLVM_DEBUG(dbgs() << "\t TX = " << TX << "\n");
2155 LLVM_DEBUG(dbgs() << "\t TY = " << TY << "\n");
2156
2157 APInt TB = BM.sdiv(G);
2158 APInt TA = AM.sdiv(G);
2159
2160 // At this point, we have the following equations:
2161 //
2162 // TA*i - TB*j = TC
2163 //
2164 // Also, we know that the all pairs of (i, j) can be expressed as:
2165 //
2166 // (TX + k*TB, TY + k*TA)
2167 //
2168 // where k is an arbitrary integer.
2169 auto [TL0, TU0] = inferDomainOfAffine(TB, TX, SrcUM);
2170 auto [TL1, TU1] = inferDomainOfAffine(TA, TY, DstUM);
2171
2172 LLVM_DEBUG(dbgs() << "\t TA = " << TA << "\n");
2173 LLVM_DEBUG(dbgs() << "\t TB = " << TB << "\n");
2174
2175 auto CreateVec = [](const std::optional<APInt> &V0,
2176 const std::optional<APInt> &V1) {
2178 if (V0)
2179 Vec.push_back(*V0);
2180 if (V1)
2181 Vec.push_back(*V1);
2182 return Vec;
2183 };
2184
2185 SmallVector<APInt, 2> TLVec = CreateVec(TL0, TL1);
2186 SmallVector<APInt, 2> TUVec = CreateVec(TU0, TU1);
2187 if (TLVec.empty() || TUVec.empty())
2188 return false;
2189
2190 TL = APIntOps::smax(TLVec.front(), TLVec.back());
2191 TU = APIntOps::smin(TUVec.front(), TUVec.back());
2192 LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n");
2193 LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n");
2194
2195 if (TL.sgt(TU))
2196 ++ExactRDIVindependence;
2197 return TL.sgt(TU);
2198}
2199
2200// symbolicRDIVtest -
2201// In Section 4.5 of the Practical Dependence Testing paper,the authors
2202// introduce a special case of Banerjee's Inequalities (also called the
2203// Extreme-Value Test) that can handle some of the SIV and RDIV cases,
2204// particularly cases with symbolics. Since it's only able to disprove
2205// dependence (not compute distances or directions), we'll use it as a
2206// fall back for the other tests.
2207//
2208// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
2209// where i and j are induction variables and c1 and c2 are loop invariants,
2210// we can use the symbolic tests to disprove some dependences, serving as a
2211// backup for the RDIV test. Note that i and j can be the same variable,
2212// letting this test serve as a backup for the various SIV tests.
2213//
2214// For a dependence to exist, c1 + a1*i must equal c2 + a2*j for some
2215// 0 <= i <= N1 and some 0 <= j <= N2, where N1 and N2 are the (normalized)
2216// loop bounds for the i and j loops, respectively. So, ...
2217//
2218// c1 + a1*i = c2 + a2*j
2219// a1*i - a2*j = c2 - c1
2220//
2221// To test for a dependence, we compute c2 - c1 and make sure it's in the
2222// range of the maximum and minimum possible values of a1*i - a2*j.
2223// Considering the signs of a1 and a2, we have 4 possible cases:
2224//
2225// 1) If a1 >= 0 and a2 >= 0, then
2226// a1*0 - a2*N2 <= c2 - c1 <= a1*N1 - a2*0
2227// -a2*N2 <= c2 - c1 <= a1*N1
2228//
2229// 2) If a1 >= 0 and a2 <= 0, then
2230// a1*0 - a2*0 <= c2 - c1 <= a1*N1 - a2*N2
2231// 0 <= c2 - c1 <= a1*N1 - a2*N2
2232//
2233// 3) If a1 <= 0 and a2 >= 0, then
2234// a1*N1 - a2*N2 <= c2 - c1 <= a1*0 - a2*0
2235// a1*N1 - a2*N2 <= c2 - c1 <= 0
2236//
2237// 4) If a1 <= 0 and a2 <= 0, then
2238// a1*N1 - a2*0 <= c2 - c1 <= a1*0 - a2*N2
2239// a1*N1 <= c2 - c1 <= -a2*N2
2240//
2241// return true if dependence disproved
2242bool DependenceInfo::symbolicRDIVtest(const SCEV *A1, const SCEV *A2,
2243 const SCEV *C1, const SCEV *C2,
2244 const Loop *Loop1,
2245 const Loop *Loop2) const {
2246 if (!isDependenceTestEnabled(DependenceTestType::SymbolicRDIV))
2247 return false;
2248
2249 ++SymbolicRDIVapplications;
2250 LLVM_DEBUG(dbgs() << "\ttry symbolic RDIV test\n");
2251 LLVM_DEBUG(dbgs() << "\t A1 = " << *A1);
2252 LLVM_DEBUG(dbgs() << ", type = " << *A1->getType() << "\n");
2253 LLVM_DEBUG(dbgs() << "\t A2 = " << *A2 << "\n");
2254 LLVM_DEBUG(dbgs() << "\t C1 = " << *C1 << "\n");
2255 LLVM_DEBUG(dbgs() << "\t C2 = " << *C2 << "\n");
2256 const SCEV *N1 = collectUpperBound(Loop1, A1->getType());
2257 const SCEV *N2 = collectUpperBound(Loop2, A1->getType());
2258 LLVM_DEBUG(if (N1) dbgs() << "\t N1 = " << *N1 << "\n");
2259 LLVM_DEBUG(if (N2) dbgs() << "\t N2 = " << *N2 << "\n");
2260 const SCEV *C2_C1 = SE->getMinusSCEV(C2, C1);
2261 const SCEV *C1_C2 = SE->getMinusSCEV(C1, C2);
2262 LLVM_DEBUG(dbgs() << "\t C2 - C1 = " << *C2_C1 << "\n");
2263 LLVM_DEBUG(dbgs() << "\t C1 - C2 = " << *C1_C2 << "\n");
2264 if (SE->isKnownNonNegative(A1)) {
2265 if (SE->isKnownNonNegative(A2)) {
2266 // A1 >= 0 && A2 >= 0
2267 if (N1) {
2268 // make sure that c2 - c1 <= a1*N1
2269 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2270 LLVM_DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n");
2271 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1)) {
2272 ++SymbolicRDIVindependence;
2273 return true;
2274 }
2275 }
2276 if (N2) {
2277 // make sure that -a2*N2 <= c2 - c1, or a2*N2 >= c1 - c2
2278 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2279 LLVM_DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n");
2280 if (isKnownPredicate(CmpInst::ICMP_SLT, A2N2, C1_C2)) {
2281 ++SymbolicRDIVindependence;
2282 return true;
2283 }
2284 }
2285 } else if (SE->isKnownNonPositive(A2)) {
2286 // a1 >= 0 && a2 <= 0
2287 if (N1 && N2) {
2288 // make sure that c2 - c1 <= a1*N1 - a2*N2
2289 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2290 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2291 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2);
2292 LLVM_DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n");
2293 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1_A2N2)) {
2294 ++SymbolicRDIVindependence;
2295 return true;
2296 }
2297 }
2298 // make sure that 0 <= c2 - c1
2299 if (SE->isKnownNegative(C2_C1)) {
2300 ++SymbolicRDIVindependence;
2301 return true;
2302 }
2303 }
2304 } else if (SE->isKnownNonPositive(A1)) {
2305 if (SE->isKnownNonNegative(A2)) {
2306 // a1 <= 0 && a2 >= 0
2307 if (N1 && N2) {
2308 // make sure that a1*N1 - a2*N2 <= c2 - c1
2309 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2310 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2311 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2);
2312 LLVM_DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n");
2313 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1_A2N2, C2_C1)) {
2314 ++SymbolicRDIVindependence;
2315 return true;
2316 }
2317 }
2318 // make sure that c2 - c1 <= 0
2319 if (SE->isKnownPositive(C2_C1)) {
2320 ++SymbolicRDIVindependence;
2321 return true;
2322 }
2323 } else if (SE->isKnownNonPositive(A2)) {
2324 // a1 <= 0 && a2 <= 0
2325 if (N1) {
2326 // make sure that a1*N1 <= c2 - c1
2327 const SCEV *A1N1 = SE->getMulExpr(A1, N1);
2328 LLVM_DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n");
2329 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1, C2_C1)) {
2330 ++SymbolicRDIVindependence;
2331 return true;
2332 }
2333 }
2334 if (N2) {
2335 // make sure that c2 - c1 <= -a2*N2, or c1 - c2 >= a2*N2
2336 const SCEV *A2N2 = SE->getMulExpr(A2, N2);
2337 LLVM_DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n");
2338 if (isKnownPredicate(CmpInst::ICMP_SLT, C1_C2, A2N2)) {
2339 ++SymbolicRDIVindependence;
2340 return true;
2341 }
2342 }
2343 }
2344 }
2345 return false;
2346}
2347
2348// testSIV -
2349// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 - a2*i]
2350// where i is an induction variable, c1 and c2 are loop invariant, and a1 and
2351// a2 are constant, we attack it with an SIV test. While they can all be
2352// solved with the Exact SIV test, it's worthwhile to use simpler tests when
2353// they apply; they're cheaper and sometimes more precise.
2354//
2355// Return true if dependence disproved.
2356bool DependenceInfo::testSIV(const SCEV *Src, const SCEV *Dst, unsigned &Level,
2357 FullDependence &Result,
2358 bool UnderRuntimeAssumptions) {
2359 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
2360 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
2361 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
2362 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
2363 if (SrcAddRec && DstAddRec) {
2364 const SCEV *SrcConst = SrcAddRec->getStart();
2365 const SCEV *DstConst = DstAddRec->getStart();
2366 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2367 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE);
2368 const Loop *CurSrcLoop = SrcAddRec->getLoop();
2369 const Loop *CurDstLoop = DstAddRec->getLoop();
2370 assert(haveSameSD(CurSrcLoop, CurDstLoop) &&
2371 "Loops in the SIV test should have the same iteration space and "
2372 "depth");
2373 Level = mapSrcLoop(CurSrcLoop);
2374 bool disproven;
2375 if (SrcCoeff == DstCoeff)
2376 disproven =
2377 strongSIVtest(SrcCoeff, SrcConst, DstConst, CurSrcLoop, CurDstLoop,
2378 Level, Result, UnderRuntimeAssumptions);
2379 else if (SrcCoeff == SE->getNegativeSCEV(DstCoeff))
2380 disproven = weakCrossingSIVtest(SrcCoeff, SrcConst, DstConst, CurSrcLoop,
2381 CurDstLoop, Level, Result);
2382 else
2383 disproven = exactSIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst,
2384 CurSrcLoop, CurDstLoop, Level, Result);
2385 return disproven || gcdMIVtest(Src, Dst, Result) ||
2386 symbolicRDIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurSrcLoop,
2387 CurDstLoop);
2388 }
2389 if (SrcAddRec) {
2390 const SCEV *SrcConst = SrcAddRec->getStart();
2391 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2392 const SCEV *DstConst = Dst;
2393 const Loop *CurSrcLoop = SrcAddRec->getLoop();
2394 Level = mapSrcLoop(CurSrcLoop);
2395 return weakZeroDstSIVtest(SrcCoeff, SrcConst, DstConst, CurSrcLoop,
2396 CurSrcLoop, Level, Result) ||
2397 gcdMIVtest(Src, Dst, Result);
2398 }
2399 if (DstAddRec) {
2400 const SCEV *DstConst = DstAddRec->getStart();
2401 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE);
2402 const SCEV *SrcConst = Src;
2403 const Loop *CurDstLoop = DstAddRec->getLoop();
2404 Level = mapDstLoop(CurDstLoop);
2405 return weakZeroSrcSIVtest(DstCoeff, SrcConst, DstConst, CurDstLoop,
2406 CurDstLoop, Level, Result) ||
2407 gcdMIVtest(Src, Dst, Result);
2408 }
2409 llvm_unreachable("SIV test expected at least one AddRec");
2410 return false;
2411}
2412
2413// testRDIV -
2414// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
2415// where i and j are induction variables, c1 and c2 are loop invariant,
2416// and a1 and a2 are constant, we can solve it exactly with an easy adaptation
2417// of the Exact SIV test, the Restricted Double Index Variable (RDIV) test.
2418// It doesn't make sense to talk about distance or direction in this case,
2419// so there's no point in making special versions of the Strong SIV test or
2420// the Weak-crossing SIV test.
2421//
2422// With minor algebra, this test can also be used for things like
2423// [c1 + a1*i + a2*j][c2].
2424//
2425// Return true if dependence disproved.
2426bool DependenceInfo::testRDIV(const SCEV *Src, const SCEV *Dst,
2427 FullDependence &Result) const {
2428 // we have 3 possible situations here:
2429 // 1) [a*i + b] and [c*j + d]
2430 // 2) [a*i + c*j + b] and [d]
2431 // 3) [b] and [a*i + c*j + d]
2432 // We need to find what we've got and get organized
2433
2434 const SCEV *SrcConst, *DstConst;
2435 const SCEV *SrcCoeff, *DstCoeff;
2436 const Loop *SrcLoop, *DstLoop;
2437
2438 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
2439 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
2440 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
2441 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
2442 if (SrcAddRec && DstAddRec) {
2443 SrcConst = SrcAddRec->getStart();
2444 SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
2445 SrcLoop = SrcAddRec->getLoop();
2446 DstConst = DstAddRec->getStart();
2447 DstCoeff = DstAddRec->getStepRecurrence(*SE);
2448 DstLoop = DstAddRec->getLoop();
2449 } else if (SrcAddRec) {
2450 if (const SCEVAddRecExpr *tmpAddRec =
2451 dyn_cast<SCEVAddRecExpr>(SrcAddRec->getStart())) {
2452 SrcConst = tmpAddRec->getStart();
2453 SrcCoeff = tmpAddRec->getStepRecurrence(*SE);
2454 SrcLoop = tmpAddRec->getLoop();
2455 DstConst = Dst;
2456 DstCoeff = SE->getNegativeSCEV(SrcAddRec->getStepRecurrence(*SE));
2457 DstLoop = SrcAddRec->getLoop();
2458 } else
2459 llvm_unreachable("RDIV reached by surprising SCEVs");
2460 } else if (DstAddRec) {
2461 if (const SCEVAddRecExpr *tmpAddRec =
2462 dyn_cast<SCEVAddRecExpr>(DstAddRec->getStart())) {
2463 DstConst = tmpAddRec->getStart();
2464 DstCoeff = tmpAddRec->getStepRecurrence(*SE);
2465 DstLoop = tmpAddRec->getLoop();
2466 SrcConst = Src;
2467 SrcCoeff = SE->getNegativeSCEV(DstAddRec->getStepRecurrence(*SE));
2468 SrcLoop = DstAddRec->getLoop();
2469 } else
2470 llvm_unreachable("RDIV reached by surprising SCEVs");
2471 } else
2472 llvm_unreachable("RDIV expected at least one AddRec");
2473 return exactRDIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, SrcLoop, DstLoop,
2474 Result) ||
2475 gcdMIVtest(Src, Dst, Result) ||
2476 symbolicRDIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, SrcLoop,
2477 DstLoop);
2478}
2479
2480// Tests the single-subscript MIV pair (Src and Dst) for dependence.
2481// Return true if dependence disproved.
2482// Can sometimes refine direction vectors.
2483bool DependenceInfo::testMIV(const SCEV *Src, const SCEV *Dst,
2484 const SmallBitVector &Loops,
2485 FullDependence &Result) const {
2486 LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
2487 LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
2488 Result.Consistent = false;
2489 return gcdMIVtest(Src, Dst, Result) ||
2490 banerjeeMIVtest(Src, Dst, Loops, Result);
2491}
2492
2493/// Given a SCEVMulExpr, returns its first operand if its first operand is a
2494/// constant and the product doesn't overflow in a signed sense. Otherwise,
2495/// returns std::nullopt. For example, given (10 * X * Y)<nsw>, it returns 10.
2496/// Notably, if it doesn't have nsw, the multiplication may overflow, and if
2497/// so, it may not a multiple of 10.
2498static std::optional<APInt> getConstantCoefficient(const SCEV *Expr) {
2499 if (const auto *Constant = dyn_cast<SCEVConstant>(Expr))
2500 return Constant->getAPInt();
2501 if (const auto *Product = dyn_cast<SCEVMulExpr>(Expr))
2502 if (const auto *Constant = dyn_cast<SCEVConstant>(Product->getOperand(0)))
2503 if (Product->hasNoSignedWrap())
2504 return Constant->getAPInt();
2505 return std::nullopt;
2506}
2507
2508bool DependenceInfo::accumulateCoefficientsGCD(const SCEV *Expr,
2509 const Loop *CurLoop,
2510 const SCEV *&CurLoopCoeff,
2511 APInt &RunningGCD) const {
2512 // If RunningGCD is already 1, exit early.
2513 // TODO: It might be better to continue the recursion to find CurLoopCoeff.
2514 if (RunningGCD == 1)
2515 return true;
2516
2517 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
2518 if (!AddRec) {
2519 assert(isLoopInvariant(Expr, CurLoop) &&
2520 "Expected loop invariant expression");
2521 return true;
2522 }
2523
2524 assert(AddRec->isAffine() && "Unexpected Expr");
2525 const SCEV *Start = AddRec->getStart();
2526 const SCEV *Step = AddRec->getStepRecurrence(*SE);
2527 if (AddRec->getLoop() == CurLoop) {
2528 CurLoopCoeff = Step;
2529 } else {
2530 std::optional<APInt> ConstCoeff = getConstantCoefficient(Step);
2531
2532 // If the coefficient is the product of a constant and other stuff, we can
2533 // use the constant in the GCD computation.
2534 if (!ConstCoeff)
2535 return false;
2536
2537 // TODO: What happens if ConstCoeff is the "most negative" signed number
2538 // (e.g. -128 for 8 bit wide APInt)?
2539 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
2540 }
2541
2542 return accumulateCoefficientsGCD(Start, CurLoop, CurLoopCoeff, RunningGCD);
2543}
2544
2545//===----------------------------------------------------------------------===//
2546// gcdMIVtest -
2547// Tests an MIV subscript pair for dependence.
2548// Returns true if any possible dependence is disproved.
2549// Marks the result as inconsistent.
2550// Can sometimes disprove the equal direction for 1 or more loops,
2551// as discussed in Michael Wolfe's book,
2552// High Performance Compilers for Parallel Computing, page 235.
2553//
2554// We spend some effort (code!) to handle cases like
2555// [10*i + 5*N*j + 15*M + 6], where i and j are induction variables,
2556// but M and N are just loop-invariant variables.
2557// This should help us handle linearized subscripts;
2558// also makes this test a useful backup to the various SIV tests.
2559//
2560// It occurs to me that the presence of loop-invariant variables
2561// changes the nature of the test from "greatest common divisor"
2562// to "a common divisor".
2563bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst,
2564 FullDependence &Result) const {
2565 if (!isDependenceTestEnabled(DependenceTestType::GCDMIV))
2566 return false;
2567
2568 LLVM_DEBUG(dbgs() << "starting gcd\n");
2569 ++GCDapplications;
2570 unsigned BitWidth = SE->getTypeSizeInBits(Src->getType());
2571 APInt RunningGCD = APInt::getZero(BitWidth);
2572
2573 // Examine Src coefficients.
2574 // Compute running GCD and record source constant.
2575 // Because we're looking for the constant at the end of the chain,
2576 // we can't quit the loop just because the GCD == 1.
2577 const SCEV *Coefficients = Src;
2578 while (const SCEVAddRecExpr *AddRec =
2579 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2580 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2581 // If the coefficient is the product of a constant and other stuff,
2582 // we can use the constant in the GCD computation.
2583 std::optional<APInt> ConstCoeff = getConstantCoefficient(Coeff);
2584 if (!ConstCoeff)
2585 return false;
2586 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
2587 Coefficients = AddRec->getStart();
2588 }
2589 const SCEV *SrcConst = Coefficients;
2590
2591 // Examine Dst coefficients.
2592 // Compute running GCD and record destination constant.
2593 // Because we're looking for the constant at the end of the chain,
2594 // we can't quit the loop just because the GCD == 1.
2595 Coefficients = Dst;
2596 while (const SCEVAddRecExpr *AddRec =
2597 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2598 const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
2599 // If the coefficient is the product of a constant and other stuff,
2600 // we can use the constant in the GCD computation.
2601 std::optional<APInt> ConstCoeff = getConstantCoefficient(Coeff);
2602 if (!ConstCoeff)
2603 return false;
2604 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
2605 Coefficients = AddRec->getStart();
2606 }
2607 const SCEV *DstConst = Coefficients;
2608
2609 APInt ExtraGCD = APInt::getZero(BitWidth);
2610 const SCEV *Delta = minusSCEVNoSignedOverflow(DstConst, SrcConst, *SE);
2611 if (!Delta)
2612 return false;
2613 LLVM_DEBUG(dbgs() << " Delta = " << *Delta << "\n");
2614 const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Delta);
2615 if (!Constant)
2616 return false;
2617 APInt ConstDelta = cast<SCEVConstant>(Constant)->getAPInt();
2618 LLVM_DEBUG(dbgs() << " ConstDelta = " << ConstDelta << "\n");
2619 if (ConstDelta == 0)
2620 return false;
2621 LLVM_DEBUG(dbgs() << " RunningGCD = " << RunningGCD << "\n");
2622 APInt Remainder = ConstDelta.srem(RunningGCD);
2623 if (Remainder != 0) {
2624 ++GCDindependence;
2625 return true;
2626 }
2627
2628 // Try to disprove equal directions.
2629 // For example, given a subscript pair [3*i + 2*j] and [i' + 2*j' - 1],
2630 // the code above can't disprove the dependence because the GCD = 1.
2631 // So we consider what happen if i = i' and what happens if j = j'.
2632 // If i = i', we can simplify the subscript to [2*i + 2*j] and [2*j' - 1],
2633 // which is infeasible, so we can disallow the = direction for the i level.
2634 // Setting j = j' doesn't help matters, so we end up with a direction vector
2635 // of [<>, *]
2636 //
2637 // Given A[5*i + 10*j*M + 9*M*N] and A[15*i + 20*j*M - 21*N*M + 5],
2638 // we need to remember that the constant part is 5 and the RunningGCD should
2639 // be initialized to ExtraGCD = 30.
2640 LLVM_DEBUG(dbgs() << " ExtraGCD = " << ExtraGCD << '\n');
2641
2642 bool Improved = false;
2643 Coefficients = Src;
2644 while (const SCEVAddRecExpr *AddRec =
2645 dyn_cast<SCEVAddRecExpr>(Coefficients)) {
2646 Coefficients = AddRec->getStart();
2647 const Loop *CurLoop = AddRec->getLoop();
2648 RunningGCD = ExtraGCD;
2649 const SCEV *SrcCoeff = AddRec->getStepRecurrence(*SE);
2650 const SCEV *DstCoeff = SE->getMinusSCEV(SrcCoeff, SrcCoeff);
2651
2652 if (!accumulateCoefficientsGCD(Src, CurLoop, SrcCoeff, RunningGCD) ||
2653 !accumulateCoefficientsGCD(Dst, CurLoop, DstCoeff, RunningGCD))
2654 return false;
2655
2656 Delta = SE->getMinusSCEV(SrcCoeff, DstCoeff);
2657 // If the coefficient is the product of a constant and other stuff,
2658 // we can use the constant in the GCD computation.
2659 std::optional<APInt> ConstCoeff = getConstantCoefficient(Delta);
2660 if (!ConstCoeff)
2661 // The difference of the two coefficients might not be a product
2662 // or constant, in which case we give up on this direction.
2663 continue;
2664 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff->abs());
2665 LLVM_DEBUG(dbgs() << "\tRunningGCD = " << RunningGCD << "\n");
2666 if (RunningGCD != 0) {
2667 Remainder = ConstDelta.srem(RunningGCD);
2668 LLVM_DEBUG(dbgs() << "\tRemainder = " << Remainder << "\n");
2669 if (Remainder != 0) {
2670 unsigned Level = mapSrcLoop(CurLoop);
2671 Result.DV[Level - 1].Direction &= ~Dependence::DVEntry::EQ;
2672 Improved = true;
2673 }
2674 }
2675 }
2676 if (Improved)
2677 ++GCDsuccesses;
2678 LLVM_DEBUG(dbgs() << "all done\n");
2679 return false;
2680}
2681
2682//===----------------------------------------------------------------------===//
2683// banerjeeMIVtest -
2684// Use Banerjee's Inequalities to test an MIV subscript pair.
2685// (Wolfe, in the race-car book, calls this the Extreme Value Test.)
2686// Generally follows the discussion in Section 2.5.2 of
2687//
2688// Optimizing Supercompilers for Supercomputers
2689// Michael Wolfe
2690//
2691// The inequalities given on page 25 are simplified in that loops are
2692// normalized so that the lower bound is always 0 and the stride is always 1.
2693// For example, Wolfe gives
2694//
2695// LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
2696//
2697// where A_k is the coefficient of the kth index in the source subscript,
2698// B_k is the coefficient of the kth index in the destination subscript,
2699// U_k is the upper bound of the kth index, L_k is the lower bound of the Kth
2700// index, and N_k is the stride of the kth index. Since all loops are normalized
2701// by the SCEV package, N_k = 1 and L_k = 0, allowing us to simplify the
2702// equation to
2703//
2704// LB^<_k = (A^-_k - B_k)^- (U_k - 0 - 1) + (A_k - B_k)0 - B_k 1
2705// = (A^-_k - B_k)^- (U_k - 1) - B_k
2706//
2707// Similar simplifications are possible for the other equations.
2708//
2709// When we can't determine the number of iterations for a loop,
2710// we use NULL as an indicator for the worst case, infinity.
2711// When computing the upper bound, NULL denotes +inf;
2712// for the lower bound, NULL denotes -inf.
2713//
2714// Return true if dependence disproved.
2715bool DependenceInfo::banerjeeMIVtest(const SCEV *Src, const SCEV *Dst,
2716 const SmallBitVector &Loops,
2717 FullDependence &Result) const {
2718 if (!isDependenceTestEnabled(DependenceTestType::BanerjeeMIV))
2719 return false;
2720
2721 LLVM_DEBUG(dbgs() << "starting Banerjee\n");
2722 ++BanerjeeApplications;
2723 LLVM_DEBUG(dbgs() << " Src = " << *Src << '\n');
2724 const SCEV *A0;
2725 CoefficientInfo *A = collectCoeffInfo(Src, true, A0);
2726 LLVM_DEBUG(dbgs() << " Dst = " << *Dst << '\n');
2727 const SCEV *B0;
2728 CoefficientInfo *B = collectCoeffInfo(Dst, false, B0);
2729 BoundInfo *Bound = new BoundInfo[MaxLevels + 1];
2730 const SCEV *Delta = SE->getMinusSCEV(B0, A0);
2731 LLVM_DEBUG(dbgs() << "\tDelta = " << *Delta << '\n');
2732
2733 // Compute bounds for all the * directions.
2734 LLVM_DEBUG(dbgs() << "\tBounds[*]\n");
2735 for (unsigned K = 1; K <= MaxLevels; ++K) {
2736 Bound[K].Iterations = A[K].Iterations ? A[K].Iterations : B[K].Iterations;
2737 Bound[K].Direction = Dependence::DVEntry::ALL;
2738 Bound[K].DirSet = Dependence::DVEntry::NONE;
2739 findBoundsALL(A, B, Bound, K);
2740#ifndef NDEBUG
2741 LLVM_DEBUG(dbgs() << "\t " << K << '\t');
2742 if (Bound[K].Lower[Dependence::DVEntry::ALL])
2743 LLVM_DEBUG(dbgs() << *Bound[K].Lower[Dependence::DVEntry::ALL] << '\t');
2744 else
2745 LLVM_DEBUG(dbgs() << "-inf\t");
2746 if (Bound[K].Upper[Dependence::DVEntry::ALL])
2747 LLVM_DEBUG(dbgs() << *Bound[K].Upper[Dependence::DVEntry::ALL] << '\n');
2748 else
2749 LLVM_DEBUG(dbgs() << "+inf\n");
2750#endif
2751 }
2752
2753 // Test the *, *, *, ... case.
2754 bool Disproved = false;
2755 if (testBounds(Dependence::DVEntry::ALL, 0, Bound, Delta)) {
2756 // Explore the direction vector hierarchy.
2757 unsigned DepthExpanded = 0;
2758 unsigned NewDeps =
2759 exploreDirections(1, A, B, Bound, Loops, DepthExpanded, Delta);
2760 if (NewDeps > 0) {
2761 bool Improved = false;
2762 for (unsigned K = 1; K <= CommonLevels; ++K) {
2763 if (Loops[K]) {
2764 unsigned Old = Result.DV[K - 1].Direction;
2765 Result.DV[K - 1].Direction = Old & Bound[K].DirSet;
2766 Improved |= Old != Result.DV[K - 1].Direction;
2767 if (!Result.DV[K - 1].Direction) {
2768 Improved = false;
2769 Disproved = true;
2770 break;
2771 }
2772 }
2773 }
2774 if (Improved)
2775 ++BanerjeeSuccesses;
2776 } else {
2777 ++BanerjeeIndependence;
2778 Disproved = true;
2779 }
2780 } else {
2781 ++BanerjeeIndependence;
2782 Disproved = true;
2783 }
2784 delete[] Bound;
2785 delete[] A;
2786 delete[] B;
2787 return Disproved;
2788}
2789
2790// Hierarchically expands the direction vector
2791// search space, combining the directions of discovered dependences
2792// in the DirSet field of Bound. Returns the number of distinct
2793// dependences discovered. If the dependence is disproved,
2794// it will return 0.
2795unsigned DependenceInfo::exploreDirections(unsigned Level, CoefficientInfo *A,
2796 CoefficientInfo *B, BoundInfo *Bound,
2797 const SmallBitVector &Loops,
2798 unsigned &DepthExpanded,
2799 const SCEV *Delta) const {
2800 // This algorithm has worst case complexity of O(3^n), where 'n' is the number
2801 // of common loop levels. To avoid excessive compile-time, pessimize all the
2802 // results and immediately return when the number of common levels is beyond
2803 // the given threshold.
2804 if (CommonLevels > MIVMaxLevelThreshold) {
2805 LLVM_DEBUG(dbgs() << "Number of common levels exceeded the threshold. MIV "
2806 "direction exploration is terminated.\n");
2807 for (unsigned K = 1; K <= CommonLevels; ++K)
2808 if (Loops[K])
2809 Bound[K].DirSet = Dependence::DVEntry::ALL;
2810 return 1;
2811 }
2812
2813 if (Level > CommonLevels) {
2814 // record result
2815 LLVM_DEBUG(dbgs() << "\t[");
2816 for (unsigned K = 1; K <= CommonLevels; ++K) {
2817 if (Loops[K]) {
2818 Bound[K].DirSet |= Bound[K].Direction;
2819#ifndef NDEBUG
2820 switch (Bound[K].Direction) {
2822 LLVM_DEBUG(dbgs() << " <");
2823 break;
2825 LLVM_DEBUG(dbgs() << " =");
2826 break;
2828 LLVM_DEBUG(dbgs() << " >");
2829 break;
2831 LLVM_DEBUG(dbgs() << " *");
2832 break;
2833 default:
2834 llvm_unreachable("unexpected Bound[K].Direction");
2835 }
2836#endif
2837 }
2838 }
2839 LLVM_DEBUG(dbgs() << " ]\n");
2840 return 1;
2841 }
2842 if (Loops[Level]) {
2843 if (Level > DepthExpanded) {
2844 DepthExpanded = Level;
2845 // compute bounds for <, =, > at current level
2846 findBoundsLT(A, B, Bound, Level);
2847 findBoundsGT(A, B, Bound, Level);
2848 findBoundsEQ(A, B, Bound, Level);
2849#ifndef NDEBUG
2850 LLVM_DEBUG(dbgs() << "\tBound for level = " << Level << '\n');
2851 LLVM_DEBUG(dbgs() << "\t <\t");
2852 if (Bound[Level].Lower[Dependence::DVEntry::LT])
2853 LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::LT]
2854 << '\t');
2855 else
2856 LLVM_DEBUG(dbgs() << "-inf\t");
2857 if (Bound[Level].Upper[Dependence::DVEntry::LT])
2858 LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::LT]
2859 << '\n');
2860 else
2861 LLVM_DEBUG(dbgs() << "+inf\n");
2862 LLVM_DEBUG(dbgs() << "\t =\t");
2863 if (Bound[Level].Lower[Dependence::DVEntry::EQ])
2864 LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::EQ]
2865 << '\t');
2866 else
2867 LLVM_DEBUG(dbgs() << "-inf\t");
2868 if (Bound[Level].Upper[Dependence::DVEntry::EQ])
2869 LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::EQ]
2870 << '\n');
2871 else
2872 LLVM_DEBUG(dbgs() << "+inf\n");
2873 LLVM_DEBUG(dbgs() << "\t >\t");
2874 if (Bound[Level].Lower[Dependence::DVEntry::GT])
2875 LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::GT]
2876 << '\t');
2877 else
2878 LLVM_DEBUG(dbgs() << "-inf\t");
2879 if (Bound[Level].Upper[Dependence::DVEntry::GT])
2880 LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::GT]
2881 << '\n');
2882 else
2883 LLVM_DEBUG(dbgs() << "+inf\n");
2884#endif
2885 }
2886
2887 unsigned NewDeps = 0;
2888
2889 // test bounds for <, *, *, ...
2890 if (testBounds(Dependence::DVEntry::LT, Level, Bound, Delta))
2891 NewDeps += exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
2892 Delta);
2893
2894 // Test bounds for =, *, *, ...
2895 if (testBounds(Dependence::DVEntry::EQ, Level, Bound, Delta))
2896 NewDeps += exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
2897 Delta);
2898
2899 // test bounds for >, *, *, ...
2900 if (testBounds(Dependence::DVEntry::GT, Level, Bound, Delta))
2901 NewDeps += exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
2902 Delta);
2903
2904 Bound[Level].Direction = Dependence::DVEntry::ALL;
2905 return NewDeps;
2906 } else
2907 return exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded,
2908 Delta);
2909}
2910
2911// Returns true iff the current bounds are plausible.
2912bool DependenceInfo::testBounds(unsigned char DirKind, unsigned Level,
2913 BoundInfo *Bound, const SCEV *Delta) const {
2914 Bound[Level].Direction = DirKind;
2915 if (const SCEV *LowerBound = getLowerBound(Bound))
2916 if (isKnownPredicate(CmpInst::ICMP_SGT, LowerBound, Delta))
2917 return false;
2918 if (const SCEV *UpperBound = getUpperBound(Bound))
2919 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, UpperBound))
2920 return false;
2921 return true;
2922}
2923
2924// Computes the upper and lower bounds for level K
2925// using the * direction. Records them in Bound.
2926// Wolfe gives the equations
2927//
2928// LB^*_k = (A^-_k - B^+_k)(U_k - L_k) + (A_k - B_k)L_k
2929// UB^*_k = (A^+_k - B^-_k)(U_k - L_k) + (A_k - B_k)L_k
2930//
2931// Since we normalize loops, we can simplify these equations to
2932//
2933// LB^*_k = (A^-_k - B^+_k)U_k
2934// UB^*_k = (A^+_k - B^-_k)U_k
2935//
2936// We must be careful to handle the case where the upper bound is unknown.
2937// Note that the lower bound is always <= 0
2938// and the upper bound is always >= 0.
2939void DependenceInfo::findBoundsALL(CoefficientInfo *A, CoefficientInfo *B,
2940 BoundInfo *Bound, unsigned K) const {
2941 Bound[K].Lower[Dependence::DVEntry::ALL] =
2942 nullptr; // Default value = -infinity.
2943 Bound[K].Upper[Dependence::DVEntry::ALL] =
2944 nullptr; // Default value = +infinity.
2945 if (Bound[K].Iterations) {
2946 Bound[K].Lower[Dependence::DVEntry::ALL] = SE->getMulExpr(
2947 SE->getMinusSCEV(A[K].NegPart, B[K].PosPart), Bound[K].Iterations);
2948 Bound[K].Upper[Dependence::DVEntry::ALL] = SE->getMulExpr(
2949 SE->getMinusSCEV(A[K].PosPart, B[K].NegPart), Bound[K].Iterations);
2950 } else {
2951 // If the difference is 0, we won't need to know the number of iterations.
2952 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].NegPart, B[K].PosPart))
2953 Bound[K].Lower[Dependence::DVEntry::ALL] =
2954 SE->getZero(A[K].Coeff->getType());
2955 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].PosPart, B[K].NegPart))
2956 Bound[K].Upper[Dependence::DVEntry::ALL] =
2957 SE->getZero(A[K].Coeff->getType());
2958 }
2959}
2960
2961// Computes the upper and lower bounds for level K
2962// using the = direction. Records them in Bound.
2963// Wolfe gives the equations
2964//
2965// LB^=_k = (A_k - B_k)^- (U_k - L_k) + (A_k - B_k)L_k
2966// UB^=_k = (A_k - B_k)^+ (U_k - L_k) + (A_k - B_k)L_k
2967//
2968// Since we normalize loops, we can simplify these equations to
2969//
2970// LB^=_k = (A_k - B_k)^- U_k
2971// UB^=_k = (A_k - B_k)^+ U_k
2972//
2973// We must be careful to handle the case where the upper bound is unknown.
2974// Note that the lower bound is always <= 0
2975// and the upper bound is always >= 0.
2976void DependenceInfo::findBoundsEQ(CoefficientInfo *A, CoefficientInfo *B,
2977 BoundInfo *Bound, unsigned K) const {
2978 Bound[K].Lower[Dependence::DVEntry::EQ] =
2979 nullptr; // Default value = -infinity.
2980 Bound[K].Upper[Dependence::DVEntry::EQ] =
2981 nullptr; // Default value = +infinity.
2982 if (Bound[K].Iterations) {
2983 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
2984 const SCEV *NegativePart = getNegativePart(Delta);
2985 Bound[K].Lower[Dependence::DVEntry::EQ] =
2986 SE->getMulExpr(NegativePart, Bound[K].Iterations);
2987 const SCEV *PositivePart = getPositivePart(Delta);
2988 Bound[K].Upper[Dependence::DVEntry::EQ] =
2989 SE->getMulExpr(PositivePart, Bound[K].Iterations);
2990 } else {
2991 // If the positive/negative part of the difference is 0,
2992 // we won't need to know the number of iterations.
2993 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
2994 const SCEV *NegativePart = getNegativePart(Delta);
2995 if (NegativePart->isZero())
2996 Bound[K].Lower[Dependence::DVEntry::EQ] = NegativePart; // Zero
2997 const SCEV *PositivePart = getPositivePart(Delta);
2998 if (PositivePart->isZero())
2999 Bound[K].Upper[Dependence::DVEntry::EQ] = PositivePart; // Zero
3000 }
3001}
3002
3003// Computes the upper and lower bounds for level K
3004// using the < direction. Records them in Bound.
3005// Wolfe gives the equations
3006//
3007// LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
3008// UB^<_k = (A^+_k - B_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
3009//
3010// Since we normalize loops, we can simplify these equations to
3011//
3012// LB^<_k = (A^-_k - B_k)^- (U_k - 1) - B_k
3013// UB^<_k = (A^+_k - B_k)^+ (U_k - 1) - B_k
3014//
3015// We must be careful to handle the case where the upper bound is unknown.
3016void DependenceInfo::findBoundsLT(CoefficientInfo *A, CoefficientInfo *B,
3017 BoundInfo *Bound, unsigned K) const {
3018 Bound[K].Lower[Dependence::DVEntry::LT] =
3019 nullptr; // Default value = -infinity.
3020 Bound[K].Upper[Dependence::DVEntry::LT] =
3021 nullptr; // Default value = +infinity.
3022 if (Bound[K].Iterations) {
3023 const SCEV *Iter_1 = SE->getMinusSCEV(
3024 Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
3025 const SCEV *NegPart =
3026 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
3027 Bound[K].Lower[Dependence::DVEntry::LT] =
3028 SE->getMinusSCEV(SE->getMulExpr(NegPart, Iter_1), B[K].Coeff);
3029 const SCEV *PosPart =
3030 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
3031 Bound[K].Upper[Dependence::DVEntry::LT] =
3032 SE->getMinusSCEV(SE->getMulExpr(PosPart, Iter_1), B[K].Coeff);
3033 } else {
3034 // If the positive/negative part of the difference is 0,
3035 // we won't need to know the number of iterations.
3036 const SCEV *NegPart =
3037 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
3038 if (NegPart->isZero())
3039 Bound[K].Lower[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
3040 const SCEV *PosPart =
3041 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
3042 if (PosPart->isZero())
3043 Bound[K].Upper[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
3044 }
3045}
3046
3047// Computes the upper and lower bounds for level K
3048// using the > direction. Records them in Bound.
3049// Wolfe gives the equations
3050//
3051// LB^>_k = (A_k - B^+_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
3052// UB^>_k = (A_k - B^-_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
3053//
3054// Since we normalize loops, we can simplify these equations to
3055//
3056// LB^>_k = (A_k - B^+_k)^- (U_k - 1) + A_k
3057// UB^>_k = (A_k - B^-_k)^+ (U_k - 1) + A_k
3058//
3059// We must be careful to handle the case where the upper bound is unknown.
3060void DependenceInfo::findBoundsGT(CoefficientInfo *A, CoefficientInfo *B,
3061 BoundInfo *Bound, unsigned K) const {
3062 Bound[K].Lower[Dependence::DVEntry::GT] =
3063 nullptr; // Default value = -infinity.
3064 Bound[K].Upper[Dependence::DVEntry::GT] =
3065 nullptr; // Default value = +infinity.
3066 if (Bound[K].Iterations) {
3067 const SCEV *Iter_1 = SE->getMinusSCEV(
3068 Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
3069 const SCEV *NegPart =
3070 getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
3071 Bound[K].Lower[Dependence::DVEntry::GT] =
3072 SE->getAddExpr(SE->getMulExpr(NegPart, Iter_1), A[K].Coeff);
3073 const SCEV *PosPart =
3074 getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
3075 Bound[K].Upper[Dependence::DVEntry::GT] =
3076 SE->getAddExpr(SE->getMulExpr(PosPart, Iter_1), A[K].Coeff);
3077 } else {
3078 // If the positive/negative part of the difference is 0,
3079 // we won't need to know the number of iterations.
3080 const SCEV *NegPart =
3081 getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
3082 if (NegPart->isZero())
3083 Bound[K].Lower[Dependence::DVEntry::GT] = A[K].Coeff;
3084 const SCEV *PosPart =
3085 getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
3086 if (PosPart->isZero())
3087 Bound[K].Upper[Dependence::DVEntry::GT] = A[K].Coeff;
3088 }
3089}
3090
3091// X^+ = max(X, 0)
3092const SCEV *DependenceInfo::getPositivePart(const SCEV *X) const {
3093 return SE->getSMaxExpr(X, SE->getZero(X->getType()));
3094}
3095
3096// X^- = min(X, 0)
3097const SCEV *DependenceInfo::getNegativePart(const SCEV *X) const {
3098 return SE->getSMinExpr(X, SE->getZero(X->getType()));
3099}
3100
3101// Walks through the subscript,
3102// collecting each coefficient, the associated loop bounds,
3103// and recording its positive and negative parts for later use.
3104DependenceInfo::CoefficientInfo *
3105DependenceInfo::collectCoeffInfo(const SCEV *Subscript, bool SrcFlag,
3106 const SCEV *&Constant) const {
3107 const SCEV *Zero = SE->getZero(Subscript->getType());
3108 CoefficientInfo *CI = new CoefficientInfo[MaxLevels + 1];
3109 for (unsigned K = 1; K <= MaxLevels; ++K) {
3110 CI[K].Coeff = Zero;
3111 CI[K].PosPart = Zero;
3112 CI[K].NegPart = Zero;
3113 CI[K].Iterations = nullptr;
3114 }
3115 while (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Subscript)) {
3116 const Loop *L = AddRec->getLoop();
3117 unsigned K = SrcFlag ? mapSrcLoop(L) : mapDstLoop(L);
3118 CI[K].Coeff = AddRec->getStepRecurrence(*SE);
3119 CI[K].PosPart = getPositivePart(CI[K].Coeff);
3120 CI[K].NegPart = getNegativePart(CI[K].Coeff);
3121 CI[K].Iterations = collectUpperBound(L, Subscript->getType());
3122 Subscript = AddRec->getStart();
3123 }
3124 Constant = Subscript;
3125#ifndef NDEBUG
3126 LLVM_DEBUG(dbgs() << "\tCoefficient Info\n");
3127 for (unsigned K = 1; K <= MaxLevels; ++K) {
3128 LLVM_DEBUG(dbgs() << "\t " << K << "\t" << *CI[K].Coeff);
3129 LLVM_DEBUG(dbgs() << "\tPos Part = ");
3130 LLVM_DEBUG(dbgs() << *CI[K].PosPart);
3131 LLVM_DEBUG(dbgs() << "\tNeg Part = ");
3132 LLVM_DEBUG(dbgs() << *CI[K].NegPart);
3133 LLVM_DEBUG(dbgs() << "\tUpper Bound = ");
3134 if (CI[K].Iterations)
3135 LLVM_DEBUG(dbgs() << *CI[K].Iterations);
3136 else
3137 LLVM_DEBUG(dbgs() << "+inf");
3138 LLVM_DEBUG(dbgs() << '\n');
3139 }
3140 LLVM_DEBUG(dbgs() << "\t Constant = " << *Subscript << '\n');
3141#endif
3142 return CI;
3143}
3144
3145// Looks through all the bounds info and
3146// computes the lower bound given the current direction settings
3147// at each level. If the lower bound for any level is -inf,
3148// the result is -inf.
3149const SCEV *DependenceInfo::getLowerBound(BoundInfo *Bound) const {
3150 const SCEV *Sum = Bound[1].Lower[Bound[1].Direction];
3151 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
3152 if (Bound[K].Lower[Bound[K].Direction])
3153 Sum = SE->getAddExpr(Sum, Bound[K].Lower[Bound[K].Direction]);
3154 else
3155 Sum = nullptr;
3156 }
3157 return Sum;
3158}
3159
3160// Looks through all the bounds info and
3161// computes the upper bound given the current direction settings
3162// at each level. If the upper bound at any level is +inf,
3163// the result is +inf.
3164const SCEV *DependenceInfo::getUpperBound(BoundInfo *Bound) const {
3165 const SCEV *Sum = Bound[1].Upper[Bound[1].Direction];
3166 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
3167 if (Bound[K].Upper[Bound[K].Direction])
3168 Sum = SE->getAddExpr(Sum, Bound[K].Upper[Bound[K].Direction]);
3169 else
3170 Sum = nullptr;
3171 }
3172 return Sum;
3173}
3174
3175/// Check if we can delinearize the subscripts. If the SCEVs representing the
3176/// source and destination array references are recurrences on a nested loop,
3177/// this function flattens the nested recurrences into separate recurrences
3178/// for each loop level.
3179bool DependenceInfo::tryDelinearize(Instruction *Src, Instruction *Dst,
3181 assert(isLoadOrStore(Src) && "instruction is not load or store");
3182 assert(isLoadOrStore(Dst) && "instruction is not load or store");
3183 Value *SrcPtr = getLoadStorePointerOperand(Src);
3184 Value *DstPtr = getLoadStorePointerOperand(Dst);
3185 Loop *SrcLoop = LI->getLoopFor(Src->getParent());
3186 Loop *DstLoop = LI->getLoopFor(Dst->getParent());
3187 const SCEV *SrcAccessFn = SE->getSCEVAtScope(SrcPtr, SrcLoop);
3188 const SCEV *DstAccessFn = SE->getSCEVAtScope(DstPtr, DstLoop);
3189 const SCEVUnknown *SrcBase =
3190 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
3191 const SCEVUnknown *DstBase =
3192 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
3193
3194 if (!SrcBase || !DstBase || SrcBase != DstBase)
3195 return false;
3196
3197 SmallVector<const SCEV *, 4> SrcSubscripts, DstSubscripts;
3198
3199 if (!tryDelinearizeFixedSize(Src, Dst, SrcAccessFn, DstAccessFn,
3200 SrcSubscripts, DstSubscripts) &&
3201 !tryDelinearizeParametricSize(Src, Dst, SrcAccessFn, DstAccessFn,
3202 SrcSubscripts, DstSubscripts))
3203 return false;
3204
3205 assert(isLoopInvariant(SrcBase, SrcLoop) &&
3206 isLoopInvariant(DstBase, DstLoop) &&
3207 "Expected SrcBase and DstBase to be loop invariant");
3208
3209 int Size = SrcSubscripts.size();
3210 LLVM_DEBUG({
3211 dbgs() << "\nSrcSubscripts: ";
3212 for (int I = 0; I < Size; I++)
3213 dbgs() << *SrcSubscripts[I];
3214 dbgs() << "\nDstSubscripts: ";
3215 for (int I = 0; I < Size; I++)
3216 dbgs() << *DstSubscripts[I];
3217 });
3218
3219 // The delinearization transforms a single-subscript MIV dependence test into
3220 // a multi-subscript SIV dependence test that is easier to compute. So we
3221 // resize Pair to contain as many pairs of subscripts as the delinearization
3222 // has found, and then initialize the pairs following the delinearization.
3223 Pair.resize(Size);
3224 SCEVMonotonicityChecker MonChecker(SE);
3225 const Loop *OutermostLoop = SrcLoop ? SrcLoop->getOutermostLoop() : nullptr;
3226 for (int I = 0; I < Size; ++I) {
3227 Pair[I].Src = SrcSubscripts[I];
3228 Pair[I].Dst = DstSubscripts[I];
3229 unifySubscriptType(&Pair[I]);
3230
3232 if (MonChecker.checkMonotonicity(Pair[I].Src, OutermostLoop).isUnknown())
3233 return false;
3234 if (MonChecker.checkMonotonicity(Pair[I].Dst, OutermostLoop).isUnknown())
3235 return false;
3236 }
3237 }
3238
3239 return true;
3240}
3241
3242/// Try to delinearize \p SrcAccessFn and \p DstAccessFn if the underlying
3243/// arrays accessed are fixed-size arrays. Return true if delinearization was
3244/// successful.
3245bool DependenceInfo::tryDelinearizeFixedSize(
3246 Instruction *Src, Instruction *Dst, const SCEV *SrcAccessFn,
3247 const SCEV *DstAccessFn, SmallVectorImpl<const SCEV *> &SrcSubscripts,
3248 SmallVectorImpl<const SCEV *> &DstSubscripts) {
3249 LLVM_DEBUG({
3250 const SCEVUnknown *SrcBase =
3251 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
3252 const SCEVUnknown *DstBase =
3253 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
3254 assert(SrcBase && DstBase && SrcBase == DstBase &&
3255 "expected src and dst scev unknowns to be equal");
3256 });
3257
3258 const SCEV *ElemSize = SE->getElementSize(Src);
3259 assert(ElemSize == SE->getElementSize(Dst) && "Different element sizes");
3260 SmallVector<const SCEV *, 4> SrcSizes, DstSizes;
3261 if (!delinearizeFixedSizeArray(*SE, SE->removePointerBase(SrcAccessFn),
3262 SrcSubscripts, SrcSizes, ElemSize) ||
3263 !delinearizeFixedSizeArray(*SE, SE->removePointerBase(DstAccessFn),
3264 DstSubscripts, DstSizes, ElemSize))
3265 return false;
3266
3267 // Check that the two size arrays are non-empty and equal in length and
3268 // value. SCEV expressions are uniqued, so we can compare pointers.
3269 if (SrcSizes.size() != DstSizes.size() ||
3270 !std::equal(SrcSizes.begin(), SrcSizes.end(), DstSizes.begin())) {
3271 SrcSubscripts.clear();
3272 DstSubscripts.clear();
3273 return false;
3274 }
3275
3276 assert(SrcSubscripts.size() == DstSubscripts.size() &&
3277 "Expected equal number of entries in the list of SrcSubscripts and "
3278 "DstSubscripts.");
3279
3280 Value *SrcPtr = getLoadStorePointerOperand(Src);
3281 Value *DstPtr = getLoadStorePointerOperand(Dst);
3282
3283 // In general we cannot safely assume that the subscripts recovered from GEPs
3284 // are in the range of values defined for their corresponding array
3285 // dimensions. For example some C language usage/interpretation make it
3286 // impossible to verify this at compile-time. As such we can only delinearize
3287 // iff the subscripts are positive and are less than the range of the
3288 // dimension.
3290 if (!validateDelinearizationResult(*SE, SrcSizes, SrcSubscripts, SrcPtr) ||
3291 !validateDelinearizationResult(*SE, DstSizes, DstSubscripts, DstPtr)) {
3292 SrcSubscripts.clear();
3293 DstSubscripts.clear();
3294 return false;
3295 }
3296 }
3297 LLVM_DEBUG({
3298 dbgs() << "Delinearized subscripts of fixed-size array\n"
3299 << "SrcGEP:" << *SrcPtr << "\n"
3300 << "DstGEP:" << *DstPtr << "\n";
3301 });
3302 return true;
3303}
3304
3305bool DependenceInfo::tryDelinearizeParametricSize(
3306 Instruction *Src, Instruction *Dst, const SCEV *SrcAccessFn,
3307 const SCEV *DstAccessFn, SmallVectorImpl<const SCEV *> &SrcSubscripts,
3308 SmallVectorImpl<const SCEV *> &DstSubscripts) {
3309
3310 Value *SrcPtr = getLoadStorePointerOperand(Src);
3311 Value *DstPtr = getLoadStorePointerOperand(Dst);
3312 const SCEVUnknown *SrcBase =
3313 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
3314 const SCEVUnknown *DstBase =
3315 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
3316 assert(SrcBase && DstBase && SrcBase == DstBase &&
3317 "expected src and dst scev unknowns to be equal");
3318
3319 const SCEV *ElementSize = SE->getElementSize(Src);
3320 if (ElementSize != SE->getElementSize(Dst))
3321 return false;
3322
3323 const SCEV *SrcSCEV = SE->getMinusSCEV(SrcAccessFn, SrcBase);
3324 const SCEV *DstSCEV = SE->getMinusSCEV(DstAccessFn, DstBase);
3325
3326 const SCEVAddRecExpr *SrcAR = dyn_cast<SCEVAddRecExpr>(SrcSCEV);
3327 const SCEVAddRecExpr *DstAR = dyn_cast<SCEVAddRecExpr>(DstSCEV);
3328 if (!SrcAR || !DstAR || !SrcAR->isAffine() || !DstAR->isAffine())
3329 return false;
3330
3331 // First step: collect parametric terms in both array references.
3333 collectParametricTerms(*SE, SrcAR, Terms);
3334 collectParametricTerms(*SE, DstAR, Terms);
3335
3336 // Second step: find subscript sizes.
3338 findArrayDimensions(*SE, Terms, Sizes, ElementSize);
3339
3340 // Third step: compute the access functions for each subscript.
3341 computeAccessFunctions(*SE, SrcAR, SrcSubscripts, Sizes);
3342 computeAccessFunctions(*SE, DstAR, DstSubscripts, Sizes);
3343
3344 // Fail when there is only a subscript: that's a linearized access function.
3345 if (SrcSubscripts.size() < 2 || DstSubscripts.size() < 2 ||
3346 SrcSubscripts.size() != DstSubscripts.size())
3347 return false;
3348
3349 // Statically check that the array bounds are in-range. The first subscript we
3350 // don't have a size for and it cannot overflow into another subscript, so is
3351 // always safe. The others need to be 0 <= subscript[i] < bound, for both src
3352 // and dst.
3353 // FIXME: It may be better to record these sizes and add them as constraints
3354 // to the dependency checks.
3356 if (!validateDelinearizationResult(*SE, Sizes, SrcSubscripts, SrcPtr) ||
3357 !validateDelinearizationResult(*SE, Sizes, DstSubscripts, DstPtr))
3358 return false;
3359
3360 return true;
3361}
3362
3363//===----------------------------------------------------------------------===//
3364
3365#ifndef NDEBUG
3366// For debugging purposes, dump a small bit vector to dbgs().
3368 dbgs() << "{";
3369 for (unsigned VI : BV.set_bits()) {
3370 dbgs() << VI;
3371 if (BV.find_next(VI) >= 0)
3372 dbgs() << ' ';
3373 }
3374 dbgs() << "}\n";
3375}
3376#endif
3377
3379 FunctionAnalysisManager::Invalidator &Inv) {
3380 // Check if the analysis itself has been invalidated.
3381 auto PAC = PA.getChecker<DependenceAnalysis>();
3382 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>())
3383 return true;
3384
3385 // Check transitive dependencies.
3386 return Inv.invalidate<AAManager>(F, PA) ||
3387 Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) ||
3388 Inv.invalidate<LoopAnalysis>(F, PA);
3389}
3390
3391// depends -
3392// Returns NULL if there is no dependence.
3393// Otherwise, return a Dependence with as many details as possible.
3394// Corresponds to Section 3.1 in the paper
3395//
3396// Practical Dependence Testing
3397// Goff, Kennedy, Tseng
3398// PLDI 1991
3399//
3400std::unique_ptr<Dependence>
3402 bool UnderRuntimeAssumptions) {
3404 bool PossiblyLoopIndependent = true;
3405 if (Src == Dst)
3406 PossiblyLoopIndependent = false;
3407
3408 if (!(Src->mayReadOrWriteMemory() && Dst->mayReadOrWriteMemory()))
3409 // if both instructions don't reference memory, there's no dependence
3410 return nullptr;
3411
3412 if (!isLoadOrStore(Src) || !isLoadOrStore(Dst)) {
3413 // can only analyze simple loads and stores, i.e., no calls, invokes, etc.
3414 LLVM_DEBUG(dbgs() << "can only handle simple loads and stores\n");
3415 return std::make_unique<Dependence>(Src, Dst,
3416 SCEVUnionPredicate(Assume, *SE));
3417 }
3418
3419 const MemoryLocation &DstLoc = MemoryLocation::get(Dst);
3420 const MemoryLocation &SrcLoc = MemoryLocation::get(Src);
3421
3422 switch (underlyingObjectsAlias(AA, F->getDataLayout(), DstLoc, SrcLoc)) {
3425 // cannot analyse objects if we don't understand their aliasing.
3426 LLVM_DEBUG(dbgs() << "can't analyze may or partial alias\n");
3427 return std::make_unique<Dependence>(Src, Dst,
3428 SCEVUnionPredicate(Assume, *SE));
3430 // If the objects noalias, they are distinct, accesses are independent.
3431 LLVM_DEBUG(dbgs() << "no alias\n");
3432 return nullptr;
3434 break; // The underlying objects alias; test accesses for dependence.
3435 }
3436
3437 if (DstLoc.Size != SrcLoc.Size || !DstLoc.Size.isPrecise() ||
3438 !SrcLoc.Size.isPrecise()) {
3439 // The dependence test gets confused if the size of the memory accesses
3440 // differ.
3441 LLVM_DEBUG(dbgs() << "can't analyze must alias with different sizes\n");
3442 return std::make_unique<Dependence>(Src, Dst,
3443 SCEVUnionPredicate(Assume, *SE));
3444 }
3445
3446 Value *SrcPtr = getLoadStorePointerOperand(Src);
3447 Value *DstPtr = getLoadStorePointerOperand(Dst);
3448 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
3449 const SCEV *DstSCEV = SE->getSCEV(DstPtr);
3450 LLVM_DEBUG(dbgs() << " SrcSCEV = " << *SrcSCEV << "\n");
3451 LLVM_DEBUG(dbgs() << " DstSCEV = " << *DstSCEV << "\n");
3452 const SCEV *SrcBase = SE->getPointerBase(SrcSCEV);
3453 const SCEV *DstBase = SE->getPointerBase(DstSCEV);
3454 if (SrcBase != DstBase) {
3455 // If two pointers have different bases, trying to analyze indexes won't
3456 // work; we can't compare them to each other. This can happen, for example,
3457 // if one is produced by an LCSSA PHI node.
3458 //
3459 // We check this upfront so we don't crash in cases where getMinusSCEV()
3460 // returns a SCEVCouldNotCompute.
3461 LLVM_DEBUG(dbgs() << "can't analyze SCEV with different pointer base\n");
3462 return std::make_unique<Dependence>(Src, Dst,
3463 SCEVUnionPredicate(Assume, *SE));
3464 }
3465
3466 // Even if the base pointers are the same, they may not be loop-invariant. It
3467 // could lead to incorrect results, as we're analyzing loop-carried
3468 // dependencies. Src and Dst can be in different loops, so we need to check
3469 // the base pointer is invariant in both loops.
3470 Loop *SrcLoop = LI->getLoopFor(Src->getParent());
3471 Loop *DstLoop = LI->getLoopFor(Dst->getParent());
3472 if (!isLoopInvariant(SrcBase, SrcLoop) ||
3473 !isLoopInvariant(DstBase, DstLoop)) {
3474 LLVM_DEBUG(dbgs() << "The base pointer is not loop invariant.\n");
3475 return std::make_unique<Dependence>(Src, Dst,
3476 SCEVUnionPredicate(Assume, *SE));
3477 }
3478
3479 uint64_t EltSize = SrcLoc.Size.toRaw();
3480 const SCEV *SrcEv = SE->getMinusSCEV(SrcSCEV, SrcBase);
3481 const SCEV *DstEv = SE->getMinusSCEV(DstSCEV, DstBase);
3482
3483 // Check that memory access offsets are multiples of element sizes.
3484 if (!SE->isKnownMultipleOf(SrcEv, EltSize, Assume) ||
3485 !SE->isKnownMultipleOf(DstEv, EltSize, Assume)) {
3486 LLVM_DEBUG(dbgs() << "can't analyze SCEV with different offsets\n");
3487 return std::make_unique<Dependence>(Src, Dst,
3488 SCEVUnionPredicate(Assume, *SE));
3489 }
3490
3491 // Runtime assumptions needed but not allowed.
3492 if (!Assume.empty() && !UnderRuntimeAssumptions)
3493 return std::make_unique<Dependence>(Src, Dst,
3494 SCEVUnionPredicate(Assume, *SE));
3495
3496 unsigned Pairs = 1;
3497 SmallVector<Subscript, 2> Pair(Pairs);
3498 Pair[0].Src = SrcEv;
3499 Pair[0].Dst = DstEv;
3500
3501 SCEVMonotonicityChecker MonChecker(SE);
3502 const Loop *OutermostLoop = SrcLoop ? SrcLoop->getOutermostLoop() : nullptr;
3504 if (MonChecker.checkMonotonicity(Pair[0].Src, OutermostLoop).isUnknown() ||
3505 MonChecker.checkMonotonicity(Pair[0].Dst, OutermostLoop).isUnknown())
3506 return std::make_unique<Dependence>(Src, Dst,
3507 SCEVUnionPredicate(Assume, *SE));
3508
3509 if (Delinearize) {
3510 if (tryDelinearize(Src, Dst, Pair)) {
3511 LLVM_DEBUG(dbgs() << " delinearized\n");
3512 Pairs = Pair.size();
3513 }
3514 }
3515
3516 // Establish loop nesting levels considering SameSD loops as common
3517 establishNestingLevels(Src, Dst);
3518
3519 LLVM_DEBUG(dbgs() << " common nesting levels = " << CommonLevels << "\n");
3520 LLVM_DEBUG(dbgs() << " maximum nesting levels = " << MaxLevels << "\n");
3521 LLVM_DEBUG(dbgs() << " SameSD nesting levels = " << SameSDLevels << "\n");
3522
3523 // Modify common levels to consider the SameSD levels in the tests
3524 CommonLevels += SameSDLevels;
3525 MaxLevels -= SameSDLevels;
3526 if (SameSDLevels > 0) {
3527 // Not all tests are handled yet over SameSD loops
3528 // Revoke if there are any tests other than ZIV, SIV or RDIV
3529 for (unsigned P = 0; P < Pairs; ++P) {
3531 Subscript::ClassificationKind TestClass =
3532 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()),
3533 Pair[P].Dst, LI->getLoopFor(Dst->getParent()), Loops);
3534
3535 if (TestClass != Subscript::ZIV && TestClass != Subscript::SIV &&
3536 TestClass != Subscript::RDIV) {
3537 // Revert the levels to not consider the SameSD levels
3538 CommonLevels -= SameSDLevels;
3539 MaxLevels += SameSDLevels;
3540 SameSDLevels = 0;
3541 break;
3542 }
3543 }
3544 }
3545
3546 if (SameSDLevels > 0)
3547 SameSDLoopsCount++;
3548
3549 FullDependence Result(Src, Dst, SCEVUnionPredicate(Assume, *SE),
3550 PossiblyLoopIndependent, CommonLevels);
3551 ++TotalArrayPairs;
3552
3553 for (unsigned P = 0; P < Pairs; ++P) {
3554 assert(Pair[P].Src->getType()->isIntegerTy() && "Src must be an integer");
3555 assert(Pair[P].Dst->getType()->isIntegerTy() && "Dst must be an integer");
3556 Pair[P].Loops.resize(MaxLevels + 1);
3557 Pair[P].GroupLoops.resize(MaxLevels + 1);
3558 Pair[P].Group.resize(Pairs);
3559 removeMatchingExtensions(&Pair[P]);
3560 Pair[P].Classification =
3561 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()), Pair[P].Dst,
3562 LI->getLoopFor(Dst->getParent()), Pair[P].Loops);
3563 Pair[P].GroupLoops = Pair[P].Loops;
3564 Pair[P].Group.set(P);
3565 LLVM_DEBUG(dbgs() << " subscript " << P << "\n");
3566 LLVM_DEBUG(dbgs() << "\tsrc = " << *Pair[P].Src << "\n");
3567 LLVM_DEBUG(dbgs() << "\tdst = " << *Pair[P].Dst << "\n");
3568 LLVM_DEBUG(dbgs() << "\tclass = " << Pair[P].Classification << "\n");
3569 LLVM_DEBUG(dbgs() << "\tloops = ");
3571 }
3572
3573 // Test each subscript individually
3574 for (unsigned SI = 0; SI < Pairs; ++SI) {
3575 LLVM_DEBUG(dbgs() << "testing subscript " << SI);
3576 switch (Pair[SI].Classification) {
3577 case Subscript::NonLinear:
3578 // ignore these, but collect loops for later
3579 ++NonlinearSubscriptPairs;
3580 collectCommonLoops(Pair[SI].Src, LI->getLoopFor(Src->getParent()),
3581 Pair[SI].Loops);
3582 collectCommonLoops(Pair[SI].Dst, LI->getLoopFor(Dst->getParent()),
3583 Pair[SI].Loops);
3584 Result.Consistent = false;
3585 break;
3586 case Subscript::ZIV:
3587 LLVM_DEBUG(dbgs() << ", ZIV\n");
3588 if (testZIV(Pair[SI].Src, Pair[SI].Dst, Result))
3589 return nullptr;
3590 break;
3591 case Subscript::SIV: {
3592 LLVM_DEBUG(dbgs() << ", SIV\n");
3593 unsigned Level;
3594 if (testSIV(Pair[SI].Src, Pair[SI].Dst, Level, Result,
3595 UnderRuntimeAssumptions))
3596 return nullptr;
3597 break;
3598 }
3599 case Subscript::RDIV:
3600 LLVM_DEBUG(dbgs() << ", RDIV\n");
3601 if (testRDIV(Pair[SI].Src, Pair[SI].Dst, Result))
3602 return nullptr;
3603 break;
3604 case Subscript::MIV:
3605 LLVM_DEBUG(dbgs() << ", MIV\n");
3606 if (testMIV(Pair[SI].Src, Pair[SI].Dst, Pair[SI].Loops, Result))
3607 return nullptr;
3608 break;
3609 }
3610 }
3611
3612 // Make sure the Scalar flags are set correctly.
3613 SmallBitVector CompleteLoops(MaxLevels + 1);
3614 for (unsigned SI = 0; SI < Pairs; ++SI)
3615 CompleteLoops |= Pair[SI].Loops;
3616 for (unsigned II = 1; II <= CommonLevels; ++II)
3617 if (CompleteLoops[II])
3618 Result.DV[II - 1].Scalar = false;
3619
3620 // Set the distance to zero if the direction is EQ.
3621 // TODO: Ideally, the distance should be set to 0 immediately simultaneously
3622 // with the corresponding direction being set to EQ.
3623 for (unsigned II = 1; II <= Result.getLevels(); ++II) {
3624 if (Result.getDirection(II) == Dependence::DVEntry::EQ) {
3625 if (Result.DV[II - 1].Distance == nullptr)
3626 Result.DV[II - 1].Distance = SE->getZero(SrcSCEV->getType());
3627 else
3628 assert(Result.DV[II - 1].Distance->isZero() &&
3629 "Inconsistency between distance and direction");
3630 }
3631
3632#ifndef NDEBUG
3633 // Check that the converse (i.e., if the distance is zero, then the
3634 // direction is EQ) holds.
3635 const SCEV *Distance = Result.getDistance(II);
3636 if (Distance && Distance->isZero())
3637 assert(Result.getDirection(II) == Dependence::DVEntry::EQ &&
3638 "Distance is zero, but direction is not EQ");
3639#endif
3640 }
3641
3642 if (SameSDLevels > 0) {
3643 // Extracting SameSD levels from the common levels
3644 // Reverting CommonLevels and MaxLevels to their original values
3645 assert(CommonLevels >= SameSDLevels);
3646 CommonLevels -= SameSDLevels;
3647 MaxLevels += SameSDLevels;
3648 std::unique_ptr<FullDependence::DVEntry[]> DV, DVSameSD;
3649 DV = std::make_unique<FullDependence::DVEntry[]>(CommonLevels);
3650 DVSameSD = std::make_unique<FullDependence::DVEntry[]>(SameSDLevels);
3651 for (unsigned Level = 0; Level < CommonLevels; ++Level)
3652 DV[Level] = Result.DV[Level];
3653 for (unsigned Level = 0; Level < SameSDLevels; ++Level)
3654 DVSameSD[Level] = Result.DV[CommonLevels + Level];
3655 Result.DV = std::move(DV);
3656 Result.DVSameSD = std::move(DVSameSD);
3657 Result.Levels = CommonLevels;
3658 Result.SameSDLevels = SameSDLevels;
3659 // Result is not consistent if it considers SameSD levels
3660 Result.Consistent = false;
3661 }
3662
3663 if (PossiblyLoopIndependent) {
3664 // Make sure the LoopIndependent flag is set correctly.
3665 // All directions must include equal, otherwise no
3666 // loop-independent dependence is possible.
3667 for (unsigned II = 1; II <= CommonLevels; ++II) {
3668 if (!(Result.getDirection(II) & Dependence::DVEntry::EQ)) {
3669 Result.LoopIndependent = false;
3670 break;
3671 }
3672 }
3673 } else {
3674 // On the other hand, if all directions are equal and there's no
3675 // loop-independent dependence possible, then no dependence exists.
3676 // However, if there are runtime assumptions, we must return the result.
3677 bool AllEqual = true;
3678 for (unsigned II = 1; II <= CommonLevels; ++II) {
3679 if (Result.getDirection(II) != Dependence::DVEntry::EQ) {
3680 AllEqual = false;
3681 break;
3682 }
3683 }
3684 if (AllEqual && Result.Assumptions.getPredicates().empty())
3685 return nullptr;
3686 }
3687
3688 return std::make_unique<FullDependence>(std::move(Result));
3689}
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)
static cl::opt< DependenceTestType > EnableDependenceTest("da-enable-dependence-test", cl::init(DependenceTestType::All), cl::ReallyHidden, cl::desc("Run only specified dependence test routine and disable others. " "The purpose is mainly to exclude the influence of other " "dependence test routines in regression tests. If set to All, all " "dependence test routines are enabled."), cl::values(clEnumValN(DependenceTestType::All, "all", "Enable all dependence test routines."), clEnumValN(DependenceTestType::StrongSIV, "strong-siv", "Enable only Strong SIV test."), clEnumValN(DependenceTestType::WeakCrossingSIV, "weak-crossing-siv", "Enable only Weak-Crossing SIV test."), clEnumValN(DependenceTestType::ExactSIV, "exact-siv", "Enable only Exact SIV test."), clEnumValN(DependenceTestType::WeakZeroSIV, "weak-zero-siv", "Enable only Weak-Zero SIV test."), clEnumValN(DependenceTestType::ExactRDIV, "exact-rdiv", "Enable only Exact RDIV test."), clEnumValN(DependenceTestType::SymbolicRDIV, "symbolic-rdiv", "Enable only Symbolic RDIV test."), clEnumValN(DependenceTestType::GCDMIV, "gcd-miv", "Enable only GCD MIV test."), clEnumValN(DependenceTestType::BanerjeeMIV, "banerjee-miv", "Enable only Banerjee MIV test.")))
static bool isLoadOrStore(const Instruction *I)
static void dumpExampleDependence(raw_ostream &OS, DependenceInfo *DA, ScalarEvolution &SE, LoopInfo &LI, bool NormalizeResults)
static bool isDependenceTestEnabled(DependenceTestType Test)
Returns true iff Test is enabled.
static bool findGCD(unsigned Bits, const APInt &AM, const APInt &BM, const APInt &Delta, APInt &G, APInt &X, APInt &Y)
static void dumpSmallBitVector(SmallBitVector &BV)
static APInt ceilingOfQuotient(const APInt &A, const APInt &B)
static APInt floorOfQuotient(const APInt &A, const APInt &B)
static const SCEV * minusSCEVNoSignedOverflow(const SCEV *A, const SCEV *B, ScalarEvolution &SE)
Returns A - B if it guaranteed not to signed wrap.
static AliasResult underlyingObjectsAlias(AAResults *AA, const DataLayout &DL, const MemoryLocation &LocA, const MemoryLocation &LocB)
static std::pair< std::optional< APInt >, std::optional< APInt > > inferDomainOfAffine(const APInt &A, const APInt &B, const std::optional< APInt > &UB)
Given an affine expression of the form A*k + B, where k is an arbitrary integer, infer the possible r...
static std::optional< APInt > getConstantCoefficient(const SCEV *Expr)
Given a SCEVMulExpr, returns its first operand if its first operand is a constant and the product doe...
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.
Loop::LoopBounds::Direction Direction
Definition LoopInfo.cpp:231
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
#define T
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
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:40
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 std::unique_ptr< Dependence > depends(Instruction *Src, Instruction *Dst, bool UnderRuntimeAssumptions=false)
depends - Tests for a dependence between the Src and Dst instructions.
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 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 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.
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
This class represents a composition of other SCEV predicates, and is the class that most clients will...
This class represents an analyzed expression in the program.
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 ...
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.
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.
#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
@ 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.
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
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)...
bool delinearizeFixedSizeArray(ScalarEvolution &SE, const SCEV *Expr, SmallVectorImpl< const SCEV * > &Subscripts, SmallVectorImpl< const SCEV * > &Sizes, const SCEV *ElementSize)
Split this SCEVAddRecExpr into two vectors of SCEVs representing the subscripts and sizes of an acces...
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()).
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.
bool validateDelinearizationResult(ScalarEvolution &SE, ArrayRef< const SCEV * > Sizes, ArrayRef< const SCEV * > Subscripts, const Value *Ptr=nullptr)
Check that each subscript in Subscripts is within the corresponding size in Sizes.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
LLVM_ABI FunctionPass * createDependenceAnalysisWrapperPass()
createDependenceAnalysisPass - This creates an instance of the DependenceAnalysis wrapper pass.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:869
#define N
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
Dependence::DVEntry - Each level in the distance/direction vector has a direction (or perhaps a union...
This class defines a simple visitor class that may be used for various SCEV analysis purposes.