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