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