blob: cb8c4b013c32b7d1679474d794985b48ef742c26 [file] [log] [blame]
Chris Lattner645e00d2002-09-01 23:53:36 +00001//===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner645e00d2002-09-01 23:53:36 +00009//
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 Lattner944fac72008-08-23 22:23:09 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattner2cdd21c2003-12-14 21:35:53 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattner645e00d2002-09-01 23:53:36 +000028/// Initialize a full (the default) or empty set for the specified type.
29///
Reid Spencerbb626a62007-02-28 22:02:48 +000030ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) :
31 Lower(BitWidth, 0), Upper(BitWidth, 0) {
Chris Lattner645e00d2002-09-01 23:53:36 +000032 if (Full)
Reid Spencer663e7112007-02-28 17:36:23 +000033 Lower = Upper = APInt::getMaxValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000034 else
Reid Spencer663e7112007-02-28 17:36:23 +000035 Lower = Upper = APInt::getMinValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000036}
37
Chris Lattnerfc33d302004-03-30 00:20:08 +000038/// Initialize a range to hold the single specified value.
39///
Reid Spencerdc5c1592007-02-28 18:57:32 +000040ConstantRange::ConstantRange(const APInt & V) : Lower(V), Upper(V + 1) { }
Chris Lattner645e00d2002-09-01 23:53:36 +000041
Reid Spencer663e7112007-02-28 17:36:23 +000042ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
43 Lower(L), Upper(U) {
44 assert(L.getBitWidth() == U.getBitWidth() &&
45 "ConstantRange with unequal bit widths");
Zhou Shengc125c002007-04-26 16:42:07 +000046 assert((L != U || (L.isMaxValue() || L.isMinValue())) &&
Reid Spencer663e7112007-02-28 17:36:23 +000047 "Lower == Upper, but they aren't min or max value!");
48}
49
Chris Lattner645e00d2002-09-01 23:53:36 +000050/// isFullSet - Return true if this set contains all of the elements possible
51/// for this data-type
52bool ConstantRange::isFullSet() const {
Zhou Shengc125c002007-04-26 16:42:07 +000053 return Lower == Upper && Lower.isMaxValue();
Chris Lattner645e00d2002-09-01 23:53:36 +000054}
Misha Brukmanfd939082005-04-21 23:48:37 +000055
Chris Lattner645e00d2002-09-01 23:53:36 +000056/// isEmptySet - Return true if this set contains no members.
57///
58bool ConstantRange::isEmptySet() const {
Zhou Shengc125c002007-04-26 16:42:07 +000059 return Lower == Upper && Lower.isMinValue();
Chris Lattner645e00d2002-09-01 23:53:36 +000060}
61
62/// isWrappedSet - Return true if this set wraps around the top of the range,
63/// for example: [100, 8)
64///
Reid Spencera6e8a952007-03-01 07:54:15 +000065bool ConstantRange::isWrappedSet() const {
Reid Spencer663e7112007-02-28 17:36:23 +000066 return Lower.ugt(Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +000067}
68
Chris Lattner645e00d2002-09-01 23:53:36 +000069/// getSetSize - Return the number of elements in this set.
70///
Reid Spencer663e7112007-02-28 17:36:23 +000071APInt ConstantRange::getSetSize() const {
72 if (isEmptySet())
Reid Spencerbb626a62007-02-28 22:02:48 +000073 return APInt(getBitWidth(), 0);
74 if (getBitWidth() == 1) {
Chris Lattner645e00d2002-09-01 23:53:36 +000075 if (Lower != Upper) // One of T or F in the set...
Reid Spencerbb626a62007-02-28 22:02:48 +000076 return APInt(2, 1);
77 return APInt(2, 2); // Must be full set...
Chris Lattner645e00d2002-09-01 23:53:36 +000078 }
Misha Brukmanfd939082005-04-21 23:48:37 +000079
Chris Lattner645e00d2002-09-01 23:53:36 +000080 // Simply subtract the bounds...
Reid Spencer663e7112007-02-28 17:36:23 +000081 return Upper - Lower;
Chris Lattner645e00d2002-09-01 23:53:36 +000082}
83
Nick Lewycky3400e6a2007-03-10 15:54:12 +000084/// 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 {
Zhou Shengdaacf222007-04-13 05:57:32 +0000108 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000109 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000110 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000111 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 {
Zhou Shengdaacf222007-04-13 05:57:32 +0000130 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000131 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000132 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000133 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
Chris Lattnerfc33d302004-03-30 00:20:08 +0000148/// contains - Return true if the specified value is in the set.
149///
Reid Spencera6e8a952007-03-01 07:54:15 +0000150bool ConstantRange::contains(const APInt &V) const {
151 if (Lower == Upper)
152 return isFullSet();
Chris Lattner645e00d2002-09-01 23:53:36 +0000153
Reid Spencera6e8a952007-03-01 07:54:15 +0000154 if (!isWrappedSet())
155 return Lower.ule(V) && V.ult(Upper);
Reid Spencer663e7112007-02-28 17:36:23 +0000156 else
157 return Lower.ule(V) || V.ult(Upper);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000158}
159
Chris Lattnerfc33d302004-03-30 00:20:08 +0000160/// subtract - Subtract the specified constant from the endpoints of this
161/// constant range.
Reid Spencer581b0d42007-02-28 19:57:34 +0000162ConstantRange ConstantRange::subtract(const APInt &Val) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000163 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
Chris Lattnerfc33d302004-03-30 00:20:08 +0000164 // If the set is empty or full, don't modify the endpoints.
Reid Spencer663e7112007-02-28 17:36:23 +0000165 if (Lower == Upper)
166 return *this;
Reid Spencer663e7112007-02-28 17:36:23 +0000167 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000168}
Chris Lattner645e00d2002-09-01 23:53:36 +0000169
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//
Reid Spencer663e7112007-02-28 17:36:23 +0000174ConstantRange
175ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
Reid Spencera6e8a952007-03-01 07:54:15 +0000176 const ConstantRange &RHS) {
177 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
Chris Lattner645e00d2002-09-01 23:53:36 +0000178
Chris Lattner645e00d2002-09-01 23:53:36 +0000179 // Check to see if we overlap on the Left side of RHS...
180 //
Reid Spencera6e8a952007-03-01 07:54:15 +0000181 if (RHS.Lower.ult(LHS.Upper)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000182 // We do overlap on the left side of RHS, see if we overlap on the right of
183 // RHS...
Reid Spencera6e8a952007-03-01 07:54:15 +0000184 if (RHS.Upper.ugt(LHS.Lower)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000185 // 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 //
Reid Spencer663e7112007-02-28 17:36:23 +0000188 if (LHS.getSetSize().ult(RHS.getSetSize()))
Chris Lattner645e00d2002-09-01 23:53:36 +0000189 return LHS;
190 else
191 return RHS;
192
193 } else {
194 // No overlap on the right, just on the left.
Reid Spencerdc5c1592007-02-28 18:57:32 +0000195 return ConstantRange(RHS.Lower, LHS.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000196 }
Chris Lattner645e00d2002-09-01 23:53:36 +0000197 } else {
198 // We don't overlap on the left side of RHS, see if we overlap on the right
199 // of RHS...
Reid Spencera6e8a952007-03-01 07:54:15 +0000200 if (RHS.Upper.ugt(LHS.Lower)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000201 // Simple overlap...
Reid Spencerdc5c1592007-02-28 18:57:32 +0000202 return ConstantRange(LHS.Lower, RHS.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000203 } else {
204 // No overlap...
Reid Spencerbb626a62007-02-28 22:02:48 +0000205 return ConstantRange(LHS.getBitWidth(), false);
Chris Lattner645e00d2002-09-01 23:53:36 +0000206 }
207 }
208}
209
Nick Lewycky3e051642007-02-11 00:58:49 +0000210/// intersectWith - Return the range that results from the intersection of this
Chris Lattner645e00d2002-09-01 23:53:36 +0000211/// range with another range.
212///
Reid Spencera6e8a952007-03-01 07:54:15 +0000213ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000214 assert(getBitWidth() == CR.getBitWidth() &&
215 "ConstantRange types don't agree!");
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000216 // Handle common special cases
Reid Spencer663e7112007-02-28 17:36:23 +0000217 if (isEmptySet() || CR.isFullSet())
218 return *this;
219 if (isFullSet() || CR.isEmptySet())
220 return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000221
Reid Spencera6e8a952007-03-01 07:54:15 +0000222 if (!isWrappedSet()) {
223 if (!CR.isWrappedSet()) {
Reid Spencer663e7112007-02-28 17:36:23 +0000224 using namespace APIntOps;
Reid Spencera6e8a952007-03-01 07:54:15 +0000225 APInt L = umax(Lower, CR.Lower);
226 APInt U = umin(Upper, CR.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000227
Reid Spencera6e8a952007-03-01 07:54:15 +0000228 if (L.ult(U)) // If range isn't empty...
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000229 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000230 else
Reid Spencerbb626a62007-02-28 22:02:48 +0000231 return ConstantRange(getBitWidth(), false);// Otherwise, empty set
Chris Lattner645e00d2002-09-01 23:53:36 +0000232 } else
Reid Spencera6e8a952007-03-01 07:54:15 +0000233 return intersect1Wrapped(CR, *this);
Chris Lattner645e00d2002-09-01 23:53:36 +0000234 } else { // We know "this" is wrapped...
Reid Spencera6e8a952007-03-01 07:54:15 +0000235 if (!CR.isWrappedSet())
236 return intersect1Wrapped(*this, CR);
Chris Lattner645e00d2002-09-01 23:53:36 +0000237 else {
238 // Both ranges are wrapped...
Reid Spencer663e7112007-02-28 17:36:23 +0000239 using namespace APIntOps;
Reid Spencera6e8a952007-03-01 07:54:15 +0000240 APInt L = umax(Lower, CR.Lower);
241 APInt U = umin(Upper, CR.Upper);
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000242 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000243 }
244 }
245 return *this;
246}
247
Nick Lewycky377b1192007-07-14 02:51:34 +0000248/// maximalIntersectWith - Return the range that results from the intersection
249/// of this range with another range. The resultant range is guaranteed to
Nick Lewycky28753f82007-07-14 17:41:03 +0000250/// 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).
Nick Lewycky377b1192007-07-14 02:51:34 +0000254ConstantRange 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
Nick Lewycky3e051642007-02-11 00:58:49 +0000331/// unionWith - Return the range that results from the union of this range with
Chris Lattner645e00d2002-09-01 23:53:36 +0000332/// another range. The resultant range is guaranteed to include the elements of
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000333/// 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.
Chris Lattner645e00d2002-09-01 23:53:36 +0000336///
Reid Spencera6e8a952007-03-01 07:54:15 +0000337ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000338 assert(getBitWidth() == CR.getBitWidth() &&
339 "ConstantRange types don't agree!");
Chris Lattner645e00d2002-09-01 23:53:36 +0000340
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000341 if ( isFullSet() || CR.isEmptySet()) return *this;
342 if (CR.isFullSet() || isEmptySet()) return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000343
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000344 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
345
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000346 APInt L = Lower, U = Upper;
347
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000348 if (!isWrappedSet() && !CR.isWrappedSet()) {
349 if (CR.Lower.ult(L))
350 L = CR.Lower;
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000351
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000352 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 }
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000409
410 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000411}
Chris Lattner96f9d722002-09-02 00:18:22 +0000412
Chris Lattnerfc33d302004-03-30 00:20:08 +0000413/// zeroExtend - Return a new range in the specified integer type, which must
414/// be strictly larger than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000415/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000416/// zero extended.
Reid Spencerbb626a62007-02-28 22:02:48 +0000417ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
418 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000419 assert(SrcTySize < DstTySize && "Not a value extension");
Reid Spencerdc5c1592007-02-28 18:57:32 +0000420 if (isFullSet())
Chris Lattnerfc33d302004-03-30 00:20:08 +0000421 // Change a source full set into [0, 1 << 8*numbytes)
Reid Spencerdc5c1592007-02-28 18:57:32 +0000422 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000423
Reid Spencer663e7112007-02-28 17:36:23 +0000424 APInt L = Lower; L.zext(DstTySize);
425 APInt U = Upper; U.zext(DstTySize);
426 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000427}
428
Nick Lewyckye32157c2007-04-07 15:41:33 +0000429/// 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
Chris Lattnerfc33d302004-03-30 00:20:08 +0000446/// truncate - Return a new range in the specified integer type, which must be
447/// strictly smaller than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000448/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000449/// truncated to the specified type.
Reid Spencerbb626a62007-02-28 22:02:48 +0000450ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
451 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000452 assert(SrcTySize > DstTySize && "Not a value truncation");
Zhou Shengdaacf222007-04-13 05:57:32 +0000453 APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
Reid Spencer663e7112007-02-28 17:36:23 +0000454 if (isFullSet() || getSetSize().ugt(Size))
Reid Spencerbb626a62007-02-28 22:02:48 +0000455 return ConstantRange(DstTySize);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000456
Reid Spencer663e7112007-02-28 17:36:23 +0000457 APInt L = Lower; L.trunc(DstTySize);
458 APInt U = Upper; U.trunc(DstTySize);
459 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000460}
461
Chris Lattner96f9d722002-09-02 00:18:22 +0000462/// print - Print out the bounds to a stream...
463///
Chris Lattner944fac72008-08-23 22:23:09 +0000464void ConstantRange::print(raw_ostream &OS) const {
Chris Lattnerfad86b02008-08-17 07:19:36 +0000465 OS << "[" << Lower << "," << Upper << ")";
Chris Lattner96f9d722002-09-02 00:18:22 +0000466}
467
468/// dump - Allow printing from a debugger easily...
469///
470void ConstantRange::dump() const {
Chris Lattner944fac72008-08-23 22:23:09 +0000471 print(errs());
Chris Lattner96f9d722002-09-02 00:18:22 +0000472}