Home | History | Annotate | Download | only in Support
      1 //===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // Represent a range of possible values that may occur when the program is run
     11 // for an integral value.  This keeps track of a lower and upper bound for the
     12 // constant, which MAY wrap around the end of the numeric range.  To do this, it
     13 // keeps track of a [lower, upper) bound, which specifies an interval just like
     14 // STL iterators.  When used with boolean values, the following are important
     15 // ranges (other integral ranges use min/max values for special range values):
     16 //
     17 //  [F, F) = {}     = Empty set
     18 //  [T, F) = {T}
     19 //  [F, T) = {F}
     20 //  [T, T) = {F, T} = Full set
     21 //
     22 //===----------------------------------------------------------------------===//
     23 
     24 #include "llvm/InstrTypes.h"
     25 #include "llvm/Support/ConstantRange.h"
     26 #include "llvm/Support/Debug.h"
     27 #include "llvm/Support/raw_ostream.h"
     28 using namespace llvm;
     29 
     30 /// Initialize a full (the default) or empty set for the specified type.
     31 ///
     32 ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) {
     33   if (Full)
     34     Lower = Upper = APInt::getMaxValue(BitWidth);
     35   else
     36     Lower = Upper = APInt::getMinValue(BitWidth);
     37 }
     38 
     39 /// Initialize a range to hold the single specified value.
     40 ///
     41 ConstantRange::ConstantRange(const APInt &V) : Lower(V), Upper(V + 1) {}
     42 
     43 ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
     44   Lower(L), Upper(U) {
     45   assert(L.getBitWidth() == U.getBitWidth() &&
     46          "ConstantRange with unequal bit widths");
     47   assert((L != U || (L.isMaxValue() || L.isMinValue())) &&
     48          "Lower == Upper, but they aren't min or max value!");
     49 }
     50 
     51 ConstantRange ConstantRange::makeICmpRegion(unsigned Pred,
     52                                             const ConstantRange &CR) {
     53   if (CR.isEmptySet())
     54     return CR;
     55 
     56   uint32_t W = CR.getBitWidth();
     57   switch (Pred) {
     58     default: assert(0 && "Invalid ICmp predicate to makeICmpRegion()");
     59     case CmpInst::ICMP_EQ:
     60       return CR;
     61     case CmpInst::ICMP_NE:
     62       if (CR.isSingleElement())
     63         return ConstantRange(CR.getUpper(), CR.getLower());
     64       return ConstantRange(W);
     65     case CmpInst::ICMP_ULT: {
     66       APInt UMax(CR.getUnsignedMax());
     67       if (UMax.isMinValue())
     68         return ConstantRange(W, /* empty */ false);
     69       return ConstantRange(APInt::getMinValue(W), UMax);
     70     }
     71     case CmpInst::ICMP_SLT: {
     72       APInt SMax(CR.getSignedMax());
     73       if (SMax.isMinSignedValue())
     74         return ConstantRange(W, /* empty */ false);
     75       return ConstantRange(APInt::getSignedMinValue(W), SMax);
     76     }
     77     case CmpInst::ICMP_ULE: {
     78       APInt UMax(CR.getUnsignedMax());
     79       if (UMax.isMaxValue())
     80         return ConstantRange(W);
     81       return ConstantRange(APInt::getMinValue(W), UMax + 1);
     82     }
     83     case CmpInst::ICMP_SLE: {
     84       APInt SMax(CR.getSignedMax());
     85       if (SMax.isMaxSignedValue())
     86         return ConstantRange(W);
     87       return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
     88     }
     89     case CmpInst::ICMP_UGT: {
     90       APInt UMin(CR.getUnsignedMin());
     91       if (UMin.isMaxValue())
     92         return ConstantRange(W, /* empty */ false);
     93       return ConstantRange(UMin + 1, APInt::getNullValue(W));
     94     }
     95     case CmpInst::ICMP_SGT: {
     96       APInt SMin(CR.getSignedMin());
     97       if (SMin.isMaxSignedValue())
     98         return ConstantRange(W, /* empty */ false);
     99       return ConstantRange(SMin + 1, APInt::getSignedMinValue(W));
    100     }
    101     case CmpInst::ICMP_UGE: {
    102       APInt UMin(CR.getUnsignedMin());
    103       if (UMin.isMinValue())
    104         return ConstantRange(W);
    105       return ConstantRange(UMin, APInt::getNullValue(W));
    106     }
    107     case CmpInst::ICMP_SGE: {
    108       APInt SMin(CR.getSignedMin());
    109       if (SMin.isMinSignedValue())
    110         return ConstantRange(W);
    111       return ConstantRange(SMin, APInt::getSignedMinValue(W));
    112     }
    113   }
    114 }
    115 
    116 /// isFullSet - Return true if this set contains all of the elements possible
    117 /// for this data-type
    118 bool ConstantRange::isFullSet() const {
    119   return Lower == Upper && Lower.isMaxValue();
    120 }
    121 
    122 /// isEmptySet - Return true if this set contains no members.
    123 ///
    124 bool ConstantRange::isEmptySet() const {
    125   return Lower == Upper && Lower.isMinValue();
    126 }
    127 
    128 /// isWrappedSet - Return true if this set wraps around the top of the range,
    129 /// for example: [100, 8)
    130 ///
    131 bool ConstantRange::isWrappedSet() const {
    132   return Lower.ugt(Upper);
    133 }
    134 
    135 /// isSignWrappedSet - Return true if this set wraps around the INT_MIN of
    136 /// its bitwidth, for example: i8 [120, 140).
    137 ///
    138 bool ConstantRange::isSignWrappedSet() const {
    139   return contains(APInt::getSignedMaxValue(getBitWidth())) &&
    140          contains(APInt::getSignedMinValue(getBitWidth()));
    141 }
    142 
    143 /// getSetSize - Return the number of elements in this set.
    144 ///
    145 APInt ConstantRange::getSetSize() const {
    146   if (isEmptySet())
    147     return APInt(getBitWidth(), 0);
    148   if (getBitWidth() == 1) {
    149     if (Lower != Upper)  // One of T or F in the set...
    150       return APInt(2, 1);
    151     return APInt(2, 2);      // Must be full set...
    152   }
    153 
    154   // Simply subtract the bounds...
    155   return Upper - Lower;
    156 }
    157 
    158 /// getUnsignedMax - Return the largest unsigned value contained in the
    159 /// ConstantRange.
    160 ///
    161 APInt ConstantRange::getUnsignedMax() const {
    162   if (isFullSet() || isWrappedSet())
    163     return APInt::getMaxValue(getBitWidth());
    164   else
    165     return getUpper() - 1;
    166 }
    167 
    168 /// getUnsignedMin - Return the smallest unsigned value contained in the
    169 /// ConstantRange.
    170 ///
    171 APInt ConstantRange::getUnsignedMin() const {
    172   if (isFullSet() || (isWrappedSet() && getUpper() != 0))
    173     return APInt::getMinValue(getBitWidth());
    174   else
    175     return getLower();
    176 }
    177 
    178 /// getSignedMax - Return the largest signed value contained in the
    179 /// ConstantRange.
    180 ///
    181 APInt ConstantRange::getSignedMax() const {
    182   APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
    183   if (!isWrappedSet()) {
    184     if (getLower().sle(getUpper() - 1))
    185       return getUpper() - 1;
    186     else
    187       return SignedMax;
    188   } else {
    189     if (getLower().isNegative() == getUpper().isNegative())
    190       return SignedMax;
    191     else
    192       return getUpper() - 1;
    193   }
    194 }
    195 
    196 /// getSignedMin - Return the smallest signed value contained in the
    197 /// ConstantRange.
    198 ///
    199 APInt ConstantRange::getSignedMin() const {
    200   APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
    201   if (!isWrappedSet()) {
    202     if (getLower().sle(getUpper() - 1))
    203       return getLower();
    204     else
    205       return SignedMin;
    206   } else {
    207     if ((getUpper() - 1).slt(getLower())) {
    208       if (getUpper() != SignedMin)
    209         return SignedMin;
    210       else
    211         return getLower();
    212     } else {
    213       return getLower();
    214     }
    215   }
    216 }
    217 
    218 /// contains - Return true if the specified value is in the set.
    219 ///
    220 bool ConstantRange::contains(const APInt &V) const {
    221   if (Lower == Upper)
    222     return isFullSet();
    223 
    224   if (!isWrappedSet())
    225     return Lower.ule(V) && V.ult(Upper);
    226   else
    227     return Lower.ule(V) || V.ult(Upper);
    228 }
    229 
    230 /// contains - Return true if the argument is a subset of this range.
    231 /// Two equal sets contain each other. The empty set contained by all other
    232 /// sets.
    233 ///
    234 bool ConstantRange::contains(const ConstantRange &Other) const {
    235   if (isFullSet() || Other.isEmptySet()) return true;
    236   if (isEmptySet() || Other.isFullSet()) return false;
    237 
    238   if (!isWrappedSet()) {
    239     if (Other.isWrappedSet())
    240       return false;
    241 
    242     return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
    243   }
    244 
    245   if (!Other.isWrappedSet())
    246     return Other.getUpper().ule(Upper) ||
    247            Lower.ule(Other.getLower());
    248 
    249   return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
    250 }
    251 
    252 /// subtract - Subtract the specified constant from the endpoints of this
    253 /// constant range.
    254 ConstantRange ConstantRange::subtract(const APInt &Val) const {
    255   assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
    256   // If the set is empty or full, don't modify the endpoints.
    257   if (Lower == Upper)
    258     return *this;
    259   return ConstantRange(Lower - Val, Upper - Val);
    260 }
    261 
    262 /// intersectWith - Return the range that results from the intersection of this
    263 /// range with another range.  The resultant range is guaranteed to include all
    264 /// elements contained in both input ranges, and to have the smallest possible
    265 /// set size that does so.  Because there may be two intersections with the
    266 /// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
    267 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
    268   assert(getBitWidth() == CR.getBitWidth() &&
    269          "ConstantRange types don't agree!");
    270 
    271   // Handle common cases.
    272   if (   isEmptySet() || CR.isFullSet()) return *this;
    273   if (CR.isEmptySet() ||    isFullSet()) return CR;
    274 
    275   if (!isWrappedSet() && CR.isWrappedSet())
    276     return CR.intersectWith(*this);
    277 
    278   if (!isWrappedSet() && !CR.isWrappedSet()) {
    279     if (Lower.ult(CR.Lower)) {
    280       if (Upper.ule(CR.Lower))
    281         return ConstantRange(getBitWidth(), false);
    282 
    283       if (Upper.ult(CR.Upper))
    284         return ConstantRange(CR.Lower, Upper);
    285 
    286       return CR;
    287     } else {
    288       if (Upper.ult(CR.Upper))
    289         return *this;
    290 
    291       if (Lower.ult(CR.Upper))
    292         return ConstantRange(Lower, CR.Upper);
    293 
    294       return ConstantRange(getBitWidth(), false);
    295     }
    296   }
    297 
    298   if (isWrappedSet() && !CR.isWrappedSet()) {
    299     if (CR.Lower.ult(Upper)) {
    300       if (CR.Upper.ult(Upper))
    301         return CR;
    302 
    303       if (CR.Upper.ult(Lower))
    304         return ConstantRange(CR.Lower, Upper);
    305 
    306       if (getSetSize().ult(CR.getSetSize()))
    307         return *this;
    308       else
    309         return CR;
    310     } else if (CR.Lower.ult(Lower)) {
    311       if (CR.Upper.ule(Lower))
    312         return ConstantRange(getBitWidth(), false);
    313 
    314       return ConstantRange(Lower, CR.Upper);
    315     }
    316     return CR;
    317   }
    318 
    319   if (CR.Upper.ult(Upper)) {
    320     if (CR.Lower.ult(Upper)) {
    321       if (getSetSize().ult(CR.getSetSize()))
    322         return *this;
    323       else
    324         return CR;
    325     }
    326 
    327     if (CR.Lower.ult(Lower))
    328       return ConstantRange(Lower, CR.Upper);
    329 
    330     return CR;
    331   } else if (CR.Upper.ult(Lower)) {
    332     if (CR.Lower.ult(Lower))
    333       return *this;
    334 
    335     return ConstantRange(CR.Lower, Upper);
    336   }
    337   if (getSetSize().ult(CR.getSetSize()))
    338     return *this;
    339   else
    340     return CR;
    341 }
    342 
    343 
    344 /// unionWith - Return the range that results from the union of this range with
    345 /// another range.  The resultant range is guaranteed to include the elements of
    346 /// both sets, but may contain more.  For example, [3, 9) union [12,15) is
    347 /// [3, 15), which includes 9, 10, and 11, which were not included in either
    348 /// set before.
    349 ///
    350 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
    351   assert(getBitWidth() == CR.getBitWidth() &&
    352          "ConstantRange types don't agree!");
    353 
    354   if (   isFullSet() || CR.isEmptySet()) return *this;
    355   if (CR.isFullSet() ||    isEmptySet()) return CR;
    356 
    357   if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
    358 
    359   if (!isWrappedSet() && !CR.isWrappedSet()) {
    360     if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
    361       // If the two ranges are disjoint, find the smaller gap and bridge it.
    362       APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
    363       if (d1.ult(d2))
    364         return ConstantRange(Lower, CR.Upper);
    365       else
    366         return ConstantRange(CR.Lower, Upper);
    367     }
    368 
    369     APInt L = Lower, U = Upper;
    370     if (CR.Lower.ult(L))
    371       L = CR.Lower;
    372     if ((CR.Upper - 1).ugt(U - 1))
    373       U = CR.Upper;
    374 
    375     if (L == 0 && U == 0)
    376       return ConstantRange(getBitWidth());
    377 
    378     return ConstantRange(L, U);
    379   }
    380 
    381   if (!CR.isWrappedSet()) {
    382     // ------U   L-----  and  ------U   L----- : this
    383     //   L--U                            L--U  : CR
    384     if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
    385       return *this;
    386 
    387     // ------U   L----- : this
    388     //    L---------U   : CR
    389     if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
    390       return ConstantRange(getBitWidth());
    391 
    392     // ----U       L---- : this
    393     //       L---U       : CR
    394     //    <d1>  <d2>
    395     if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
    396       APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
    397       if (d1.ult(d2))
    398         return ConstantRange(Lower, CR.Upper);
    399       else
    400         return ConstantRange(CR.Lower, Upper);
    401     }
    402 
    403     // ----U     L----- : this
    404     //        L----U    : CR
    405     if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
    406       return ConstantRange(CR.Lower, Upper);
    407 
    408     // ------U    L---- : this
    409     //    L-----U       : CR
    410     if (CR.Lower.ult(Upper) && CR.Upper.ult(Lower))
    411       return ConstantRange(Lower, CR.Upper);
    412   }
    413 
    414   assert(isWrappedSet() && CR.isWrappedSet() &&
    415          "ConstantRange::unionWith missed wrapped union unwrapped case");
    416 
    417   // ------U    L----  and  ------U    L---- : this
    418   // -U  L-----------  and  ------------U  L : CR
    419   if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
    420     return ConstantRange(getBitWidth());
    421 
    422   APInt L = Lower, U = Upper;
    423   if (CR.Upper.ugt(U))
    424     U = CR.Upper;
    425   if (CR.Lower.ult(L))
    426     L = CR.Lower;
    427 
    428   return ConstantRange(L, U);
    429 }
    430 
    431 /// zeroExtend - Return a new range in the specified integer type, which must
    432 /// be strictly larger than the current type.  The returned range will
    433 /// correspond to the possible range of values as if the source range had been
    434 /// zero extended.
    435 ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
    436   if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
    437 
    438   unsigned SrcTySize = getBitWidth();
    439   assert(SrcTySize < DstTySize && "Not a value extension");
    440   if (isFullSet() || isWrappedSet())
    441     // Change into [0, 1 << src bit width)
    442     return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
    443 
    444   return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
    445 }
    446 
    447 /// signExtend - Return a new range in the specified integer type, which must
    448 /// be strictly larger than the current type.  The returned range will
    449 /// correspond to the possible range of values as if the source range had been
    450 /// sign extended.
    451 ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
    452   if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
    453 
    454   unsigned SrcTySize = getBitWidth();
    455   assert(SrcTySize < DstTySize && "Not a value extension");
    456   if (isFullSet() || isSignWrappedSet()) {
    457     return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
    458                          APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
    459   }
    460 
    461   return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
    462 }
    463 
    464 /// truncate - Return a new range in the specified integer type, which must be
    465 /// strictly smaller than the current type.  The returned range will
    466 /// correspond to the possible range of values as if the source range had been
    467 /// truncated to the specified type.
    468 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
    469   unsigned SrcTySize = getBitWidth();
    470   assert(SrcTySize > DstTySize && "Not a value truncation");
    471   APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
    472   if (isFullSet() || getSetSize().ugt(Size))
    473     return ConstantRange(DstTySize, /*isFullSet=*/true);
    474 
    475   return ConstantRange(Lower.trunc(DstTySize), Upper.trunc(DstTySize));
    476 }
    477 
    478 /// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
    479 /// value is zero extended, truncated, or left alone to make it that width.
    480 ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
    481   unsigned SrcTySize = getBitWidth();
    482   if (SrcTySize > DstTySize)
    483     return truncate(DstTySize);
    484   else if (SrcTySize < DstTySize)
    485     return zeroExtend(DstTySize);
    486   else
    487     return *this;
    488 }
    489 
    490 /// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
    491 /// value is sign extended, truncated, or left alone to make it that width.
    492 ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
    493   unsigned SrcTySize = getBitWidth();
    494   if (SrcTySize > DstTySize)
    495     return truncate(DstTySize);
    496   else if (SrcTySize < DstTySize)
    497     return signExtend(DstTySize);
    498   else
    499     return *this;
    500 }
    501 
    502 ConstantRange
    503 ConstantRange::add(const ConstantRange &Other) const {
    504   if (isEmptySet() || Other.isEmptySet())
    505     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
    506   if (isFullSet() || Other.isFullSet())
    507     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
    508 
    509   APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
    510   APInt NewLower = getLower() + Other.getLower();
    511   APInt NewUpper = getUpper() + Other.getUpper() - 1;
    512   if (NewLower == NewUpper)
    513     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
    514 
    515   ConstantRange X = ConstantRange(NewLower, NewUpper);
    516   if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
    517     // We've wrapped, therefore, full set.
    518     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
    519 
    520   return X;
    521 }
    522 
    523 ConstantRange
    524 ConstantRange::sub(const ConstantRange &Other) const {
    525   if (isEmptySet() || Other.isEmptySet())
    526     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
    527   if (isFullSet() || Other.isFullSet())
    528     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
    529 
    530   APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
    531   APInt NewLower = getLower() - Other.getUpper() + 1;
    532   APInt NewUpper = getUpper() - Other.getLower();
    533   if (NewLower == NewUpper)
    534     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
    535 
    536   ConstantRange X = ConstantRange(NewLower, NewUpper);
    537   if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
    538     // We've wrapped, therefore, full set.
    539     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
    540 
    541   return X;
    542 }
    543 
    544 ConstantRange
    545 ConstantRange::multiply(const ConstantRange &Other) const {
    546   // TODO: If either operand is a single element and the multiply is known to
    547   // be non-wrapping, round the result min and max value to the appropriate
    548   // multiple of that element. If wrapping is possible, at least adjust the
    549   // range according to the greatest power-of-two factor of the single element.
    550 
    551   if (isEmptySet() || Other.isEmptySet())
    552     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
    553   if (isFullSet() || Other.isFullSet())
    554     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
    555 
    556   APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
    557   APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
    558   APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
    559   APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
    560 
    561   ConstantRange Result_zext = ConstantRange(this_min * Other_min,
    562                                             this_max * Other_max + 1);
    563   return Result_zext.truncate(getBitWidth());
    564 }
    565 
    566 ConstantRange
    567 ConstantRange::smax(const ConstantRange &Other) const {
    568   // X smax Y is: range(smax(X_smin, Y_smin),
    569   //                    smax(X_smax, Y_smax))
    570   if (isEmptySet() || Other.isEmptySet())
    571     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
    572   APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
    573   APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
    574   if (NewU == NewL)
    575     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
    576   return ConstantRange(NewL, NewU);
    577 }
    578 
    579 ConstantRange
    580 ConstantRange::umax(const ConstantRange &Other) const {
    581   // X umax Y is: range(umax(X_umin, Y_umin),
    582   //                    umax(X_umax, Y_umax))
    583   if (isEmptySet() || Other.isEmptySet())
    584     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
    585   APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
    586   APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
    587   if (NewU == NewL)
    588     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
    589   return ConstantRange(NewL, NewU);
    590 }
    591 
    592 ConstantRange
    593 ConstantRange::udiv(const ConstantRange &RHS) const {
    594   if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
    595     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
    596   if (RHS.isFullSet())
    597     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
    598 
    599   APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
    600 
    601   APInt RHS_umin = RHS.getUnsignedMin();
    602   if (RHS_umin == 0) {
    603     // We want the lowest value in RHS excluding zero. Usually that would be 1
    604     // except for a range in the form of [X, 1) in which case it would be X.
    605     if (RHS.getUpper() == 1)
    606       RHS_umin = RHS.getLower();
    607     else
    608       RHS_umin = APInt(getBitWidth(), 1);
    609   }
    610 
    611   APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
    612 
    613   // If the LHS is Full and the RHS is a wrapped interval containing 1 then
    614   // this could occur.
    615   if (Lower == Upper)
    616     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
    617 
    618   return ConstantRange(Lower, Upper);
    619 }
    620 
    621 ConstantRange
    622 ConstantRange::binaryAnd(const ConstantRange &Other) const {
    623   if (isEmptySet() || Other.isEmptySet())
    624     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
    625 
    626   // TODO: replace this with something less conservative
    627 
    628   APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax());
    629   if (umin.isAllOnesValue())
    630     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
    631   return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1);
    632 }
    633 
    634 ConstantRange
    635 ConstantRange::binaryOr(const ConstantRange &Other) const {
    636   if (isEmptySet() || Other.isEmptySet())
    637     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
    638 
    639   // TODO: replace this with something less conservative
    640 
    641   APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
    642   if (umax.isMinValue())
    643     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
    644   return ConstantRange(umax, APInt::getNullValue(getBitWidth()));
    645 }
    646 
    647 ConstantRange
    648 ConstantRange::shl(const ConstantRange &Other) const {
    649   if (isEmptySet() || Other.isEmptySet())
    650     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
    651 
    652   APInt min = getUnsignedMin().shl(Other.getUnsignedMin());
    653   APInt max = getUnsignedMax().shl(Other.getUnsignedMax());
    654 
    655   // there's no overflow!
    656   APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
    657   if (Zeros.ugt(Other.getUnsignedMax()))
    658     return ConstantRange(min, max + 1);
    659 
    660   // FIXME: implement the other tricky cases
    661   return ConstantRange(getBitWidth(), /*isFullSet=*/true);
    662 }
    663 
    664 ConstantRange
    665 ConstantRange::lshr(const ConstantRange &Other) const {
    666   if (isEmptySet() || Other.isEmptySet())
    667     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
    668 
    669   APInt max = getUnsignedMax().lshr(Other.getUnsignedMin());
    670   APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
    671   if (min == max + 1)
    672     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
    673 
    674   return ConstantRange(min, max + 1);
    675 }
    676 
    677 ConstantRange ConstantRange::inverse() const {
    678   if (isFullSet()) {
    679     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
    680   } else if (isEmptySet()) {
    681     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
    682   }
    683   return ConstantRange(Upper, Lower);
    684 }
    685 
    686 /// print - Print out the bounds to a stream...
    687 ///
    688 void ConstantRange::print(raw_ostream &OS) const {
    689   if (isFullSet())
    690     OS << "full-set";
    691   else if (isEmptySet())
    692     OS << "empty-set";
    693   else
    694     OS << "[" << Lower << "," << Upper << ")";
    695 }
    696 
    697 /// dump - Allow printing from a debugger easily...
    698 ///
    699 void ConstantRange::dump() const {
    700   print(dbgs());
    701 }
    702