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/Constants.h"
25 #include "llvm/Support/ConstantRange.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Instructions.h"
31 /// Initialize a full (the default) or empty set for the specified type.
33 ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) {
35 Lower = Upper = APInt::getMaxValue(BitWidth);
37 Lower = Upper = APInt::getMinValue(BitWidth);
40 /// Initialize a range to hold the single specified value.
42 ConstantRange::ConstantRange(const APInt &V) : Lower(V), Upper(V + 1) {}
44 ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
46 assert(L.getBitWidth() == U.getBitWidth() &&
47 "ConstantRange with unequal bit widths");
48 assert((L != U || (L.isMaxValue() || L.isMinValue())) &&
49 "Lower == Upper, but they aren't min or max value!");
52 ConstantRange ConstantRange::makeICmpRegion(unsigned Pred,
53 const ConstantRange &CR) {
54 uint32_t W = CR.getBitWidth();
56 default: assert(!"Invalid ICmp predicate to makeICmpRegion()");
57 case ICmpInst::ICMP_EQ:
59 case ICmpInst::ICMP_NE:
60 if (CR.isSingleElement())
61 return ConstantRange(CR.getUpper(), CR.getLower());
62 return ConstantRange(W);
63 case ICmpInst::ICMP_ULT:
64 return ConstantRange(APInt::getMinValue(W), CR.getUnsignedMax());
65 case ICmpInst::ICMP_SLT:
66 return ConstantRange(APInt::getSignedMinValue(W), CR.getSignedMax());
67 case ICmpInst::ICMP_ULE: {
68 APInt UMax(CR.getUnsignedMax());
69 if (UMax.isMaxValue())
70 return ConstantRange(W);
71 return ConstantRange(APInt::getMinValue(W), UMax + 1);
73 case ICmpInst::ICMP_SLE: {
74 APInt SMax(CR.getSignedMax());
75 if (SMax.isMaxSignedValue() || (SMax+1).isMaxSignedValue())
76 return ConstantRange(W);
77 return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
79 case ICmpInst::ICMP_UGT:
80 return ConstantRange(CR.getUnsignedMin() + 1, APInt::getNullValue(W));
81 case ICmpInst::ICMP_SGT:
82 return ConstantRange(CR.getSignedMin() + 1,
83 APInt::getSignedMinValue(W));
84 case ICmpInst::ICMP_UGE: {
85 APInt UMin(CR.getUnsignedMin());
86 if (UMin.isMinValue())
87 return ConstantRange(W);
88 return ConstantRange(UMin, APInt::getNullValue(W));
90 case ICmpInst::ICMP_SGE: {
91 APInt SMin(CR.getSignedMin());
92 if (SMin.isMinSignedValue())
93 return ConstantRange(W);
94 return ConstantRange(SMin, APInt::getSignedMinValue(W));
99 /// isFullSet - Return true if this set contains all of the elements possible
100 /// for this data-type
101 bool ConstantRange::isFullSet() const {
102 return Lower == Upper && Lower.isMaxValue();
105 /// isEmptySet - Return true if this set contains no members.
107 bool ConstantRange::isEmptySet() const {
108 return Lower == Upper && Lower.isMinValue();
111 /// isWrappedSet - Return true if this set wraps around the top of the range,
112 /// for example: [100, 8)
114 bool ConstantRange::isWrappedSet() const {
115 return Lower.ugt(Upper);
118 /// isSignWrappedSet - Return true if this set wraps around the INT_MIN of
119 /// its bitwidth, for example: i8 [120, 140).
121 bool ConstantRange::isSignWrappedSet() const {
122 return contains(APInt::getSignedMaxValue(getBitWidth())) &&
123 contains(APInt::getSignedMinValue(getBitWidth()));
126 /// getSetSize - Return the number of elements in this set.
128 APInt ConstantRange::getSetSize() const {
130 return APInt(getBitWidth(), 0);
131 if (getBitWidth() == 1) {
132 if (Lower != Upper) // One of T or F in the set...
134 return APInt(2, 2); // Must be full set...
137 // Simply subtract the bounds...
138 return Upper - Lower;
141 /// getUnsignedMax - Return the largest unsigned value contained in the
144 APInt ConstantRange::getUnsignedMax() const {
145 if (isFullSet() || isWrappedSet())
146 return APInt::getMaxValue(getBitWidth());
148 return getUpper() - 1;
151 /// getUnsignedMin - Return the smallest unsigned value contained in the
154 APInt ConstantRange::getUnsignedMin() const {
155 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
156 return APInt::getMinValue(getBitWidth());
161 /// getSignedMax - Return the largest signed value contained in the
164 APInt ConstantRange::getSignedMax() const {
165 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
166 if (!isWrappedSet()) {
167 if (getLower().sle(getUpper() - 1))
168 return getUpper() - 1;
172 if (getLower().isNegative() == getUpper().isNegative())
175 return getUpper() - 1;
179 /// getSignedMin - Return the smallest signed value contained in the
182 APInt ConstantRange::getSignedMin() const {
183 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
184 if (!isWrappedSet()) {
185 if (getLower().sle(getUpper() - 1))
190 if ((getUpper() - 1).slt(getLower())) {
191 if (getUpper() != SignedMin)
201 /// contains - Return true if the specified value is in the set.
203 bool ConstantRange::contains(const APInt &V) const {
208 return Lower.ule(V) && V.ult(Upper);
210 return Lower.ule(V) || V.ult(Upper);
213 /// contains - Return true if the argument is a subset of this range.
214 /// Two equal sets contain each other. The empty set contained by all other
217 bool ConstantRange::contains(const ConstantRange &Other) const {
218 if (isFullSet() || Other.isEmptySet()) return true;
219 if (isEmptySet() || Other.isFullSet()) return false;
221 if (!isWrappedSet()) {
222 if (Other.isWrappedSet())
225 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
228 if (!Other.isWrappedSet())
229 return Other.getUpper().ule(Upper) ||
230 Lower.ule(Other.getLower());
232 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
235 /// subtract - Subtract the specified constant from the endpoints of this
237 ConstantRange ConstantRange::subtract(const APInt &Val) const {
238 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
239 // If the set is empty or full, don't modify the endpoints.
242 return ConstantRange(Lower - Val, Upper - Val);
245 /// intersectWith - Return the range that results from the intersection of this
246 /// range with another range. The resultant range is guaranteed to include all
247 /// elements contained in both input ranges, and to have the smallest possible
248 /// set size that does so. Because there may be two intersections with the
249 /// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
250 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
251 assert(getBitWidth() == CR.getBitWidth() &&
252 "ConstantRange types don't agree!");
254 // Handle common cases.
255 if ( isEmptySet() || CR.isFullSet()) return *this;
256 if (CR.isEmptySet() || isFullSet()) return CR;
258 if (!isWrappedSet() && CR.isWrappedSet())
259 return CR.intersectWith(*this);
261 if (!isWrappedSet() && !CR.isWrappedSet()) {
262 if (Lower.ult(CR.Lower)) {
263 if (Upper.ule(CR.Lower))
264 return ConstantRange(getBitWidth(), false);
266 if (Upper.ult(CR.Upper))
267 return ConstantRange(CR.Lower, Upper);
271 if (Upper.ult(CR.Upper))
274 if (Lower.ult(CR.Upper))
275 return ConstantRange(Lower, CR.Upper);
277 return ConstantRange(getBitWidth(), false);
281 if (isWrappedSet() && !CR.isWrappedSet()) {
282 if (CR.Lower.ult(Upper)) {
283 if (CR.Upper.ult(Upper))
286 if (CR.Upper.ult(Lower))
287 return ConstantRange(CR.Lower, Upper);
289 if (getSetSize().ult(CR.getSetSize()))
293 } else if (CR.Lower.ult(Lower)) {
294 if (CR.Upper.ule(Lower))
295 return ConstantRange(getBitWidth(), false);
297 return ConstantRange(Lower, CR.Upper);
302 if (CR.Upper.ult(Upper)) {
303 if (CR.Lower.ult(Upper)) {
304 if (getSetSize().ult(CR.getSetSize()))
310 if (CR.Lower.ult(Lower))
311 return ConstantRange(Lower, CR.Upper);
314 } else if (CR.Upper.ult(Lower)) {
315 if (CR.Lower.ult(Lower))
318 return ConstantRange(CR.Lower, Upper);
320 if (getSetSize().ult(CR.getSetSize()))
327 /// unionWith - Return the range that results from the union of this range with
328 /// another range. The resultant range is guaranteed to include the elements of
329 /// both sets, but may contain more. For example, [3, 9) union [12,15) is
330 /// [3, 15), which includes 9, 10, and 11, which were not included in either
333 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
334 assert(getBitWidth() == CR.getBitWidth() &&
335 "ConstantRange types don't agree!");
337 if ( isFullSet() || CR.isEmptySet()) return *this;
338 if (CR.isFullSet() || isEmptySet()) return CR;
340 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
342 if (!isWrappedSet() && !CR.isWrappedSet()) {
343 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
344 // If the two ranges are disjoint, find the smaller gap and bridge it.
345 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
347 return ConstantRange(Lower, CR.Upper);
349 return ConstantRange(CR.Lower, Upper);
352 APInt L = Lower, U = Upper;
355 if ((CR.Upper - 1).ugt(U - 1))
358 if (L == 0 && U == 0)
359 return ConstantRange(getBitWidth());
361 return ConstantRange(L, U);
364 if (!CR.isWrappedSet()) {
365 // ------U L----- and ------U L----- : this
367 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
370 // ------U L----- : this
372 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
373 return ConstantRange(getBitWidth());
375 // ----U L---- : this
378 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
379 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
381 return ConstantRange(Lower, CR.Upper);
383 return ConstantRange(CR.Lower, Upper);
386 // ----U L----- : this
388 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
389 return ConstantRange(CR.Lower, Upper);
391 // ------U L---- : this
393 if (CR.Lower.ult(Upper) && CR.Upper.ult(Lower))
394 return ConstantRange(Lower, CR.Upper);
397 assert(isWrappedSet() && CR.isWrappedSet() &&
398 "ConstantRange::unionWith missed wrapped union unwrapped case");
400 // ------U L---- and ------U L---- : this
401 // -U L----------- and ------------U L : CR
402 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
403 return ConstantRange(getBitWidth());
405 APInt L = Lower, U = Upper;
411 return ConstantRange(L, U);
414 /// zeroExtend - Return a new range in the specified integer type, which must
415 /// be strictly larger than the current type. The returned range will
416 /// correspond to the possible range of values as if the source range had been
418 ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
419 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
421 unsigned SrcTySize = getBitWidth();
422 assert(SrcTySize < DstTySize && "Not a value extension");
423 if (isFullSet() || isWrappedSet())
424 // Change into [0, 1 << src bit width)
425 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
427 APInt L = Lower; L.zext(DstTySize);
428 APInt U = Upper; U.zext(DstTySize);
429 return ConstantRange(L, U);
432 /// signExtend - Return a new range in the specified integer type, which must
433 /// be strictly larger than the current type. The returned range will
434 /// correspond to the possible range of values as if the source range had been
436 ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
437 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
439 unsigned SrcTySize = getBitWidth();
440 assert(SrcTySize < DstTySize && "Not a value extension");
441 if (isFullSet() || isSignWrappedSet()) {
442 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
443 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
446 APInt L = Lower; L.sext(DstTySize);
447 APInt U = Upper; U.sext(DstTySize);
448 return ConstantRange(L, U);
451 /// truncate - Return a new range in the specified integer type, which must be
452 /// strictly smaller than the current type. The returned range will
453 /// correspond to the possible range of values as if the source range had been
454 /// truncated to the specified type.
455 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
456 unsigned SrcTySize = getBitWidth();
457 assert(SrcTySize > DstTySize && "Not a value truncation");
458 APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
459 if (isFullSet() || getSetSize().ugt(Size))
460 return ConstantRange(DstTySize, /*isFullSet=*/true);
462 APInt L = Lower; L.trunc(DstTySize);
463 APInt U = Upper; U.trunc(DstTySize);
464 return ConstantRange(L, U);
467 /// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
468 /// value is zero extended, truncated, or left alone to make it that width.
469 ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
470 unsigned SrcTySize = getBitWidth();
471 if (SrcTySize > DstTySize)
472 return truncate(DstTySize);
473 else if (SrcTySize < DstTySize)
474 return zeroExtend(DstTySize);
479 /// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
480 /// value is sign extended, truncated, or left alone to make it that width.
481 ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
482 unsigned SrcTySize = getBitWidth();
483 if (SrcTySize > DstTySize)
484 return truncate(DstTySize);
485 else if (SrcTySize < DstTySize)
486 return signExtend(DstTySize);
492 ConstantRange::add(const ConstantRange &Other) const {
493 if (isEmptySet() || Other.isEmptySet())
494 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
495 if (isFullSet() || Other.isFullSet())
496 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
498 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
499 APInt NewLower = getLower() + Other.getLower();
500 APInt NewUpper = getUpper() + Other.getUpper() - 1;
501 if (NewLower == NewUpper)
502 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
504 ConstantRange X = ConstantRange(NewLower, NewUpper);
505 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
506 // We've wrapped, therefore, full set.
507 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
513 ConstantRange::sub(const ConstantRange &Other) const {
514 if (isEmptySet() || Other.isEmptySet())
515 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
516 if (isFullSet() || Other.isFullSet())
517 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
519 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
520 APInt NewLower = getLower() - Other.getLower();
521 APInt NewUpper = getUpper() - Other.getUpper() + 1;
522 if (NewLower == NewUpper)
523 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
525 ConstantRange X = ConstantRange(NewLower, NewUpper);
526 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
527 // We've wrapped, therefore, full set.
528 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
534 ConstantRange::multiply(const ConstantRange &Other) const {
535 // TODO: If either operand is a single element and the multiply is known to
536 // be non-wrapping, round the result min and max value to the appropriate
537 // multiple of that element. If wrapping is possible, at least adjust the
538 // range according to the greatest power-of-two factor of the single element.
540 if (isEmptySet() || Other.isEmptySet())
541 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
542 if (isFullSet() || Other.isFullSet())
543 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
545 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
546 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
547 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
548 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
550 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
551 this_max * Other_max + 1);
552 return Result_zext.truncate(getBitWidth());
556 ConstantRange::smax(const ConstantRange &Other) const {
557 // X smax Y is: range(smax(X_smin, Y_smin),
558 // smax(X_smax, Y_smax))
559 if (isEmptySet() || Other.isEmptySet())
560 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
561 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
562 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
564 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
565 return ConstantRange(NewL, NewU);
569 ConstantRange::umax(const ConstantRange &Other) const {
570 // X umax Y is: range(umax(X_umin, Y_umin),
571 // umax(X_umax, Y_umax))
572 if (isEmptySet() || Other.isEmptySet())
573 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
574 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
575 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
577 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
578 return ConstantRange(NewL, NewU);
582 ConstantRange::udiv(const ConstantRange &RHS) const {
583 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
584 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
586 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
588 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
590 APInt RHS_umin = RHS.getUnsignedMin();
592 // We want the lowest value in RHS excluding zero. Usually that would be 1
593 // except for a range in the form of [X, 1) in which case it would be X.
594 if (RHS.getUpper() == 1)
595 RHS_umin = RHS.getLower();
597 RHS_umin = APInt(getBitWidth(), 1);
600 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
602 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
605 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
607 return ConstantRange(Lower, Upper);
611 ConstantRange::shl(const ConstantRange &Other) const {
612 if (isEmptySet() || Other.isEmptySet())
613 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
615 APInt min = getUnsignedMin().shl(Other.getUnsignedMin());
616 APInt max = getUnsignedMax().shl(Other.getUnsignedMax());
618 // there's no overflow!
619 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
620 if (Zeros.ugt(Other.getUnsignedMax()))
621 return ConstantRange(min, max + 1);
623 // FIXME: implement the other tricky cases
624 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
628 ConstantRange::lshr(const ConstantRange &Other) const {
629 if (isEmptySet() || Other.isEmptySet())
630 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
632 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin());
633 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
635 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
637 return ConstantRange(min, max + 1);
640 ConstantRange ConstantRange::inverse() const {
642 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
643 } else if (isEmptySet()) {
644 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
646 return ConstantRange(Upper, Lower);
649 /// print - Print out the bounds to a stream...
651 void ConstantRange::print(raw_ostream &OS) const {
654 else if (isEmptySet())
657 OS << "[" << Lower << "," << Upper << ")";
660 /// dump - Allow printing from a debugger easily...
662 void ConstantRange::dump() const {