Lines Matching defs:ConstantRange
1 //===- ConstantRange.cpp - ConstantRange implementation -------------------===//
23 #include "llvm/IR/ConstantRange.h"
45 ConstantRange::ConstantRange(uint32_t BitWidth, bool Full)
49 ConstantRange::ConstantRange(APInt V)
52 ConstantRange::ConstantRange(APInt L, APInt U)
55 "ConstantRange with unequal bit widths");
60 ConstantRange ConstantRange::fromKnownBits(const KnownBits &Known,
70 return ConstantRange(Known.getMinValue(), Known.getMaxValue() + 1);
77 return ConstantRange(Lower, Upper + 1);
80 KnownBits ConstantRange::toKnownBits() const {
98 ConstantRange ConstantRange::makeAllowedICmpRegion(CmpInst::Predicate Pred,
99 const ConstantRange &CR) {
111 return ConstantRange(CR.getUpper(), CR.getLower());
117 return ConstantRange(APInt::getMinValue(W), std::move(UMax));
123 return ConstantRange(APInt::getSignedMinValue(W), std::move(SMax));
133 return ConstantRange(std::move(UMin) + 1, APInt::getZero(W));
139 return ConstantRange(std::move(SMin) + 1, APInt::getSignedMinValue(W));
148 ConstantRange ConstantRange::makeSatisfyingICmpRegion(CmpInst::Predicate Pred,
149 const ConstantRange &CR) {
158 ConstantRange ConstantRange::makeExactICmpRegion(CmpInst::Predicate Pred,
170 bool ConstantRange::areInsensitiveToSignednessOfICmpPredicate(
171 const ConstantRange &CR1, const ConstantRange &CR2) {
179 bool ConstantRange::areInsensitiveToSignednessOfInvertedICmpPredicate(
180 const ConstantRange &CR1, const ConstantRange &CR2) {
188 CmpInst::Predicate ConstantRange::getEquivalentPredWithFlippedSignedness(
189 CmpInst::Predicate Pred, const ConstantRange &CR1,
190 const ConstantRange &CR2) {
206 void ConstantRange::getEquivalentICmp(CmpInst::Predicate &Pred,
232 assert(ConstantRange::makeExactICmpRegion(Pred, RHS) == add(Offset) &&
236 bool ConstantRange::getEquivalentICmp(CmpInst::Predicate &Pred,
243 bool ConstantRange::icmp(CmpInst::Predicate Pred,
244 const ConstantRange &Other) const {
278 static ConstantRange makeExactMulNUWRegion(const APInt &V) {
281 return ConstantRange::getFull(V.getBitWidth());
283 return ConstantRange::getNonEmpty(
291 static ConstantRange makeExactMulNSWRegion(const APInt &V) {
295 return ConstantRange::getFull(BitWidth);
301 return ConstantRange(-MaxValue, MinValue);
311 return ConstantRange::getNonEmpty(Lower, Upper + 1);
314 ConstantRange
315 ConstantRange::makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp,
316 const ConstantRange &Other,
369 ConstantRange ShAmt = Other.intersectWith(
370 ConstantRange(APInt(BitWidth, 0), APInt(BitWidth, (BitWidth - 1) + 1)));
389 ConstantRange ConstantRange::makeExactNoWrapRegion(Instruction::BinaryOps BinOp,
394 return makeGuaranteedNoWrapRegion(BinOp, ConstantRange(Other), NoWrapKind);
397 ConstantRange ConstantRange::makeMaskNotEqualRange(const APInt &Mask,
410 return ConstantRange::getNonEmpty(
414 bool ConstantRange::isFullSet() const {
418 bool ConstantRange::isEmptySet() const {
422 bool ConstantRange::isWrappedSet() const {
426 bool ConstantRange::isUpperWrapped() const {
430 bool ConstantRange::isSignWrappedSet() const {
434 bool ConstantRange::isUpperSignWrapped() const {
439 ConstantRange::isSizeStrictlySmallerThan(const ConstantRange &Other) const {
449 ConstantRange::isSizeLargerThan(uint64_t MaxSize) const {
458 bool ConstantRange::isAllNegative() const {
468 bool ConstantRange::isAllNonNegative() const {
473 bool ConstantRange::isAllPositive() const {
483 APInt ConstantRange::getUnsignedMax() const {
489 APInt ConstantRange::getUnsignedMin() const {
495 APInt ConstantRange::getSignedMax() const {
501 APInt ConstantRange::getSignedMin() const {
507 bool ConstantRange::contains(const APInt &V) const {
516 bool ConstantRange::contains(const ConstantRange &Other) const {
534 unsigned ConstantRange::getActiveBits() const {
541 unsigned ConstantRange::getMinSignedBits() const {
549 ConstantRange ConstantRange::subtract(const APInt &Val) const {
554 return ConstantRange(Lower - Val, Upper - Val);
557 ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
561 static ConstantRange getPreferredRange(
562 const ConstantRange &CR1, const ConstantRange &CR2,
563 ConstantRange::PreferredRangeType Type) {
564 if (Type == ConstantRange::Unsigned) {
569 } else if (Type == ConstantRange::Signed) {
581 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR,
584 "ConstantRange types don't agree!");
603 return ConstantRange(CR.Lower, Upper);
617 return ConstantRange(Lower, CR.Upper);
634 return ConstantRange(CR.Lower, Upper);
648 return ConstantRange(Lower, CR.Upper);
665 return ConstantRange(Lower, CR.Upper);
679 return ConstantRange(CR.Lower, Upper);
687 ConstantRange ConstantRange::unionWith(const ConstantRange &CR,
690 "ConstantRange types don't agree!");
706 ConstantRange(Lower, CR.Upper), ConstantRange(CR.Lower, Upper), Type);
714 return ConstantRange(std::move(L), std::move(U));
735 ConstantRange(Lower, CR.Upper), ConstantRange(CR.Lower, Upper), Type);
740 return ConstantRange(CR.Lower, Upper);
745 "ConstantRange::unionWith missed a case with one range wrapped");
746 return ConstantRange(Lower, CR.Upper);
757 return ConstantRange(std::move(L), std::move(U));
760 std::optional<ConstantRange>
761 ConstantRange::exactIntersectWith(const ConstantRange &CR) const {
763 ConstantRange Result = intersectWith(CR);
769 std::optional<ConstantRange>
770 ConstantRange::exactUnionWith(const ConstantRange &CR) const {
772 ConstantRange Result = unionWith(CR);
778 ConstantRange ConstantRange::castOp(Instruction::CastOps CastOp,
829 ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
839 return ConstantRange(std::move(LowerExt),
843 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
846 ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
854 return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize));
857 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
861 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
864 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
872 ConstantRange Union(DstTySize, /*isFullSet=*/false);
883 Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
902 return ConstantRange(LowerDiv.trunc(DstTySize),
910 return ConstantRange(LowerDiv.trunc(DstTySize),
917 ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
926 ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
935 ConstantRange ConstantRange::binaryOp(Instruction::BinaryOps BinOp,
936 const ConstantRange &Other) const {
980 ConstantRange ConstantRange::overflowingBinaryOp(Instruction::BinaryOps BinOp,
981 const ConstantRange &Other,
1001 bool ConstantRange::isIntrinsicSupported(Intrinsic::ID IntrinsicID) {
1021 ConstantRange ConstantRange::intrinsic(Intrinsic::ID IntrinsicID,
1022 ArrayRef<ConstantRange> Ops) {
1066 ConstantRange
1067 ConstantRange::add(const ConstantRange &Other) const {
1078 ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper));
1086 ConstantRange ConstantRange::addWithNoWrap(const ConstantRange &Other,
1097 ConstantRange Result = add(Other);
1113 ConstantRange
1114 ConstantRange::sub(const ConstantRange &Other) const {
1125 ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper));
1133 ConstantRange ConstantRange::subWithNoWrap(const ConstantRange &Other,
1144 ConstantRange Result = sub(Other);
1163 ConstantRange
1164 ConstantRange::multiply(const ConstantRange &Other) const {
1177 return ConstantRange(APInt::getZero(getBitWidth())).sub(Other);
1184 return ConstantRange(APInt::getZero(getBitWidth())).sub(*this);
1199 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
1201 ConstantRange UR = Result_zext.truncate(getBitWidth());
1225 ConstantRange Result_sext(std::min(L, Compare), std::max(L, Compare) + 1);
1226 ConstantRange SR = Result_sext.truncate(getBitWidth());
1231 ConstantRange
1232 ConstantRange::multiplyWithNoWrap(const ConstantRange &Other,
1240 ConstantRange Result = multiply(Other);
1262 ConstantRange ConstantRange::smul_fast(const ConstantRange &Other) const {
1281 ConstantRange
1282 ConstantRange::smax(const ConstantRange &Other) const {
1289 ConstantRange Res = getNonEmpty(std::move(NewL), std::move(NewU));
1295 ConstantRange
1296 ConstantRange::umax(const ConstantRange &Other) const {
1303 ConstantRange Res = getNonEmpty(std::move(NewL), std::move(NewU));
1309 ConstantRange
1310 ConstantRange::smin(const ConstantRange &Other) const {
1317 ConstantRange Res = getNonEmpty(std::move(NewL), std::move(NewU));
1323 ConstantRange
1324 ConstantRange::umin(const ConstantRange &Other) const {
1331 ConstantRange Res = getNonEmpty(std::move(NewL), std::move(NewU));
1337 ConstantRange
1338 ConstantRange::udiv(const ConstantRange &RHS) const {
1358 ConstantRange ConstantRange::sdiv(const ConstantRange &RHS) const {
1365 ConstantRange PosFilter =
1367 : ConstantRange(APInt(getBitWidth(), 1), SignedMin);
1368 ConstantRange NegFilter(SignedMin, Zero);
1369 ConstantRange PosL = intersectWith(PosFilter);
1370 ConstantRange NegL = intersectWith(NegFilter);
1371 ConstantRange PosR = RHS.intersectWith(PosFilter);
1372 ConstantRange NegR = RHS.intersectWith(NegFilter);
1374 ConstantRange PosRes = getEmpty();
1377 PosRes = ConstantRange(PosL.Lower.sdiv(PosR.Upper - 1),
1401 ConstantRange(Lo, NegL.Lower.sdiv(AdjNegRUpper - 1) + 1));
1416 ConstantRange(std::move(Lo),
1421 ConstantRange(std::move(Lo), NegL.Lower.sdiv(NegR.Upper - 1) + 1));
1425 ConstantRange NegRes = getEmpty();
1428 NegRes = ConstantRange((PosL.Upper - 1).sdiv(NegR.Upper - 1),
1434 ConstantRange(NegL.Lower.sdiv(PosR.Lower),
1438 ConstantRange Res = NegRes.unionWith(PosRes, PreferredRangeType::Signed);
1442 Res = Res.unionWith(ConstantRange(Zero));
1446 ConstantRange ConstantRange::urem(const ConstantRange &RHS) const {
1468 ConstantRange ConstantRange::srem(const ConstantRange &RHS) const {
1481 ConstantRange AbsRHS = RHS.abs();
1501 return ConstantRange(APInt::getZero(getBitWidth()), std::move(Upper));
1510 return ConstantRange(std::move(Lower), APInt(getBitWidth(), 1));
1516 return ConstantRange(std::move(Lower), std::move(Upper));
1519 ConstantRange ConstantRange::binaryNot() const {
1520 return ConstantRange(APInt::getAllOnes(getBitWidth())).sub(*this);
1547 static APInt estimateBitMaskedAndLowerBound(const ConstantRange &LHS,
1548 const ConstantRange &RHS) {
1580 ConstantRange ConstantRange::binaryAnd(const ConstantRange &Other) const {
1584 ConstantRange KnownBitsRange =
1587 ConstantRange UMinUMaxRange = getNonEmpty(
1592 ConstantRange ConstantRange::binaryOr(const ConstantRange &Other) const {
1596 ConstantRange KnownBitsRange =
1607 ConstantRange UMaxUMinRange = getNonEmpty(
1612 ConstantRange ConstantRange::binaryXor(const ConstantRange &Other) const {
1629 ConstantRange CR = fromKnownBits(Known, /*IsSigned*/ false);
1644 ConstantRange
1645 ConstantRange::shl(const ConstantRange &Other) const {
1670 return ConstantRange::getNonEmpty(std::move(Min), std::move(Max) + 1);
1682 return ConstantRange::getNonEmpty(std::move(Min), std::move(Max) + 1);
1685 static ConstantRange computeShlNUW(const ConstantRange &LHS,
1686 const ConstantRange &RHS) {
1693 return ConstantRange::getEmpty(BitWidth);
1705 return ConstantRange::getNonEmpty(MinShl, MaxShl + 1);
1708 static ConstantRange computeShlNSWWithNNegLHS(const APInt &LHSMin,
1716 return ConstantRange::getEmpty(BitWidth);
1726 return ConstantRange::getNonEmpty(MinShl, MaxShl + 1);
1729 static ConstantRange computeShlNSWWithNegLHS(const APInt &LHSMin,
1736 return ConstantRange::getEmpty(BitWidth);
1745 return ConstantRange::getNonEmpty(MinShl, MaxShl + 1);
1748 static ConstantRange computeShlNSW(const ConstantRange &LHS,
1749 const ConstantRange &RHS) {
1763 ConstantRange::Signed);
1766 ConstantRange ConstantRange::shlWithNoWrap(const ConstantRange &Other,
1788 ConstantRange
1789 ConstantRange::lshr(const ConstantRange &Other) const {
1798 ConstantRange
1799 ConstantRange::ashr(const ConstantRange &Other) const {
1849 ConstantRange ConstantRange::uadd_sat(const ConstantRange &Other) const {
1858 ConstantRange ConstantRange::sadd_sat(const ConstantRange &Other) const {
1867 ConstantRange ConstantRange::usub_sat(const ConstantRange &Other) const {
1876 ConstantRange ConstantRange::ssub_sat(const ConstantRange &Other) const {
1885 ConstantRange ConstantRange::umul_sat(const ConstantRange &Other) const {
1894 ConstantRange ConstantRange::smul_sat(const ConstantRange &Other) const {
1915 ConstantRange ConstantRange::ushl_sat(const ConstantRange &Other) const {
1924 ConstantRange ConstantRange::sshl_sat(const ConstantRange &Other) const {
1935 ConstantRange ConstantRange::inverse() const {
1940 return ConstantRange(Upper, Lower);
1943 ConstantRange ConstantRange::abs(bool IntMinIsPoison) const {
1957 return ConstantRange(Lo, APInt::getSignedMinValue(getBitWidth()));
1959 return ConstantRange(Lo, APInt::getSignedMinValue(getBitWidth()) + 1);
1974 return ConstantRange(SMin, SMax + 1);
1978 return ConstantRange(-SMax, -SMin + 1);
1981 return ConstantRange::getNonEmpty(APInt::getZero(getBitWidth()),
1985 ConstantRange ConstantRange::ctlz(bool ZeroIsPoison) const {
2005 return ConstantRange(
2010 return ConstantRange(Zero,
2013 return ConstantRange(Zero, APInt(getBitWidth(), getBitWidth()));
2023 static ConstantRange getUnsignedCountTrailingZerosRange(const APInt &Lower,
2025 assert(!ConstantRange(Lower, Upper).isWrappedSet() &&
2030 return ConstantRange(APInt(BitWidth, Lower.countr_zero()));
2032 return ConstantRange(APInt::getZero(BitWidth),
2039 return ConstantRange(
2045 ConstantRange ConstantRange::cttz(bool ZeroIsPoison) const {
2071 ConstantRange CR1 = getUnsignedCountTrailingZerosRange(Lower, Zero);
2072 ConstantRange CR2 =
2085 ConstantRange CR1 = getUnsignedCountTrailingZerosRange(Lower, Zero);
2087 ConstantRange CR2 = getUnsignedCountTrailingZerosRange(Zero, Upper);
2091 static ConstantRange getUnsignedPopCountRange(const APInt &Lower,
2093 assert(!ConstantRange(Lower, Upper).isWrappedSet() &&
2098 return ConstantRange(APInt(BitWidth, Lower.popcount()));
2114 return ConstantRange(APInt(BitWidth, MinBits), APInt(BitWidth, MaxBits + 1));
2117 ConstantRange ConstantRange::ctpop() const {
2130 ConstantRange CR1 = ConstantRange(APInt(BitWidth, Lower.countl_one()),
2133 ConstantRange CR2 = getUnsignedPopCountRange(Zero, Upper);
2137 ConstantRange::OverflowResult ConstantRange::unsignedAddMayOverflow(
2138 const ConstantRange &Other) const {
2153 ConstantRange::OverflowResult ConstantRange::signedAddMayOverflow(
2154 const ConstantRange &Other) const {
2183 ConstantRange::OverflowResult ConstantRange::unsignedSubMayOverflow(
2184 const ConstantRange &Other) const {
2199 ConstantRange::OverflowResult ConstantRange::signedSubMayOverflow(
2200 const ConstantRange &Other) const {
2229 ConstantRange::OverflowResult ConstantRange::unsignedMulMayOverflow(
2230 const ConstantRange &Other) const {
2249 void ConstantRange::print(raw_ostream &OS) const {
2259 LLVM_DUMP_METHOD void ConstantRange::dump() const {
2264 ConstantRange llvm::getConstantRangeFromMetadata(const MDNode &Ranges) {
2272 ConstantRange CR(FirstLow->getValue(), FirstHigh->getValue());
2280 CR = CR.unionWith(ConstantRange(Low->getValue(), High->getValue()));