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