add a new code
[oota-llvm.git] / lib / Support / ConstantRange.cpp
index dd3e44e4e92a0d7eea29b17a4ad8c72decc1b087..1e2a6375c457985cbd523c2a003136ba3ce7c22e 100644 (file)
@@ -44,22 +44,20 @@ 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))) &&
+  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 == APInt::getMaxValue(getBitWidth());
+  return Lower == Upper && Lower.isMaxValue();
 }
 
 /// isEmptySet - Return true if this set contains no members.
 ///
 bool ConstantRange::isEmptySet() const {
-  return Lower == Upper && Lower == APInt::getMinValue(getBitWidth());
+  return Lower == Upper && Lower.isMinValue();
 }
 
 /// isWrappedSet - Return true if this set wraps around the top of the range,
@@ -108,7 +106,7 @@ APInt ConstantRange::getUnsignedMin() const {
 /// ConstantRange.
 ///
 APInt ConstantRange::getSignedMax() const {
-  APInt SignedMax = APInt::getSignedMaxValue(getBitWidth());
+  APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
   if (!isWrappedSet()) {
     if (getLower().slt(getUpper() - 1))
       return getUpper() - 1;
@@ -130,7 +128,7 @@ APInt ConstantRange::getSignedMax() const {
 /// ConstantRange.
 ///
 APInt ConstantRange::getSignedMin() const {
-  APInt SignedMin = APInt::getSignedMinValue(getBitWidth());
+  APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
   if (!isWrappedSet()) {
     if (getLower().slt(getUpper() - 1))
       return getLower();
@@ -346,6 +344,23 @@ ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
   return ConstantRange(L, U);
 }
 
+/// signExtend - Return a new range in the specified integer type, which must
+/// be strictly larger than the current type.  The returned range will
+/// correspond to the possible range of values as if the source range had been
+/// sign extended.
+ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
+  unsigned SrcTySize = getBitWidth();
+  assert(SrcTySize < DstTySize && "Not a value extension");
+  if (isFullSet()) {
+    return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
+                         APInt::getLowBitsSet(DstTySize, SrcTySize-1));
+  }
+
+  APInt L = Lower; L.sext(DstTySize);
+  APInt U = Upper; U.sext(DstTySize);
+  return ConstantRange(L, U);
+}
+
 /// truncate - Return a new range in the specified integer type, which must be
 /// strictly smaller than the current type.  The returned range will
 /// correspond to the possible range of values as if the source range had been
@@ -353,7 +368,7 @@ ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
   unsigned SrcTySize = getBitWidth();
   assert(SrcTySize > DstTySize && "Not a value truncation");
-  APInt Size = APInt::getMaxValue(DstTySize).zext(SrcTySize);
+  APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
   if (isFullSet() || getSetSize().ugt(Size))
     return ConstantRange(DstTySize);