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/InstrTypes.h"
25 #include "llvm/Support/ConstantRange.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/raw_ostream.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) {
56 uint32_t W = CR.getBitWidth();
58 default: llvm_unreachable("Invalid ICmp predicate to makeICmpRegion()");
59 case CmpInst::ICMP_EQ:
61 case CmpInst::ICMP_NE:
62 if (CR.isSingleElement())
63 return ConstantRange(CR.getUpper(), CR.getLower());
64 return ConstantRange(W);
65 case CmpInst::ICMP_ULT: {
66 APInt UMax(CR.getUnsignedMax());
67 if (UMax.isMinValue())
68 return ConstantRange(W, /* empty */ false);
69 return ConstantRange(APInt::getMinValue(W), UMax);
71 case CmpInst::ICMP_SLT: {
72 APInt SMax(CR.getSignedMax());
73 if (SMax.isMinSignedValue())
74 return ConstantRange(W, /* empty */ false);
75 return ConstantRange(APInt::getSignedMinValue(W), SMax);
77 case CmpInst::ICMP_ULE: {
78 APInt UMax(CR.getUnsignedMax());
79 if (UMax.isMaxValue())
80 return ConstantRange(W);
81 return ConstantRange(APInt::getMinValue(W), UMax + 1);
83 case CmpInst::ICMP_SLE: {
84 APInt SMax(CR.getSignedMax());
85 if (SMax.isMaxSignedValue())
86 return ConstantRange(W);
87 return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
89 case CmpInst::ICMP_UGT: {
90 APInt UMin(CR.getUnsignedMin());
91 if (UMin.isMaxValue())
92 return ConstantRange(W, /* empty */ false);
93 return ConstantRange(UMin + 1, APInt::getNullValue(W));
95 case CmpInst::ICMP_SGT: {
96 APInt SMin(CR.getSignedMin());
97 if (SMin.isMaxSignedValue())
98 return ConstantRange(W, /* empty */ false);
99 return ConstantRange(SMin + 1, APInt::getSignedMinValue(W));
101 case CmpInst::ICMP_UGE: {
102 APInt UMin(CR.getUnsignedMin());
103 if (UMin.isMinValue())
104 return ConstantRange(W);
105 return ConstantRange(UMin, APInt::getNullValue(W));
107 case CmpInst::ICMP_SGE: {
108 APInt SMin(CR.getSignedMin());
109 if (SMin.isMinSignedValue())
110 return ConstantRange(W);
111 return ConstantRange(SMin, APInt::getSignedMinValue(W));
116 /// isFullSet - Return true if this set contains all of the elements possible
117 /// for this data-type
118 bool ConstantRange::isFullSet() const {
119 return Lower == Upper && Lower.isMaxValue();
122 /// isEmptySet - Return true if this set contains no members.
124 bool ConstantRange::isEmptySet() const {
125 return Lower == Upper && Lower.isMinValue();
128 /// isWrappedSet - Return true if this set wraps around the top of the range,
129 /// for example: [100, 8)
131 bool ConstantRange::isWrappedSet() const {
132 return Lower.ugt(Upper);
135 /// isSignWrappedSet - Return true if this set wraps around the INT_MIN of
136 /// its bitwidth, for example: i8 [120, 140).
138 bool ConstantRange::isSignWrappedSet() const {
139 return contains(APInt::getSignedMaxValue(getBitWidth())) &&
140 contains(APInt::getSignedMinValue(getBitWidth()));
143 /// getSetSize - Return the number of elements in this set.
145 APInt ConstantRange::getSetSize() const {
147 return APInt(getBitWidth(), 0);
148 if (getBitWidth() == 1) {
149 if (Lower != Upper) // One of T or F in the set...
151 return APInt(2, 2); // Must be full set...
154 // Simply subtract the bounds...
155 return Upper - Lower;
158 /// getUnsignedMax - Return the largest unsigned value contained in the
161 APInt ConstantRange::getUnsignedMax() const {
162 if (isFullSet() || isWrappedSet())
163 return APInt::getMaxValue(getBitWidth());
164 return getUpper() - 1;
167 /// getUnsignedMin - Return the smallest unsigned value contained in the
170 APInt ConstantRange::getUnsignedMin() const {
171 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
172 return APInt::getMinValue(getBitWidth());
176 /// getSignedMax - Return the largest signed value contained in the
179 APInt ConstantRange::getSignedMax() const {
180 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
181 if (!isWrappedSet()) {
182 if (getLower().sle(getUpper() - 1))
183 return getUpper() - 1;
186 if (getLower().isNegative() == getUpper().isNegative())
188 return getUpper() - 1;
191 /// getSignedMin - Return the smallest signed value contained in the
194 APInt ConstantRange::getSignedMin() const {
195 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
196 if (!isWrappedSet()) {
197 if (getLower().sle(getUpper() - 1))
201 if ((getUpper() - 1).slt(getLower())) {
202 if (getUpper() != SignedMin)
208 /// contains - Return true if the specified value is in the set.
210 bool ConstantRange::contains(const APInt &V) const {
215 return Lower.ule(V) && V.ult(Upper);
216 return Lower.ule(V) || V.ult(Upper);
219 /// contains - Return true if the argument is a subset of this range.
220 /// Two equal sets contain each other. The empty set contained by all other
223 bool ConstantRange::contains(const ConstantRange &Other) const {
224 if (isFullSet() || Other.isEmptySet()) return true;
225 if (isEmptySet() || Other.isFullSet()) return false;
227 if (!isWrappedSet()) {
228 if (Other.isWrappedSet())
231 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
234 if (!Other.isWrappedSet())
235 return Other.getUpper().ule(Upper) ||
236 Lower.ule(Other.getLower());
238 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
241 /// subtract - Subtract the specified constant from the endpoints of this
243 ConstantRange ConstantRange::subtract(const APInt &Val) const {
244 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
245 // If the set is empty or full, don't modify the endpoints.
248 return ConstantRange(Lower - Val, Upper - Val);
251 /// \brief Subtract the specified range from this range (aka relative complement
253 ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
254 return intersectWith(CR.inverse());
257 /// intersectWith - Return the range that results from the intersection of this
258 /// range with another range. The resultant range is guaranteed to include all
259 /// elements contained in both input ranges, and to have the smallest possible
260 /// set size that does so. Because there may be two intersections with the
261 /// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
262 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
263 assert(getBitWidth() == CR.getBitWidth() &&
264 "ConstantRange types don't agree!");
266 // Handle common cases.
267 if ( isEmptySet() || CR.isFullSet()) return *this;
268 if (CR.isEmptySet() || isFullSet()) return CR;
270 if (!isWrappedSet() && CR.isWrappedSet())
271 return CR.intersectWith(*this);
273 if (!isWrappedSet() && !CR.isWrappedSet()) {
274 if (Lower.ult(CR.Lower)) {
275 if (Upper.ule(CR.Lower))
276 return ConstantRange(getBitWidth(), false);
278 if (Upper.ult(CR.Upper))
279 return ConstantRange(CR.Lower, Upper);
283 if (Upper.ult(CR.Upper))
286 if (Lower.ult(CR.Upper))
287 return ConstantRange(Lower, CR.Upper);
289 return ConstantRange(getBitWidth(), false);
292 if (isWrappedSet() && !CR.isWrappedSet()) {
293 if (CR.Lower.ult(Upper)) {
294 if (CR.Upper.ult(Upper))
297 if (CR.Upper.ule(Lower))
298 return ConstantRange(CR.Lower, Upper);
300 if (getSetSize().ult(CR.getSetSize()))
304 if (CR.Lower.ult(Lower)) {
305 if (CR.Upper.ule(Lower))
306 return ConstantRange(getBitWidth(), false);
308 return ConstantRange(Lower, CR.Upper);
313 if (CR.Upper.ult(Upper)) {
314 if (CR.Lower.ult(Upper)) {
315 if (getSetSize().ult(CR.getSetSize()))
320 if (CR.Lower.ult(Lower))
321 return ConstantRange(Lower, CR.Upper);
325 if (CR.Upper.ule(Lower)) {
326 if (CR.Lower.ult(Lower))
329 return ConstantRange(CR.Lower, Upper);
331 if (getSetSize().ult(CR.getSetSize()))
337 /// unionWith - Return the range that results from the union of this range with
338 /// another range. The resultant range is guaranteed to include the elements of
339 /// both sets, but may contain more. For example, [3, 9) union [12,15) is
340 /// [3, 15), which includes 9, 10, and 11, which were not included in either
343 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
344 assert(getBitWidth() == CR.getBitWidth() &&
345 "ConstantRange types don't agree!");
347 if ( isFullSet() || CR.isEmptySet()) return *this;
348 if (CR.isFullSet() || isEmptySet()) return CR;
350 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
352 if (!isWrappedSet() && !CR.isWrappedSet()) {
353 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
354 // If the two ranges are disjoint, find the smaller gap and bridge it.
355 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
357 return ConstantRange(Lower, CR.Upper);
358 return ConstantRange(CR.Lower, Upper);
361 APInt L = Lower, U = Upper;
364 if ((CR.Upper - 1).ugt(U - 1))
367 if (L == 0 && U == 0)
368 return ConstantRange(getBitWidth());
370 return ConstantRange(L, U);
373 if (!CR.isWrappedSet()) {
374 // ------U L----- and ------U L----- : this
376 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
379 // ------U L----- : this
381 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
382 return ConstantRange(getBitWidth());
384 // ----U L---- : this
387 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
388 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
390 return ConstantRange(Lower, CR.Upper);
391 return ConstantRange(CR.Lower, Upper);
394 // ----U L----- : this
396 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
397 return ConstantRange(CR.Lower, Upper);
399 // ------U L---- : this
401 assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) &&
402 "ConstantRange::unionWith missed a case with one range wrapped");
403 return ConstantRange(Lower, CR.Upper);
406 // ------U L---- and ------U L---- : this
407 // -U L----------- and ------------U L : CR
408 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
409 return ConstantRange(getBitWidth());
411 APInt L = Lower, U = Upper;
417 return ConstantRange(L, U);
420 /// zeroExtend - Return a new range in the specified integer type, which must
421 /// be strictly larger than the current type. The returned range will
422 /// correspond to the possible range of values as if the source range had been
424 ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
425 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
427 unsigned SrcTySize = getBitWidth();
428 assert(SrcTySize < DstTySize && "Not a value extension");
429 if (isFullSet() || isWrappedSet())
430 // Change into [0, 1 << src bit width)
431 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
433 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
436 /// signExtend - Return a new range in the specified integer type, which must
437 /// be strictly larger than the current type. The returned range will
438 /// correspond to the possible range of values as if the source range had been
440 ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
441 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
443 unsigned SrcTySize = getBitWidth();
444 assert(SrcTySize < DstTySize && "Not a value extension");
445 if (isFullSet() || isSignWrappedSet()) {
446 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
447 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
450 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
453 /// truncate - Return a new range in the specified integer type, which must be
454 /// strictly smaller than the current type. The returned range will
455 /// correspond to the possible range of values as if the source range had been
456 /// truncated to the specified type.
457 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
458 assert(getBitWidth() > DstTySize && "Not a value truncation");
459 if (isFullSet() || getSetSize().getActiveBits() > DstTySize)
460 return ConstantRange(DstTySize, /*isFullSet=*/true);
462 return ConstantRange(Lower.trunc(DstTySize), Upper.trunc(DstTySize));
465 /// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
466 /// value is zero extended, truncated, or left alone to make it that width.
467 ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
468 unsigned SrcTySize = getBitWidth();
469 if (SrcTySize > DstTySize)
470 return truncate(DstTySize);
471 if (SrcTySize < DstTySize)
472 return zeroExtend(DstTySize);
476 /// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
477 /// value is sign extended, truncated, or left alone to make it that width.
478 ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
479 unsigned SrcTySize = getBitWidth();
480 if (SrcTySize > DstTySize)
481 return truncate(DstTySize);
482 if (SrcTySize < DstTySize)
483 return signExtend(DstTySize);
488 ConstantRange::add(const ConstantRange &Other) const {
489 if (isEmptySet() || Other.isEmptySet())
490 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
491 if (isFullSet() || Other.isFullSet())
492 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
494 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
495 APInt NewLower = getLower() + Other.getLower();
496 APInt NewUpper = getUpper() + Other.getUpper() - 1;
497 if (NewLower == NewUpper)
498 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
500 ConstantRange X = ConstantRange(NewLower, NewUpper);
501 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
502 // We've wrapped, therefore, full set.
503 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
509 ConstantRange::sub(const ConstantRange &Other) const {
510 if (isEmptySet() || Other.isEmptySet())
511 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
512 if (isFullSet() || Other.isFullSet())
513 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
515 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
516 APInt NewLower = getLower() - Other.getUpper() + 1;
517 APInt NewUpper = getUpper() - Other.getLower();
518 if (NewLower == NewUpper)
519 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
521 ConstantRange X = ConstantRange(NewLower, NewUpper);
522 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
523 // We've wrapped, therefore, full set.
524 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
530 ConstantRange::multiply(const ConstantRange &Other) const {
531 // TODO: If either operand is a single element and the multiply is known to
532 // be non-wrapping, round the result min and max value to the appropriate
533 // multiple of that element. If wrapping is possible, at least adjust the
534 // range according to the greatest power-of-two factor of the single element.
536 if (isEmptySet() || Other.isEmptySet())
537 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
538 if (isFullSet() || Other.isFullSet())
539 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
541 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
542 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
543 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
544 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
546 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
547 this_max * Other_max + 1);
548 return Result_zext.truncate(getBitWidth());
552 ConstantRange::smax(const ConstantRange &Other) const {
553 // X smax Y is: range(smax(X_smin, Y_smin),
554 // smax(X_smax, Y_smax))
555 if (isEmptySet() || Other.isEmptySet())
556 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
557 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
558 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
560 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
561 return ConstantRange(NewL, NewU);
565 ConstantRange::umax(const ConstantRange &Other) const {
566 // X umax Y is: range(umax(X_umin, Y_umin),
567 // umax(X_umax, Y_umax))
568 if (isEmptySet() || Other.isEmptySet())
569 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
570 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
571 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
573 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
574 return ConstantRange(NewL, NewU);
578 ConstantRange::udiv(const ConstantRange &RHS) const {
579 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
580 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
582 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
584 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
586 APInt RHS_umin = RHS.getUnsignedMin();
588 // We want the lowest value in RHS excluding zero. Usually that would be 1
589 // except for a range in the form of [X, 1) in which case it would be X.
590 if (RHS.getUpper() == 1)
591 RHS_umin = RHS.getLower();
593 RHS_umin = APInt(getBitWidth(), 1);
596 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
598 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
601 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
603 return ConstantRange(Lower, Upper);
607 ConstantRange::binaryAnd(const ConstantRange &Other) const {
608 if (isEmptySet() || Other.isEmptySet())
609 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
611 // TODO: replace this with something less conservative
613 APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax());
614 if (umin.isAllOnesValue())
615 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
616 return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1);
620 ConstantRange::binaryOr(const ConstantRange &Other) const {
621 if (isEmptySet() || Other.isEmptySet())
622 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
624 // TODO: replace this with something less conservative
626 APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
627 if (umax.isMinValue())
628 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
629 return ConstantRange(umax, APInt::getNullValue(getBitWidth()));
633 ConstantRange::shl(const ConstantRange &Other) const {
634 if (isEmptySet() || Other.isEmptySet())
635 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
637 APInt min = getUnsignedMin().shl(Other.getUnsignedMin());
638 APInt max = getUnsignedMax().shl(Other.getUnsignedMax());
640 // there's no overflow!
641 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
642 if (Zeros.ugt(Other.getUnsignedMax()))
643 return ConstantRange(min, max + 1);
645 // FIXME: implement the other tricky cases
646 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
650 ConstantRange::lshr(const ConstantRange &Other) const {
651 if (isEmptySet() || Other.isEmptySet())
652 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
654 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin());
655 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
657 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
659 return ConstantRange(min, max + 1);
662 ConstantRange ConstantRange::inverse() const {
664 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
666 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
667 return ConstantRange(Upper, Lower);
670 /// print - Print out the bounds to a stream...
672 void ConstantRange::print(raw_ostream &OS) const {
675 else if (isEmptySet())
678 OS << "[" << Lower << "," << Upper << ")";
681 /// dump - Allow printing from a debugger easily...
683 void ConstantRange::dump() const {