1 //===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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):
17 // [F, F) = {} = Empty set
20 // [T, T) = {F, T} = Full set
22 //===----------------------------------------------------------------------===//
24 #include "llvm/Support/ConstantRange.h"
25 #include "llvm/Support/raw_ostream.h"
28 /// Initialize a full (the default) or empty set for the specified type.
30 ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) {
32 Lower = Upper = APInt::getMaxValue(BitWidth);
34 Lower = Upper = APInt::getMinValue(BitWidth);
37 /// Initialize a range to hold the single specified value.
39 ConstantRange::ConstantRange(const APInt & V) : Lower(V), Upper(V + 1) {}
41 ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
43 assert(L.getBitWidth() == U.getBitWidth() &&
44 "ConstantRange with unequal bit widths");
45 assert((L != U || (L.isMaxValue() || L.isMinValue())) &&
46 "Lower == Upper, but they aren't min or max value!");
49 /// isFullSet - Return true if this set contains all of the elements possible
50 /// for this data-type
51 bool ConstantRange::isFullSet() const {
52 return Lower == Upper && Lower.isMaxValue();
55 /// isEmptySet - Return true if this set contains no members.
57 bool ConstantRange::isEmptySet() const {
58 return Lower == Upper && Lower.isMinValue();
61 /// isWrappedSet - Return true if this set wraps around the top of the range,
62 /// for example: [100, 8)
64 bool ConstantRange::isWrappedSet() const {
65 return Lower.ugt(Upper);
68 /// getSetSize - Return the number of elements in this set.
70 APInt ConstantRange::getSetSize() const {
72 return APInt(getBitWidth(), 0);
73 if (getBitWidth() == 1) {
74 if (Lower != Upper) // One of T or F in the set...
76 return APInt(2, 2); // Must be full set...
79 // Simply subtract the bounds...
83 /// getUnsignedMax - Return the largest unsigned value contained in the
86 APInt ConstantRange::getUnsignedMax() const {
87 if (isFullSet() || isWrappedSet())
88 return APInt::getMaxValue(getBitWidth());
90 return getUpper() - 1;
93 /// getUnsignedMin - Return the smallest unsigned value contained in the
96 APInt ConstantRange::getUnsignedMin() const {
97 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
98 return APInt::getMinValue(getBitWidth());
103 /// getSignedMax - Return the largest signed value contained in the
106 APInt ConstantRange::getSignedMax() const {
107 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
108 if (!isWrappedSet()) {
109 if (getLower().sle(getUpper() - 1))
110 return getUpper() - 1;
114 if ((getUpper() - 1).slt(getLower())) {
115 if (getLower() != SignedMax)
118 return getUpper() - 1;
120 return getUpper() - 1;
125 /// getSignedMin - Return the smallest signed value contained in the
128 APInt ConstantRange::getSignedMin() const {
129 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
130 if (!isWrappedSet()) {
131 if (getLower().sle(getUpper() - 1))
136 if ((getUpper() - 1).slt(getLower())) {
137 if (getUpper() != SignedMin)
147 /// contains - Return true if the specified value is in the set.
149 bool ConstantRange::contains(const APInt &V) const {
154 return Lower.ule(V) && V.ult(Upper);
156 return Lower.ule(V) || V.ult(Upper);
159 /// subtract - Subtract the specified constant from the endpoints of this
161 ConstantRange ConstantRange::subtract(const APInt &Val) const {
162 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
163 // If the set is empty or full, don't modify the endpoints.
166 return ConstantRange(Lower - Val, Upper - Val);
170 // intersect1Wrapped - This helper function is used to intersect two ranges when
171 // it is known that LHS is wrapped and RHS isn't.
174 ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
175 const ConstantRange &RHS) {
176 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
178 // Check to see if we overlap on the Left side of RHS...
180 if (RHS.Lower.ult(LHS.Upper)) {
181 // We do overlap on the left side of RHS, see if we overlap on the right of
183 if (RHS.Upper.ugt(LHS.Lower)) {
184 // 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...
187 if (LHS.getSetSize().ult(RHS.getSetSize()))
193 // No overlap on the right, just on the left.
194 return ConstantRange(RHS.Lower, LHS.Upper);
197 // We don't overlap on the left side of RHS, see if we overlap on the right
199 if (RHS.Upper.ugt(LHS.Lower)) {
201 return ConstantRange(LHS.Lower, RHS.Upper);
204 return ConstantRange(LHS.getBitWidth(), false);
209 /// intersectWith - Return the range that results from the intersection of this
210 /// range with another range.
212 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
213 assert(getBitWidth() == CR.getBitWidth() &&
214 "ConstantRange types don't agree!");
215 // Handle common special cases
216 if (isEmptySet() || CR.isFullSet())
218 if (isFullSet() || CR.isEmptySet())
221 if (!isWrappedSet()) {
222 if (!CR.isWrappedSet()) {
223 APInt L = APIntOps::umax(Lower, CR.Lower);
224 APInt U = APIntOps::umin(Upper, CR.Upper);
226 if (L.ult(U)) // If range isn't empty...
227 return ConstantRange(L, U);
229 return ConstantRange(getBitWidth(), false);// Otherwise, empty set
231 return intersect1Wrapped(CR, *this);
232 } else { // We know "this" is wrapped...
233 if (!CR.isWrappedSet())
234 return intersect1Wrapped(*this, CR);
236 // Both ranges are wrapped...
237 APInt L = APIntOps::umax(Lower, CR.Lower);
238 APInt U = APIntOps::umin(Upper, CR.Upper);
239 return ConstantRange(L, U);
245 /// maximalIntersectWith - Return the range that results from the intersection
246 /// of this range with another range. The resultant range is guaranteed to
247 /// 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).
252 ConstantRange::maximalIntersectWith(const ConstantRange &CR) const {
253 assert(getBitWidth() == CR.getBitWidth() &&
254 "ConstantRange types don't agree!");
256 // Handle common cases.
257 if ( isEmptySet() || CR.isFullSet()) return *this;
258 if (CR.isEmptySet() || isFullSet()) return CR;
260 if (!isWrappedSet() && CR.isWrappedSet())
261 return CR.maximalIntersectWith(*this);
263 if (!isWrappedSet() && !CR.isWrappedSet()) {
264 if (Lower.ult(CR.Lower)) {
265 if (Upper.ule(CR.Lower))
266 return ConstantRange(getBitWidth(), false);
268 if (Upper.ult(CR.Upper))
269 return ConstantRange(CR.Lower, Upper);
273 if (Upper.ult(CR.Upper))
276 if (Lower.ult(CR.Upper))
277 return ConstantRange(Lower, CR.Upper);
279 return ConstantRange(getBitWidth(), false);
283 if (isWrappedSet() && !CR.isWrappedSet()) {
284 if (CR.Lower.ult(Upper)) {
285 if (CR.Upper.ult(Upper))
288 if (CR.Upper.ult(Lower))
289 return ConstantRange(CR.Lower, Upper);
291 if (getSetSize().ult(CR.getSetSize()))
295 } else if (CR.Lower.ult(Lower)) {
296 if (CR.Upper.ule(Lower))
297 return ConstantRange(getBitWidth(), false);
299 return ConstantRange(Lower, CR.Upper);
304 if (CR.Upper.ult(Upper)) {
305 if (CR.Lower.ult(Upper)) {
306 if (getSetSize().ult(CR.getSetSize()))
312 if (CR.Lower.ult(Lower))
313 return ConstantRange(Lower, CR.Upper);
316 } else if (CR.Upper.ult(Lower)) {
317 if (CR.Lower.ult(Lower))
320 return ConstantRange(CR.Lower, Upper);
322 if (getSetSize().ult(CR.getSetSize()))
329 /// unionWith - Return the range that results from the union of this range with
330 /// another range. The resultant range is guaranteed to include the elements of
331 /// 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
335 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
336 assert(getBitWidth() == CR.getBitWidth() &&
337 "ConstantRange types don't agree!");
339 if ( isFullSet() || CR.isEmptySet()) return *this;
340 if (CR.isFullSet() || isEmptySet()) return CR;
342 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
344 APInt L = Lower, U = Upper;
346 if (!isWrappedSet() && !CR.isWrappedSet()) {
354 if (isWrappedSet() && !CR.isWrappedSet()) {
355 if ((CR.Lower.ult(Upper) && CR.Upper.ult(Upper)) ||
356 (CR.Lower.ugt(Lower) && CR.Upper.ugt(Lower))) {
360 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) {
361 return ConstantRange(getBitWidth());
364 if (CR.Lower.ule(Upper) && CR.Upper.ule(Lower)) {
365 APInt d1 = CR.Upper - Upper, d2 = Lower - CR.Upper;
373 if (Upper.ult(CR.Lower) && CR.Upper.ult(Lower)) {
374 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
382 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) {
383 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Lower;
393 if (isWrappedSet() && CR.isWrappedSet()) {
394 if (Lower.ult(CR.Upper) || CR.Lower.ult(Upper))
395 return ConstantRange(getBitWidth());
397 if (CR.Upper.ugt(U)) {
401 if (CR.Lower.ult(L)) {
405 if (L == U) return ConstantRange(getBitWidth());
408 return ConstantRange(L, U);
411 /// zeroExtend - Return a new range in the specified integer type, which must
412 /// be strictly larger than the current type. The returned range will
413 /// correspond to the possible range of values as if the source range had been
415 ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
416 unsigned SrcTySize = getBitWidth();
417 assert(SrcTySize < DstTySize && "Not a value extension");
419 // Change a source full set into [0, 1 << 8*numbytes)
420 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
422 APInt L = Lower; L.zext(DstTySize);
423 APInt U = Upper; U.zext(DstTySize);
424 return ConstantRange(L, U);
427 /// 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
431 ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
432 unsigned SrcTySize = getBitWidth();
433 assert(SrcTySize < DstTySize && "Not a value extension");
435 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
436 APInt::getLowBitsSet(DstTySize, SrcTySize-1));
439 APInt L = Lower; L.sext(DstTySize);
440 APInt U = Upper; U.sext(DstTySize);
441 return ConstantRange(L, U);
444 /// truncate - Return a new range in the specified integer type, which must be
445 /// strictly smaller than the current type. The returned range will
446 /// correspond to the possible range of values as if the source range had been
447 /// truncated to the specified type.
448 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
449 unsigned SrcTySize = getBitWidth();
450 assert(SrcTySize > DstTySize && "Not a value truncation");
451 APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
452 if (isFullSet() || getSetSize().ugt(Size))
453 return ConstantRange(DstTySize);
455 APInt L = Lower; L.trunc(DstTySize);
456 APInt U = Upper; U.trunc(DstTySize);
457 return ConstantRange(L, U);
461 ConstantRange::add(const ConstantRange &Other) const {
462 if (isEmptySet() || Other.isEmptySet())
463 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
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);
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);
480 ConstantRange::multiply(const ConstantRange &Other) const {
481 // TODO: Implement multiply.
482 return ConstantRange(getBitWidth(),
483 !(isEmptySet() || Other.isEmptySet()));
487 ConstantRange::smax(const ConstantRange &Other) const {
488 // 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;
497 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
498 return ConstantRange(NewL, NewU);
502 ConstantRange::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;
512 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
513 return ConstantRange(NewL, NewU);
517 ConstantRange::udiv(const ConstantRange &Other) const {
518 // TODO: Implement udiv.
519 return ConstantRange(getBitWidth(),
520 !(isEmptySet() || Other.isEmptySet()));
523 /// print - Print out the bounds to a stream...
525 void ConstantRange::print(raw_ostream &OS) const {
526 OS << "[" << Lower << "," << Upper << ")";
529 /// dump - Allow printing from a debugger easily...
531 void ConstantRange::dump() const {
535 std::ostream &llvm::operator<<(std::ostream &o,
536 const ConstantRange &CR) {
537 raw_os_ostream OS(o);