blob: 809a02411569c3629967964393d0ef5276f2b8cb [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///
Dan Gohman38b06442009-07-09 23:16:10 +000030ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) {
Chris Lattner645e00d2002-09-01 23:53:36 +000031 if (Full)
Reid Spencer663e7112007-02-28 17:36:23 +000032 Lower = Upper = APInt::getMaxValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000033 else
Reid Spencer663e7112007-02-28 17:36:23 +000034 Lower = Upper = APInt::getMinValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000035}
36
Chris Lattnerfc33d302004-03-30 00:20:08 +000037/// Initialize a range to hold the single specified value.
38///
Dan Gohman38b06442009-07-09 23:16:10 +000039ConstantRange::ConstantRange(const APInt & V) : Lower(V), Upper(V + 1) {}
Chris Lattner645e00d2002-09-01 23:53:36 +000040
Dan Gohman38b06442009-07-09 23:16:10 +000041ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
42 Lower(L), Upper(U) {
43 assert(L.getBitWidth() == U.getBitWidth() &&
44 "ConstantRange with unequal bit widths");
Zhou Shengc125c002007-04-26 16:42:07 +000045 assert((L != U || (L.isMaxValue() || L.isMinValue())) &&
Reid Spencer663e7112007-02-28 17:36:23 +000046 "Lower == Upper, but they aren't min or max value!");
47}
48
Chris Lattner645e00d2002-09-01 23:53:36 +000049/// isFullSet - Return true if this set contains all of the elements possible
50/// for this data-type
51bool ConstantRange::isFullSet() const {
Zhou Shengc125c002007-04-26 16:42:07 +000052 return Lower == Upper && Lower.isMaxValue();
Chris Lattner645e00d2002-09-01 23:53:36 +000053}
Misha Brukmanfd939082005-04-21 23:48:37 +000054
Chris Lattner645e00d2002-09-01 23:53:36 +000055/// isEmptySet - Return true if this set contains no members.
56///
57bool ConstantRange::isEmptySet() const {
Zhou Shengc125c002007-04-26 16:42:07 +000058 return Lower == Upper && Lower.isMinValue();
Chris Lattner645e00d2002-09-01 23:53:36 +000059}
60
61/// isWrappedSet - Return true if this set wraps around the top of the range,
62/// for example: [100, 8)
63///
Reid Spencera6e8a952007-03-01 07:54:15 +000064bool ConstantRange::isWrappedSet() const {
Reid Spencer663e7112007-02-28 17:36:23 +000065 return Lower.ugt(Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +000066}
67
Chris Lattner645e00d2002-09-01 23:53:36 +000068/// getSetSize - Return the number of elements in this set.
69///
Reid Spencer663e7112007-02-28 17:36:23 +000070APInt ConstantRange::getSetSize() const {
71 if (isEmptySet())
Reid Spencerbb626a62007-02-28 22:02:48 +000072 return APInt(getBitWidth(), 0);
73 if (getBitWidth() == 1) {
Chris Lattner645e00d2002-09-01 23:53:36 +000074 if (Lower != Upper) // One of T or F in the set...
Reid Spencerbb626a62007-02-28 22:02:48 +000075 return APInt(2, 1);
76 return APInt(2, 2); // Must be full set...
Chris Lattner645e00d2002-09-01 23:53:36 +000077 }
Misha Brukmanfd939082005-04-21 23:48:37 +000078
Chris Lattner645e00d2002-09-01 23:53:36 +000079 // Simply subtract the bounds...
Reid Spencer663e7112007-02-28 17:36:23 +000080 return Upper - Lower;
Chris Lattner645e00d2002-09-01 23:53:36 +000081}
82
Nick Lewycky3400e6a2007-03-10 15:54:12 +000083/// getUnsignedMax - Return the largest unsigned value contained in the
84/// ConstantRange.
85///
86APInt ConstantRange::getUnsignedMax() const {
87 if (isFullSet() || isWrappedSet())
88 return APInt::getMaxValue(getBitWidth());
89 else
90 return getUpper() - 1;
91}
92
93/// getUnsignedMin - Return the smallest unsigned value contained in the
94/// ConstantRange.
95///
96APInt ConstantRange::getUnsignedMin() const {
97 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
98 return APInt::getMinValue(getBitWidth());
99 else
100 return getLower();
101}
102
103/// getSignedMax - Return the largest signed value contained in the
104/// ConstantRange.
105///
106APInt ConstantRange::getSignedMax() const {
Zhou Shengdaacf222007-04-13 05:57:32 +0000107 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000108 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000109 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000110 return getUpper() - 1;
111 else
112 return SignedMax;
113 } else {
114 if ((getUpper() - 1).slt(getLower())) {
115 if (getLower() != SignedMax)
116 return SignedMax;
117 else
118 return getUpper() - 1;
119 } else {
120 return getUpper() - 1;
121 }
122 }
123}
124
125/// getSignedMin - Return the smallest signed value contained in the
126/// ConstantRange.
127///
128APInt ConstantRange::getSignedMin() const {
Zhou Shengdaacf222007-04-13 05:57:32 +0000129 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000130 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000131 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000132 return getLower();
133 else
134 return SignedMin;
135 } else {
136 if ((getUpper() - 1).slt(getLower())) {
137 if (getUpper() != SignedMin)
138 return SignedMin;
139 else
140 return getLower();
141 } else {
142 return getLower();
143 }
144 }
145}
146
Chris Lattnerfc33d302004-03-30 00:20:08 +0000147/// contains - Return true if the specified value is in the set.
148///
Reid Spencera6e8a952007-03-01 07:54:15 +0000149bool ConstantRange::contains(const APInt &V) const {
150 if (Lower == Upper)
151 return isFullSet();
Chris Lattner645e00d2002-09-01 23:53:36 +0000152
Reid Spencera6e8a952007-03-01 07:54:15 +0000153 if (!isWrappedSet())
154 return Lower.ule(V) && V.ult(Upper);
Reid Spencer663e7112007-02-28 17:36:23 +0000155 else
156 return Lower.ule(V) || V.ult(Upper);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000157}
158
Chris Lattnerfc33d302004-03-30 00:20:08 +0000159/// subtract - Subtract the specified constant from the endpoints of this
160/// constant range.
Reid Spencer581b0d42007-02-28 19:57:34 +0000161ConstantRange ConstantRange::subtract(const APInt &Val) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000162 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
Chris Lattnerfc33d302004-03-30 00:20:08 +0000163 // If the set is empty or full, don't modify the endpoints.
Reid Spencer663e7112007-02-28 17:36:23 +0000164 if (Lower == Upper)
165 return *this;
Reid Spencer663e7112007-02-28 17:36:23 +0000166 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000167}
Chris Lattner645e00d2002-09-01 23:53:36 +0000168
169
170// intersect1Wrapped - This helper function is used to intersect two ranges when
171// it is known that LHS is wrapped and RHS isn't.
172//
Reid Spencer663e7112007-02-28 17:36:23 +0000173ConstantRange
174ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
Reid Spencera6e8a952007-03-01 07:54:15 +0000175 const ConstantRange &RHS) {
176 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
Chris Lattner645e00d2002-09-01 23:53:36 +0000177
Chris Lattner645e00d2002-09-01 23:53:36 +0000178 // Check to see if we overlap on the Left side of RHS...
179 //
Reid Spencera6e8a952007-03-01 07:54:15 +0000180 if (RHS.Lower.ult(LHS.Upper)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000181 // We do overlap on the left side of RHS, see if we overlap on the right of
182 // RHS...
Reid Spencera6e8a952007-03-01 07:54:15 +0000183 if (RHS.Upper.ugt(LHS.Lower)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000184 // Ok, the result overlaps on both the left and right sides. See if the
185 // resultant interval will be smaller if we wrap or not...
186 //
Reid Spencer663e7112007-02-28 17:36:23 +0000187 if (LHS.getSetSize().ult(RHS.getSetSize()))
Chris Lattner645e00d2002-09-01 23:53:36 +0000188 return LHS;
189 else
190 return RHS;
191
192 } else {
193 // No overlap on the right, just on the left.
Reid Spencerdc5c1592007-02-28 18:57:32 +0000194 return ConstantRange(RHS.Lower, LHS.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000195 }
Chris Lattner645e00d2002-09-01 23:53:36 +0000196 } else {
197 // We don't overlap on the left side of RHS, see if we overlap on the right
198 // of RHS...
Reid Spencera6e8a952007-03-01 07:54:15 +0000199 if (RHS.Upper.ugt(LHS.Lower)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000200 // Simple overlap...
Reid Spencerdc5c1592007-02-28 18:57:32 +0000201 return ConstantRange(LHS.Lower, RHS.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000202 } else {
203 // No overlap...
Reid Spencerbb626a62007-02-28 22:02:48 +0000204 return ConstantRange(LHS.getBitWidth(), false);
Chris Lattner645e00d2002-09-01 23:53:36 +0000205 }
206 }
207}
208
Nick Lewycky3e051642007-02-11 00:58:49 +0000209/// intersectWith - Return the range that results from the intersection of this
Chris Lattner645e00d2002-09-01 23:53:36 +0000210/// range with another range.
211///
Reid Spencera6e8a952007-03-01 07:54:15 +0000212ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000213 assert(getBitWidth() == CR.getBitWidth() &&
214 "ConstantRange types don't agree!");
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000215 // Handle common special cases
Reid Spencer663e7112007-02-28 17:36:23 +0000216 if (isEmptySet() || CR.isFullSet())
217 return *this;
218 if (isFullSet() || CR.isEmptySet())
219 return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000220
Reid Spencera6e8a952007-03-01 07:54:15 +0000221 if (!isWrappedSet()) {
222 if (!CR.isWrappedSet()) {
Dan Gohmana3755d82009-07-09 22:07:27 +0000223 APInt L = APIntOps::umax(Lower, CR.Lower);
224 APInt U = APIntOps::umin(Upper, CR.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000225
Reid Spencera6e8a952007-03-01 07:54:15 +0000226 if (L.ult(U)) // If range isn't empty...
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000227 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000228 else
Reid Spencerbb626a62007-02-28 22:02:48 +0000229 return ConstantRange(getBitWidth(), false);// Otherwise, empty set
Chris Lattner645e00d2002-09-01 23:53:36 +0000230 } else
Reid Spencera6e8a952007-03-01 07:54:15 +0000231 return intersect1Wrapped(CR, *this);
Chris Lattner645e00d2002-09-01 23:53:36 +0000232 } else { // We know "this" is wrapped...
Reid Spencera6e8a952007-03-01 07:54:15 +0000233 if (!CR.isWrappedSet())
234 return intersect1Wrapped(*this, CR);
Chris Lattner645e00d2002-09-01 23:53:36 +0000235 else {
236 // Both ranges are wrapped...
Dan Gohmana3755d82009-07-09 22:07:27 +0000237 APInt L = APIntOps::umax(Lower, CR.Lower);
238 APInt U = APIntOps::umin(Upper, CR.Upper);
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000239 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000240 }
241 }
242 return *this;
243}
244
Nick Lewycky377b1192007-07-14 02:51:34 +0000245/// maximalIntersectWith - Return the range that results from the intersection
246/// of this range with another range. The resultant range is guaranteed to
Nick Lewycky28753f82007-07-14 17:41:03 +0000247/// include all elements contained in both input ranges, and to have the
248/// smallest possible set size that does so. Because there may be two
249/// intersections with the same set size, A.maximalIntersectWith(B) might not
250/// be equal to B.maximalIntersect(A).
Dan Gohmana3755d82009-07-09 22:07:27 +0000251ConstantRange
252ConstantRange::maximalIntersectWith(const ConstantRange &CR) const {
Nick Lewycky377b1192007-07-14 02:51:34 +0000253 assert(getBitWidth() == CR.getBitWidth() &&
254 "ConstantRange types don't agree!");
255
256 // Handle common cases.
257 if ( isEmptySet() || CR.isFullSet()) return *this;
258 if (CR.isEmptySet() || isFullSet()) return CR;
259
260 if (!isWrappedSet() && CR.isWrappedSet())
261 return CR.maximalIntersectWith(*this);
262
263 if (!isWrappedSet() && !CR.isWrappedSet()) {
264 if (Lower.ult(CR.Lower)) {
265 if (Upper.ule(CR.Lower))
266 return ConstantRange(getBitWidth(), false);
267
268 if (Upper.ult(CR.Upper))
269 return ConstantRange(CR.Lower, Upper);
270
271 return CR;
272 } else {
273 if (Upper.ult(CR.Upper))
274 return *this;
275
276 if (Lower.ult(CR.Upper))
277 return ConstantRange(Lower, CR.Upper);
278
279 return ConstantRange(getBitWidth(), false);
280 }
281 }
282
283 if (isWrappedSet() && !CR.isWrappedSet()) {
284 if (CR.Lower.ult(Upper)) {
285 if (CR.Upper.ult(Upper))
286 return CR;
287
288 if (CR.Upper.ult(Lower))
289 return ConstantRange(CR.Lower, Upper);
290
291 if (getSetSize().ult(CR.getSetSize()))
292 return *this;
293 else
294 return CR;
295 } else if (CR.Lower.ult(Lower)) {
296 if (CR.Upper.ule(Lower))
297 return ConstantRange(getBitWidth(), false);
298
299 return ConstantRange(Lower, CR.Upper);
300 }
301 return CR;
302 }
303
304 if (CR.Upper.ult(Upper)) {
305 if (CR.Lower.ult(Upper)) {
306 if (getSetSize().ult(CR.getSetSize()))
307 return *this;
308 else
309 return CR;
310 }
311
312 if (CR.Lower.ult(Lower))
313 return ConstantRange(Lower, CR.Upper);
314
315 return CR;
316 } else if (CR.Upper.ult(Lower)) {
317 if (CR.Lower.ult(Lower))
318 return *this;
319
320 return ConstantRange(CR.Lower, Upper);
321 }
322 if (getSetSize().ult(CR.getSetSize()))
323 return *this;
324 else
325 return CR;
326}
327
328
Nick Lewycky3e051642007-02-11 00:58:49 +0000329/// unionWith - Return the range that results from the union of this range with
Chris Lattner645e00d2002-09-01 23:53:36 +0000330/// another range. The resultant range is guaranteed to include the elements of
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000331/// both sets, but may contain more. For example, [3, 9) union [12,15) is
332/// [3, 15), which includes 9, 10, and 11, which were not included in either
333/// set before.
Chris Lattner645e00d2002-09-01 23:53:36 +0000334///
Reid Spencera6e8a952007-03-01 07:54:15 +0000335ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000336 assert(getBitWidth() == CR.getBitWidth() &&
337 "ConstantRange types don't agree!");
Chris Lattner645e00d2002-09-01 23:53:36 +0000338
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000339 if ( isFullSet() || CR.isEmptySet()) return *this;
340 if (CR.isFullSet() || isEmptySet()) return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000341
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000342 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
343
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000344 APInt L = Lower, U = Upper;
345
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000346 if (!isWrappedSet() && !CR.isWrappedSet()) {
347 if (CR.Lower.ult(L))
348 L = CR.Lower;
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000349
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000350 if (CR.Upper.ugt(U))
351 U = CR.Upper;
352 }
353
354 if (isWrappedSet() && !CR.isWrappedSet()) {
355 if ((CR.Lower.ult(Upper) && CR.Upper.ult(Upper)) ||
356 (CR.Lower.ugt(Lower) && CR.Upper.ugt(Lower))) {
357 return *this;
358 }
359
360 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) {
361 return ConstantRange(getBitWidth());
362 }
363
364 if (CR.Lower.ule(Upper) && CR.Upper.ule(Lower)) {
365 APInt d1 = CR.Upper - Upper, d2 = Lower - CR.Upper;
366 if (d1.ult(d2)) {
367 U = CR.Upper;
368 } else {
369 L = CR.Upper;
370 }
371 }
372
373 if (Upper.ult(CR.Lower) && CR.Upper.ult(Lower)) {
374 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
375 if (d1.ult(d2)) {
376 U = CR.Lower + 1;
377 } else {
378 L = CR.Upper - 1;
379 }
380 }
381
382 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) {
383 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Lower;
384
385 if (d1.ult(d2)) {
386 U = CR.Lower + 1;
387 } else {
388 L = CR.Lower;
389 }
390 }
391 }
392
393 if (isWrappedSet() && CR.isWrappedSet()) {
394 if (Lower.ult(CR.Upper) || CR.Lower.ult(Upper))
395 return ConstantRange(getBitWidth());
396
397 if (CR.Upper.ugt(U)) {
398 U = CR.Upper;
399 }
400
401 if (CR.Lower.ult(L)) {
402 L = CR.Lower;
403 }
404
405 if (L == U) return ConstantRange(getBitWidth());
406 }
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000407
408 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000409}
Chris Lattner96f9d722002-09-02 00:18:22 +0000410
Chris Lattnerfc33d302004-03-30 00:20:08 +0000411/// zeroExtend - Return a new range in the specified integer type, which must
412/// be strictly larger than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000413/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000414/// zero extended.
Reid Spencerbb626a62007-02-28 22:02:48 +0000415ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
416 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000417 assert(SrcTySize < DstTySize && "Not a value extension");
Reid Spencerdc5c1592007-02-28 18:57:32 +0000418 if (isFullSet())
Chris Lattnerfc33d302004-03-30 00:20:08 +0000419 // Change a source full set into [0, 1 << 8*numbytes)
Reid Spencerdc5c1592007-02-28 18:57:32 +0000420 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000421
Reid Spencer663e7112007-02-28 17:36:23 +0000422 APInt L = Lower; L.zext(DstTySize);
423 APInt U = Upper; U.zext(DstTySize);
424 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000425}
426
Nick Lewyckye32157c2007-04-07 15:41:33 +0000427/// signExtend - Return a new range in the specified integer type, which must
428/// be strictly larger than the current type. The returned range will
429/// correspond to the possible range of values as if the source range had been
430/// sign extended.
431ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
432 unsigned SrcTySize = getBitWidth();
433 assert(SrcTySize < DstTySize && "Not a value extension");
434 if (isFullSet()) {
435 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
436 APInt::getLowBitsSet(DstTySize, SrcTySize-1));
437 }
438
439 APInt L = Lower; L.sext(DstTySize);
440 APInt U = Upper; U.sext(DstTySize);
441 return ConstantRange(L, U);
442}
443
Chris Lattnerfc33d302004-03-30 00:20:08 +0000444/// truncate - Return a new range in the specified integer type, which must be
445/// strictly smaller than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000446/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000447/// truncated to the specified type.
Reid Spencerbb626a62007-02-28 22:02:48 +0000448ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
449 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000450 assert(SrcTySize > DstTySize && "Not a value truncation");
Zhou Shengdaacf222007-04-13 05:57:32 +0000451 APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
Reid Spencer663e7112007-02-28 17:36:23 +0000452 if (isFullSet() || getSetSize().ugt(Size))
Reid Spencerbb626a62007-02-28 22:02:48 +0000453 return ConstantRange(DstTySize);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000454
Reid Spencer663e7112007-02-28 17:36:23 +0000455 APInt L = Lower; L.trunc(DstTySize);
456 APInt U = Upper; U.trunc(DstTySize);
457 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000458}
459
Dan Gohmana3755d82009-07-09 22:07:27 +0000460ConstantRange
461ConstantRange::add(const ConstantRange &Other) const {
462 if (isEmptySet() || Other.isEmptySet())
463 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
464
465 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
466 APInt NewLower = getLower() + Other.getLower();
467 APInt NewUpper = getUpper() + Other.getUpper() - 1;
468 if (NewLower == NewUpper)
469 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
470
471 ConstantRange X = ConstantRange(NewLower, NewUpper);
472 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
473 // We've wrapped, therefore, full set.
474 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
475
476 return X;
Chris Lattner96f9d722002-09-02 00:18:22 +0000477}
478
Dan Gohmana3755d82009-07-09 22:07:27 +0000479ConstantRange
480ConstantRange::multiply(const ConstantRange &Other) const {
481 // TODO: Implement multiply.
482 return ConstantRange(getBitWidth(),
483 !(isEmptySet() || Other.isEmptySet()));
484}
485
486ConstantRange
487ConstantRange::smax(const ConstantRange &Other) const {
Dan Gohman38b06442009-07-09 23:16:10 +0000488 // X smax Y is: range(smax(X_smin, Y_smin),
489 // smax(X_smax, Y_smax))
490 if (isEmptySet() || Other.isEmptySet())
491 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
492 if (isFullSet() || Other.isFullSet())
493 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
494 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
495 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
496 if (NewU == NewL)
497 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
498 return ConstantRange(NewL, NewU);
Dan Gohmana3755d82009-07-09 22:07:27 +0000499}
500
501ConstantRange
502ConstantRange::umax(const ConstantRange &Other) const {
503 // X umax Y is: range(umax(X_umin, Y_umin),
504 // umax(X_umax, Y_umax))
505 if (isEmptySet() || Other.isEmptySet())
506 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
507 if (isFullSet() || Other.isFullSet())
508 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
509 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
510 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
511 if (NewU == NewL)
512 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
513 return ConstantRange(NewL, NewU);
514}
515
516ConstantRange
517ConstantRange::udiv(const ConstantRange &Other) const {
518 // TODO: Implement udiv.
519 return ConstantRange(getBitWidth(),
520 !(isEmptySet() || Other.isEmptySet()));
521}
522
Dan Gohman38b06442009-07-09 23:16:10 +0000523/// print - Print out the bounds to a stream...
Chris Lattner96f9d722002-09-02 00:18:22 +0000524///
Dan Gohman38b06442009-07-09 23:16:10 +0000525void ConstantRange::print(raw_ostream &OS) const {
526 OS << "[" << Lower << "," << Upper << ")";
Dan Gohmana3755d82009-07-09 22:07:27 +0000527}
528
Dan Gohman38b06442009-07-09 23:16:10 +0000529/// dump - Allow printing from a debugger easily...
Dan Gohmana3755d82009-07-09 22:07:27 +0000530///
Dan Gohman38b06442009-07-09 23:16:10 +0000531void ConstantRange::dump() const {
532 print(errs());
Dan Gohmana3755d82009-07-09 22:07:27 +0000533}
534
Dan Gohman38b06442009-07-09 23:16:10 +0000535std::ostream &llvm::operator<<(std::ostream &o,
536 const ConstantRange &CR) {
537 raw_os_ostream OS(o);
538 OS << CR;
539 return o;
Chris Lattner96f9d722002-09-02 00:18:22 +0000540}