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