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/Debug.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Instructions.h"
30 /// Initialize a full (the default) or empty set for the specified type.
32 ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) {
34 Lower = Upper = APInt::getMaxValue(BitWidth);
36 Lower = Upper = APInt::getMinValue(BitWidth);
39 /// Initialize a range to hold the single specified value.
41 ConstantRange::ConstantRange(const APInt & V) : Lower(V), Upper(V + 1) {}
43 ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
45 assert(L.getBitWidth() == U.getBitWidth() &&
46 "ConstantRange with unequal bit widths");
47 assert((L != U || (L.isMaxValue() || L.isMinValue())) &&
48 "Lower == Upper, but they aren't min or max value!");
51 ConstantRange ConstantRange::makeICmpRegion(unsigned Pred,
52 const ConstantRange &CR) {
53 uint32_t W = CR.getBitWidth();
55 default: assert(!"Invalid ICmp predicate to makeICmpRegion()");
56 case ICmpInst::ICMP_EQ:
58 case ICmpInst::ICMP_NE:
59 if (CR.isSingleElement())
60 return ConstantRange(CR.getUpper(), CR.getLower());
61 return ConstantRange(W);
62 case ICmpInst::ICMP_ULT:
63 return ConstantRange(APInt::getMinValue(W), CR.getUnsignedMax());
64 case ICmpInst::ICMP_SLT:
65 return ConstantRange(APInt::getSignedMinValue(W), CR.getSignedMax());
66 case ICmpInst::ICMP_ULE: {
67 APInt UMax(CR.getUnsignedMax());
68 if (UMax.isMaxValue())
69 return ConstantRange(W);
70 return ConstantRange(APInt::getMinValue(W), UMax + 1);
72 case ICmpInst::ICMP_SLE: {
73 APInt SMax(CR.getSignedMax());
74 if (SMax.isMaxSignedValue() || (SMax+1).isMaxSignedValue())
75 return ConstantRange(W);
76 return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
78 case ICmpInst::ICMP_UGT:
79 return ConstantRange(CR.getUnsignedMin() + 1, APInt::getNullValue(W));
80 case ICmpInst::ICMP_SGT:
81 return ConstantRange(CR.getSignedMin() + 1,
82 APInt::getSignedMinValue(W));
83 case ICmpInst::ICMP_UGE: {
84 APInt UMin(CR.getUnsignedMin());
85 if (UMin.isMinValue())
86 return ConstantRange(W);
87 return ConstantRange(UMin, APInt::getNullValue(W));
89 case ICmpInst::ICMP_SGE: {
90 APInt SMin(CR.getSignedMin());
91 if (SMin.isMinSignedValue())
92 return ConstantRange(W);
93 return ConstantRange(SMin, APInt::getSignedMinValue(W));
98 /// isFullSet - Return true if this set contains all of the elements possible
99 /// for this data-type
100 bool ConstantRange::isFullSet() const {
101 return Lower == Upper && Lower.isMaxValue();
104 /// isEmptySet - Return true if this set contains no members.
106 bool ConstantRange::isEmptySet() const {
107 return Lower == Upper && Lower.isMinValue();
110 /// isWrappedSet - Return true if this set wraps around the top of the range,
111 /// for example: [100, 8)
113 bool ConstantRange::isWrappedSet() const {
114 return Lower.ugt(Upper);
117 /// getSetSize - Return the number of elements in this set.
119 APInt ConstantRange::getSetSize() const {
121 return APInt(getBitWidth(), 0);
122 if (getBitWidth() == 1) {
123 if (Lower != Upper) // One of T or F in the set...
125 return APInt(2, 2); // Must be full set...
128 // Simply subtract the bounds...
129 return Upper - Lower;
132 /// getUnsignedMax - Return the largest unsigned value contained in the
135 APInt ConstantRange::getUnsignedMax() const {
136 if (isFullSet() || isWrappedSet())
137 return APInt::getMaxValue(getBitWidth());
139 return getUpper() - 1;
142 /// getUnsignedMin - Return the smallest unsigned value contained in the
145 APInt ConstantRange::getUnsignedMin() const {
146 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
147 return APInt::getMinValue(getBitWidth());
152 /// getSignedMax - Return the largest signed value contained in the
155 APInt ConstantRange::getSignedMax() const {
156 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
157 if (!isWrappedSet()) {
158 if (getLower().sle(getUpper() - 1))
159 return getUpper() - 1;
163 if (getLower().isNegative() == getUpper().isNegative())
166 return getUpper() - 1;
170 /// getSignedMin - Return the smallest signed value contained in the
173 APInt ConstantRange::getSignedMin() const {
174 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
175 if (!isWrappedSet()) {
176 if (getLower().sle(getUpper() - 1))
181 if ((getUpper() - 1).slt(getLower())) {
182 if (getUpper() != SignedMin)
192 /// contains - Return true if the specified value is in the set.
194 bool ConstantRange::contains(const APInt &V) const {
199 return Lower.ule(V) && V.ult(Upper);
201 return Lower.ule(V) || V.ult(Upper);
204 /// contains - Return true if the argument is a subset of this range.
205 /// Two equal set contain each other. The empty set is considered to be
206 /// contained by all other sets.
208 bool ConstantRange::contains(const ConstantRange &Other) const {
209 if (isFullSet()) return true;
210 if (Other.isFullSet()) return false;
211 if (Other.isEmptySet()) return true;
212 if (isEmptySet()) return false;
214 if (!isWrappedSet()) {
215 if (Other.isWrappedSet())
218 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
221 if (!Other.isWrappedSet())
222 return Other.getUpper().ule(Upper) ||
223 Lower.ule(Other.getLower());
225 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
228 /// subtract - Subtract the specified constant from the endpoints of this
230 ConstantRange ConstantRange::subtract(const APInt &Val) const {
231 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
232 // If the set is empty or full, don't modify the endpoints.
235 return ConstantRange(Lower - Val, Upper - Val);
239 // intersect1Wrapped - This helper function is used to intersect two ranges when
240 // it is known that LHS is wrapped and RHS isn't.
243 ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
244 const ConstantRange &RHS) {
245 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
247 // Check to see if we overlap on the Left side of RHS...
249 if (RHS.Lower.ult(LHS.Upper)) {
250 // We do overlap on the left side of RHS, see if we overlap on the right of
252 if (RHS.Upper.ugt(LHS.Lower)) {
253 // Ok, the result overlaps on both the left and right sides. See if the
254 // resultant interval will be smaller if we wrap or not...
256 if (LHS.getSetSize().ult(RHS.getSetSize()))
262 // No overlap on the right, just on the left.
263 return ConstantRange(RHS.Lower, LHS.Upper);
266 // We don't overlap on the left side of RHS, see if we overlap on the right
268 if (RHS.Upper.ugt(LHS.Lower)) {
270 return ConstantRange(LHS.Lower, RHS.Upper);
273 return ConstantRange(LHS.getBitWidth(), false);
278 /// intersectWith - Return the range that results from the intersection of this
279 /// range with another range. The resultant range is guaranteed to include all
280 /// elements contained in both input ranges, and to have the smallest possible
281 /// set size that does so. Because there may be two intersections with the
282 /// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
283 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
284 assert(getBitWidth() == CR.getBitWidth() &&
285 "ConstantRange types don't agree!");
287 // Handle common cases.
288 if ( isEmptySet() || CR.isFullSet()) return *this;
289 if (CR.isEmptySet() || isFullSet()) return CR;
291 if (!isWrappedSet() && CR.isWrappedSet())
292 return CR.intersectWith(*this);
294 if (!isWrappedSet() && !CR.isWrappedSet()) {
295 if (Lower.ult(CR.Lower)) {
296 if (Upper.ule(CR.Lower))
297 return ConstantRange(getBitWidth(), false);
299 if (Upper.ult(CR.Upper))
300 return ConstantRange(CR.Lower, Upper);
304 if (Upper.ult(CR.Upper))
307 if (Lower.ult(CR.Upper))
308 return ConstantRange(Lower, CR.Upper);
310 return ConstantRange(getBitWidth(), false);
314 if (isWrappedSet() && !CR.isWrappedSet()) {
315 if (CR.Lower.ult(Upper)) {
316 if (CR.Upper.ult(Upper))
319 if (CR.Upper.ult(Lower))
320 return ConstantRange(CR.Lower, Upper);
322 if (getSetSize().ult(CR.getSetSize()))
326 } else if (CR.Lower.ult(Lower)) {
327 if (CR.Upper.ule(Lower))
328 return ConstantRange(getBitWidth(), false);
330 return ConstantRange(Lower, CR.Upper);
335 if (CR.Upper.ult(Upper)) {
336 if (CR.Lower.ult(Upper)) {
337 if (getSetSize().ult(CR.getSetSize()))
343 if (CR.Lower.ult(Lower))
344 return ConstantRange(Lower, CR.Upper);
347 } else if (CR.Upper.ult(Lower)) {
348 if (CR.Lower.ult(Lower))
351 return ConstantRange(CR.Lower, Upper);
353 if (getSetSize().ult(CR.getSetSize()))
360 /// unionWith - Return the range that results from the union of this range with
361 /// another range. The resultant range is guaranteed to include the elements of
362 /// both sets, but may contain more. For example, [3, 9) union [12,15) is
363 /// [3, 15), which includes 9, 10, and 11, which were not included in either
366 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
367 assert(getBitWidth() == CR.getBitWidth() &&
368 "ConstantRange types don't agree!");
370 if ( isFullSet() || CR.isEmptySet()) return *this;
371 if (CR.isFullSet() || isEmptySet()) return CR;
373 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
375 if (!isWrappedSet() && !CR.isWrappedSet()) {
376 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
377 // If the two ranges are disjoint, find the smaller gap and bridge it.
378 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
380 return ConstantRange(Lower, CR.Upper);
382 return ConstantRange(CR.Lower, Upper);
385 APInt L = Lower, U = Upper;
388 if ((CR.Upper - 1).ugt(U - 1))
391 if (L == 0 && U == 0)
392 return ConstantRange(getBitWidth());
394 return ConstantRange(L, U);
397 if (!CR.isWrappedSet()) {
398 // ------U L----- and ------U L----- : this
400 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
403 // ------U L----- : this
405 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
406 return ConstantRange(getBitWidth());
408 // ----U L---- : this
411 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
412 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
414 return ConstantRange(Lower, CR.Upper);
416 return ConstantRange(CR.Lower, Upper);
419 // ----U L----- : this
421 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
422 return ConstantRange(CR.Lower, Upper);
424 // ------U L---- : this
426 if (CR.Lower.ult(Upper) && CR.Upper.ult(Lower))
427 return ConstantRange(Lower, CR.Upper);
430 assert(isWrappedSet() && CR.isWrappedSet() &&
431 "ConstantRange::unionWith missed wrapped union unwrapped case");
433 // ------U L---- and ------U L---- : this
434 // -U L----------- and ------------U L : CR
435 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
436 return ConstantRange(getBitWidth());
438 APInt L = Lower, U = Upper;
444 return ConstantRange(L, U);
447 /// zeroExtend - Return a new range in the specified integer type, which must
448 /// be strictly larger than the current type. The returned range will
449 /// correspond to the possible range of values as if the source range had been
451 ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
452 unsigned SrcTySize = getBitWidth();
453 assert(SrcTySize < DstTySize && "Not a value extension");
455 // Change a source full set into [0, 1 << 8*numbytes)
456 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
458 APInt L = Lower; L.zext(DstTySize);
459 APInt U = Upper; U.zext(DstTySize);
460 return ConstantRange(L, U);
463 /// signExtend - Return a new range in the specified integer type, which must
464 /// be strictly larger than the current type. The returned range will
465 /// correspond to the possible range of values as if the source range had been
467 ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
468 unsigned SrcTySize = getBitWidth();
469 assert(SrcTySize < DstTySize && "Not a value extension");
471 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
472 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
475 APInt L = Lower; L.sext(DstTySize);
476 APInt U = Upper; U.sext(DstTySize);
477 return ConstantRange(L, U);
480 /// truncate - Return a new range in the specified integer type, which must be
481 /// strictly smaller than the current type. The returned range will
482 /// correspond to the possible range of values as if the source range had been
483 /// truncated to the specified type.
484 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
485 unsigned SrcTySize = getBitWidth();
486 assert(SrcTySize > DstTySize && "Not a value truncation");
487 APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
488 if (isFullSet() || getSetSize().ugt(Size))
489 return ConstantRange(DstTySize);
491 APInt L = Lower; L.trunc(DstTySize);
492 APInt U = Upper; U.trunc(DstTySize);
493 return ConstantRange(L, U);
496 /// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
497 /// value is zero extended, truncated, or left alone to make it that width.
498 ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
499 unsigned SrcTySize = getBitWidth();
500 if (SrcTySize > DstTySize)
501 return truncate(DstTySize);
502 else if (SrcTySize < DstTySize)
503 return zeroExtend(DstTySize);
508 /// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
509 /// value is sign extended, truncated, or left alone to make it that width.
510 ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
511 unsigned SrcTySize = getBitWidth();
512 if (SrcTySize > DstTySize)
513 return truncate(DstTySize);
514 else if (SrcTySize < DstTySize)
515 return signExtend(DstTySize);
521 ConstantRange::add(const ConstantRange &Other) const {
522 if (isEmptySet() || Other.isEmptySet())
523 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
524 if (isFullSet() || Other.isFullSet())
525 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
527 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
528 APInt NewLower = getLower() + Other.getLower();
529 APInt NewUpper = getUpper() + Other.getUpper() - 1;
530 if (NewLower == NewUpper)
531 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
533 ConstantRange X = ConstantRange(NewLower, NewUpper);
534 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
535 // We've wrapped, therefore, full set.
536 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
542 ConstantRange::multiply(const ConstantRange &Other) const {
543 // TODO: If either operand is a single element and the multiply is known to
544 // be non-wrapping, round the result min and max value to the appropriate
545 // multiple of that element. If wrapping is possible, at least adjust the
546 // range according to the greatest power-of-two factor of the single element.
548 if (isEmptySet() || Other.isEmptySet())
549 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
550 if (isFullSet() || Other.isFullSet())
551 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
553 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
554 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
555 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
556 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
558 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
559 this_max * Other_max + 1);
560 return Result_zext.truncate(getBitWidth());
564 ConstantRange::smax(const ConstantRange &Other) const {
565 // X smax Y is: range(smax(X_smin, Y_smin),
566 // smax(X_smax, Y_smax))
567 if (isEmptySet() || Other.isEmptySet())
568 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
569 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
570 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
572 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
573 return ConstantRange(NewL, NewU);
577 ConstantRange::umax(const ConstantRange &Other) const {
578 // X umax Y is: range(umax(X_umin, Y_umin),
579 // umax(X_umax, Y_umax))
580 if (isEmptySet() || Other.isEmptySet())
581 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
582 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
583 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
585 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
586 return ConstantRange(NewL, NewU);
590 ConstantRange::udiv(const ConstantRange &RHS) const {
591 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
592 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
594 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
596 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
598 APInt RHS_umin = RHS.getUnsignedMin();
600 // We want the lowest value in RHS excluding zero. Usually that would be 1
601 // except for a range in the form of [X, 1) in which case it would be X.
602 if (RHS.getUpper() == 1)
603 RHS_umin = RHS.getLower();
605 RHS_umin = APInt(getBitWidth(), 1);
608 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
610 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
613 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
615 return ConstantRange(Lower, Upper);
619 ConstantRange::shl(const ConstantRange &Amount) const {
623 APInt min = getUnsignedMin() << Amount.getUnsignedMin();
624 APInt max = getUnsignedMax() << Amount.getUnsignedMax();
626 // there's no overflow!
627 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
628 if (Zeros.uge(Amount.getUnsignedMax()))
629 return ConstantRange(min, max);
631 // FIXME: implement the other tricky cases
632 return ConstantRange(getBitWidth());
636 ConstantRange::ashr(const ConstantRange &Amount) const {
640 APInt min = getUnsignedMax().ashr(Amount.getUnsignedMin());
641 APInt max = getUnsignedMin().ashr(Amount.getUnsignedMax());
642 return ConstantRange(min, max);
646 ConstantRange::lshr(const ConstantRange &Amount) const {
650 APInt min = getUnsignedMax().lshr(Amount.getUnsignedMin());
651 APInt max = getUnsignedMin().lshr(Amount.getUnsignedMax());
652 return ConstantRange(min, max);
655 /// print - Print out the bounds to a stream...
657 void ConstantRange::print(raw_ostream &OS) const {
660 else if (isEmptySet())
663 OS << "[" << Lower << "," << Upper << ")";
666 /// dump - Allow printing from a debugger easily...
668 void ConstantRange::dump() const {