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