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 | |
Owen Anderson | 634bab1 | 2010-08-07 00:42:06 +0000 | [diff] [blame] | 24 | #include "llvm/Constants.h" |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ConstantRange.h" |
David Greene | 7fd5fb4 | 2010-01-05 01:28:32 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Nick Lewycky | bf8c7f0 | 2009-07-11 06:15:39 +0000 | [diff] [blame] | 28 | #include "llvm/Instructions.h" |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 31 | /// Initialize a full (the default) or empty set for the specified type. |
| 32 | /// |
Dan Gohman | 38b0644 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 33 | ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 34 | if (Full) |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 35 | Lower = Upper = APInt::getMaxValue(BitWidth); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 36 | else |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 37 | Lower = Upper = APInt::getMinValue(BitWidth); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 40 | /// Initialize a range to hold the single specified value. |
| 41 | /// |
Nick Lewycky | 7f9ef4b | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 42 | ConstantRange::ConstantRange(const APInt &V) : Lower(V), Upper(V + 1) {} |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 43 | |
Dan Gohman | 38b0644 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 44 | ConstantRange::ConstantRange(const APInt &L, const APInt &U) : |
| 45 | Lower(L), Upper(U) { |
| 46 | assert(L.getBitWidth() == U.getBitWidth() && |
| 47 | "ConstantRange with unequal bit widths"); |
Zhou Sheng | c125c00 | 2007-04-26 16:42:07 +0000 | [diff] [blame] | 48 | assert((L != U || (L.isMaxValue() || L.isMinValue())) && |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 49 | "Lower == Upper, but they aren't min or max value!"); |
| 50 | } |
| 51 | |
Nick Lewycky | bf8c7f0 | 2009-07-11 06:15:39 +0000 | [diff] [blame] | 52 | ConstantRange ConstantRange::makeICmpRegion(unsigned Pred, |
| 53 | const ConstantRange &CR) { |
Nick Lewycky | f2d7b7c | 2010-09-28 18:18:36 +0000 | [diff] [blame] | 54 | if (CR.isEmptySet()) |
| 55 | return CR; |
| 56 | |
Nick Lewycky | bf8c7f0 | 2009-07-11 06:15:39 +0000 | [diff] [blame] | 57 | uint32_t W = CR.getBitWidth(); |
| 58 | switch (Pred) { |
| 59 | default: assert(!"Invalid ICmp predicate to makeICmpRegion()"); |
| 60 | case ICmpInst::ICMP_EQ: |
Nick Lewycky | f067a23 | 2009-07-11 17:04:01 +0000 | [diff] [blame] | 61 | return CR; |
Nick Lewycky | bf8c7f0 | 2009-07-11 06:15:39 +0000 | [diff] [blame] | 62 | case ICmpInst::ICMP_NE: |
| 63 | if (CR.isSingleElement()) |
| 64 | return ConstantRange(CR.getUpper(), CR.getLower()); |
| 65 | return ConstantRange(W); |
Nick Lewycky | f2d7b7c | 2010-09-28 18:18:36 +0000 | [diff] [blame] | 66 | case ICmpInst::ICMP_ULT: { |
| 67 | APInt UMax(CR.getUnsignedMax()); |
| 68 | if (UMax.isMinValue()) |
| 69 | return ConstantRange(W, /* empty */ false); |
| 70 | return ConstantRange(APInt::getMinValue(W), UMax); |
| 71 | } |
| 72 | case ICmpInst::ICMP_SLT: { |
| 73 | APInt SMax(CR.getSignedMax()); |
| 74 | if (SMax.isMinSignedValue()) |
| 75 | return ConstantRange(W, /* empty */ false); |
| 76 | return ConstantRange(APInt::getSignedMinValue(W), SMax); |
| 77 | } |
Nick Lewycky | bf8c7f0 | 2009-07-11 06:15:39 +0000 | [diff] [blame] | 78 | case ICmpInst::ICMP_ULE: { |
| 79 | APInt UMax(CR.getUnsignedMax()); |
| 80 | if (UMax.isMaxValue()) |
| 81 | return ConstantRange(W); |
| 82 | return ConstantRange(APInt::getMinValue(W), UMax + 1); |
| 83 | } |
| 84 | case ICmpInst::ICMP_SLE: { |
| 85 | APInt SMax(CR.getSignedMax()); |
Nick Lewycky | f2d7b7c | 2010-09-28 18:18:36 +0000 | [diff] [blame] | 86 | if (SMax.isMaxSignedValue()) |
Nick Lewycky | bf8c7f0 | 2009-07-11 06:15:39 +0000 | [diff] [blame] | 87 | return ConstantRange(W); |
| 88 | return ConstantRange(APInt::getSignedMinValue(W), SMax + 1); |
| 89 | } |
Nick Lewycky | f2d7b7c | 2010-09-28 18:18:36 +0000 | [diff] [blame] | 90 | case ICmpInst::ICMP_UGT: { |
| 91 | APInt UMin(CR.getUnsignedMin()); |
| 92 | if (UMin.isMaxValue()) |
| 93 | return ConstantRange(W, /* empty */ false); |
| 94 | return ConstantRange(UMin + 1, APInt::getNullValue(W)); |
| 95 | } |
| 96 | case ICmpInst::ICMP_SGT: { |
| 97 | APInt SMin(CR.getSignedMin()); |
| 98 | if (SMin.isMaxSignedValue()) |
| 99 | return ConstantRange(W, /* empty */ false); |
| 100 | return ConstantRange(SMin + 1, APInt::getSignedMinValue(W)); |
| 101 | } |
Nick Lewycky | bf8c7f0 | 2009-07-11 06:15:39 +0000 | [diff] [blame] | 102 | case ICmpInst::ICMP_UGE: { |
| 103 | APInt UMin(CR.getUnsignedMin()); |
| 104 | if (UMin.isMinValue()) |
| 105 | return ConstantRange(W); |
| 106 | return ConstantRange(UMin, APInt::getNullValue(W)); |
| 107 | } |
| 108 | case ICmpInst::ICMP_SGE: { |
| 109 | APInt SMin(CR.getSignedMin()); |
| 110 | if (SMin.isMinSignedValue()) |
| 111 | return ConstantRange(W); |
| 112 | return ConstantRange(SMin, APInt::getSignedMinValue(W)); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 117 | /// isFullSet - Return true if this set contains all of the elements possible |
| 118 | /// for this data-type |
| 119 | bool ConstantRange::isFullSet() const { |
Zhou Sheng | c125c00 | 2007-04-26 16:42:07 +0000 | [diff] [blame] | 120 | return Lower == Upper && Lower.isMaxValue(); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 121 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 123 | /// isEmptySet - Return true if this set contains no members. |
| 124 | /// |
| 125 | bool ConstantRange::isEmptySet() const { |
Zhou Sheng | c125c00 | 2007-04-26 16:42:07 +0000 | [diff] [blame] | 126 | return Lower == Upper && Lower.isMinValue(); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /// isWrappedSet - Return true if this set wraps around the top of the range, |
| 130 | /// for example: [100, 8) |
| 131 | /// |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 132 | bool ConstantRange::isWrappedSet() const { |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 133 | return Lower.ugt(Upper); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Nick Lewycky | 32cda11 | 2010-09-06 23:52:49 +0000 | [diff] [blame] | 136 | /// isSignWrappedSet - Return true if this set wraps around the INT_MIN of |
| 137 | /// its bitwidth, for example: i8 [120, 140). |
| 138 | /// |
| 139 | bool ConstantRange::isSignWrappedSet() const { |
| 140 | return contains(APInt::getSignedMaxValue(getBitWidth())) && |
| 141 | contains(APInt::getSignedMinValue(getBitWidth())); |
| 142 | } |
| 143 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 144 | /// getSetSize - Return the number of elements in this set. |
| 145 | /// |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 146 | APInt ConstantRange::getSetSize() const { |
| 147 | if (isEmptySet()) |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 148 | return APInt(getBitWidth(), 0); |
| 149 | if (getBitWidth() == 1) { |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 150 | if (Lower != Upper) // One of T or F in the set... |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 151 | return APInt(2, 1); |
| 152 | return APInt(2, 2); // Must be full set... |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 153 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 155 | // Simply subtract the bounds... |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 156 | return Upper - Lower; |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Nick Lewycky | 3400e6a | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 159 | /// getUnsignedMax - Return the largest unsigned value contained in the |
| 160 | /// ConstantRange. |
| 161 | /// |
| 162 | APInt ConstantRange::getUnsignedMax() const { |
| 163 | if (isFullSet() || isWrappedSet()) |
| 164 | return APInt::getMaxValue(getBitWidth()); |
| 165 | else |
| 166 | return getUpper() - 1; |
| 167 | } |
| 168 | |
| 169 | /// getUnsignedMin - Return the smallest unsigned value contained in the |
| 170 | /// ConstantRange. |
| 171 | /// |
| 172 | APInt ConstantRange::getUnsignedMin() const { |
| 173 | if (isFullSet() || (isWrappedSet() && getUpper() != 0)) |
| 174 | return APInt::getMinValue(getBitWidth()); |
| 175 | else |
| 176 | return getLower(); |
| 177 | } |
| 178 | |
| 179 | /// getSignedMax - Return the largest signed value contained in the |
| 180 | /// ConstantRange. |
| 181 | /// |
| 182 | APInt ConstantRange::getSignedMax() const { |
Zhou Sheng | daacf22 | 2007-04-13 05:57:32 +0000 | [diff] [blame] | 183 | APInt SignedMax(APInt::getSignedMaxValue(getBitWidth())); |
Nick Lewycky | 3400e6a | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 184 | if (!isWrappedSet()) { |
Nick Lewycky | ae5eb7a | 2007-06-09 04:20:33 +0000 | [diff] [blame] | 185 | if (getLower().sle(getUpper() - 1)) |
Nick Lewycky | 3400e6a | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 186 | return getUpper() - 1; |
| 187 | else |
| 188 | return SignedMax; |
| 189 | } else { |
Nick Lewycky | 780905e | 2009-07-13 04:50:21 +0000 | [diff] [blame] | 190 | if (getLower().isNegative() == getUpper().isNegative()) |
| 191 | return SignedMax; |
| 192 | else |
Nick Lewycky | 3400e6a | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 193 | return getUpper() - 1; |
Nick Lewycky | 3400e6a | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | /// getSignedMin - Return the smallest signed value contained in the |
| 198 | /// ConstantRange. |
| 199 | /// |
| 200 | APInt ConstantRange::getSignedMin() const { |
Zhou Sheng | daacf22 | 2007-04-13 05:57:32 +0000 | [diff] [blame] | 201 | APInt SignedMin(APInt::getSignedMinValue(getBitWidth())); |
Nick Lewycky | 3400e6a | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 202 | if (!isWrappedSet()) { |
Nick Lewycky | ae5eb7a | 2007-06-09 04:20:33 +0000 | [diff] [blame] | 203 | if (getLower().sle(getUpper() - 1)) |
Nick Lewycky | 3400e6a | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 204 | return getLower(); |
| 205 | else |
| 206 | return SignedMin; |
| 207 | } else { |
| 208 | if ((getUpper() - 1).slt(getLower())) { |
| 209 | if (getUpper() != SignedMin) |
| 210 | return SignedMin; |
| 211 | else |
| 212 | return getLower(); |
| 213 | } else { |
| 214 | return getLower(); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 219 | /// contains - Return true if the specified value is in the set. |
| 220 | /// |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 221 | bool ConstantRange::contains(const APInt &V) const { |
| 222 | if (Lower == Upper) |
| 223 | return isFullSet(); |
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 | return Lower.ule(V) && V.ult(Upper); |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 227 | else |
| 228 | return Lower.ule(V) || V.ult(Upper); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Nick Lewycky | bf8c7f0 | 2009-07-11 06:15:39 +0000 | [diff] [blame] | 231 | /// contains - Return true if the argument is a subset of this range. |
Nick Lewycky | 7f9ef4b | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 232 | /// Two equal sets contain each other. The empty set contained by all other |
| 233 | /// sets. |
Nick Lewycky | bf8c7f0 | 2009-07-11 06:15:39 +0000 | [diff] [blame] | 234 | /// |
| 235 | bool ConstantRange::contains(const ConstantRange &Other) const { |
Nick Lewycky | 7f9ef4b | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 236 | if (isFullSet() || Other.isEmptySet()) return true; |
| 237 | if (isEmptySet() || Other.isFullSet()) return false; |
Nick Lewycky | bf8c7f0 | 2009-07-11 06:15:39 +0000 | [diff] [blame] | 238 | |
| 239 | if (!isWrappedSet()) { |
| 240 | if (Other.isWrappedSet()) |
| 241 | return false; |
| 242 | |
| 243 | return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper); |
| 244 | } |
| 245 | |
| 246 | if (!Other.isWrappedSet()) |
| 247 | return Other.getUpper().ule(Upper) || |
| 248 | Lower.ule(Other.getLower()); |
| 249 | |
| 250 | return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower()); |
| 251 | } |
| 252 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 253 | /// subtract - Subtract the specified constant from the endpoints of this |
| 254 | /// constant range. |
Reid Spencer | 581b0d4 | 2007-02-28 19:57:34 +0000 | [diff] [blame] | 255 | ConstantRange ConstantRange::subtract(const APInt &Val) const { |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 256 | assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width"); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 257 | // If the set is empty or full, don't modify the endpoints. |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 258 | if (Lower == Upper) |
| 259 | return *this; |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 260 | return ConstantRange(Lower - Val, Upper - Val); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 261 | } |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 262 | |
Nick Lewycky | 3e05164 | 2007-02-11 00:58:49 +0000 | [diff] [blame] | 263 | /// intersectWith - Return the range that results from the intersection of this |
Nick Lewycky | 3a4a884 | 2009-07-18 06:34:42 +0000 | [diff] [blame] | 264 | /// range with another range. The resultant range is guaranteed to include all |
| 265 | /// elements contained in both input ranges, and to have the smallest possible |
| 266 | /// set size that does so. Because there may be two intersections with the |
| 267 | /// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A). |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 268 | ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const { |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 269 | assert(getBitWidth() == CR.getBitWidth() && |
| 270 | "ConstantRange types don't agree!"); |
Nick Lewycky | 377b119 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 271 | |
| 272 | // Handle common cases. |
| 273 | if ( isEmptySet() || CR.isFullSet()) return *this; |
| 274 | if (CR.isEmptySet() || isFullSet()) return CR; |
| 275 | |
| 276 | if (!isWrappedSet() && CR.isWrappedSet()) |
Nick Lewycky | 3a4a884 | 2009-07-18 06:34:42 +0000 | [diff] [blame] | 277 | return CR.intersectWith(*this); |
Nick Lewycky | 377b119 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 278 | |
| 279 | if (!isWrappedSet() && !CR.isWrappedSet()) { |
| 280 | if (Lower.ult(CR.Lower)) { |
| 281 | if (Upper.ule(CR.Lower)) |
| 282 | return ConstantRange(getBitWidth(), false); |
| 283 | |
| 284 | if (Upper.ult(CR.Upper)) |
| 285 | return ConstantRange(CR.Lower, Upper); |
| 286 | |
| 287 | return CR; |
| 288 | } else { |
| 289 | if (Upper.ult(CR.Upper)) |
| 290 | return *this; |
| 291 | |
| 292 | if (Lower.ult(CR.Upper)) |
| 293 | return ConstantRange(Lower, CR.Upper); |
| 294 | |
| 295 | return ConstantRange(getBitWidth(), false); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | if (isWrappedSet() && !CR.isWrappedSet()) { |
| 300 | if (CR.Lower.ult(Upper)) { |
| 301 | if (CR.Upper.ult(Upper)) |
| 302 | return CR; |
| 303 | |
| 304 | if (CR.Upper.ult(Lower)) |
| 305 | return ConstantRange(CR.Lower, Upper); |
| 306 | |
| 307 | if (getSetSize().ult(CR.getSetSize())) |
| 308 | return *this; |
| 309 | else |
| 310 | return CR; |
| 311 | } else if (CR.Lower.ult(Lower)) { |
| 312 | if (CR.Upper.ule(Lower)) |
| 313 | return ConstantRange(getBitWidth(), false); |
| 314 | |
| 315 | return ConstantRange(Lower, CR.Upper); |
| 316 | } |
| 317 | return CR; |
| 318 | } |
| 319 | |
| 320 | if (CR.Upper.ult(Upper)) { |
| 321 | if (CR.Lower.ult(Upper)) { |
| 322 | if (getSetSize().ult(CR.getSetSize())) |
| 323 | return *this; |
| 324 | else |
| 325 | return CR; |
| 326 | } |
| 327 | |
| 328 | if (CR.Lower.ult(Lower)) |
| 329 | return ConstantRange(Lower, CR.Upper); |
| 330 | |
| 331 | return CR; |
| 332 | } else if (CR.Upper.ult(Lower)) { |
| 333 | if (CR.Lower.ult(Lower)) |
| 334 | return *this; |
| 335 | |
| 336 | return ConstantRange(CR.Lower, Upper); |
| 337 | } |
| 338 | if (getSetSize().ult(CR.getSetSize())) |
| 339 | return *this; |
| 340 | else |
| 341 | return CR; |
| 342 | } |
| 343 | |
| 344 | |
Nick Lewycky | 3e05164 | 2007-02-11 00:58:49 +0000 | [diff] [blame] | 345 | /// 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] | 346 | /// another range. The resultant range is guaranteed to include the elements of |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 347 | /// both sets, but may contain more. For example, [3, 9) union [12,15) is |
| 348 | /// [3, 15), which includes 9, 10, and 11, which were not included in either |
| 349 | /// set before. |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 350 | /// |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 351 | ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const { |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 352 | assert(getBitWidth() == CR.getBitWidth() && |
| 353 | "ConstantRange types don't agree!"); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 354 | |
Nick Lewycky | c6a28fc | 2007-03-02 03:33:05 +0000 | [diff] [blame] | 355 | if ( isFullSet() || CR.isEmptySet()) return *this; |
| 356 | if (CR.isFullSet() || isEmptySet()) return CR; |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 357 | |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 358 | if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this); |
| 359 | |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 360 | if (!isWrappedSet() && !CR.isWrappedSet()) { |
Nick Lewycky | 7e7dc45 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 361 | if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) { |
| 362 | // If the two ranges are disjoint, find the smaller gap and bridge it. |
| 363 | APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper; |
| 364 | if (d1.ult(d2)) |
| 365 | return ConstantRange(Lower, CR.Upper); |
| 366 | else |
| 367 | return ConstantRange(CR.Lower, Upper); |
| 368 | } |
| 369 | |
| 370 | APInt L = Lower, U = Upper; |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 371 | if (CR.Lower.ult(L)) |
| 372 | L = CR.Lower; |
Nick Lewycky | 7e7dc45 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 373 | if ((CR.Upper - 1).ugt(U - 1)) |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 374 | U = CR.Upper; |
Nick Lewycky | 7e7dc45 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 375 | |
| 376 | if (L == 0 && U == 0) |
| 377 | return ConstantRange(getBitWidth()); |
| 378 | |
| 379 | return ConstantRange(L, U); |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Nick Lewycky | 7e7dc45 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 382 | if (!CR.isWrappedSet()) { |
| 383 | // ------U L----- and ------U L----- : this |
| 384 | // L--U L--U : CR |
| 385 | if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower)) |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 386 | return *this; |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 387 | |
Nick Lewycky | 7e7dc45 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 388 | // ------U L----- : this |
| 389 | // L---------U : CR |
| 390 | if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 391 | return ConstantRange(getBitWidth()); |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 392 | |
Nick Lewycky | 7e7dc45 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 393 | // ----U L---- : this |
| 394 | // L---U : CR |
| 395 | // <d1> <d2> |
| 396 | if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) { |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 397 | APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper; |
Nick Lewycky | 7e7dc45 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 398 | if (d1.ult(d2)) |
| 399 | return ConstantRange(Lower, CR.Upper); |
| 400 | else |
| 401 | return ConstantRange(CR.Lower, Upper); |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Nick Lewycky | 7e7dc45 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 404 | // ----U L----- : this |
| 405 | // L----U : CR |
| 406 | if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) |
| 407 | return ConstantRange(CR.Lower, Upper); |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 408 | |
Nick Lewycky | 7e7dc45 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 409 | // ------U L---- : this |
| 410 | // L-----U : CR |
| 411 | if (CR.Lower.ult(Upper) && CR.Upper.ult(Lower)) |
| 412 | return ConstantRange(Lower, CR.Upper); |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Nick Lewycky | 7e7dc45 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 415 | assert(isWrappedSet() && CR.isWrappedSet() && |
| 416 | "ConstantRange::unionWith missed wrapped union unwrapped case"); |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 417 | |
Nick Lewycky | 7e7dc45 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 418 | // ------U L---- and ------U L---- : this |
| 419 | // -U L----------- and ------------U L : CR |
| 420 | if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper)) |
| 421 | return ConstantRange(getBitWidth()); |
Nick Lewycky | 9babd0e | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 422 | |
Nick Lewycky | 7e7dc45 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 423 | APInt L = Lower, U = Upper; |
| 424 | if (CR.Upper.ugt(U)) |
| 425 | U = CR.Upper; |
| 426 | if (CR.Lower.ult(L)) |
| 427 | L = CR.Lower; |
Nick Lewycky | c6a28fc | 2007-03-02 03:33:05 +0000 | [diff] [blame] | 428 | |
| 429 | return ConstantRange(L, U); |
Chris Lattner | 645e00d | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 430 | } |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 431 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 432 | /// zeroExtend - Return a new range in the specified integer type, which must |
| 433 | /// be strictly larger than the current type. The returned range will |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 434 | /// 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] | 435 | /// zero extended. |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 436 | ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const { |
Nick Lewycky | 32cda11 | 2010-09-06 23:52:49 +0000 | [diff] [blame] | 437 | if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false); |
| 438 | |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 439 | unsigned SrcTySize = getBitWidth(); |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 440 | assert(SrcTySize < DstTySize && "Not a value extension"); |
Nick Lewycky | 32cda11 | 2010-09-06 23:52:49 +0000 | [diff] [blame] | 441 | if (isFullSet() || isWrappedSet()) |
| 442 | // Change into [0, 1 << src bit width) |
Reid Spencer | dc5c159 | 2007-02-28 18:57:32 +0000 | [diff] [blame] | 443 | return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize)); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 444 | |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 445 | APInt L = Lower; L.zext(DstTySize); |
| 446 | APInt U = Upper; U.zext(DstTySize); |
| 447 | return ConstantRange(L, U); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Nick Lewycky | e32157c | 2007-04-07 15:41:33 +0000 | [diff] [blame] | 450 | /// signExtend - Return a new range in the specified integer type, which must |
| 451 | /// be strictly larger than the current type. The returned range will |
| 452 | /// correspond to the possible range of values as if the source range had been |
| 453 | /// sign extended. |
| 454 | ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const { |
Nick Lewycky | 32cda11 | 2010-09-06 23:52:49 +0000 | [diff] [blame] | 455 | if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false); |
| 456 | |
Nick Lewycky | e32157c | 2007-04-07 15:41:33 +0000 | [diff] [blame] | 457 | unsigned SrcTySize = getBitWidth(); |
| 458 | assert(SrcTySize < DstTySize && "Not a value extension"); |
Nick Lewycky | 32cda11 | 2010-09-06 23:52:49 +0000 | [diff] [blame] | 459 | if (isFullSet() || isSignWrappedSet()) { |
Nick Lewycky | e32157c | 2007-04-07 15:41:33 +0000 | [diff] [blame] | 460 | return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1), |
Nick Lewycky | ff84de7 | 2009-07-13 04:17:23 +0000 | [diff] [blame] | 461 | APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1); |
Nick Lewycky | e32157c | 2007-04-07 15:41:33 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | APInt L = Lower; L.sext(DstTySize); |
| 465 | APInt U = Upper; U.sext(DstTySize); |
| 466 | return ConstantRange(L, U); |
| 467 | } |
| 468 | |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 469 | /// truncate - Return a new range in the specified integer type, which must be |
| 470 | /// strictly smaller than the current type. The returned range will |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 471 | /// 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] | 472 | /// truncated to the specified type. |
Reid Spencer | bb626a6 | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 473 | ConstantRange ConstantRange::truncate(uint32_t DstTySize) const { |
| 474 | unsigned SrcTySize = getBitWidth(); |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 475 | assert(SrcTySize > DstTySize && "Not a value truncation"); |
Zhou Sheng | daacf22 | 2007-04-13 05:57:32 +0000 | [diff] [blame] | 476 | APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize)); |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 477 | if (isFullSet() || getSetSize().ugt(Size)) |
Nick Lewycky | 7f9ef4b | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 478 | return ConstantRange(DstTySize, /*isFullSet=*/true); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 479 | |
Reid Spencer | 663e711 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 480 | APInt L = Lower; L.trunc(DstTySize); |
| 481 | APInt U = Upper; U.trunc(DstTySize); |
| 482 | return ConstantRange(L, U); |
Chris Lattner | fc33d30 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Nuno Lopes | 95a3be0 | 2009-11-09 15:36:28 +0000 | [diff] [blame] | 485 | /// zextOrTrunc - make this range have the bit width given by \p DstTySize. The |
| 486 | /// value is zero extended, truncated, or left alone to make it that width. |
| 487 | ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const { |
| 488 | unsigned SrcTySize = getBitWidth(); |
| 489 | if (SrcTySize > DstTySize) |
| 490 | return truncate(DstTySize); |
| 491 | else if (SrcTySize < DstTySize) |
| 492 | return zeroExtend(DstTySize); |
| 493 | else |
| 494 | return *this; |
| 495 | } |
| 496 | |
| 497 | /// sextOrTrunc - make this range have the bit width given by \p DstTySize. The |
| 498 | /// value is sign extended, truncated, or left alone to make it that width. |
| 499 | ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const { |
| 500 | unsigned SrcTySize = getBitWidth(); |
| 501 | if (SrcTySize > DstTySize) |
| 502 | return truncate(DstTySize); |
| 503 | else if (SrcTySize < DstTySize) |
| 504 | return signExtend(DstTySize); |
| 505 | else |
| 506 | return *this; |
| 507 | } |
| 508 | |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 509 | ConstantRange |
| 510 | ConstantRange::add(const ConstantRange &Other) const { |
| 511 | if (isEmptySet() || Other.isEmptySet()) |
| 512 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Nick Lewycky | cf9e07d | 2009-07-13 02:49:08 +0000 | [diff] [blame] | 513 | if (isFullSet() || Other.isFullSet()) |
| 514 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 515 | |
| 516 | APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize(); |
| 517 | APInt NewLower = getLower() + Other.getLower(); |
| 518 | APInt NewUpper = getUpper() + Other.getUpper() - 1; |
| 519 | if (NewLower == NewUpper) |
| 520 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 521 | |
| 522 | ConstantRange X = ConstantRange(NewLower, NewUpper); |
| 523 | if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y)) |
| 524 | // We've wrapped, therefore, full set. |
| 525 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 526 | |
| 527 | return X; |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 530 | ConstantRange |
Nick Lewycky | 7f9ef4b | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 531 | ConstantRange::sub(const ConstantRange &Other) const { |
| 532 | if (isEmptySet() || Other.isEmptySet()) |
| 533 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 534 | if (isFullSet() || Other.isFullSet()) |
| 535 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 536 | |
| 537 | APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize(); |
| 538 | APInt NewLower = getLower() - Other.getLower(); |
| 539 | APInt NewUpper = getUpper() - Other.getUpper() + 1; |
| 540 | if (NewLower == NewUpper) |
| 541 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 542 | |
| 543 | ConstantRange X = ConstantRange(NewLower, NewUpper); |
| 544 | if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y)) |
| 545 | // We've wrapped, therefore, full set. |
| 546 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 547 | |
| 548 | return X; |
| 549 | } |
| 550 | |
| 551 | ConstantRange |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 552 | ConstantRange::multiply(const ConstantRange &Other) const { |
Dan Gohman | 8dcc58e | 2010-01-26 15:56:18 +0000 | [diff] [blame] | 553 | // TODO: If either operand is a single element and the multiply is known to |
| 554 | // be non-wrapping, round the result min and max value to the appropriate |
| 555 | // multiple of that element. If wrapping is possible, at least adjust the |
| 556 | // range according to the greatest power-of-two factor of the single element. |
Dan Gohman | 153f1eb | 2010-01-26 04:13:15 +0000 | [diff] [blame] | 557 | |
Nick Lewycky | 2ff893f | 2009-07-12 02:19:05 +0000 | [diff] [blame] | 558 | if (isEmptySet() || Other.isEmptySet()) |
| 559 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 560 | if (isFullSet() || Other.isFullSet()) |
| 561 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 562 | |
Nick Lewycky | f1db120 | 2009-07-13 03:27:41 +0000 | [diff] [blame] | 563 | APInt this_min = getUnsignedMin().zext(getBitWidth() * 2); |
| 564 | APInt this_max = getUnsignedMax().zext(getBitWidth() * 2); |
| 565 | APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2); |
| 566 | APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2); |
Nick Lewycky | 2ff893f | 2009-07-12 02:19:05 +0000 | [diff] [blame] | 567 | |
Nick Lewycky | f1db120 | 2009-07-13 03:27:41 +0000 | [diff] [blame] | 568 | ConstantRange Result_zext = ConstantRange(this_min * Other_min, |
| 569 | this_max * Other_max + 1); |
Nick Lewycky | 2ff893f | 2009-07-12 02:19:05 +0000 | [diff] [blame] | 570 | return Result_zext.truncate(getBitWidth()); |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | ConstantRange |
| 574 | ConstantRange::smax(const ConstantRange &Other) const { |
Dan Gohman | 38b0644 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 575 | // X smax Y is: range(smax(X_smin, Y_smin), |
| 576 | // smax(X_smax, Y_smax)) |
| 577 | if (isEmptySet() || Other.isEmptySet()) |
| 578 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Dan Gohman | 38b0644 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 579 | APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin()); |
| 580 | APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1; |
| 581 | if (NewU == NewL) |
| 582 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 583 | return ConstantRange(NewL, NewU); |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | ConstantRange |
| 587 | ConstantRange::umax(const ConstantRange &Other) const { |
| 588 | // X umax Y is: range(umax(X_umin, Y_umin), |
| 589 | // umax(X_umax, Y_umax)) |
| 590 | if (isEmptySet() || Other.isEmptySet()) |
| 591 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 592 | APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin()); |
| 593 | APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1; |
| 594 | if (NewU == NewL) |
| 595 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 596 | return ConstantRange(NewL, NewU); |
| 597 | } |
| 598 | |
| 599 | ConstantRange |
Nick Lewycky | 956daf0 | 2009-07-12 05:18:18 +0000 | [diff] [blame] | 600 | ConstantRange::udiv(const ConstantRange &RHS) const { |
| 601 | if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0) |
| 602 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 603 | if (RHS.isFullSet()) |
| 604 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 605 | |
| 606 | APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax()); |
| 607 | |
| 608 | APInt RHS_umin = RHS.getUnsignedMin(); |
| 609 | if (RHS_umin == 0) { |
| 610 | // We want the lowest value in RHS excluding zero. Usually that would be 1 |
| 611 | // except for a range in the form of [X, 1) in which case it would be X. |
| 612 | if (RHS.getUpper() == 1) |
| 613 | RHS_umin = RHS.getLower(); |
| 614 | else |
| 615 | RHS_umin = APInt(getBitWidth(), 1); |
| 616 | } |
| 617 | |
| 618 | APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1; |
| 619 | |
| 620 | // If the LHS is Full and the RHS is a wrapped interval containing 1 then |
| 621 | // this could occur. |
| 622 | if (Lower == Upper) |
| 623 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 624 | |
| 625 | return ConstantRange(Lower, Upper); |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Nuno Lopes | 34e992d | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 628 | ConstantRange |
Nick Lewycky | 198381e | 2010-09-07 05:39:02 +0000 | [diff] [blame] | 629 | ConstantRange::binaryAnd(const ConstantRange &Other) const { |
| 630 | if (isEmptySet() || Other.isEmptySet()) |
| 631 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 632 | |
| 633 | // TODO: replace this with something less conservative |
| 634 | |
| 635 | APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax()); |
| 636 | if (umin.isAllOnesValue()) |
| 637 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 638 | return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1); |
| 639 | } |
| 640 | |
| 641 | ConstantRange |
| 642 | ConstantRange::binaryOr(const ConstantRange &Other) const { |
| 643 | if (isEmptySet() || Other.isEmptySet()) |
| 644 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 645 | |
| 646 | // TODO: replace this with something less conservative |
| 647 | |
| 648 | APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin()); |
| 649 | if (umax.isMinValue()) |
| 650 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 651 | return ConstantRange(umax, APInt::getNullValue(getBitWidth())); |
| 652 | } |
| 653 | |
| 654 | ConstantRange |
Nick Lewycky | 7f9ef4b | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 655 | ConstantRange::shl(const ConstantRange &Other) const { |
| 656 | if (isEmptySet() || Other.isEmptySet()) |
| 657 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Nuno Lopes | 34e992d | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 658 | |
Nick Lewycky | 7f9ef4b | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 659 | APInt min = getUnsignedMin().shl(Other.getUnsignedMin()); |
| 660 | APInt max = getUnsignedMax().shl(Other.getUnsignedMax()); |
Nuno Lopes | 34e992d | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 661 | |
| 662 | // there's no overflow! |
Nuno Lopes | 4459145 | 2009-11-12 15:10:33 +0000 | [diff] [blame] | 663 | APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros()); |
Nick Lewycky | 7f9ef4b | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 664 | if (Zeros.ugt(Other.getUnsignedMax())) |
| 665 | return ConstantRange(min, max + 1); |
Nuno Lopes | 34e992d | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 666 | |
| 667 | // FIXME: implement the other tricky cases |
Nick Lewycky | 7f9ef4b | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 668 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
Nuno Lopes | 34e992d | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | ConstantRange |
Nick Lewycky | 7f9ef4b | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 672 | ConstantRange::lshr(const ConstantRange &Other) const { |
| 673 | if (isEmptySet() || Other.isEmptySet()) |
| 674 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Nuno Lopes | 34e992d | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 675 | |
Nick Lewycky | 7f9ef4b | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 676 | APInt max = getUnsignedMax().lshr(Other.getUnsignedMin()); |
| 677 | APInt min = getUnsignedMin().lshr(Other.getUnsignedMax()); |
| 678 | if (min == max + 1) |
| 679 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 680 | |
| 681 | return ConstantRange(min, max + 1); |
Nuno Lopes | 34e992d | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Owen Anderson | 9773e45 | 2010-08-07 05:47:46 +0000 | [diff] [blame] | 684 | ConstantRange ConstantRange::inverse() const { |
| 685 | if (isFullSet()) { |
Nick Lewycky | 7f9ef4b | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 686 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Owen Anderson | 9773e45 | 2010-08-07 05:47:46 +0000 | [diff] [blame] | 687 | } else if (isEmptySet()) { |
Nick Lewycky | 7f9ef4b | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 688 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
Owen Anderson | 9773e45 | 2010-08-07 05:47:46 +0000 | [diff] [blame] | 689 | } |
| 690 | return ConstantRange(Upper, Lower); |
| 691 | } |
| 692 | |
Dan Gohman | 38b0644 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 693 | /// print - Print out the bounds to a stream... |
Chris Lattner | 96f9d72 | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 694 | /// |
Dan Gohman | 38b0644 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 695 | void ConstantRange::print(raw_ostream &OS) const { |
Dan Gohman | df6d5e0 | 2010-01-26 04:12:55 +0000 | [diff] [blame] | 696 | if (isFullSet()) |
| 697 | OS << "full-set"; |
| 698 | else if (isEmptySet()) |
| 699 | OS << "empty-set"; |
| 700 | else |
| 701 | OS << "[" << Lower << "," << Upper << ")"; |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Dan Gohman | 38b0644 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 704 | /// dump - Allow printing from a debugger easily... |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 705 | /// |
Dan Gohman | 38b0644 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 706 | void ConstantRange::dump() const { |
David Greene | 7fd5fb4 | 2010-01-05 01:28:32 +0000 | [diff] [blame] | 707 | print(dbgs()); |
Dan Gohman | a3755d8 | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 708 | } |