221ca948ca5ecf18325dc7bdafc03be62cee15be
[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/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: llvm_unreachable("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()+1, 0);
148
149   if (isFullSet())
150     return APInt::getMaxValue(getBitWidth()).zext(getBitWidth()+1) + 1;
151
152   if (isWrappedSet()) {
153     APInt Result = Upper + (APInt::getMaxValue(getBitWidth()) - Lower + 1);
154     return Result.zext(getBitWidth()+1);
155   }
156
157   return (Upper - Lower).zext(getBitWidth()+1);
158 }
159
160 /// getUnsignedMax - Return the largest unsigned value contained in the
161 /// ConstantRange.
162 ///
163 APInt ConstantRange::getUnsignedMax() const {
164   if (isFullSet() || isWrappedSet())
165     return APInt::getMaxValue(getBitWidth());
166   return getUpper() - 1;
167 }
168
169 /// getUnsignedMin - Return the smallest unsigned value contained in the
170 /// ConstantRange.
171 ///
172 APInt ConstantRange::getUnsignedMin() const {
173   if (isFullSet() || (isWrappedSet() && getUpper() != 0))
174     return APInt::getMinValue(getBitWidth());
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     return SignedMax;
187   }
188   if (getLower().isNegative() == getUpper().isNegative())
189     return SignedMax;
190   return getUpper() - 1;
191 }
192
193 /// getSignedMin - Return the smallest signed value contained in the
194 /// ConstantRange.
195 ///
196 APInt ConstantRange::getSignedMin() const {
197   APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
198   if (!isWrappedSet()) {
199     if (getLower().sle(getUpper() - 1))
200       return getLower();
201     return SignedMin;
202   }
203   if ((getUpper() - 1).slt(getLower())) {
204     if (getUpper() != SignedMin)
205       return SignedMin;
206   }
207   return getLower();
208 }
209
210 /// contains - Return true if the specified value is in the set.
211 ///
212 bool ConstantRange::contains(const APInt &V) const {
213   if (Lower == Upper)
214     return isFullSet();
215
216   if (!isWrappedSet())
217     return Lower.ule(V) && V.ult(Upper);
218   return Lower.ule(V) || V.ult(Upper);
219 }
220
221 /// contains - Return true if the argument is a subset of this range.
222 /// Two equal sets contain each other. The empty set contained by all other
223 /// sets.
224 ///
225 bool ConstantRange::contains(const ConstantRange &Other) const {
226   if (isFullSet() || Other.isEmptySet()) return true;
227   if (isEmptySet() || Other.isFullSet()) return false;
228
229   if (!isWrappedSet()) {
230     if (Other.isWrappedSet())
231       return false;
232
233     return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
234   }
235
236   if (!Other.isWrappedSet())
237     return Other.getUpper().ule(Upper) ||
238            Lower.ule(Other.getLower());
239
240   return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
241 }
242
243 /// subtract - Subtract the specified constant from the endpoints of this
244 /// constant range.
245 ConstantRange ConstantRange::subtract(const APInt &Val) const {
246   assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
247   // If the set is empty or full, don't modify the endpoints.
248   if (Lower == Upper) 
249     return *this;
250   return ConstantRange(Lower - Val, Upper - Val);
251 }
252
253 /// \brief Subtract the specified range from this range (aka relative complement
254 /// of the sets).
255 ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
256   return intersectWith(CR.inverse());
257 }
258
259 /// intersectWith - Return the range that results from the intersection of this
260 /// range with another range.  The resultant range is guaranteed to include all
261 /// elements contained in both input ranges, and to have the smallest possible
262 /// set size that does so.  Because there may be two intersections with the
263 /// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
264 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
265   assert(getBitWidth() == CR.getBitWidth() && 
266          "ConstantRange types don't agree!");
267
268   // Handle common cases.
269   if (   isEmptySet() || CR.isFullSet()) return *this;
270   if (CR.isEmptySet() ||    isFullSet()) return CR;
271
272   if (!isWrappedSet() && CR.isWrappedSet())
273     return CR.intersectWith(*this);
274
275   if (!isWrappedSet() && !CR.isWrappedSet()) {
276     if (Lower.ult(CR.Lower)) {
277       if (Upper.ule(CR.Lower))
278         return ConstantRange(getBitWidth(), false);
279
280       if (Upper.ult(CR.Upper))
281         return ConstantRange(CR.Lower, Upper);
282
283       return CR;
284     }
285     if (Upper.ult(CR.Upper))
286       return *this;
287
288     if (Lower.ult(CR.Upper))
289       return ConstantRange(Lower, CR.Upper);
290
291     return ConstantRange(getBitWidth(), false);
292   }
293
294   if (isWrappedSet() && !CR.isWrappedSet()) {
295     if (CR.Lower.ult(Upper)) {
296       if (CR.Upper.ult(Upper))
297         return CR;
298
299       if (CR.Upper.ule(Lower))
300         return ConstantRange(CR.Lower, Upper);
301
302       if (getSetSize().ult(CR.getSetSize()))
303         return *this;
304       return CR;
305     }
306     if (CR.Lower.ult(Lower)) {
307       if (CR.Upper.ule(Lower))
308         return ConstantRange(getBitWidth(), false);
309
310       return ConstantRange(Lower, CR.Upper);
311     }
312     return CR;
313   }
314
315   if (CR.Upper.ult(Upper)) {
316     if (CR.Lower.ult(Upper)) {
317       if (getSetSize().ult(CR.getSetSize()))
318         return *this;
319       return CR;
320     }
321
322     if (CR.Lower.ult(Lower))
323       return ConstantRange(Lower, CR.Upper);
324
325     return CR;
326   }
327   if (CR.Upper.ule(Lower)) {
328     if (CR.Lower.ult(Lower))
329       return *this;
330
331     return ConstantRange(CR.Lower, Upper);
332   }
333   if (getSetSize().ult(CR.getSetSize()))
334     return *this;
335   return CR;
336 }
337
338
339 /// unionWith - Return the range that results from the union of this range with
340 /// another range.  The resultant range is guaranteed to include the elements of
341 /// both sets, but may contain more.  For example, [3, 9) union [12,15) is
342 /// [3, 15), which includes 9, 10, and 11, which were not included in either
343 /// set before.
344 ///
345 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
346   assert(getBitWidth() == CR.getBitWidth() && 
347          "ConstantRange types don't agree!");
348
349   if (   isFullSet() || CR.isEmptySet()) return *this;
350   if (CR.isFullSet() ||    isEmptySet()) return CR;
351
352   if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
353
354   if (!isWrappedSet() && !CR.isWrappedSet()) {
355     if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
356       // If the two ranges are disjoint, find the smaller gap and bridge it.
357       APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
358       if (d1.ult(d2))
359         return ConstantRange(Lower, CR.Upper);
360       return ConstantRange(CR.Lower, Upper);
361     }
362
363     APInt L = Lower, U = Upper;
364     if (CR.Lower.ult(L))
365       L = CR.Lower;
366     if ((CR.Upper - 1).ugt(U - 1))
367       U = CR.Upper;
368
369     if (L == 0 && U == 0)
370       return ConstantRange(getBitWidth());
371
372     return ConstantRange(L, U);
373   }
374
375   if (!CR.isWrappedSet()) {
376     // ------U   L-----  and  ------U   L----- : this
377     //   L--U                            L--U  : CR
378     if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
379       return *this;
380
381     // ------U   L----- : this
382     //    L---------U   : CR
383     if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
384       return ConstantRange(getBitWidth());
385
386     // ----U       L---- : this
387     //       L---U       : CR
388     //    <d1>  <d2>
389     if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
390       APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
391       if (d1.ult(d2))
392         return ConstantRange(Lower, CR.Upper);
393       return ConstantRange(CR.Lower, Upper);
394     }
395
396     // ----U     L----- : this
397     //        L----U    : CR
398     if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
399       return ConstantRange(CR.Lower, Upper);
400
401     // ------U    L---- : this
402     //    L-----U       : CR
403     assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) &&
404            "ConstantRange::unionWith missed a case with one range wrapped");
405     return ConstantRange(Lower, CR.Upper);
406   }
407
408   // ------U    L----  and  ------U    L---- : this
409   // -U  L-----------  and  ------------U  L : CR
410   if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
411     return ConstantRange(getBitWidth());
412
413   APInt L = Lower, U = Upper;
414   if (CR.Upper.ugt(U))
415     U = CR.Upper;
416   if (CR.Lower.ult(L))
417     L = CR.Lower;
418
419   return ConstantRange(L, U);
420 }
421
422 /// zeroExtend - Return a new range in the specified integer type, which must
423 /// be strictly larger than the current type.  The returned range will
424 /// correspond to the possible range of values as if the source range had been
425 /// zero extended.
426 ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
427   if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
428
429   unsigned SrcTySize = getBitWidth();
430   assert(SrcTySize < DstTySize && "Not a value extension");
431   if (isFullSet() || isWrappedSet())
432     // Change into [0, 1 << src bit width)
433     return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
434
435   return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
436 }
437
438 /// signExtend - Return a new range in the specified integer type, which must
439 /// be strictly larger than the current type.  The returned range will
440 /// correspond to the possible range of values as if the source range had been
441 /// sign extended.
442 ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
443   if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
444
445   unsigned SrcTySize = getBitWidth();
446   assert(SrcTySize < DstTySize && "Not a value extension");
447   if (isFullSet() || isSignWrappedSet()) {
448     return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
449                          APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
450   }
451
452   return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
453 }
454
455 /// truncate - Return a new range in the specified integer type, which must be
456 /// strictly smaller than the current type.  The returned range will
457 /// correspond to the possible range of values as if the source range had been
458 /// truncated to the specified type.
459 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
460   assert(getBitWidth() > DstTySize && "Not a value truncation");
461   if (isFullSet() || getSetSize().getActiveBits() > DstTySize)
462     return ConstantRange(DstTySize, /*isFullSet=*/true);
463
464   return ConstantRange(Lower.trunc(DstTySize), Upper.trunc(DstTySize));
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   if (SrcTySize < DstTySize)
474     return zeroExtend(DstTySize);
475   return *this;
476 }
477
478 /// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
479 /// value is sign extended, truncated, or left alone to make it that width.
480 ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
481   unsigned SrcTySize = getBitWidth();
482   if (SrcTySize > DstTySize)
483     return truncate(DstTySize);
484   if (SrcTySize < DstTySize)
485     return signExtend(DstTySize);
486   return *this;
487 }
488
489 ConstantRange
490 ConstantRange::add(const ConstantRange &Other) const {
491   if (isEmptySet() || Other.isEmptySet())
492     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
493   if (isFullSet() || Other.isFullSet())
494     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
495
496   APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
497   APInt NewLower = getLower() + Other.getLower();
498   APInt NewUpper = getUpper() + Other.getUpper() - 1;
499   if (NewLower == NewUpper)
500     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
501
502   ConstantRange X = ConstantRange(NewLower, NewUpper);
503   if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
504     // We've wrapped, therefore, full set.
505     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
506
507   return X;
508 }
509
510 ConstantRange
511 ConstantRange::sub(const ConstantRange &Other) const {
512   if (isEmptySet() || Other.isEmptySet())
513     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
514   if (isFullSet() || Other.isFullSet())
515     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
516
517   APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
518   APInt NewLower = getLower() - Other.getUpper() + 1;
519   APInt NewUpper = getUpper() - Other.getLower();
520   if (NewLower == NewUpper)
521     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
522
523   ConstantRange X = ConstantRange(NewLower, NewUpper);
524   if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
525     // We've wrapped, therefore, full set.
526     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
527
528   return X;
529 }
530
531 ConstantRange
532 ConstantRange::multiply(const ConstantRange &Other) const {
533   // TODO: If either operand is a single element and the multiply is known to
534   // be non-wrapping, round the result min and max value to the appropriate
535   // multiple of that element. If wrapping is possible, at least adjust the
536   // range according to the greatest power-of-two factor of the single element.
537
538   if (isEmptySet() || Other.isEmptySet())
539     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
540
541   // If any of the operands is zero, then the result is also zero.
542   if ((getSingleElement() && *getSingleElement() == 0) ||
543       (Other.getSingleElement() && *Other.getSingleElement() == 0))
544     return ConstantRange(APInt(getBitWidth(), 0));
545
546   if (isFullSet() || Other.isFullSet())
547     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
548
549   APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
550   APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
551   APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
552   APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
553
554   ConstantRange Result_zext = ConstantRange(this_min * Other_min,
555                                             this_max * Other_max + 1);
556   return Result_zext.truncate(getBitWidth());
557 }
558
559 ConstantRange
560 ConstantRange::smax(const ConstantRange &Other) const {
561   // X smax Y is: range(smax(X_smin, Y_smin),
562   //                    smax(X_smax, Y_smax))
563   if (isEmptySet() || Other.isEmptySet())
564     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
565   APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
566   APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
567   if (NewU == NewL)
568     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
569   return ConstantRange(NewL, NewU);
570 }
571
572 ConstantRange
573 ConstantRange::umax(const ConstantRange &Other) const {
574   // X umax Y is: range(umax(X_umin, Y_umin),
575   //                    umax(X_umax, Y_umax))
576   if (isEmptySet() || Other.isEmptySet())
577     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
578   APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
579   APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
580   if (NewU == NewL)
581     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
582   return ConstantRange(NewL, NewU);
583 }
584
585 ConstantRange
586 ConstantRange::udiv(const ConstantRange &RHS) const {
587   if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
588     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
589   if (RHS.isFullSet())
590     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
591
592   APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
593
594   APInt RHS_umin = RHS.getUnsignedMin();
595   if (RHS_umin == 0) {
596     // We want the lowest value in RHS excluding zero. Usually that would be 1
597     // except for a range in the form of [X, 1) in which case it would be X.
598     if (RHS.getUpper() == 1)
599       RHS_umin = RHS.getLower();
600     else
601       RHS_umin = APInt(getBitWidth(), 1);
602   }
603
604   APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
605
606   // If the LHS is Full and the RHS is a wrapped interval containing 1 then
607   // this could occur.
608   if (Lower == Upper)
609     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
610
611   return ConstantRange(Lower, Upper);
612 }
613
614 ConstantRange
615 ConstantRange::binaryAnd(const ConstantRange &Other) const {
616   if (isEmptySet() || Other.isEmptySet())
617     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
618
619   // TODO: replace this with something less conservative
620
621   APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax());
622   if (umin.isAllOnesValue())
623     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
624   return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1);
625 }
626
627 ConstantRange
628 ConstantRange::binaryOr(const ConstantRange &Other) const {
629   if (isEmptySet() || Other.isEmptySet())
630     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
631
632   // TODO: replace this with something less conservative
633
634   APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
635   if (umax.isMinValue())
636     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
637   return ConstantRange(umax, APInt::getNullValue(getBitWidth()));
638 }
639
640 ConstantRange
641 ConstantRange::shl(const ConstantRange &Other) const {
642   if (isEmptySet() || Other.isEmptySet())
643     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
644
645   APInt min = getUnsignedMin().shl(Other.getUnsignedMin());
646   APInt max = getUnsignedMax().shl(Other.getUnsignedMax());
647
648   // there's no overflow!
649   APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
650   if (Zeros.ugt(Other.getUnsignedMax()))
651     return ConstantRange(min, max + 1);
652
653   // FIXME: implement the other tricky cases
654   return ConstantRange(getBitWidth(), /*isFullSet=*/true);
655 }
656
657 ConstantRange
658 ConstantRange::lshr(const ConstantRange &Other) const {
659   if (isEmptySet() || Other.isEmptySet())
660     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
661   
662   APInt max = getUnsignedMax().lshr(Other.getUnsignedMin());
663   APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
664   if (min == max + 1)
665     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
666
667   return ConstantRange(min, max + 1);
668 }
669
670 ConstantRange ConstantRange::inverse() const {
671   if (isFullSet())
672     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
673   if (isEmptySet())
674     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
675   return ConstantRange(Upper, Lower);
676 }
677
678 /// print - Print out the bounds to a stream...
679 ///
680 void ConstantRange::print(raw_ostream &OS) const {
681   if (isFullSet())
682     OS << "full-set";
683   else if (isEmptySet())
684     OS << "empty-set";
685   else
686     OS << "[" << Lower << "," << Upper << ")";
687 }
688
689 /// dump - Allow printing from a debugger easily...
690 ///
691 void ConstantRange::dump() const {
692   print(dbgs());
693 }