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