2002-09-01 23:53:36 +00:00
|
|
|
//===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-09-01 23:53:36 +00:00
|
|
|
//
|
|
|
|
// Represent a range of possible values that may occur when the program is run
|
|
|
|
// for an integral value. This keeps track of a lower and upper bound for the
|
|
|
|
// constant, which MAY wrap around the end of the numeric range. To do this, it
|
|
|
|
// keeps track of a [lower, upper) bound, which specifies an interval just like
|
|
|
|
// STL iterators. When used with boolean values, the following are important
|
|
|
|
// ranges (other integral ranges use min/max values for special range values):
|
|
|
|
//
|
|
|
|
// [F, F) = {} = Empty set
|
|
|
|
// [T, F) = {T}
|
|
|
|
// [F, T) = {F}
|
|
|
|
// [T, T) = {F, T} = Full set
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/ConstantRange.h"
|
2006-11-28 22:46:12 +00:00
|
|
|
#include "llvm/Support/Streams.h"
|
|
|
|
#include <ostream>
|
2003-12-14 21:35:53 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-09-01 23:53:36 +00:00
|
|
|
/// Initialize a full (the default) or empty set for the specified type.
|
|
|
|
///
|
2007-02-28 22:02:48 +00:00
|
|
|
ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) :
|
|
|
|
Lower(BitWidth, 0), Upper(BitWidth, 0) {
|
2002-09-01 23:53:36 +00:00
|
|
|
if (Full)
|
2007-02-28 17:36:23 +00:00
|
|
|
Lower = Upper = APInt::getMaxValue(BitWidth);
|
2002-09-01 23:53:36 +00:00
|
|
|
else
|
2007-02-28 17:36:23 +00:00
|
|
|
Lower = Upper = APInt::getMinValue(BitWidth);
|
2002-09-01 23:53:36 +00:00
|
|
|
}
|
|
|
|
|
2004-03-30 00:20:08 +00:00
|
|
|
/// Initialize a range to hold the single specified value.
|
|
|
|
///
|
2007-02-28 18:57:32 +00:00
|
|
|
ConstantRange::ConstantRange(const APInt & V) : Lower(V), Upper(V + 1) { }
|
2002-09-01 23:53:36 +00:00
|
|
|
|
2007-02-28 17:36:23 +00:00
|
|
|
ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
|
|
|
|
Lower(L), Upper(U) {
|
|
|
|
assert(L.getBitWidth() == U.getBitWidth() &&
|
|
|
|
"ConstantRange with unequal bit widths");
|
|
|
|
uint32_t BitWidth = L.getBitWidth();
|
|
|
|
assert((L != U || (L == APInt::getMaxValue(BitWidth) ||
|
|
|
|
L == APInt::getMinValue(BitWidth))) &&
|
|
|
|
"Lower == Upper, but they aren't min or max value!");
|
|
|
|
}
|
|
|
|
|
2002-09-01 23:53:36 +00:00
|
|
|
/// isFullSet - Return true if this set contains all of the elements possible
|
|
|
|
/// for this data-type
|
|
|
|
bool ConstantRange::isFullSet() const {
|
2007-02-28 22:02:48 +00:00
|
|
|
return Lower == Upper && Lower == APInt::getMaxValue(getBitWidth());
|
2002-09-01 23:53:36 +00:00
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-09-01 23:53:36 +00:00
|
|
|
/// isEmptySet - Return true if this set contains no members.
|
|
|
|
///
|
|
|
|
bool ConstantRange::isEmptySet() const {
|
2007-02-28 22:02:48 +00:00
|
|
|
return Lower == Upper && Lower == APInt::getMinValue(getBitWidth());
|
2002-09-01 23:53:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// isWrappedSet - Return true if this set wraps around the top of the range,
|
|
|
|
/// for example: [100, 8)
|
|
|
|
///
|
2006-12-23 06:05:41 +00:00
|
|
|
bool ConstantRange::isWrappedSet(bool isSigned) const {
|
2007-02-28 17:36:23 +00:00
|
|
|
if (isSigned)
|
|
|
|
return Lower.sgt(Upper);
|
|
|
|
return Lower.ugt(Upper);
|
2002-09-01 23:53:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getSetSize - Return the number of elements in this set.
|
|
|
|
///
|
2007-02-28 17:36:23 +00:00
|
|
|
APInt ConstantRange::getSetSize() const {
|
|
|
|
if (isEmptySet())
|
2007-02-28 22:02:48 +00:00
|
|
|
return APInt(getBitWidth(), 0);
|
|
|
|
if (getBitWidth() == 1) {
|
2002-09-01 23:53:36 +00:00
|
|
|
if (Lower != Upper) // One of T or F in the set...
|
2007-02-28 22:02:48 +00:00
|
|
|
return APInt(2, 1);
|
|
|
|
return APInt(2, 2); // Must be full set...
|
2002-09-01 23:53:36 +00:00
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-09-01 23:53:36 +00:00
|
|
|
// Simply subtract the bounds...
|
2007-02-28 17:36:23 +00:00
|
|
|
return Upper - Lower;
|
2002-09-01 23:53:36 +00:00
|
|
|
}
|
|
|
|
|
2004-03-30 00:20:08 +00:00
|
|
|
/// contains - Return true if the specified value is in the set.
|
|
|
|
///
|
2007-02-28 19:57:34 +00:00
|
|
|
bool ConstantRange::contains(const APInt &V, bool isSigned) const {
|
2004-03-30 00:20:08 +00:00
|
|
|
if (Lower == Upper) {
|
2007-02-28 17:36:23 +00:00
|
|
|
if (isFullSet())
|
|
|
|
return true;
|
2004-03-30 00:20:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
if (!isWrappedSet(isSigned))
|
2007-02-28 17:36:23 +00:00
|
|
|
if (isSigned)
|
|
|
|
return Lower.sle(V) && V.slt(Upper);
|
|
|
|
else
|
|
|
|
return Lower.ule(V) && V.ult(Upper);
|
|
|
|
if (isSigned)
|
|
|
|
return Lower.sle(V) || V.slt(Upper);
|
|
|
|
else
|
|
|
|
return Lower.ule(V) || V.ult(Upper);
|
2004-03-30 00:20:08 +00:00
|
|
|
}
|
2002-09-01 23:53:36 +00:00
|
|
|
|
2004-03-30 00:20:08 +00:00
|
|
|
/// subtract - Subtract the specified constant from the endpoints of this
|
|
|
|
/// constant range.
|
2007-02-28 19:57:34 +00:00
|
|
|
ConstantRange ConstantRange::subtract(const APInt &Val) const {
|
2007-02-28 22:02:48 +00:00
|
|
|
assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
|
2004-03-30 00:20:08 +00:00
|
|
|
// If the set is empty or full, don't modify the endpoints.
|
2007-02-28 17:36:23 +00:00
|
|
|
if (Lower == Upper)
|
|
|
|
return *this;
|
|
|
|
return ConstantRange(Lower - Val, Upper - Val);
|
2004-03-30 00:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-01 23:53:36 +00:00
|
|
|
// intersect1Wrapped - This helper function is used to intersect two ranges when
|
|
|
|
// it is known that LHS is wrapped and RHS isn't.
|
|
|
|
//
|
2007-02-28 17:36:23 +00:00
|
|
|
ConstantRange
|
|
|
|
ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
|
|
|
|
const ConstantRange &RHS, bool isSigned) {
|
2006-12-23 06:05:41 +00:00
|
|
|
assert(LHS.isWrappedSet(isSigned) && !RHS.isWrappedSet(isSigned));
|
2002-09-01 23:53:36 +00:00
|
|
|
|
|
|
|
// Check to see if we overlap on the Left side of RHS...
|
|
|
|
//
|
2007-02-28 17:36:23 +00:00
|
|
|
bool LT = (isSigned ? RHS.Lower.slt(LHS.Upper) : RHS.Lower.ult(LHS.Upper));
|
|
|
|
bool GT = (isSigned ? RHS.Upper.sgt(LHS.Lower) : RHS.Upper.ugt(LHS.Lower));
|
|
|
|
if (LT) {
|
2002-09-01 23:53:36 +00:00
|
|
|
// We do overlap on the left side of RHS, see if we overlap on the right of
|
|
|
|
// RHS...
|
2007-02-28 17:36:23 +00:00
|
|
|
if (GT) {
|
2002-09-01 23:53:36 +00:00
|
|
|
// Ok, the result overlaps on both the left and right sides. See if the
|
|
|
|
// resultant interval will be smaller if we wrap or not...
|
|
|
|
//
|
2007-02-28 17:36:23 +00:00
|
|
|
if (LHS.getSetSize().ult(RHS.getSetSize()))
|
2002-09-01 23:53:36 +00:00
|
|
|
return LHS;
|
|
|
|
else
|
|
|
|
return RHS;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// No overlap on the right, just on the left.
|
2007-02-28 18:57:32 +00:00
|
|
|
return ConstantRange(RHS.Lower, LHS.Upper);
|
2002-09-01 23:53:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We don't overlap on the left side of RHS, see if we overlap on the right
|
|
|
|
// of RHS...
|
2007-02-28 17:36:23 +00:00
|
|
|
if (GT) {
|
2002-09-01 23:53:36 +00:00
|
|
|
// Simple overlap...
|
2007-02-28 18:57:32 +00:00
|
|
|
return ConstantRange(LHS.Lower, RHS.Upper);
|
2002-09-01 23:53:36 +00:00
|
|
|
} else {
|
|
|
|
// No overlap...
|
2007-02-28 22:02:48 +00:00
|
|
|
return ConstantRange(LHS.getBitWidth(), false);
|
2002-09-01 23:53:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-11 00:58:49 +00:00
|
|
|
/// intersectWith - Return the range that results from the intersection of this
|
2002-09-01 23:53:36 +00:00
|
|
|
/// range with another range.
|
|
|
|
///
|
2006-12-23 06:05:41 +00:00
|
|
|
ConstantRange ConstantRange::intersectWith(const ConstantRange &CR,
|
|
|
|
bool isSigned) const {
|
2007-02-28 22:02:48 +00:00
|
|
|
assert(getBitWidth() == CR.getBitWidth() &&
|
|
|
|
"ConstantRange types don't agree!");
|
2002-09-02 20:49:27 +00:00
|
|
|
// Handle common special cases
|
2007-02-28 17:36:23 +00:00
|
|
|
if (isEmptySet() || CR.isFullSet())
|
|
|
|
return *this;
|
|
|
|
if (isFullSet() || CR.isEmptySet())
|
|
|
|
return CR;
|
2002-09-01 23:53:36 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
if (!isWrappedSet(isSigned)) {
|
|
|
|
if (!CR.isWrappedSet(isSigned)) {
|
2007-02-28 17:36:23 +00:00
|
|
|
using namespace APIntOps;
|
|
|
|
APInt L = isSigned ? smax(Lower, CR.Lower) : umax(Lower, CR.Lower);
|
|
|
|
APInt U = isSigned ? smin(Upper, CR.Upper) : umin(Upper, CR.Upper);
|
2002-09-01 23:53:36 +00:00
|
|
|
|
2007-02-28 17:36:23 +00:00
|
|
|
if (isSigned ? L.slt(U) : L.ult(U)) // If range isn't empty...
|
2002-09-02 20:49:27 +00:00
|
|
|
return ConstantRange(L, U);
|
2002-09-01 23:53:36 +00:00
|
|
|
else
|
2007-02-28 22:02:48 +00:00
|
|
|
return ConstantRange(getBitWidth(), false);// Otherwise, empty set
|
2002-09-01 23:53:36 +00:00
|
|
|
} else
|
2006-12-23 06:05:41 +00:00
|
|
|
return intersect1Wrapped(CR, *this, isSigned);
|
2002-09-01 23:53:36 +00:00
|
|
|
} else { // We know "this" is wrapped...
|
2006-12-23 06:05:41 +00:00
|
|
|
if (!CR.isWrappedSet(isSigned))
|
|
|
|
return intersect1Wrapped(*this, CR, isSigned);
|
2002-09-01 23:53:36 +00:00
|
|
|
else {
|
|
|
|
// Both ranges are wrapped...
|
2007-02-28 17:36:23 +00:00
|
|
|
using namespace APIntOps;
|
|
|
|
APInt L = isSigned ? smax(Lower, CR.Lower) : umax(Lower, CR.Lower);
|
|
|
|
APInt U = isSigned ? smin(Upper, CR.Upper) : umin(Upper, CR.Upper);
|
2002-09-02 20:49:27 +00:00
|
|
|
return ConstantRange(L, U);
|
2002-09-01 23:53:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2007-02-11 00:58:49 +00:00
|
|
|
/// unionWith - Return the range that results from the union of this range with
|
2002-09-01 23:53:36 +00:00
|
|
|
/// another range. The resultant range is guaranteed to include the elements of
|
|
|
|
/// both sets, but may contain more. For example, [3, 9) union [12,15) is [3,
|
|
|
|
/// 15), which includes 9, 10, and 11, which were not included in either set
|
|
|
|
/// before.
|
|
|
|
///
|
2006-12-23 06:05:41 +00:00
|
|
|
ConstantRange ConstantRange::unionWith(const ConstantRange &CR,
|
|
|
|
bool isSigned) const {
|
2007-02-28 22:02:48 +00:00
|
|
|
assert(getBitWidth() == CR.getBitWidth() &&
|
|
|
|
"ConstantRange types don't agree!");
|
2002-09-01 23:53:36 +00:00
|
|
|
|
|
|
|
assert(0 && "Range union not implemented yet!");
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
2002-09-02 00:18:22 +00:00
|
|
|
|
2004-03-30 00:20:08 +00:00
|
|
|
/// zeroExtend - Return a new range in the specified integer type, which must
|
|
|
|
/// be strictly larger than the current type. The returned range will
|
2006-12-23 06:05:41 +00:00
|
|
|
/// correspond to the possible range of values as if the source range had been
|
2004-03-30 00:20:08 +00:00
|
|
|
/// zero extended.
|
2007-02-28 22:02:48 +00:00
|
|
|
ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
|
|
|
|
unsigned SrcTySize = getBitWidth();
|
2007-02-28 17:36:23 +00:00
|
|
|
assert(SrcTySize < DstTySize && "Not a value extension");
|
2007-02-28 18:57:32 +00:00
|
|
|
if (isFullSet())
|
2004-03-30 00:20:08 +00:00
|
|
|
// Change a source full set into [0, 1 << 8*numbytes)
|
2007-02-28 18:57:32 +00:00
|
|
|
return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
|
2004-03-30 00:20:08 +00:00
|
|
|
|
2007-02-28 17:36:23 +00:00
|
|
|
APInt L = Lower; L.zext(DstTySize);
|
|
|
|
APInt U = Upper; U.zext(DstTySize);
|
|
|
|
return ConstantRange(L, U);
|
2004-03-30 00:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// truncate - Return a new range in the specified integer type, which must be
|
|
|
|
/// strictly smaller than the current type. The returned range will
|
2006-12-23 06:05:41 +00:00
|
|
|
/// correspond to the possible range of values as if the source range had been
|
2004-03-30 00:20:08 +00:00
|
|
|
/// truncated to the specified type.
|
2007-02-28 22:02:48 +00:00
|
|
|
ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
|
|
|
|
unsigned SrcTySize = getBitWidth();
|
2007-02-28 17:36:23 +00:00
|
|
|
assert(SrcTySize > DstTySize && "Not a value truncation");
|
|
|
|
APInt Size = APInt::getMaxValue(DstTySize).zext(SrcTySize);
|
|
|
|
if (isFullSet() || getSetSize().ugt(Size))
|
2007-02-28 22:02:48 +00:00
|
|
|
return ConstantRange(DstTySize);
|
2004-03-30 00:20:08 +00:00
|
|
|
|
2007-02-28 17:36:23 +00:00
|
|
|
APInt L = Lower; L.trunc(DstTySize);
|
|
|
|
APInt U = Upper; U.trunc(DstTySize);
|
|
|
|
return ConstantRange(L, U);
|
2004-03-30 00:20:08 +00:00
|
|
|
}
|
|
|
|
|
2002-09-02 00:18:22 +00:00
|
|
|
/// print - Print out the bounds to a stream...
|
|
|
|
///
|
|
|
|
void ConstantRange::print(std::ostream &OS) const {
|
2007-02-28 17:36:23 +00:00
|
|
|
OS << "[" << Lower.toStringSigned(10) << ","
|
|
|
|
<< Upper.toStringSigned(10) << " )";
|
2002-09-02 00:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// dump - Allow printing from a debugger easily...
|
|
|
|
///
|
|
|
|
void ConstantRange::dump() const {
|
2006-12-07 01:30:32 +00:00
|
|
|
print(cerr);
|
2002-09-02 00:18:22 +00:00
|
|
|
}
|