blob: cb8c4b013c32b7d1679474d794985b48ef742c26 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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"
Chris Lattner1fefaac2008-08-23 22:23:09 +000025#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026using namespace llvm;
27
28/// Initialize a full (the default) or empty set for the specified type.
29///
30ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) :
31 Lower(BitWidth, 0), Upper(BitWidth, 0) {
32 if (Full)
33 Lower = Upper = APInt::getMaxValue(BitWidth);
34 else
35 Lower = Upper = APInt::getMinValue(BitWidth);
36}
37
38/// Initialize a range to hold the single specified value.
39///
40ConstantRange::ConstantRange(const APInt & V) : Lower(V), Upper(V + 1) { }
41
42ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
43 Lower(L), Upper(U) {
44 assert(L.getBitWidth() == U.getBitWidth() &&
45 "ConstantRange with unequal bit widths");
46 assert((L != U || (L.isMaxValue() || L.isMinValue())) &&
47 "Lower == Upper, but they aren't min or max value!");
48}
49
50/// isFullSet - Return true if this set contains all of the elements possible
51/// for this data-type
52bool ConstantRange::isFullSet() const {
53 return Lower == Upper && Lower.isMaxValue();
54}
55
56/// isEmptySet - Return true if this set contains no members.
57///
58bool ConstantRange::isEmptySet() const {
59 return Lower == Upper && Lower.isMinValue();
60}
61
62/// isWrappedSet - Return true if this set wraps around the top of the range,
63/// for example: [100, 8)
64///
65bool ConstantRange::isWrappedSet() const {
66 return Lower.ugt(Upper);
67}
68
69/// getSetSize - Return the number of elements in this set.
70///
71APInt ConstantRange::getSetSize() const {
72 if (isEmptySet())
73 return APInt(getBitWidth(), 0);
74 if (getBitWidth() == 1) {
75 if (Lower != Upper) // One of T or F in the set...
76 return APInt(2, 1);
77 return APInt(2, 2); // Must be full set...
78 }
79
80 // Simply subtract the bounds...
81 return Upper - Lower;
82}
83
84/// getUnsignedMax - Return the largest unsigned value contained in the
85/// ConstantRange.
86///
87APInt ConstantRange::getUnsignedMax() const {
88 if (isFullSet() || isWrappedSet())
89 return APInt::getMaxValue(getBitWidth());
90 else
91 return getUpper() - 1;
92}
93
94/// getUnsignedMin - Return the smallest unsigned value contained in the
95/// ConstantRange.
96///
97APInt ConstantRange::getUnsignedMin() const {
98 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
99 return APInt::getMinValue(getBitWidth());
100 else
101 return getLower();
102}
103
104/// getSignedMax - Return the largest signed value contained in the
105/// ConstantRange.
106///
107APInt ConstantRange::getSignedMax() const {
108 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
109 if (!isWrappedSet()) {
110 if (getLower().sle(getUpper() - 1))
111 return getUpper() - 1;
112 else
113 return SignedMax;
114 } else {
115 if ((getUpper() - 1).slt(getLower())) {
116 if (getLower() != SignedMax)
117 return SignedMax;
118 else
119 return getUpper() - 1;
120 } else {
121 return getUpper() - 1;
122 }
123 }
124}
125
126/// getSignedMin - Return the smallest signed value contained in the
127/// ConstantRange.
128///
129APInt ConstantRange::getSignedMin() const {
130 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
131 if (!isWrappedSet()) {
132 if (getLower().sle(getUpper() - 1))
133 return getLower();
134 else
135 return SignedMin;
136 } else {
137 if ((getUpper() - 1).slt(getLower())) {
138 if (getUpper() != SignedMin)
139 return SignedMin;
140 else
141 return getLower();
142 } else {
143 return getLower();
144 }
145 }
146}
147
148/// contains - Return true if the specified value is in the set.
149///
150bool ConstantRange::contains(const APInt &V) const {
151 if (Lower == Upper)
152 return isFullSet();
153
154 if (!isWrappedSet())
155 return Lower.ule(V) && V.ult(Upper);
156 else
157 return Lower.ule(V) || V.ult(Upper);
158}
159
160/// subtract - Subtract the specified constant from the endpoints of this
161/// constant range.
162ConstantRange ConstantRange::subtract(const APInt &Val) const {
163 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
164 // If the set is empty or full, don't modify the endpoints.
165 if (Lower == Upper)
166 return *this;
167 return ConstantRange(Lower - Val, Upper - Val);
168}
169
170
171// intersect1Wrapped - This helper function is used to intersect two ranges when
172// it is known that LHS is wrapped and RHS isn't.
173//
174ConstantRange
175ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
176 const ConstantRange &RHS) {
177 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
178
179 // Check to see if we overlap on the Left side of RHS...
180 //
181 if (RHS.Lower.ult(LHS.Upper)) {
182 // We do overlap on the left side of RHS, see if we overlap on the right of
183 // RHS...
184 if (RHS.Upper.ugt(LHS.Lower)) {
185 // Ok, the result overlaps on both the left and right sides. See if the
186 // resultant interval will be smaller if we wrap or not...
187 //
188 if (LHS.getSetSize().ult(RHS.getSetSize()))
189 return LHS;
190 else
191 return RHS;
192
193 } else {
194 // No overlap on the right, just on the left.
195 return ConstantRange(RHS.Lower, LHS.Upper);
196 }
197 } else {
198 // We don't overlap on the left side of RHS, see if we overlap on the right
199 // of RHS...
200 if (RHS.Upper.ugt(LHS.Lower)) {
201 // Simple overlap...
202 return ConstantRange(LHS.Lower, RHS.Upper);
203 } else {
204 // No overlap...
205 return ConstantRange(LHS.getBitWidth(), false);
206 }
207 }
208}
209
210/// intersectWith - Return the range that results from the intersection of this
211/// range with another range.
212///
213ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
214 assert(getBitWidth() == CR.getBitWidth() &&
215 "ConstantRange types don't agree!");
216 // Handle common special cases
217 if (isEmptySet() || CR.isFullSet())
218 return *this;
219 if (isFullSet() || CR.isEmptySet())
220 return CR;
221
222 if (!isWrappedSet()) {
223 if (!CR.isWrappedSet()) {
224 using namespace APIntOps;
225 APInt L = umax(Lower, CR.Lower);
226 APInt U = umin(Upper, CR.Upper);
227
228 if (L.ult(U)) // If range isn't empty...
229 return ConstantRange(L, U);
230 else
231 return ConstantRange(getBitWidth(), false);// Otherwise, empty set
232 } else
233 return intersect1Wrapped(CR, *this);
234 } else { // We know "this" is wrapped...
235 if (!CR.isWrappedSet())
236 return intersect1Wrapped(*this, CR);
237 else {
238 // Both ranges are wrapped...
239 using namespace APIntOps;
240 APInt L = umax(Lower, CR.Lower);
241 APInt U = umin(Upper, CR.Upper);
242 return ConstantRange(L, U);
243 }
244 }
245 return *this;
246}
247
248/// maximalIntersectWith - Return the range that results from the intersection
249/// of this range with another range. The resultant range is guaranteed to
250/// include all elements contained in both input ranges, and to have the
251/// smallest possible set size that does so. Because there may be two
252/// intersections with the same set size, A.maximalIntersectWith(B) might not
253/// be equal to B.maximalIntersect(A).
254ConstantRange ConstantRange::maximalIntersectWith(const ConstantRange &CR) const {
255 assert(getBitWidth() == CR.getBitWidth() &&
256 "ConstantRange types don't agree!");
257
258 // Handle common cases.
259 if ( isEmptySet() || CR.isFullSet()) return *this;
260 if (CR.isEmptySet() || isFullSet()) return CR;
261
262 if (!isWrappedSet() && CR.isWrappedSet())
263 return CR.maximalIntersectWith(*this);
264
265 if (!isWrappedSet() && !CR.isWrappedSet()) {
266 if (Lower.ult(CR.Lower)) {
267 if (Upper.ule(CR.Lower))
268 return ConstantRange(getBitWidth(), false);
269
270 if (Upper.ult(CR.Upper))
271 return ConstantRange(CR.Lower, Upper);
272
273 return CR;
274 } else {
275 if (Upper.ult(CR.Upper))
276 return *this;
277
278 if (Lower.ult(CR.Upper))
279 return ConstantRange(Lower, CR.Upper);
280
281 return ConstantRange(getBitWidth(), false);
282 }
283 }
284
285 if (isWrappedSet() && !CR.isWrappedSet()) {
286 if (CR.Lower.ult(Upper)) {
287 if (CR.Upper.ult(Upper))
288 return CR;
289
290 if (CR.Upper.ult(Lower))
291 return ConstantRange(CR.Lower, Upper);
292
293 if (getSetSize().ult(CR.getSetSize()))
294 return *this;
295 else
296 return CR;
297 } else if (CR.Lower.ult(Lower)) {
298 if (CR.Upper.ule(Lower))
299 return ConstantRange(getBitWidth(), false);
300
301 return ConstantRange(Lower, CR.Upper);
302 }
303 return CR;
304 }
305
306 if (CR.Upper.ult(Upper)) {
307 if (CR.Lower.ult(Upper)) {
308 if (getSetSize().ult(CR.getSetSize()))
309 return *this;
310 else
311 return CR;
312 }
313
314 if (CR.Lower.ult(Lower))
315 return ConstantRange(Lower, CR.Upper);
316
317 return CR;
318 } else if (CR.Upper.ult(Lower)) {
319 if (CR.Lower.ult(Lower))
320 return *this;
321
322 return ConstantRange(CR.Lower, Upper);
323 }
324 if (getSetSize().ult(CR.getSetSize()))
325 return *this;
326 else
327 return CR;
328}
329
330
331/// unionWith - Return the range that results from the union of this range with
332/// another range. The resultant range is guaranteed to include the elements of
333/// both sets, but may contain more. For example, [3, 9) union [12,15) is
334/// [3, 15), which includes 9, 10, and 11, which were not included in either
335/// set before.
336///
337ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
338 assert(getBitWidth() == CR.getBitWidth() &&
339 "ConstantRange types don't agree!");
340
341 if ( isFullSet() || CR.isEmptySet()) return *this;
342 if (CR.isFullSet() || isEmptySet()) return CR;
343
344 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
345
346 APInt L = Lower, U = Upper;
347
348 if (!isWrappedSet() && !CR.isWrappedSet()) {
349 if (CR.Lower.ult(L))
350 L = CR.Lower;
351
352 if (CR.Upper.ugt(U))
353 U = CR.Upper;
354 }
355
356 if (isWrappedSet() && !CR.isWrappedSet()) {
357 if ((CR.Lower.ult(Upper) && CR.Upper.ult(Upper)) ||
358 (CR.Lower.ugt(Lower) && CR.Upper.ugt(Lower))) {
359 return *this;
360 }
361
362 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) {
363 return ConstantRange(getBitWidth());
364 }
365
366 if (CR.Lower.ule(Upper) && CR.Upper.ule(Lower)) {
367 APInt d1 = CR.Upper - Upper, d2 = Lower - CR.Upper;
368 if (d1.ult(d2)) {
369 U = CR.Upper;
370 } else {
371 L = CR.Upper;
372 }
373 }
374
375 if (Upper.ult(CR.Lower) && CR.Upper.ult(Lower)) {
376 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
377 if (d1.ult(d2)) {
378 U = CR.Lower + 1;
379 } else {
380 L = CR.Upper - 1;
381 }
382 }
383
384 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) {
385 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Lower;
386
387 if (d1.ult(d2)) {
388 U = CR.Lower + 1;
389 } else {
390 L = CR.Lower;
391 }
392 }
393 }
394
395 if (isWrappedSet() && CR.isWrappedSet()) {
396 if (Lower.ult(CR.Upper) || CR.Lower.ult(Upper))
397 return ConstantRange(getBitWidth());
398
399 if (CR.Upper.ugt(U)) {
400 U = CR.Upper;
401 }
402
403 if (CR.Lower.ult(L)) {
404 L = CR.Lower;
405 }
406
407 if (L == U) return ConstantRange(getBitWidth());
408 }
409
410 return ConstantRange(L, U);
411}
412
413/// zeroExtend - Return a new range in the specified integer type, which must
414/// be strictly larger than the current type. The returned range will
415/// correspond to the possible range of values as if the source range had been
416/// zero extended.
417ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
418 unsigned SrcTySize = getBitWidth();
419 assert(SrcTySize < DstTySize && "Not a value extension");
420 if (isFullSet())
421 // Change a source full set into [0, 1 << 8*numbytes)
422 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
423
424 APInt L = Lower; L.zext(DstTySize);
425 APInt U = Upper; U.zext(DstTySize);
426 return ConstantRange(L, U);
427}
428
429/// signExtend - Return a new range in the specified integer type, which must
430/// be strictly larger than the current type. The returned range will
431/// correspond to the possible range of values as if the source range had been
432/// sign extended.
433ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
434 unsigned SrcTySize = getBitWidth();
435 assert(SrcTySize < DstTySize && "Not a value extension");
436 if (isFullSet()) {
437 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
438 APInt::getLowBitsSet(DstTySize, SrcTySize-1));
439 }
440
441 APInt L = Lower; L.sext(DstTySize);
442 APInt U = Upper; U.sext(DstTySize);
443 return ConstantRange(L, U);
444}
445
446/// truncate - Return a new range in the specified integer type, which must be
447/// strictly smaller than the current type. The returned range will
448/// correspond to the possible range of values as if the source range had been
449/// truncated to the specified type.
450ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
451 unsigned SrcTySize = getBitWidth();
452 assert(SrcTySize > DstTySize && "Not a value truncation");
453 APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
454 if (isFullSet() || getSetSize().ugt(Size))
455 return ConstantRange(DstTySize);
456
457 APInt L = Lower; L.trunc(DstTySize);
458 APInt U = Upper; U.trunc(DstTySize);
459 return ConstantRange(L, U);
460}
461
462/// print - Print out the bounds to a stream...
463///
Chris Lattner1fefaac2008-08-23 22:23:09 +0000464void ConstantRange::print(raw_ostream &OS) const {
Chris Lattner89b36582008-08-17 07:19:36 +0000465 OS << "[" << Lower << "," << Upper << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466}
467
468/// dump - Allow printing from a debugger easily...
469///
470void ConstantRange::dump() const {
Chris Lattner1fefaac2008-08-23 22:23:09 +0000471 print(errs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472}