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"
26 #include "llvm/Instructions.h"
29 /// Initialize a full (the default) or empty set for the specified type.
31 ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) {
33 Lower = Upper = APInt::getMaxValue(BitWidth);
35 Lower = Upper = APInt::getMinValue(BitWidth);
38 /// Initialize a range to hold the single specified value.
40 ConstantRange::ConstantRange(const APInt & V) : Lower(V), Upper(V + 1) {}
42 ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
44 assert(L.getBitWidth() == U.getBitWidth() &&
45 "ConstantRange with unequal bit widths");
46 assert((L != U || (L.isMaxValue() || L.isMinValue())) &&
47 "Lower == Upper, but they aren't min or max value!");
50 ConstantRange ConstantRange::makeICmpRegion(unsigned Pred,
51 const ConstantRange &CR) {
52 uint32_t W = CR.getBitWidth();
54 default: assert(!"Invalid ICmp predicate to makeICmpRegion()");
55 case ICmpInst::ICMP_EQ:
57 case ICmpInst::ICMP_NE:
58 if (CR.isSingleElement())
59 return ConstantRange(CR.getUpper(), CR.getLower());
60 return ConstantRange(W);
61 case ICmpInst::ICMP_ULT:
62 return ConstantRange(APInt::getMinValue(W), CR.getUnsignedMax());
63 case ICmpInst::ICMP_SLT:
64 return ConstantRange(APInt::getSignedMinValue(W), CR.getSignedMax());
65 case ICmpInst::ICMP_ULE: {
66 APInt UMax(CR.getUnsignedMax());
67 if (UMax.isMaxValue())
68 return ConstantRange(W);
69 return ConstantRange(APInt::getMinValue(W), UMax + 1);
71 case ICmpInst::ICMP_SLE: {
72 APInt SMax(CR.getSignedMax());
73 if (SMax.isMaxSignedValue() || (SMax+1).isMaxSignedValue())
74 return ConstantRange(W);
75 return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
77 case ICmpInst::ICMP_UGT:
78 return ConstantRange(CR.getUnsignedMin() + 1, APInt::getNullValue(W));
79 case ICmpInst::ICMP_SGT:
80 return ConstantRange(CR.getSignedMin() + 1,
81 APInt::getSignedMinValue(W));
82 case ICmpInst::ICMP_UGE: {
83 APInt UMin(CR.getUnsignedMin());
84 if (UMin.isMinValue())
85 return ConstantRange(W);
86 return ConstantRange(UMin, APInt::getNullValue(W));
88 case ICmpInst::ICMP_SGE: {
89 APInt SMin(CR.getSignedMin());
90 if (SMin.isMinSignedValue())
91 return ConstantRange(W);
92 return ConstantRange(SMin, APInt::getSignedMinValue(W));
97 /// isFullSet - Return true if this set contains all of the elements possible
98 /// for this data-type
99 bool ConstantRange::isFullSet() const {
100 return Lower == Upper && Lower.isMaxValue();
103 /// isEmptySet - Return true if this set contains no members.
105 bool ConstantRange::isEmptySet() const {
106 return Lower == Upper && Lower.isMinValue();
109 /// isWrappedSet - Return true if this set wraps around the top of the range,
110 /// for example: [100, 8)
112 bool ConstantRange::isWrappedSet() const {
113 return Lower.ugt(Upper);
116 /// getSetSize - Return the number of elements in this set.
118 APInt ConstantRange::getSetSize() const {
120 return APInt(getBitWidth(), 0);
121 if (getBitWidth() == 1) {
122 if (Lower != Upper) // One of T or F in the set...
124 return APInt(2, 2); // Must be full set...
127 // Simply subtract the bounds...
128 return Upper - Lower;
131 /// getUnsignedMax - Return the largest unsigned value contained in the
134 APInt ConstantRange::getUnsignedMax() const {
135 if (isFullSet() || isWrappedSet())
136 return APInt::getMaxValue(getBitWidth());
138 return getUpper() - 1;
141 /// getUnsignedMin - Return the smallest unsigned value contained in the
144 APInt ConstantRange::getUnsignedMin() const {
145 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
146 return APInt::getMinValue(getBitWidth());
151 /// getSignedMax - Return the largest signed value contained in the
154 APInt ConstantRange::getSignedMax() const {
155 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
156 if (!isWrappedSet()) {
157 if (getLower().sle(getUpper() - 1))
158 return getUpper() - 1;
162 if ((getUpper() - 1).slt(getLower())) {
163 if (getLower() != SignedMax)
166 return getUpper() - 1;
168 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.
284 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
285 assert(getBitWidth() == CR.getBitWidth() &&
286 "ConstantRange types don't agree!");
287 // Handle common special cases
288 if (isEmptySet() || CR.isFullSet())
290 if (isFullSet() || CR.isEmptySet())
293 if (!isWrappedSet()) {
294 if (!CR.isWrappedSet()) {
295 APInt L = APIntOps::umax(Lower, CR.Lower);
296 APInt U = APIntOps::umin(Upper, CR.Upper);
298 if (L.ult(U)) // If range isn't empty...
299 return ConstantRange(L, U);
301 return ConstantRange(getBitWidth(), false);// Otherwise, empty set
303 return intersect1Wrapped(CR, *this);
304 } else { // We know "this" is wrapped...
305 if (!CR.isWrappedSet())
306 return intersect1Wrapped(*this, CR);
308 // Both ranges are wrapped...
309 APInt L = APIntOps::umax(Lower, CR.Lower);
310 APInt U = APIntOps::umin(Upper, CR.Upper);
311 return ConstantRange(L, U);
317 /// maximalIntersectWith - Return the range that results from the intersection
318 /// of this range with another range. The resultant range is guaranteed to
319 /// include all elements contained in both input ranges, and to have the
320 /// smallest possible set size that does so. Because there may be two
321 /// intersections with the same set size, A.maximalIntersectWith(B) might not
322 /// be equal to B.maximalIntersect(A).
324 ConstantRange::maximalIntersectWith(const ConstantRange &CR) const {
325 assert(getBitWidth() == CR.getBitWidth() &&
326 "ConstantRange types don't agree!");
328 // Handle common cases.
329 if ( isEmptySet() || CR.isFullSet()) return *this;
330 if (CR.isEmptySet() || isFullSet()) return CR;
332 if (!isWrappedSet() && CR.isWrappedSet())
333 return CR.maximalIntersectWith(*this);
335 if (!isWrappedSet() && !CR.isWrappedSet()) {
336 if (Lower.ult(CR.Lower)) {
337 if (Upper.ule(CR.Lower))
338 return ConstantRange(getBitWidth(), false);
340 if (Upper.ult(CR.Upper))
341 return ConstantRange(CR.Lower, Upper);
345 if (Upper.ult(CR.Upper))
348 if (Lower.ult(CR.Upper))
349 return ConstantRange(Lower, CR.Upper);
351 return ConstantRange(getBitWidth(), false);
355 if (isWrappedSet() && !CR.isWrappedSet()) {
356 if (CR.Lower.ult(Upper)) {
357 if (CR.Upper.ult(Upper))
360 if (CR.Upper.ult(Lower))
361 return ConstantRange(CR.Lower, Upper);
363 if (getSetSize().ult(CR.getSetSize()))
367 } else if (CR.Lower.ult(Lower)) {
368 if (CR.Upper.ule(Lower))
369 return ConstantRange(getBitWidth(), false);
371 return ConstantRange(Lower, CR.Upper);
376 if (CR.Upper.ult(Upper)) {
377 if (CR.Lower.ult(Upper)) {
378 if (getSetSize().ult(CR.getSetSize()))
384 if (CR.Lower.ult(Lower))
385 return ConstantRange(Lower, CR.Upper);
388 } else if (CR.Upper.ult(Lower)) {
389 if (CR.Lower.ult(Lower))
392 return ConstantRange(CR.Lower, Upper);
394 if (getSetSize().ult(CR.getSetSize()))
401 /// unionWith - Return the range that results from the union of this range with
402 /// another range. The resultant range is guaranteed to include the elements of
403 /// both sets, but may contain more. For example, [3, 9) union [12,15) is
404 /// [3, 15), which includes 9, 10, and 11, which were not included in either
407 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
408 assert(getBitWidth() == CR.getBitWidth() &&
409 "ConstantRange types don't agree!");
411 if ( isFullSet() || CR.isEmptySet()) return *this;
412 if (CR.isFullSet() || isEmptySet()) return CR;
414 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
416 APInt L = Lower, U = Upper;
418 if (!isWrappedSet() && !CR.isWrappedSet()) {
426 if (isWrappedSet() && !CR.isWrappedSet()) {
427 if ((CR.Lower.ult(Upper) && CR.Upper.ult(Upper)) ||
428 (CR.Lower.ugt(Lower) && CR.Upper.ugt(Lower))) {
432 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) {
433 return ConstantRange(getBitWidth());
436 if (CR.Lower.ule(Upper) && CR.Upper.ule(Lower)) {
437 APInt d1 = CR.Upper - Upper, d2 = Lower - CR.Upper;
445 if (Upper.ult(CR.Lower) && CR.Upper.ult(Lower)) {
446 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
454 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) {
455 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Lower;
465 if (isWrappedSet() && CR.isWrappedSet()) {
466 if (Lower.ult(CR.Upper) || CR.Lower.ult(Upper))
467 return ConstantRange(getBitWidth());
469 if (CR.Upper.ugt(U)) {
473 if (CR.Lower.ult(L)) {
477 if (L == U) return ConstantRange(getBitWidth());
480 return ConstantRange(L, U);
483 /// zeroExtend - Return a new range in the specified integer type, which must
484 /// be strictly larger than the current type. The returned range will
485 /// correspond to the possible range of values as if the source range had been
487 ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
488 unsigned SrcTySize = getBitWidth();
489 assert(SrcTySize < DstTySize && "Not a value extension");
491 // Change a source full set into [0, 1 << 8*numbytes)
492 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
494 APInt L = Lower; L.zext(DstTySize);
495 APInt U = Upper; U.zext(DstTySize);
496 return ConstantRange(L, U);
499 /// signExtend - Return a new range in the specified integer type, which must
500 /// be strictly larger than the current type. The returned range will
501 /// correspond to the possible range of values as if the source range had been
503 ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
504 unsigned SrcTySize = getBitWidth();
505 assert(SrcTySize < DstTySize && "Not a value extension");
507 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
508 APInt::getLowBitsSet(DstTySize, SrcTySize-1));
511 APInt L = Lower; L.sext(DstTySize);
512 APInt U = Upper; U.sext(DstTySize);
513 return ConstantRange(L, U);
516 /// truncate - Return a new range in the specified integer type, which must be
517 /// strictly smaller than the current type. The returned range will
518 /// correspond to the possible range of values as if the source range had been
519 /// truncated to the specified type.
520 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
521 unsigned SrcTySize = getBitWidth();
522 assert(SrcTySize > DstTySize && "Not a value truncation");
523 APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
524 if (isFullSet() || getSetSize().ugt(Size))
525 return ConstantRange(DstTySize);
527 APInt L = Lower; L.trunc(DstTySize);
528 APInt U = Upper; U.trunc(DstTySize);
529 return ConstantRange(L, U);
533 ConstantRange::add(const ConstantRange &Other) const {
534 if (isEmptySet() || Other.isEmptySet())
535 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
536 if (isFullSet() || Other.isFullSet())
537 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
539 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
540 APInt NewLower = getLower() + Other.getLower();
541 APInt NewUpper = getUpper() + Other.getUpper() - 1;
542 if (NewLower == NewUpper)
543 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
545 ConstantRange X = ConstantRange(NewLower, NewUpper);
546 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
547 // We've wrapped, therefore, full set.
548 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
554 ConstantRange::multiply(const ConstantRange &Other) const {
555 if (isEmptySet() || Other.isEmptySet())
556 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
557 if (isFullSet() || Other.isFullSet())
558 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
560 ConstantRange this_zext = zeroExtend(getBitWidth() * 2);
561 ConstantRange Other_zext = Other.zeroExtend(getBitWidth() * 2);
563 ConstantRange Result_zext = ConstantRange(
564 this_zext.getLower() * Other_zext.getLower(),
565 ((this_zext.getUpper()-1) * (Other_zext.getUpper()-1)) + 1);
567 return Result_zext.truncate(getBitWidth());
571 ConstantRange::smax(const ConstantRange &Other) const {
572 // X smax Y is: range(smax(X_smin, Y_smin),
573 // smax(X_smax, Y_smax))
574 if (isEmptySet() || Other.isEmptySet())
575 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
576 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
577 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
579 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
580 return ConstantRange(NewL, NewU);
584 ConstantRange::umax(const ConstantRange &Other) const {
585 // X umax Y is: range(umax(X_umin, Y_umin),
586 // umax(X_umax, Y_umax))
587 if (isEmptySet() || Other.isEmptySet())
588 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
589 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
590 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
592 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
593 return ConstantRange(NewL, NewU);
597 ConstantRange::udiv(const ConstantRange &RHS) const {
598 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
599 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
601 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
603 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
605 APInt RHS_umin = RHS.getUnsignedMin();
607 // We want the lowest value in RHS excluding zero. Usually that would be 1
608 // except for a range in the form of [X, 1) in which case it would be X.
609 if (RHS.getUpper() == 1)
610 RHS_umin = RHS.getLower();
612 RHS_umin = APInt(getBitWidth(), 1);
615 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
617 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
620 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
622 return ConstantRange(Lower, Upper);
625 /// print - Print out the bounds to a stream...
627 void ConstantRange::print(raw_ostream &OS) const {
628 OS << "[" << Lower << "," << Upper << ")";
631 /// dump - Allow printing from a debugger easily...
633 void ConstantRange::dump() const {
637 std::ostream &llvm::operator<<(std::ostream &o,
638 const ConstantRange &CR) {
639 raw_os_ostream OS(o);