blob: 4f132e5ed5fd2900f949c5031fcb80b6e00afbea [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//
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.
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"
Bill Wendling6f81b512006-11-28 22:46:12 +000025#include "llvm/Support/Streams.h"
26#include <ostream>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner645e00d2002-09-01 23:53:36 +000029/// Initialize a full (the default) or empty set for the specified type.
30///
Reid Spencerbb626a62007-02-28 22:02:48 +000031ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) :
32 Lower(BitWidth, 0), Upper(BitWidth, 0) {
Chris Lattner645e00d2002-09-01 23:53:36 +000033 if (Full)
Reid Spencer663e7112007-02-28 17:36:23 +000034 Lower = Upper = APInt::getMaxValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000035 else
Reid Spencer663e7112007-02-28 17:36:23 +000036 Lower = Upper = APInt::getMinValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000037}
38
Chris Lattnerfc33d302004-03-30 00:20:08 +000039/// Initialize a range to hold the single specified value.
40///
Reid Spencerdc5c1592007-02-28 18:57:32 +000041ConstantRange::ConstantRange(const APInt & V) : Lower(V), Upper(V + 1) { }
Chris Lattner645e00d2002-09-01 23:53:36 +000042
Reid Spencer663e7112007-02-28 17:36:23 +000043ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
44 Lower(L), Upper(U) {
45 assert(L.getBitWidth() == U.getBitWidth() &&
46 "ConstantRange with unequal bit widths");
Zhou Shengc125c002007-04-26 16:42:07 +000047 assert((L != U || (L.isMaxValue() || L.isMinValue())) &&
Reid Spencer663e7112007-02-28 17:36:23 +000048 "Lower == Upper, but they aren't min or max value!");
49}
50
Chris Lattner645e00d2002-09-01 23:53:36 +000051/// isFullSet - Return true if this set contains all of the elements possible
52/// for this data-type
53bool ConstantRange::isFullSet() const {
Zhou Shengc125c002007-04-26 16:42:07 +000054 return Lower == Upper && Lower.isMaxValue();
Chris Lattner645e00d2002-09-01 23:53:36 +000055}
Misha Brukmanfd939082005-04-21 23:48:37 +000056
Chris Lattner645e00d2002-09-01 23:53:36 +000057/// isEmptySet - Return true if this set contains no members.
58///
59bool ConstantRange::isEmptySet() const {
Zhou Shengc125c002007-04-26 16:42:07 +000060 return Lower == Upper && Lower.isMinValue();
Chris Lattner645e00d2002-09-01 23:53:36 +000061}
62
63/// isWrappedSet - Return true if this set wraps around the top of the range,
64/// for example: [100, 8)
65///
Reid Spencera6e8a952007-03-01 07:54:15 +000066bool ConstantRange::isWrappedSet() const {
Reid Spencer663e7112007-02-28 17:36:23 +000067 return Lower.ugt(Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +000068}
69
Chris Lattner645e00d2002-09-01 23:53:36 +000070/// getSetSize - Return the number of elements in this set.
71///
Reid Spencer663e7112007-02-28 17:36:23 +000072APInt ConstantRange::getSetSize() const {
73 if (isEmptySet())
Reid Spencerbb626a62007-02-28 22:02:48 +000074 return APInt(getBitWidth(), 0);
75 if (getBitWidth() == 1) {
Chris Lattner645e00d2002-09-01 23:53:36 +000076 if (Lower != Upper) // One of T or F in the set...
Reid Spencerbb626a62007-02-28 22:02:48 +000077 return APInt(2, 1);
78 return APInt(2, 2); // Must be full set...
Chris Lattner645e00d2002-09-01 23:53:36 +000079 }
Misha Brukmanfd939082005-04-21 23:48:37 +000080
Chris Lattner645e00d2002-09-01 23:53:36 +000081 // Simply subtract the bounds...
Reid Spencer663e7112007-02-28 17:36:23 +000082 return Upper - Lower;
Chris Lattner645e00d2002-09-01 23:53:36 +000083}
84
Nick Lewycky3400e6a2007-03-10 15:54:12 +000085/// getUnsignedMax - Return the largest unsigned value contained in the
86/// ConstantRange.
87///
88APInt 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///
98APInt 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///
108APInt ConstantRange::getSignedMax() const {
Zhou Shengdaacf222007-04-13 05:57:32 +0000109 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000110 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000111 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000112 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///
130APInt ConstantRange::getSignedMin() const {
Zhou Shengdaacf222007-04-13 05:57:32 +0000131 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000132 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000133 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000134 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
Chris Lattnerfc33d302004-03-30 00:20:08 +0000149/// contains - Return true if the specified value is in the set.
150///
Reid Spencera6e8a952007-03-01 07:54:15 +0000151bool ConstantRange::contains(const APInt &V) const {
152 if (Lower == Upper)
153 return isFullSet();
Chris Lattner645e00d2002-09-01 23:53:36 +0000154
Reid Spencera6e8a952007-03-01 07:54:15 +0000155 if (!isWrappedSet())
156 return Lower.ule(V) && V.ult(Upper);
Reid Spencer663e7112007-02-28 17:36:23 +0000157 else
158 return Lower.ule(V) || V.ult(Upper);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000159}
160
Chris Lattnerfc33d302004-03-30 00:20:08 +0000161/// subtract - Subtract the specified constant from the endpoints of this
162/// constant range.
Reid Spencer581b0d42007-02-28 19:57:34 +0000163ConstantRange ConstantRange::subtract(const APInt &Val) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000164 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
Chris Lattnerfc33d302004-03-30 00:20:08 +0000165 // If the set is empty or full, don't modify the endpoints.
Reid Spencer663e7112007-02-28 17:36:23 +0000166 if (Lower == Upper)
167 return *this;
Reid Spencer663e7112007-02-28 17:36:23 +0000168 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000169}
Chris Lattner645e00d2002-09-01 23:53:36 +0000170
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//
Reid Spencer663e7112007-02-28 17:36:23 +0000175ConstantRange
176ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
Reid Spencera6e8a952007-03-01 07:54:15 +0000177 const ConstantRange &RHS) {
178 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
Chris Lattner645e00d2002-09-01 23:53:36 +0000179
Chris Lattner645e00d2002-09-01 23:53:36 +0000180 // Check to see if we overlap on the Left side of RHS...
181 //
Reid Spencera6e8a952007-03-01 07:54:15 +0000182 if (RHS.Lower.ult(LHS.Upper)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000183 // We do overlap on the left side of RHS, see if we overlap on the right of
184 // RHS...
Reid Spencera6e8a952007-03-01 07:54:15 +0000185 if (RHS.Upper.ugt(LHS.Lower)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000186 // 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 //
Reid Spencer663e7112007-02-28 17:36:23 +0000189 if (LHS.getSetSize().ult(RHS.getSetSize()))
Chris Lattner645e00d2002-09-01 23:53:36 +0000190 return LHS;
191 else
192 return RHS;
193
194 } else {
195 // No overlap on the right, just on the left.
Reid Spencerdc5c1592007-02-28 18:57:32 +0000196 return ConstantRange(RHS.Lower, LHS.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000197 }
Chris Lattner645e00d2002-09-01 23:53:36 +0000198 } else {
199 // We don't overlap on the left side of RHS, see if we overlap on the right
200 // of RHS...
Reid Spencera6e8a952007-03-01 07:54:15 +0000201 if (RHS.Upper.ugt(LHS.Lower)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000202 // Simple overlap...
Reid Spencerdc5c1592007-02-28 18:57:32 +0000203 return ConstantRange(LHS.Lower, RHS.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000204 } else {
205 // No overlap...
Reid Spencerbb626a62007-02-28 22:02:48 +0000206 return ConstantRange(LHS.getBitWidth(), false);
Chris Lattner645e00d2002-09-01 23:53:36 +0000207 }
208 }
209}
210
Nick Lewycky3e051642007-02-11 00:58:49 +0000211/// intersectWith - Return the range that results from the intersection of this
Chris Lattner645e00d2002-09-01 23:53:36 +0000212/// range with another range.
213///
Reid Spencera6e8a952007-03-01 07:54:15 +0000214ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000215 assert(getBitWidth() == CR.getBitWidth() &&
216 "ConstantRange types don't agree!");
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000217 // Handle common special cases
Reid Spencer663e7112007-02-28 17:36:23 +0000218 if (isEmptySet() || CR.isFullSet())
219 return *this;
220 if (isFullSet() || CR.isEmptySet())
221 return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000222
Reid Spencera6e8a952007-03-01 07:54:15 +0000223 if (!isWrappedSet()) {
224 if (!CR.isWrappedSet()) {
Reid Spencer663e7112007-02-28 17:36:23 +0000225 using namespace APIntOps;
Reid Spencera6e8a952007-03-01 07:54:15 +0000226 APInt L = umax(Lower, CR.Lower);
227 APInt U = umin(Upper, CR.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000228
Reid Spencera6e8a952007-03-01 07:54:15 +0000229 if (L.ult(U)) // If range isn't empty...
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000230 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000231 else
Reid Spencerbb626a62007-02-28 22:02:48 +0000232 return ConstantRange(getBitWidth(), false);// Otherwise, empty set
Chris Lattner645e00d2002-09-01 23:53:36 +0000233 } else
Reid Spencera6e8a952007-03-01 07:54:15 +0000234 return intersect1Wrapped(CR, *this);
Chris Lattner645e00d2002-09-01 23:53:36 +0000235 } else { // We know "this" is wrapped...
Reid Spencera6e8a952007-03-01 07:54:15 +0000236 if (!CR.isWrappedSet())
237 return intersect1Wrapped(*this, CR);
Chris Lattner645e00d2002-09-01 23:53:36 +0000238 else {
239 // Both ranges are wrapped...
Reid Spencer663e7112007-02-28 17:36:23 +0000240 using namespace APIntOps;
Reid Spencera6e8a952007-03-01 07:54:15 +0000241 APInt L = umax(Lower, CR.Lower);
242 APInt U = umin(Upper, CR.Upper);
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000243 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000244 }
245 }
246 return *this;
247}
248
Nick Lewycky377b1192007-07-14 02:51:34 +0000249/// maximalIntersectWith - Return the range that results from the intersection
250/// of this range with another range. The resultant range is guaranteed to
251/// include all elements contained in both input ranges, and is also guaranteed
252/// to be the smallest possible set that does so.
253ConstantRange ConstantRange::maximalIntersectWith(const ConstantRange &CR) const {
254 assert(getBitWidth() == CR.getBitWidth() &&
255 "ConstantRange types don't agree!");
256
257 // Handle common cases.
258 if ( isEmptySet() || CR.isFullSet()) return *this;
259 if (CR.isEmptySet() || isFullSet()) return CR;
260
261 if (!isWrappedSet() && CR.isWrappedSet())
262 return CR.maximalIntersectWith(*this);
263
264 if (!isWrappedSet() && !CR.isWrappedSet()) {
265 if (Lower.ult(CR.Lower)) {
266 if (Upper.ule(CR.Lower))
267 return ConstantRange(getBitWidth(), false);
268
269 if (Upper.ult(CR.Upper))
270 return ConstantRange(CR.Lower, Upper);
271
272 return CR;
273 } else {
274 if (Upper.ult(CR.Upper))
275 return *this;
276
277 if (Lower.ult(CR.Upper))
278 return ConstantRange(Lower, CR.Upper);
279
280 return ConstantRange(getBitWidth(), false);
281 }
282 }
283
284 if (isWrappedSet() && !CR.isWrappedSet()) {
285 if (CR.Lower.ult(Upper)) {
286 if (CR.Upper.ult(Upper))
287 return CR;
288
289 if (CR.Upper.ult(Lower))
290 return ConstantRange(CR.Lower, Upper);
291
292 if (getSetSize().ult(CR.getSetSize()))
293 return *this;
294 else
295 return CR;
296 } else if (CR.Lower.ult(Lower)) {
297 if (CR.Upper.ule(Lower))
298 return ConstantRange(getBitWidth(), false);
299
300 return ConstantRange(Lower, CR.Upper);
301 }
302 return CR;
303 }
304
305 if (CR.Upper.ult(Upper)) {
306 if (CR.Lower.ult(Upper)) {
307 if (getSetSize().ult(CR.getSetSize()))
308 return *this;
309 else
310 return CR;
311 }
312
313 if (CR.Lower.ult(Lower))
314 return ConstantRange(Lower, CR.Upper);
315
316 return CR;
317 } else if (CR.Upper.ult(Lower)) {
318 if (CR.Lower.ult(Lower))
319 return *this;
320
321 return ConstantRange(CR.Lower, Upper);
322 }
323 if (getSetSize().ult(CR.getSetSize()))
324 return *this;
325 else
326 return CR;
327}
328
329
Nick Lewycky3e051642007-02-11 00:58:49 +0000330/// unionWith - Return the range that results from the union of this range with
Chris Lattner645e00d2002-09-01 23:53:36 +0000331/// another range. The resultant range is guaranteed to include the elements of
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000332/// both sets, but may contain more. For example, [3, 9) union [12,15) is
333/// [3, 15), which includes 9, 10, and 11, which were not included in either
334/// set before.
Chris Lattner645e00d2002-09-01 23:53:36 +0000335///
Reid Spencera6e8a952007-03-01 07:54:15 +0000336ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000337 assert(getBitWidth() == CR.getBitWidth() &&
338 "ConstantRange types don't agree!");
Chris Lattner645e00d2002-09-01 23:53:36 +0000339
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000340 if ( isFullSet() || CR.isEmptySet()) return *this;
341 if (CR.isFullSet() || isEmptySet()) return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000342
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000343 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
344
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000345 APInt L = Lower, U = Upper;
346
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000347 if (!isWrappedSet() && !CR.isWrappedSet()) {
348 if (CR.Lower.ult(L))
349 L = CR.Lower;
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000350
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000351 if (CR.Upper.ugt(U))
352 U = CR.Upper;
353 }
354
355 if (isWrappedSet() && !CR.isWrappedSet()) {
356 if ((CR.Lower.ult(Upper) && CR.Upper.ult(Upper)) ||
357 (CR.Lower.ugt(Lower) && CR.Upper.ugt(Lower))) {
358 return *this;
359 }
360
361 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) {
362 return ConstantRange(getBitWidth());
363 }
364
365 if (CR.Lower.ule(Upper) && CR.Upper.ule(Lower)) {
366 APInt d1 = CR.Upper - Upper, d2 = Lower - CR.Upper;
367 if (d1.ult(d2)) {
368 U = CR.Upper;
369 } else {
370 L = CR.Upper;
371 }
372 }
373
374 if (Upper.ult(CR.Lower) && CR.Upper.ult(Lower)) {
375 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
376 if (d1.ult(d2)) {
377 U = CR.Lower + 1;
378 } else {
379 L = CR.Upper - 1;
380 }
381 }
382
383 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) {
384 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Lower;
385
386 if (d1.ult(d2)) {
387 U = CR.Lower + 1;
388 } else {
389 L = CR.Lower;
390 }
391 }
392 }
393
394 if (isWrappedSet() && CR.isWrappedSet()) {
395 if (Lower.ult(CR.Upper) || CR.Lower.ult(Upper))
396 return ConstantRange(getBitWidth());
397
398 if (CR.Upper.ugt(U)) {
399 U = CR.Upper;
400 }
401
402 if (CR.Lower.ult(L)) {
403 L = CR.Lower;
404 }
405
406 if (L == U) return ConstantRange(getBitWidth());
407 }
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000408
409 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000410}
Chris Lattner96f9d722002-09-02 00:18:22 +0000411
Chris Lattnerfc33d302004-03-30 00:20:08 +0000412/// zeroExtend - Return a new range in the specified integer type, which must
413/// be strictly larger than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000414/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000415/// zero extended.
Reid Spencerbb626a62007-02-28 22:02:48 +0000416ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
417 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000418 assert(SrcTySize < DstTySize && "Not a value extension");
Reid Spencerdc5c1592007-02-28 18:57:32 +0000419 if (isFullSet())
Chris Lattnerfc33d302004-03-30 00:20:08 +0000420 // Change a source full set into [0, 1 << 8*numbytes)
Reid Spencerdc5c1592007-02-28 18:57:32 +0000421 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000422
Reid Spencer663e7112007-02-28 17:36:23 +0000423 APInt L = Lower; L.zext(DstTySize);
424 APInt U = Upper; U.zext(DstTySize);
425 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000426}
427
Nick Lewyckye32157c2007-04-07 15:41:33 +0000428/// signExtend - Return a new range in the specified integer type, which must
429/// be strictly larger than the current type. The returned range will
430/// correspond to the possible range of values as if the source range had been
431/// sign extended.
432ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
433 unsigned SrcTySize = getBitWidth();
434 assert(SrcTySize < DstTySize && "Not a value extension");
435 if (isFullSet()) {
436 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
437 APInt::getLowBitsSet(DstTySize, SrcTySize-1));
438 }
439
440 APInt L = Lower; L.sext(DstTySize);
441 APInt U = Upper; U.sext(DstTySize);
442 return ConstantRange(L, U);
443}
444
Chris Lattnerfc33d302004-03-30 00:20:08 +0000445/// truncate - Return a new range in the specified integer type, which must be
446/// strictly smaller than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000447/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000448/// truncated to the specified type.
Reid Spencerbb626a62007-02-28 22:02:48 +0000449ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
450 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000451 assert(SrcTySize > DstTySize && "Not a value truncation");
Zhou Shengdaacf222007-04-13 05:57:32 +0000452 APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
Reid Spencer663e7112007-02-28 17:36:23 +0000453 if (isFullSet() || getSetSize().ugt(Size))
Reid Spencerbb626a62007-02-28 22:02:48 +0000454 return ConstantRange(DstTySize);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000455
Reid Spencer663e7112007-02-28 17:36:23 +0000456 APInt L = Lower; L.trunc(DstTySize);
457 APInt U = Upper; U.trunc(DstTySize);
458 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000459}
460
Chris Lattner96f9d722002-09-02 00:18:22 +0000461/// print - Print out the bounds to a stream...
462///
463void ConstantRange::print(std::ostream &OS) const {
Reid Spencer663e7112007-02-28 17:36:23 +0000464 OS << "[" << Lower.toStringSigned(10) << ","
465 << Upper.toStringSigned(10) << " )";
Chris Lattner96f9d722002-09-02 00:18:22 +0000466}
467
468/// dump - Allow printing from a debugger easily...
469///
470void ConstantRange::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000471 print(cerr);
Chris Lattner96f9d722002-09-02 00:18:22 +0000472}