94 #define DEBUG_TYPE "scalar-evolution"
97 "Number of trip counts computed with array length");
99 "Number of loops with predictable loop counts");
101 "Number of loops without predictable loop counts");
102 STATISTIC(NumBruteForceTripCountsComputed,
103 "Number of loops with trip counts computed by force");
107 cl::desc(
"Maximum number of iterations SCEV will "
108 "symbolically execute a constant "
115 cl::desc(
"Verify ScalarEvolution's backedge taken counts (slow)"));
118 "Scalar Evolution Analysis",
false,
true)
125 char ScalarEvolution::
ID = 0;
135 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
145 cast<SCEVConstant>(
this)->getValue()->printAsOperand(OS,
false);
150 OS <<
"(trunc " << *Op->getType() <<
" " << *Op <<
" to "
157 OS <<
"(zext " << *Op->getType() <<
" " << *Op <<
" to "
164 OS <<
"(sext " << *Op->getType() <<
" " << *Op <<
" to "
190 const char *OpStr =
nullptr;
201 if (std::next(I) != E)
217 OS <<
"(" << *UDiv->
getLHS() <<
" /u " << *UDiv->
getRHS() <<
")";
224 OS <<
"sizeof(" << *AllocTy <<
")";
228 OS <<
"alignof(" << *AllocTy <<
")";
235 OS <<
"offsetof(" << *CTy <<
", ";
246 OS <<
"***COULDNOTCOMPUTE***";
255 return cast<SCEVConstant>(
this)->
getType();
259 return cast<SCEVCastExpr>(
this)->
getType();
264 return cast<SCEVNAryExpr>(
this)->
getType();
266 return cast<SCEVAddExpr>(
this)->
getType();
268 return cast<SCEVUDivExpr>(
this)->
getType();
270 return cast<SCEVUnknown>(
this)->
getType();
279 return SC->getValue()->isZero();
285 return SC->getValue()->isOne();
291 return SC->getValue()->isAllOnesValue();
299 if (!Mul)
return false;
303 if (!SC)
return false;
321 if (
const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
return S;
323 UniqueSCEVs.InsertNode(S, IP);
339 :
SCEV(ID, SCEVTy), Op(op), Ty(ty) {}
346 "Cannot truncate non-integer value!");
354 "Cannot zero extend non-integer value!");
362 "Cannot sign extend non-integer value!");
365 void SCEVUnknown::deleted() {
367 SE->forgetMemoizedResults(
this);
370 SE->UniqueSCEVs.RemoveNode(
this);
376 void SCEVUnknown::allUsesReplacedWith(
Value *New) {
378 SE->forgetMemoizedResults(
this);
381 SE->UniqueSCEVs.RemoveNode(
this);
391 if (VCE->getOpcode() == Instruction::PtrToInt)
392 if (
ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
393 if (CE->getOpcode() == Instruction::GetElementPtr &&
394 CE->getOperand(0)->isNullValue() &&
395 CE->getNumOperands() == 2)
396 if (
ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(1)))
398 AllocTy = cast<PointerType>(CE->getOperand(0)->getType())
408 if (VCE->getOpcode() == Instruction::PtrToInt)
409 if (
ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
410 if (CE->getOpcode() == Instruction::GetElementPtr &&
411 CE->getOperand(0)->isNullValue()) {
413 cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
414 if (
StructType *STy = dyn_cast<StructType>(Ty))
415 if (!STy->isPacked() &&
416 CE->getNumOperands() == 3 &&
417 CE->getOperand(1)->isNullValue()) {
418 if (
ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(2)))
420 STy->getNumElements() == 2 &&
421 STy->getElementType(0)->isIntegerTy(1)) {
422 AllocTy = STy->getElementType(1);
433 if (VCE->getOpcode() == Instruction::PtrToInt)
434 if (
ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
435 if (CE->getOpcode() == Instruction::GetElementPtr &&
436 CE->getNumOperands() == 3 &&
437 CE->getOperand(0)->isNullValue() &&
438 CE->getOperand(1)->isNullValue()) {
440 cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
445 FieldNo = CE->getOperand(2);
461 class SCEVComplexityCompare {
464 explicit SCEVComplexityCompare(
const LoopInfo *li) : LI(li) {}
467 bool operator()(
const SCEV *LHS,
const SCEV *RHS)
const {
482 return (
int)LType - (
int)RType;
487 switch (static_cast<SCEVTypes>(LType)) {
499 RIsPointer = RV->getType()->isPointerTy();
500 if (LIsPointer != RIsPointer)
501 return (
int)LIsPointer - (
int)RIsPointer;
504 unsigned LID = LV->getValueID(),
505 RID = RV->getValueID();
507 return (
int)LID - (
int)RID;
510 if (
const Argument *LA = dyn_cast<Argument>(LV)) {
511 const Argument *RA = cast<Argument>(RV);
513 return (
int)LArgNo - (
int)RArgNo;
518 if (
const Instruction *LInst = dyn_cast<Instruction>(LV)) {
524 if (LParent != RParent) {
525 unsigned LDepth = LI->getLoopDepth(LParent),
526 RDepth = LI->getLoopDepth(RParent);
527 if (LDepth != RDepth)
528 return (
int)LDepth - (
int)RDepth;
532 unsigned LNumOps = LInst->getNumOperands(),
534 return (
int)LNumOps - (
int)RNumOps;
546 const APInt &RA = RC->getValue()->getValue();
547 unsigned LBitWidth = LA.getBitWidth(), RBitWidth = RA.
getBitWidth();
548 if (LBitWidth != RBitWidth)
549 return (
int)LBitWidth - (
int)RBitWidth;
550 return LA.ult(RA) ? -1 : 1;
558 const Loop *LLoop = LA->
getLoop(), *RLoop = RA->getLoop();
559 if (LLoop != RLoop) {
560 unsigned LDepth = LLoop->getLoopDepth(),
561 RDepth = RLoop->getLoopDepth();
562 if (LDepth != RDepth)
563 return (
int)LDepth - (
int)RDepth;
567 unsigned LNumOps = LA->
getNumOperands(), RNumOps = RA->getNumOperands();
568 if (LNumOps != RNumOps)
569 return (
int)LNumOps - (
int)RNumOps;
572 for (
unsigned i = 0; i != LNumOps; ++i) {
589 unsigned LNumOps = LC->
getNumOperands(), RNumOps = RC->getNumOperands();
590 if (LNumOps != RNumOps)
591 return (
int)LNumOps - (
int)RNumOps;
593 for (
unsigned i = 0; i != LNumOps; ++i) {
600 return (
int)LNumOps - (
int)RNumOps;
644 if (Ops.
size() < 2)
return;
645 if (Ops.
size() == 2) {
648 const SCEV *&LHS = Ops[0], *&RHS = Ops[1];
649 if (SCEVComplexityCompare(LI)(RHS, LHS))
655 std::stable_sort(Ops.
begin(), Ops.
end(), SCEVComplexityCompare(LI));
661 for (
unsigned i = 0, e = Ops.
size(); i != e-2; ++i) {
662 const SCEV *S = Ops[i];
667 for (
unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
672 if (i == e-2)
return;
679 struct FindSCEVSize {
681 FindSCEVSize() : Size(0) {}
683 bool follow(
const SCEV *S) {
688 bool isDone()
const {
704 struct SCEVDivision :
public SCEVVisitor<SCEVDivision, void> {
708 static void divide(ScalarEvolution &SE,
const SCEV *Numerator,
709 const SCEV *Denominator,
const SCEV **Quotient,
710 const SCEV **Remainder) {
711 assert(Numerator && Denominator &&
"Uninitialized SCEV");
713 SCEVDivision D(SE, Numerator, Denominator);
717 if (Numerator == Denominator) {
723 if (Numerator->
isZero()) {
730 if (Denominator->
isOne()) {
731 *Quotient = Numerator;
737 if (
const SCEVMulExpr *
T = dyn_cast<const SCEVMulExpr>(Denominator)) {
739 *Quotient = Numerator;
740 for (
const SCEV *Op :
T->operands()) {
741 divide(SE, *Quotient, Op, &Q, &R);
748 *Remainder = Numerator;
757 *Quotient = D.Quotient;
758 *Remainder = D.Remainder;
773 if (
const SCEVConstant *D = dyn_cast<SCEVConstant>(Denominator)) {
775 APInt DenominatorVal = D->getValue()->getValue();
777 uint32_t DenominatorBW = DenominatorVal.
getBitWidth();
779 if (NumeratorBW > DenominatorBW)
780 DenominatorVal = DenominatorVal.
sext(NumeratorBW);
781 else if (NumeratorBW < DenominatorBW)
782 NumeratorVal = NumeratorVal.
sext(DenominatorBW);
786 APInt::sdivrem(NumeratorVal, DenominatorVal, QuotientVal, RemainderVal);
794 const SCEV *StartQ, *StartR, *StepQ, *StepR;
795 assert(Numerator->
isAffine() &&
"Numerator should be affine");
796 divide(SE, Numerator->
getStart(), Denominator, &StartQ, &StartR);
800 if (Ty != StartQ->getType() || Ty != StartR->
getType() ||
801 Ty != StepQ->getType() || Ty != StepR->
getType()) {
803 Remainder = Numerator;
818 divide(SE, Op, Denominator, &Q, &R);
823 Remainder = Numerator;
831 if (Qs.
size() == 1) {
845 bool FoundDenominatorTerm =
false;
848 if (Ty != Op->getType()) {
850 Remainder = Numerator;
854 if (FoundDenominatorTerm) {
861 divide(SE, Op, Denominator, &Q, &R);
870 Remainder = Numerator;
874 FoundDenominatorTerm =
true;
878 if (FoundDenominatorTerm) {
887 if (!isa<SCEVUnknown>(Denominator)) {
889 Remainder = Numerator;
895 RewriteMap[cast<SCEVUnknown>(Denominator)->getValue()] =
896 cast<SCEVConstant>(Zero)->getValue();
899 if (Remainder->
isZero()) {
901 RewriteMap[cast<SCEVUnknown>(Denominator)->getValue()] =
902 cast<SCEVConstant>(
One)->getValue();
914 Remainder = Numerator;
917 divide(SE, Diff, Denominator, &Q, &R);
919 "(Numerator - Remainder) should evenly divide Denominator");
924 SCEVDivision(ScalarEvolution &S,
const SCEV *Numerator,
925 const SCEV *Denominator)
926 : SE(S), Denominator(Denominator) {
933 Remainder = Numerator;
937 const SCEV *Denominator, *Quotient, *Remainder, *Zero, *
One;
1015 APInt OddFactorial(W, 1);
1017 for (
unsigned i = 3; i <= K; ++i) {
1021 Mult = Mult.
lshr(TwoFactors);
1022 OddFactorial *=
Mult;
1026 unsigned CalculationBits = W +
T;
1035 APInt MultiplyFactor = OddFactorial.
zext(W+1);
1037 MultiplyFactor = MultiplyFactor.
trunc(W);
1043 for (
unsigned i = 1; i != K; ++i) {
1068 ScalarEvolution &SE)
const {
1075 if (isa<SCEVCouldNotCompute>(Coeff))
1090 "This is not a truncating conversion!");
1092 "This is not a conversion to a SCEVable type!");
1100 if (
const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
return S;
1121 if (
const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Op)) {
1123 bool hasTrunc =
false;
1124 for (
unsigned i = 0, e = SA->getNumOperands(); i != e && !hasTrunc; ++i) {
1126 if (!isa<SCEVCastExpr>(SA->getOperand(i)))
1127 hasTrunc = isa<SCEVTruncateExpr>(S);
1132 UniqueSCEVs.FindNodeOrInsertPos(ID, IP);
1137 if (
const SCEVMulExpr *SM = dyn_cast<SCEVMulExpr>(Op)) {
1139 bool hasTrunc =
false;
1140 for (
unsigned i = 0, e = SM->getNumOperands(); i != e && !hasTrunc; ++i) {
1142 if (!isa<SCEVCastExpr>(SM->getOperand(i)))
1143 hasTrunc = isa<SCEVTruncateExpr>(S);
1148 UniqueSCEVs.FindNodeOrInsertPos(ID, IP);
1152 if (
const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
1154 for (
unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
1164 UniqueSCEVs.InsertNode(S, IP);
1173 ScalarEvolution *SE) {
1193 ScalarEvolution *SE) {
1203 struct ExtendOpTraitsBase {
1204 typedef const SCEV *(ScalarEvolution::*GetExtendExprTy)(
const SCEV *,
Type *);
1208 template <
typename ExtendOp>
struct ExtendOpTraits {
1224 static const GetExtendExprTy GetExtendExpr;
1226 static const SCEV *getOverflowLimitForStep(
const SCEV *Step,
1228 ScalarEvolution *SE) {
1233 const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
1240 static const GetExtendExprTy GetExtendExpr;
1242 static const SCEV *getOverflowLimitForStep(
const SCEV *Step,
1244 ScalarEvolution *SE) {
1249 const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
1260 template <
typename ExtendOpTy>
1262 ScalarEvolution *SE) {
1263 auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
1264 auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
1306 const SCEV *OperandExtendedStart =
1307 SE->
getAddExpr((SE->*GetExtendExpr)(PreStart, WideTy),
1308 (SE->*GetExtendExpr)(Step, WideTy));
1309 if ((SE->*GetExtendExpr)(Start, WideTy) == OperandExtendedStart) {
1321 const SCEV *OverflowLimit =
1322 ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(Step, &Pred, SE);
1324 if (OverflowLimit &&
1332 template <
typename ExtendOpTy>
1334 ScalarEvolution *SE) {
1335 auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
1337 const SCEV *PreStart = getPreStartForExtend<ExtendOpTy>(AR, Ty, SE);
1339 return (SE->*GetExtendExpr)(AR->getStart(), Ty);
1341 return SE->
getAddExpr((SE->*GetExtendExpr)(AR->getStepRecurrence(*SE), Ty),
1342 (SE->*GetExtendExpr)(PreStart, Ty));
1378 template <
typename ExtendOpTy>
1379 bool ScalarEvolution::proveNoWrapByVaryingStart(
const SCEV *Start,
1382 auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
1395 for (
unsigned Delta : {-2, -1, 1, 2}) {
1408 this->UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
1414 const SCEV *Limit = ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(
1415 DeltaS, &Pred,
this);
1427 "This is not an extending conversion!");
1429 "This is not a conversion to a SCEVable type!");
1448 if (
const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
return S;
1454 const SCEV *X =
ST->getOperand();
1468 if (AR->isAffine()) {
1469 const SCEV *Start = AR->getStart();
1470 const SCEV *Step = AR->getStepRecurrence(*
this);
1472 const Loop *L = AR->getLoop();
1478 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty,
this),
1490 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
1496 const SCEV *CastedMaxBECount =
1498 const SCEV *RecastedMaxBECount =
1500 if (MaxBECount == RecastedMaxBECount) {
1506 const SCEV *WideMaxBECount =
1508 const SCEV *OperandExtendedAdd =
1512 if (ZAdd == OperandExtendedAdd) {
1517 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty,
this),
1522 OperandExtendedAdd =
1526 if (ZAdd == OperandExtendedAdd) {
1532 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty,
this),
1547 AR->getPostIncExpr(*
this),
N))) {
1552 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty,
this),
1561 AR->getPostIncExpr(*
this),
N))) {
1567 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty,
this),
1573 if (proveNoWrapByVaryingStart<SCEVZeroExtendExpr>(Start, Step, L)) {
1576 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty,
this),
1583 if (
const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
return S;
1586 UniqueSCEVs.InsertNode(S, IP);
1593 "This is not an extending conversion!");
1595 "This is not a conversion to a SCEVable type!");
1618 if (
const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
return S;
1628 const SCEV *X =
ST->getOperand();
1638 if (
auto SA = dyn_cast<SCEVAddExpr>(Op)) {
1639 if (SA->getNumOperands() == 2) {
1643 if (
auto SC2 = dyn_cast<SCEVConstant>(SMul->getOperand(0))) {
1644 const APInt &C1 = SC1->getValue()->getValue();
1645 const APInt &C2 = SC2->getValue()->getValue();
1659 if (AR->isAffine()) {
1660 const SCEV *Start = AR->getStart();
1661 const SCEV *Step = AR->getStepRecurrence(*
this);
1663 const Loop *L = AR->getLoop();
1669 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty,
this),
1681 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
1687 const SCEV *CastedMaxBECount =
1689 const SCEV *RecastedMaxBECount =
1691 if (MaxBECount == RecastedMaxBECount) {
1697 const SCEV *WideMaxBECount =
1699 const SCEV *OperandExtendedAdd =
1703 if (SAdd == OperandExtendedAdd) {
1708 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty,
this),
1713 OperandExtendedAdd =
1717 if (SAdd == OperandExtendedAdd) {
1730 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty,
this),
1740 const SCEV *OverflowLimit =
1742 if (OverflowLimit &&
1750 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty,
this),
1760 const APInt &C1 = SC1->getValue()->getValue();
1761 const APInt &C2 = SC2->getValue()->getValue();
1766 L, AR->getNoWrapFlags());
1771 if (proveNoWrapByVaryingStart<SCEVSignExtendExpr>(Start, Step, L)) {
1774 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty,
this),
1781 if (
const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
return S;
1784 UniqueSCEVs.InsertNode(S, IP);
1794 "This is not an extending conversion!");
1796 "This is not a conversion to a SCEVable type!");
1801 if (
SC->getValue()->getValue().isNegative())
1806 const SCEV *NewOp =
T->getOperand();
1814 if (!isa<SCEVZeroExtendExpr>(ZExt))
1819 if (!isa<SCEVSignExtendExpr>(SExt))
1825 for (
const SCEV *Op : AR->operands())
1831 if (isa<SCEVSMaxExpr>(Op))
1866 APInt &AccumulatedConstant,
1867 const SCEV *
const *Ops,
size_t NumOperands,
1869 ScalarEvolution &SE) {
1870 bool Interesting =
false;
1874 while (
const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
1877 if (Scale != 1 || AccumulatedConstant != 0 || C->getValue()->isZero())
1879 AccumulatedConstant += Scale * C->getValue()->getValue();
1884 for (; i != NumOperands; ++i) {
1886 if (Mul && isa<SCEVConstant>(Mul->
getOperand(0))) {
1888 Scale * cast<SCEVConstant>(Mul->
getOperand(0))->getValue()->getValue();
1901 std::pair<DenseMap<const SCEV *, APInt>::iterator,
bool> Pair =
1902 M.
insert(std::make_pair(Key, NewScale));
1906 Pair.first->second += NewScale;
1914 std::pair<DenseMap<const SCEV *, APInt>::iterator,
bool> Pair =
1915 M.
insert(std::make_pair(Ops[i], Scale));
1919 Pair.first->second += Scale;
1931 struct APIntCompare {
1932 bool operator()(
const APInt &LHS,
const APInt &RHS)
const {
1933 return LHS.
ult(RHS);
1945 using namespace std::placeholders;
1950 assert(CanAnalyze &&
"don't call from other places!");
1957 auto IsKnownNonNegative =
1973 "only nuw or nsw allowed");
1974 assert(!Ops.
empty() &&
"Cannot get empty add!");
1975 if (Ops.
size() == 1)
return Ops[0];
1978 for (
unsigned i = 1, e = Ops.
size(); i != e; ++i)
1980 "SCEVAddExpr operand types don't match!");
1990 if (
const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
1992 assert(Idx < Ops.
size());
1993 while (
const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
1995 Ops[0] =
getConstant(LHSC->getValue()->getValue() +
1996 RHSC->getValue()->getValue());
1997 if (Ops.
size() == 2)
return Ops[0];
1999 LHSC = cast<SCEVConstant>(Ops[0]);
2003 if (LHSC->getValue()->isZero()) {
2008 if (Ops.
size() == 1)
return Ops[0];
2014 Type *Ty = Ops[0]->getType();
2015 bool FoundMatch =
false;
2016 for (
unsigned i = 0, e = Ops.
size(); i != e-1; ++i)
2017 if (Ops[i] == Ops[i+1]) {
2020 while (i+Count != e && Ops[i+Count] == Ops[i])
2025 if (Ops.
size() == Count)
2029 --i; e -= Count - 1;
2039 for (; Idx < Ops.
size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) {
2047 for (
unsigned i = 0, e = Ops.
size(); i != e; ++i) {
2049 if (
T->getOperand()->getType() != SrcType) {
2054 }
else if (
const SCEVConstant *
C = dyn_cast<SCEVConstant>(Ops[i])) {
2056 }
else if (
const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) {
2058 for (
unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) {
2060 dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) {
2061 if (
T->getOperand()->getType() != SrcType) {
2067 dyn_cast<SCEVConstant>(M->getOperand(j))) {
2085 if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold))
2091 while (Idx < Ops.
size() && Ops[Idx]->getSCEVType() <
scAddExpr)
2095 if (Idx < Ops.
size()) {
2096 bool DeletedAdd =
false;
2097 while (
const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
2101 Ops.
append(Add->op_begin(), Add->op_end());
2113 while (Idx < Ops.
size() && Ops[Idx]->getSCEVType() <
scMulExpr)
2118 if (Idx < Ops.
size() && isa<SCEVMulExpr>(Ops[Idx])) {
2122 APInt AccumulatedConstant(BitWidth, 0);
2125 APInt(BitWidth, 1), *
this)) {
2129 std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists;
2131 E = NewOps.
end();
I != E; ++
I)
2132 MulOpLists[M.
find(*I)->second].push_back(*
I);
2135 if (AccumulatedConstant != 0)
2138 I = MulOpLists.begin(), E = MulOpLists.end();
I != E; ++
I)
2144 if (Ops.
size() == 1)
2153 for (; Idx < Ops.
size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
2154 const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
2155 for (
unsigned MulOp = 0, e = Mul->
getNumOperands(); MulOp != e; ++MulOp) {
2157 if (isa<SCEVConstant>(MulOpSCEV))
2159 for (
unsigned AddOp = 0, e = Ops.
size(); AddOp != e; ++AddOp)
2160 if (MulOpSCEV == Ops[AddOp]) {
2174 if (Ops.
size() == 2)
return OuterMul;
2187 for (
unsigned OtherMulIdx = Idx+1;
2188 OtherMulIdx < Ops.
size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
2190 const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]);
2194 OMulOp != e; ++OMulOp)
2195 if (OtherMul->
getOperand(OMulOp) == MulOpSCEV) {
2213 if (Ops.
size() == 2)
return OuterMul;
2230 for (; Idx < Ops.
size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
2236 for (
unsigned i = 0, e = Ops.
size(); i != e; ++i)
2244 if (!LIOps.
empty()) {
2259 if (Ops.
size() == 1)
return NewRec;
2262 for (
unsigned i = 0;; ++i)
2263 if (Ops[i] == AddRec) {
2273 for (
unsigned OtherIdx = Idx+1;
2274 OtherIdx < Ops.
size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
2276 if (AddRecLoop == cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()) {
2280 for (; OtherIdx != Ops.
size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
2283 dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx]))
2284 if (OtherAddRec->getLoop() == AddRecLoop) {
2285 for (
unsigned i = 0, e = OtherAddRec->getNumOperands();
2287 if (i >= AddRecOps.size()) {
2288 AddRecOps.
append(OtherAddRec->op_begin()+i,
2289 OtherAddRec->op_end());
2293 OtherAddRec->getOperand(i));
2295 Ops.
erase(Ops.
begin() + OtherIdx); --OtherIdx;
2310 for (
unsigned i = 0, e = Ops.
size(); i != e; ++i)
2314 static_cast<SCEVAddExpr *
>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2317 std::uninitialized_copy(Ops.
begin(), Ops.
end(), O);
2320 UniqueSCEVs.InsertNode(S, IP);
2326 static uint64_t
umul_ov(uint64_t i, uint64_t j,
bool &Overflow) {
2328 if (j > 1 && k / j != i) Overflow =
true;
2335 static uint64_t
Choose(uint64_t n, uint64_t k,
bool &Overflow) {
2344 if (n == 0 || n == k)
return 1;
2345 if (k > n)
return 0;
2351 for (uint64_t i = 1; i <= k; ++i) {
2352 r =
umul_ov(r, n-(i-1), Overflow);
2363 while (!Ops.
empty()) {
2365 if (isa<SCEVConstant>(*CurrentExpr))
2368 if (isa<SCEVAddExpr>(*CurrentExpr) || isa<SCEVMulExpr>(*CurrentExpr)) {
2369 const auto *CurrentNAry = cast<SCEVNAryExpr>(CurrentExpr);
2370 Ops.
append(CurrentNAry->op_begin(), CurrentNAry->op_end());
2381 "only nuw or nsw allowed");
2382 assert(!Ops.
empty() &&
"Cannot get empty mul!");
2383 if (Ops.
size() == 1)
return Ops[0];
2386 for (
unsigned i = 1, e = Ops.
size(); i != e; ++i)
2388 "SCEVMulExpr operand types don't match!");
2398 if (
const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
2401 if (Ops.
size() == 2)
2402 if (
const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1]))
2405 if (Add->getNumOperands() == 2)
2411 while (
const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
2414 LHSC->getValue()->getValue() *
2415 RHSC->getValue()->getValue());
2417 Ops.erase(Ops.begin()+1);
2418 if (Ops.size() == 1)
return Ops[0];
2419 LHSC = cast<SCEVConstant>(Ops[0]);
2423 if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) {
2426 }
else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) {
2429 }
else if (Ops[0]->isAllOnesValue()) {
2432 if (Ops.
size() == 2) {
2433 if (
const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) {
2435 bool AnyFolded =
false;
2437 E = Add->op_end();
I != E; ++
I) {
2439 if (!isa<SCEVMulExpr>(Mul)) AnyFolded =
true;
2446 AddRec = dyn_cast<SCEVAddRecExpr>(Ops[1])) {
2450 E = AddRec->op_end();
I != E; ++
I) {
2459 if (Ops.
size() == 1)
2464 while (Idx < Ops.
size() && Ops[Idx]->getSCEVType() <
scMulExpr)
2468 if (Idx < Ops.
size()) {
2469 bool DeletedMul =
false;
2470 while (
const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
2474 Ops.
append(Mul->op_begin(), Mul->op_end());
2492 for (; Idx < Ops.
size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
2498 for (
unsigned i = 0, e = Ops.
size(); i != e; ++i)
2506 if (!LIOps.
empty()) {
2523 if (Ops.
size() == 1)
return NewRec;
2526 for (
unsigned i = 0;; ++i)
2527 if (Ops[i] == AddRec) {
2548 bool OpsModified =
false;
2549 for (
unsigned OtherIdx = Idx+1;
2550 OtherIdx != Ops.
size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
2554 if (!OtherAddRec || OtherAddRec->
getLoop() != AddRecLoop)
2557 bool Overflow =
false;
2564 for (
int y = x, ye = 2*x+1; y != ye && !Overflow; ++y) {
2565 uint64_t Coeff1 =
Choose(x, 2*x - y, Overflow);
2568 z < ze && !Overflow; ++z) {
2569 uint64_t Coeff2 =
Choose(2*x - y, x-z, Overflow);
2571 if (LargerThan64Bits)
2572 Coeff =
umul_ov(Coeff1, Coeff2, Overflow);
2574 Coeff = Coeff1*Coeff2;
2586 if (Ops.
size() == 2)
return NewAddRec;
2587 Ops[Idx] = NewAddRec;
2588 Ops.
erase(Ops.
begin() + OtherIdx); --OtherIdx;
2606 for (
unsigned i = 0, e = Ops.
size(); i != e; ++i)
2610 static_cast<SCEVMulExpr *
>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2613 std::uninitialized_copy(Ops.
begin(), Ops.
end(), O);
2616 UniqueSCEVs.InsertNode(S, IP);
2628 "SCEVUDivExpr operand types don't match!");
2630 if (
const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
2631 if (RHSC->getValue()->equalsInt(1))
2636 if (!RHSC->getValue()->isZero()) {
2641 unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros();
2645 if (!RHSC->getValue()->getValue().isPowerOf2())
2651 dyn_cast<SCEVConstant>(AR->getStepRecurrence(*
this))) {
2653 const APInt &StepInt = Step->getValue()->getValue();
2654 const APInt &DivInt = RHSC->getValue()->getValue();
2655 if (!StepInt.
urem(DivInt) &&
2661 for (
unsigned i = 0, e = AR->getNumOperands(); i != e; ++i)
2670 if (StartC && !DivInt.
urem(StepInt) &&
2676 const APInt &StartRem = StartInt.
urem(StepInt);
2683 if (
const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) {
2685 for (
unsigned i = 0, e = M->getNumOperands(); i != e; ++i)
2689 for (
unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
2690 const SCEV *Op = M->getOperand(i);
2692 if (!isa<SCEVUDivExpr>(Div) &&
getMulExpr(Div, RHSC) == Op) {
2701 if (
const SCEVAddExpr *
A = dyn_cast<SCEVAddExpr>(LHS)) {
2703 for (
unsigned i = 0, e =
A->getNumOperands(); i != e; ++i)
2707 for (
unsigned i = 0, e =
A->getNumOperands(); i != e; ++i) {
2709 if (isa<SCEVUDivExpr>(Op) ||
2714 if (Operands.
size() ==
A->getNumOperands())
2720 if (
const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
2721 Constant *LHSCV = LHSC->getValue();
2722 Constant *RHSCV = RHSC->getValue();
2734 if (
const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
return S;
2737 UniqueSCEVs.InsertNode(S, IP);
2769 if (
const SCEVConstant *RHSCst = dyn_cast<SCEVConstant>(RHS)) {
2773 dyn_cast<SCEVConstant>(Mul->
getOperand(0))) {
2774 if (LHSCst == RHSCst) {
2783 APInt Factor =
gcd(LHSCst, RHSCst);
2785 LHSCst = cast<SCEVConstant>(
2786 getConstant(LHSCst->getValue()->getValue().udiv(Factor)));
2787 RHSCst = cast<SCEVConstant>(
2788 getConstant(RHSCst->getValue()->getValue().udiv(Factor)));
2820 if (
const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
2821 if (StepChrec->getLoop() == L) {
2822 Operands.
append(StepChrec->op_begin(), StepChrec->op_end());
2835 if (Operands.
size() == 1)
return Operands[0];
2838 for (
unsigned i = 1, e = Operands.
size(); i != e; ++i)
2840 "SCEVAddRecExpr operand types don't match!");
2841 for (
unsigned i = 0, e = Operands.
size(); i != e; ++i)
2843 "SCEVAddRecExpr operand is not loop-invariant!");
2860 if (
const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) {
2861 const Loop *NestedLoop = NestedAR->getLoop();
2867 NestedAR->op_end());
2868 Operands[0] = NestedAR->getStart();
2872 bool AllInvariant =
true;
2873 for (
unsigned i = 0, e = Operands.
size(); i != e; ++i)
2875 AllInvariant =
false;
2887 AllInvariant =
true;
2888 for (
unsigned i = 0, e = NestedOperands.size(); i != e; ++i)
2890 AllInvariant =
false;
2900 return getAddRecExpr(NestedOperands, NestedLoop, InnerFlags);
2904 Operands[0] = NestedAR;
2912 for (
unsigned i = 0, e = Operands.
size(); i != e; ++i)
2917 static_cast<SCEVAddRecExpr *
>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2920 std::uninitialized_copy(Operands.
begin(), Operands.
end(), O);
2922 O, Operands.
size(), L);
2923 UniqueSCEVs.InsertNode(S, IP);
2946 for (
const SCEV *IndexExpr : IndexExprs) {
2948 if (
StructType *STy = dyn_cast<StructType>(CurTy)) {
2950 ConstantInt *Index = cast<SCEVConstant>(IndexExpr)->getValue();
2955 TotalOffset =
getAddExpr(TotalOffset, FieldOffset);
2958 CurTy = STy->getTypeAtIndex(Index);
2961 CurTy = cast<SequentialType>(CurTy)->getElementType();
2968 const SCEV *LocalOffset =
getMulExpr(IndexExpr, ElementSize, Wrap);
2971 TotalOffset =
getAddExpr(TotalOffset, LocalOffset);
2976 return getAddExpr(BaseExpr, TotalOffset, Wrap);
2989 assert(!Ops.
empty() &&
"Cannot get empty smax!");
2990 if (Ops.
size() == 1)
return Ops[0];
2993 for (
unsigned i = 1, e = Ops.
size(); i != e; ++i)
2995 "SCEVSMaxExpr operand types don't match!");
3003 if (
const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
3005 assert(Idx < Ops.
size());
3006 while (
const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
3010 RHSC->getValue()->getValue()));
3013 if (Ops.
size() == 1)
return Ops[0];
3014 LHSC = cast<SCEVConstant>(Ops[0]);
3018 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(
true)) {
3021 }
else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(
true)) {
3027 if (Ops.
size() == 1)
return Ops[0];
3036 if (Idx < Ops.
size()) {
3037 bool DeletedSMax =
false;
3038 while (
const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) {
3040 Ops.
append(SMax->op_begin(), SMax->op_end());
3051 for (
unsigned i = 0, e = Ops.
size()-1; i != e; ++i)
3054 if (Ops[i] == Ops[i+1] ||
3063 if (Ops.
size() == 1)
return Ops[0];
3065 assert(!Ops.
empty() &&
"Reduced smax down to nothing!");
3071 for (
unsigned i = 0, e = Ops.
size(); i != e; ++i)
3072 ID.AddPointer(Ops[i]);
3074 if (
const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
return S;
3076 std::uninitialized_copy(Ops.
begin(), Ops.
end(), O);
3079 UniqueSCEVs.InsertNode(S, IP);
3093 assert(!Ops.
empty() &&
"Cannot get empty umax!");
3094 if (Ops.
size() == 1)
return Ops[0];
3097 for (
unsigned i = 1, e = Ops.
size(); i != e; ++i)
3099 "SCEVUMaxExpr operand types don't match!");
3107 if (
const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
3109 assert(Idx < Ops.
size());
3110 while (
const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
3114 RHSC->getValue()->getValue()));
3117 if (Ops.
size() == 1)
return Ops[0];
3118 LHSC = cast<SCEVConstant>(Ops[0]);
3122 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(
false)) {
3125 }
else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(
false)) {
3131 if (Ops.
size() == 1)
return Ops[0];
3140 if (Idx < Ops.
size()) {
3141 bool DeletedUMax =
false;
3142 while (
const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) {
3144 Ops.
append(UMax->op_begin(), UMax->op_end());
3155 for (
unsigned i = 0, e = Ops.
size()-1; i != e; ++i)
3158 if (Ops[i] == Ops[i+1] ||
3167 if (Ops.
size() == 1)
return Ops[0];
3169 assert(!Ops.
empty() &&
"Reduced umax down to nothing!");
3175 for (
unsigned i = 0, e = Ops.
size(); i != e; ++i)
3176 ID.AddPointer(Ops[i]);
3178 if (
const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
return S;
3180 std::uninitialized_copy(Ops.
begin(), Ops.
end(), O);
3183 UniqueSCEVs.InsertNode(S, IP);
3229 if (
SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) {
3230 assert(cast<SCEVUnknown>(S)->getValue() == V &&
3231 "Stale SCEVUnknown in uniquing map!");
3236 FirstUnknown = cast<SCEVUnknown>(S);
3237 UniqueSCEVs.InsertNode(S, IP);
3257 assert(
isSCEVable(Ty) &&
"Type is not SCEVable!");
3266 assert(
isSCEVable(Ty) &&
"Type is not SCEVable!");
3273 assert(Ty->
isPointerTy() &&
"Unexpected non-pointer non-integer type!");
3278 return &CouldNotCompute;
3286 struct FindInvalidSCEVUnknown {
3288 FindInvalidSCEVUnknown() { FindOne =
false; }
3289 bool follow(
const SCEV *S) {
3290 switch (static_cast<SCEVTypes>(S->
getSCEVType())) {
3294 if (!cast<SCEVUnknown>(S)->getValue())
3301 bool isDone()
const {
return FindOne; }
3305 bool ScalarEvolution::checkValidity(
const SCEV *S)
const {
3306 FindInvalidSCEVUnknown F;
3319 if (I != ValueExprMap.
end()) {
3320 const SCEV *S = I->second;
3321 if (checkValidity(S))
3324 ValueExprMap.
erase(I);
3326 const SCEV *S = createSCEV(V);
3357 const SCEV *AllOnes =
3384 "Cannot truncate or zero extend with non-integer arguments!");
3401 "Cannot truncate or zero extend with non-integer arguments!");
3417 "Cannot noop or zero extend with non-integer arguments!");
3419 "getNoopOrZeroExtend cannot truncate!");
3433 "Cannot noop or sign extend with non-integer arguments!");
3435 "getNoopOrSignExtend cannot truncate!");
3450 "Cannot noop or any extend with non-integer arguments!");
3452 "getNoopOrAnyExtend cannot truncate!");
3465 "Cannot truncate or noop with non-integer arguments!");
3467 "getTruncateOrNoop cannot extend!");
3478 const SCEV *PromotedLHS = LHS;
3479 const SCEV *PromotedRHS = RHS;
3494 const SCEV *PromotedLHS = LHS;
3495 const SCEV *PromotedRHS = RHS;
3514 if (
const SCEVCastExpr *Cast = dyn_cast<SCEVCastExpr>(V)) {
3517 else if (
const SCEVNAryExpr *NAry = dyn_cast<SCEVNAryExpr>(V)) {
3518 const SCEV *PtrOp =
nullptr;
3521 if ((*I)->getType()->isPointerTy()) {
3542 Worklist.
push_back(cast<Instruction>(U));
3550 ScalarEvolution::ForgetSymbolicName(
Instruction *PN,
const SCEV *SymName) {
3556 while (!Worklist.
empty()) {
3558 if (!Visited.
insert(I).second)
3562 ValueExprMap.
find_as(static_cast<Value *>(I));
3563 if (It != ValueExprMap.
end()) {
3564 const SCEV *Old = It->second;
3568 if (Old != SymName && !
hasOperand(Old, SymName))
3578 if (!isa<PHINode>(I) ||
3579 !isa<SCEVUnknown>(Old) ||
3580 (I != PN && Old == SymName)) {
3581 forgetMemoizedResults(Old);
3582 ValueExprMap.
erase(It);
3593 const SCEV *ScalarEvolution::createNodeForPHI(
PHINode *PN) {
3599 Value *BEValueV =
nullptr, *StartValueV =
nullptr;
3605 }
else if (BEValueV != V) {
3609 }
else if (!StartValueV) {
3611 }
else if (StartValueV != V) {
3612 StartValueV =
nullptr;
3616 if (BEValueV && StartValueV) {
3619 assert(ValueExprMap.
find_as(PN) == ValueExprMap.
end() &&
3620 "PHI node already processed?");
3632 if (
const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) {
3635 unsigned FoundIndex = Add->getNumOperands();
3636 for (
unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
3637 if (Add->getOperand(i) == SymbolicName)
3638 if (FoundIndex == e) {
3643 if (FoundIndex != Add->getNumOperands()) {
3646 for (
unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
3647 if (i != FoundIndex)
3654 (isa<SCEVAddRecExpr>(Accum) &&
3655 cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) {
3660 if (
const AddOperator *OBO = dyn_cast<AddOperator>(BEValueV)) {
3661 if (OBO->getOperand(0) == PN) {
3662 if (OBO->hasNoUnsignedWrap())
3664 if (OBO->hasNoSignedWrap())
3674 if (
GEP->isInBounds() &&
GEP->getOperand(0) == PN) {
3699 ForgetSymbolicName(PN, SymbolicName);
3705 dyn_cast<SCEVAddRecExpr>(BEValue)) {
3711 if (AddRec->getLoop() == L && AddRec->isAffine()) {
3717 AddRec->getOperand(1))) {
3720 const SCEV *PHISCEV =
3727 ForgetSymbolicName(PN, SymbolicName);
3772 return C->getValue()->getValue().countTrailingZeros();
3793 for (
unsigned i = 1, e =
A->getNumOperands(); MinOpRes && i != e; ++i)
3798 if (
const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
3802 for (
unsigned i = 1, e = M->getNumOperands();
3803 SumOpRes != BitWidth && i != e; ++i)
3812 for (
unsigned i = 1, e =
A->getNumOperands(); MinOpRes && i != e; ++i)
3817 if (
const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) {
3820 for (
unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
3825 if (
const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) {
3828 for (
unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
3833 if (
const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
3836 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
3838 F->getParent()->getDataLayout(), 0, AC,
nullptr, DT);
3839 return Zeros.countTrailingOnes();
3854 unsigned NumRanges = MD->getNumOperands() / 2;
3855 assert(NumRanges >= 1);
3857 for (
unsigned i = 0; i < NumRanges; ++i) {
3859 mdconst::extract<ConstantInt>(MD->getOperand(2 * i + 0));
3861 mdconst::extract<ConstantInt>(MD->getOperand(2 * i + 1));
3863 TotalRange = TotalRange.
unionWith(Range);
3878 ScalarEvolution::getRange(
const SCEV *S,
3879 ScalarEvolution::RangeSignHint SignHint) {
3881 SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? UnsignedRanges
3886 if (I != Cache.
end())
3890 return setRange(
C, SignHint,
ConstantRange(
C->getValue()->getValue()));
3899 if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED)
3900 ConservativeResult =
3909 if (
const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
3911 for (
unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
3912 X = X.
add(getRange(Add->getOperand(i), SignHint));
3913 return setRange(Add, SignHint, ConservativeResult.intersectWith(X));
3916 if (
const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
3918 for (
unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i)
3919 X = X.
multiply(getRange(Mul->getOperand(i), SignHint));
3920 return setRange(Mul, SignHint, ConservativeResult.intersectWith(X));
3923 if (
const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) {
3925 for (
unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i)
3926 X = X.
smax(getRange(SMax->getOperand(i), SignHint));
3927 return setRange(SMax, SignHint, ConservativeResult.intersectWith(X));
3930 if (
const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) {
3932 for (
unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i)
3933 X = X.
umax(getRange(UMax->getOperand(i), SignHint));
3934 return setRange(UMax, SignHint, ConservativeResult.intersectWith(X));
3937 if (
const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) {
3940 return setRange(UDiv, SignHint,
3941 ConservativeResult.intersectWith(X.
udiv(Y)));
3946 return setRange(ZExt, SignHint,
3947 ConservativeResult.intersectWith(X.
zeroExtend(BitWidth)));
3952 return setRange(SExt, SignHint,
3953 ConservativeResult.intersectWith(X.
signExtend(BitWidth)));
3958 return setRange(Trunc, SignHint,
3959 ConservativeResult.intersectWith(X.
truncate(BitWidth)));
3962 if (
const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
3966 if (
const SCEVConstant *
C = dyn_cast<SCEVConstant>(AddRec->getStart()))
3967 if (!
C->getValue()->isZero())
3968 ConservativeResult =
3969 ConservativeResult.intersectWith(
3975 bool AllNonNeg =
true;
3976 bool AllNonPos =
true;
3977 for (
unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
3982 ConservativeResult = ConservativeResult.intersectWith(
3986 ConservativeResult = ConservativeResult.intersectWith(
3988 APInt(BitWidth, 1)));
3992 if (AddRec->isAffine()) {
3993 Type *Ty = AddRec->getType();
3995 if (!isa<SCEVCouldNotCompute>(MaxBECount) &&
4007 const SCEV *Start = AddRec->getStart();
4008 const SCEV *Step = AddRec->getStepRecurrence(*
this);
4014 StartURange.
add(MaxBECountRange.
multiply(StepSRange));
4020 if (ZExtStartURange.
add(ZExtMaxBECountRange.
multiply(SExtStepSRange)) ==
4026 bool IsFullRange = Min.
isMinValue() && Max.isMaxValue();
4028 ConservativeResult =
4029 ConservativeResult.intersectWith(
ConstantRange(Min, Max + 1));
4034 StartSRange.
add(MaxBECountRange.
multiply(StepSRange));
4042 if (SExtStartSRange.
add(ZExtMaxBECountRange.
multiply(SExtStepSRange)) ==
4050 ConservativeResult =
4051 ConservativeResult.intersectWith(
ConstantRange(Min, Max + 1));
4056 return setRange(AddRec, SignHint, ConservativeResult);
4059 if (
const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
4063 ConservativeResult = ConservativeResult.intersectWith(MDRange.
getValue());
4068 const DataLayout &
DL = F->getParent()->getDataLayout();
4069 if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED) {
4071 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
4073 if (Ones != ~Zeros + 1)
4074 ConservativeResult =
4075 ConservativeResult.intersectWith(
ConstantRange(Ones, ~Zeros + 1));
4077 assert(SignHint == ScalarEvolution::HINT_RANGE_SIGNED &&
4078 "generalize as needed!");
4081 ConservativeResult = ConservativeResult.intersectWith(
4086 return setRange(U, SignHint, ConservativeResult);
4089 return setRange(S, SignHint, ConservativeResult);
4095 const SCEV *ScalarEvolution::createSCEV(
Value *V) {
4099 unsigned Opcode = Instruction::UserOp1;
4101 Opcode = I->getOpcode();
4107 if (!DT->isReachableFromEntry(I->getParent()))
4109 }
else if (
ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
4110 Opcode =
CE->getOpcode();
4111 else if (
ConstantInt *CI = dyn_cast<ConstantInt>(V))
4113 else if (isa<ConstantPointerNull>(V))
4115 else if (
GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
4122 case Instruction::Add: {
4138 unsigned Opcode = Op->getValueID() - Value::InstructionVal;
4139 if (Opcode != Instruction::Add && Opcode != Instruction::Sub)
4141 U = cast<Operator>(Op);
4143 if (Opcode == Instruction::Sub)
4151 case Instruction::Mul: {
4156 Op->
getValueID() == Instruction::Mul + Value::InstructionVal;
4158 U = cast<Operator>(Op);
4164 case Instruction::UDiv:
4167 case Instruction::Sub:
4174 if (CI->isNullValue())
4176 if (CI->isAllOnesValue())
4178 const APInt &
A = CI->getValue();
4187 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
4189 F->getParent()->getDataLayout(), 0, AC,
nullptr, DT);
4191 APInt EffectiveMask =
4193 if ((LZ != 0 || TZ != 0) && !((~A & ~KnownZero) & EffectiveMask)) {
4216 const APInt &CIVal = CI->getValue();
4218 (CIVal.getBitWidth() - CIVal.countLeadingZeros())) {
4236 if (CI->getValue().isSignBit())
4241 if (CI->isAllOnesValue())
4249 if (
ConstantInt *LCI = dyn_cast<ConstantInt>(BO->getOperand(1)))
4251 LCI->getValue() == CI->getValue())
4255 const SCEV *Z0 = Z->getOperand();
4268 APInt Trunc = CI->getValue().
trunc(Z0TySize);
4277 case Instruction::Shl:
4286 if (SA->getValue().uge(BitWidth))
4295 case Instruction::LShr:
4304 if (SA->getValue().uge(BitWidth))
4313 case Instruction::AShr:
4317 if (L->getOpcode() == Instruction::Shl &&
4325 if (CI->getValue().uge(BitWidth))
4328 uint64_t Amt = BitWidth - CI->getZExtValue();
4329 if (Amt == BitWidth)
4330 return getSCEV(L->getOperand(0));
4339 case Instruction::Trunc:
4342 case Instruction::ZExt:
4345 case Instruction::SExt:
4348 case Instruction::BitCast:
4359 case Instruction::GetElementPtr:
4360 return createNodeForGEP(cast<GEPOperator>(U));
4363 return createNodeForPHI(cast<PHINode>(U));
4369 Value *LHS = ICI->getOperand(0);
4370 Value *RHS = ICI->getOperand(1);
4371 switch (ICI->getPredicate()) {
4424 isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->
isZero()) {
4439 isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->
isZero()) {
4488 assert(ExitingBlock &&
"Must pass a non-null exiting block!");
4490 "Exiting block must actually branch out of the loop!");
4529 assert(ExitingBlock &&
"Must pass a non-null exiting block!");
4531 "Exiting block must actually branch out of the loop!");
4541 if (
const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(TCMul))
4542 TCMul = Mul->getOperand(0);
4564 return getBackedgeTakenInfo(L).getExact(ExitingBlock,
this);
4579 return getBackedgeTakenInfo(L).getExact(
this);
4586 return getBackedgeTakenInfo(L).getMax(
this);
4601 const ScalarEvolution::BackedgeTakenInfo &
4602 ScalarEvolution::getBackedgeTakenInfo(
const Loop *L) {
4608 std::pair<DenseMap<const Loop *, BackedgeTakenInfo>::iterator,
bool> Pair =
4609 BackedgeTakenCounts.insert(std::make_pair(L, BackedgeTakenInfo()));
4611 return Pair.first->second;
4616 BackedgeTakenInfo Result = ComputeBackedgeTakenCount(L);
4621 "Computed backedge-taken count isn't loop invariant for loop!");
4622 ++NumTripCountsComputed;
4625 isa<PHINode>(L->
getHeader()->begin())) {
4627 ++NumTripCountsNotComputed;
4635 if (Result.hasAnyInfo()) {
4640 while (!Worklist.
empty()) {
4642 if (!Visited.
insert(I).second)
4646 ValueExprMap.
find_as(static_cast<Value *>(I));
4647 if (It != ValueExprMap.
end()) {
4648 const SCEV *Old = It->second;
4656 if (!isa<PHINode>(I) || !isa<SCEVUnknown>(Old)) {
4657 forgetMemoizedResults(Old);
4658 ValueExprMap.
erase(It);
4660 if (
PHINode *PN = dyn_cast<PHINode>(I))
4661 ConstantEvolutionLoopExitValue.erase(PN);
4673 return BackedgeTakenCounts.find(L)->second = Result;
4682 BackedgeTakenCounts.find(L);
4683 if (BTCPos != BackedgeTakenCounts.end()) {
4684 BTCPos->second.clear();
4685 BackedgeTakenCounts.erase(BTCPos);
4693 while (!Worklist.
empty()) {
4695 if (!Visited.
insert(I).second)
4699 ValueExprMap.
find_as(static_cast<Value *>(I));
4700 if (It != ValueExprMap.
end()) {
4701 forgetMemoizedResults(It->second);
4702 ValueExprMap.
erase(It);
4703 if (
PHINode *PN = dyn_cast<PHINode>(I))
4704 ConstantEvolutionLoopExitValue.erase(PN);
4728 while (!Worklist.
empty()) {
4730 if (!Visited.
insert(I).second)
4734 ValueExprMap.
find_as(static_cast<Value *>(I));
4735 if (It != ValueExprMap.
end()) {
4736 forgetMemoizedResults(It->second);
4737 ValueExprMap.
erase(It);
4738 if (
PHINode *PN = dyn_cast<PHINode>(I))
4739 ConstantEvolutionLoopExitValue.erase(PN);
4755 ScalarEvolution::BackedgeTakenInfo::getExact(ScalarEvolution *SE)
const {
4761 assert(ExitNotTaken.ExactNotTaken &&
"uninitialized not-taken info");
4763 const SCEV *BECount =
nullptr;
4764 for (
const ExitNotTakenInfo *ENT = &ExitNotTaken;
4765 ENT !=
nullptr; ENT = ENT->getNextExit()) {
4770 BECount = ENT->ExactNotTaken;
4771 else if (BECount != ENT->ExactNotTaken)
4774 assert(BECount &&
"Invalid not taken count for loop exit");
4780 ScalarEvolution::BackedgeTakenInfo::getExact(
BasicBlock *ExitingBlock,
4781 ScalarEvolution *SE)
const {
4782 for (
const ExitNotTakenInfo *ENT = &ExitNotTaken;
4783 ENT !=
nullptr; ENT = ENT->getNextExit()) {
4785 if (ENT->ExitingBlock == ExitingBlock)
4786 return ENT->ExactNotTaken;
4793 ScalarEvolution::BackedgeTakenInfo::getMax(ScalarEvolution *SE)
const {
4797 bool ScalarEvolution::BackedgeTakenInfo::hasOperand(
const SCEV *S,
4798 ScalarEvolution *SE)
const {
4802 if (!ExitNotTaken.ExitingBlock)
4805 for (
const ExitNotTakenInfo *ENT = &ExitNotTaken;
4806 ENT !=
nullptr; ENT = ENT->getNextExit()) {
4818 ScalarEvolution::BackedgeTakenInfo::BackedgeTakenInfo(
4820 bool Complete,
const SCEV *MaxCount) : Max(MaxCount) {
4823 ExitNotTaken.setIncomplete();
4825 unsigned NumExits = ExitCounts.size();
4826 if (NumExits == 0)
return;
4828 ExitNotTaken.ExitingBlock = ExitCounts[0].first;
4829 ExitNotTaken.ExactNotTaken = ExitCounts[0].second;
4830 if (NumExits == 1)
return;
4833 ExitNotTakenInfo *ENT =
new ExitNotTakenInfo[NumExits-1];
4835 ExitNotTakenInfo *PrevENT = &ExitNotTaken;
4836 for (
unsigned i = 1; i < NumExits; ++i, PrevENT = ENT, ++ENT) {
4837 PrevENT->setNextExit(ENT);
4838 ENT->ExitingBlock = ExitCounts[i].first;
4839 ENT->ExactNotTaken = ExitCounts[i].second;
4844 void ScalarEvolution::BackedgeTakenInfo::clear() {
4845 ExitNotTaken.ExitingBlock =
nullptr;
4846 ExitNotTaken.ExactNotTaken =
nullptr;
4847 delete[] ExitNotTaken.getNextExit();
4852 ScalarEvolution::BackedgeTakenInfo
4853 ScalarEvolution::ComputeBackedgeTakenCount(
const Loop *L) {
4858 bool CouldComputeBECount =
true;
4860 const SCEV *MustExitMaxBECount =
nullptr;
4861 const SCEV *MayExitMaxBECount =
nullptr;
4865 for (
unsigned i = 0, e = ExitingBlocks.
size(); i != e; ++i) {
4867 ExitLimit EL = ComputeExitLimit(L, ExitBB);
4874 CouldComputeBECount =
false;
4876 ExitCounts.
push_back(std::make_pair(ExitBB, EL.Exact));
4888 DT->dominates(ExitBB, Latch)) {
4889 if (!MustExitMaxBECount)
4890 MustExitMaxBECount = EL.Max;
4892 MustExitMaxBECount =
4897 MayExitMaxBECount = EL.Max;
4904 const SCEV *MaxBECount = MustExitMaxBECount ? MustExitMaxBECount :
4906 return BackedgeTakenInfo(ExitCounts, CouldComputeBECount, MaxBECount);
4911 ScalarEvolution::ExitLimit
4912 ScalarEvolution::ComputeExitLimit(
const Loop *L,
BasicBlock *ExitingBlock) {
4917 bool MustExecuteLoopHeader =
true;
4926 MustExecuteLoopHeader =
false;
4946 if (!MustExecuteLoopHeader && ExitingBlock != L->
getHeader()) {
4976 if (
BranchInst *BI = dyn_cast<BranchInst>(Term)) {
4977 assert(BI->isConditional() &&
"If unconditional, it can't be in loop!");
4979 return ComputeExitLimitFromCond(L, BI->getCondition(), BI->getSuccessor(0),
4980 BI->getSuccessor(1),
4985 return ComputeExitLimitFromSingleExitSwitch(L,
SI, Exit,
4999 ScalarEvolution::ExitLimit
5000 ScalarEvolution::ComputeExitLimitFromCond(
const Loop *L,
5004 bool ControlsExit) {
5009 bool EitherMayExit = L->
contains(TBB);
5010 ExitLimit EL0 = ComputeExitLimitFromCond(L, BO->getOperand(0), TBB, FBB,
5011 ControlsExit && !EitherMayExit);
5012 ExitLimit EL1 = ComputeExitLimitFromCond(L, BO->getOperand(1), TBB, FBB,
5013 ControlsExit && !EitherMayExit);
5016 if (EitherMayExit) {
5025 MaxBECount = EL1.Max;
5027 MaxBECount = EL0.Max;
5033 assert(L->
contains(FBB) &&
"Loop block has no successor in loop!");
5034 if (EL0.Max == EL1.Max)
5035 MaxBECount = EL0.Max;
5036 if (EL0.Exact == EL1.Exact)
5037 BECount = EL0.Exact;
5040 return ExitLimit(BECount, MaxBECount);
5044 bool EitherMayExit = L->
contains(FBB);
5045 ExitLimit EL0 = ComputeExitLimitFromCond(L, BO->getOperand(0), TBB, FBB,
5046 ControlsExit && !EitherMayExit);
5047 ExitLimit EL1 = ComputeExitLimitFromCond(L, BO->getOperand(1), TBB, FBB,
5048 ControlsExit && !EitherMayExit);
5051 if (EitherMayExit) {
5060 MaxBECount = EL1.Max;
5062 MaxBECount = EL0.Max;
5068 assert(L->
contains(TBB) &&
"Loop block has no successor in loop!");
5069 if (EL0.Max == EL1.Max)
5070 MaxBECount = EL0.Max;
5071 if (EL0.Exact == EL1.Exact)
5072 BECount = EL0.Exact;
5075 return ExitLimit(BECount, MaxBECount);
5081 if (
ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond))
5082 return ComputeExitLimitFromICmp(L, ExitCondICmp, TBB, FBB, ControlsExit);
5088 if (
ConstantInt *CI = dyn_cast<ConstantInt>(ExitCond)) {
5089 if (L->
contains(FBB) == !CI->getZExtValue())
5098 return ComputeExitCountExhaustively(L, ExitCond, !L->
contains(TBB));
5104 ScalarEvolution::ExitLimit
5105 ScalarEvolution::ComputeExitLimitFromICmp(
const Loop *L,
5109 bool ControlsExit) {
5122 ComputeLoadConstantCompareExitLimit(LI, RHS, L, Cond);
5123 if (ItCnt.hasAnyInfo())
5147 if (
const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS))
5148 if (
const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS))
5149 if (AddRec->getLoop() == L) {
5154 const SCEV *
Ret = AddRec->getNumIterationsInRange(CompRange, *
this);
5155 if (!isa<SCEVCouldNotCompute>(Ret))
return Ret;
5161 ExitLimit EL = HowFarToZero(
getMinusSCEV(LHS, RHS), L, ControlsExit);
5162 if (EL.hasAnyInfo())
return EL;
5167 ExitLimit EL = HowFarToNonZero(
getMinusSCEV(LHS, RHS), L);
5168 if (EL.hasAnyInfo())
return EL;
5174 ExitLimit EL = HowManyLessThans(LHS, RHS, L, IsSigned, ControlsExit);
5175 if (EL.hasAnyInfo())
return EL;
5181 ExitLimit EL = HowManyGreaterThans(LHS, RHS, L, IsSigned, ControlsExit);
5182 if (EL.hasAnyInfo())
return EL;
5187 dbgs() <<
"ComputeBackedgeTakenCount ";
5189 dbgs() <<
"[unsigned] ";
5190 dbgs() << *LHS <<
" "
5192 <<
" " << *RHS <<
"\n";
5196 return ComputeExitCountExhaustively(L, ExitCond, !L->
contains(TBB));
5199 ScalarEvolution::ExitLimit
5200 ScalarEvolution::ComputeExitLimitFromSingleExitSwitch(
const Loop *L,
5203 bool ControlsExit) {
5204 assert(!L->
contains(ExitingBlock) &&
"Not an exiting block!");
5211 "Default case must not exit the loop!");
5216 ExitLimit EL = HowFarToZero(
getMinusSCEV(LHS, RHS), L, ControlsExit);
5217 if (EL.hasAnyInfo())
5225 ScalarEvolution &SE) {
5228 assert(isa<SCEVConstant>(Val) &&
5229 "Evaluation of SCEV at constant didn't fold correctly?");
5230 return cast<SCEVConstant>(Val)->getValue();
5236 ScalarEvolution::ExitLimit
5237 ScalarEvolution::ComputeLoadConstantCompareExitLimit(
5255 !cast<Constant>(GEP->
getOperand(1))->isNullValue())
5259 Value *VarIdx =
nullptr;
5260 std::vector<Constant*> Indexes;
5261 unsigned VarIdxNum = 0;
5264 Indexes.push_back(CI);
5265 }
else if (!isa<ConstantInt>(GEP->
getOperand(i))) {
5269 Indexes.push_back(
nullptr);
5285 !isa<SCEVConstant>(IdxExpr->
getOperand(0)) ||
5290 for (
unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) {
5292 cast<IntegerType>(IdxExpr->
getType()), IterationNum);
5296 Indexes[VarIdxNum] = Val;
5304 if (!isa<ConstantInt>(Result))
break;
5305 if (cast<ConstantInt>(Result)->getValue().isMinValue()) {
5307 dbgs() <<
"\n***\n*** Computed loop count " << *ItCst
5308 <<
"\n*** From global " << *GV <<
"*** BB: " << *L->
getHeader()
5311 ++NumArrayLenItCounts;
5322 if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
5323 isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I) ||
5327 if (
const CallInst *CI = dyn_cast<CallInst>(I))
5328 if (
const Function *F = CI->getCalledFunction())
5339 if (isa<PHINode>(I)) {
5360 OpE = UseInst->
op_end(); OpI != OpE; ++OpI) {
5362 if (isa<Constant>(*OpI))
continue;
5372 P = PHIMap.
lookup(OpInst);
5381 if (PHI && PHI != P)
5398 if (
PHINode *PN = dyn_cast<PHINode>(I)) {
5416 if (
Constant *C = dyn_cast<Constant>(V))
return C;
5418 if (!I)
return nullptr;
5429 if (isa<PHINode>(I))
return nullptr;
5442 if (!C)
return nullptr;
5446 if (
CmpInst *CI = dyn_cast<CmpInst>(I))
5449 if (
LoadInst *LI = dyn_cast<LoadInst>(I)) {
5462 ScalarEvolution::getConstantEvolutionLoopExitValue(
PHINode *PN,
5466 ConstantEvolutionLoopExitValue.find(PN);
5467 if (I != ConstantEvolutionLoopExitValue.end())
5471 return ConstantEvolutionLoopExitValue[PN] =
nullptr;
5473 Constant *&RetVal = ConstantEvolutionLoopExitValue[PN];
5477 assert(PN->
getParent() == Header &&
"Can't evaluate PHI not in loop header!");
5488 if (!StartCST)
continue;
5489 CurrentIterVals[
PHI] = StartCST;
5491 if (!CurrentIterVals.
count(PN))
5492 return RetVal =
nullptr;
5498 return RetVal =
nullptr;
5501 unsigned IterationNum = 0;
5502 const DataLayout &DL = F->getParent()->getDataLayout();
5503 for (; ; ++IterationNum) {
5504 if (IterationNum == NumIterations)
5505 return RetVal = CurrentIterVals[PN];
5514 NextIterVals[PN] = NextPHI;
5516 bool StoppedEvolving = NextPHI == CurrentIterVals[PN];
5523 I = CurrentIterVals.
begin(), E = CurrentIterVals.
end(); I != E; ++
I){
5525 if (!PHI || PHI == PN || PHI->
getParent() != Header)
continue;
5526 PHIsToCompute.
push_back(std::make_pair(PHI, I->second));
5530 for (
SmallVectorImpl<std::pair<PHINode *, Constant*> >::const_iterator
5531 I = PHIsToCompute.
begin(), E = PHIsToCompute.
end(); I != E; ++
I) {
5538 if (NextPHI != I->second)
5539 StoppedEvolving =
false;
5544 if (StoppedEvolving)
5545 return RetVal = CurrentIterVals[PN];
5547 CurrentIterVals.
swap(NextIterVals);
5556 const SCEV *ScalarEvolution::ComputeExitCountExhaustively(
const Loop *L,
5568 assert(PN->
getParent() == Header &&
"Can't evaluate PHI not in loop header!");
5578 if (!StartCST)
continue;
5579 CurrentIterVals[
PHI] = StartCST;
5581 if (!CurrentIterVals.
count(PN))
5588 const DataLayout &DL = F->getParent()->getDataLayout();
5589 for (
unsigned IterationNum = 0; IterationNum !=
MaxIterations;++IterationNum){
5590 ConstantInt *CondVal = dyn_cast_or_null<ConstantInt>(
5596 if (CondVal->
getValue() == uint64_t(ExitWhen)) {
5597 ++NumBruteForceTripCountsComputed;
5609 I = CurrentIterVals.
begin(), E = CurrentIterVals.
end(); I != E; ++
I){
5611 if (!PHI || PHI->
getParent() != Header)
continue;
5615 E = PHIsToCompute.
end(); I != E; ++
I) {
5618 if (NextPHI)
continue;
5623 CurrentIterVals.
swap(NextIterVals);
5643 for (
unsigned u = 0; u < Values.
size(); u++) {
5644 if (Values[u].first == L)
5645 return Values[u].second ? Values[u].second : V;
5647 Values.
push_back(std::make_pair(L, static_cast<const SCEV *>(
nullptr)));
5649 const SCEV *
C = computeSCEVAtScope(V, L);
5651 for (
unsigned u = Values2.
size(); u > 0; u--) {
5652 if (Values2[u - 1].first == L) {
5653 Values2[u - 1].second =
C;
5665 switch (static_cast<SCEVTypes>(V->
getSCEVType())) {
5670 return cast<SCEVConstant>(V)->getValue();
5694 if (
PointerType *PTy = dyn_cast<PointerType>(C->getType())) {
5695 unsigned AS = PTy->getAddressSpace();
5701 if (!C2)
return nullptr;
5718 if (
PointerType *PTy = dyn_cast<PointerType>(C->getType())) {
5719 if (PTy->getElementType()->isStructTy())
5734 if (C->getType()->isPointerTy())
return nullptr;
5759 const SCEV *ScalarEvolution::computeSCEVAtScope(
const SCEV *V,
const Loop *L) {
5760 if (isa<SCEVConstant>(V))
return V;
5764 if (
const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) {
5765 if (
Instruction *I = dyn_cast<Instruction>(SU->getValue())) {
5766 const Loop *LI = (*this->LI)[I->getParent()];
5768 if (
PHINode *PN = dyn_cast<PHINode>(I))
5776 dyn_cast<SCEVConstant>(BackedgeTakenCount)) {
5780 Constant *RV = getConstantEvolutionLoopExitValue(PN,
5781 BTCC->getValue()->getValue(),
5793 bool MadeImprovement =
false;
5794 for (
unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
5795 Value *Op = I->getOperand(i);
5796 if (
Constant *
C = dyn_cast<Constant>(Op)) {
5809 MadeImprovement |= OrigV != OpV;
5822 if (MadeImprovement) {
5824 const DataLayout &DL = F->getParent()->getDataLayout();
5825 if (
const CmpInst *CI = dyn_cast<CmpInst>(I))
5827 Operands[1],
DL, TLI);
5828 else if (
const LoadInst *LI = dyn_cast<LoadInst>(I)) {
5829 if (!LI->isVolatile())
5847 for (
unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) {
5849 if (OpAtScope != Comm->getOperand(i)) {
5853 Comm->op_begin()+i);
5856 for (++i; i != e; ++i) {
5858 NewOps.push_back(OpAtScope);
5860 if (isa<SCEVAddExpr>(Comm))
5862 if (isa<SCEVMulExpr>(Comm))
5864 if (isa<SCEVSMaxExpr>(Comm))
5866 if (isa<SCEVUMaxExpr>(Comm))
5875 if (
const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) {
5878 if (LHS == Div->getLHS() && RHS == Div->getRHS())
5885 if (
const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) {
5889 for (
unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
5891 if (OpAtScope == AddRec->getOperand(i))
5897 AddRec->op_begin()+i);
5899 for (++i; i != e; ++i)
5902 const SCEV *FoldedRec =
5916 if (!AddRec->getLoop()->contains(L)) {
5923 return AddRec->evaluateAtIteration(BackedgeTakenCount, *
this);
5931 if (Op == Cast->getOperand())
5938 if (Op == Cast->getOperand())
5945 if (Op == Cast->getOperand())
5969 ScalarEvolution &SE) {
5971 assert(BW == B.
getBitWidth() &&
"Bit widths must be the same.");
5972 assert(A != 0 &&
"A must be non-zero.");
5994 APInt Mod(BW + 1, 0);
6011 static std::pair<const SCEV *,const SCEV *>
6013 assert(AddRec->
getNumOperands() == 3 &&
"This is not a quadratic chrec!");
6019 if (!LC || !MC || !NC) {
6021 return std::make_pair(CNC, CNC);
6024 uint32_t BitWidth = LC->getValue()->getValue().getBitWidth();
6025 const APInt &L = LC->getValue()->getValue();
6026 const APInt &M = MC->getValue()->getValue();
6027 const APInt &
N = NC->getValue()->getValue();
6028 APInt Two(BitWidth, 2);
6029 APInt Four(BitWidth, 4);
6032 using namespace APIntOps;
6045 SqrtTerm -= Four * (A * C);
6047 if (SqrtTerm.isNegative()) {
6050 return std::make_pair(CNC, CNC);
6055 APInt SqrtVal(SqrtTerm.sqrt());
6063 return std::make_pair(CNC, CNC);
6085 ScalarEvolution::ExitLimit
6086 ScalarEvolution::HowFarToZero(
const SCEV *V,
const Loop *L,
bool ControlsExit) {
6088 if (
const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
6090 if (C->getValue()->isZero())
return C;
6095 if (!AddRec || AddRec->
getLoop() != L)
6101 std::pair<const SCEV *,const SCEV *> Roots =
6107 dbgs() <<
"HFTZ: " << *V <<
" - sol#1: " << *R1
6108 <<
" sol#2: " << *R2 <<
"\n";
6115 if (!CB->getZExtValue())
6155 if (!StepC || StepC->getValue()->equalsInt(0))
6163 bool CountDown = StepC->getValue()->getValue().isNegative();
6169 if (StepC->getValue()->equalsInt(1) || StepC->getValue()->isAllOnesValue()) {
6171 const SCEV *MaxBECount;
6180 return ExitLimit(Distance, MaxBECount);
6188 const APInt &StepV = StepC->getValue()->getValue();
6205 return ExitLimit(Exact, Exact);
6209 if (
const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start))
6219 ScalarEvolution::ExitLimit
6220 ScalarEvolution::HowFarToNonZero(
const SCEV *V,
const Loop *L) {
6227 if (
const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
6243 std::pair<BasicBlock *, BasicBlock *>
6244 ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(
BasicBlock *BB) {
6249 return std::make_pair(Pred, BB);
6254 if (
Loop *L = LI->getLoopFor(BB))
6257 return std::pair<BasicBlock *, BasicBlock *>();
6268 if (A == B)
return true;
6272 if (
const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A))
6273 if (
const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B))
6274 if (
const Instruction *AI = dyn_cast<Instruction>(AU->getValue()))
6275 if (
const Instruction *BI = dyn_cast<Instruction>(BU->getValue()))
6276 if (AI->isIdenticalTo(BI) && !AI->mayReadFromMemory())
6287 const SCEV *&LHS,
const SCEV *&RHS,
6289 bool Changed =
false;
6296 if (
const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
6298 if (
const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
6301 RHSC->getValue())->isNullValue())
6302 goto trivially_false;
6304 goto trivially_true;
6316 const Loop *L = AR->getLoop();
6326 if (
const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) {
6327 const APInt &RA = RC->getValue()->getValue();
6334 if (
const SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(LHS))
6335 if (
const SCEVMulExpr *ME = dyn_cast<SCEVMulExpr>(AE->getOperand(0)))
6336 if (AE->getNumOperands() == 2 && ME->getNumOperands() == 2 &&
6337 ME->getOperand(0)->isAllOnesValue()) {
6338 RHS = AE->getOperand(1);
6339 LHS = ME->getOperand(1);
6344 if ((RA - 1).isMinValue()) {
6362 if ((RA + 1).isMaxValue()) {
6380 if ((RA - 1).isMinSignedValue()) {
6398 if ((RA + 1).isMaxSignedValue()) {
6421 if ((RA + 1).isMaxValue()) {
6435 if ((RA - 1).isMinValue()) {
6449 if ((RA + 1).isMaxSignedValue()) {
6463 if ((RA - 1).isMinSignedValue()) {
6477 goto trivially_true;
6479 goto trivially_false;
6491 }
else if (!
getSignedRange(LHS).getSignedMin().isMinSignedValue()) {
6584 const SCEV *LHS,
const SCEV *RHS) {
6594 bool LeftGuarded =
false;
6595 bool RightGuarded =
false;
6600 if (!RAR)
return true;
6605 const Loop *L = RAR->getLoop();
6608 if (!LAR)
return true;
6609 RightGuarded =
true;
6612 if (LeftGuarded && RightGuarded)
6616 return isKnownPredicateWithRanges(Pred, LHS, RHS);
6621 const SCEV *LHS,
const SCEV *RHS) {
6699 const SCEV *LHS,
const SCEV *RHS) {
6702 if (!L)
return true;
6704 if (isKnownPredicateWithRanges(Pred, LHS, RHS))
return true;
6712 if (LoopContinuePredicate && LoopContinuePredicate->
isConditional() &&
6713 isImpliedCond(Pred, LHS, RHS,
6719 for (
auto &AssumeVH : AC->assumptions()) {
6722 auto *CI = cast<CallInst>(AssumeVH);
6726 if (isImpliedCond(Pred, LHS, RHS, CI->getArgOperand(0),
false))
6730 struct ClearWalkingBEDominatingCondsOnExit {
6731 ScalarEvolution &SE;
6733 explicit ClearWalkingBEDominatingCondsOnExit(ScalarEvolution &SE)
6736 ~ClearWalkingBEDominatingCondsOnExit() {
6737 SE.WalkingBEDominatingConds =
false;
6743 if (WalkingBEDominatingConds)
6746 WalkingBEDominatingConds =
true;
6747 ClearWalkingBEDominatingCondsOnExit ClearOnExit(*
this);
6752 if (!DT->isReachableFromEntry(L->
getHeader()))
6759 assert(DTN &&
"should reach the loop header before reaching the root!");
6767 if (!ContinuePredicate || !ContinuePredicate->
isConditional())
6781 assert(DT->dominates(DominatingEdge, Latch) &&
"should be!");
6783 if (isImpliedCond(Pred, LHS, RHS, Condition,
6798 const SCEV *LHS,
const SCEV *RHS) {
6801 if (!L)
return false;
6803 if (isKnownPredicateWithRanges(Pred, LHS, RHS))
return true;
6808 for (std::pair<BasicBlock *, BasicBlock *>
6811 Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) {
6815 if (!LoopEntryPredicate ||
6819 if (isImpliedCond(Pred, LHS, RHS,
6826 for (
auto &AssumeVH : AC->assumptions()) {
6829 auto *CI = cast<CallInst>(AssumeVH);
6833 if (isImpliedCond(Pred, LHS, RHS, CI->getArgOperand(0),
false))
6849 : Cond(C), LoopPreds(LP) {
6850 Pending = !LoopPreds.insert(Cond).second;
6854 LoopPreds.erase(Cond);
6862 Value *FoundCondValue,
6869 if (
BinaryOperator *BO = dyn_cast<BinaryOperator>(FoundCondValue)) {
6872 return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) ||
6873 isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse);
6876 return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) ||
6877 isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse);
6882 if (!ICI)
return false;
6922 if (FoundLHS == FoundRHS)
6926 if (LHS == FoundRHS || RHS == FoundLHS) {
6927 if (isa<SCEVConstant>(RHS)) {
6937 if (FoundPred == Pred)
6938 return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS);
6943 if (isa<SCEVConstant>(RHS))
6944 return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS);
6947 RHS, LHS, FoundLHS, FoundRHS);
6952 (isa<SCEVConstant>(FoundLHS) || isa<SCEVConstant>(FoundRHS))) {
6955 const SCEV *V =
nullptr;
6957 if (isa<SCEVConstant>(FoundLHS)) {
6958 C = cast<SCEVConstant>(FoundLHS);
6961 C = cast<SCEVConstant>(FoundRHS);
6978 APInt SharperMin = Min + 1;
6985 if (isImpliedCondOperands(Pred, LHS, RHS, V,
7000 if (isImpliedCondOperands(Pred, LHS, RHS, V,
getConstant(Min)))
7013 if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS))
7017 if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS))
7029 const SCEV *FoundLHS,
7030 const SCEV *FoundRHS) {
7031 if (isImpliedCondOperandsViaRanges(Pred, LHS, RHS, FoundLHS, FoundRHS))
7034 return isImpliedCondOperandsHelper(Pred, LHS, RHS,
7035 FoundLHS, FoundRHS) ||
7037 isImpliedCondOperandsHelper(Pred, LHS, RHS,
7064 template<
typename MaxExprType>
7066 const SCEV *Candidate) {
7067 const MaxExprType *MaxExpr =
dyn_cast<MaxExprType>(MaybeMaxExpr);
7068 if (!MaxExpr)
return false;
7070 auto It = std::find(MaxExpr->op_begin(), MaxExpr->op_end(), Candidate);
7071 return It != MaxExpr->op_end();
7076 template<
typename MaxExprType>
7078 const SCEV *MaybeMinExpr,
7079 const SCEV *Candidate) {
7084 return IsMaxConsistingOf<MaxExprType>(MaybeMaxExpr, SE.
getNotSCEV(Candidate));
7092 const SCEV *LHS,
const SCEV *RHS) {
7103 IsMinConsistingOf<SCEVSMaxExpr>(SE, LHS, RHS) ||
7105 IsMaxConsistingOf<SCEVSMaxExpr>(RHS, LHS);
7113 IsMinConsistingOf<SCEVUMaxExpr>(SE, LHS, RHS) ||
7115 IsMaxConsistingOf<SCEVUMaxExpr>(RHS, LHS);
7127 const SCEV *FoundLHS,
7128 const SCEV *FoundRHS) {
7129 auto IsKnownPredicateFull =
7131 return isKnownPredicateWithRanges(Pred, LHS, RHS) ||
7176 const SCEV *FoundLHS,
7177 const SCEV *FoundRHS) {
7178 if (!isa<SCEVConstant>(RHS) || !isa<SCEVConstant>(FoundRHS))
7184 if (!AddLHS || AddLHS->
getOperand(1) != FoundLHS ||
7188 APInt ConstFoundRHS = cast<SCEVConstant>(FoundRHS)->getValue()->getValue();
7198 cast<SCEVConstant>(AddLHS->
getOperand(0))->getValue()->getValue();
7203 APInt ConstRHS = cast<SCEVConstant>(RHS)->getValue()->getValue();
7209 return SatisfyingLHSRange.
contains(LHSRange);
7215 bool ScalarEvolution::doesIVOverflowOnLT(
const SCEV *RHS,
const SCEV *Stride,
7216 bool IsSigned,
bool NoWrap) {
7217 if (NoWrap)
return false;
7229 return (MaxValue - MaxStrideMinusOne).
slt(MaxRHS);
7238 return (MaxValue - MaxStrideMinusOne).
ult(MaxRHS);
7244 bool ScalarEvolution::doesIVOverflowOnGT(
const SCEV *RHS,
const SCEV *Stride,
7245 bool IsSigned,
bool NoWrap) {
7246 if (NoWrap)
return false;
7258 return (MinValue + MaxStrideMinusOne).
sgt(MinRHS);
7267 return (MinValue + MaxStrideMinusOne).
ugt(MinRHS);
7272 const SCEV *ScalarEvolution::computeBECount(
const SCEV *Delta,
const SCEV *Step,
7287 ScalarEvolution::ExitLimit
7288 ScalarEvolution::HowManyLessThans(
const SCEV *LHS,
const SCEV *RHS,
7289 const Loop *L,
bool IsSigned,
7290 bool ControlsExit) {
7301 bool NoWrap = ControlsExit &&
7314 if (!Stride->
isOne() && doesIVOverflowOnLT(RHS, Stride, IsSigned, NoWrap))
7320 const SCEV *End = RHS;
7326 if (NoWrap && isa<SCEVConstant>(Diff)) {
7335 const SCEV *BECount = computeBECount(
getMinusSCEV(End, Start), Stride,
false);
7354 const SCEV *MaxBECount;
7355 if (isa<SCEVConstant>(BECount))
7356 MaxBECount = BECount;
7358 MaxBECount = computeBECount(
getConstant(MaxEnd - MinStart),
7361 if (isa<SCEVCouldNotCompute>(MaxBECount))
7362 MaxBECount = BECount;
7364 return ExitLimit(BECount, MaxBECount);
7367 ScalarEvolution::ExitLimit
7368 ScalarEvolution::HowManyGreaterThans(
const SCEV *LHS,
const SCEV *RHS,
7369 const Loop *L,
bool IsSigned,
7370 bool ControlsExit) {
7381 bool NoWrap = ControlsExit &&
7394 if (!Stride->
isOne() && doesIVOverflowOnGT(RHS, Stride, IsSigned, NoWrap))
7401 const SCEV *End = RHS;
7407 if (NoWrap && isa<SCEVConstant>(Diff)) {
7416 const SCEV *BECount = computeBECount(
getMinusSCEV(Start, End), Stride,
false);
7437 if (isa<SCEVConstant>(BECount))
7438 MaxBECount = BECount;
7440 MaxBECount = computeBECount(
getConstant(MaxStart - MinEnd),
7443 if (isa<SCEVCouldNotCompute>(MaxBECount))
7444 MaxBECount = BECount;
7446 return ExitLimit(BECount, MaxBECount);
7455 ScalarEvolution &SE)
const {
7461 if (!
SC->getValue()->isZero()) {
7467 dyn_cast<SCEVAddRecExpr>(Shifted))
7468 return ShiftedAddRec->getNumIterationsInRange(
7469 Range.
subtract(
SC->getValue()->getValue()), SE);
7510 if (Range.
contains(Val->getValue()))
7517 "Linear scev computation is off in a bad way!");
7531 std::pair<const SCEV *,const SCEV *> Roots =
7540 if (!CB->getZExtValue())
7555 if (!Range.
contains(R1Val->getValue()))
7565 if (Range.
contains(R1Val->getValue()))
7578 FindUndefs() :
Found(false) {}
7580 bool follow(
const SCEV *S) {
7581 if (
const SCEVUnknown *C = dyn_cast<SCEVUnknown>(S)) {
7582 if (isa<UndefValue>(C->
getValue()))
7584 }
else if (
const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) {
7585 if (isa<UndefValue>(C->
getValue()))
7592 bool isDone()
const {
7611 struct SCEVCollectStrides {
7612 ScalarEvolution &SE;
7616 : SE(SE), Strides(S) {}
7618 bool follow(
const SCEV *S) {
7620 Strides.push_back(AR->getStepRecurrence(SE));
7623 bool isDone()
const {
return false; }
7627 struct SCEVCollectTerms {
7633 bool follow(
const SCEV *S) {
7634 if (isa<SCEVUnknown>(S) || isa<SCEVMulExpr>(S)) {
7645 bool isDone()
const {
return false; }
7653 SCEVCollectStrides StrideCollector(*
this, Strides);
7657 dbgs() <<
"Strides:\n";
7658 for (
const SCEV *S : Strides)
7659 dbgs() << *S <<
"\n";
7662 for (
const SCEV *S : Strides) {
7663 SCEVCollectTerms TermCollector(Terms);
7668 dbgs() <<
"Terms:\n";
7669 for (
const SCEV *
T : Terms)
7670 dbgs() << *
T <<
"\n";
7677 int Last = Terms.
size() - 1;
7678 const SCEV *Step = Terms[Last];
7682 if (
const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Step)) {
7684 for (
const SCEV *Op : M->operands())
7685 if (!isa<SCEVConstant>(Op))
7695 for (
const SCEV *&Term : Terms) {
7698 SCEVDivision::divide(SE, Term, Step, &Q, &R);
7708 Terms.erase(std::remove_if(Terms.begin(), Terms.end(), [](
const SCEV *E) {
7709 return isa<SCEVConstant>(E);
7713 if (Terms.size() > 0)
7722 struct FindParameter {
7723 bool FoundParameter;
7724 FindParameter() : FoundParameter(false) {}
7726 bool follow(
const SCEV *S) {
7727 if (isa<SCEVUnknown>(S)) {
7728 FoundParameter =
true;
7735 bool isDone()
const {
7737 return FoundParameter;
7749 return F.FoundParameter;
7755 for (
const SCEV *
T : Terms)
7763 if (
const SCEVMulExpr *Expr = dyn_cast<SCEVMulExpr>(S))
7764 return Expr->getNumOperands();
7769 if (isa<SCEVConstant>(T))
7772 if (isa<SCEVUnknown>(T))
7775 if (
const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(T)) {
7777 for (
const SCEV *Op : M->operands())
7778 if (!isa<SCEVConstant>(Op))
7791 Ty =
Store->getValueOperand()->getType();
7793 Ty =
Load->getType();
7805 const SCEV *ElementSize)
const {
7807 if (Terms.
size() < 1 || !ElementSize)
7816 dbgs() <<
"Terms:\n";
7817 for (
const SCEV *
T : Terms)
7818 dbgs() << *
T <<
"\n";
7822 std::sort(Terms.
begin(), Terms.
end());
7826 std::sort(Terms.
begin(), Terms.
end(), [](
const SCEV *LHS,
const SCEV *RHS) {
7830 ScalarEvolution &SE = *
const_cast<ScalarEvolution *
>(
this);
7833 for (
const SCEV *&Term : Terms) {
7835 SCEVDivision::divide(SE, Term, ElementSize, &Q, &R);
7842 for (
const SCEV *
T : Terms)
7847 dbgs() <<
"Terms after sorting:\n";
7848 for (
const SCEV *
T : NewTerms)
7849 dbgs() << *
T <<
"\n";
7852 if (NewTerms.
empty() ||
7862 dbgs() <<
"Sizes:\n";
7863 for (
const SCEV *S : Sizes)
7864 dbgs() << *S <<
"\n";
7878 if (
auto AR = dyn_cast<SCEVAddRecExpr>(Expr))
7879 if (!AR->isAffine())
7882 const SCEV *Res = Expr;
7883 int Last = Sizes.
size() - 1;
7884 for (
int i = Last; i >= 0; i--) {
7886 SCEVDivision::divide(*
this, Res, Sizes[i], &Q, &R);
7889 dbgs() <<
"Res: " << *Res <<
"\n";
7890 dbgs() <<
"Sizes[i]: " << *Sizes[i] <<
"\n";
7891 dbgs() <<
"Res divided by Sizes[i]:\n";
7892 dbgs() <<
"Quotient: " << *Q <<
"\n";
7893 dbgs() <<
"Remainder: " << *R <<
"\n";
7903 if (isa<SCEVAddRecExpr>(R)) {
7920 std::reverse(Subscripts.
begin(), Subscripts.
end());
7923 dbgs() <<
"Subscripts:\n";
7924 for (
const SCEV *S : Subscripts)
7925 dbgs() << *S <<
"\n";
7981 const SCEV *ElementSize) {
7998 if (Subscripts.
empty())
8002 dbgs() <<
"succeeded to delinearize " << *Expr <<
"\n";
8003 dbgs() <<
"ArrayDecl[UnknownSize]";
8004 for (
const SCEV *S : Sizes)
8005 dbgs() <<
"[" << *S <<
"]";
8007 dbgs() <<
"\nArrayRef";
8008 for (
const SCEV *S : Subscripts)
8009 dbgs() <<
"[" << *S <<
"]";
8018 void ScalarEvolution::SCEVCallbackVH::deleted() {
8019 assert(SE &&
"SCEVCallbackVH called with a null ScalarEvolution!");
8020 if (
PHINode *PN = dyn_cast<PHINode>(getValPtr()))
8021 SE->ConstantEvolutionLoopExitValue.erase(PN);
8022 SE->ValueExprMap.
erase(getValPtr());
8026 void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(
Value *V) {
8027 assert(SE &&
"SCEVCallbackVH called with a null ScalarEvolution!");
8032 Value *Old = getValPtr();
8035 while (!Worklist.
empty()) {
8041 if (!Visited.
insert(U).second)
8043 if (
PHINode *PN = dyn_cast<PHINode>(U))
8044 SE->ConstantEvolutionLoopExitValue.erase(PN);
8045 SE->ValueExprMap.
erase(U);
8049 if (
PHINode *PN = dyn_cast<PHINode>(Old))
8050 SE->ConstantEvolutionLoopExitValue.erase(PN);
8051 SE->ValueExprMap.
erase(Old);
8055 ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(
Value *V, ScalarEvolution *se)
8063 :
FunctionPass(ID), WalkingBEDominatingConds(false), ValuesAtScopes(64),
8064 LoopDispositions(64), BlockDispositions(64), FirstUnknown(nullptr) {
8070 AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
8071 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
8072 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
8073 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
8080 for (
SCEVUnknown *U = FirstUnknown; U; U = U->Next)
8082 FirstUnknown =
nullptr;
8084 ValueExprMap.
clear();
8089 BackedgeTakenCounts.begin(), E = BackedgeTakenCounts.end();
8094 assert(PendingLoopPredicates.
empty() &&
"isImpliedCond garbage");
8095 assert(!WalkingBEDominatingConds &&
"isLoopBackedgeGuardedByCond garbage!");
8097 BackedgeTakenCounts.clear();
8098 ConstantEvolutionLoopExitValue.clear();
8099 ValuesAtScopes.clear();
8100 LoopDispositions.clear();
8101 BlockDispositions.clear();
8102 UnsignedRanges.clear();
8103 SignedRanges.clear();
8104 UniqueSCEVs.clear();
8105 SCEVAllocator.
Reset();
8127 L->
getHeader()->printAsOperand(OS,
false);
8132 if (ExitBlocks.
size() != 1)
8133 OS <<
"<multiple exits> ";
8138 OS <<
"Unpredictable backedge-taken count. ";
8143 L->
getHeader()->printAsOperand(OS,
false);
8149 OS <<
"Unpredictable max backedge-taken count. ";
8162 ScalarEvolution &SE = *
const_cast<ScalarEvolution *
>(
this);
8164 OS <<
"Classifying expressions for: ";
8165 F->printAsOperand(OS,
false);
8168 if (
isSCEVable(I->getType()) && !isa<CmpInst>(*I)) {
8173 if (!isa<SCEVCouldNotCompute>(SV)) {
8180 const Loop *L = LI->getLoopFor((*I).getParent());
8186 if (!isa<SCEVCouldNotCompute>(AtUse)) {
8195 OS <<
"\t\t" "Exits: ";
8198 OS <<
"<<Unknown>>";
8207 OS <<
"Determining loop execution counts for: ";
8208 F->printAsOperand(OS,
false);
8216 auto &Values = LoopDispositions[S];
8217 for (
auto &V : Values) {
8218 if (V.getPointer() == L)
8223 auto &Values2 = LoopDispositions[S];
8224 for (
auto &V :
make_range(Values2.rbegin(), Values2.rend())) {
8225 if (V.getPointer() == L) {
8234 ScalarEvolution::computeLoopDisposition(
const SCEV *S,
const Loop *L) {
8235 switch (static_cast<SCEVTypes>(S->
getSCEVType())) {
8276 bool HasVarying =
false;
8303 if (
Instruction *I = dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue()))
8322 auto &Values = BlockDispositions[S];
8323 for (
auto &V : Values) {
8324 if (V.getPointer() == BB)
8329 auto &Values2 = BlockDispositions[S];
8330 for (
auto &V :
make_range(Values2.rbegin(), Values2.rend())) {
8331 if (V.getPointer() == BB) {
8340 ScalarEvolution::computeBlockDisposition(
const SCEV *S,
const BasicBlock *BB) {
8341 switch (static_cast<SCEVTypes>(S->
getSCEVType())) {
8388 dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue())) {
8389 if (I->getParent() == BB)
8391 if (DT->properlyDominates(I->getParent(), BB))
8417 SCEVSearch(
const SCEV *
N): Node(N), IsFound(false) {}
8419 bool follow(
const SCEV *S) {
8420 IsFound |= (S == Node);
8423 bool isDone()
const {
return IsFound; }
8428 SCEVSearch Search(Op);
8430 return Search.IsFound;
8433 void ScalarEvolution::forgetMemoizedResults(
const SCEV *S) {
8434 ValuesAtScopes.erase(S);
8435 LoopDispositions.erase(S);
8436 BlockDispositions.erase(S);
8437 UnsignedRanges.erase(S);
8438 SignedRanges.erase(S);
8441 BackedgeTakenCounts.begin(), E = BackedgeTakenCounts.end(); I != E; ) {
8442 BackedgeTakenInfo &BEInfo = I->second;
8443 if (BEInfo.hasOperand(S,
this)) {
8445 BackedgeTakenCounts.erase(I++);
8457 while ((Pos = Str.find(From, Pos)) != std::string::npos) {
8469 std::string &S = Map[L];
8489 ScalarEvolution &SE = *
const_cast<ScalarEvolution *
>(
this);
8494 VerifyMap BackedgeDumpsOld, BackedgeDumpsNew;
8506 assert(BackedgeDumpsOld.
size() == BackedgeDumpsNew.
size() &&
8507 "New loops suddenly appeared!");
8510 OldE = BackedgeDumpsOld.
end(),
8511 NewI = BackedgeDumpsNew.
begin();
8512 OldI != OldE; ++OldI, ++NewI) {
8513 assert(OldI->first == NewI->first &&
"Loop order changed!");
8520 if (OldI->second != NewI->second &&
8521 OldI->second.find(
"undef") == std::string::npos &&
8522 NewI->second.find(
"undef") == std::string::npos &&
8523 OldI->second !=
"***COULDNOTCOMPUTE***" &&
8524 NewI->second !=
"***COULDNOTCOMPUTE***") {
8525 dbgs() <<
"SCEVValidator: SCEV for loop '"
8526 << OldI->first->getHeader()->getName()
8527 <<
"' changed from '" << OldI->second
8528 <<
"' to '" << NewI->second <<
"'!\n";
NoWrapFlags getNoWrapFlags(NoWrapFlags Mask=NoWrapMask) const
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type (if unknown returns 0).
const SCEV * getTruncateOrNoop(const SCEV *V, Type *Ty)
getTruncateOrNoop - Return a SCEV corresponding to a conversion of the input value to the specified t...
const SCEV * evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const
evaluateAtIteration - Return the value of this chain of recurrences at the specified iteration number...
void AddPointer(const void *Ptr)
Add* - Add various data types to Bit data.
APInt multiplicativeInverse(const APInt &modulo) const
Computes the multiplicative inverse of this APInt for a given modulo.
Type * getSourceElementType() const
void push_back(const T &Elt)
APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
A parsed version of the target data layout string in and methods for querying it. ...
static cl::opt< bool > VerifySCEV("verify-scev", cl::desc("Verify ScalarEvolution's backedge taken counts (slow)"))
static ConstantInt * getFalse(LLVMContext &Context)
This class is the base class for the comparison instructions.
The SCEV properly dominates the block.
BasicBlock * getUniquePredecessor()
Return the predecessor of this block if it has a unique predecessor block.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT abs() const
Get the absolute value;.
static const SCEV * SolveLinEquationWithOverflow(const APInt &A, const APInt &B, ScalarEvolution &SE)
SolveLinEquationWithOverflow - Finds the minimum unsigned root of the following equation: ...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
SCEVCastExpr(const FoldingSetNodeIDRef ID, unsigned SCEVTy, const SCEV *op, Type *ty)
LLVM Argument representation.
uint64_t getZExtValue() const
Get zero extended value.
bool isOne() const
isOne - Return true if the expression is a constant one.
const SCEV * getExitCount(Loop *L, BasicBlock *ExitingBlock)
const SCEV * getConstant(ConstantInt *V)
STATISTIC(NumFunctions,"Total number of functions")
size_t size() const
size - Get the string size.
static const SCEV * getExtendAddRecStart(const SCEVAddRecExpr *AR, Type *Ty, ScalarEvolution *SE)
ValueT lookup(const KeyT &Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
ConstantRange sextOrTrunc(uint32_t BitWidth) const
Make this range have the bit width given by BitWidth.
APInt GreatestCommonDivisor(const APInt &Val1, const APInt &Val2)
Compute GCD of two APInt values.
static const SCEV * removeConstantFactors(ScalarEvolution &SE, const SCEV *T)
bool canConstantFoldCallTo(const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function...
A Module instance is used to store all the information related to an LLVM module. ...
LLVMContext & getContext() const
static void PushDefUseChildren(Instruction *I, SmallVectorImpl< Instruction * > &Worklist)
PushDefUseChildren - Push users of the given Instruction onto the given Worklist. ...
bool isZero() const
isZero - Return true if the expression is a constant zero.
void setBit(unsigned bitPosition)
Set a given bit to 1.
unsigned getNumOperands() const
static uint64_t Choose(uint64_t n, uint64_t k, bool &Overflow)
Compute the result of "n choose k", the binomial coefficient.
const SCEV * getPointerBase(const SCEV *V)
getPointerBase - Transitively follow the chain of pointer-type operands until reaching a SCEV that do...
ScalarEvolution - This class is the main scalar evolution driver.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
CallInst - This class represents a function call, abstracting a target machine's calling convention...
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
An immutable pass that tracks lazily created AssumptionCache objects.
scalar Scalar Evolution false
void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
bool isKnownNonNegative(const SCEV *S)
isKnownNonNegative - Test if the given expression is known to be non-negative.
static const SCEV * getPreStartForExtend(const SCEVAddRecExpr *AR, Type *Ty, ScalarEvolution *SE)
std::vector< LoopT * >::const_reverse_iterator reverse_iterator
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, bool InBounds=false, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
bool isSigned() const
Determine if this instruction is using a signed comparison.
static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
bool properlyDominates(const SCEV *S, const BasicBlock *BB)
properlyDominates - Return true if elements that makes up the given SCEV properly dominate the specif...
static void PushLoopPHIs(const Loop *L, SmallVectorImpl< Instruction * > &Worklist)
PushLoopPHIs - Push PHI nodes in the header of the given loop onto the given Worklist.
uint32_t GetMinTrailingZeros(const SCEV *S)
GetMinTrailingZeros - Determine the minimum number of zero bits that S is guaranteed to end in (at ev...
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
getStepRecurrence - This method constructs and returns the recurrence indicating how much this expres...
APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
bool isLoopExiting(const BlockT *BB) const
isLoopExiting - True if terminator in the block can branch to another block that is outside of the cu...
SCEVTruncateExpr - This class represents a truncation of an integer value to a smaller integer value...
bool isMask(unsigned numBits, const APInt &APIVal)
bool isLoopInvariant(const SCEV *S, const Loop *L)
isLoopInvariant - Return true if the value of the given SCEV is unchanging in the specified loop...
static ConstantInt * EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C, ScalarEvolution &SE)
LoopT * getParentLoop() const
const Function * getParent() const
Return the enclosing method, or null if none.
static const SCEV * getSignedOverflowLimitForStep(const SCEV *Step, ICmpInst::Predicate *Pred, ScalarEvolution *SE)
bool isLoopBackedgeGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS)
isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is protected by a conditional bet...
const SCEV *const * Operands
LoadInst - an instruction for reading from memory.
static int getMaxValue(uint64_t TSFlags)
FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > Tys=None)
Return the function type for an intrinsic.
RAII wrapper to prevent recursive application of isImpliedCond.
LoopT * getLoopFor(const BlockT *BB) const
getLoopFor - Return the inner most loop that BB lives in.
static ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the smallest range such that all values that may satisfy the given predicate with any value c...
SCEVCouldNotCompute - An object of this class is returned by queries that could not be answered...
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
void reserve(size_type N)
static bool HasSameValue(const SCEV *A, const SCEV *B)
HasSameValue - SCEV structural equivalence is usually sufficient for testing whether two expressions ...
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
const SCEV * getUMaxFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS)
getUMaxFromMismatchedTypes - Promote the operands to the wider of the types using zero-extension...
static Optional< ConstantRange > GetRangeFromMetadata(Value *V)
GetRangeFromMetadata - Helper method to assign a range to V from metadata present in the IR...
BlockT * getHeader() const
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
ConstantInt * findCaseDest(BasicBlock *BB)
findCaseDest - Finds the unique case value for a given successor.
SCEVCastExpr - This is the base class for unary cast operator classes.
ConstantRange smax(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a signed maximum of a value in thi...
bool isOffsetOf(Type *&STy, Constant *&FieldNo) const
Type * getPointerElementType() const
const SCEV * getStart() const
std::vector< LoopT * >::const_iterator iterator
iterator/begin/end - The interface to the top-level loops in the current function.
BlockT * getLoopLatch() const
getLoopLatch - If there is a single latch block for this loop, return it.
iterator begin()
Instruction iterator methods.
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
bool isKnownNonPositive(const SCEV *S)
isKnownNonPositive - Test if the given expression is known to be non-positive.
static void getLoopBackedgeTakenCounts(Loop *L, VerifyMap &Map, ScalarEvolution &SE)
getLoopBackedgeTakenCounts - Helper method for verifyAnalysis.
bool isNegative() const
Determine sign of this APInt.
The SCEV is loop-invariant.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
const SCEV * getGEPExpr(Type *PointeeType, const SCEV *BaseExpr, const SmallVectorImpl< const SCEV * > &IndexExprs, bool InBounds=false)
Returns an expression for a GEP.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT urem(const APInt &RHS) const
Unsigned remainder operation.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
void computeAccessFunctions(const SCEV *Expr, SmallVectorImpl< const SCEV * > &Subscripts, SmallVectorImpl< const SCEV * > &Sizes)
Return in Subscripts the access functions for each dimension in Sizes.
bool isUnconditional() const
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
bool isAlignOf(Type *&AllocTy) const
inst_iterator inst_begin(Function *F)
MarkPendingLoopPredicate(Value *C, DenseSet< Value * > &LP)
ConstantRange truncate(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly smaller than the current typ...
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it...
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
APInt urem(const APInt &LHS, const APInt &RHS)
Function for unsigned remainder operation.
const APInt & getValue() const
Return the constant as an APInt value reference.
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val()
StructType - Class to represent struct types.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT lshr(unsigned shiftAmt) const
Logical right-shift function.
Constant * ConstantFoldLoadFromConstPtr(Constant *C, const DataLayout &DL)
ConstantFoldLoadFromConstPtr - Return the value that a load from C would produce if it is constant an...
const SCEV *const * op_iterator
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
static Constant * AddOne(Constant *C)
Add one to a Constant.
void getExitingBlocks(SmallVectorImpl< BlockT * > &ExitingBlocks) const
getExitingBlocks - Return all blocks inside the loop that have successors outside of the loop...
uint64_t getTypeSizeInBits(Type *Ty) const
getTypeSizeInBits - Return the size in bits of the specified type, for which isSCEVable must return t...
bool isSized(SmallPtrSetImpl< const Type * > *Visited=nullptr) const
isSized - Return true if it makes sense to take the size of this type.
void AddInteger(signed I)
static Constant * getSExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Number of individual test Apply this number of consecutive mutations to each input exit after the first new interesting input is found the minimized corpus is saved into the first input directory Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
Windows NT (Windows on ARM)
SCEVCommutativeExpr - This node is the base class for n'ary commutative operators.
APInt umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
bool isIntN(unsigned N) const
Check if this APInt has an N-bits unsigned integer value.
void print(raw_ostream &OS, const Module *=nullptr) const override
print - Print out the internal state of the pass.
ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
op_iterator op_begin() const
void setNoWrapFlags(NoWrapFlags Flags)
Set flags for a non-recurrence without clearing previously set flags.
SCEVMulExpr - This node represents multiplication of some number of SCEVs.
static SCEV::NoWrapFlags LLVM_ATTRIBUTE_UNUSED_RESULT clearFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OffFlags)
ConstantRange multiply(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
APInt lshr(const APInt &LHS, unsigned shiftAmt)
Logical right-shift function.
void getExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
getExitBlocks - Return all of the successor blocks of this loop.
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
static const SCEV * BinomialCoefficient(const SCEV *It, unsigned K, ScalarEvolution &SE, Type *ResultTy)
BinomialCoefficient - Compute BC(It, K).
static Constant * getZExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
bool isKnownPredicate(ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS)
isKnownPredicate - Test if the given expression is known to satisfy the condition described by Pred...
reverse_iterator rbegin() const
ConstantExpr - a constant value that is initialized with an expression using other constant values...
const SCEV * getOffsetOfExpr(Type *IntTy, StructType *STy, unsigned FieldNo)
getOffsetOfExpr - Return an expression for offsetof on the given field with type IntTy ...
ConstantRange getSignedRange(const SCEV *S)
getSignedRange - Determine the signed range for a particular SCEV.
SCEVAddRecExpr - This node represents a polynomial recurrence on the trip count of the specified loop...
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
const T & getValue() const LLVM_LVALUE_FUNCTION
APInt udiv(const APInt &LHS, const APInt &RHS)
Unsigned division function for APInt.
bool sgt(const APInt &RHS) const
Signed greather than comparison.
BasicBlock * getSuccessor(unsigned i) const
ConstantRange unionWith(const ConstantRange &CR) const
Return the range that results from the union of this range with another range.
const SCEV * getSizeOfExpr(Type *IntTy, Type *AllocTy)
getSizeOfExpr - Return an expression for sizeof AllocTy that is type IntTy
Base class for the actual dominator tree node.
unsigned getActiveBits() const
Compute the number of active bits in the value.
APInt sdiv(const APInt &LHS, const APInt &RHS)
Signed division function for APInt.
StoreInst - an instruction for storing to memory.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT shl(unsigned shiftAmt) const
Left-shift function.
static cl::opt< unsigned > MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden, cl::desc("Maximum number of iterations SCEV will ""symbolically execute a constant ""derived loop"), cl::init(100))
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
bool isArrayTy() const
isArrayTy - True if this is an instance of ArrayType.
static Constant * getUDiv(Constant *C1, Constant *C2, bool isExact=false)
APInt ashr(const APInt &LHS, unsigned shiftAmt)
Arithmetic right-shift function.
Type * getEffectiveSCEVType(Type *Ty) const
getEffectiveSCEVType - Return a type with the same bitwidth as the given type and which represents ho...
ConstantRange subtract(const APInt &CI) const
Subtract the specified constant from the endpoints of this constant range.
const char * getOpcodeName() const
const SCEV * getAddRecExpr(const SCEV *Start, const SCEV *Step, const Loop *L, SCEV::NoWrapFlags Flags)
getAddRecExpr - Get an add recurrence expression for the specified loop.
static std::pair< const SCEV *, const SCEV * > SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE)
SolveQuadraticEquation - Find the roots of the quadratic equation for the given quadratic chrec {L...
size_t getNumOperands() const
PointerType - Class to represent pointers.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
static ConstantRange makeSatisfyingICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the largest range such that all values in the returned range satisfy the given predicate with...
static cl::opt< unsigned > MaxIterations("max-cg-scc-iterations", cl::ReallyHidden, cl::init(4))
Interval::succ_iterator succ_end(Interval *I)
unsigned getNumIncomingValues() const
getNumIncomingValues - Return the number of incoming edges
std::vector< LoopT * >::const_reverse_iterator reverse_iterator
uint64_t getElementOffset(unsigned Idx) const
bool isLoopEntryGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS)
isLoopEntryGuardedByCond - Test whether entry to the loop is protected by a conditional between LHS a...
static const SCEV * MatchNotExpr(const SCEV *Expr)
If Expr computes ~A, return A else return nullptr.
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
const SCEV * getNumIterationsInRange(ConstantRange Range, ScalarEvolution &SE) const
getNumIterationsInRange - Return the number of iterations of this loop that produce values in the spe...
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
GetElementPtrInst - an instruction for type-safe pointer arithmetic to access elements of arrays and ...
const SCEV * getCouldNotCompute()
Visit all nodes in the expression tree using worklist traversal.
SCEVUnknown - This means that we are dealing with an entirely unknown SCEV value, and only represent ...
initializer< Ty > init(const Ty &Val)
bool erase(const KeyT &Val)
bool isAffine() const
isAffine - Return true if this represents an expression A + B*x where A and B are loop invariant valu...
bool isSCEVable(Type *Ty) const
isSCEVable - Test if values of the given type are analyzable within the SCEV framework.
Subclasses of this class are all able to terminate a basic block.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
ConstantRange intersectWith(const ConstantRange &CR) const
Return the range that results from the intersection of this range with another range.
SCEVVisitor - This class defines a simple visitor class that may be used for various SCEV analysis pu...
bool isSizeOf(Type *&AllocTy) const
isSizeOf, isAlignOf, isOffsetOf - Test whether this is a special constant representing a type size...
APInt LLVM_ATTRIBUTE_UNUSED_RESULT trunc(unsigned width) const
Truncate to new width.
bool isFullSet() const
Return true if this set contains all of the elements possible for this data-type. ...
LLVM Basic Block Representation.
static const SCEV * rewrite(const SCEV *Scev, ScalarEvolution &SE, ValueToValueMap &Map, bool InterpretConsts=false)
SCEVUDivExpr - This class represents a binary unsigned division operation.
The instances of the Type class are immutable: once they are created, they are never changed...
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
This is an important class for using LLVM in a threaded context.
Type * getType() const
getType - Return the LLVM type of this SCEV expression.
LoopDisposition getLoopDisposition(const SCEV *S, const Loop *L)
getLoopDisposition - Return the "disposition" of the given SCEV with respect to the given loop...
static void GroupByComplexity(SmallVectorImpl< const SCEV * > &Ops, LoopInfo *LI)
GroupByComplexity - Given a list of SCEV objects, order them by their complexity, and group objects o...
BranchInst - Conditional or Unconditional Branch instruction.
static bool IsMinConsistingOf(ScalarEvolution &SE, const SCEV *MaybeMinExpr, const SCEV *Candidate)
Is MaybeMinExpr an SMin or UMin of Candidate and some other values?
bool sge(const APInt &RHS) const
Signed greather or equal comparison.
This is an important base class in LLVM.
bool isMaxSignedValue() const
Determine if this is the largest signed value.
void dump() const
dump - This method is used for debugging.
const SCEV * getOperand(unsigned i) const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
Allocate space at the specified alignment.
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
static bool canConstantEvolve(Instruction *I, const Loop *L)
Determine whether this instruction can constant evolve within this loop assuming its operands can all...
void visitAll(const SCEV *Root)
const SCEV * getSMaxExpr(const SCEV *LHS, const SCEV *RHS)
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT sext(unsigned width) const
Sign extend to a new width.
bool sle(const APInt &RHS) const
Signed less or equal comparison.
const SCEV * getSCEVAtScope(const SCEV *S, const Loop *L)
getSCEVAtScope - Return a SCEV expression for the specified value at the specified scope in the progr...
Represent the analysis usage information of a pass.
ConstantRange udiv(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an unsigned division of a value in...
BasicBlock * getIncomingBlock(unsigned i) const
getIncomingBlock - Return incoming basic block number i.
bool contains(const LoopT *L) const
contains - Return true if the specified loop is contained within in this loop.
bool isFalseWhenEqual() const
This is just a convenience.
bool hasOperand(const SCEV *S, const SCEV *Op) const
hasOperand - Test whether the given SCEV has Op as a direct or indirect operand.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
This instruction compares its operands according to the predicate given to the constructor.
const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
getMinusSCEV - Return LHS-RHS. Minus is represented in SCEV as A+B*-1.
const SCEV * getMaxBackedgeTakenCount(const Loop *L)
getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except return the least SCEV value that ...
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
unsigned getBitWidth() const
Return the number of bits in the APInt.
unsigned getValueID() const
Return an ID for the concrete type of this object.
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
op_range operands() const
const SCEVAddRecExpr * getPostIncExpr(ScalarEvolution &SE) const
getPostIncExpr - Return an expression representing the value of this expression one iteration of the ...
FunctionPass class - This class is used to implement most global optimizations.
BlockT * getExitingBlock() const
getExitingBlock - If getExitingBlocks would return exactly one block, return that block...
bool isMaxValue() const
Determine if this is the largest unsigned value.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT sdiv(const APInt &RHS) const
Signed division function for APInt.
Value * getOperand(unsigned i) const
unsigned getSmallConstantTripCount(Loop *L)
Returns the maximum trip count of the loop if it is a single-exit loop and we can compute a small max...
static Constant * getICmp(unsigned short pred, Constant *LHS, Constant *RHS, bool OnlyIfReduced=false)
get* - Return some common constants without having to specify the full Instruction::OPCODE identifier...
void print(raw_ostream &OS) const
Print out the bounds to a stream.
Class to represent integer types.
Predicate getPredicate() const
Return the predicate for this instruction.
ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
bool isKnownNegative(const SCEV *S)
isKnownNegative - Test if the given expression is known to be negative.
static Constant * getNot(Constant *C)
void forgetValue(Value *V)
forgetValue - This method should be called by the client when it has changed a value in a way that ma...
void setNoWrapFlags(NoWrapFlags Flags)
Set flags for a recurrence without clearing any previously set flags.
static SCEV::NoWrapFlags LLVM_ATTRIBUTE_UNUSED_RESULT setFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OnFlags)
static Constant * getAllOnesValue(Type *Ty)
Get the all ones value.
const SCEV * getLHS() const
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
void findArrayDimensions(SmallVectorImpl< const SCEV * > &Terms, SmallVectorImpl< const SCEV * > &Sizes, const SCEV *ElementSize) const
Compute the array dimensions Sizes from the set of Terms extracted from the memory access function of...
bool isEmptySet() const
Return true if this set contains no members.
const SCEV * getUMinFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS)
getUMinFromMismatchedTypes - Promote the operands to the wider of the types using zero-extension...
bool isPointerTy() const
isPointerTy - True if this is an instance of PointerType.
iterator erase(iterator I)
bool isNonConstantNegative() const
isNonConstantNegative - Return true if the specified scev is negated, but not a constant.
static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow)
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
const SCEV * getNoopOrZeroExtend(const SCEV *V, Type *Ty)
getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the input value to the specified...
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldCompareInstOperands - Attempt to constant fold a compare instruction (icmp/fcmp) with the...
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
ConstantRange add(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an addition of a value in this ran...
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
const SCEV * getRHS() const
const SCEV * getUDivExactExpr(const SCEV *LHS, const SCEV *RHS)
getUDivExactExpr - Get a canonical unsigned division expression, or something simpler if possible...
The SCEV is loop-variant (unknown).
static Constant * EvaluateExpression(Value *V, const Loop *L, DenseMap< Instruction *, Constant * > &Vals, const DataLayout &DL, const TargetLibraryInfo *TLI)
EvaluateExpression - Given an expression that passes the getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node in the loop has the value PHIVal.
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
bool isConditional() const
const SCEV * getSMinExpr(const SCEV *LHS, const SCEV *RHS)
bool ugt(const APInt &RHS) const
Unsigned greather than comparison.
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
static int numberOfTerms(const SCEV *S)
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
static PHINode * getConstantEvolvingPHI(Value *V, const Loop *L)
getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node in the loop that V is deri...
DomTreeNodeBase< NodeT > * getIDom() const
static const APInt gcd(const SCEVConstant *C1, const SCEVConstant *C2)
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
static PointerType * getUnqual(Type *ElementType)
PointerType::getUnqual - This constructs a pointer to an object of the specified type in the generic ...
This is the shared class of boolean and integer constants.
bool isKnownPositive(const SCEV *S)
isKnownPositive - Test if the given expression is known to be positive.
bool slt(const APInt &RHS) const
Signed less than comparison.
Value * getIncomingValue(unsigned i) const
getIncomingValue - Return incoming value number x
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
reverse_iterator rend() const
const SCEV * getTruncateExpr(const SCEV *Op, Type *Ty)
const SCEV * getTruncateOrSignExtend(const SCEV *V, Type *Ty)
getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the input value to the speci...
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
void delinearize(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 array...
DenseSet< Value * > & LoopPreds
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Constant * ConstantFoldInstOperands(unsigned Opcode, Type *DestTy, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands...
bool isAllOnesValue() const
isAllOnesValue - Return true if the expression is a constant all-ones value.
This is a utility class that provides an abstraction for the common functionality between Instruction...
Type * getType() const
All values are typed, get the type of this value.
Provides information about what library functions are available for the current target.
The SCEV dominates the block.
MDNode * getMetadata(unsigned KindID) const
getMetadata - Get the metadata of given kind attached to this Instruction.
unsigned getSmallConstantTripMultiple(Loop *L)
Returns the largest constant divisor of the trip count of the loop if it is a single-exit loop and we...
static SCEV::NoWrapFlags StrengthenNoWrapFlags(ScalarEvolution *SE, SCEVTypes Type, const SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags OldFlags)
This class represents a range of values.
bool isVolatile() const
isVolatile - Return true if this is a load from a volatile memory location.
const SCEV * getNoopOrSignExtend(const SCEV *V, Type *Ty)
getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the input value to the specified...
unsigned ComputeNumSignBits(Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
ComputeNumSignBits - Return the number of times the sign bit of the register is replicated into the o...
const APInt & getLower() const
Return the lower value for this range.
bool isTrueWhenEqual() const
This is just a convenience.
CHAIN = SC CHAIN, Imm128 - System call.
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
DenseMapIterator< SCEVCallbackVH, const SCEV *, DenseMapInfo< Value * >, detail::DenseMapPair< SCEVCallbackVH, const SCEV * > > iterator
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
ConstantInt * getValue() const
const SCEV * getUMaxExpr(const SCEV *LHS, const SCEV *RHS)
bool isNullValue() const
isNullValue - Return true if this is the value that would be returned by getNullValue.
APInt umin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
static SCEV::NoWrapFlags LLVM_ATTRIBUTE_UNUSED_RESULT maskFlags(SCEV::NoWrapFlags Flags, int Mask)
Convenient NoWrapFlags manipulation that hides enum casts and is visible in the ScalarEvolution name ...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
size_type count(const KeyT &Val) const
Return 1 if the specified key is in the map, 0 otherwise.
scalar Scalar Evolution Analysis
APInt smax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be signed.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
LoopDisposition
LoopDisposition - An enum describing the relationship between a SCEV and a loop.
Class for arbitrary precision integers.
SCEVAddExpr - This node represents an addition of some number of SCEVs.
const SCEV * getSignExtendExpr(const SCEV *Op, Type *Ty)
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool isIntegerTy() const
isIntegerTy - True if this is an instance of IntegerType.
void setPreservesAll()
Set by analyses that do not transform their input at all.
SCEVSMaxExpr - This class represents a signed maximum selection.
iterator_range< user_iterator > users()
BasicBlock * getSinglePredecessor()
Return the predecessor of this block if it has a single predecessor block.
static const SCEV * getUnsignedOverflowLimitForStep(const SCEV *Step, ICmpInst::Predicate *Pred, ScalarEvolution *SE)
DenseMap< const Loop *, std::string > VerifyMap
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
getAddExpr - Get a canonical add expression, or something simpler if possible.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
void visitAll(const SCEV *Root, SV &Visitor)
Use SCEVTraversal to visit all nodes in the given expression tree.
Value * getCondition() const
iterator insert(iterator I, T &&Elt)
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
bool isMinValue() const
Determine if this is the smallest unsigned value.
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
static Constant * BuildConstantFromSCEV(const SCEV *V)
This builds up a Constant using the ConstantExpr interface.
bool isStructTy() const
isStructTy - True if this is an instance of StructType.
ConstantRange umax(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an unsigned maximum of a value in ...
SCEVZeroExtendExpr - This class represents a zero extension of a small integer value to a larger inte...
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
bool isAllOnesValue() const
Determine if all bits are set.
static void replaceSubString(std::string &Str, StringRef From, StringRef To)
replaceSubString - Replaces all occurrences of From in Str with To.
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
void collectParametricTerms(const SCEV *Expr, SmallVectorImpl< const SCEV * > &Terms)
Collect parametric terms occurring in step expressions.
Value * getCondition() const
BasicBlock * getDefaultDest() const
The SCEV does not dominate the block.
FoldingSetNodeIDRef - This class describes a reference to an interned FoldingSetNodeID, which can be a useful to store node id data rather than using plain FoldingSetNodeIDs, since the 32-element SmallVector is often much larger than necessary, and the possibility of heap allocation means it requires a non-trivial destructor call.
bool isMinSignedValue() const
Determine if this is the smallest signed value.
void forgetLoop(const Loop *L)
forgetLoop - This method should be called by the client when it has changed a loop in a way that may ...
pointer data()
Return a pointer to the vector's buffer, even if empty().
SCEV - This class represents an analyzed expression in the program.
static IntegerType * getInt32Ty(LLVMContext &C)
bool isInBounds() const
Test whether this is an inbounds GEP, as defined by LangRef.html.
unsigned greater or equal
static PHINode * getConstantEvolvingPHIOperands(Instruction *UseInst, const Loop *L, DenseMap< Instruction *, PHINode * > &PHIMap)
getConstantEvolvingPHIOperands - Implement getConstantEvolvingPHI by recursing through each instructi...
static Instruction::CastOps getCastOpcode(const Value *Val, bool SrcIsSigned, Type *Ty, bool DstIsSigned)
Returns the opcode necessary to cast Val into Ty using usual casting rules.
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
static bool containsParameters(const SCEV *S)
SCEVSignExtendExpr - This class represents a sign extension of a small integer value to a larger inte...
iterator find(const KeyT &Val)
bool isSignBit() const
Check if the APInt's value is returned by getSignBit.
BlockDisposition
BlockDisposition - An enum describing the relationship between a SCEV and a basic block...
bool isKnownNonZero(const SCEV *S)
isKnownNonZero - Test if the given expression is known to be non-zero.
BlockT * getLoopPredecessor() const
getLoopPredecessor - If the given loop's header has exactly one unique predecessor outside the loop...
APInt smin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
SCEVUMaxExpr - This class represents an unsigned maximum selection.
static ConstantRange makeConstantRange(Predicate pred, const APInt &C)
Initialize a set of values that all satisfy the predicate with C.
static bool CollectAddOperandsWithScales(DenseMap< const SCEV *, APInt > &M, SmallVectorImpl< const SCEV * > &NewOps, APInt &AccumulatedConstant, const SCEV *const *Ops, size_t NumOperands, const APInt &Scale, ScalarEvolution &SE)
CollectAddOperandsWithScales - Process the given Ops list, which is a list of operands to be added un...
AnalysisUsage & addRequiredTransitive()
const Loop * getLoop() const
bool all_of(R &&Range, UnaryPredicate &&P)
Provide wrappers to std::all_of which take ranges instead of having to pass being/end explicitly...
iterator find_as(const LookupKeyT &Val)
Alternate version of find() which allows a different, and possibly less expensive, key type.
INITIALIZE_PASS_BEGIN(ScalarEvolution,"scalar-evolution","Scalar Evolution Analysis", false, true) INITIALIZE_PASS_END(ScalarEvolution
int compare(DigitsT LDigits, int16_t LScale, DigitsT RDigits, int16_t RScale)
Compare two scaled numbers.
ConstantRange getUnsignedRange(const SCEV *S)
getUnsignedRange - Determine the unsigned range for a particular SCEV.
SwitchInst - Multiway switch.
const SCEV * getBackedgeTakenCount(const Loop *L)
getBackedgeTakenCount - If the specified loop has a predictable backedge-taken count, return it, otherwise return a SCEVCouldNotCompute object.
const APInt & getUpper() const
Return the upper value for this range.
bool isSingleEdge() const
bool SimplifyICmpOperands(ICmpInst::Predicate &Pred, const SCEV *&LHS, const SCEV *&RHS, unsigned Depth=0)
SimplifyICmpOperands - Simplify LHS and RHS in a comparison with predicate Pred.
user_iterator user_begin()
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
void initializeScalarEvolutionPass(PassRegistry &)
unsigned getSCEVType() const
const SCEV * getUMinExpr(const SCEV *LHS, const SCEV *RHS)
A raw_ostream that writes to an std::string.
~MarkPendingLoopPredicate()
void print(raw_ostream &OS) const
print - Print out the internal representation of this scalar to the specified stream.
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
const SCEV * getSCEV(Value *V)
getSCEV - Return a SCEV expression for the full generality of the specified expression.
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE, const Loop *L)
unsigned getArgNo() const
Return the index of this formal argument in its containing function.
APInt shl(const APInt &LHS, unsigned shiftAmt)
Left-shift function.
ConstantRange zextOrTrunc(uint32_t BitWidth) const
Make this range have the bit width given by BitWidth.
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
static bool containsConstantSomewhere(const SCEV *StartExpr)
Determine if any of the operands in this SCEV are a constant or if any of the add or multiply express...
This class implements an extremely fast bulk output stream that can only output to a stream...
const SCEV * getUDivExpr(const SCEV *LHS, const SCEV *RHS)
getUDivExpr - Get a canonical unsigned division expression, or something simpler if possible...
unsigned countLeadingZeros() const
The APInt version of the countLeadingZeros functions in MathExtras.h.
const SCEV * getUnknown(Value *V)
FoldingSetNodeIDRef Intern(BumpPtrAllocator &Allocator) const
Intern - Copy this node's data to a memory region allocated from the given allocator and return a Fol...
The SCEV varies predictably with the loop.
The legacy pass manager's analysis pass to compute loop information.
static bool findArrayDimensionsRec(ScalarEvolution &SE, SmallVectorImpl< const SCEV * > &Terms, SmallVectorImpl< const SCEV * > &Sizes)
bool dominates(const SCEV *S, const BasicBlock *BB)
dominates - Return true if elements that makes up the given SCEV dominate the specified basic block...
op_iterator op_end() const
Value handle with callbacks on RAUW and destruction.
static int sizeOfSCEV(const SCEV *S)
StringRef - Represent a constant reference to a string, i.e.
inst_iterator inst_end(Function *F)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT zext(unsigned width) const
Zero extend to a new width.
Value * SimplifyInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
SimplifyInstruction - See if we can compute a simplified version of this instruction.
Legacy analysis pass which computes a DominatorTree.
BlockDisposition getBlockDisposition(const SCEV *S, const BasicBlock *BB)
getLoopDisposition - Return the "disposition" of the given SCEV with respect to the given block...
const SCEV * getTruncateOrZeroExtend(const SCEV *V, Type *Ty)
getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the input value to the speci...
bool replacementPreservesLCSSAForm(Instruction *From, Value *To)
replacementPreservesLCSSAForm - Returns true if replacing From with To everywhere is guaranteed to pr...
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
static bool IsMaxConsistingOf(const SCEV *MaybeMaxExpr, const SCEV *Candidate)
Is MaybeMaxExpr an SMax or UMax of Candidate and some other values?
std::vector< LoopT * >::const_iterator iterator
const SCEV * getNegativeSCEV(const SCEV *V)
getNegativeSCEV - Return the SCEV object corresponding to -V.
const SCEV * getElementSize(Instruction *Inst)
Return the size of an element read or written by Inst.
static bool CanConstantFold(const Instruction *I)
CanConstantFold - Return true if we can constant fold an instruction of the specified type...
Constant * ConstantFoldLoadThroughGEPIndices(Constant *C, ArrayRef< Constant * > Indices)
ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr indices (with an implied zero ...
const SCEV * getZeroExtendExpr(const SCEV *Op, Type *Ty)
static bool containsUndefs(const SCEV *S)
bool hasComputableLoopEvolution(const SCEV *S, const Loop *L)
hasComputableLoopEvolution - Return true if the given SCEV changes value in a known way in the specif...
SCEVNAryExpr - This node is a base class providing common functionality for n'ary operators...
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
const SCEV * getNoopOrAnyExtend(const SCEV *V, Type *Ty)
getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of the input value to the specified ...
NoWrapFlags
NoWrapFlags are bitfield indices into SubclassData.
friend class SCEVCallbackVH
const SCEV * getOperand() const
unsigned getLoopDepth() const
getLoopDepth - Return the nesting level of this loop.
const SCEV * getNotSCEV(const SCEV *V)
getNotSCEV - Return the SCEV object corresponding to ~V.
const SCEV * getMulExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
getMulExpr - Get a canonical multiply expression, or something simpler if possible.
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
bool hasLoopInvariantBackedgeTakenCount(const Loop *L)
hasLoopInvariantBackedgeTakenCount - Return true if the specified loop has an analyzable loop-invaria...
const BasicBlock * getParent() const
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
bool isQuadratic() const
isQuadratic - Return true if this represents an expression A + B*x + C*x^2 where A, B and C are loop invariant values.
SCEVConstant - This class represents a constant integer value.
const SCEV * getAnyExtendExpr(const SCEV *Op, Type *Ty)
getAnyExtendExpr - Return a SCEV for the given operand extended with unspecified bits out to the give...
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
APInt getUnsignedMin() const
Return the smallest unsigned value contained in the ConstantRange.
static bool IsKnownPredicateViaMinOrMax(ScalarEvolution &SE, ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS)
Is LHS Pred RHS true on the virtue of LHS or RHS being a Min or Max expression?