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