//===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// 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"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
/// Initialize a range to hold the single specified value.
///
ConstantRangeBase::ConstantRangeBase(const APInt & V)
: Lower(V), Upper(V + 1) {}
ConstantRangeBase::ConstantRangeBase(const APInt &L, const APInt &U)
: Lower(L), Upper(U) {
assert(L.getBitWidth() == U.getBitWidth() &&
"ConstantRange with unequal bit widths");
}
/// print - Print out the bounds to a stream...
///
void ConstantRangeBase::print(raw_ostream &OS) const {
OS << "[" << Lower << "," << Upper << ")";
}
/// dump - Allow printing from a debugger easily...
///
void ConstantRangeBase::dump() const {
print(errs());
}
std::ostream &llvm::operator<<(std::ostream &o,
const ConstantRangeBase &CR) {
raw_os_ostream OS(o);
OS << CR;
return o;
}
/// Initialize a full (the default) or empty set for the specified type.
///
ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) :
ConstantRangeBase(APInt(BitWidth, 0), APInt(BitWidth, 0)) {
if (Full)
Lower = Upper = APInt::getMaxValue(BitWidth);
else
Lower = Upper = APInt::getMinValue(BitWidth);
}
/// Initialize a range to hold the single specified value.
///
ConstantRange::ConstantRange(const APInt & V) : ConstantRangeBase(V) {}
ConstantRange::ConstantRange(const APInt &L, const APInt &U)
: ConstantRangeBase(L, U) {
assert((L != U || (L.isMaxValue() || L.isMinValue())) &&
"Lower == Upper, but they aren't min or max value!");
}
/// isFullSet - Return true if this set contains all of the elements possible
/// for this data-type
bool ConstantRange::isFullSet() const {
return Lower == Upper && Lower.isMaxValue();
}
/// isEmptySet - Return true if this set contains no members.
///
bool ConstantRange::isEmptySet() const {
return Lower == Upper && Lower.isMinValue();
}
/// isWrappedSet - Return true if this set wraps around the top of the range,
/// for example: [100, 8)
///
bool ConstantRange::isWrappedSet() const {
return Lower.ugt(Upper);
}
/// getSetSize - Return the number of elements in this set.
///
APInt ConstantRange::getSetSize() const {
if (isEmptySet())
return APInt(getBitWidth(), 0);
if (getBitWidth() == 1) {
if (Lower != Upper) // One of T or F in the set...
return APInt(2, 1);
return APInt(2, 2); // Must be full set...
}
// Simply subtract the bounds...
return Upper - Lower;
}
/// getUnsignedMax - Return the largest unsigned value contained in the
/// ConstantRange.
///
APInt ConstantRange::getUnsignedMax() const {
if (isFullSet() || isWrappedSet())
return APInt::getMaxValue(getBitWidth());
else
return getUpper() - 1;
}
/// getUnsignedMin - Return the smallest unsigned value contained in the
/// ConstantRange.
///
APInt ConstantRange::getUnsignedMin() const {
if (isFullSet() || (isWrappedSet() && getUpper() != 0))
return APInt::getMinValue(getBitWidth());
else
return getLower();
}
/// getSignedMax - Return the largest signed value contained in the
/// ConstantRange.
///
APInt ConstantRange::getSignedMax() const {
APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
if (!isWrappedSet()) {
if (getLower().sle(getUpper() - 1))
return getUpper() - 1;
else
return SignedMax;
} else {
if ((getUpper() - 1).slt(getLower())) {
if (getLower() != SignedMax)
return SignedMax;
else
return g