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) {}
43 ConstantRange::ConstantRange(const ConstantInt *V)
44 : Lower(V->getValue()), Upper(V->getValue() + 1) {}
46 ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
48 assert(L.getBitWidth() == U.getBitWidth() &&
49 "ConstantRange with unequal bit widths");
50 assert((L != U || (L.isMaxValue() || L.isMinValue())) &&
51 "Lower == Upper, but they aren't min or max value!");
54 ConstantRange ConstantRange::makeICmpRegion(unsigned Pred,
55 const ConstantRange &CR) {
56 uint32_t W = CR.getBitWidth();
58 default: assert(!"Invalid ICmp predicate to makeICmpRegion()");
59 case ICmpInst::ICMP_EQ:
61 case ICmpInst::ICMP_NE:
62 if (CR.isSingleElement())
63 return ConstantRange(CR.getUpper(), CR.getLower());
64 return ConstantRange(W);
65 case ICmpInst::ICMP_ULT:
66 return ConstantRange(APInt::getMinValue(W), CR.getUnsignedMax());
67 case ICmpInst::ICMP_SLT:
68 return ConstantRange(APInt::getSignedMinValue(W), CR.getSignedMax());
69 case ICmpInst::ICMP_ULE: {
70 APInt UMax(CR.getUnsignedMax());
71 if (UMax.isMaxValue())
72 return ConstantRange(W);
73 return ConstantRange(APInt::getMinValue(W), UMax + 1);
75 case ICmpInst::ICMP_SLE: {
76 APInt SMax(CR.getSignedMax());
77 if (SMax.isMaxSignedValue() || (SMax+1).isMaxSignedValue())
78 return ConstantRange(W);
79 return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
81 case ICmpInst::ICMP_UGT:
82 return ConstantRange(CR.getUnsignedMin() + 1, APInt::getNullValue(W));
83 case ICmpInst::ICMP_SGT:
84 return ConstantRange(CR.getSignedMin() + 1,
85 APInt::getSignedMinValue(W));
86 case ICmpInst::ICMP_UGE: {
87 APInt UMin(CR.getUnsignedMin());
88 if (UMin.isMinValue())
89 return ConstantRange(W);
90 return ConstantRange(UMin, APInt::getNullValue(W));
92 case ICmpInst::ICMP_SGE: {
93 APInt SMin(CR.getSignedMin());
94 if (SMin.isMinSignedValue())
95 return ConstantRange(W);
96 return ConstantRange(SMin, APInt::getSignedMinValue(W));
101 /// isFullSet - Return true if this set contains all of the elements possible
102 /// for this data-type
103 bool ConstantRange::isFullSet() const {
104 return Lower == Upper && Lower.isMaxValue();
107 /// isEmptySet - Return true if this set contains no members.
109 bool ConstantRange::isEmptySet() const {
110 return Lower == Upper && Lower.isMinValue();
113 /// isWrappedSet - Return true if this set wraps around the top of the range,
114 /// for example: [100, 8)
116 bool ConstantRange::isWrappedSet() const {
117 return Lower.ugt(Upper);
120 /// getSetSize - Return the number of elements in this set.
122 APInt ConstantRange::getSetSize() const {
124 return APInt(getBitWidth(), 0);
125 if (getBitWidth() == 1) {
126 if (Lower != Upper) // One of T or F in the set...
128 return APInt(2, 2); // Must be full set...
131 // Simply subtract the bounds...
132 return Upper - Lower;
135 /// getUnsignedMax - Return the largest unsigned value contained in the
138 APInt ConstantRange::getUnsignedMax() const {
139 if (isFullSet() || isWrappedSet())
140 return APInt::getMaxValue(getBitWidth());
142 return getUpper() - 1;
145 /// getUnsignedMin - Return the smallest unsigned value contained in the
148 APInt ConstantRange::getUnsignedMin() const {
149 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
150 return APInt::getMinValue(getBitWidth());
155 /// getSignedMax - Return the largest signed value contained in the
158 APInt ConstantRange::getSignedMax() const {
159 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
160 if (!isWrappedSet()) {
161 if (getLower().sle(getUpper() - 1))
162 return getUpper() - 1;
166 if (getLower().isNegative() == getUpper().isNegative())
169 return getUpper() - 1;
173 /// getSignedMin - Return the smallest signed value contained in the
176 APInt ConstantRange::getSignedMin() const {
177 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
178 if (!isWrappedSet()) {
179 if (getLower().sle(getUpper() - 1))
184 if ((getUpper() - 1).slt(getLower())) {
185 if (getUpper() != SignedMin)
195 /// contains - Return true if the specified value is in the set.
197 bool ConstantRange::contains(const APInt &V) const {
202 return Lower.ule(V) && V.ult(Upper);
204 return Lower.ule(V) || V.ult(Upper);
207 /// contains - Return true if the argument is a subset of this range.
208 /// Two equal set contain each other. The empty set is considered to be
209 /// contained by all other sets.
211 bool ConstantRange::contains(const ConstantRange &Other) const {
212 if (isFullSet()) return true;
213 if (Other.isFullSet()) return false;
214 if (Other.isEmptySet()) return true;
215 if (isEmptySet()) return false;
217 if (!isWrappedSet()) {
218 if (Other.isWrappedSet())
221 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
224 if (!Other.isWrappedSet())
225 return Other.getUpper().ule(Upper) ||
226 Lower.ule(Other.getLower());
228 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
231 /// subtract - Subtract the specified constant from the endpoints of this
233 ConstantRange ConstantRange::subtract(const APInt &Val) const {
234 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
235 // If the set is empty or full, don't modify the endpoints.
238 return ConstantRange(Lower - Val, Upper - Val);
242 // intersect1Wrapped - This helper function is used to intersect two ranges when
243 // it is known that LHS is wrapped and RHS isn't.
246 ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
247 const ConstantRange &RHS) {
248 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
250 // Check to see if we overlap on the Left side of RHS...
252 if (RHS.Lower.ult(LHS.Upper)) {
253 // We do overlap on the left side of RHS, see if we overlap on the right of
255 if (RHS.Upper.ugt(LHS.Lower)) {
256 // Ok, the result overlaps on both the left and right sides. See if the
257 // resultant interval will be smaller if we wrap or not...
259 if (LHS.getSetSize().ult(RHS.getSetSize()))
265 // No overlap on the right, just on the left.
266 return ConstantRange(RHS.Lower, LHS.Upper);
269 // We don't overlap on the left side of RHS, see if we overlap on the right
271 if (RHS.Upper.ugt(LHS.Lower)) {
273 return ConstantRange(LHS.Lower, RHS.Upper);
276 return ConstantRange(LHS.getBitWidth(), false);
281 /// intersectWith - Return the range that results from the intersection of this
282 /// range with another range. The resultant range is guaranteed to include all
283 /// elements contained in both input ranges, and to have the smallest possible
284 /// set size that does so. Because there may be two intersections with the
285 /// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
286 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
287 assert(getBitWidth() == CR.getBitWidth() &&
288 "ConstantRange types don't agree!");
290 // Handle common cases.
291 if ( isEmptySet() || CR.isFullSet()) return *this;
292 if (CR.isEmptySet() || isFullSet()) return CR;
294 if (!isWrappedSet() && CR.isWrappedSet())
295 return CR.intersectWith(*this);
297 if (!isWrappedSet() && !CR.isWrappedSet()) {
298 if (Lower.ult(CR.Lower)) {
299 if (Upper.ule(CR.Lower))
300 return ConstantRange(getBitWidth(), false);
302 if (Upper.ult(CR.Upper))
303 return ConstantRange(CR.Lower, Upper);
307 if (Upper.ult(CR.Upper))
310 if (Lower.ult(CR.Upper))
311 return ConstantRange(Lower, CR.Upper);
313 return ConstantRange(getBitWidth(), false);
317 if (isWrappedSet() && !CR.isWrappedSet()) {
318 if (CR.Lower.ult(Upper)) {
319 if (CR.Upper.ult(Upper))
322 if (CR.Upper.ult(Lower))
323 return ConstantRange(CR.Lower, Upper);
325 if (getSetSize().ult(CR.getSetSize()))
329 } else if (CR.Lower.ult(Lower)) {
330 if (CR.Upper.ule(Lower))
331 return ConstantRange(getBitWidth(), false);
333 return ConstantRange(Lower, CR.Upper);
338 if (CR.Upper.ult(Upper)) {
339 if (CR.Lower.ult(Upper)) {
340 if (getSetSize().ult(CR.getSetSize()))
346 if (CR.Lower.ult(Lower))
347 return ConstantRange(Lower, CR.Upper);
350 } else if (CR.Upper.ult(Lower)) {
351 if (CR.Lower.ult(Lower))
354 return ConstantRange(CR.Lower, Upper);
356 if (getSetSize().ult(CR.getSetSize()))
363 /// unionWith - Return the range that results from the union of this range with
364 /// another range. The resultant range is guaranteed to include the elements of
365 /// both sets, but may contain more. For example, [3, 9) union [12,15) is
366 /// [3, 15), which includes 9, 10, and 11, which were not included in either
369 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
370 assert(getBitWidth() == CR.getBitWidth() &&
371 "ConstantRange types don't agree!");
373 if ( isFullSet() || CR.isEmptySet()) return *this;
374 if (CR.isFullSet() || isEmptySet()) return CR;
376 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
378 if (!isWrappedSet() && !CR.isWrappedSet()) {
379 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
380 // If the two ranges are disjoint, find the smaller gap and bridge it.
381 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
383 return ConstantRange(Lower, CR.Upper);
385 return ConstantRange(CR.Lower, Upper);
388 APInt L = Lower, U = Upper;
391 if ((CR.Upper - 1).ugt(U - 1))
394 if (L == 0 && U == 0)
395 return ConstantRange(getBitWidth());
397 return ConstantRange(L, U);
400 if (!CR.isWrappedSet()) {
401 // ------U L----- and ------U L----- : this
403 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
406 // ------U L----- : this
408 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
409 return ConstantRange(getBitWidth());
411 // ----U L---- : this
414 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
415 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
417 return ConstantRange(Lower, CR.Upper);
419 return ConstantRange(CR.Lower, Upper);
422 // ----U L----- : this
424 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
425 return ConstantRange(CR.Lower, Upper);
427 // ------U L---- : this
429 if (CR.Lower.ult(Upper) && CR.Upper.ult(Lower))
430 return ConstantRange(Lower, CR.Upper);
433 assert(isWrappedSet() && CR.isWrappedSet() &&
434 "ConstantRange::unionWith missed wrapped union unwrapped case");
436 // ------U L---- and ------U L---- : this
437 // -U L----------- and ------------U L : CR
438 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
439 return ConstantRange(getBitWidth());
441 APInt L = Lower, U = Upper;
447 return ConstantRange(L, U);
450 /// zeroExtend - Return a new range in the specified integer type, which must
451 /// be strictly larger than the current type. The returned range will
452 /// correspond to the possible range of values as if the source range had been
454 ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
455 unsigned SrcTySize = getBitWidth();
456 assert(SrcTySize < DstTySize && "Not a value extension");
458 // Change a source full set into [0, 1 << 8*numbytes)
459 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
461 APInt L = Lower; L.zext(DstTySize);
462 APInt U = Upper; U.zext(DstTySize);
463 return ConstantRange(L, U);
466 /// signExtend - Return a new range in the specified integer type, which must
467 /// be strictly larger than the current type. The returned range will
468 /// correspond to the possible range of values as if the source range had been
470 ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
471 unsigned SrcTySize = getBitWidth();
472 assert(SrcTySize < DstTySize && "Not a value extension");
474 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
475 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
478 APInt L = Lower; L.sext(DstTySize);
479 APInt U = Upper; U.sext(DstTySize);
480 return ConstantRange(L, U);
483 /// truncate - Return a new range in the specified integer type, which must be
484 /// strictly smaller than the current type. The returned range will
485 /// correspond to the possible range of values as if the source range had been
486 /// truncated to the specified type.
487 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
488 unsigned SrcTySize = getBitWidth();
489 assert(SrcTySize > DstTySize && "Not a value truncation");
490 APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
491 if (isFullSet() || getSetSize().ugt(Size))
492 return ConstantRange(DstTySize);
494 APInt L = Lower; L.trunc(DstTySize);
495 APInt U = Upper; U.trunc(DstTySize);
496 return ConstantRange(L, U);
499 /// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
500 /// value is zero extended, truncated, or left alone to make it that width.
501 ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
502 unsigned SrcTySize = getBitWidth();
503 if (SrcTySize > DstTySize)
504 return truncate(DstTySize);
505 else if (SrcTySize < DstTySize)
506 return zeroExtend(DstTySize);
511 /// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
512 /// value is sign extended, truncated, or left alone to make it that width.
513 ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
514 unsigned SrcTySize = getBitWidth();
515 if (SrcTySize > DstTySize)
516 return truncate(DstTySize);
517 else if (SrcTySize < DstTySize)
518 return signExtend(DstTySize);
524 ConstantRange::add(const ConstantRange &Other) const {
525 if (isEmptySet() || Other.isEmptySet())
526 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
527 if (isFullSet() || Other.isFullSet())
528 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
530 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
531 APInt NewLower = getLower() + Other.getLower();
532 APInt NewUpper = getUpper() + Other.getUpper() - 1;
533 if (NewLower == NewUpper)
534 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
536 ConstantRange X = ConstantRange(NewLower, NewUpper);
537 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
538 // We've wrapped, therefore, full set.
539 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
545 ConstantRange::multiply(const ConstantRange &Other) const {
546 // TODO: If either operand is a single element and the multiply is known to
547 // be non-wrapping, round the result min and max value to the appropriate
548 // multiple of that element. If wrapping is possible, at least adjust the
549 // range according to the greatest power-of-two factor of the single element.
551 if (isEmptySet() || Other.isEmptySet())
552 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
553 if (isFullSet() || Other.isFullSet())
554 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
556 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
557 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
558 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
559 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
561 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
562 this_max * Other_max + 1);
563 return Result_zext.truncate(getBitWidth());
567 ConstantRange::smax(const ConstantRange &Other) const {
568 // X smax Y is: range(smax(X_smin, Y_smin),
569 // smax(X_smax, Y_smax))
570 if (isEmptySet() || Other.isEmptySet())
571 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
572 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
573 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
575 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
576 return ConstantRange(NewL, NewU);
580 ConstantRange::umax(const ConstantRange &Other) const {
581 // X umax Y is: range(umax(X_umin, Y_umin),
582 // umax(X_umax, Y_umax))
583 if (isEmptySet() || Other.isEmptySet())
584 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
585 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
586 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
588 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
589 return ConstantRange(NewL, NewU);
593 ConstantRange::udiv(const ConstantRange &RHS) const {
594 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
595 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
597 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
599 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
601 APInt RHS_umin = RHS.getUnsignedMin();
603 // We want the lowest value in RHS excluding zero. Usually that would be 1
604 // except for a range in the form of [X, 1) in which case it would be X.
605 if (RHS.getUpper() == 1)
606 RHS_umin = RHS.getLower();
608 RHS_umin = APInt(getBitWidth(), 1);
611 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
613 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
616 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
618 return ConstantRange(Lower, Upper);
622 ConstantRange::shl(const ConstantRange &Amount) const {
626 APInt min = getUnsignedMin() << Amount.getUnsignedMin();
627 APInt max = getUnsignedMax() << Amount.getUnsignedMax();
629 // there's no overflow!
630 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
631 if (Zeros.uge(Amount.getUnsignedMax()))
632 return ConstantRange(min, max);
634 // FIXME: implement the other tricky cases
635 return ConstantRange(getBitWidth());
639 ConstantRange::ashr(const ConstantRange &Amount) const {
643 APInt min = getUnsignedMax().ashr(Amount.getUnsignedMin());
644 APInt max = getUnsignedMin().ashr(Amount.getUnsignedMax());
645 return ConstantRange(min, max);
649 ConstantRange::lshr(const ConstantRange &Amount) const {
653 APInt min = getUnsignedMax().lshr(Amount.getUnsignedMin());
654 APInt max = getUnsignedMin().lshr(Amount.getUnsignedMax());
655 return ConstantRange(min, max);
658 /// print - Print out the bounds to a stream...
660 void ConstantRange::print(raw_ostream &OS) const {
663 else if (isEmptySet())
666 OS << "[" << Lower << "," << Upper << ")";
669 /// dump - Allow printing from a debugger easily...
671 void ConstantRange::dump() const {