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