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