#include "llvm/ADT/APInt.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Operator.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/KnownBits.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
using namespace llvm;
ConstantRange::ConstantRange(uint32_t BitWidth, bool Full)
: Lower(Full ? APInt::getMaxValue(BitWidth) : APInt::getMinValue(BitWidth)),
Upper(Lower) {}
ConstantRange::ConstantRange(APInt V)
: Lower(std::move(V)), Upper(Lower + 1) {}
ConstantRange::ConstantRange(APInt L, APInt U)
: Lower(std::move(L)), Upper(std::move(U)) {
assert(Lower.getBitWidth() == Upper.getBitWidth() &&
"ConstantRange with unequal bit widths");
assert((Lower != Upper || (Lower.isMaxValue() || Lower.isMinValue())) &&
"Lower == Upper, but they aren't min or max value!");
}
ConstantRange ConstantRange::fromKnownBits(const KnownBits &Known,
bool IsSigned) {
assert(!Known.hasConflict() && "Expected valid KnownBits");
if (Known.isUnknown())
return getFull(Known.getBitWidth());
if (!IsSigned || Known.isNegative() || Known.isNonNegative())
return ConstantRange(Known.getMinValue(), Known.getMaxValue() + 1);
APInt Lower = Known.getMinValue(), Upper = Known.getMaxValue();
Lower.setSignBit();
Upper.clearSignBit();
return ConstantRange(Lower, Upper + 1);
}
KnownBits ConstantRange::toKnownBits() const {
if (isEmptySet())
return KnownBits(getBitWidth());
APInt Min = getUnsignedMin();
APInt Max = getUnsignedMax();
KnownBits Known = KnownBits::makeConstant(Min);
if (Optional<unsigned> DifferentBit =
APIntOps::GetMostSignificantDifferentBit(Min, Max)) {
Known.Zero.clearLowBits(*DifferentBit + 1);
Known.One.clearLowBits(*DifferentBit + 1);
}
return Known;
}
ConstantRange ConstantRange::makeAllowedICmpRegion(CmpInst::Predicate Pred,
const ConstantRange &CR) {
if (CR.isEmptySet())
return CR;
uint32_t W = CR.getBitWidth();
switch (Pred) {
default:
llvm_unreachable("Invalid ICmp predicate to makeAllowedICmpRegion()");
case CmpInst::ICMP_EQ:
return CR;
case CmpInst::ICMP_NE:
if (CR.isSingleElement())
return ConstantRange(CR.getUpper(), CR.getLower());
return getFull(W);
case CmpInst::ICMP_ULT: {
APInt UMax(CR.getUnsignedMax());
if (UMax.isMinValue())
return getEmpty(W);
return ConstantRange(APInt::getMinValue(W), std::move(UMax));
}
case CmpInst::ICMP_SLT: {
APInt SMax(CR.getSignedMax());
if (SMax.isMinSignedValue())
return getEmpty(W);
return ConstantRange(APInt::getSignedMinValue(W), std::move(SMax));
}
case CmpInst::ICMP_ULE:
return getNonEmpty(APInt::getMinValue(W), CR.getUnsignedMax() + 1);
case CmpInst::ICMP_SLE:
return getNonEmpty(APInt::getSignedMinValue(W), CR.getSignedMax() + 1);
case CmpInst::ICMP_UGT: {
APInt UMin(CR.getUnsignedMin());
if (UMin.isMaxValue())
return getEmpty(W);
return ConstantRange(std::move(UMin) + 1, APInt::getZero(W));
}
case CmpInst::ICMP_SGT: {
APInt SMin(CR.getSignedMin());
if (SMin.isMaxSignedValue())
return getEmpty(W);
return ConstantRange(std::move(SMin) + 1, APInt::getSignedMinValue(W));
}
case CmpInst::ICMP_UGE:
return getNonEmpty(CR.getUnsignedMin(), APInt::getZero(W));
case CmpInst::ICMP_SGE:
return getNonEmpty(CR.getSignedMin(), APInt::getSignedMinValue(W));
}
}
ConstantRange ConstantRange::makeSatisfyingICmpRegion(CmpInst::Predicate Pred,
const ConstantRange &CR) {
return makeAllowedICmpRegion(CmpInst::getInversePredicate(Pred), CR)
.inverse();
}
ConstantRange ConstantRange::makeExactICmpRegion(CmpInst::Predicate Pred,
const APInt &C) {
assert(makeAllowedICmpRegion(Pred, C) == makeSatisfyingICmpRegion(Pred, C));
return makeAllowedICmpRegion(Pred, C);
}
bool ConstantRange::areInsensitiveToSignednessOfICmpPredicate(
const ConstantRange &CR1, const ConstantRange &CR2) {
if (CR1.isEmptySet() || CR2.isEmptySet())
return true;
return (CR1.isAllNonNegative() && CR2.isAllNonNegative()) ||
(CR1.isAllNegative() && CR2.isAllNegative());
}
bool ConstantRange::areInsensitiveToSignednessOfInvertedICmpPredicate(
const ConstantRange &CR1, const ConstantRange &CR2) {
if (CR1.isEmptySet() || CR2.isEmptySet())
return true;
return (CR1.isAllNonNegative() && CR2.isAllNegative()) ||
(CR1.isAllNegative() && CR2.isAllNonNegative());
}
CmpInst::Predicate ConstantRange::getEquivalentPredWithFlippedSignedness(
CmpInst::Predicate Pred, const ConstantRange &CR1,
const ConstantRange &CR2) {
assert(CmpInst::isIntPredicate(Pred) && CmpInst::isRelational(Pred) &&
"Only for relational integer predicates!");
CmpInst::Predicate FlippedSignednessPred =
CmpInst::getFlippedSignednessPredicate(Pred);
if (areInsensitiveToSignednessOfICmpPredicate(CR1, CR2))
return FlippedSignednessPred;
if (areInsensitiveToSignednessOfInvertedICmpPredicate(CR1, CR2))
return CmpInst::getInversePredicate(FlippedSignednessPred);
return CmpInst::Predicate::BAD_ICMP_PREDICATE;
}
void ConstantRange::getEquivalentICmp(CmpInst::Predicate &Pred,
APInt &RHS, APInt &Offset) const {
Offset = APInt(getBitWidth(), 0);
if (isFullSet() || isEmptySet()) {
Pred = isEmptySet() ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE;
RHS = APInt(getBitWidth(), 0);
} else if (auto *OnlyElt = getSingleElement()) {
Pred = CmpInst::ICMP_EQ;
RHS = *OnlyElt;
} else if (auto *OnlyMissingElt = getSingleMissingElement()) {
Pred = CmpInst::ICMP_NE;
RHS = *OnlyMissingElt;
} else if (getLower().isMinSignedValue() || getLower().isMinValue()) {
Pred =
getLower().isMinSignedValue() ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT;
RHS = getUpper();
} else if (getUpper().isMinSignedValue() || getUpper().isMinValue()) {
Pred =
getUpper().isMinSignedValue() ? CmpInst::ICMP_SGE : CmpInst::ICMP_UGE;
RHS = getLower();
} else {
Pred = CmpInst::ICMP_ULT;
RHS = getUpper() - getLower();
Offset = -getLower();
}
assert(ConstantRange::makeExactICmpRegion(Pred, RHS) == add(Offset) &&
"Bad result!");
}
bool ConstantRange::getEquivalentICmp(CmpInst::Predicate &Pred,
APInt &RHS) const {
APInt Offset;
getEquivalentICmp(Pred, RHS, Offset);
return Offset.isZero();
}
bool ConstantRange::icmp(CmpInst::Predicate Pred,
const ConstantRange &Other) const {
return makeSatisfyingICmpRegion(Pred, Other).contains(*this);
}
static ConstantRange makeExactMulNUWRegion(const APInt &V) {
unsigned BitWidth = V.getBitWidth();
if (V == 0)
return ConstantRange::getFull(V.getBitWidth());
return ConstantRange::getNonEmpty(
APIntOps::RoundingUDiv(APInt::getMinValue(BitWidth), V,
APInt::Rounding::UP),
APIntOps::RoundingUDiv(APInt::getMaxValue(BitWidth), V,
APInt::Rounding::DOWN) + 1);
}
static ConstantRange makeExactMulNSWRegion(const APInt &V) {
unsigned BitWidth = V.getBitWidth();
if (V == 0 || V.isOne())
return ConstantRange::getFull(BitWidth);
APInt MinValue = APInt::getSignedMinValue(BitWidth);
APInt MaxValue = APInt::getSignedMaxValue(BitWidth);
if (V.isAllOnes())
return ConstantRange(-MaxValue, MinValue);
APInt Lower, Upper;
if (V.isNegative()) {
Lower = APIntOps::RoundingSDiv(MaxValue, V, APInt::Rounding::UP);
Upper = APIntOps::RoundingSDiv(MinValue, V, APInt::Rounding::DOWN);
} else {
Lower = APIntOps::RoundingSDiv(MinValue, V, APInt::Rounding::UP);
Upper = APIntOps::RoundingSDiv(MaxValue, V, APInt::Rounding::DOWN);
}
return ConstantRange(Lower, Upper + 1);
}
ConstantRange
ConstantRange::makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp,
const ConstantRange &Other,
unsigned NoWrapKind) {
using OBO = OverflowingBinaryOperator;
assert(Instruction::isBinaryOp(BinOp) && "Binary operators only!");
assert((NoWrapKind == OBO::NoSignedWrap ||
NoWrapKind == OBO::NoUnsignedWrap) &&
"NoWrapKind invalid!");
bool Unsigned = NoWrapKind == OBO::NoUnsignedWrap;
unsigned BitWidth = Other.getBitWidth();
switch (BinOp) {
default:
llvm_unreachable("Unsupported binary op");
case Instruction::Add: {
if (Unsigned)
return getNonEmpty(APInt::getZero(BitWidth), -Other.getUnsignedMax());
APInt SignedMinVal = APInt::getSignedMinValue(BitWidth);
APInt SMin = Other.getSignedMin(), SMax = Other.getSignedMax();
return getNonEmpty(
SMin.isNegative() ? SignedMinVal - SMin : SignedMinVal,
SMax.isStrictlyPositive() ? SignedMinVal - SMax : SignedMinVal);
}
case Instruction::Sub: {
if (Unsigned)
return getNonEmpty(Other.getUnsignedMax(), APInt::getMinValue(BitWidth));
APInt SignedMinVal = APInt::getSignedMinValue(BitWidth);
APInt SMin = Other.getSignedMin(), SMax = Other.getSignedMax();
return getNonEmpty(
SMax.isStrictlyPositive() ? SignedMinVal + SMax : SignedMinVal,
SMin.isNegative() ? SignedMinVal + SMin : SignedMinVal);
}
case Instruction::Mul:
if (Unsigned)
return makeExactMulNUWRegion(Other.getUnsignedMax());
return makeExactMulNSWRegion(Other.getSignedMin())
.intersectWith(makeExactMulNSWRegion(Other.getSignedMax()));
case Instruction::Shl: {
ConstantRange ShAmt = Other.intersectWith(
ConstantRange(APInt(BitWidth, 0), APInt(BitWidth, (BitWidth - 1) + 1)));
if (ShAmt.isEmptySet()) {
return getFull(BitWidth);
}
APInt ShAmtUMax = ShAmt.getUnsignedMax();
if (Unsigned)
return getNonEmpty(APInt::getZero(BitWidth),
APInt::getMaxValue(BitWidth).lshr(ShAmtUMax) + 1);
return getNonEmpty(APInt::getSignedMinValue(BitWidth).ashr(ShAmtUMax),
APInt::getSignedMaxValue(BitWidth).ashr(ShAmtUMax) + 1);
}
}
}
ConstantRange ConstantRange::makeExactNoWrapRegion(Instruction::BinaryOps BinOp,
const APInt &Other,
unsigned NoWrapKind) {
return makeGuaranteedNoWrapRegion(BinOp, ConstantRange(Other), NoWrapKind);
}
bool ConstantRange::isFullSet() const {
return Lower == Upper && Lower.isMaxValue();
}
bool ConstantRange::isEmptySet() const {
return Lower == Upper && Lower.isMinValue();
}
bool ConstantRange::isWrappedSet() const {
return Lower.ugt(Upper) && !Upper.isZero();
}
bool ConstantRange::isUpperWrapped() const {
return Lower.ugt(Upper);
}
bool ConstantRange::isSignWrappedSet() const {
return Lower.sgt(Upper) && !Upper.isMinSignedValue();
}
bool ConstantRange::isUpperSignWrapped() const {
return Lower.sgt(Upper);
}
bool
ConstantRange::isSizeStrictlySmallerThan(const ConstantRange &Other) const {
assert(getBitWidth() == Other.getBitWidth());
if (isFullSet())
return false;
if (Other.isFullSet())
return true;
return (Upper - Lower).ult(Other.Upper - Other.Lower);
}
bool
ConstantRange::isSizeLargerThan(uint64_t MaxSize) const {
if (isFullSet())
return MaxSize == 0 || APInt::getMaxValue(getBitWidth()).ugt(MaxSize - 1);
return (Upper - Lower).ugt(MaxSize);
}
bool ConstantRange::isAllNegative() const {
if (isEmptySet())
return true;
if (isFullSet())
return false;
return !isUpperSignWrapped() && !Upper.isStrictlyPositive();
}
bool ConstantRange::isAllNonNegative() const {
return !isSignWrappedSet() && Lower.isNonNegative();
}
APInt ConstantRange::getUnsignedMax() const {
if (isFullSet() || isUpperWrapped())
return APInt::getMaxValue(getBitWidth());
return getUpper() - 1;
}
APInt ConstantRange::getUnsignedMin() const {
if (isFullSet() || isWrappedSet())
return APInt::getMinValue(getBitWidth());
return getLower();
}
APInt ConstantRange::getSignedMax() const {
if (isFullSet() || isUpperSignWrapped())
return APInt::getSignedMaxValue(getBitWidth());
return getUpper() - 1;
}
APInt ConstantRange::getSignedMin() const {
if (isFullSet() || isSignWrappedSet())
return APInt::getSignedMinValue(getBitWidth());
return getLower();
}
bool ConstantRange::contains(const APInt &V) const {
if (Lower == Upper)
return isFullSet();
if (!isUpperWrapped())
return Lower.ule(V) && V.ult(Upper);
return Lower.ule(V) || V.ult(Upper);
}
bool ConstantRange::contains(const ConstantRange &Other) const {
if (isFullSet() || Other.isEmptySet()) return true;
if (isEmptySet() || Other.isFullSet()) return false;
if (!isUpperWrapped()) {
if (Other.isUpperWrapped())
return false;
return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
}
if (!Other.isUpperWrapped())
return Other.getUpper().ule(Upper) ||
Lower.ule(Other.getLower());
return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
}
unsigned ConstantRange::getActiveBits() const {
if (isEmptySet())
return 0;
return getUnsignedMax().getActiveBits();
}
unsigned ConstantRange::getMinSignedBits() const {
if (isEmptySet())
return 0;
return std::max(getSignedMin().getMinSignedBits(),
getSignedMax().getMinSignedBits());
}
ConstantRange ConstantRange::subtract(const APInt &Val) const {
assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
if (Lower == Upper)
return *this;
return ConstantRange(Lower - Val, Upper - Val);
}
ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
return intersectWith(CR.inverse());
}
static ConstantRange getPreferredRange(
const ConstantRange &CR1, const ConstantRange &CR2,
ConstantRange::PreferredRangeType Type) {
if (Type == ConstantRange::Unsigned) {
if (!CR1.isWrappedSet() && CR2.isWrappedSet())
return CR1;
if (CR1.isWrappedSet() && !CR2.isWrappedSet())
return CR2;
} else if (Type == ConstantRange::Signed) {
if (!CR1.isSignWrappedSet() && CR2.isSignWrappedSet())
return CR1;
if (CR1.isSignWrappedSet() && !CR2.isSignWrappedSet())
return CR2;
}
if (CR1.isSizeStrictlySmallerThan(CR2))
return CR1;
return CR2;
}
ConstantRange ConstantRange::intersectWith(const ConstantRange &CR,
PreferredRangeType Type) const {
assert(getBitWidth() == CR.getBitWidth() &&
"ConstantRange types don't agree!");
if ( isEmptySet() || CR.isFullSet()) return *this;
if (CR.isEmptySet() || isFullSet()) return CR;
if (!isUpperWrapped() && CR.isUpperWrapped())
return CR.intersectWith(*this, Type);
if (!isUpperWrapped() && !CR.isUpperWrapped()) {
if (Lower.ult(CR.Lower)) {
if (Upper.ule(CR.Lower))
return getEmpty();
if (Upper.ult(CR.Upper))
return ConstantRange(CR.Lower, Upper);
return CR;
}
if (Upper.ult(CR.Upper))
return *this;
if (Lower.ult(CR.Upper))
return ConstantRange(Lower, CR.Upper);
return getEmpty();
}
if (isUpperWrapped() && !CR.isUpperWrapped()) {
if (CR.Lower.ult(Upper)) {
if (CR.Upper.ult(Upper))
return CR;
if (CR.Upper.ule(Lower))
return ConstantRange(CR.Lower, Upper);
return getPreferredRange(*this, CR, Type);
}
if (CR.Lower.ult(Lower)) {
if (CR.Upper.ule(Lower))
return getEmpty();
return ConstantRange(Lower, CR.Upper);
}
return CR;
}
if (CR.Upper.ult(Upper)) {
if (CR.Lower.ult(Upper))
return getPreferredRange(*this, CR, Type);
if (CR.Lower.ult(Lower))
return ConstantRange(Lower, CR.Upper);
return CR;
}
if (CR.Upper.ule(Lower)) {
if (CR.Lower.ult(Lower))
return *this;
return ConstantRange(CR.Lower, Upper);
}
return getPreferredRange(*this, CR, Type);
}
ConstantRange ConstantRange::unionWith(const ConstantRange &CR,
PreferredRangeType Type) const {
assert(getBitWidth() == CR.getBitWidth() &&
"ConstantRange types don't agree!");
if ( isFullSet() || CR.isEmptySet()) return *this;
if (CR.isFullSet() || isEmptySet()) return CR;
if (!isUpperWrapped() && CR.isUpperWrapped())
return CR.unionWith(*this, Type);
if (!isUpperWrapped() && !CR.isUpperWrapped()) {
if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower))
return getPreferredRange(
ConstantRange(Lower, CR.Upper), ConstantRange(CR.Lower, Upper), Type);
APInt L = CR.Lower.ult(Lower) ? CR.Lower : Lower;
APInt U = (CR.Upper - 1).ugt(Upper - 1) ? CR.Upper : Upper;
if (L.isZero() && U.isZero())
return getFull();
return ConstantRange(std::move(L), std::move(U));
}
if (!CR.isUpperWrapped()) {
if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
return *this;
if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
return getFull();
if (Upper.ult(CR.Lower) && CR.Upper.ult(Lower))
return getPreferredRange(
ConstantRange(Lower, CR.Upper), ConstantRange(CR.Lower, Upper), Type);
if (Upper.ult(CR.Lower) && Lower.ule(CR.Upper))
return ConstantRange(CR.Lower, Upper);
assert(CR.Lower.ule(Upper) && CR.Upper.ult(Lower) &&
"ConstantRange::unionWith missed a case with one range wrapped");
return ConstantRange(Lower, CR.Upper);
}
if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
return getFull();
APInt L = CR.Lower.ult(Lower) ? CR.Lower : Lower;
APInt U = CR.Upper.ugt(Upper) ? CR.Upper : Upper;
return ConstantRange(std::move(L), std::move(U));
}
Optional<ConstantRange>
ConstantRange::exactIntersectWith(const ConstantRange &CR) const {
ConstantRange Result = intersectWith(CR);
if (Result == inverse().unionWith(CR.inverse()).inverse())
return Result;
return None;
}
Optional<ConstantRange>
ConstantRange::exactUnionWith(const ConstantRange &CR) const {
ConstantRange Result = unionWith(CR);
if (Result == inverse().intersectWith(CR.inverse()).inverse())
return Result;
return None;
}
ConstantRange ConstantRange::castOp(Instruction::CastOps CastOp,
uint32_t ResultBitWidth) const {
switch (CastOp) {
default:
llvm_unreachable("unsupported cast type");
case Instruction::Trunc:
return truncate(ResultBitWidth);
case Instruction::SExt:
return signExtend(ResultBitWidth);
case Instruction::ZExt:
return zeroExtend(ResultBitWidth);
case Instruction::BitCast:
return *this;
case Instruction::FPToUI:
case Instruction::FPToSI:
if (getBitWidth() == ResultBitWidth)
return *this;
else
return getFull(ResultBitWidth);
case Instruction::UIToFP: {
auto BW = getBitWidth();
APInt Min = APInt::getMinValue(BW);
APInt Max = APInt::getMaxValue(BW);
if (ResultBitWidth > BW) {
Min = Min.zext(ResultBitWidth);
Max = Max.zext(ResultBitWidth);
}
return ConstantRange(std::move(Min), std::move(Max));
}
case Instruction::SIToFP: {
auto BW = getBitWidth();
APInt SMin = APInt::getSignedMinValue(BW);
APInt SMax = APInt::getSignedMaxValue(BW);
if (ResultBitWidth > BW) {
SMin = SMin.sext(ResultBitWidth);
SMax = SMax.sext(ResultBitWidth);
}
return ConstantRange(std::move(SMin), std::move(SMax));
}
case Instruction::FPTrunc:
case Instruction::FPExt:
case Instruction::IntToPtr:
case Instruction::PtrToInt:
case Instruction::AddrSpaceCast:
return getFull(ResultBitWidth);
};
}
ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
if (isEmptySet()) return getEmpty(DstTySize);
unsigned SrcTySize = getBitWidth();
assert(SrcTySize < DstTySize && "Not a value extension");
if (isFullSet() || isUpperWrapped()) {
APInt LowerExt(DstTySize, 0);
if (!Upper) LowerExt = Lower.zext(DstTySize);
return ConstantRange(std::move(LowerExt),
APInt::getOneBitSet(DstTySize, SrcTySize));
}
return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
}
ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
if (isEmptySet()) return getEmpty(DstTySize);
unsigned SrcTySize = getBitWidth();
assert(SrcTySize < DstTySize && "Not a value extension");
if (Upper.isMinSignedValue())
return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize));
if (isFullSet() || isSignWrappedSet()) {
return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
}
return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
}
ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
assert(getBitWidth() > DstTySize && "Not a value truncation");
if (isEmptySet())
return getEmpty(DstTySize);
if (isFullSet())
return getFull(DstTySize);
APInt LowerDiv(Lower), UpperDiv(Upper);
ConstantRange Union(DstTySize, false);
if (isUpperWrapped()) {
if (Upper.getActiveBits() > DstTySize ||
Upper.countTrailingOnes() == DstTySize)
return getFull(DstTySize);
Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
UpperDiv.setAllBits();
if (LowerDiv == UpperDiv)
return Union;
}
if (LowerDiv.getActiveBits() > DstTySize) {
APInt Adjust = LowerDiv & APInt::getBitsSetFrom(getBitWidth(), DstTySize);
LowerDiv -= Adjust;
UpperDiv -= Adjust;
}
unsigned UpperDivWidth = UpperDiv.getActiveBits();
if (UpperDivWidth <= DstTySize)
return ConstantRange(LowerDiv.trunc(DstTySize),
UpperDiv.trunc(DstTySize)).unionWith(Union);
if (UpperDivWidth == DstTySize + 1) {
UpperDiv.clearBit(DstTySize);
if (UpperDiv.ult(LowerDiv))
return ConstantRange(LowerDiv.trunc(DstTySize),
UpperDiv.trunc(DstTySize)).unionWith(Union);
}
return getFull(DstTySize);
}
ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
unsigned SrcTySize = getBitWidth();
if (SrcTySize > DstTySize)
return truncate(DstTySize);
if (SrcTySize < DstTySize)
return zeroExtend(DstTySize);
return *this;
}
ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
unsigned SrcTySize = getBitWidth();
if (SrcTySize > DstTySize)
return truncate(DstTySize);
if (SrcTySize < DstTySize)
return signExtend(DstTySize);
return *this;
}
ConstantRange ConstantRange::binaryOp(Instruction::BinaryOps BinOp,
const ConstantRange &Other) const {
assert(Instruction::isBinaryOp(BinOp) && "Binary operators only!");
switch (BinOp) {
case Instruction::Add:
return add(Other);
case Instruction::Sub:
return sub(Other);
case Instruction::Mul:
return multiply(Other);
case Instruction::UDiv:
return udiv(Other);
case Instruction::SDiv:
return sdiv(Other);
case Instruction::URem:
return urem(Other);
case Instruction::SRem:
return srem(Other);
case Instruction::Shl:
return shl(Other);
case Instruction::LShr:
return lshr(Other);
case Instruction::AShr:
return ashr(Other);
case Instruction::And:
return binaryAnd(Other);
case Instruction::Or:
return binaryOr(Other);
case Instruction::Xor:
return binaryXor(Other);
case Instruction::FAdd:
return add(Other);
case Instruction::FSub:
return sub(Other);
case Instruction::FMul:
return multiply(Other);
default:
return getFull();
}
}
ConstantRange ConstantRange::overflowingBinaryOp(Instruction::BinaryOps BinOp,
const ConstantRange &Other,
unsigned NoWrapKind) const {
assert(Instruction::isBinaryOp(BinOp) && "Binary operators only!");
switch (BinOp) {
case Instruction::Add:
return addWithNoWrap(Other, NoWrapKind);
case Instruction::Sub:
return subWithNoWrap(Other, NoWrapKind);
default:
return binaryOp(BinOp, Other);
}
}
bool ConstantRange::isIntrinsicSupported(Intrinsic::ID IntrinsicID) {
switch (IntrinsicID) {
case Intrinsic::uadd_sat:
case Intrinsic::usub_sat:
case Intrinsic::sadd_sat:
case Intrinsic::ssub_sat:
case Intrinsic::umin:
case Intrinsic::umax:
case Intrinsic::smin:
case Intrinsic::smax:
case Intrinsic::abs:
return true;
default:
return false;
}
}
ConstantRange ConstantRange::intrinsic(Intrinsic::ID IntrinsicID,
ArrayRef<ConstantRange> Ops) {
switch (IntrinsicID) {
case Intrinsic::uadd_sat:
return Ops[0].uadd_sat(Ops[1]);
case Intrinsic::usub_sat:
return Ops[0].usub_sat(Ops[1]);
case Intrinsic::sadd_sat:
return Ops[0].sadd_sat(Ops[1]);
case Intrinsic::ssub_sat:
return Ops[0].ssub_sat(Ops[1]);
case Intrinsic::umin:
return Ops[0].umin(Ops[1]);
case Intrinsic::umax:
return Ops[0].umax(Ops[1]);
case Intrinsic::smin:
return Ops[0].smin(Ops[1]);
case Intrinsic::smax:
return Ops[0].smax(Ops[1]);
case Intrinsic::abs: {
const APInt *IntMinIsPoison = Ops[1].getSingleElement();
assert(IntMinIsPoison && "Must be known (immarg)");
assert(IntMinIsPoison->getBitWidth() == 1 && "Must be boolean");
return Ops[0].abs(IntMinIsPoison->getBoolValue());
}
default:
assert(!isIntrinsicSupported(IntrinsicID) && "Shouldn't be supported");
llvm_unreachable("Unsupported intrinsic");
}
}
ConstantRange
ConstantRange::add(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
if (isFullSet() || Other.isFullSet())
return getFull();
APInt NewLower = getLower() + Other.getLower();
APInt NewUpper = getUpper() + Other.getUpper() - 1;
if (NewLower == NewUpper)
return getFull();
ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper));
if (X.isSizeStrictlySmallerThan(*this) ||
X.isSizeStrictlySmallerThan(Other))
return getFull();
return X;
}
ConstantRange ConstantRange::addWithNoWrap(const ConstantRange &Other,
unsigned NoWrapKind,
PreferredRangeType RangeType) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
if (isFullSet() && Other.isFullSet())
return getFull();
using OBO = OverflowingBinaryOperator;
ConstantRange Result = add(Other);
if (NoWrapKind & OBO::NoSignedWrap)
Result = Result.intersectWith(sadd_sat(Other), RangeType);
if (NoWrapKind & OBO::NoUnsignedWrap)
Result = Result.intersectWith(uadd_sat(Other), RangeType);
return Result;
}
ConstantRange
ConstantRange::sub(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
if (isFullSet() || Other.isFullSet())
return getFull();
APInt NewLower = getLower() - Other.getUpper() + 1;
APInt NewUpper = getUpper() - Other.getLower();
if (NewLower == NewUpper)
return getFull();
ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper));
if (X.isSizeStrictlySmallerThan(*this) ||
X.isSizeStrictlySmallerThan(Other))
return getFull();
return X;
}
ConstantRange ConstantRange::subWithNoWrap(const ConstantRange &Other,
unsigned NoWrapKind,
PreferredRangeType RangeType) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
if (isFullSet() && Other.isFullSet())
return getFull();
using OBO = OverflowingBinaryOperator;
ConstantRange Result = sub(Other);
if (NoWrapKind & OBO::NoSignedWrap)
Result = Result.intersectWith(ssub_sat(Other), RangeType);
if (NoWrapKind & OBO::NoUnsignedWrap) {
if (getUnsignedMax().ult(Other.getUnsignedMin()))
return getEmpty(); Result = Result.intersectWith(usub_sat(Other), RangeType);
}
return Result;
}
ConstantRange
ConstantRange::multiply(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
ConstantRange Result_zext = ConstantRange(this_min * Other_min,
this_max * Other_max + 1);
ConstantRange UR = Result_zext.truncate(getBitWidth());
if (!UR.isUpperWrapped() &&
(UR.getUpper().isNonNegative() || UR.getUpper().isMinSignedValue()))
return UR;
this_min = getSignedMin().sext(getBitWidth() * 2);
this_max = getSignedMax().sext(getBitWidth() * 2);
Other_min = Other.getSignedMin().sext(getBitWidth() * 2);
Other_max = Other.getSignedMax().sext(getBitWidth() * 2);
auto L = {this_min * Other_min, this_min * Other_max,
this_max * Other_min, this_max * Other_max};
auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
ConstantRange Result_sext(std::min(L, Compare), std::max(L, Compare) + 1);
ConstantRange SR = Result_sext.truncate(getBitWidth());
return UR.isSizeStrictlySmallerThan(SR) ? UR : SR;
}
ConstantRange ConstantRange::smul_fast(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
APInt Min = getSignedMin();
APInt Max = getSignedMax();
APInt OtherMin = Other.getSignedMin();
APInt OtherMax = Other.getSignedMax();
bool O1, O2, O3, O4;
auto Muls = {Min.smul_ov(OtherMin, O1), Min.smul_ov(OtherMax, O2),
Max.smul_ov(OtherMin, O3), Max.smul_ov(OtherMax, O4)};
if (O1 || O2 || O3 || O4)
return getFull();
auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
return getNonEmpty(std::min(Muls, Compare), std::max(Muls, Compare) + 1);
}
ConstantRange
ConstantRange::smax(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
ConstantRange Res = getNonEmpty(std::move(NewL), std::move(NewU));
if (isSignWrappedSet() || Other.isSignWrappedSet())
return Res.intersectWith(unionWith(Other, Signed), Signed);
return Res;
}
ConstantRange
ConstantRange::umax(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
ConstantRange Res = getNonEmpty(std::move(NewL), std::move(NewU));
if (isWrappedSet() || Other.isWrappedSet())
return Res.intersectWith(unionWith(Other, Unsigned), Unsigned);
return Res;
}
ConstantRange
ConstantRange::smin(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
APInt NewL = APIntOps::smin(getSignedMin(), Other.getSignedMin());
APInt NewU = APIntOps::smin(getSignedMax(), Other.getSignedMax()) + 1;
ConstantRange Res = getNonEmpty(std::move(NewL), std::move(NewU));
if (isSignWrappedSet() || Other.isSignWrappedSet())
return Res.intersectWith(unionWith(Other, Signed), Signed);
return Res;
}
ConstantRange
ConstantRange::umin(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
APInt NewL = APIntOps::umin(getUnsignedMin(), Other.getUnsignedMin());
APInt NewU = APIntOps::umin(getUnsignedMax(), Other.getUnsignedMax()) + 1;
ConstantRange Res = getNonEmpty(std::move(NewL), std::move(NewU));
if (isWrappedSet() || Other.isWrappedSet())
return Res.intersectWith(unionWith(Other, Unsigned), Unsigned);
return Res;
}
ConstantRange
ConstantRange::udiv(const ConstantRange &RHS) const {
if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax().isZero())
return getEmpty();
APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
APInt RHS_umin = RHS.getUnsignedMin();
if (RHS_umin.isZero()) {
if (RHS.getUpper() == 1)
RHS_umin = RHS.getLower();
else
RHS_umin = 1;
}
APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
return getNonEmpty(std::move(Lower), std::move(Upper));
}
ConstantRange ConstantRange::sdiv(const ConstantRange &RHS) const {
APInt Zero = APInt::getZero(getBitWidth());
APInt SignedMin = APInt::getSignedMinValue(getBitWidth());
ConstantRange PosFilter =
getBitWidth() == 1 ? getEmpty()
: ConstantRange(APInt(getBitWidth(), 1), SignedMin);
ConstantRange NegFilter(SignedMin, Zero);
ConstantRange PosL = intersectWith(PosFilter);
ConstantRange NegL = intersectWith(NegFilter);
ConstantRange PosR = RHS.intersectWith(PosFilter);
ConstantRange NegR = RHS.intersectWith(NegFilter);
ConstantRange PosRes = getEmpty();
if (!PosL.isEmptySet() && !PosR.isEmptySet())
PosRes = ConstantRange(PosL.Lower.sdiv(PosR.Upper - 1),
(PosL.Upper - 1).sdiv(PosR.Lower) + 1);
if (!NegL.isEmptySet() && !NegR.isEmptySet()) {
APInt Lo = (NegL.Upper - 1).sdiv(NegR.Lower);
if (NegL.Lower.isMinSignedValue() && NegR.Upper.isZero()) {
if (!NegR.Lower.isAllOnes()) {
APInt AdjNegRUpper;
if (RHS.Lower.isAllOnes())
AdjNegRUpper = RHS.Upper;
else
AdjNegRUpper = NegR.Upper - 1;
PosRes = PosRes.unionWith(
ConstantRange(Lo, NegL.Lower.sdiv(AdjNegRUpper - 1) + 1));
}
if (NegL.Upper != SignedMin + 1) {
APInt AdjNegLLower;
if (Upper == SignedMin + 1)
AdjNegLLower = Lower;
else
AdjNegLLower = NegL.Lower + 1;
PosRes = PosRes.unionWith(
ConstantRange(std::move(Lo),
AdjNegLLower.sdiv(NegR.Upper - 1) + 1));
}
} else {
PosRes = PosRes.unionWith(
ConstantRange(std::move(Lo), NegL.Lower.sdiv(NegR.Upper - 1) + 1));
}
}
ConstantRange NegRes = getEmpty();
if (!PosL.isEmptySet() && !NegR.isEmptySet())
NegRes = ConstantRange((PosL.Upper - 1).sdiv(NegR.Upper - 1),
PosL.Lower.sdiv(NegR.Lower) + 1);
if (!NegL.isEmptySet() && !PosR.isEmptySet())
NegRes = NegRes.unionWith(
ConstantRange(NegL.Lower.sdiv(PosR.Lower),
(NegL.Upper - 1).sdiv(PosR.Upper - 1) + 1));
ConstantRange Res = NegRes.unionWith(PosRes, PreferredRangeType::Signed);
if (contains(Zero) && (!PosR.isEmptySet() || !NegR.isEmptySet()))
Res = Res.unionWith(ConstantRange(Zero));
return Res;
}
ConstantRange ConstantRange::urem(const ConstantRange &RHS) const {
if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax().isZero())
return getEmpty();
if (const APInt *RHSInt = RHS.getSingleElement()) {
if (RHSInt->isZero())
return getEmpty();
if (const APInt *LHSInt = getSingleElement())
return {LHSInt->urem(*RHSInt)};
}
if (getUnsignedMax().ult(RHS.getUnsignedMin()))
return *this;
APInt Upper = APIntOps::umin(getUnsignedMax(), RHS.getUnsignedMax() - 1) + 1;
return getNonEmpty(APInt::getZero(getBitWidth()), std::move(Upper));
}
ConstantRange ConstantRange::srem(const ConstantRange &RHS) const {
if (isEmptySet() || RHS.isEmptySet())
return getEmpty();
if (const APInt *RHSInt = RHS.getSingleElement()) {
if (RHSInt->isZero())
return getEmpty();
if (const APInt *LHSInt = getSingleElement())
return {LHSInt->srem(*RHSInt)};
}
ConstantRange AbsRHS = RHS.abs();
APInt MinAbsRHS = AbsRHS.getUnsignedMin();
APInt MaxAbsRHS = AbsRHS.getUnsignedMax();
if (MaxAbsRHS.isZero())
return getEmpty();
if (MinAbsRHS.isZero())
++MinAbsRHS;
APInt MinLHS = getSignedMin(), MaxLHS = getSignedMax();
if (MinLHS.isNonNegative()) {
if (MaxLHS.ult(MinAbsRHS))
return *this;
APInt Upper = APIntOps::umin(MaxLHS, MaxAbsRHS - 1) + 1;
return ConstantRange(APInt::getZero(getBitWidth()), std::move(Upper));
}
if (MaxLHS.isNegative()) {
if (MinLHS.ugt(-MinAbsRHS))
return *this;
APInt Lower = APIntOps::umax(MinLHS, -MaxAbsRHS + 1);
return ConstantRange(std::move(Lower), APInt(getBitWidth(), 1));
}
APInt Lower = APIntOps::umax(MinLHS, -MaxAbsRHS + 1);
APInt Upper = APIntOps::umin(MaxLHS, MaxAbsRHS - 1) + 1;
return ConstantRange(std::move(Lower), std::move(Upper));
}
ConstantRange ConstantRange::binaryNot() const {
return ConstantRange(APInt::getAllOnes(getBitWidth())).sub(*this);
}
ConstantRange ConstantRange::binaryAnd(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
ConstantRange KnownBitsRange =
fromKnownBits(toKnownBits() & Other.toKnownBits(), false);
ConstantRange UMinUMaxRange =
getNonEmpty(APInt::getZero(getBitWidth()),
APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax()) + 1);
return KnownBitsRange.intersectWith(UMinUMaxRange);
}
ConstantRange ConstantRange::binaryOr(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
ConstantRange KnownBitsRange =
fromKnownBits(toKnownBits() | Other.toKnownBits(), false);
ConstantRange UMaxUMinRange =
getNonEmpty(APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin()),
APInt::getZero(getBitWidth()));
return KnownBitsRange.intersectWith(UMaxUMinRange);
}
ConstantRange ConstantRange::binaryXor(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
if (isSingleElement() && Other.isSingleElement())
return {*getSingleElement() ^ *Other.getSingleElement()};
if (Other.isSingleElement() && Other.getSingleElement()->isAllOnes())
return binaryNot();
if (isSingleElement() && getSingleElement()->isAllOnes())
return Other.binaryNot();
return fromKnownBits(toKnownBits() ^ Other.toKnownBits(), false);
}
ConstantRange
ConstantRange::shl(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
APInt Min = getUnsignedMin();
APInt Max = getUnsignedMax();
if (const APInt *RHS = Other.getSingleElement()) {
unsigned BW = getBitWidth();
if (RHS->uge(BW))
return getEmpty();
unsigned EqualLeadingBits = (Min ^ Max).countLeadingZeros();
if (RHS->ule(EqualLeadingBits))
return getNonEmpty(Min << *RHS, (Max << *RHS) + 1);
return getNonEmpty(APInt::getZero(BW),
APInt::getBitsSetFrom(BW, RHS->getZExtValue()) + 1);
}
APInt OtherMax = Other.getUnsignedMax();
if (OtherMax.ugt(Max.countLeadingZeros()))
return getFull();
Min <<= Other.getUnsignedMin();
Max <<= OtherMax;
return ConstantRange::getNonEmpty(std::move(Min), std::move(Max) + 1);
}
ConstantRange
ConstantRange::lshr(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
APInt max = getUnsignedMax().lshr(Other.getUnsignedMin()) + 1;
APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
return getNonEmpty(std::move(min), std::move(max));
}
ConstantRange
ConstantRange::ashr(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
APInt PosMax = getSignedMax().ashr(Other.getUnsignedMin()) + 1;
APInt PosMin = getSignedMin().ashr(Other.getUnsignedMax());
APInt NegMax = getSignedMax().ashr(Other.getUnsignedMax()) + 1;
APInt NegMin = getSignedMin().ashr(Other.getUnsignedMin());
APInt max, min;
if (getSignedMin().isNonNegative()) {
min = PosMin;
max = PosMax;
} else if (getSignedMax().isNegative()) {
min = NegMin;
max = NegMax;
} else {
min = NegMin;
max = PosMax;
}
return getNonEmpty(std::move(min), std::move(max));
}
ConstantRange ConstantRange::uadd_sat(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
APInt NewL = getUnsignedMin().uadd_sat(Other.getUnsignedMin());
APInt NewU = getUnsignedMax().uadd_sat(Other.getUnsignedMax()) + 1;
return getNonEmpty(std::move(NewL), std::move(NewU));
}
ConstantRange ConstantRange::sadd_sat(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
APInt NewL = getSignedMin().sadd_sat(Other.getSignedMin());
APInt NewU = getSignedMax().sadd_sat(Other.getSignedMax()) + 1;
return getNonEmpty(std::move(NewL), std::move(NewU));
}
ConstantRange ConstantRange::usub_sat(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
APInt NewL = getUnsignedMin().usub_sat(Other.getUnsignedMax());
APInt NewU = getUnsignedMax().usub_sat(Other.getUnsignedMin()) + 1;
return getNonEmpty(std::move(NewL), std::move(NewU));
}
ConstantRange ConstantRange::ssub_sat(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
APInt NewL = getSignedMin().ssub_sat(Other.getSignedMax());
APInt NewU = getSignedMax().ssub_sat(Other.getSignedMin()) + 1;
return getNonEmpty(std::move(NewL), std::move(NewU));
}
ConstantRange ConstantRange::umul_sat(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
APInt NewL = getUnsignedMin().umul_sat(Other.getUnsignedMin());
APInt NewU = getUnsignedMax().umul_sat(Other.getUnsignedMax()) + 1;
return getNonEmpty(std::move(NewL), std::move(NewU));
}
ConstantRange ConstantRange::smul_sat(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
APInt Min = getSignedMin();
APInt Max = getSignedMax();
APInt OtherMin = Other.getSignedMin();
APInt OtherMax = Other.getSignedMax();
auto L = {Min.smul_sat(OtherMin), Min.smul_sat(OtherMax),
Max.smul_sat(OtherMin), Max.smul_sat(OtherMax)};
auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
return getNonEmpty(std::min(L, Compare), std::max(L, Compare) + 1);
}
ConstantRange ConstantRange::ushl_sat(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
APInt NewL = getUnsignedMin().ushl_sat(Other.getUnsignedMin());
APInt NewU = getUnsignedMax().ushl_sat(Other.getUnsignedMax()) + 1;
return getNonEmpty(std::move(NewL), std::move(NewU));
}
ConstantRange ConstantRange::sshl_sat(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return getEmpty();
APInt Min = getSignedMin(), Max = getSignedMax();
APInt ShAmtMin = Other.getUnsignedMin(), ShAmtMax = Other.getUnsignedMax();
APInt NewL = Min.sshl_sat(Min.isNonNegative() ? ShAmtMin : ShAmtMax);
APInt NewU = Max.sshl_sat(Max.isNegative() ? ShAmtMin : ShAmtMax) + 1;
return getNonEmpty(std::move(NewL), std::move(NewU));
}
ConstantRange ConstantRange::inverse() const {
if (isFullSet())
return getEmpty();
if (isEmptySet())
return getFull();
return ConstantRange(Upper, Lower);
}
ConstantRange ConstantRange::abs(bool IntMinIsPoison) const {
if (isEmptySet())
return getEmpty();
if (isSignWrappedSet()) {
APInt Lo;
if (Upper.isStrictlyPositive() || !Lower.isStrictlyPositive())
Lo = APInt::getZero(getBitWidth());
else
Lo = APIntOps::umin(Lower, -Upper + 1);
if (IntMinIsPoison)
return ConstantRange(Lo, APInt::getSignedMinValue(getBitWidth()));
else
return ConstantRange(Lo, APInt::getSignedMinValue(getBitWidth()) + 1);
}
APInt SMin = getSignedMin(), SMax = getSignedMax();
if (IntMinIsPoison && SMin.isMinSignedValue()) {
if (SMax.isMinSignedValue())
return getEmpty();
++SMin;
}
if (SMin.isNonNegative())
return *this;
if (SMax.isNegative())
return ConstantRange(-SMax, -SMin + 1);
return ConstantRange(APInt::getZero(getBitWidth()),
APIntOps::umax(-SMin, SMax) + 1);
}
ConstantRange::OverflowResult ConstantRange::unsignedAddMayOverflow(
const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return OverflowResult::MayOverflow;
APInt Min = getUnsignedMin(), Max = getUnsignedMax();
APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
if (Min.ugt(~OtherMin))
return OverflowResult::AlwaysOverflowsHigh;
if (Max.ugt(~OtherMax))
return OverflowResult::MayOverflow;
return OverflowResult::NeverOverflows;
}
ConstantRange::OverflowResult ConstantRange::signedAddMayOverflow(
const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return OverflowResult::MayOverflow;
APInt Min = getSignedMin(), Max = getSignedMax();
APInt OtherMin = Other.getSignedMin(), OtherMax = Other.getSignedMax();
APInt SignedMin = APInt::getSignedMinValue(getBitWidth());
APInt SignedMax = APInt::getSignedMaxValue(getBitWidth());
if (Min.isNonNegative() && OtherMin.isNonNegative() &&
Min.sgt(SignedMax - OtherMin))
return OverflowResult::AlwaysOverflowsHigh;
if (Max.isNegative() && OtherMax.isNegative() &&
Max.slt(SignedMin - OtherMax))
return OverflowResult::AlwaysOverflowsLow;
if (Max.isNonNegative() && OtherMax.isNonNegative() &&
Max.sgt(SignedMax - OtherMax))
return OverflowResult::MayOverflow;
if (Min.isNegative() && OtherMin.isNegative() &&
Min.slt(SignedMin - OtherMin))
return OverflowResult::MayOverflow;
return OverflowResult::NeverOverflows;
}
ConstantRange::OverflowResult ConstantRange::unsignedSubMayOverflow(
const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return OverflowResult::MayOverflow;
APInt Min = getUnsignedMin(), Max = getUnsignedMax();
APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
if (Max.ult(OtherMin))
return OverflowResult::AlwaysOverflowsLow;
if (Min.ult(OtherMax))
return OverflowResult::MayOverflow;
return OverflowResult::NeverOverflows;
}
ConstantRange::OverflowResult ConstantRange::signedSubMayOverflow(
const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return OverflowResult::MayOverflow;
APInt Min = getSignedMin(), Max = getSignedMax();
APInt OtherMin = Other.getSignedMin(), OtherMax = Other.getSignedMax();
APInt SignedMin = APInt::getSignedMinValue(getBitWidth());
APInt SignedMax = APInt::getSignedMaxValue(getBitWidth());
if (Min.isNonNegative() && OtherMax.isNegative() &&
Min.sgt(SignedMax + OtherMax))
return OverflowResult::AlwaysOverflowsHigh;
if (Max.isNegative() && OtherMin.isNonNegative() &&
Max.slt(SignedMin + OtherMin))
return OverflowResult::AlwaysOverflowsLow;
if (Max.isNonNegative() && OtherMin.isNegative() &&
Max.sgt(SignedMax + OtherMin))
return OverflowResult::MayOverflow;
if (Min.isNegative() && OtherMax.isNonNegative() &&
Min.slt(SignedMin + OtherMax))
return OverflowResult::MayOverflow;
return OverflowResult::NeverOverflows;
}
ConstantRange::OverflowResult ConstantRange::unsignedMulMayOverflow(
const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
return OverflowResult::MayOverflow;
APInt Min = getUnsignedMin(), Max = getUnsignedMax();
APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
bool Overflow;
(void) Min.umul_ov(OtherMin, Overflow);
if (Overflow)
return OverflowResult::AlwaysOverflowsHigh;
(void) Max.umul_ov(OtherMax, Overflow);
if (Overflow)
return OverflowResult::MayOverflow;
return OverflowResult::NeverOverflows;
}
void ConstantRange::print(raw_ostream &OS) const {
if (isFullSet())
OS << "full-set";
else if (isEmptySet())
OS << "empty-set";
else
OS << "[" << Lower << "," << Upper << ")";
}
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD void ConstantRange::dump() const {
print(dbgs());
}
#endif
ConstantRange llvm::getConstantRangeFromMetadata(const MDNode &Ranges) {
const unsigned NumRanges = Ranges.getNumOperands() / 2;
assert(NumRanges >= 1 && "Must have at least one range!");
assert(Ranges.getNumOperands() % 2 == 0 && "Must be a sequence of pairs");
auto *FirstLow = mdconst::extract<ConstantInt>(Ranges.getOperand(0));
auto *FirstHigh = mdconst::extract<ConstantInt>(Ranges.getOperand(1));
ConstantRange CR(FirstLow->getValue(), FirstHigh->getValue());
for (unsigned i = 1; i < NumRanges; ++i) {
auto *Low = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
auto *High = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
CR = CR.unionWith(ConstantRange(Low->getValue(), High->getValue()));
}
return CR;
}