Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 1 | //===-- ConstantRange.cpp - ConstantRange implementation ------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 9 | // |
| 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): |
| 16 | // |
| 17 | // [F, F) = {} = Empty set |
| 18 | // [T, F) = {T} |
| 19 | // [F, T) = {F} |
| 20 | // [T, T) = {F, T} = Full set |
| 21 | // |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
| 24 | #include "llvm/Support/ConstantRange.h" |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 26 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 28 | /// Initialize a full (the default) or empty set for the specified type. |
| 29 | /// |
Dan Gohman | 38b0644 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 30 | ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 31 | if (Full) |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 32 | Lower = Upper = APInt::getMaxValue(BitWidth); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 33 | else |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 34 | Lower = Upper = APInt::getMinValue(BitWidth); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 37 | /// Initialize a range to hold the single specified value. |
| 38 | /// |
Dan Gohman | 38b0644 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 39 | ConstantRange::ConstantRange(const APInt & V) : Lower(V), Upper(V + 1) {} |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 40 | |
Dan Gohman | 38b0644 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 41 | ConstantRange::ConstantRange(const APInt &L, const APInt &U) : |
| 42 | Lower(L), Upper(U) { |
| 43 | assert(L.getBitWidth() == U.getBitWidth() && |
| 44 | "ConstantRange with unequal bit widths"); |
Zhou Sheng | c125c00 | 2007-04-26 16:42:07 +0000 | [diff] [blame] | 45 | assert((L != U || (L.isMaxValue() || L.isMinValue())) && |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 46 | "Lower == Upper, but they aren't min or max value!"); |
| 47 | } |
| 48 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 49 | /// isFullSet - Return true if this set contains all of the elements possible |
| 50 | /// for this data-type |
| 51 | bool ConstantRange::isFullSet() const { |
Zhou Sheng | c125c00 | 2007-04-26 16:42:07 +0000 | [diff] [blame] | 52 | return Lower == Upper && Lower.isMaxValue(); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 53 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 55 | /// isEmptySet - Return true if this set contains no members. |
| 56 | /// |
| 57 | bool ConstantRange::isEmptySet() const { |
Zhou Sheng | c125c00 | 2007-04-26 16:42:07 +0000 | [diff] [blame] | 58 | return Lower == Upper && Lower.isMinValue(); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /// isWrappedSet - Return true if this set wraps around the top of the range, |
| 62 | /// for example: [100, 8) |
| 63 | /// |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 64 | bool ConstantRange::isWrappedSet() const { |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 65 | return Lower.ugt(Upper); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 68 | /// getSetSize - Return the number of elements in this set. |
| 69 | /// |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 70 | APInt ConstantRange::getSetSize() const { |
| 71 | if (isEmptySet()) |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 72 | return APInt(getBitWidth(), 0); |
| 73 | if (getBitWidth() == 1) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 74 | if (Lower != Upper) // One of T or F in the set... |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 75 | return APInt(2, 1); |
| 76 | return APInt(2, 2); // Must be full set... |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 77 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 79 | // Simply subtract the bounds... |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 80 | return Upper - Lower; |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Nick Lewycky | 3400e6a | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 83 | /// getUnsignedMax - Return the largest unsigned value contained in the |
| 84 | /// ConstantRange. |
| 85 | /// |
| 86 | APInt ConstantRange::getUnsignedMax() const { |
| 87 | if (isFullSet() || isWrappedSet()) |
| 88 | return APInt::getMaxValue(getBitWidth()); |
| 89 | else |
| 90 | return getUpper() - 1; |
| 91 | } |
| 92 | |
| 93 | /// getUnsignedMin - Return the smallest unsigned value contained in the |
| 94 | /// ConstantRange. |
| 95 | /// |
| 96 | APInt ConstantRange::getUnsignedMin() const { |
| 97 | if (isFullSet() || (isWrappedSet() && getUpper() != 0)) |
| 98 | return APInt::getMinValue(getBitWidth()); |
| 99 | else |
| 100 | return getLower(); |
| 101 | } |
| 102 | |
| 103 | /// getSignedMax - Return the largest signed value contained in the |
| 104 | /// ConstantRange. |
| 105 | /// |
| 106 | APInt ConstantRange::getSignedMax() const { |
Zhou Sheng | daacf22 | 2007-04-13 05:57:32 +0000 | [diff] [blame] | 107 | APInt SignedMax(APInt::getSignedMaxValue(getBitWidth())); |
Nick Lewycky | 3400e6a | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 108 | if (!isWrappedSet()) { |
Nick Lewycky | ae5eb7a | 2007-06-09 04:20:33 +0000 | [diff] [blame] | 109 | if (getLower().sle(getUpper() - 1)) |
Nick Lewycky | 3400e6a | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 110 | return getUpper() - 1; |
| 111 | else |
| 112 | return SignedMax; |
| 113 | } else { |
| 114 | if ((getUpper() - 1).slt(getLower())) { |
| 115 | if (getLower() != SignedMax) |
| 116 | return SignedMax; |
| 117 | else |
| 118 | return getUpper() - 1; |
| 119 | } else { |
| 120 | return getUpper() - 1; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /// getSignedMin - Return the smallest signed value contained in the |
| 126 | /// ConstantRange. |
| 127 | /// |
| 128 | APInt ConstantRange::getSignedMin() const { |
Zhou Sheng | daacf22 | 2007-04-13 05:57:32 +0000 | [diff] [blame] | 129 | APInt SignedMin(APInt::getSignedMinValue(getBitWidth())); |
Nick Lewycky | 3400e6a | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 130 | if (!isWrappedSet()) { |
Nick Lewycky | ae5eb7a | 2007-06-09 04:20:33 +0000 | [diff] [blame] | 131 | if (getLower().sle(getUpper() - 1)) |
Nick Lewycky | 3400e6a | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 132 | return getLower(); |
| 133 | else |
| 134 | return SignedMin; |
| 135 | } else { |
| 136 | if ((getUpper() - 1).slt(getLower())) { |
| 137 | if (getUpper() != SignedMin) |
| 138 | return SignedMin; |
| 139 | else |
| 140 | return getLower(); |
| 141 | } else { |
| 142 | return getLower(); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 147 | /// contains - Return true if the specified value is in the set. |
| 148 | /// |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 149 | bool ConstantRange::contains(const APInt &V) const { |
| 150 | if (Lower == Upper) |
| 151 | return isFullSet(); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 152 | |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 153 | if (!isWrappedSet()) |
| 154 | return Lower.ule(V) && V.ult(Upper); |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 155 | else |
| 156 | return Lower.ule(V) || V.ult(Upper); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 159 | /// subtract - Subtract the specified constant from the endpoints of this |
| 160 | /// constant range. |
Reid Spencer | 581b0d4 | 2007-02-28 19:57:34 +0000 | [diff] [blame] | 161 | ConstantRange ConstantRange::subtract(const APInt &Val) const { |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 162 | assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width"); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 163 | // If the set is empty or full, don't modify the endpoints. |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 164 | if (Lower == Upper) |
| 165 | return *this; |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 166 | return ConstantRange(Lower - Val, Upper - Val); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 167 | } |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 168 | |
| 169 | |
| 170 | // intersect1Wrapped - This helper function is used to intersect two ranges when |
| 171 | // it is known that LHS is wrapped and RHS isn't. |
| 172 | // |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 173 | ConstantRange |
| 174 | ConstantRange::intersect1Wrapped(const ConstantRange &LHS, |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 175 | const ConstantRange &RHS) { |
| 176 | assert(LHS.isWrappedSet() && !RHS.isWrappedSet()); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 177 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 178 | // Check to see if we overlap on the Left side of RHS... |
| 179 | // |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 180 | if (RHS.Lower.ult(LHS.Upper)) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 181 | // We do overlap on the left side of RHS, see if we overlap on the right of |
| 182 | // RHS... |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 183 | if (RHS.Upper.ugt(LHS.Lower)) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 184 | // Ok, the result overlaps on both the left and right sides. See if the |
| 185 | // resultant interval will be smaller if we wrap or not... |
| 186 | // |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 187 | if (LHS.getSetSize().ult(RHS.getSetSize())) |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 188 | return LHS; |
| 189 | else |
| 190 | return RHS; |
| 191 | |
| 192 | } else { |
| 193 | // No overlap on the right, just on the left. |
Reid Spencer | dc5c159 | 2007-02-28 18:57:32 +0000 | [diff] [blame] | 194 | return ConstantRange(RHS.Lower, LHS.Upper); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 195 | } |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 196 | } else { |
| 197 | // We don't overlap on the left side of RHS, see if we overlap on the right |
| 198 | // of RHS... |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 199 | if (RHS.Upper.ugt(LHS.Lower)) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 200 | // Simple overlap... |
Reid Spencer | dc5c159 | 2007-02-28 18:57:32 +0000 | [diff] [blame] | 201 | return ConstantRange(LHS.Lower, RHS.Upper); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 202 | } else { |
| 203 | // No overlap... |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 204 | return ConstantRange(LHS.getBitWidth(), false); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
Nick Lewycky | 3e05164 | 2007-02-11 00:58:49 +0000 | [diff] [blame] | 209 | /// intersectWith - Return the range that results from the intersection of this |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 210 | /// range with another range. |
| 211 | /// |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 212 | ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const { |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 213 | assert(getBitWidth() == CR.getBitWidth() && |
| 214 | "ConstantRange types don't agree!"); |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 215 | // Handle common special cases |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 216 | if (isEmptySet() || CR.isFullSet()) |
| 217 | return *this; |
| 218 | if (isFullSet() || CR.isEmptySet()) |
| 219 | return CR; |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 220 | |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 221 | if (!isWrappedSet()) { |
| 222 | if (!CR.isWrappedSet()) { |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 223 | APInt L = APIntOps::umax(Lower, CR.Lower); |
| 224 | APInt U = APIntOps::umin(Upper, CR.Upper); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 225 | |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 226 | if (L.ult(U)) // If range isn't empty... |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 227 | return ConstantRange(L, U); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 228 | else |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 229 | return ConstantRange(getBitWidth(), false);// Otherwise, empty set |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 230 | } else |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 231 | return intersect1Wrapped(CR, *this); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 232 | } else { // We know "this" is wrapped... |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 233 | if (!CR.isWrappedSet()) |
| 234 | return intersect1Wrapped(*this, CR); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 235 | else { |
| 236 | // Both ranges are wrapped... |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 237 | APInt L = APIntOps::umax(Lower, CR.Lower); |
| 238 | APInt U = APIntOps::umin(Upper, CR.Upper); |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 239 | return ConstantRange(L, U); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | return *this; |
| 243 | } |
| 244 | |
Nick Lewycky | 377b119 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 245 | /// maximalIntersectWith - Return the range that results from the intersection |
| 246 | /// of this range with another range. The resultant range is guaranteed to |
Nick Lewycky | 28753f8 | 2007-07-14 17:41:03 +0000 | [diff] [blame] | 247 | /// include all elements contained in both input ranges, and to have the |
| 248 | /// smallest possible set size that does so. Because there may be two |
| 249 | /// intersections with the same set size, A.maximalIntersectWith(B) might not |
| 250 | /// be equal to B.maximalIntersect(A). |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 251 | ConstantRange |
| 252 | ConstantRange::maximalIntersectWith(const ConstantRange &CR) const { |
Nick Lewycky | 377b119 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 253 | assert(getBitWidth() == CR.getBitWidth() && |
| 254 | "ConstantRange types don't agree!"); |
| 255 | |
| 256 | // Handle common cases. |
| 257 | if ( isEmptySet() || CR.isFullSet()) return *this; |
| 258 | if (CR.isEmptySet() || isFullSet()) return CR; |
| 259 | |
| 260 | if (!isWrappedSet() && CR.isWrappedSet()) |
| 261 | return CR.maximalIntersectWith(*this); |
| 262 | |
| 263 | if (!isWrappedSet() && !CR.isWrappedSet()) { |
| 264 | if (Lower.ult(CR.Lower)) { |
| 265 | if (Upper.ule(CR.Lower)) |
| 266 | return ConstantRange(getBitWidth(), false); |
| 267 | |
| 268 | if (Upper.ult(CR.Upper)) |
| 269 | return ConstantRange(CR.Lower, Upper); |
| 270 | |
| 271 | return CR; |
| 272 | } else { |
| 273 | if (Upper.ult(CR.Upper)) |
| 274 | return *this; |
| 275 | |
| 276 | if (Lower.ult(CR.Upper)) |
| 277 | return ConstantRange(Lower, CR.Upper); |
| 278 | |
| 279 | return ConstantRange(getBitWidth(), false); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | if (isWrappedSet() && !CR.isWrappedSet()) { |
| 284 | if (CR.Lower.ult(Upper)) { |
| 285 | if (CR.Upper.ult(Upper)) |
| 286 | return CR; |
| 287 | |
| 288 | if (CR.Upper.ult(Lower)) |
| 289 | return ConstantRange(CR.Lower, Upper); |
| 290 | |
| 291 | if (getSetSize().ult(CR.getSetSize())) |
| 292 | return *this; |
| 293 | else |
| 294 | return CR; |
| 295 | } else if (CR.Lower.ult(Lower)) { |
| 296 | if (CR.Upper.ule(Lower)) |
| 297 | return ConstantRange(getBitWidth(), false); |
| 298 | |
| 299 | return ConstantRange(Lower, CR.Upper); |
| 300 | } |
| 301 | return CR; |
| 302 | } |
| 303 | |
| 304 | if (CR.Upper.ult(Upper)) { |
| 305 | if (CR.Lower.ult(Upper)) { |
| 306 | if (getSetSize().ult(CR.getSetSize())) |
| 307 | return *this; |
| 308 | else |
| 309 | return CR; |
| 310 | } |
| 311 | |
| 312 | if (CR.Lower.ult(Lower)) |
| 313 | return ConstantRange(Lower, CR.Upper); |
| 314 | |
| 315 | return CR; |
| 316 | } else if (CR.Upper.ult(Lower)) { |
| 317 | if (CR.Lower.ult(Lower)) |
| 318 | return *this; |
| 319 | |
| 320 | return ConstantRange(CR.Lower, Upper); |
| 321 | } |
| 322 | if (getSetSize().ult(CR.getSetSize())) |
| 323 | return *this; |
| 324 | else |
| 325 | return CR; |
| 326 | } |
| 327 | |
| 328 | |
Nick Lewycky | 3e05164 | 2007-02-11 00:58:49 +0000 | [diff] [blame] | 329 | /// unionWith - Return the range that results from the union of this range with |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 330 | /// another range. The resultant range is guaranteed to include the elements of |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 331 | /// both sets, but may contain more. For example, [3, 9) union [12,15) is |
| 332 | /// [3, 15), which includes 9, 10, and 11, which were not included in either |
| 333 | /// set before. |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 334 | /// |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 335 | ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const { |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 336 | assert(getBitWidth() == CR.getBitWidth() && |
| 337 | "ConstantRange types don't agree!"); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 338 | |
Nick Lewycky | c6a28fc | 2007-03-02 03:33:05 +0000 | [diff] [blame] | 339 | if ( isFullSet() || CR.isEmptySet()) return *this; |
| 340 | if (CR.isFullSet() || isEmptySet()) return CR; |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 341 | |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 342 | if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this); |
| 343 | |
Nick Lewycky | c6a28fc | 2007-03-02 03:33:05 +0000 | [diff] [blame] | 344 | APInt L = Lower, U = Upper; |
| 345 | |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 346 | if (!isWrappedSet() && !CR.isWrappedSet()) { |
| 347 | if (CR.Lower.ult(L)) |
| 348 | L = CR.Lower; |
Nick Lewycky | c6a28fc | 2007-03-02 03:33:05 +0000 | [diff] [blame] | 349 | |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 350 | if (CR.Upper.ugt(U)) |
| 351 | U = CR.Upper; |
| 352 | } |
| 353 | |
| 354 | if (isWrappedSet() && !CR.isWrappedSet()) { |
| 355 | if ((CR.Lower.ult(Upper) && CR.Upper.ult(Upper)) || |
| 356 | (CR.Lower.ugt(Lower) && CR.Upper.ugt(Lower))) { |
| 357 | return *this; |
| 358 | } |
| 359 | |
| 360 | if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) { |
| 361 | return ConstantRange(getBitWidth()); |
| 362 | } |
| 363 | |
| 364 | if (CR.Lower.ule(Upper) && CR.Upper.ule(Lower)) { |
| 365 | APInt d1 = CR.Upper - Upper, d2 = Lower - CR.Upper; |
| 366 | if (d1.ult(d2)) { |
| 367 | U = CR.Upper; |
| 368 | } else { |
| 369 | L = CR.Upper; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | if (Upper.ult(CR.Lower) && CR.Upper.ult(Lower)) { |
| 374 | APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper; |
| 375 | if (d1.ult(d2)) { |
| 376 | U = CR.Lower + 1; |
| 377 | } else { |
| 378 | L = CR.Upper - 1; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) { |
| 383 | APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Lower; |
| 384 | |
| 385 | if (d1.ult(d2)) { |
| 386 | U = CR.Lower + 1; |
| 387 | } else { |
| 388 | L = CR.Lower; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | if (isWrappedSet() && CR.isWrappedSet()) { |
| 394 | if (Lower.ult(CR.Upper) || CR.Lower.ult(Upper)) |
| 395 | return ConstantRange(getBitWidth()); |
| 396 | |
| 397 | if (CR.Upper.ugt(U)) { |
| 398 | U = CR.Upper; |
| 399 | } |
| 400 | |
| 401 | if (CR.Lower.ult(L)) { |
| 402 | L = CR.Lower; |
| 403 | } |
| 404 | |
| 405 | if (L == U) return ConstantRange(getBitWidth()); |
| 406 | } |
Nick Lewycky | c6a28fc | 2007-03-02 03:33:05 +0000 | [diff] [blame] | 407 | |
| 408 | return ConstantRange(L, U); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 409 | } |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 410 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 411 | /// zeroExtend - Return a new range in the specified integer type, which must |
| 412 | /// be strictly larger than the current type. The returned range will |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 413 | /// correspond to the possible range of values as if the source range had been |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 414 | /// zero extended. |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 415 | ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const { |
| 416 | unsigned SrcTySize = getBitWidth(); |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 417 | assert(SrcTySize < DstTySize && "Not a value extension"); |
Reid Spencer | dc5c159 | 2007-02-28 18:57:32 +0000 | [diff] [blame] | 418 | if (isFullSet()) |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 419 | // Change a source full set into [0, 1 << 8*numbytes) |
Reid Spencer | dc5c159 | 2007-02-28 18:57:32 +0000 | [diff] [blame] | 420 | return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize)); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 421 | |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 422 | APInt L = Lower; L.zext(DstTySize); |
| 423 | APInt U = Upper; U.zext(DstTySize); |
| 424 | return ConstantRange(L, U); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Nick Lewycky | e32157c | 2007-04-07 15:41:33 +0000 | [diff] [blame] | 427 | /// signExtend - Return a new range in the specified integer type, which must |
| 428 | /// be strictly larger than the current type. The returned range will |
| 429 | /// correspond to the possible range of values as if the source range had been |
| 430 | /// sign extended. |
| 431 | ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const { |
| 432 | unsigned SrcTySize = getBitWidth(); |
| 433 | assert(SrcTySize < DstTySize && "Not a value extension"); |
| 434 | if (isFullSet()) { |
| 435 | return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1), |
| 436 | APInt::getLowBitsSet(DstTySize, SrcTySize-1)); |
| 437 | } |
| 438 | |
| 439 | APInt L = Lower; L.sext(DstTySize); |
| 440 | APInt U = Upper; U.sext(DstTySize); |
| 441 | return ConstantRange(L, U); |
| 442 | } |
| 443 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 444 | /// truncate - Return a new range in the specified integer type, which must be |
| 445 | /// strictly smaller than the current type. The returned range will |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 446 | /// correspond to the possible range of values as if the source range had been |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 447 | /// truncated to the specified type. |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 448 | ConstantRange ConstantRange::truncate(uint32_t DstTySize) const { |
| 449 | unsigned SrcTySize = getBitWidth(); |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 450 | assert(SrcTySize > DstTySize && "Not a value truncation"); |
Zhou Sheng | daacf22 | 2007-04-13 05:57:32 +0000 | [diff] [blame] | 451 | APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize)); |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 452 | if (isFullSet() || getSetSize().ugt(Size)) |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 453 | return ConstantRange(DstTySize); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 454 | |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 455 | APInt L = Lower; L.trunc(DstTySize); |
| 456 | APInt U = Upper; U.trunc(DstTySize); |
| 457 | return ConstantRange(L, U); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 460 | ConstantRange |
| 461 | ConstantRange::add(const ConstantRange &Other) const { |
| 462 | if (isEmptySet() || Other.isEmptySet()) |
| 463 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 464 | |
| 465 | APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize(); |
| 466 | APInt NewLower = getLower() + Other.getLower(); |
| 467 | APInt NewUpper = getUpper() + Other.getUpper() - 1; |
| 468 | if (NewLower == NewUpper) |
| 469 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 470 | |
| 471 | ConstantRange X = ConstantRange(NewLower, NewUpper); |
| 472 | if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y)) |
| 473 | // We've wrapped, therefore, full set. |
| 474 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 475 | |
| 476 | return X; |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 479 | ConstantRange |
| 480 | ConstantRange::multiply(const ConstantRange &Other) const { |
| 481 | // TODO: Implement multiply. |
| 482 | return ConstantRange(getBitWidth(), |
| 483 | !(isEmptySet() || Other.isEmptySet())); |
| 484 | } |
| 485 | |
| 486 | ConstantRange |
| 487 | ConstantRange::smax(const ConstantRange &Other) const { |
Dan Gohman | 38b0644 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 488 | // X smax Y is: range(smax(X_smin, Y_smin), |
| 489 | // smax(X_smax, Y_smax)) |
| 490 | if (isEmptySet() || Other.isEmptySet()) |
| 491 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 492 | if (isFullSet() || Other.isFullSet()) |
| 493 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 494 | APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin()); |
| 495 | APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1; |
| 496 | if (NewU == NewL) |
| 497 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 498 | return ConstantRange(NewL, NewU); |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | ConstantRange |
| 502 | ConstantRange::umax(const ConstantRange &Other) const { |
| 503 | // X umax Y is: range(umax(X_umin, Y_umin), |
| 504 | // umax(X_umax, Y_umax)) |
| 505 | if (isEmptySet() || Other.isEmptySet()) |
| 506 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 507 | if (isFullSet() || Other.isFullSet()) |
| 508 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 509 | APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin()); |
| 510 | APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1; |
| 511 | if (NewU == NewL) |
| 512 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 513 | return ConstantRange(NewL, NewU); |
| 514 | } |
| 515 | |
| 516 | ConstantRange |
| 517 | ConstantRange::udiv(const ConstantRange &Other) const { |
| 518 | // TODO: Implement udiv. |
| 519 | return ConstantRange(getBitWidth(), |
| 520 | !(isEmptySet() || Other.isEmptySet())); |
| 521 | } |
| 522 | |
Dan Gohman | 38b0644 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 523 | /// print - Print out the bounds to a stream... |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 524 | /// |
Dan Gohman | 38b0644 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 525 | void ConstantRange::print(raw_ostream &OS) const { |
| 526 | OS << "[" << Lower << "," << Upper << ")"; |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Dan Gohman | 38b0644 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 529 | /// dump - Allow printing from a debugger easily... |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 530 | /// |
Dan Gohman | 38b0644 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 531 | void ConstantRange::dump() const { |
| 532 | print(errs()); |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Dan Gohman | 38b0644 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 535 | std::ostream &llvm::operator<<(std::ostream &o, |
| 536 | const ConstantRange &CR) { |
| 537 | raw_os_ostream OS(o); |
| 538 | OS << CR; |
| 539 | return o; |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 540 | } |