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