add a new code
[oota-llvm.git] / lib / Support / ConstantRange.cpp
1 //===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/Streams.h"
26 #include <ostream>
27 using namespace llvm;
28
29 /// Initialize a full (the default) or empty set for the specified type.
30 ///
31 ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) :
32   Lower(BitWidth, 0), Upper(BitWidth, 0) {
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 /// isFullSet - Return true if this set contains all of the elements possible
52 /// for this data-type
53 bool ConstantRange::isFullSet() const {
54   return Lower == Upper && Lower.isMaxValue();
55 }
56
57 /// isEmptySet - Return true if this set contains no members.
58 ///
59 bool ConstantRange::isEmptySet() const {
60   return Lower == Upper && Lower.isMinValue();
61 }
62
63 /// isWrappedSet - Return true if this set wraps around the top of the range,
64 /// for example: [100, 8)
65 ///
66 bool ConstantRange::isWrappedSet() const {
67   return Lower.ugt(Upper);
68 }
69
70 /// getSetSize - Return the number of elements in this set.
71 ///
72 APInt ConstantRange::getSetSize() const {
73   if (isEmptySet()) 
74     return APInt(getBitWidth(), 0);
75   if (getBitWidth() == 1) {
76     if (Lower != Upper)  // One of T or F in the set...
77       return APInt(2, 1);
78     return APInt(2, 2);      // Must be full set...
79   }
80
81   // Simply subtract the bounds...
82   return Upper - Lower;
83 }
84
85 /// getUnsignedMax - Return the largest unsigned value contained in the
86 /// ConstantRange.
87 ///
88 APInt ConstantRange::getUnsignedMax() const {
89   if (isFullSet() || isWrappedSet())
90     return APInt::getMaxValue(getBitWidth());
91   else
92     return getUpper() - 1;
93 }
94
95 /// getUnsignedMin - Return the smallest unsigned value contained in the
96 /// ConstantRange.
97 ///
98 APInt ConstantRange::getUnsignedMin() const {
99   if (isFullSet() || (isWrappedSet() && getUpper() != 0))
100     return APInt::getMinValue(getBitWidth());
101   else
102     return getLower();
103 }
104
105 /// getSignedMax - Return the largest signed value contained in the
106 /// ConstantRange.
107 ///
108 APInt ConstantRange::getSignedMax() const {
109   APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
110   if (!isWrappedSet()) {
111     if (getLower().slt(getUpper() - 1))
112       return getUpper() - 1;
113     else
114       return SignedMax;
115   } else {
116     if ((getUpper() - 1).slt(getLower())) {
117       if (getLower() != SignedMax)
118         return SignedMax;
119       else
120         return getUpper() - 1;
121     } else {
122       return getUpper() - 1;
123     }
124   }
125 }
126
127 /// getSignedMin - Return the smallest signed value contained in the
128 /// ConstantRange.
129 ///
130 APInt ConstantRange::getSignedMin() const {
131   APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
132   if (!isWrappedSet()) {
133     if (getLower().slt(getUpper() - 1))
134       return getLower();
135     else
136       return SignedMin;
137   } else {
138     if ((getUpper() - 1).slt(getLower())) {
139       if (getUpper() != SignedMin)
140         return SignedMin;
141       else
142         return getLower();
143     } else {
144       return getLower();
145     }
146   }
147 }
148
149 /// contains - Return true if the specified value is in the set.
150 ///
151 bool ConstantRange::contains(const APInt &V) const {
152   if (Lower == Upper)
153     return isFullSet();
154
155   if (!isWrappedSet())
156     return Lower.ule(V) && V.ult(Upper);
157   else
158     return Lower.ule(V) || V.ult(Upper);
159 }
160
161 /// subtract - Subtract the specified constant from the endpoints of this
162 /// constant range.
163 ConstantRange ConstantRange::subtract(const APInt &Val) const {
164   assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
165   // If the set is empty or full, don't modify the endpoints.
166   if (Lower == Upper) 
167     return *this;
168   return ConstantRange(Lower - Val, Upper - Val);
169 }
170
171
172 // intersect1Wrapped - This helper function is used to intersect two ranges when
173 // it is known that LHS is wrapped and RHS isn't.
174 //
175 ConstantRange 
176 ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
177                                  const ConstantRange &RHS) {
178   assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
179
180   // Check to see if we overlap on the Left side of RHS...
181   //
182   if (RHS.Lower.ult(LHS.Upper)) {
183     // We do overlap on the left side of RHS, see if we overlap on the right of
184     // RHS...
185     if (RHS.Upper.ugt(LHS.Lower)) {
186       // Ok, the result overlaps on both the left and right sides.  See if the
187       // resultant interval will be smaller if we wrap or not...
188       //
189       if (LHS.getSetSize().ult(RHS.getSetSize()))
190         return LHS;
191       else
192         return RHS;
193
194     } else {
195       // No overlap on the right, just on the left.
196       return ConstantRange(RHS.Lower, LHS.Upper);
197     }
198   } else {
199     // We don't overlap on the left side of RHS, see if we overlap on the right
200     // of RHS...
201     if (RHS.Upper.ugt(LHS.Lower)) {
202       // Simple overlap...
203       return ConstantRange(LHS.Lower, RHS.Upper);
204     } else {
205       // No overlap...
206       return ConstantRange(LHS.getBitWidth(), false);
207     }
208   }
209 }
210
211 /// intersectWith - Return the range that results from the intersection of this
212 /// range with another range.
213 ///
214 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
215   assert(getBitWidth() == CR.getBitWidth() && 
216          "ConstantRange types don't agree!");
217   // Handle common special cases
218   if (isEmptySet() || CR.isFullSet())  
219     return *this;
220   if (isFullSet()  || CR.isEmptySet()) 
221     return CR;
222
223   if (!isWrappedSet()) {
224     if (!CR.isWrappedSet()) {
225       using namespace APIntOps;
226       APInt L = umax(Lower, CR.Lower);
227       APInt U = umin(Upper, CR.Upper);
228
229       if (L.ult(U)) // If range isn't empty...
230         return ConstantRange(L, U);
231       else
232         return ConstantRange(getBitWidth(), false);// Otherwise, empty set
233     } else
234       return intersect1Wrapped(CR, *this);
235   } else {   // We know "this" is wrapped...
236     if (!CR.isWrappedSet())
237       return intersect1Wrapped(*this, CR);
238     else {
239       // Both ranges are wrapped...
240       using namespace APIntOps;
241       APInt L = umax(Lower, CR.Lower);
242       APInt U = umin(Upper, CR.Upper);
243       return ConstantRange(L, U);
244     }
245   }
246   return *this;
247 }
248
249 /// unionWith - Return the range that results from the union of this range with
250 /// another range.  The resultant range is guaranteed to include the elements of
251 /// both sets, but may contain more.  For example, [3, 9) union [12,15) is
252 /// [3, 15), which includes 9, 10, and 11, which were not included in either
253 /// set before.
254 ///
255 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
256   assert(getBitWidth() == CR.getBitWidth() && 
257          "ConstantRange types don't agree!");
258
259   if (   isFullSet() || CR.isEmptySet()) return *this;
260   if (CR.isFullSet() ||    isEmptySet()) return CR;
261
262   if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
263
264   APInt L = Lower, U = Upper;
265
266   if (!isWrappedSet() && !CR.isWrappedSet()) {
267     if (CR.Lower.ult(L))
268       L = CR.Lower;
269
270     if (CR.Upper.ugt(U))
271       U = CR.Upper;
272   }
273
274   if (isWrappedSet() && !CR.isWrappedSet()) {
275     if ((CR.Lower.ult(Upper) && CR.Upper.ult(Upper)) ||
276         (CR.Lower.ugt(Lower) && CR.Upper.ugt(Lower))) {
277       return *this;
278     }
279
280     if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) {
281       return ConstantRange(getBitWidth());
282     }
283
284     if (CR.Lower.ule(Upper) && CR.Upper.ule(Lower)) {
285       APInt d1 = CR.Upper - Upper, d2 = Lower - CR.Upper;
286       if (d1.ult(d2)) {
287         U = CR.Upper;
288       } else {
289         L = CR.Upper;
290       }
291     }
292
293     if (Upper.ult(CR.Lower) && CR.Upper.ult(Lower)) {
294       APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
295       if (d1.ult(d2)) {
296         U = CR.Lower + 1;
297       } else {
298         L = CR.Upper - 1;
299       }
300     }
301
302     if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) {
303       APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Lower;
304
305       if (d1.ult(d2)) {
306         U = CR.Lower + 1;
307       } else {
308         L = CR.Lower;
309       }
310     }
311   }
312
313   if (isWrappedSet() && CR.isWrappedSet()) {
314     if (Lower.ult(CR.Upper) || CR.Lower.ult(Upper))
315       return ConstantRange(getBitWidth());
316
317     if (CR.Upper.ugt(U)) {
318       U = CR.Upper;
319     }
320
321     if (CR.Lower.ult(L)) {
322       L = CR.Lower;
323     }
324
325     if (L == U) return ConstantRange(getBitWidth());
326   }
327
328   return ConstantRange(L, U);
329 }
330
331 /// zeroExtend - Return a new range in the specified integer type, which must
332 /// be strictly larger than the current type.  The returned range will
333 /// correspond to the possible range of values as if the source range had been
334 /// zero extended.
335 ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
336   unsigned SrcTySize = getBitWidth();
337   assert(SrcTySize < DstTySize && "Not a value extension");
338   if (isFullSet())
339     // Change a source full set into [0, 1 << 8*numbytes)
340     return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
341
342   APInt L = Lower; L.zext(DstTySize);
343   APInt U = Upper; U.zext(DstTySize);
344   return ConstantRange(L, U);
345 }
346
347 /// signExtend - Return a new range in the specified integer type, which must
348 /// be strictly larger than the current type.  The returned range will
349 /// correspond to the possible range of values as if the source range had been
350 /// sign extended.
351 ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
352   unsigned SrcTySize = getBitWidth();
353   assert(SrcTySize < DstTySize && "Not a value extension");
354   if (isFullSet()) {
355     return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
356                          APInt::getLowBitsSet(DstTySize, SrcTySize-1));
357   }
358
359   APInt L = Lower; L.sext(DstTySize);
360   APInt U = Upper; U.sext(DstTySize);
361   return ConstantRange(L, U);
362 }
363
364 /// truncate - Return a new range in the specified integer type, which must be
365 /// strictly smaller than the current type.  The returned range will
366 /// correspond to the possible range of values as if the source range had been
367 /// truncated to the specified type.
368 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
369   unsigned SrcTySize = getBitWidth();
370   assert(SrcTySize > DstTySize && "Not a value truncation");
371   APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
372   if (isFullSet() || getSetSize().ugt(Size))
373     return ConstantRange(DstTySize);
374
375   APInt L = Lower; L.trunc(DstTySize);
376   APInt U = Upper; U.trunc(DstTySize);
377   return ConstantRange(L, U);
378 }
379
380 /// print - Print out the bounds to a stream...
381 ///
382 void ConstantRange::print(std::ostream &OS) const {
383   OS << "[" << Lower.toStringSigned(10) << "," 
384             << Upper.toStringSigned(10) << " )";
385 }
386
387 /// dump - Allow printing from a debugger easily...
388 ///
389 void ConstantRange::dump() const {
390   print(cerr);
391 }