becb8b61f78322b8535bb5b9bdc6357017419c42
[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   uint32_t BitWidth = L.getBitWidth();
48   assert((L != U || (L == APInt::getMaxValue(BitWidth) ||
49                      L == APInt::getMinValue(BitWidth))) &&
50          "Lower == Upper, but they aren't min or max value!");
51 }
52
53 /// isFullSet - Return true if this set contains all of the elements possible
54 /// for this data-type
55 bool ConstantRange::isFullSet() const {
56   return Lower == Upper && Lower == APInt::getMaxValue(getBitWidth());
57 }
58
59 /// isEmptySet - Return true if this set contains no members.
60 ///
61 bool ConstantRange::isEmptySet() const {
62   return Lower == Upper && Lower == APInt::getMinValue(getBitWidth());
63 }
64
65 /// isWrappedSet - Return true if this set wraps around the top of the range,
66 /// for example: [100, 8)
67 ///
68 bool ConstantRange::isWrappedSet() const {
69   return Lower.ugt(Upper);
70 }
71
72 /// getSetSize - Return the number of elements in this set.
73 ///
74 APInt ConstantRange::getSetSize() const {
75   if (isEmptySet()) 
76     return APInt(getBitWidth(), 0);
77   if (getBitWidth() == 1) {
78     if (Lower != Upper)  // One of T or F in the set...
79       return APInt(2, 1);
80     return APInt(2, 2);      // Must be full set...
81   }
82
83   // Simply subtract the bounds...
84   return Upper - Lower;
85 }
86
87 /// getUnsignedMax - Return the largest unsigned value contained in the
88 /// ConstantRange.
89 ///
90 APInt ConstantRange::getUnsignedMax() const {
91   if (isFullSet() || isWrappedSet())
92     return APInt::getMaxValue(getBitWidth());
93   else
94     return getUpper() - 1;
95 }
96
97 /// getUnsignedMin - Return the smallest unsigned value contained in the
98 /// ConstantRange.
99 ///
100 APInt ConstantRange::getUnsignedMin() const {
101   if (isFullSet() || (isWrappedSet() && getUpper() != 0))
102     return APInt::getMinValue(getBitWidth());
103   else
104     return getLower();
105 }
106
107 /// getSignedMax - Return the largest signed value contained in the
108 /// ConstantRange.
109 ///
110 APInt ConstantRange::getSignedMax() const {
111   APInt SignedMax = APInt::getSignedMaxValue(getBitWidth());
112   if (!isWrappedSet()) {
113     if (getLower().slt(getUpper() - 1))
114       return getUpper() - 1;
115     else
116       return SignedMax;
117   } else {
118     if ((getUpper() - 1).slt(getLower())) {
119       if (getLower() != SignedMax)
120         return SignedMax;
121       else
122         return getUpper() - 1;
123     } else {
124       return getUpper() - 1;
125     }
126   }
127 }
128
129 /// getSignedMin - Return the smallest signed value contained in the
130 /// ConstantRange.
131 ///
132 APInt ConstantRange::getSignedMin() const {
133   APInt SignedMin = APInt::getSignedMinValue(getBitWidth());
134   if (!isWrappedSet()) {
135     if (getLower().slt(getUpper() - 1))
136       return getLower();
137     else
138       return SignedMin;
139   } else {
140     if ((getUpper() - 1).slt(getLower())) {
141       if (getUpper() != SignedMin)
142         return SignedMin;
143       else
144         return getLower();
145     } else {
146       return getLower();
147     }
148   }
149 }
150
151 /// contains - Return true if the specified value is in the set.
152 ///
153 bool ConstantRange::contains(const APInt &V) const {
154   if (Lower == Upper)
155     return isFullSet();
156
157   if (!isWrappedSet())
158     return Lower.ule(V) && V.ult(Upper);
159   else
160     return Lower.ule(V) || V.ult(Upper);
161 }
162
163 /// subtract - Subtract the specified constant from the endpoints of this
164 /// constant range.
165 ConstantRange ConstantRange::subtract(const APInt &Val) const {
166   assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
167   // If the set is empty or full, don't modify the endpoints.
168   if (Lower == Upper) 
169     return *this;
170   return ConstantRange(Lower - Val, Upper - Val);
171 }
172
173
174 // intersect1Wrapped - This helper function is used to intersect two ranges when
175 // it is known that LHS is wrapped and RHS isn't.
176 //
177 ConstantRange 
178 ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
179                                  const ConstantRange &RHS) {
180   assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
181
182   // Check to see if we overlap on the Left side of RHS...
183   //
184   if (RHS.Lower.ult(LHS.Upper)) {
185     // We do overlap on the left side of RHS, see if we overlap on the right of
186     // RHS...
187     if (RHS.Upper.ugt(LHS.Lower)) {
188       // Ok, the result overlaps on both the left and right sides.  See if the
189       // resultant interval will be smaller if we wrap or not...
190       //
191       if (LHS.getSetSize().ult(RHS.getSetSize()))
192         return LHS;
193       else
194         return RHS;
195
196     } else {
197       // No overlap on the right, just on the left.
198       return ConstantRange(RHS.Lower, LHS.Upper);
199     }
200   } else {
201     // We don't overlap on the left side of RHS, see if we overlap on the right
202     // of RHS...
203     if (RHS.Upper.ugt(LHS.Lower)) {
204       // Simple overlap...
205       return ConstantRange(LHS.Lower, RHS.Upper);
206     } else {
207       // No overlap...
208       return ConstantRange(LHS.getBitWidth(), false);
209     }
210   }
211 }
212
213 /// intersectWith - Return the range that results from the intersection of this
214 /// range with another range.
215 ///
216 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
217   assert(getBitWidth() == CR.getBitWidth() && 
218          "ConstantRange types don't agree!");
219   // Handle common special cases
220   if (isEmptySet() || CR.isFullSet())  
221     return *this;
222   if (isFullSet()  || CR.isEmptySet()) 
223     return CR;
224
225   if (!isWrappedSet()) {
226     if (!CR.isWrappedSet()) {
227       using namespace APIntOps;
228       APInt L = umax(Lower, CR.Lower);
229       APInt U = umin(Upper, CR.Upper);
230
231       if (L.ult(U)) // If range isn't empty...
232         return ConstantRange(L, U);
233       else
234         return ConstantRange(getBitWidth(), false);// Otherwise, empty set
235     } else
236       return intersect1Wrapped(CR, *this);
237   } else {   // We know "this" is wrapped...
238     if (!CR.isWrappedSet())
239       return intersect1Wrapped(*this, CR);
240     else {
241       // Both ranges are wrapped...
242       using namespace APIntOps;
243       APInt L = umax(Lower, CR.Lower);
244       APInt U = umin(Upper, CR.Upper);
245       return ConstantRange(L, U);
246     }
247   }
248   return *this;
249 }
250
251 /// unionWith - Return the range that results from the union of this range with
252 /// another range.  The resultant range is guaranteed to include the elements of
253 /// both sets, but may contain more.  For example, [3, 9) union [12,15) is [3,
254 /// 15), which includes 9, 10, and 11, which were not included in either set
255 /// before.
256 ///
257 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
258   assert(getBitWidth() == CR.getBitWidth() && 
259          "ConstantRange types don't agree!");
260
261   if (   isFullSet() || CR.isEmptySet()) return *this;
262   if (CR.isFullSet() ||    isEmptySet()) return CR;
263
264   APInt L = Lower, U = Upper;
265
266   if (!contains(CR.Lower))
267     L = APIntOps::umin(L, CR.Lower);
268
269   if (!contains(CR.Upper - 1))
270     U = APIntOps::umax(U, CR.Upper);
271
272   return ConstantRange(L, U);
273 }
274
275 /// zeroExtend - Return a new range in the specified integer type, which must
276 /// be strictly larger than the current type.  The returned range will
277 /// correspond to the possible range of values as if the source range had been
278 /// zero extended.
279 ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
280   unsigned SrcTySize = getBitWidth();
281   assert(SrcTySize < DstTySize && "Not a value extension");
282   if (isFullSet())
283     // Change a source full set into [0, 1 << 8*numbytes)
284     return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
285
286   APInt L = Lower; L.zext(DstTySize);
287   APInt U = Upper; U.zext(DstTySize);
288   return ConstantRange(L, U);
289 }
290
291 /// truncate - Return a new range in the specified integer type, which must be
292 /// strictly smaller than the current type.  The returned range will
293 /// correspond to the possible range of values as if the source range had been
294 /// truncated to the specified type.
295 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
296   unsigned SrcTySize = getBitWidth();
297   assert(SrcTySize > DstTySize && "Not a value truncation");
298   APInt Size = APInt::getMaxValue(DstTySize).zext(SrcTySize);
299   if (isFullSet() || getSetSize().ugt(Size))
300     return ConstantRange(DstTySize);
301
302   APInt L = Lower; L.trunc(DstTySize);
303   APInt U = Upper; U.trunc(DstTySize);
304   return ConstantRange(L, U);
305 }
306
307 /// print - Print out the bounds to a stream...
308 ///
309 void ConstantRange::print(std::ostream &OS) const {
310   OS << "[" << Lower.toStringSigned(10) << "," 
311             << Upper.toStringSigned(10) << " )";
312 }
313
314 /// dump - Allow printing from a debugger easily...
315 ///
316 void ConstantRange::dump() const {
317   print(cerr);
318 }