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