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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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"); |
| 47 | uint32_t BitWidth = L.getBitWidth(); |
| 48 | assert((L != U || (L == APInt::getMaxValue(BitWidth) || |
| 49 | L == APInt::getMinValue(BitWidth))) && |
| 50 | "Lower == Upper, but they aren't min or max value!"); |
| 51 | } |
| 52 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 53 | /// isFullSet - Return true if this set contains all of the elements possible |
| 54 | /// for this data-type |
| 55 | bool ConstantRange::isFullSet() const { |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 56 | return Lower == Upper && Lower == APInt::getMaxValue(getBitWidth()); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 57 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 59 | /// isEmptySet - Return true if this set contains no members. |
| 60 | /// |
| 61 | bool ConstantRange::isEmptySet() const { |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 62 | return Lower == Upper && Lower == APInt::getMinValue(getBitWidth()); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /// isWrappedSet - Return true if this set wraps around the top of the range, |
| 66 | /// for example: [100, 8) |
| 67 | /// |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 68 | bool ConstantRange::isWrappedSet() const { |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 69 | return Lower.ugt(Upper); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 72 | /// getSetSize - Return the number of elements in this set. |
| 73 | /// |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 74 | APInt ConstantRange::getSetSize() const { |
| 75 | if (isEmptySet()) |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 76 | return APInt(getBitWidth(), 0); |
| 77 | if (getBitWidth() == 1) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 78 | if (Lower != Upper) // One of T or F in the set... |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 79 | return APInt(2, 1); |
| 80 | return APInt(2, 2); // Must be full set... |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 81 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 83 | // Simply subtract the bounds... |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 84 | return Upper - Lower; |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Nick Lewycky | 3400e6a | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 87 | /// getUnsignedMax - Return the largest unsigned value contained in the |
| 88 | /// ConstantRange. |
| 89 | /// |
| 90 | APInt ConstantRange::getUnsignedMax() const { |
| 91 | if (isFullSet() || isWrappedSet()) |
| 92 | return APInt::getMaxValue(getBitWidth()); |
| 93 | else |
| 94 | return getUpper() - 1; |
| 95 | } |
| 96 | |
| 97 | /// getUnsignedMin - Return the smallest unsigned value contained in the |
| 98 | /// ConstantRange. |
| 99 | /// |
| 100 | APInt ConstantRange::getUnsignedMin() const { |
| 101 | if (isFullSet() || (isWrappedSet() && getUpper() != 0)) |
| 102 | return APInt::getMinValue(getBitWidth()); |
| 103 | else |
| 104 | return getLower(); |
| 105 | } |
| 106 | |
| 107 | /// getSignedMax - Return the largest signed value contained in the |
| 108 | /// ConstantRange. |
| 109 | /// |
| 110 | APInt ConstantRange::getSignedMax() const { |
| 111 | APInt SignedMax = APInt::getSignedMaxValue(getBitWidth()); |
| 112 | if (!isWrappedSet()) { |
| 113 | if (getLower().slt(getUpper() - 1)) |
| 114 | return getUpper() - 1; |
| 115 | else |
| 116 | return SignedMax; |
| 117 | } else { |
| 118 | if ((getUpper() - 1).slt(getLower())) { |
| 119 | if (getLower() != SignedMax) |
| 120 | return SignedMax; |
| 121 | else |
| 122 | return getUpper() - 1; |
| 123 | } else { |
| 124 | return getUpper() - 1; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /// getSignedMin - Return the smallest signed value contained in the |
| 130 | /// ConstantRange. |
| 131 | /// |
| 132 | APInt ConstantRange::getSignedMin() const { |
| 133 | APInt SignedMin = APInt::getSignedMinValue(getBitWidth()); |
| 134 | if (!isWrappedSet()) { |
| 135 | if (getLower().slt(getUpper() - 1)) |
| 136 | return getLower(); |
| 137 | else |
| 138 | return SignedMin; |
| 139 | } else { |
| 140 | if ((getUpper() - 1).slt(getLower())) { |
| 141 | if (getUpper() != SignedMin) |
| 142 | return SignedMin; |
| 143 | else |
| 144 | return getLower(); |
| 145 | } else { |
| 146 | return getLower(); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 151 | /// contains - Return true if the specified value is in the set. |
| 152 | /// |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 153 | bool ConstantRange::contains(const APInt &V) const { |
| 154 | if (Lower == Upper) |
| 155 | return isFullSet(); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 156 | |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 157 | if (!isWrappedSet()) |
| 158 | return Lower.ule(V) && V.ult(Upper); |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 159 | else |
| 160 | return Lower.ule(V) || V.ult(Upper); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 163 | /// subtract - Subtract the specified constant from the endpoints of this |
| 164 | /// constant range. |
Reid Spencer | 581b0d4 | 2007-02-28 19:57:34 +0000 | [diff] [blame] | 165 | ConstantRange ConstantRange::subtract(const APInt &Val) const { |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 166 | assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width"); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 167 | // If the set is empty or full, don't modify the endpoints. |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 168 | if (Lower == Upper) |
| 169 | return *this; |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 170 | return ConstantRange(Lower - Val, Upper - Val); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 171 | } |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 172 | |
| 173 | |
| 174 | // intersect1Wrapped - This helper function is used to intersect two ranges when |
| 175 | // it is known that LHS is wrapped and RHS isn't. |
| 176 | // |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 177 | ConstantRange |
| 178 | ConstantRange::intersect1Wrapped(const ConstantRange &LHS, |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 179 | const ConstantRange &RHS) { |
| 180 | assert(LHS.isWrappedSet() && !RHS.isWrappedSet()); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 181 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 182 | // Check to see if we overlap on the Left side of RHS... |
| 183 | // |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 184 | if (RHS.Lower.ult(LHS.Upper)) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 185 | // We do overlap on the left side of RHS, see if we overlap on the right of |
| 186 | // RHS... |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 187 | if (RHS.Upper.ugt(LHS.Lower)) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 188 | // Ok, the result overlaps on both the left and right sides. See if the |
| 189 | // resultant interval will be smaller if we wrap or not... |
| 190 | // |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 191 | if (LHS.getSetSize().ult(RHS.getSetSize())) |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 192 | return LHS; |
| 193 | else |
| 194 | return RHS; |
| 195 | |
| 196 | } else { |
| 197 | // No overlap on the right, just on the left. |
Reid Spencer | dc5c159 | 2007-02-28 18:57:32 +0000 | [diff] [blame] | 198 | return ConstantRange(RHS.Lower, LHS.Upper); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 199 | } |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 200 | } else { |
| 201 | // We don't overlap on the left side of RHS, see if we overlap on the right |
| 202 | // of RHS... |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 203 | if (RHS.Upper.ugt(LHS.Lower)) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 204 | // Simple overlap... |
Reid Spencer | dc5c159 | 2007-02-28 18:57:32 +0000 | [diff] [blame] | 205 | return ConstantRange(LHS.Lower, RHS.Upper); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 206 | } else { |
| 207 | // No overlap... |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 208 | return ConstantRange(LHS.getBitWidth(), false); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
Nick Lewycky | 3e05164 | 2007-02-11 00:58:49 +0000 | [diff] [blame] | 213 | /// intersectWith - Return the range that results from the intersection of this |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 214 | /// range with another range. |
| 215 | /// |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 216 | ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const { |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 217 | assert(getBitWidth() == CR.getBitWidth() && |
| 218 | "ConstantRange types don't agree!"); |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 219 | // Handle common special cases |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 220 | if (isEmptySet() || CR.isFullSet()) |
| 221 | return *this; |
| 222 | if (isFullSet() || CR.isEmptySet()) |
| 223 | return CR; |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 224 | |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 225 | if (!isWrappedSet()) { |
| 226 | if (!CR.isWrappedSet()) { |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 227 | using namespace APIntOps; |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 228 | APInt L = umax(Lower, CR.Lower); |
| 229 | APInt U = umin(Upper, CR.Upper); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 230 | |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 231 | if (L.ult(U)) // If range isn't empty... |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 232 | return ConstantRange(L, U); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 233 | else |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 234 | return ConstantRange(getBitWidth(), false);// Otherwise, empty set |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 235 | } else |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 236 | return intersect1Wrapped(CR, *this); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 237 | } else { // We know "this" is wrapped... |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 238 | if (!CR.isWrappedSet()) |
| 239 | return intersect1Wrapped(*this, CR); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 240 | else { |
| 241 | // Both ranges are wrapped... |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 242 | using namespace APIntOps; |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 243 | APInt L = umax(Lower, CR.Lower); |
| 244 | APInt U = umin(Upper, CR.Upper); |
Chris Lattner | d122f4b | 2002-09-02 20:49:27 +0000 | [diff] [blame] | 245 | return ConstantRange(L, U); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | return *this; |
| 249 | } |
| 250 | |
Nick Lewycky | 3e05164 | 2007-02-11 00:58:49 +0000 | [diff] [blame] | 251 | /// 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] | 252 | /// another range. The resultant range is guaranteed to include the elements of |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 253 | /// both sets, but may contain more. For example, [3, 9) union [12,15) is |
| 254 | /// [3, 15), which includes 9, 10, and 11, which were not included in either |
| 255 | /// set before. |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 256 | /// |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 257 | ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const { |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 258 | assert(getBitWidth() == CR.getBitWidth() && |
| 259 | "ConstantRange types don't agree!"); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 260 | |
Nick Lewycky | c6a28fc | 2007-03-02 03:33:05 +0000 | [diff] [blame] | 261 | if ( isFullSet() || CR.isEmptySet()) return *this; |
| 262 | if (CR.isFullSet() || isEmptySet()) return CR; |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 263 | |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 264 | if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this); |
| 265 | |
Nick Lewycky | c6a28fc | 2007-03-02 03:33:05 +0000 | [diff] [blame] | 266 | APInt L = Lower, U = Upper; |
| 267 | |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 268 | if (!isWrappedSet() && !CR.isWrappedSet()) { |
| 269 | if (CR.Lower.ult(L)) |
| 270 | L = CR.Lower; |
Nick Lewycky | c6a28fc | 2007-03-02 03:33:05 +0000 | [diff] [blame] | 271 | |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 272 | if (CR.Upper.ugt(U)) |
| 273 | U = CR.Upper; |
| 274 | } |
| 275 | |
| 276 | if (isWrappedSet() && !CR.isWrappedSet()) { |
| 277 | if ((CR.Lower.ult(Upper) && CR.Upper.ult(Upper)) || |
| 278 | (CR.Lower.ugt(Lower) && CR.Upper.ugt(Lower))) { |
| 279 | return *this; |
| 280 | } |
| 281 | |
| 282 | if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) { |
| 283 | return ConstantRange(getBitWidth()); |
| 284 | } |
| 285 | |
| 286 | if (CR.Lower.ule(Upper) && CR.Upper.ule(Lower)) { |
| 287 | APInt d1 = CR.Upper - Upper, d2 = Lower - CR.Upper; |
| 288 | if (d1.ult(d2)) { |
| 289 | U = CR.Upper; |
| 290 | } else { |
| 291 | L = CR.Upper; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | if (Upper.ult(CR.Lower) && CR.Upper.ult(Lower)) { |
| 296 | APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper; |
| 297 | if (d1.ult(d2)) { |
| 298 | U = CR.Lower + 1; |
| 299 | } else { |
| 300 | L = CR.Upper - 1; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) { |
| 305 | APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Lower; |
| 306 | |
| 307 | if (d1.ult(d2)) { |
| 308 | U = CR.Lower + 1; |
| 309 | } else { |
| 310 | L = CR.Lower; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if (isWrappedSet() && CR.isWrappedSet()) { |
| 316 | if (Lower.ult(CR.Upper) || CR.Lower.ult(Upper)) |
| 317 | return ConstantRange(getBitWidth()); |
| 318 | |
| 319 | if (CR.Upper.ugt(U)) { |
| 320 | U = CR.Upper; |
| 321 | } |
| 322 | |
| 323 | if (CR.Lower.ult(L)) { |
| 324 | L = CR.Lower; |
| 325 | } |
| 326 | |
| 327 | if (L == U) return ConstantRange(getBitWidth()); |
| 328 | } |
Nick Lewycky | c6a28fc | 2007-03-02 03:33:05 +0000 | [diff] [blame] | 329 | |
| 330 | return ConstantRange(L, U); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 331 | } |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 332 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 333 | /// zeroExtend - Return a new range in the specified integer type, which must |
| 334 | /// be strictly larger than the current type. The returned range will |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 335 | /// 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] | 336 | /// zero extended. |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 337 | ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const { |
| 338 | unsigned SrcTySize = getBitWidth(); |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 339 | assert(SrcTySize < DstTySize && "Not a value extension"); |
Reid Spencer | dc5c159 | 2007-02-28 18:57:32 +0000 | [diff] [blame] | 340 | if (isFullSet()) |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 341 | // Change a source full set into [0, 1 << 8*numbytes) |
Reid Spencer | dc5c159 | 2007-02-28 18:57:32 +0000 | [diff] [blame] | 342 | return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize)); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 343 | |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 344 | APInt L = Lower; L.zext(DstTySize); |
| 345 | APInt U = Upper; U.zext(DstTySize); |
| 346 | return ConstantRange(L, U); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /// truncate - Return a new range in the specified integer type, which must be |
| 350 | /// strictly smaller than the current type. The returned range will |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 351 | /// 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] | 352 | /// truncated to the specified type. |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 353 | ConstantRange ConstantRange::truncate(uint32_t DstTySize) const { |
| 354 | unsigned SrcTySize = getBitWidth(); |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 355 | assert(SrcTySize > DstTySize && "Not a value truncation"); |
| 356 | APInt Size = APInt::getMaxValue(DstTySize).zext(SrcTySize); |
| 357 | if (isFullSet() || getSetSize().ugt(Size)) |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 358 | return ConstantRange(DstTySize); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 359 | |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 360 | APInt L = Lower; L.trunc(DstTySize); |
| 361 | APInt U = Upper; U.trunc(DstTySize); |
| 362 | return ConstantRange(L, U); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 365 | /// print - Print out the bounds to a stream... |
| 366 | /// |
| 367 | void ConstantRange::print(std::ostream &OS) const { |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 368 | OS << "[" << Lower.toStringSigned(10) << "," |
| 369 | << Upper.toStringSigned(10) << " )"; |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /// dump - Allow printing from a debugger easily... |
| 373 | /// |
| 374 | void ConstantRange::dump() const { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 375 | print(cerr); |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 376 | } |