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