65#define DEBUG_TYPE "da"
71STATISTIC(NonlinearSubscriptPairs,
"Nonlinear subscript pairs");
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");
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");
91STATISTIC(BanerjeeApplications,
"Banerjee applications");
92STATISTIC(BanerjeeIndependence,
"Banerjee independence");
94STATISTIC(SameSDLoopsCount,
"Loops with Same iteration Space and Depth");
123struct OverflowSafeSignedAPInt {
124 OverflowSafeSignedAPInt() :
Value(
std::nullopt) {}
126 OverflowSafeSignedAPInt(
const std::optional<APInt> &
V) :
Value(
V) {}
128 OverflowSafeSignedAPInt
operator+(
const OverflowSafeSignedAPInt &
RHS)
const {
130 return OverflowSafeSignedAPInt();
134 return OverflowSafeSignedAPInt();
135 return OverflowSafeSignedAPInt(Result);
140 return OverflowSafeSignedAPInt();
141 return *
this + fromInt(
RHS);
144 OverflowSafeSignedAPInt
operator-(
const OverflowSafeSignedAPInt &
RHS)
const {
146 return OverflowSafeSignedAPInt();
150 return OverflowSafeSignedAPInt();
151 return OverflowSafeSignedAPInt(Result);
156 return OverflowSafeSignedAPInt();
157 return *
this - fromInt(
RHS);
160 OverflowSafeSignedAPInt
operator*(
const OverflowSafeSignedAPInt &
RHS)
const {
162 return OverflowSafeSignedAPInt();
166 return OverflowSafeSignedAPInt();
167 return OverflowSafeSignedAPInt(Result);
170 OverflowSafeSignedAPInt
operator-()
const {
172 return OverflowSafeSignedAPInt();
173 if (
Value->isMinSignedValue())
174 return OverflowSafeSignedAPInt();
175 return OverflowSafeSignedAPInt(-*
Value);
178 operator bool()
const {
return Value.has_value(); }
187 const APInt *operator->()
const {
195 std::optional<APInt>
Value;
197 OverflowSafeSignedAPInt fromInt(
uint64_t V)
const {
199 return OverflowSafeSignedAPInt(
200 APInt(
Value->getBitWidth(),
V,
true));
212 bool NormalizeResults) {
213 auto *
F = DA->getFunction();
217 if (SrcI->mayReadOrWriteMemory()) {
220 if (DstI->mayReadOrWriteMemory()) {
221 OS <<
"Src:" << *SrcI <<
" --> Dst:" << *DstI <<
"\n";
222 OS <<
" da analyze - ";
223 if (
auto D = DA->depends(&*SrcI, &*DstI,
229 for (
unsigned Level = 1; Level <=
D->getLevels(); Level++) {
230 const SCEV *Distance =
D->getDistance(Level);
231 bool IsDistanceZero = Distance && Distance->
isZero();
234 assert(IsDistanceZero == IsDirectionEQ &&
235 "Inconsistent distance and direction.");
240 if (NormalizeResults &&
D->normalize(&SE))
241 OS <<
"normalized - ";
253 static_cast<PassInfoMixin<DependenceAnalysisPrinterPass> *
>(
this)
255 if (NormalizeResults)
256 OS <<
"<normalized-results>";
261 OS <<
"Printing analysis 'Dependence Analysis' for function '" <<
F.getName()
274 return Src->mayReadFromMemory() &&
Dst->mayReadFromMemory();
279 return Src->mayWriteToMemory() &&
Dst->mayWriteToMemory();
284 return Src->mayWriteToMemory() &&
Dst->mayReadFromMemory();
289 return Src->mayReadFromMemory() &&
Dst->mayWriteToMemory();
303 bool PossiblyLoopIndependent,
304 unsigned CommonLevels)
305 :
Dependence(Source, Destination, Assumes), Levels(CommonLevels),
306 LoopIndependent(PossiblyLoopIndependent) {
309 DV = std::make_unique<
DVEntry[]>(CommonLevels);
328 for (
unsigned Level = 1; Level <= Levels; ++Level) {
329 unsigned char Direction = DV[Level - 1].Direction;
342 for (
unsigned Level = 1; Level <= Levels; ++Level) {
343 unsigned char Direction = DV[Level - 1].Direction;
351 DV[Level - 1].Direction = RevDirection;
353 if (DV[Level - 1].Distance !=
nullptr)
362 LLVM_DEBUG(
dbgs() <<
"Before normalizing negative direction vectors:\n";
365 LLVM_DEBUG(
dbgs() <<
"After normalizing negative direction vectors:\n";
395 assert(0 < Level && Level <=
static_cast<unsigned>(Levels) + SameSDLevels &&
396 "Level out of range");
397 return Level > Levels;
418 if (SameSDLevels > 0) {
419 OS <<
" / assuming " << SameSDLevels <<
" loop level(s) fused: ";
426 if (!Assumptions.isAlwaysTrue()) {
427 OS <<
" Runtime Assumptions:\n";
428 Assumptions.print(OS, 2);
437 bool OnSameSD =
false;
438 unsigned LevelNum = Levels;
440 LevelNum += SameSDLevels;
442 for (
unsigned II = 1;
II <= LevelNum; ++
II) {
475 if (!ScopeList || LoopAliasScopes.
empty())
481 return LoopAliasScopes.contains(cast<MDNode>(Scope));
541 return LI->isUnordered();
543 return SI->isUnordered();
551bool DependenceInfo::haveSameSD(
const Loop *SrcLoop,
552 const Loop *DstLoop)
const {
553 if (SrcLoop == DstLoop)
563 const SCEV *SrcUB = SE->getBackedgeTakenCount(SrcLoop);
564 const SCEV *DstUB = SE->getBackedgeTakenCount(DstLoop);
569 SrcUB = SE->getNoopOrZeroExtend(SrcUB, WiderType);
570 DstUB = SE->getNoopOrZeroExtend(DstUB, WiderType);
642void DependenceInfo::establishNestingLevels(
const Instruction *Src,
644 const BasicBlock *SrcBlock = Src->getParent();
645 const BasicBlock *DstBlock = Dst->getParent();
646 unsigned SrcLevel = LI->getLoopDepth(SrcBlock);
647 unsigned DstLevel = LI->getLoopDepth(DstBlock);
648 const Loop *SrcLoop = LI->getLoopFor(SrcBlock);
649 const Loop *DstLoop = LI->getLoopFor(DstBlock);
650 SrcLevels = SrcLevel;
651 MaxLevels = SrcLevel + DstLevel;
653 while (SrcLevel > DstLevel) {
657 while (DstLevel > SrcLevel) {
662 const Loop *SrcUncommonFrontier =
nullptr, *DstUncommonFrontier =
nullptr;
665 while (SrcLoop != DstLoop) {
666 SrcUncommonFrontier = SrcLoop;
667 DstUncommonFrontier = DstLoop;
672 if (SrcUncommonFrontier && DstUncommonFrontier &&
673 haveSameSD(SrcUncommonFrontier, DstUncommonFrontier))
675 CommonLevels = SrcLevel;
676 MaxLevels -= CommonLevels;
681unsigned DependenceInfo::mapSrcLoop(
const Loop *SrcLoop)
const {
687unsigned DependenceInfo::mapDstLoop(
const Loop *DstLoop)
const {
689 if (
D > CommonLevels)
692 return D - CommonLevels + SrcLevels;
719 if (Level <= CommonLevels && !SE->isLoopInvariant(Expression, LoopNest))
731 return isLoopInvariant(Expr, LoopNest);
738 const Loop *
L = LoopNest;
739 while (L && AddRec->
getLoop() != L)
740 L =
L->getParentLoop();
749 if (!isLoopInvariant(Step, LoopNest))
755 return checkSubscript(Start, LoopNest,
Loops, IsSrc);
760bool DependenceInfo::checkSrcSubscript(
const SCEV *Src,
const Loop *
LoopNest,
762 return checkSubscript(Src, LoopNest,
Loops,
true);
767bool DependenceInfo::checkDstSubscript(
const SCEV *Dst,
const Loop *
LoopNest,
769 return checkSubscript(Dst, LoopNest,
Loops,
false);
775DependenceInfo::Subscript::ClassificationKind
776DependenceInfo::classifyPair(
const SCEV *Src,
const Loop *SrcLoopNest,
777 const SCEV *Dst,
const Loop *DstLoopNest,
779 SmallBitVector SrcLoops(MaxLevels + 1);
780 SmallBitVector DstLoops(MaxLevels + 1);
781 if (!checkSrcSubscript(Src, SrcLoopNest, SrcLoops))
782 return Subscript::NonLinear;
783 if (!checkDstSubscript(Dst, DstLoopNest, DstLoops))
784 return Subscript::NonLinear;
787 unsigned N =
Loops.count();
789 return Subscript::ZIV;
791 return Subscript::SIV;
792 if (
N == 2 && SrcLoops.count() == 1 && DstLoops.count() == 1)
793 return Subscript::RDIV;
794 return Subscript::MIV;
804const SCEV *DependenceInfo::collectUpperBound(
const Loop *L,
Type *
T)
const {
805 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
806 const SCEV *UB = SE->getBackedgeTakenCount(L);
807 return SE->getTruncateOrZeroExtend(UB,
T);
815DependenceInfo::collectNonNegativeConstantUpperBound(
const Loop *L,
817 if (
const SCEV *UB = collectUpperBound(L,
T))
819 APInt Res =
C->getAPInt();
845 return Opts.da_enable_dependence_test ==
Test;
858bool DependenceInfo::testZIV(
const SCEV *Src,
const SCEV *Dst,
906 bool UnderRuntimeAssumptions) {
910 const SCEV *Coeff = Src->getStepRecurrence(*SE);
911 assert(Coeff == Dst->getStepRecurrence(*SE) &&
912 "Expecting same coefficient in Strong SIV test");
913 const SCEV *SrcConst = Src->getStart();
914 const SCEV *DstConst = Dst->getStart();
922 ++StrongSIVapplications;
923 assert(0 < Level && Level <= CommonLevels &&
"level out of range");
936 APInt Distance = ConstDelta;
937 APInt Remainder = ConstDelta;
942 if (Remainder != 0) {
944 ++StrongSIVindependence;
945 ++StrongSIVsuccesses;
948 Result.DV[
Level].Distance = SE->getConstant(Distance);
951 else if (Distance.
slt(0))
955 ++StrongSIVsuccesses;
956 }
else if (Delta->
isZero()) {
960 if (SE->isKnownNonZero(Coeff)) {
962 dbgs() <<
"\t Coefficient proven non-zero by SCEV analysis\n");
965 if (UnderRuntimeAssumptions) {
966 const SCEVPredicate *
Pred = SE->getComparePredicate(
974 LLVM_DEBUG(
dbgs() <<
"\t Would need runtime assumption " << *Coeff
975 <<
" != 0, but not allowed. Failing this test.\n");
982 ++StrongSIVsuccesses;
984 if (Coeff->
isOne()) {
990 bool DeltaMaybeZero = !SE->isKnownNonZero(Delta);
991 bool DeltaMaybePositive = !SE->isKnownNonPositive(Delta);
992 bool DeltaMaybeNegative = !SE->isKnownNonNegative(Delta);
993 bool CoeffMaybePositive = !SE->isKnownNonPositive(Coeff);
994 bool CoeffMaybeNegative = !SE->isKnownNonNegative(Coeff);
999 if ((DeltaMaybePositive && CoeffMaybePositive) ||
1000 (DeltaMaybeNegative && CoeffMaybeNegative))
1004 if ((DeltaMaybeNegative && CoeffMaybePositive) ||
1005 (DeltaMaybePositive && CoeffMaybeNegative))
1007 if (NewDirection <
Result.DV[Level].Direction)
1008 ++StrongSIVsuccesses;
1042bool DependenceInfo::weakCrossingSIVtest(
const SCEVAddRecExpr *Src,
1049 const SCEV *Coeff = Src->getStepRecurrence(*SE);
1050 const SCEV *SrcConst = Src->getStart();
1051 const SCEV *DstConst = Dst->getStart();
1053 assert(Coeff == SE->getNegativeSCEV(Dst->getStepRecurrence(*SE)) &&
1054 "Unexpected input for weakCrossingSIVtest");
1060 ++WeakCrossingSIVapplications;
1061 assert(0 < Level && Level <= CommonLevels &&
"Level out of range");
1076 ConstantRange SrcRange = SE->getSignedRange(Src);
1077 ConstantRange DstRange = SE->getSignedRange(Dst);
1082 Result.DV[
Level].Direction &= ~Dependence::DVEntry::LT;
1083 Result.DV[
Level].Direction &= ~Dependence::DVEntry::GT;
1084 ++WeakCrossingSIVsuccesses;
1085 if (!
Result.DV[Level].Direction) {
1086 ++WeakCrossingSIVindependence;
1094 APInt APDelta = ConstDelta->
getAPInt();
1095 APInt APCoeff = ConstCoeff->
getAPInt();
1096 APInt Distance = APDelta;
1097 APInt Remainder = APDelta;
1100 if (Remainder != 0) {
1102 ++WeakCrossingSIVindependence;
1103 ++WeakCrossingSIVsuccesses;
1111 Result.DV[
Level].Direction &= ~Dependence::DVEntry::EQ;
1112 ++WeakCrossingSIVsuccesses;
1136 APInt A0(Bits, 1,
true), A1(Bits, 0,
true);
1137 APInt B0(Bits, 0,
true), B1(Bits, 1,
true);
1141 return std::nullopt;
1150 APInt A2 = A0 - Q*A1; A0 = A1; A1 = A2;
1151 APInt B2 = B0 - Q*B1; B0 = B1; B1 = B2;
1158 X = AM.
slt(0) ? -A1 : A1;
1159 Y = BM.
slt(0) ? B1 : -B1;
1169static OverflowSafeSignedAPInt
1171 const OverflowSafeSignedAPInt &OB) {
1173 return OverflowSafeSignedAPInt();
1182 if ((
A.sgt(0) &&
B.sgt(0)) || (
A.slt(0) &&
B.slt(0)))
1184 return OverflowSafeSignedAPInt(Q) - 1;
1187static OverflowSafeSignedAPInt
1189 const OverflowSafeSignedAPInt &OB) {
1191 return OverflowSafeSignedAPInt();
1200 if ((
A.sgt(0) &&
B.sgt(0)) || (
A.slt(0) &&
B.slt(0)))
1201 return OverflowSafeSignedAPInt(Q) + 1;
1235static std::pair<OverflowSafeSignedAPInt, OverflowSafeSignedAPInt>
1237 OverflowSafeSignedAPInt UB) {
1238 assert(
A &&
B &&
"A and B must be available");
1239 assert(*
A != 0 &&
"A must be non-zero");
1240 assert((!UB || UB->isNonNegative()) &&
"UB must be non-negative");
1241 OverflowSafeSignedAPInt TL, TU;
1244 LLVM_DEBUG(
if (TL)
dbgs() <<
"\t Possible TL = " << *TL <<
"\n");
1248 LLVM_DEBUG(
if (TU)
dbgs() <<
"\t Possible TU = " << *TU <<
"\n");
1251 LLVM_DEBUG(
if (TU)
dbgs() <<
"\t Possible TU = " << *TU <<
"\n");
1255 LLVM_DEBUG(
if (TL)
dbgs() <<
"\t Possible TL = " << *TL <<
"\n");
1257 return std::make_pair(TL, TU);
1286 ++ExactSIVapplications;
1287 assert(0 < Level && Level <= CommonLevels &&
"Level out of range");
1289 bool Res = exactTestImpl(Src, Dst, Result, Level);
1291 ++ExactSIVsuccesses;
1292 ++ExactSIVindependence;
1302 return ConstDividend.
srem(ConstDivisor) == 0;
1305bool DependenceInfo::weakZeroSIVtestImpl(
const SCEVAddRecExpr *AR,
1306 const SCEV *Const,
unsigned Level,
1309 const SCEV *ARConst = AR->
getStart();
1311 if (Const == ARConst && SE->isKnownNonZero(ARCoeff)) {
1312 if (Level < CommonLevels) {
1314 ++WeakZeroSIVsuccesses;
1326 if (
const SCEV *UpperBound =
1329 bool OverlapAtLast = [&] {
1330 if (!SE->isKnownNonZero(ConstCoeff))
1335 if (OverlapAtLast) {
1337 if (Level < CommonLevels) {
1339 ++WeakZeroSIVsuccesses;
1348 ++WeakZeroSIVindependence;
1349 ++WeakZeroSIVsuccesses;
1384bool DependenceInfo::weakZeroSrcSIVtest(
const SCEV *SrcConst,
1394 [[maybe_unused]]
const SCEV *DstCoeff = Dst->getStepRecurrence(*SE);
1395 [[maybe_unused]]
const SCEV *DstConst = Dst->getStart();
1400 ++WeakZeroSIVapplications;
1401 assert(0 < Level && Level <= MaxLevels &&
"Level out of range");
1410 bool Res = weakZeroSIVtestImpl(Dst, SrcConst, Level, Result);
1444bool DependenceInfo::weakZeroDstSIVtest(
const SCEVAddRecExpr *Src,
1445 const SCEV *DstConst,
unsigned Level,
1452 [[maybe_unused]]
const SCEV *SrcCoeff = Src->getStepRecurrence(*SE);
1453 [[maybe_unused]]
const SCEV *SrcConst = Src->getStart();
1458 ++WeakZeroSIVapplications;
1459 assert(0 < Level && Level <= SrcLevels &&
"Level out of range");
1462 return weakZeroSIVtestImpl(Src, DstConst, Level, Result);
1478 ++ExactRDIVapplications;
1479 bool Res = exactTestImpl(Src, Dst, Result, std::nullopt);
1481 ++ExactRDIVindependence;
1488 std::optional<unsigned> Level)
const {
1489 const SCEV *SrcCoeff = Src->getStepRecurrence(*SE);
1490 const SCEV *SrcConst = Src->getStart();
1491 const SCEV *DstCoeff = Dst->getStepRecurrence(*SE);
1492 const SCEV *DstConst = Dst->getStart();
1505 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
1510 APInt AM = ConstSrcCoeff->
getAPInt();
1511 APInt BM = ConstDstCoeff->
getAPInt();
1514 std::optional<bool> GCDRes =
findGCD(Bits, AM, BM, CM,
G,
X,
Y);
1527 std::optional<APInt> SrcUM =
1528 collectNonNegativeConstantUpperBound(Src->getLoop(), Delta->
getType());
1532 std::optional<APInt> DstUM =
1533 collectNonNegativeConstantUpperBound(Dst->getLoop(), Delta->
getType());
1537 OverflowSafeSignedAPInt TC = CM.
sdiv(
G);
1538 OverflowSafeSignedAPInt TX = OverflowSafeSignedAPInt(
X) * TC;
1539 OverflowSafeSignedAPInt TY = OverflowSafeSignedAPInt(
Y) * TC;
1540 if (!TC || !TX || !TY)
1564 auto GetMaxOrMin = [](
const OverflowSafeSignedAPInt &
V0,
1565 const OverflowSafeSignedAPInt &
V1,
1566 bool IsMin) -> std::optional<APInt> {
1573 return std::nullopt;
1576 std::optional<APInt> OptTL = GetMaxOrMin(TL0, TL1,
false);
1577 std::optional<APInt> OptTU = GetMaxOrMin(TU0, TU1,
true);
1578 if (!OptTL || !OptTU)
1581 APInt TL = std::move(*OptTL);
1582 APInt TU = std::move(*OptTU);
1591 assert(SrcUM == DstUM &&
"Expecting same upper bound for Src and Dst");
1595 OverflowSafeSignedAPInt LowerDistance, UpperDistance;
1596 OverflowSafeSignedAPInt OTY(TY), OTX(TX), OTA(TA), OTB(TB), OTL(TL), OTU(TU);
1600 LowerDistance = (OTY - OTX) + (OTA - OTB) * OTL;
1601 UpperDistance = (OTY - OTX) + (OTA - OTB) * OTU;
1603 LowerDistance = (OTY - OTX) + (OTA - OTB) * OTU;
1604 UpperDistance = (OTY - OTX) + (OTA - OTB) * OTL;
1607 if (!LowerDistance || !UpperDistance)
1610 LLVM_DEBUG(
dbgs() <<
"\t LowerDistance = " << *LowerDistance <<
"\n");
1611 LLVM_DEBUG(
dbgs() <<
"\t UpperDistance = " << *UpperDistance <<
"\n");
1613 if (LowerDistance->sle(0) && UpperDistance->sge(0))
1615 if (LowerDistance->slt(0))
1617 if (UpperDistance->sgt(0))
1635bool DependenceInfo::testSIV(
const SCEV *Src,
const SCEV *Dst,
unsigned &Level,
1637 bool UnderRuntimeAssumptions) {
1642 if (SrcAddRec && DstAddRec) {
1646 [[maybe_unused]]
const Loop *CurDstLoop = DstAddRec->
getLoop();
1647 assert(haveSameSD(CurSrcLoop, CurDstLoop) &&
1648 "Loops in the SIV test should have the same iteration space and "
1650 Level = mapSrcLoop(CurSrcLoop);
1651 bool disproven =
false;
1652 if (SrcCoeff == DstCoeff)
1653 disproven = strongSIVtest(SrcAddRec, DstAddRec, Level, Result,
1654 UnderRuntimeAssumptions);
1655 else if (SrcCoeff == SE->getNegativeSCEV(DstCoeff))
1656 disproven = weakCrossingSIVtest(SrcAddRec, DstAddRec, Level, Result);
1657 return disproven || exactSIVtest(SrcAddRec, DstAddRec, Level, Result);
1661 Level = mapSrcLoop(CurSrcLoop);
1662 return weakZeroDstSIVtest(SrcAddRec, Dst, Level, Result);
1666 Level = mapDstLoop(CurDstLoop);
1667 return weakZeroSrcSIVtest(Src, DstAddRec, Level, Result);
1683bool DependenceInfo::testRDIV(
const SCEV *Src,
const SCEV *Dst,
1689 assert(SrcAddRec && DstAddRec &&
"Unexpected non-addrec input");
1690 return exactRDIVtest(SrcAddRec, DstAddRec, Result) ||
1691 gcdMIVtest(Src, Dst, Result);
1697bool DependenceInfo::testMIV(
const SCEV *Src,
const SCEV *Dst,
1702 return gcdMIVtest(Src, Dst, Result) ||
1703 banerjeeMIVtest(Src, Dst,
Loops, Result);
1716 if (Product->hasNoSignedWrap())
1718 return std::nullopt;
1721const SCEV *DependenceInfo::accumulateCoefficientsGCD(
const SCEV *Expr,
1722 const Loop *CurLoop,
1723 const SCEV *&CurLoopCoeff,
1724 APInt &RunningGCD)
const {
1727 assert(isLoopInvariant(Expr, CurLoop) &&
1728 "Expected loop invariant expression");
1735 if (AddRec->
getLoop() == CurLoop) {
1736 CurLoopCoeff = Step;
1750 return accumulateCoefficientsGCD(Start, CurLoop, CurLoopCoeff, RunningGCD);
1770bool DependenceInfo::gcdMIVtest(
const SCEV *Src,
const SCEV *Dst,
1777 unsigned BitWidth = SE->getTypeSizeInBits(Src->getType());
1780 const SCEV *Dummy =
nullptr;
1781 const SCEV *SrcConst =
1782 accumulateCoefficientsGCD(Src,
nullptr, Dummy, RunningGCD);
1785 const SCEV *DstConst =
1786 accumulateCoefficientsGCD(Dst,
nullptr, Dummy, RunningGCD);
1797 APInt ConstDelta =
Constant->getAPInt();
1799 if (ConstDelta == 0)
1802 APInt Remainder = ConstDelta.
srem(RunningGCD);
1803 if (Remainder != 0) {
1817 bool Improved =
false;
1818 const SCEV *Coefficients = Src;
1819 while (
const SCEVAddRecExpr *AddRec =
1825 const SCEV *DstCoeff = SE->getZero(SrcCoeff->
getType());
1827 if (!accumulateCoefficientsGCD(Src, CurLoop, SrcCoeff, RunningGCD) ||
1828 !accumulateCoefficientsGCD(Dst, CurLoop, DstCoeff, RunningGCD))
1843 if (RunningGCD != 0) {
1844 Remainder = ConstDelta.
srem(RunningGCD);
1846 if (Remainder != 0) {
1847 unsigned Level = mapSrcLoop(CurLoop);
1848 Result.DV[
Level - 1].Direction &= ~Dependence::DVEntry::EQ;
1867struct BanerjeeInterval {
1871 BanerjeeInterval(
const SCEV *
Lower,
const SCEV *
Upper)
1874 bool isEmpty(ScalarEvolution &SE)
const {
1884 const BanerjeeInterval &
B,
1888 if (
A.Lower &&
B.Lower)
1890 if (
A.Upper &&
B.Upper)
1899 const BanerjeeInterval &
B,
1912 return BanerjeeInterval(
C,
C);
1930 return BanerjeeInterval(SE.
getSMinExpr(LowerValue, UpperValue),
1943 return BanerjeeInterval(
nullptr,
Zero);
1945 return BanerjeeInterval(
Zero,
nullptr);
1946 return BanerjeeInterval(
nullptr,
nullptr);
2007 assert(!
Values.empty() &&
"expected at least one value");
2031 for (
const SCEV *Subscript : {Src, Dst}) {
2059bool DependenceInfo::banerjeeMIVtest(
const SCEV *Src,
const SCEV *Dst,
2066 ++BanerjeeApplications;
2069 unsigned BaseBits = SE->getTypeSizeInBits(
BaseType);
2079 unsigned WideBits = 2 * BaseBits + MaxLevels + 1;
2081 const SCEV *
Zero = SE->getZero(WideType);
2083 CoefficientInfo EmptyCoeff{
Zero,
Zero,
nullptr};
2085 assert(
Loops.size() > MaxLevels &&
"loop bit vector is too small");
2087 "direction vector is too small for common levels");
2089 const SCEV *A0 = collectCoeffInfo(Src,
true, WideType, CI);
2090 const SCEV *
B0 = collectCoeffInfo(Dst,
false, WideType, CI);
2091 const SCEV *Delta = SE->getMinusSCEV(B0, A0);
2095 for (
unsigned K = 0;
K <= MaxLevels; ++
K) {
2099 for (
unsigned K = 1;
K <= MaxLevels; ++
K) {
2100 findBoundsALL(CI, Bound, K);
2101 findBoundsLT(CI, Bound, K);
2102 findBoundsEQ(CI, Bound, K);
2103 findBoundsGT(CI, Bound, K);
2107 ++BanerjeeIndependence;
2111 unsigned NewDeps = exploreDirections(1, Bound,
Loops, Delta, Result);
2113 ++BanerjeeIndependence;
2117 bool Improved =
false;
2118 for (
unsigned K = 1;
K <= CommonLevels; ++
K) {
2121 unsigned Old =
Result.DV[
K - 1].Direction;
2122 Result.DV[
K - 1].Direction = Old & Bound[
K].DirSet;
2123 Improved |= Old !=
Result.DV[
K - 1].Direction;
2124 if (!
Result.DV[K - 1].Direction) {
2125 ++BanerjeeIndependence;
2131 ++BanerjeeSuccesses;
2140DependenceInfo::collectCoeffInfo(
const SCEV *Subscript,
bool SrcFlag,
2143 SmallBitVector SeenLevels(MaxLevels + 1);
2146 SrcFlag ? mapSrcLoop(AddRec->
getLoop()) : mapDstLoop(AddRec->getLoop());
2147 assert(K > 0 && K <= MaxLevels &&
"invalid mapped loop level");
2148 assert(!SeenLevels[K] &&
"duplicate recurrence for loop level");
2152 const SCEV *&Coeff = SrcFlag ? CI[
K].SrcCoeff : CI[
K].DstCoeff;
2153 Coeff = SE->getNoopOrSignExtend(Step, WideType);
2155 const SCEV *MaxIterIndex = SE->getBackedgeTakenCount(AddRec->
getLoop());
2161 CI[
K].MaxIterIndex =
2162 SE->isKnownNonNegative(MaxIterIndex)
2163 ? SE->getNoopOrSignExtend(MaxIterIndex, WideType)
2164 : SE->getNoopOrZeroExtend(MaxIterIndex, WideType);
2169 return SE->getNoopOrSignExtend(Subscript, WideType);
2174 const SCEV *Sum = Bound[1].Lower[Bound[1].Direction];
2175 for (
unsigned K = 2; Sum &&
K <= MaxLevels; ++
K) {
2185 const SCEV *Sum = Bound[1].Upper[Bound[1].Direction];
2186 for (
unsigned K = 2; Sum &&
K <= MaxLevels; ++
K) {
2196bool DependenceInfo::testBounds(
unsigned char DirKind,
unsigned Level,
2198 const SCEV *Delta)
const {
2199 Bound[
Level].Direction = DirKind;
2200 for (
unsigned K = 1;
K <= MaxLevels; ++
K) {
2201 unsigned char Direction = Bound[
K].Direction;
2207 if (
const SCEV *
Lower = getLowerBound(Bound))
2210 if (
const SCEV *
Upper = getUpperBound(Bound))
2217unsigned DependenceInfo::exploreDirections(
unsigned Level,
2222 if (CommonLevels > Opts.da_miv_max_level_threshold) {
2223 LLVM_DEBUG(
dbgs() <<
"Number of common levels exceeded the threshold. MIV "
2224 "direction exploration is terminated.\n");
2225 for (
unsigned K = 1;
K <= CommonLevels; ++
K)
2231 if (Level > CommonLevels) {
2232 for (
unsigned K = 1;
K <= CommonLevels; ++
K)
2234 Bound[
K].DirSet |= Bound[
K].Direction;
2239 return exploreDirections(Level + 1, Bound,
Loops, Delta, Result);
2241 unsigned NewDeps = 0;
2242 unsigned OldDirections =
Result.DV[
Level - 1].Direction;
2245 if (!(OldDirections & Dir))
2247 if (testBounds(Dir, Level, Bound, Delta))
2248 NewDeps += exploreDirections(Level + 1, Bound,
Loops, Delta, Result);
2272 BanerjeeInterval SrcInterval =
2275 SE->getNegativeSCEV(CI[K].DstCoeff), CI[K].MaxIterIndex, *SE);
2294 CI[K].MaxIterIndex, *SE);
2315 const SCEV *ACoeff = CI[
K].SrcCoeff;
2316 const SCEV *BCoeff = CI[
K].DstCoeff;
2317 const SCEV *MaxIterIndex = CI[
K].MaxIterIndex;
2321 if (MaxIterIndex && MaxIterIndex->
isZero()) {
2323 }
else if (MaxIterIndex) {
2324 const SCEV *
Zero = SE->getZero(ACoeff->
getType());
2325 const SCEV *One = SE->getOne(ACoeff->
getType());
2326 const SCEV *MaxMinusOne = SE->getMinusSCEV(MaxIterIndex, One);
2333 MaxIterIndex, *SE));
2357 const SCEV *ACoeff = CI[
K].SrcCoeff;
2358 const SCEV *BCoeff = CI[
K].DstCoeff;
2359 const SCEV *MaxIterIndex = CI[
K].MaxIterIndex;
2363 if (MaxIterIndex && MaxIterIndex->
isZero()) {
2365 }
else if (MaxIterIndex) {
2366 const SCEV *
Zero = SE->getZero(ACoeff->
getType());
2367 const SCEV *One = SE->getOne(ACoeff->
getType());
2368 const SCEV *MaxMinusOne = SE->getMinusSCEV(MaxIterIndex, One);
2393 Loop *SrcLoop = LI->getLoopFor(Src->getParent());
2394 Loop *DstLoop = LI->getLoopFor(Dst->getParent());
2395 const SCEV *SrcAccessFn = SE->getSCEVAtScope(SrcPtr, SrcLoop);
2396 const SCEV *DstAccessFn = SE->getSCEVAtScope(DstPtr, DstLoop);
2397 const SCEVUnknown *SrcBase =
2399 const SCEVUnknown *DstBase =
2402 if (!SrcBase || !DstBase || SrcBase != DstBase)
2407 if (!tryDelinearizeFixedSize(Src, Dst, SrcAccessFn, DstAccessFn,
2408 SrcSubscripts, DstSubscripts) &&
2409 !tryDelinearizeParametricSize(Src, Dst, SrcAccessFn, DstAccessFn,
2410 SrcSubscripts, DstSubscripts))
2413 assert(isLoopInvariant(SrcBase, SrcLoop) &&
2414 isLoopInvariant(DstBase, DstLoop) &&
2415 "Expected SrcBase and DstBase to be loop invariant");
2419 dbgs() <<
"\nSrcSubscripts: ";
2420 for (
int I = 0;
I <
Size;
I++)
2421 dbgs() << *SrcSubscripts[
I];
2422 dbgs() <<
"\nDstSubscripts: ";
2423 for (
int I = 0;
I <
Size;
I++)
2424 dbgs() << *DstSubscripts[
I];
2433 for (
int I = 0;
I <
Size; ++
I) {
2434 Pair[
I].Src = SrcSubscripts[
I];
2435 Pair[
I].Dst = DstSubscripts[
I];
2437 assert(Pair[
I].Src->getType() == Pair[
I].Dst->getType() &&
2438 "Unexpected different types for the subscripts");
2447bool DependenceInfo::tryDelinearizeFixedSize(
2452 const SCEVUnknown *SrcBase =
2454 const SCEVUnknown *DstBase =
2456 assert(SrcBase && DstBase && SrcBase == DstBase &&
2457 "expected src and dst scev unknowns to be equal");
2460 const SCEV *ElemSize = SE->getElementSize(Src);
2461 assert(ElemSize == SE->getElementSize(Dst) &&
"Different element sizes");
2464 SrcSubscripts, SrcSizes, ElemSize) ||
2466 DstSubscripts, DstSizes, ElemSize))
2471 if (SrcSizes.
size() != DstSizes.
size() ||
2472 !std::equal(SrcSizes.
begin(), SrcSizes.
end(), DstSizes.
begin())) {
2473 SrcSubscripts.
clear();
2474 DstSubscripts.
clear();
2479 "Expected equal number of entries in the list of SrcSubscripts and "
2488 if (Opts.da_delinearization_checks) {
2491 SrcSubscripts.
clear();
2492 DstSubscripts.
clear();
2497 dbgs() <<
"Delinearized subscripts of fixed-size array\n"
2504bool DependenceInfo::tryDelinearizeParametricSize(
2509 const SCEVUnknown *SrcBase =
2511 const SCEVUnknown *DstBase =
2513 assert(SrcBase && DstBase && SrcBase == DstBase &&
2514 "expected src and dst scev unknowns to be equal");
2516 const SCEV *ElementSize = SE->getElementSize(Src);
2517 if (ElementSize != SE->getElementSize(Dst))
2520 const SCEV *SrcSCEV = SE->getMinusSCEV(SrcAccessFn, SrcBase);
2521 const SCEV *DstSCEV = SE->getMinusSCEV(DstAccessFn, DstBase);
2542 if (SrcSubscripts.
size() < 2 || DstSubscripts.
size() < 2 ||
2543 SrcSubscripts.
size() != DstSubscripts.
size())
2552 if (Opts.da_delinearization_checks)
2566 for (
unsigned VI : BV.
set_bits()) {
2576 FunctionAnalysisManager::Invalidator &Inv) {
2583 return Inv.invalidate<
AAManager>(F, PA) ||
2588void DependenceInfo::updateLoopAliasScopes(
const Loop *L) {
2589 if (!L || !AlreadyCheckedLoops.insert(L).second)
2594 Intrinsic::experimental_noalias_scope_decl))
2607 for (
Metadata *
Op : Decl->getScopeList()->operands())
2612 updateLoopAliasScopes(
L->getParentLoop());
2624std::unique_ptr<Dependence>
2626 bool UnderRuntimeAssumptions) {
2628 bool PossiblyLoopIndependent =
true;
2630 PossiblyLoopIndependent =
false;
2632 if (!(Src->mayReadOrWriteMemory() && Dst->mayReadOrWriteMemory()))
2638 LLVM_DEBUG(
dbgs() <<
"can only handle simple loads and stores\n");
2639 return std::make_unique<Dependence>(Src, Dst,
2646 updateLoopAliasScopes(LI->getLoopFor(Src->getParent()));
2647 updateLoopAliasScopes(LI->getLoopFor(Dst->getParent()));
2655 return std::make_unique<Dependence>(Src, Dst,
2669 LLVM_DEBUG(
dbgs() <<
"can't analyze must alias with different sizes\n");
2670 return std::make_unique<Dependence>(Src, Dst,
2676 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
2677 const SCEV *DstSCEV = SE->getSCEV(DstPtr);
2680 const SCEV *SrcBase = SE->getPointerBase(SrcSCEV);
2681 const SCEV *DstBase = SE->getPointerBase(DstSCEV);
2682 if (SrcBase != DstBase) {
2689 LLVM_DEBUG(
dbgs() <<
"can't analyze SCEV with different pointer base\n");
2690 return std::make_unique<Dependence>(Src, Dst,
2698 Loop *SrcLoop = LI->getLoopFor(Src->getParent());
2699 Loop *DstLoop = LI->getLoopFor(Dst->getParent());
2700 if (!isLoopInvariant(SrcBase, SrcLoop) ||
2701 !isLoopInvariant(DstBase, DstLoop)) {
2702 LLVM_DEBUG(
dbgs() <<
"The base pointer is not loop invariant.\n");
2703 return std::make_unique<Dependence>(Src, Dst,
2708 const SCEV *SrcEv = SE->getMinusSCEV(SrcSCEV, SrcBase);
2709 const SCEV *DstEv = SE->getMinusSCEV(DstSCEV, DstBase);
2713 if (!SE->isKnownMultipleOf(SrcEv, EltSize,
2714 UnderRuntimeAssumptions ? &Assume :
nullptr) ||
2715 !SE->isKnownMultipleOf(DstEv, EltSize,
2716 UnderRuntimeAssumptions ? &Assume :
nullptr)) {
2717 LLVM_DEBUG(
dbgs() <<
"can't analyze SCEV with different offsets\n");
2718 return std::make_unique<Dependence>(Src, Dst,
2724 Pair[0].Src = SrcEv;
2725 Pair[0].Dst = DstEv;
2726 if (Opts.da_delinearize) {
2727 if (tryDelinearize(Src, Dst, Pair)) {
2729 Pairs = Pair.
size();
2734 establishNestingLevels(Src, Dst);
2736 LLVM_DEBUG(
dbgs() <<
" common nesting levels = " << CommonLevels <<
"\n");
2737 LLVM_DEBUG(
dbgs() <<
" maximum nesting levels = " << MaxLevels <<
"\n");
2738 LLVM_DEBUG(
dbgs() <<
" SameSD nesting levels = " << SameSDLevels <<
"\n");
2741 CommonLevels += SameSDLevels;
2742 MaxLevels -= SameSDLevels;
2743 if (SameSDLevels > 0) {
2746 for (
unsigned P = 0;
P < Pairs; ++
P) {
2748 Subscript::ClassificationKind TestClass =
2749 classifyPair(Pair[
P].Src, SrcLoop, Pair[
P].Dst, DstLoop,
Loops);
2751 if (TestClass != Subscript::ZIV && TestClass != Subscript::SIV &&
2752 TestClass != Subscript::RDIV) {
2754 CommonLevels -= SameSDLevels;
2755 MaxLevels += SameSDLevels;
2762 if (SameSDLevels > 0)
2766 PossiblyLoopIndependent, CommonLevels);
2769 for (
unsigned P = 0;
P < Pairs; ++
P) {
2770 assert(Pair[
P].Src->getType()->isIntegerTy() &&
"Src must be an integer");
2771 assert(Pair[
P].Dst->getType()->isIntegerTy() &&
"Dst must be an integer");
2772 Pair[
P].Loops.
resize(MaxLevels + 1);
2773 Pair[
P].Classification =
2774 classifyPair(Pair[
P].Src, SrcLoop, Pair[
P].Dst, DstLoop, Pair[
P].
Loops);
2784 for (
unsigned SI = 0;
SI < Pairs; ++
SI) {
2793 switch (Pair[
SI].Classification) {
2794 case Subscript::NonLinear:
2796 ++NonlinearSubscriptPairs;
2797 collectCommonLoops(Pair[
SI].Src, SrcLoop, Pair[
SI].
Loops);
2798 collectCommonLoops(Pair[
SI].Dst, DstLoop, Pair[
SI].
Loops);
2800 case Subscript::ZIV:
2802 if (testZIV(Pair[
SI].Src, Pair[
SI].Dst, Result))
2805 case Subscript::SIV: {
2808 if (testSIV(Pair[
SI].Src, Pair[
SI].Dst, Level, Result,
2809 UnderRuntimeAssumptions))
2813 case Subscript::RDIV:
2815 if (testRDIV(Pair[
SI].Src, Pair[
SI].Dst, Result))
2818 case Subscript::MIV:
2820 if (testMIV(Pair[
SI].Src, Pair[
SI].Dst, Pair[
SI].
Loops, Result))
2828 for (
unsigned SI = 0;
SI < Pairs; ++
SI)
2829 CompleteLoops |= Pair[
SI].
Loops;
2830 for (
unsigned II = 1;
II <= CommonLevels; ++
II)
2831 if (CompleteLoops[
II])
2832 Result.DV[
II - 1].Scalar =
false;
2837 for (
unsigned II = 1;
II <= Result.getLevels(); ++
II) {
2839 if (Result.DV[
II - 1].Distance ==
nullptr)
2840 Result.DV[
II - 1].Distance = SE->getZero(SrcSCEV->
getType());
2842 assert(Result.DV[
II - 1].Distance->isZero() &&
2843 "Inconsistency between distance and direction");
2849 const SCEV *Distance = Result.getDistance(
II);
2850 if (Distance && Distance->
isZero())
2852 "Distance is zero, but direction is not EQ");
2856 if (SameSDLevels > 0) {
2859 assert(CommonLevels >= SameSDLevels);
2860 CommonLevels -= SameSDLevels;
2861 MaxLevels += SameSDLevels;
2862 std::unique_ptr<FullDependence::DVEntry[]> DV, DVSameSD;
2863 DV = std::make_unique<FullDependence::DVEntry[]>(CommonLevels);
2864 DVSameSD = std::make_unique<FullDependence::DVEntry[]>(SameSDLevels);
2865 for (
unsigned Level = 0; Level < CommonLevels; ++Level)
2866 DV[Level] = Result.DV[Level];
2867 for (
unsigned Level = 0; Level < SameSDLevels; ++Level)
2868 DVSameSD[Level] = Result.DV[CommonLevels + Level];
2869 Result.DV = std::move(DV);
2870 Result.DVSameSD = std::move(DVSameSD);
2871 Result.Levels = CommonLevels;
2872 Result.SameSDLevels = SameSDLevels;
2875 if (PossiblyLoopIndependent) {
2879 for (
unsigned II = 1;
II <= CommonLevels; ++
II) {
2881 Result.LoopIndependent =
false;
2889 bool AllEqual =
true;
2890 for (
unsigned II = 1;
II <= CommonLevels; ++
II) {
2896 if (AllEqual && Result.Assumptions.getPredicates().empty())
2900 return std::make_unique<FullDependence>(std::move(Result));
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
bpf unwind BPF Unwind Lower
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
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")
static bool isLoadOrStore(const Instruction *I)
static std::optional< bool > findGCD(unsigned Bits, const APInt &AM, const APInt &BM, const APInt &Delta, APInt &G, APInt &X, APInt &Y)
static OverflowSafeSignedAPInt floorOfQuotient(const OverflowSafeSignedAPInt &OA, const OverflowSafeSignedAPInt &OB)
static void dumpExampleDependence(raw_ostream &OS, DependenceInfo *DA, ScalarEvolution &SE, LoopInfo &LI, bool NormalizeResults)
static OverflowSafeSignedAPInt ceilingOfQuotient(const OverflowSafeSignedAPInt &OA, const OverflowSafeSignedAPInt &OB)
static void dumpSmallBitVector(SmallBitVector &BV)
static BanerjeeInterval emptyInterval(Type *Ty, ScalarEvolution &SE)
Return the canonical empty interval [1, 0].
static std::pair< OverflowSafeSignedAPInt, OverflowSafeSignedAPInt > inferDomainOfAffine(OverflowSafeSignedAPInt A, OverflowSafeSignedAPInt B, OverflowSafeSignedAPInt UB)
Given an affine expression of the form A*k + B, where k is an arbitrary integer, infer the possible r...
static MDNode * adjustAliasScopeList(MDNode *ScopeList, const SmallPtrSetImpl< const MDNode * > &LoopAliasScopes)
Drop alias scopes that are only valid within a single loop iteration.
static const SCEV * minusSCEVNoSignedOverflow(const SCEV *A, const SCEV *B, ScalarEvolution &SE)
Returns A - B if it guaranteed not to signed wrap.
static AliasResult underlyingObjectsAlias(AAResults *AA, const DataLayout &DL, const MemoryLocation &LocA, const MemoryLocation &LocB, const SmallPtrSetImpl< const MDNode * > &LoopAliasScopes)
static BanerjeeInterval constantInterval(const SCEV *C)
Return the singleton interval containing C.
static Type * getBanerjeeBaseType(const SCEV *Src, const SCEV *Dst, ScalarEvolution &SE)
Return the widest type used by a subscript or by an exact backedge-taken count of one of its recurren...
static BanerjeeInterval variableInterval(const SCEV *Coeff, const SCEV *Upper, ScalarEvolution &SE)
Compute the range of Coeff * X for 0 <= X <= Upper.
static std::optional< APInt > getConstantCoefficient(const SCEV *Expr)
Given a SCEVMulExpr, returns its first operand if its first operand is a constant and the product doe...
static BanerjeeInterval strictDirectionIntervalWithUnknownUpperBound(const SCEV *ACoeff, const SCEV *BCoeff, unsigned char Direction, ScalarEvolution &SE)
Compute any finite one-sided bound on.
static const SCEV * evaluateSubscriptDifference(const SCEV *A, const SCEV *SrcIndex, const SCEV *B, const SCEV *DstIndex, ScalarEvolution &SE)
Evaluate one loop level's contribution A * SrcIndex - B * DstIndex to the complete source-minus-desti...
static bool isRemainderZero(const SCEVConstant *Dividend, const SCEVConstant *Divisor)
static BanerjeeInterval signedRangeInterval(const SCEV *Coeff, const SCEV *Lower, const SCEV *Upper, ScalarEvolution &SE)
Compute the range of Coeff * X for Lower <=s X <=s Upper.
static BanerjeeInterval intervalFromValues(ArrayRef< const SCEV * > Values, ScalarEvolution &SE)
Return the smallest closed interval containing Values.
static bool isDependenceTestEnabled(const AnalysisOptions &Opts, DependenceTestType Test)
Returns true iff Test is enabled.
static BanerjeeInterval addIntervals(const BanerjeeInterval &A, const BanerjeeInterval &B, ScalarEvolution &SE)
Add two intervals.
static BanerjeeInterval intersectIntervals(const BanerjeeInterval &A, const BanerjeeInterval &B, ScalarEvolution &SE)
Intersect two intervals.
static MemoryLocation adjustLoc(MemoryLocation Loc, const SmallPtrSetImpl< const MDNode * > &LoopAliasScopes)
Module.h This file contains the declarations for the Module class.
Loop::LoopBounds::Direction Direction
std::pair< uint64_t, uint64_t > Interval
uint64_t IntrinsicInst * II
FunctionAnalysisManager FAM
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
A manager for alias analyses.
Class for arbitrary precision integers.
bool isMinSignedValue() const
Determine if this is the smallest signed value.
static LLVM_ABI void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
APInt abs() const
Get the absolute value.
bool sgt(const APInt &RHS) const
Signed greater than comparison.
unsigned getBitWidth() const
Return the number of bits in the APInt.
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
bool slt(const APInt &RHS) const
Signed less than comparison.
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
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....
Represent a constant reference to an array (0 or more elements consecutively in memory),...
LLVM Basic Block Representation.
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
void enableCrossIterationMode()
Assume that values may come from different cycle iterations.
bool isNoAlias(const MemoryLocation &LocA, const MemoryLocation &LocB)
@ ICMP_SGT
signed greater than
This class represents a range of values.
LLVM_ABI bool isEmptySet() const
Return true if this set contains no members.
bool isSingleElement() const
Return true if this set contains exactly one member.
LLVM_ABI ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
This is an important base class in LLVM.
A parsed version of the target data layout string in and methods for querying it.
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 DependenceInfo(Function *F, AAResults *AA, ScalarEvolution *SE, LoopInfo *LI)
LLVM_ABI std::unique_ptr< Dependence > depends(Instruction *Src, Instruction *Dst, bool UnderRuntimeAssumptions=false)
depends - Tests for a dependence between the Src and Dst instructions.
void dumpImp(raw_ostream &OS, bool IsSameSD=false) const
dumpImp - For debugging purposes.
Dependence(Dependence &&)=default
SCEVUnionPredicate getRuntimeAssumptions() const
getRuntimeAssumptions - Returns the runtime assumptions under which this Dependence relation is valid...
virtual bool isConfused() const
isConfused - Returns true if this dependence is confused (the compiler understands nothing and makes ...
virtual unsigned getSameSDLevels() const
getSameSDLevels - Returns the number of separate SameSD loops surrounding the source and destination ...
virtual const SCEV * getDistance(unsigned Level, bool SameSD=false) const
getDistance - Returns the distance (or NULL) associated with a particular common or SameSD level.
virtual unsigned getLevels() const
getLevels - Returns the number of common loops surrounding the source and destination of the dependen...
virtual unsigned getDirection(unsigned Level, bool SameSD=false) const
getDirection - Returns the direction associated with a particular common or SameSD level.
virtual bool isScalar(unsigned Level, bool SameSD=false) const
isScalar - Returns true if a particular regular or SameSD level is scalar; that is,...
bool isFlow() const
isFlow - Returns true if this is a flow (aka true) dependence.
bool isInput() const
isInput - Returns true if this is an input dependence.
bool isAnti() const
isAnti - Returns true if this is an anti dependence.
virtual bool isLoopIndependent() const
isLoopIndependent - Returns true if this is a loop-independent dependence.
bool isOutput() const
isOutput - Returns true if this is an output dependence.
void dump(raw_ostream &OS) const
dump - For debugging purposes, dumps a dependence to OS.
virtual bool inSameSDLoops(unsigned Level) const
inSameSDLoops - Returns true if this level is an SameSD level, i.e., performed across two separate lo...
Class representing an expression and its matching format.
FullDependence - This class represents a dependence between two memory references in a function.
FullDependence(Instruction *Source, Instruction *Destination, const SCEVUnionPredicate &Assumes, bool PossiblyLoopIndependent, unsigned Levels)
unsigned getDirection(unsigned Level, bool SameSD=false) const override
getDirection - Returns the direction associated with a particular common or SameSD level.
bool isScalar(unsigned Level, bool SameSD=false) const override
isScalar - Returns true if a particular regular or SameSD level is scalar; that is,...
bool isDirectionNegative() const override
Check if the direction vector is negative.
void negate(ScalarEvolution &SE) override
Negate the dependence by swapping the source and destination, and reversing the direction and distanc...
const SCEV * getDistance(unsigned Level, bool SameSD=false) const override
getDistance - Returns the distance (or NULL) associated with a particular common or SameSD level.
DVEntry getDVEntry(unsigned Level, bool IsSameSD) const
getDVEntry - Returns the DV entry associated with a regular or a SameSD level.
bool inSameSDLoops(unsigned Level) const override
inSameSDLoops - Returns true if this level is an SameSD level, i.e., performed across two separate lo...
bool normalize(ScalarEvolution *SE) override
If the direction vector is negative, normalize the direction vector to make it non-negative.
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
An instruction for reading from memory.
Analysis pass that exposes the LoopInfo for a function.
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.
This class represents a loop nest and can be used to query its properties.
Represents a single loop in the control flow graph.
ArrayRef< MDOperand > operands() const
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.
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
This node represents a polynomial recurrence on the trip count of the specified loop.
LLVM_ABI const SCEV * evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const
Return the value of this chain of recurrences at the specified iteration number.
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values.
const Loop * getLoop() const
SCEVUse getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
This class represents a constant integer value.
const APInt & getAPInt() const
bool hasNoSignedWrap() const
This class represents a composition of other SCEV predicates, and is the class that most clients will...
This class represents an analyzed expression in the program.
LLVM_ABI bool isOne() const
Return true if the expression is a constant one.
LLVM_ABI bool isZero() const
Return true if the expression is a constant zero.
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 bool isKnownNonNegative(const SCEV *S)
Test if the given expression is known to be non-negative.
LLVM_ABI Type * getWiderType(Type *Ty1, Type *Ty2) const
LLVM_ABI bool isKnownNonPositive(const SCEV *S)
Test if the given expression is known to be non-positive.
LLVM_ABI bool isKnownNegative(const SCEV *S)
Test if the given expression is known to be negative.
LLVM_ABI const SCEV * getBackedgeTakenCount(const Loop *L, ExitCountKind Kind=Exact)
If the specified loop has a predictable backedge-taken count, return it, otherwise return a SCEVCould...
LLVM_ABI const SCEV * getSMinExpr(SCEVUse LHS, SCEVUse RHS)
const SCEV * getZero(Type *Ty)
Return a SCEV for the constant 0 of a specific type.
LLVM_ABI bool willNotOverflow(Instruction::BinaryOps BinOp, bool Signed, const SCEV *LHS, const SCEV *RHS, const Instruction *CtxI=nullptr)
Is operation BinOp between LHS and RHS provably does not have a signed/unsigned overflow (Signed)?
LLVM_ABI const SCEV * getMinusSCEV(SCEVUse LHS, SCEVUse RHS, SCEVFlags Flags=SCEV::FlagNone, unsigned Depth=0)
Return LHS-RHS.
const SCEV * getOne(Type *Ty)
Return a SCEV for the constant 1 of a specific type.
LLVM_ABI SCEVUse getAddExpr(SmallVectorImpl< SCEVUse > &Ops, SCEVFlagsPair Flags={}, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
LLVM_ABI SCEVUse getMulExpr(SmallVectorImpl< SCEVUse > &Ops, SCEVFlagsPair Flags={}, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
LLVM_ABI const SCEV * getSMaxExpr(SCEVUse LHS, SCEVUse RHS)
LLVM_ABI const SCEV * getNegativeSCEV(const SCEV *V, SCEVFlags Flags=SCEV::FlagNone)
Return the SCEV object corresponding to -V.
LLVM_ABI bool isKnownPredicate(CmpPredicate Pred, SCEVUse LHS, SCEVUse RHS, unsigned Depth=0)
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.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
LLVM_NO_SANITIZE_CFI_UNRELATED_CAST iterator begin()
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
Represent a constant reference to a string, i.e.
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
LLVM_ABI Value(Type *Ty, unsigned scid)
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract Attribute helper functions.
const APInt & smin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
const APInt & smax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be signed.
LLVM_ABI APInt GreatestCommonDivisor(APInt A, APInt B, bool IsSigned=false)
Compute GCD of two APInt values.
constexpr bool operator!(E Val)
@ BasicBlock
Various leaf nodes.
LLVM_ABI StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
@ TB
TB - TwoByte - Set if this instruction has a two byte opcode, which starts with a 0x0F byte before th...
This is an optimization pass for GlobalISel generic memory operations.
InstIterator< SymbolTableList< BasicBlock >, Function::iterator, BasicBlock::iterator, Instruction > inst_iterator
LLVM_ABI void collectParametricTerms(ScalarEvolution &SE, const SCEV *Expr, SmallVectorImpl< const SCEV * > &Terms)
Collect parametric terms occurring in step expressions (first step of delinearization).
LLVM_ABI 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...
RelativeUniformCounterPtr Values
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
APInt operator*(APInt a, uint64_t RHS)
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)
LLVM_ABI bool validateDelinearizationResult(ScalarEvolution &SE, ArrayRef< const SCEV * > Sizes, ArrayRef< const SCEV * > Subscripts)
Check that each subscript in Subscripts is within the corresponding size in Sizes.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
const AnalysisOptions & getAnalysisOptions(const LLVMContext &C)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI 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)...
LLVM_ABI 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.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth, bool MustPreserveProvenance=false)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
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...
inst_iterator inst_end(Function *F)
DWARFExpression::Operation Op
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
APInt operator+(APInt a, const APInt &b)
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
Implement std::hash so that hash_code can be used in STL containers.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
MDNode * Scope
The tag for alias scope specification (used with noalias).
MDNode * NoAlias
The tag specifying the noalias scope.
A special type used by analysis passes to provide an address that identifies that particular analysis...
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
Dependence::DVEntry - Each level in the distance/direction vector has a direction (or perhaps a union...