LLVM API Documentation

ConstantRange.cpp
Go to the documentation of this file.
00001 //===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Represent a range of possible values that may occur when the program is run
00011 // for an integral value.  This keeps track of a lower and upper bound for the
00012 // constant, which MAY wrap around the end of the numeric range.  To do this, it
00013 // keeps track of a [lower, upper) bound, which specifies an interval just like
00014 // STL iterators.  When used with boolean values, the following are important
00015 // ranges (other integral ranges use min/max values for special range values):
00016 //
00017 //  [F, F) = {}     = Empty set
00018 //  [T, F) = {T}
00019 //  [F, T) = {F}
00020 //  [T, T) = {F, T} = Full set
00021 //
00022 //===----------------------------------------------------------------------===//
00023 
00024 #include "llvm/IR/InstrTypes.h"
00025 #include "llvm/IR/ConstantRange.h"
00026 #include "llvm/Support/Debug.h"
00027 #include "llvm/Support/raw_ostream.h"
00028 using namespace llvm;
00029 
00030 /// Initialize a full (the default) or empty set for the specified type.
00031 ///
00032 ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) {
00033   if (Full)
00034     Lower = Upper = APInt::getMaxValue(BitWidth);
00035   else
00036     Lower = Upper = APInt::getMinValue(BitWidth);
00037 }
00038 
00039 /// Initialize a range to hold the single specified value.
00040 ///
00041 ConstantRange::ConstantRange(APIntMoveTy V)
00042     : Lower(std::move(V)), Upper(Lower + 1) {}
00043 
00044 ConstantRange::ConstantRange(APIntMoveTy L, APIntMoveTy U)
00045     : Lower(std::move(L)), Upper(std::move(U)) {
00046   assert(Lower.getBitWidth() == Upper.getBitWidth() &&
00047          "ConstantRange with unequal bit widths");
00048   assert((Lower != Upper || (Lower.isMaxValue() || Lower.isMinValue())) &&
00049          "Lower == Upper, but they aren't min or max value!");
00050 }
00051 
00052 ConstantRange ConstantRange::makeICmpRegion(unsigned Pred,
00053                                             const ConstantRange &CR) {
00054   if (CR.isEmptySet())
00055     return CR;
00056 
00057   uint32_t W = CR.getBitWidth();
00058   switch (Pred) {
00059     default: llvm_unreachable("Invalid ICmp predicate to makeICmpRegion()");
00060     case CmpInst::ICMP_EQ:
00061       return CR;
00062     case CmpInst::ICMP_NE:
00063       if (CR.isSingleElement())
00064         return ConstantRange(CR.getUpper(), CR.getLower());
00065       return ConstantRange(W);
00066     case CmpInst::ICMP_ULT: {
00067       APInt UMax(CR.getUnsignedMax());
00068       if (UMax.isMinValue())
00069         return ConstantRange(W, /* empty */ false);
00070       return ConstantRange(APInt::getMinValue(W), UMax);
00071     }
00072     case CmpInst::ICMP_SLT: {
00073       APInt SMax(CR.getSignedMax());
00074       if (SMax.isMinSignedValue())
00075         return ConstantRange(W, /* empty */ false);
00076       return ConstantRange(APInt::getSignedMinValue(W), SMax);
00077     }
00078     case CmpInst::ICMP_ULE: {
00079       APInt UMax(CR.getUnsignedMax());
00080       if (UMax.isMaxValue())
00081         return ConstantRange(W);
00082       return ConstantRange(APInt::getMinValue(W), UMax + 1);
00083     }
00084     case CmpInst::ICMP_SLE: {
00085       APInt SMax(CR.getSignedMax());
00086       if (SMax.isMaxSignedValue())
00087         return ConstantRange(W);
00088       return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
00089     }
00090     case CmpInst::ICMP_UGT: {
00091       APInt UMin(CR.getUnsignedMin());
00092       if (UMin.isMaxValue())
00093         return ConstantRange(W, /* empty */ false);
00094       return ConstantRange(UMin + 1, APInt::getNullValue(W));
00095     }
00096     case CmpInst::ICMP_SGT: {
00097       APInt SMin(CR.getSignedMin());
00098       if (SMin.isMaxSignedValue())
00099         return ConstantRange(W, /* empty */ false);
00100       return ConstantRange(SMin + 1, APInt::getSignedMinValue(W));
00101     }
00102     case CmpInst::ICMP_UGE: {
00103       APInt UMin(CR.getUnsignedMin());
00104       if (UMin.isMinValue())
00105         return ConstantRange(W);
00106       return ConstantRange(UMin, APInt::getNullValue(W));
00107     }
00108     case CmpInst::ICMP_SGE: {
00109       APInt SMin(CR.getSignedMin());
00110       if (SMin.isMinSignedValue())
00111         return ConstantRange(W);
00112       return ConstantRange(SMin, APInt::getSignedMinValue(W));
00113     }
00114   }
00115 }
00116 
00117 /// isFullSet - Return true if this set contains all of the elements possible
00118 /// for this data-type
00119 bool ConstantRange::isFullSet() const {
00120   return Lower == Upper && Lower.isMaxValue();
00121 }
00122 
00123 /// isEmptySet - Return true if this set contains no members.
00124 ///
00125 bool ConstantRange::isEmptySet() const {
00126   return Lower == Upper && Lower.isMinValue();
00127 }
00128 
00129 /// isWrappedSet - Return true if this set wraps around the top of the range,
00130 /// for example: [100, 8)
00131 ///
00132 bool ConstantRange::isWrappedSet() const {
00133   return Lower.ugt(Upper);
00134 }
00135 
00136 /// isSignWrappedSet - Return true if this set wraps around the INT_MIN of
00137 /// its bitwidth, for example: i8 [120, 140).
00138 ///
00139 bool ConstantRange::isSignWrappedSet() const {
00140   return contains(APInt::getSignedMaxValue(getBitWidth())) &&
00141          contains(APInt::getSignedMinValue(getBitWidth()));
00142 }
00143 
00144 /// getSetSize - Return the number of elements in this set.
00145 ///
00146 APInt ConstantRange::getSetSize() const {
00147   if (isFullSet()) {
00148     APInt Size(getBitWidth()+1, 0);
00149     Size.setBit(getBitWidth());
00150     return Size;
00151   }
00152 
00153   // This is also correct for wrapped sets.
00154   return (Upper - Lower).zext(getBitWidth()+1);
00155 }
00156 
00157 /// getUnsignedMax - Return the largest unsigned value contained in the
00158 /// ConstantRange.
00159 ///
00160 APInt ConstantRange::getUnsignedMax() const {
00161   if (isFullSet() || isWrappedSet())
00162     return APInt::getMaxValue(getBitWidth());
00163   return getUpper() - 1;
00164 }
00165 
00166 /// getUnsignedMin - Return the smallest unsigned value contained in the
00167 /// ConstantRange.
00168 ///
00169 APInt ConstantRange::getUnsignedMin() const {
00170   if (isFullSet() || (isWrappedSet() && getUpper() != 0))
00171     return APInt::getMinValue(getBitWidth());
00172   return getLower();
00173 }
00174 
00175 /// getSignedMax - Return the largest signed value contained in the
00176 /// ConstantRange.
00177 ///
00178 APInt ConstantRange::getSignedMax() const {
00179   APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
00180   if (!isWrappedSet()) {
00181     if (getLower().sle(getUpper() - 1))
00182       return getUpper() - 1;
00183     return SignedMax;
00184   }
00185   if (getLower().isNegative() == getUpper().isNegative())
00186     return SignedMax;
00187   return getUpper() - 1;
00188 }
00189 
00190 /// getSignedMin - Return the smallest signed value contained in the
00191 /// ConstantRange.
00192 ///
00193 APInt ConstantRange::getSignedMin() const {
00194   APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
00195   if (!isWrappedSet()) {
00196     if (getLower().sle(getUpper() - 1))
00197       return getLower();
00198     return SignedMin;
00199   }
00200   if ((getUpper() - 1).slt(getLower())) {
00201     if (getUpper() != SignedMin)
00202       return SignedMin;
00203   }
00204   return getLower();
00205 }
00206 
00207 /// contains - Return true if the specified value is in the set.
00208 ///
00209 bool ConstantRange::contains(const APInt &V) const {
00210   if (Lower == Upper)
00211     return isFullSet();
00212 
00213   if (!isWrappedSet())
00214     return Lower.ule(V) && V.ult(Upper);
00215   return Lower.ule(V) || V.ult(Upper);
00216 }
00217 
00218 /// contains - Return true if the argument is a subset of this range.
00219 /// Two equal sets contain each other. The empty set contained by all other
00220 /// sets.
00221 ///
00222 bool ConstantRange::contains(const ConstantRange &Other) const {
00223   if (isFullSet() || Other.isEmptySet()) return true;
00224   if (isEmptySet() || Other.isFullSet()) return false;
00225 
00226   if (!isWrappedSet()) {
00227     if (Other.isWrappedSet())
00228       return false;
00229 
00230     return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
00231   }
00232 
00233   if (!Other.isWrappedSet())
00234     return Other.getUpper().ule(Upper) ||
00235            Lower.ule(Other.getLower());
00236 
00237   return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
00238 }
00239 
00240 /// subtract - Subtract the specified constant from the endpoints of this
00241 /// constant range.
00242 ConstantRange ConstantRange::subtract(const APInt &Val) const {
00243   assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
00244   // If the set is empty or full, don't modify the endpoints.
00245   if (Lower == Upper) 
00246     return *this;
00247   return ConstantRange(Lower - Val, Upper - Val);
00248 }
00249 
00250 /// \brief Subtract the specified range from this range (aka relative complement
00251 /// of the sets).
00252 ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
00253   return intersectWith(CR.inverse());
00254 }
00255 
00256 /// intersectWith - Return the range that results from the intersection of this
00257 /// range with another range.  The resultant range is guaranteed to include all
00258 /// elements contained in both input ranges, and to have the smallest possible
00259 /// set size that does so.  Because there may be two intersections with the
00260 /// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
00261 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
00262   assert(getBitWidth() == CR.getBitWidth() && 
00263          "ConstantRange types don't agree!");
00264 
00265   // Handle common cases.
00266   if (   isEmptySet() || CR.isFullSet()) return *this;
00267   if (CR.isEmptySet() ||    isFullSet()) return CR;
00268 
00269   if (!isWrappedSet() && CR.isWrappedSet())
00270     return CR.intersectWith(*this);
00271 
00272   if (!isWrappedSet() && !CR.isWrappedSet()) {
00273     if (Lower.ult(CR.Lower)) {
00274       if (Upper.ule(CR.Lower))
00275         return ConstantRange(getBitWidth(), false);
00276 
00277       if (Upper.ult(CR.Upper))
00278         return ConstantRange(CR.Lower, Upper);
00279 
00280       return CR;
00281     }
00282     if (Upper.ult(CR.Upper))
00283       return *this;
00284 
00285     if (Lower.ult(CR.Upper))
00286       return ConstantRange(Lower, CR.Upper);
00287 
00288     return ConstantRange(getBitWidth(), false);
00289   }
00290 
00291   if (isWrappedSet() && !CR.isWrappedSet()) {
00292     if (CR.Lower.ult(Upper)) {
00293       if (CR.Upper.ult(Upper))
00294         return CR;
00295 
00296       if (CR.Upper.ule(Lower))
00297         return ConstantRange(CR.Lower, Upper);
00298 
00299       if (getSetSize().ult(CR.getSetSize()))
00300         return *this;
00301       return CR;
00302     }
00303     if (CR.Lower.ult(Lower)) {
00304       if (CR.Upper.ule(Lower))
00305         return ConstantRange(getBitWidth(), false);
00306 
00307       return ConstantRange(Lower, CR.Upper);
00308     }
00309     return CR;
00310   }
00311 
00312   if (CR.Upper.ult(Upper)) {
00313     if (CR.Lower.ult(Upper)) {
00314       if (getSetSize().ult(CR.getSetSize()))
00315         return *this;
00316       return CR;
00317     }
00318 
00319     if (CR.Lower.ult(Lower))
00320       return ConstantRange(Lower, CR.Upper);
00321 
00322     return CR;
00323   }
00324   if (CR.Upper.ule(Lower)) {
00325     if (CR.Lower.ult(Lower))
00326       return *this;
00327 
00328     return ConstantRange(CR.Lower, Upper);
00329   }
00330   if (getSetSize().ult(CR.getSetSize()))
00331     return *this;
00332   return CR;
00333 }
00334 
00335 
00336 /// unionWith - Return the range that results from the union of this range with
00337 /// another range.  The resultant range is guaranteed to include the elements of
00338 /// both sets, but may contain more.  For example, [3, 9) union [12,15) is
00339 /// [3, 15), which includes 9, 10, and 11, which were not included in either
00340 /// set before.
00341 ///
00342 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
00343   assert(getBitWidth() == CR.getBitWidth() && 
00344          "ConstantRange types don't agree!");
00345 
00346   if (   isFullSet() || CR.isEmptySet()) return *this;
00347   if (CR.isFullSet() ||    isEmptySet()) return CR;
00348 
00349   if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
00350 
00351   if (!isWrappedSet() && !CR.isWrappedSet()) {
00352     if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
00353       // If the two ranges are disjoint, find the smaller gap and bridge it.
00354       APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
00355       if (d1.ult(d2))
00356         return ConstantRange(Lower, CR.Upper);
00357       return ConstantRange(CR.Lower, Upper);
00358     }
00359 
00360     APInt L = Lower, U = Upper;
00361     if (CR.Lower.ult(L))
00362       L = CR.Lower;
00363     if ((CR.Upper - 1).ugt(U - 1))
00364       U = CR.Upper;
00365 
00366     if (L == 0 && U == 0)
00367       return ConstantRange(getBitWidth());
00368 
00369     return ConstantRange(L, U);
00370   }
00371 
00372   if (!CR.isWrappedSet()) {
00373     // ------U   L-----  and  ------U   L----- : this
00374     //   L--U                            L--U  : CR
00375     if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
00376       return *this;
00377 
00378     // ------U   L----- : this
00379     //    L---------U   : CR
00380     if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
00381       return ConstantRange(getBitWidth());
00382 
00383     // ----U       L---- : this
00384     //       L---U       : CR
00385     //    <d1>  <d2>
00386     if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
00387       APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
00388       if (d1.ult(d2))
00389         return ConstantRange(Lower, CR.Upper);
00390       return ConstantRange(CR.Lower, Upper);
00391     }
00392 
00393     // ----U     L----- : this
00394     //        L----U    : CR
00395     if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
00396       return ConstantRange(CR.Lower, Upper);
00397 
00398     // ------U    L---- : this
00399     //    L-----U       : CR
00400     assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) &&
00401            "ConstantRange::unionWith missed a case with one range wrapped");
00402     return ConstantRange(Lower, CR.Upper);
00403   }
00404 
00405   // ------U    L----  and  ------U    L---- : this
00406   // -U  L-----------  and  ------------U  L : CR
00407   if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
00408     return ConstantRange(getBitWidth());
00409 
00410   APInt L = Lower, U = Upper;
00411   if (CR.Upper.ugt(U))
00412     U = CR.Upper;
00413   if (CR.Lower.ult(L))
00414     L = CR.Lower;
00415 
00416   return ConstantRange(L, U);
00417 }
00418 
00419 /// zeroExtend - Return a new range in the specified integer type, which must
00420 /// be strictly larger than the current type.  The returned range will
00421 /// correspond to the possible range of values as if the source range had been
00422 /// zero extended.
00423 ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
00424   if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
00425 
00426   unsigned SrcTySize = getBitWidth();
00427   assert(SrcTySize < DstTySize && "Not a value extension");
00428   if (isFullSet() || isWrappedSet()) {
00429     // Change into [0, 1 << src bit width)
00430     APInt LowerExt(DstTySize, 0);
00431     if (!Upper) // special case: [X, 0) -- not really wrapping around
00432       LowerExt = Lower.zext(DstTySize);
00433     return ConstantRange(LowerExt, APInt::getOneBitSet(DstTySize, SrcTySize));
00434   }
00435 
00436   return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
00437 }
00438 
00439 /// signExtend - Return a new range in the specified integer type, which must
00440 /// be strictly larger than the current type.  The returned range will
00441 /// correspond to the possible range of values as if the source range had been
00442 /// sign extended.
00443 ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
00444   if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
00445 
00446   unsigned SrcTySize = getBitWidth();
00447   assert(SrcTySize < DstTySize && "Not a value extension");
00448 
00449   // special case: [X, INT_MIN) -- not really wrapping around
00450   if (Upper.isMinSignedValue())
00451     return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize));
00452 
00453   if (isFullSet() || isSignWrappedSet()) {
00454     return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
00455                          APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
00456   }
00457 
00458   return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
00459 }
00460 
00461 /// truncate - Return a new range in the specified integer type, which must be
00462 /// strictly smaller than the current type.  The returned range will
00463 /// correspond to the possible range of values as if the source range had been
00464 /// truncated to the specified type.
00465 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
00466   assert(getBitWidth() > DstTySize && "Not a value truncation");
00467   if (isEmptySet())
00468     return ConstantRange(DstTySize, /*isFullSet=*/false);
00469   if (isFullSet())
00470     return ConstantRange(DstTySize, /*isFullSet=*/true);
00471 
00472   APInt MaxValue = APInt::getMaxValue(DstTySize).zext(getBitWidth());
00473   APInt MaxBitValue(getBitWidth(), 0);
00474   MaxBitValue.setBit(DstTySize);
00475 
00476   APInt LowerDiv(Lower), UpperDiv(Upper);
00477   ConstantRange Union(DstTySize, /*isFullSet=*/false);
00478 
00479   // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue]
00480   // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and
00481   // then we do the union with [MaxValue, Upper)
00482   if (isWrappedSet()) {
00483     // if Upper is greater than Max Value, it covers the whole truncated range.
00484     if (Upper.uge(MaxValue))
00485       return ConstantRange(DstTySize, /*isFullSet=*/true);
00486 
00487     Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
00488     UpperDiv = APInt::getMaxValue(getBitWidth());
00489 
00490     // Union covers the MaxValue case, so return if the remaining range is just
00491     // MaxValue.
00492     if (LowerDiv == UpperDiv)
00493       return Union;
00494   }
00495 
00496   // Chop off the most significant bits that are past the destination bitwidth.
00497   if (LowerDiv.uge(MaxValue)) {
00498     APInt Div(getBitWidth(), 0);
00499     APInt::udivrem(LowerDiv, MaxBitValue, Div, LowerDiv);
00500     UpperDiv = UpperDiv - MaxBitValue * Div;
00501   }
00502 
00503   if (UpperDiv.ule(MaxValue))
00504     return ConstantRange(LowerDiv.trunc(DstTySize),
00505                          UpperDiv.trunc(DstTySize)).unionWith(Union);
00506 
00507   // The truncated value wrapps around. Check if we can do better than fullset.
00508   APInt UpperModulo = UpperDiv - MaxBitValue;
00509   if (UpperModulo.ult(LowerDiv))
00510     return ConstantRange(LowerDiv.trunc(DstTySize),
00511                          UpperModulo.trunc(DstTySize)).unionWith(Union);
00512 
00513   return ConstantRange(DstTySize, /*isFullSet=*/true);
00514 }
00515 
00516 /// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
00517 /// value is zero extended, truncated, or left alone to make it that width.
00518 ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
00519   unsigned SrcTySize = getBitWidth();
00520   if (SrcTySize > DstTySize)
00521     return truncate(DstTySize);
00522   if (SrcTySize < DstTySize)
00523     return zeroExtend(DstTySize);
00524   return *this;
00525 }
00526 
00527 /// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
00528 /// value is sign extended, truncated, or left alone to make it that width.
00529 ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
00530   unsigned SrcTySize = getBitWidth();
00531   if (SrcTySize > DstTySize)
00532     return truncate(DstTySize);
00533   if (SrcTySize < DstTySize)
00534     return signExtend(DstTySize);
00535   return *this;
00536 }
00537 
00538 ConstantRange
00539 ConstantRange::add(const ConstantRange &Other) const {
00540   if (isEmptySet() || Other.isEmptySet())
00541     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
00542   if (isFullSet() || Other.isFullSet())
00543     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
00544 
00545   APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
00546   APInt NewLower = getLower() + Other.getLower();
00547   APInt NewUpper = getUpper() + Other.getUpper() - 1;
00548   if (NewLower == NewUpper)
00549     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
00550 
00551   ConstantRange X = ConstantRange(NewLower, NewUpper);
00552   if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
00553     // We've wrapped, therefore, full set.
00554     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
00555 
00556   return X;
00557 }
00558 
00559 ConstantRange
00560 ConstantRange::sub(const ConstantRange &Other) const {
00561   if (isEmptySet() || Other.isEmptySet())
00562     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
00563   if (isFullSet() || Other.isFullSet())
00564     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
00565 
00566   APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
00567   APInt NewLower = getLower() - Other.getUpper() + 1;
00568   APInt NewUpper = getUpper() - Other.getLower();
00569   if (NewLower == NewUpper)
00570     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
00571 
00572   ConstantRange X = ConstantRange(NewLower, NewUpper);
00573   if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
00574     // We've wrapped, therefore, full set.
00575     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
00576 
00577   return X;
00578 }
00579 
00580 ConstantRange
00581 ConstantRange::multiply(const ConstantRange &Other) const {
00582   // TODO: If either operand is a single element and the multiply is known to
00583   // be non-wrapping, round the result min and max value to the appropriate
00584   // multiple of that element. If wrapping is possible, at least adjust the
00585   // range according to the greatest power-of-two factor of the single element.
00586 
00587   if (isEmptySet() || Other.isEmptySet())
00588     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
00589 
00590   APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
00591   APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
00592   APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
00593   APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
00594 
00595   ConstantRange Result_zext = ConstantRange(this_min * Other_min,
00596                                             this_max * Other_max + 1);
00597   return Result_zext.truncate(getBitWidth());
00598 }
00599 
00600 ConstantRange
00601 ConstantRange::smax(const ConstantRange &Other) const {
00602   // X smax Y is: range(smax(X_smin, Y_smin),
00603   //                    smax(X_smax, Y_smax))
00604   if (isEmptySet() || Other.isEmptySet())
00605     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
00606   APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
00607   APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
00608   if (NewU == NewL)
00609     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
00610   return ConstantRange(NewL, NewU);
00611 }
00612 
00613 ConstantRange
00614 ConstantRange::umax(const ConstantRange &Other) const {
00615   // X umax Y is: range(umax(X_umin, Y_umin),
00616   //                    umax(X_umax, Y_umax))
00617   if (isEmptySet() || Other.isEmptySet())
00618     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
00619   APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
00620   APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
00621   if (NewU == NewL)
00622     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
00623   return ConstantRange(NewL, NewU);
00624 }
00625 
00626 ConstantRange
00627 ConstantRange::udiv(const ConstantRange &RHS) const {
00628   if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
00629     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
00630   if (RHS.isFullSet())
00631     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
00632 
00633   APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
00634 
00635   APInt RHS_umin = RHS.getUnsignedMin();
00636   if (RHS_umin == 0) {
00637     // We want the lowest value in RHS excluding zero. Usually that would be 1
00638     // except for a range in the form of [X, 1) in which case it would be X.
00639     if (RHS.getUpper() == 1)
00640       RHS_umin = RHS.getLower();
00641     else
00642       RHS_umin = APInt(getBitWidth(), 1);
00643   }
00644 
00645   APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
00646 
00647   // If the LHS is Full and the RHS is a wrapped interval containing 1 then
00648   // this could occur.
00649   if (Lower == Upper)
00650     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
00651 
00652   return ConstantRange(Lower, Upper);
00653 }
00654 
00655 ConstantRange
00656 ConstantRange::binaryAnd(const ConstantRange &Other) const {
00657   if (isEmptySet() || Other.isEmptySet())
00658     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
00659 
00660   // TODO: replace this with something less conservative
00661 
00662   APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax());
00663   if (umin.isAllOnesValue())
00664     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
00665   return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1);
00666 }
00667 
00668 ConstantRange
00669 ConstantRange::binaryOr(const ConstantRange &Other) const {
00670   if (isEmptySet() || Other.isEmptySet())
00671     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
00672 
00673   // TODO: replace this with something less conservative
00674 
00675   APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
00676   if (umax.isMinValue())
00677     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
00678   return ConstantRange(umax, APInt::getNullValue(getBitWidth()));
00679 }
00680 
00681 ConstantRange
00682 ConstantRange::shl(const ConstantRange &Other) const {
00683   if (isEmptySet() || Other.isEmptySet())
00684     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
00685 
00686   APInt min = getUnsignedMin().shl(Other.getUnsignedMin());
00687   APInt max = getUnsignedMax().shl(Other.getUnsignedMax());
00688 
00689   // there's no overflow!
00690   APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
00691   if (Zeros.ugt(Other.getUnsignedMax()))
00692     return ConstantRange(min, max + 1);
00693 
00694   // FIXME: implement the other tricky cases
00695   return ConstantRange(getBitWidth(), /*isFullSet=*/true);
00696 }
00697 
00698 ConstantRange
00699 ConstantRange::lshr(const ConstantRange &Other) const {
00700   if (isEmptySet() || Other.isEmptySet())
00701     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
00702   
00703   APInt max = getUnsignedMax().lshr(Other.getUnsignedMin());
00704   APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
00705   if (min == max + 1)
00706     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
00707 
00708   return ConstantRange(min, max + 1);
00709 }
00710 
00711 ConstantRange ConstantRange::inverse() const {
00712   if (isFullSet())
00713     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
00714   if (isEmptySet())
00715     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
00716   return ConstantRange(Upper, Lower);
00717 }
00718 
00719 /// print - Print out the bounds to a stream...
00720 ///
00721 void ConstantRange::print(raw_ostream &OS) const {
00722   if (isFullSet())
00723     OS << "full-set";
00724   else if (isEmptySet())
00725     OS << "empty-set";
00726   else
00727     OS << "[" << Lower << "," << Upper << ")";
00728 }
00729 
00730 /// dump - Allow printing from a debugger easily...
00731 ///
00732 void ConstantRange::dump() const {
00733   print(dbgs());
00734 }