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