//===----------------------------------------------------------------------===//
#include "llvm/Support/ConstantRange.h"
+#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Instructions.h"
using namespace llvm;
else
return SignedMax;
} else {
- if ((getUpper() - 1).slt(getLower())) {
- if (getLower() != SignedMax)
- return SignedMax;
- else
- return getUpper() - 1;
- } else {
+ if (getLower().isNegative() == getUpper().isNegative())
+ return SignedMax;
+ else
return getUpper() - 1;
- }
}
}
}
/// intersectWith - Return the range that results from the intersection of this
-/// range with another range.
-///
+/// range with another range. The resultant range is guaranteed to include all
+/// elements contained in both input ranges, and to have the smallest possible
+/// set size that does so. Because there may be two intersections with the
+/// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
- assert(getBitWidth() == CR.getBitWidth() &&
- "ConstantRange types don't agree!");
- // Handle common special cases
- if (isEmptySet() || CR.isFullSet())
- return *this;
- if (isFullSet() || CR.isEmptySet())
- return CR;
-
- if (!isWrappedSet()) {
- if (!CR.isWrappedSet()) {
- APInt L = APIntOps::umax(Lower, CR.Lower);
- APInt U = APIntOps::umin(Upper, CR.Upper);
-
- if (L.ult(U)) // If range isn't empty...
- return ConstantRange(L, U);
- else
- return ConstantRange(getBitWidth(), false);// Otherwise, empty set
- } else
- return intersect1Wrapped(CR, *this);
- } else { // We know "this" is wrapped...
- if (!CR.isWrappedSet())
- return intersect1Wrapped(*this, CR);
- else {
- // Both ranges are wrapped...
- APInt L = APIntOps::umax(Lower, CR.Lower);
- APInt U = APIntOps::umin(Upper, CR.Upper);
- return ConstantRange(L, U);
- }
- }
- return *this;
-}
-
-/// maximalIntersectWith - Return the range that results from the intersection
-/// of this range with another range. The resultant range is guaranteed to
-/// include all elements contained in both input ranges, and to have the
-/// smallest possible set size that does so. Because there may be two
-/// intersections with the same set size, A.maximalIntersectWith(B) might not
-/// be equal to B.maximalIntersect(A).
-ConstantRange
-ConstantRange::maximalIntersectWith(const ConstantRange &CR) const {
assert(getBitWidth() == CR.getBitWidth() &&
"ConstantRange types don't agree!");
if (CR.isEmptySet() || isFullSet()) return CR;
if (!isWrappedSet() && CR.isWrappedSet())
- return CR.maximalIntersectWith(*this);
+ return CR.intersectWith(*this);
if (!isWrappedSet() && !CR.isWrappedSet()) {
if (Lower.ult(CR.Lower)) {
if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
- APInt L = Lower, U = Upper;
-
if (!isWrappedSet() && !CR.isWrappedSet()) {
+ if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
+ // If the two ranges are disjoint, find the smaller gap and bridge it.
+ APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
+ if (d1.ult(d2))
+ return ConstantRange(Lower, CR.Upper);
+ else
+ return ConstantRange(CR.Lower, Upper);
+ }
+
+ APInt L = Lower, U = Upper;
if (CR.Lower.ult(L))
L = CR.Lower;
-
- if (CR.Upper.ugt(U))
+ if ((CR.Upper - 1).ugt(U - 1))
U = CR.Upper;
+
+ if (L == 0 && U == 0)
+ return ConstantRange(getBitWidth());
+
+ return ConstantRange(L, U);
}
- if (isWrappedSet() && !CR.isWrappedSet()) {
- if ((CR.Lower.ult(Upper) && CR.Upper.ult(Upper)) ||
- (CR.Lower.ugt(Lower) && CR.Upper.ugt(Lower))) {
+ if (!CR.isWrappedSet()) {
+ // ------U L----- and ------U L----- : this
+ // L--U L--U : CR
+ if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
return *this;
- }
- if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) {
+ // ------U L----- : this
+ // L---------U : CR
+ if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
return ConstantRange(getBitWidth());
- }
-
- if (CR.Lower.ule(Upper) && CR.Upper.ule(Lower)) {
- APInt d1 = CR.Upper - Upper, d2 = Lower - CR.Upper;
- if (d1.ult(d2)) {
- U = CR.Upper;
- } else {
- L = CR.Upper;
- }
- }
- if (Upper.ult(CR.Lower) && CR.Upper.ult(Lower)) {
+ // ----U L---- : this
+ // L---U : CR
+ // <d1> <d2>
+ if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
- if (d1.ult(d2)) {
- U = CR.Lower + 1;
- } else {
- L = CR.Upper - 1;
- }
+ if (d1.ult(d2))
+ return ConstantRange(Lower, CR.Upper);
+ else
+ return ConstantRange(CR.Lower, Upper);
}
- if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) {
- APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Lower;
+ // ----U L----- : this
+ // L----U : CR
+ if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
+ return ConstantRange(CR.Lower, Upper);
- if (d1.ult(d2)) {
- U = CR.Lower + 1;
- } else {
- L = CR.Lower;
- }
- }
+ // ------U L---- : this
+ // L-----U : CR
+ if (CR.Lower.ult(Upper) && CR.Upper.ult(Lower))
+ return ConstantRange(Lower, CR.Upper);
}
- if (isWrappedSet() && CR.isWrappedSet()) {
- if (Lower.ult(CR.Upper) || CR.Lower.ult(Upper))
- return ConstantRange(getBitWidth());
-
- if (CR.Upper.ugt(U)) {
- U = CR.Upper;
- }
+ assert(isWrappedSet() && CR.isWrappedSet() &&
+ "ConstantRange::unionWith missed wrapped union unwrapped case");
- if (CR.Lower.ult(L)) {
- L = CR.Lower;
- }
+ // ------U L---- and ------U L---- : this
+ // -U L----------- and ------------U L : CR
+ if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
+ return ConstantRange(getBitWidth());
- if (L == U) return ConstantRange(getBitWidth());
- }
+ APInt L = Lower, U = Upper;
+ if (CR.Upper.ugt(U))
+ U = CR.Upper;
+ if (CR.Lower.ult(L))
+ L = CR.Lower;
return ConstantRange(L, U);
}
assert(SrcTySize < DstTySize && "Not a value extension");
if (isFullSet()) {
return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
- APInt::getLowBitsSet(DstTySize, SrcTySize-1));
+ APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
}
APInt L = Lower; L.sext(DstTySize);
return ConstantRange(L, U);
}
+/// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
+/// value is zero extended, truncated, or left alone to make it that width.
+ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
+ unsigned SrcTySize = getBitWidth();
+ if (SrcTySize > DstTySize)
+ return truncate(DstTySize);
+ else if (SrcTySize < DstTySize)
+ return zeroExtend(DstTySize);
+ else
+ return *this;
+}
+
+/// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
+/// value is sign extended, truncated, or left alone to make it that width.
+ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
+ unsigned SrcTySize = getBitWidth();
+ if (SrcTySize > DstTySize)
+ return truncate(DstTySize);
+ else if (SrcTySize < DstTySize)
+ return signExtend(DstTySize);
+ else
+ return *this;
+}
+
ConstantRange
ConstantRange::add(const ConstantRange &Other) const {
if (isEmptySet() || Other.isEmptySet())
ConstantRange
ConstantRange::multiply(const ConstantRange &Other) const {
+ // TODO: If either operand is a single element and the multiply is known to
+ // be non-wrapping, round the result min and max value to the appropriate
+ // multiple of that element. If wrapping is possible, at least adjust the
+ // range according to the greatest power-of-two factor of the single element.
+
if (isEmptySet() || Other.isEmptySet())
return ConstantRange(getBitWidth(), /*isFullSet=*/false);
if (isFullSet() || Other.isFullSet())
return ConstantRange(Lower, Upper);
}
+ConstantRange
+ConstantRange::shl(const ConstantRange &Amount) const {
+ if (isEmptySet())
+ return *this;
+
+ APInt min = getUnsignedMin() << Amount.getUnsignedMin();
+ APInt max = getUnsignedMax() << Amount.getUnsignedMax();
+
+ // there's no overflow!
+ APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
+ if (Zeros.uge(Amount.getUnsignedMax()))
+ return ConstantRange(min, max);
+
+ // FIXME: implement the other tricky cases
+ return ConstantRange(getBitWidth());
+}
+
+ConstantRange
+ConstantRange::ashr(const ConstantRange &Amount) const {
+ if (isEmptySet())
+ return *this;
+
+ APInt min = getUnsignedMax().ashr(Amount.getUnsignedMin());
+ APInt max = getUnsignedMin().ashr(Amount.getUnsignedMax());
+ return ConstantRange(min, max);
+}
+
+ConstantRange
+ConstantRange::lshr(const ConstantRange &Amount) const {
+ if (isEmptySet())
+ return *this;
+
+ APInt min = getUnsignedMax().lshr(Amount.getUnsignedMin());
+ APInt max = getUnsignedMin().lshr(Amount.getUnsignedMax());
+ return ConstantRange(min, max);
+}
+
/// print - Print out the bounds to a stream...
///
void ConstantRange::print(raw_ostream &OS) const {
- OS << "[" << Lower << "," << Upper << ")";
+ if (isFullSet())
+ OS << "full-set";
+ else if (isEmptySet())
+ OS << "empty-set";
+ else
+ OS << "[" << Lower << "," << Upper << ")";
}
/// dump - Allow printing from a debugger easily...
///
void ConstantRange::dump() const {
- print(errs());
+ print(dbgs());
}
-std::ostream &llvm::operator<<(std::ostream &o,
- const ConstantRange &CR) {
- raw_os_ostream OS(o);
- OS << CR;
- return o;
-}
+