Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 1 | //===-- ConstantRange.cpp - ConstantRange implementation ------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 113f2ae | 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 | |
Sanjoy Das | 6ed0530 | 2015-10-22 03:12:57 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Instruction.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/InstrTypes.h" |
Sanjoy Das | 6ed0530 | 2015-10-22 03:12:57 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Operator.h" |
Chandler Carruth | 8cd041e | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 27 | #include "llvm/IR/ConstantRange.h" |
David Greene | de7b353 | 2010-01-05 01:28:32 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | c9499b6 | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 32 | /// Initialize a full (the default) or empty set for the specified type. |
| 33 | /// |
Dan Gohman | dc33ae2 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 34 | ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) { |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 35 | if (Full) |
Reid Spencer | e1f3f19 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 36 | Lower = Upper = APInt::getMaxValue(BitWidth); |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 37 | else |
Reid Spencer | e1f3f19 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 38 | Lower = Upper = APInt::getMinValue(BitWidth); |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 41 | /// Initialize a range to hold the single specified value. |
| 42 | /// |
Benjamin Kramer | cce97be | 2013-07-11 15:37:27 +0000 | [diff] [blame] | 43 | ConstantRange::ConstantRange(APIntMoveTy V) |
Chandler Carruth | 002da5d | 2014-03-02 04:08:41 +0000 | [diff] [blame] | 44 | : Lower(std::move(V)), Upper(Lower + 1) {} |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 45 | |
Benjamin Kramer | cce97be | 2013-07-11 15:37:27 +0000 | [diff] [blame] | 46 | ConstantRange::ConstantRange(APIntMoveTy L, APIntMoveTy U) |
Chandler Carruth | 002da5d | 2014-03-02 04:08:41 +0000 | [diff] [blame] | 47 | : Lower(std::move(L)), Upper(std::move(U)) { |
Benjamin Kramer | cce97be | 2013-07-11 15:37:27 +0000 | [diff] [blame] | 48 | assert(Lower.getBitWidth() == Upper.getBitWidth() && |
Dan Gohman | dc33ae2 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 49 | "ConstantRange with unequal bit widths"); |
Benjamin Kramer | cce97be | 2013-07-11 15:37:27 +0000 | [diff] [blame] | 50 | assert((Lower != Upper || (Lower.isMaxValue() || Lower.isMinValue())) && |
Reid Spencer | e1f3f19 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 51 | "Lower == Upper, but they aren't min or max value!"); |
| 52 | } |
| 53 | |
Sanjoy Das | 7182d36 | 2015-03-18 00:41:24 +0000 | [diff] [blame] | 54 | ConstantRange ConstantRange::makeAllowedICmpRegion(CmpInst::Predicate Pred, |
| 55 | const ConstantRange &CR) { |
Nick Lewycky | 5154ee0 | 2010-09-28 18:18:36 +0000 | [diff] [blame] | 56 | if (CR.isEmptySet()) |
| 57 | return CR; |
| 58 | |
Nick Lewycky | dcfdce9 | 2009-07-11 06:15:39 +0000 | [diff] [blame] | 59 | uint32_t W = CR.getBitWidth(); |
| 60 | switch (Pred) { |
Sanjoy Das | 7182d36 | 2015-03-18 00:41:24 +0000 | [diff] [blame] | 61 | default: |
| 62 | llvm_unreachable("Invalid ICmp predicate to makeAllowedICmpRegion()"); |
Sanjay Patel | f8ee0e0 | 2016-06-19 17:20:27 +0000 | [diff] [blame] | 63 | case CmpInst::ICMP_EQ: |
| 64 | return CR; |
| 65 | case CmpInst::ICMP_NE: |
| 66 | if (CR.isSingleElement()) |
| 67 | return ConstantRange(CR.getUpper(), CR.getLower()); |
| 68 | return ConstantRange(W); |
| 69 | case CmpInst::ICMP_ULT: { |
| 70 | APInt UMax(CR.getUnsignedMax()); |
| 71 | if (UMax.isMinValue()) |
| 72 | return ConstantRange(W, /* empty */ false); |
| 73 | return ConstantRange(APInt::getMinValue(W), UMax); |
| 74 | } |
| 75 | case CmpInst::ICMP_SLT: { |
| 76 | APInt SMax(CR.getSignedMax()); |
| 77 | if (SMax.isMinSignedValue()) |
| 78 | return ConstantRange(W, /* empty */ false); |
| 79 | return ConstantRange(APInt::getSignedMinValue(W), SMax); |
| 80 | } |
| 81 | case CmpInst::ICMP_ULE: { |
| 82 | APInt UMax(CR.getUnsignedMax()); |
| 83 | if (UMax.isMaxValue()) |
Nick Lewycky | dcfdce9 | 2009-07-11 06:15:39 +0000 | [diff] [blame] | 84 | return ConstantRange(W); |
Sanjay Patel | f8ee0e0 | 2016-06-19 17:20:27 +0000 | [diff] [blame] | 85 | return ConstantRange(APInt::getMinValue(W), UMax + 1); |
| 86 | } |
| 87 | case CmpInst::ICMP_SLE: { |
| 88 | APInt SMax(CR.getSignedMax()); |
| 89 | if (SMax.isMaxSignedValue()) |
| 90 | return ConstantRange(W); |
| 91 | return ConstantRange(APInt::getSignedMinValue(W), SMax + 1); |
| 92 | } |
| 93 | case CmpInst::ICMP_UGT: { |
| 94 | APInt UMin(CR.getUnsignedMin()); |
| 95 | if (UMin.isMaxValue()) |
| 96 | return ConstantRange(W, /* empty */ false); |
| 97 | return ConstantRange(UMin + 1, APInt::getNullValue(W)); |
| 98 | } |
| 99 | case CmpInst::ICMP_SGT: { |
| 100 | APInt SMin(CR.getSignedMin()); |
| 101 | if (SMin.isMaxSignedValue()) |
| 102 | return ConstantRange(W, /* empty */ false); |
| 103 | return ConstantRange(SMin + 1, APInt::getSignedMinValue(W)); |
| 104 | } |
| 105 | case CmpInst::ICMP_UGE: { |
| 106 | APInt UMin(CR.getUnsignedMin()); |
| 107 | if (UMin.isMinValue()) |
| 108 | return ConstantRange(W); |
| 109 | return ConstantRange(UMin, APInt::getNullValue(W)); |
| 110 | } |
| 111 | case CmpInst::ICMP_SGE: { |
| 112 | APInt SMin(CR.getSignedMin()); |
| 113 | if (SMin.isMinSignedValue()) |
| 114 | return ConstantRange(W); |
| 115 | return ConstantRange(SMin, APInt::getSignedMinValue(W)); |
| 116 | } |
Nick Lewycky | dcfdce9 | 2009-07-11 06:15:39 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Sanjoy Das | 7182d36 | 2015-03-18 00:41:24 +0000 | [diff] [blame] | 120 | ConstantRange ConstantRange::makeSatisfyingICmpRegion(CmpInst::Predicate Pred, |
| 121 | const ConstantRange &CR) { |
| 122 | // Follows from De-Morgan's laws: |
| 123 | // |
| 124 | // ~(~A union ~B) == A intersect B. |
| 125 | // |
| 126 | return makeAllowedICmpRegion(CmpInst::getInversePredicate(Pred), CR) |
| 127 | .inverse(); |
| 128 | } |
| 129 | |
Balaram Makam | 569eaec | 2016-05-04 21:32:14 +0000 | [diff] [blame] | 130 | ConstantRange ConstantRange::makeExactICmpRegion(CmpInst::Predicate Pred, |
| 131 | const APInt &C) { |
| 132 | // Computes the exact range that is equal to both the constant ranges returned |
| 133 | // by makeAllowedICmpRegion and makeSatisfyingICmpRegion. This is always true |
| 134 | // when RHS is a singleton such as an APInt and so the assert is valid. |
| 135 | // However for non-singleton RHS, for example ult [2,5) makeAllowedICmpRegion |
| 136 | // returns [0,4) but makeSatisfyICmpRegion returns [0,2). |
| 137 | // |
| 138 | assert(makeAllowedICmpRegion(Pred, C) == makeSatisfyingICmpRegion(Pred, C)); |
| 139 | return makeAllowedICmpRegion(Pred, C); |
| 140 | } |
| 141 | |
Sanjoy Das | 590614c | 2016-05-19 03:53:06 +0000 | [diff] [blame] | 142 | bool ConstantRange::getEquivalentICmp(CmpInst::Predicate &Pred, |
| 143 | APInt &RHS) const { |
| 144 | bool Success = false; |
| 145 | |
| 146 | if (isFullSet() || isEmptySet()) { |
| 147 | Pred = isEmptySet() ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE; |
| 148 | RHS = APInt(getBitWidth(), 0); |
| 149 | Success = true; |
Sanjoy Das | c7d3291 | 2016-10-02 20:59:05 +0000 | [diff] [blame] | 150 | } else if (auto *OnlyElt = getSingleElement()) { |
| 151 | Pred = CmpInst::ICMP_EQ; |
| 152 | RHS = *OnlyElt; |
| 153 | Success = true; |
| 154 | } else if (auto *OnlyMissingElt = getSingleMissingElement()) { |
| 155 | Pred = CmpInst::ICMP_NE; |
| 156 | RHS = *OnlyMissingElt; |
| 157 | Success = true; |
Sanjoy Das | 590614c | 2016-05-19 03:53:06 +0000 | [diff] [blame] | 158 | } else if (getLower().isMinSignedValue() || getLower().isMinValue()) { |
| 159 | Pred = |
| 160 | getLower().isMinSignedValue() ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT; |
| 161 | RHS = getUpper(); |
| 162 | Success = true; |
| 163 | } else if (getUpper().isMinSignedValue() || getUpper().isMinValue()) { |
| 164 | Pred = |
| 165 | getUpper().isMinSignedValue() ? CmpInst::ICMP_SGE : CmpInst::ICMP_UGE; |
| 166 | RHS = getLower(); |
| 167 | Success = true; |
| 168 | } |
| 169 | |
| 170 | assert((!Success || ConstantRange::makeExactICmpRegion(Pred, RHS) == *this) && |
| 171 | "Bad result!"); |
| 172 | |
| 173 | return Success; |
| 174 | } |
| 175 | |
Sanjoy Das | 5079f62 | 2016-02-22 16:13:02 +0000 | [diff] [blame] | 176 | ConstantRange |
| 177 | ConstantRange::makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp, |
Sanjoy Das | f3867e6 | 2016-03-03 18:31:16 +0000 | [diff] [blame] | 178 | const ConstantRange &Other, |
| 179 | unsigned NoWrapKind) { |
Sanjoy Das | 6ed0530 | 2015-10-22 03:12:57 +0000 | [diff] [blame] | 180 | typedef OverflowingBinaryOperator OBO; |
| 181 | |
| 182 | // Computes the intersection of CR0 and CR1. It is different from |
| 183 | // intersectWith in that the ConstantRange returned will only contain elements |
| 184 | // in both CR0 and CR1 (i.e. SubsetIntersect(X, Y) is a *subset*, proper or |
| 185 | // not, of both X and Y). |
| 186 | auto SubsetIntersect = |
| 187 | [](const ConstantRange &CR0, const ConstantRange &CR1) { |
| 188 | return CR0.inverse().unionWith(CR1.inverse()).inverse(); |
| 189 | }; |
| 190 | |
| 191 | assert(BinOp >= Instruction::BinaryOpsBegin && |
| 192 | BinOp < Instruction::BinaryOpsEnd && "Binary operators only!"); |
| 193 | |
| 194 | assert((NoWrapKind == OBO::NoSignedWrap || |
| 195 | NoWrapKind == OBO::NoUnsignedWrap || |
| 196 | NoWrapKind == (OBO::NoUnsignedWrap | OBO::NoSignedWrap)) && |
| 197 | "NoWrapKind invalid!"); |
| 198 | |
Sanjoy Das | f3867e6 | 2016-03-03 18:31:16 +0000 | [diff] [blame] | 199 | unsigned BitWidth = Other.getBitWidth(); |
Sanjoy Das | 6ed0530 | 2015-10-22 03:12:57 +0000 | [diff] [blame] | 200 | if (BinOp != Instruction::Add) |
| 201 | // Conservative answer: empty set |
| 202 | return ConstantRange(BitWidth, false); |
| 203 | |
Sanjoy Das | f3867e6 | 2016-03-03 18:31:16 +0000 | [diff] [blame] | 204 | if (auto *C = Other.getSingleElement()) |
| 205 | if (C->isMinValue()) |
| 206 | // Full set: nothing signed / unsigned wraps when added to 0. |
| 207 | return ConstantRange(BitWidth); |
Sanjoy Das | 6ed0530 | 2015-10-22 03:12:57 +0000 | [diff] [blame] | 208 | |
| 209 | ConstantRange Result(BitWidth); |
| 210 | |
| 211 | if (NoWrapKind & OBO::NoUnsignedWrap) |
Sanjoy Das | f3867e6 | 2016-03-03 18:31:16 +0000 | [diff] [blame] | 212 | Result = |
| 213 | SubsetIntersect(Result, ConstantRange(APInt::getNullValue(BitWidth), |
| 214 | -Other.getUnsignedMax())); |
Sanjoy Das | 6ed0530 | 2015-10-22 03:12:57 +0000 | [diff] [blame] | 215 | |
| 216 | if (NoWrapKind & OBO::NoSignedWrap) { |
Sanjoy Das | f3867e6 | 2016-03-03 18:31:16 +0000 | [diff] [blame] | 217 | APInt SignedMin = Other.getSignedMin(); |
| 218 | APInt SignedMax = Other.getSignedMax(); |
| 219 | |
| 220 | if (SignedMax.isStrictlyPositive()) |
Sanjoy Das | 6ed0530 | 2015-10-22 03:12:57 +0000 | [diff] [blame] | 221 | Result = SubsetIntersect( |
Sanjoy Das | f3867e6 | 2016-03-03 18:31:16 +0000 | [diff] [blame] | 222 | Result, |
| 223 | ConstantRange(APInt::getSignedMinValue(BitWidth), |
| 224 | APInt::getSignedMinValue(BitWidth) - SignedMax)); |
| 225 | |
| 226 | if (SignedMin.isNegative()) |
Sanjoy Das | 6ed0530 | 2015-10-22 03:12:57 +0000 | [diff] [blame] | 227 | Result = SubsetIntersect( |
Sanjoy Das | f3867e6 | 2016-03-03 18:31:16 +0000 | [diff] [blame] | 228 | Result, ConstantRange(APInt::getSignedMinValue(BitWidth) - SignedMin, |
Sanjoy Das | 6ed0530 | 2015-10-22 03:12:57 +0000 | [diff] [blame] | 229 | APInt::getSignedMinValue(BitWidth))); |
| 230 | } |
| 231 | |
| 232 | return Result; |
| 233 | } |
| 234 | |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 235 | /// isFullSet - Return true if this set contains all of the elements possible |
| 236 | /// for this data-type |
| 237 | bool ConstantRange::isFullSet() const { |
Zhou Sheng | 3178736 | 2007-04-26 16:42:07 +0000 | [diff] [blame] | 238 | return Lower == Upper && Lower.isMaxValue(); |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 239 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 241 | /// isEmptySet - Return true if this set contains no members. |
| 242 | /// |
| 243 | bool ConstantRange::isEmptySet() const { |
Zhou Sheng | 3178736 | 2007-04-26 16:42:07 +0000 | [diff] [blame] | 244 | return Lower == Upper && Lower.isMinValue(); |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /// isWrappedSet - Return true if this set wraps around the top of the range, |
| 248 | /// for example: [100, 8) |
| 249 | /// |
Reid Spencer | 6a44033 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 250 | bool ConstantRange::isWrappedSet() const { |
Reid Spencer | e1f3f19 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 251 | return Lower.ugt(Upper); |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Nick Lewycky | a35462d | 2010-09-06 23:52:49 +0000 | [diff] [blame] | 254 | /// isSignWrappedSet - Return true if this set wraps around the INT_MIN of |
| 255 | /// its bitwidth, for example: i8 [120, 140). |
| 256 | /// |
| 257 | bool ConstantRange::isSignWrappedSet() const { |
| 258 | return contains(APInt::getSignedMaxValue(getBitWidth())) && |
| 259 | contains(APInt::getSignedMinValue(getBitWidth())); |
| 260 | } |
| 261 | |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 262 | /// getSetSize - Return the number of elements in this set. |
| 263 | /// |
Reid Spencer | e1f3f19 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 264 | APInt ConstantRange::getSetSize() const { |
Nuno Lopes | 216d571 | 2012-07-17 15:43:59 +0000 | [diff] [blame] | 265 | if (isFullSet()) { |
| 266 | APInt Size(getBitWidth()+1, 0); |
| 267 | Size.setBit(getBitWidth()); |
| 268 | return Size; |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 269 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 270 | |
Nuno Lopes | 216d571 | 2012-07-17 15:43:59 +0000 | [diff] [blame] | 271 | // This is also correct for wrapped sets. |
Nuno Lopes | 99504c5 | 2012-07-16 18:08:12 +0000 | [diff] [blame] | 272 | return (Upper - Lower).zext(getBitWidth()+1); |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 275 | /// getUnsignedMax - Return the largest unsigned value contained in the |
| 276 | /// ConstantRange. |
| 277 | /// |
| 278 | APInt ConstantRange::getUnsignedMax() const { |
| 279 | if (isFullSet() || isWrappedSet()) |
| 280 | return APInt::getMaxValue(getBitWidth()); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 281 | return getUpper() - 1; |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /// getUnsignedMin - Return the smallest unsigned value contained in the |
| 285 | /// ConstantRange. |
| 286 | /// |
| 287 | APInt ConstantRange::getUnsignedMin() const { |
| 288 | if (isFullSet() || (isWrappedSet() && getUpper() != 0)) |
| 289 | return APInt::getMinValue(getBitWidth()); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 290 | return getLower(); |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /// getSignedMax - Return the largest signed value contained in the |
| 294 | /// ConstantRange. |
| 295 | /// |
| 296 | APInt ConstantRange::getSignedMax() const { |
Zhou Sheng | 01c175e | 2007-04-13 05:57:32 +0000 | [diff] [blame] | 297 | APInt SignedMax(APInt::getSignedMaxValue(getBitWidth())); |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 298 | if (!isWrappedSet()) { |
Nick Lewycky | d18b160 | 2007-06-09 04:20:33 +0000 | [diff] [blame] | 299 | if (getLower().sle(getUpper() - 1)) |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 300 | return getUpper() - 1; |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 301 | return SignedMax; |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 302 | } |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 303 | if (getLower().isNegative() == getUpper().isNegative()) |
| 304 | return SignedMax; |
| 305 | return getUpper() - 1; |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /// getSignedMin - Return the smallest signed value contained in the |
| 309 | /// ConstantRange. |
| 310 | /// |
| 311 | APInt ConstantRange::getSignedMin() const { |
Zhou Sheng | 01c175e | 2007-04-13 05:57:32 +0000 | [diff] [blame] | 312 | APInt SignedMin(APInt::getSignedMinValue(getBitWidth())); |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 313 | if (!isWrappedSet()) { |
Nick Lewycky | d18b160 | 2007-06-09 04:20:33 +0000 | [diff] [blame] | 314 | if (getLower().sle(getUpper() - 1)) |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 315 | return getLower(); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 316 | return SignedMin; |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 317 | } |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 318 | if ((getUpper() - 1).slt(getLower())) { |
| 319 | if (getUpper() != SignedMin) |
| 320 | return SignedMin; |
| 321 | } |
| 322 | return getLower(); |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 325 | /// contains - Return true if the specified value is in the set. |
| 326 | /// |
Reid Spencer | 6a44033 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 327 | bool ConstantRange::contains(const APInt &V) const { |
| 328 | if (Lower == Upper) |
| 329 | return isFullSet(); |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 330 | |
Reid Spencer | 6a44033 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 331 | if (!isWrappedSet()) |
| 332 | return Lower.ule(V) && V.ult(Upper); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 333 | return Lower.ule(V) || V.ult(Upper); |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Nick Lewycky | dcfdce9 | 2009-07-11 06:15:39 +0000 | [diff] [blame] | 336 | /// contains - Return true if the argument is a subset of this range. |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 337 | /// Two equal sets contain each other. The empty set contained by all other |
| 338 | /// sets. |
Nick Lewycky | dcfdce9 | 2009-07-11 06:15:39 +0000 | [diff] [blame] | 339 | /// |
| 340 | bool ConstantRange::contains(const ConstantRange &Other) const { |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 341 | if (isFullSet() || Other.isEmptySet()) return true; |
| 342 | if (isEmptySet() || Other.isFullSet()) return false; |
Nick Lewycky | dcfdce9 | 2009-07-11 06:15:39 +0000 | [diff] [blame] | 343 | |
| 344 | if (!isWrappedSet()) { |
| 345 | if (Other.isWrappedSet()) |
| 346 | return false; |
| 347 | |
| 348 | return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper); |
| 349 | } |
| 350 | |
| 351 | if (!Other.isWrappedSet()) |
| 352 | return Other.getUpper().ule(Upper) || |
| 353 | Lower.ule(Other.getLower()); |
| 354 | |
| 355 | return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower()); |
| 356 | } |
| 357 | |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 358 | /// subtract - Subtract the specified constant from the endpoints of this |
| 359 | /// constant range. |
Reid Spencer | 3a7e9d8 | 2007-02-28 19:57:34 +0000 | [diff] [blame] | 360 | ConstantRange ConstantRange::subtract(const APInt &Val) const { |
Reid Spencer | 9b3d6ec | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 361 | assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width"); |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 362 | // If the set is empty or full, don't modify the endpoints. |
Reid Spencer | e1f3f19 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 363 | if (Lower == Upper) |
| 364 | return *this; |
Reid Spencer | e1f3f19 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 365 | return ConstantRange(Lower - Val, Upper - Val); |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 366 | } |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 367 | |
Nuno Lopes | 5020db2 | 2012-06-28 16:10:13 +0000 | [diff] [blame] | 368 | /// \brief Subtract the specified range from this range (aka relative complement |
| 369 | /// of the sets). |
| 370 | ConstantRange ConstantRange::difference(const ConstantRange &CR) const { |
| 371 | return intersectWith(CR.inverse()); |
| 372 | } |
| 373 | |
Nick Lewycky | 63f1108 | 2007-02-11 00:58:49 +0000 | [diff] [blame] | 374 | /// intersectWith - Return the range that results from the intersection of this |
Nick Lewycky | 0d13903 | 2009-07-18 06:34:42 +0000 | [diff] [blame] | 375 | /// range with another range. The resultant range is guaranteed to include all |
| 376 | /// elements contained in both input ranges, and to have the smallest possible |
| 377 | /// set size that does so. Because there may be two intersections with the |
| 378 | /// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A). |
Reid Spencer | 6a44033 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 379 | ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const { |
Reid Spencer | 9b3d6ec | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 380 | assert(getBitWidth() == CR.getBitWidth() && |
| 381 | "ConstantRange types don't agree!"); |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 382 | |
| 383 | // Handle common cases. |
| 384 | if ( isEmptySet() || CR.isFullSet()) return *this; |
| 385 | if (CR.isEmptySet() || isFullSet()) return CR; |
| 386 | |
| 387 | if (!isWrappedSet() && CR.isWrappedSet()) |
Nick Lewycky | 0d13903 | 2009-07-18 06:34:42 +0000 | [diff] [blame] | 388 | return CR.intersectWith(*this); |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 389 | |
| 390 | if (!isWrappedSet() && !CR.isWrappedSet()) { |
| 391 | if (Lower.ult(CR.Lower)) { |
| 392 | if (Upper.ule(CR.Lower)) |
| 393 | return ConstantRange(getBitWidth(), false); |
| 394 | |
| 395 | if (Upper.ult(CR.Upper)) |
| 396 | return ConstantRange(CR.Lower, Upper); |
| 397 | |
| 398 | return CR; |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 399 | } |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 400 | if (Upper.ult(CR.Upper)) |
| 401 | return *this; |
| 402 | |
| 403 | if (Lower.ult(CR.Upper)) |
| 404 | return ConstantRange(Lower, CR.Upper); |
| 405 | |
| 406 | return ConstantRange(getBitWidth(), false); |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | if (isWrappedSet() && !CR.isWrappedSet()) { |
| 410 | if (CR.Lower.ult(Upper)) { |
| 411 | if (CR.Upper.ult(Upper)) |
| 412 | return CR; |
| 413 | |
Nuno Lopes | 63afc08 | 2012-05-18 00:14:36 +0000 | [diff] [blame] | 414 | if (CR.Upper.ule(Lower)) |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 415 | return ConstantRange(CR.Lower, Upper); |
| 416 | |
| 417 | if (getSetSize().ult(CR.getSetSize())) |
| 418 | return *this; |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 419 | return CR; |
| 420 | } |
| 421 | if (CR.Lower.ult(Lower)) { |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 422 | if (CR.Upper.ule(Lower)) |
| 423 | return ConstantRange(getBitWidth(), false); |
| 424 | |
| 425 | return ConstantRange(Lower, CR.Upper); |
| 426 | } |
| 427 | return CR; |
| 428 | } |
| 429 | |
| 430 | if (CR.Upper.ult(Upper)) { |
| 431 | if (CR.Lower.ult(Upper)) { |
| 432 | if (getSetSize().ult(CR.getSetSize())) |
| 433 | return *this; |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 434 | return CR; |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | if (CR.Lower.ult(Lower)) |
| 438 | return ConstantRange(Lower, CR.Upper); |
| 439 | |
| 440 | return CR; |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 441 | } |
Nuno Lopes | ebb0c94 | 2012-06-28 00:59:33 +0000 | [diff] [blame] | 442 | if (CR.Upper.ule(Lower)) { |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 443 | if (CR.Lower.ult(Lower)) |
| 444 | return *this; |
| 445 | |
| 446 | return ConstantRange(CR.Lower, Upper); |
| 447 | } |
| 448 | if (getSetSize().ult(CR.getSetSize())) |
| 449 | return *this; |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 450 | return CR; |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | |
Nick Lewycky | 63f1108 | 2007-02-11 00:58:49 +0000 | [diff] [blame] | 454 | /// unionWith - Return the range that results from the union of this range with |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 455 | /// another range. The resultant range is guaranteed to include the elements of |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 456 | /// both sets, but may contain more. For example, [3, 9) union [12,15) is |
| 457 | /// [3, 15), which includes 9, 10, and 11, which were not included in either |
| 458 | /// set before. |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 459 | /// |
Reid Spencer | 6a44033 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 460 | ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const { |
Reid Spencer | 9b3d6ec | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 461 | assert(getBitWidth() == CR.getBitWidth() && |
| 462 | "ConstantRange types don't agree!"); |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 463 | |
Nick Lewycky | cf87f9e | 2007-03-02 03:33:05 +0000 | [diff] [blame] | 464 | if ( isFullSet() || CR.isEmptySet()) return *this; |
| 465 | if (CR.isFullSet() || isEmptySet()) return CR; |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 466 | |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 467 | if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this); |
| 468 | |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 469 | if (!isWrappedSet() && !CR.isWrappedSet()) { |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 470 | if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) { |
| 471 | // If the two ranges are disjoint, find the smaller gap and bridge it. |
| 472 | APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper; |
| 473 | if (d1.ult(d2)) |
| 474 | return ConstantRange(Lower, CR.Upper); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 475 | return ConstantRange(CR.Lower, Upper); |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | APInt L = Lower, U = Upper; |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 479 | if (CR.Lower.ult(L)) |
| 480 | L = CR.Lower; |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 481 | if ((CR.Upper - 1).ugt(U - 1)) |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 482 | U = CR.Upper; |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 483 | |
| 484 | if (L == 0 && U == 0) |
| 485 | return ConstantRange(getBitWidth()); |
| 486 | |
| 487 | return ConstantRange(L, U); |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 490 | if (!CR.isWrappedSet()) { |
| 491 | // ------U L----- and ------U L----- : this |
| 492 | // L--U L--U : CR |
| 493 | if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower)) |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 494 | return *this; |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 495 | |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 496 | // ------U L----- : this |
| 497 | // L---------U : CR |
| 498 | if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 499 | return ConstantRange(getBitWidth()); |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 500 | |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 501 | // ----U L---- : this |
| 502 | // L---U : CR |
| 503 | // <d1> <d2> |
| 504 | if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) { |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 505 | APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper; |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 506 | if (d1.ult(d2)) |
| 507 | return ConstantRange(Lower, CR.Upper); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 508 | return ConstantRange(CR.Lower, Upper); |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 511 | // ----U L----- : this |
| 512 | // L----U : CR |
| 513 | if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) |
| 514 | return ConstantRange(CR.Lower, Upper); |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 515 | |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 516 | // ------U L---- : this |
| 517 | // L-----U : CR |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 518 | assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) && |
| 519 | "ConstantRange::unionWith missed a case with one range wrapped"); |
| 520 | return ConstantRange(Lower, CR.Upper); |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 523 | // ------U L---- and ------U L---- : this |
| 524 | // -U L----------- and ------------U L : CR |
| 525 | if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper)) |
| 526 | return ConstantRange(getBitWidth()); |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 527 | |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 528 | APInt L = Lower, U = Upper; |
| 529 | if (CR.Upper.ugt(U)) |
| 530 | U = CR.Upper; |
| 531 | if (CR.Lower.ult(L)) |
| 532 | L = CR.Lower; |
Nick Lewycky | cf87f9e | 2007-03-02 03:33:05 +0000 | [diff] [blame] | 533 | |
| 534 | return ConstantRange(L, U); |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 535 | } |
Chris Lattner | e0bb9eb | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 536 | |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 537 | /// zeroExtend - Return a new range in the specified integer type, which must |
| 538 | /// be strictly larger than the current type. The returned range will |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 539 | /// correspond to the possible range of values as if the source range had been |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 540 | /// zero extended. |
Reid Spencer | 9b3d6ec | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 541 | ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const { |
Nick Lewycky | a35462d | 2010-09-06 23:52:49 +0000 | [diff] [blame] | 542 | if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false); |
| 543 | |
Reid Spencer | 9b3d6ec | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 544 | unsigned SrcTySize = getBitWidth(); |
Reid Spencer | e1f3f19 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 545 | assert(SrcTySize < DstTySize && "Not a value extension"); |
Nuno Lopes | eb9d275 | 2012-07-23 20:33:29 +0000 | [diff] [blame] | 546 | if (isFullSet() || isWrappedSet()) { |
Nick Lewycky | a35462d | 2010-09-06 23:52:49 +0000 | [diff] [blame] | 547 | // Change into [0, 1 << src bit width) |
Nuno Lopes | eb9d275 | 2012-07-23 20:33:29 +0000 | [diff] [blame] | 548 | APInt LowerExt(DstTySize, 0); |
| 549 | if (!Upper) // special case: [X, 0) -- not really wrapping around |
| 550 | LowerExt = Lower.zext(DstTySize); |
Benjamin Kramer | fc3ea6f | 2013-07-11 16:05:50 +0000 | [diff] [blame] | 551 | return ConstantRange(LowerExt, APInt::getOneBitSet(DstTySize, SrcTySize)); |
Nuno Lopes | eb9d275 | 2012-07-23 20:33:29 +0000 | [diff] [blame] | 552 | } |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 553 | |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 554 | return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize)); |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Nick Lewycky | b89804f | 2007-04-07 15:41:33 +0000 | [diff] [blame] | 557 | /// signExtend - Return a new range in the specified integer type, which must |
| 558 | /// be strictly larger than the current type. The returned range will |
| 559 | /// correspond to the possible range of values as if the source range had been |
| 560 | /// sign extended. |
| 561 | ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const { |
Nick Lewycky | a35462d | 2010-09-06 23:52:49 +0000 | [diff] [blame] | 562 | if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false); |
| 563 | |
Nick Lewycky | b89804f | 2007-04-07 15:41:33 +0000 | [diff] [blame] | 564 | unsigned SrcTySize = getBitWidth(); |
| 565 | assert(SrcTySize < DstTySize && "Not a value extension"); |
Nuno Lopes | 1112eca | 2013-10-30 15:36:50 +0000 | [diff] [blame] | 566 | |
| 567 | // special case: [X, INT_MIN) -- not really wrapping around |
Nuno Lopes | 14d4b0c | 2013-10-31 19:53:53 +0000 | [diff] [blame] | 568 | if (Upper.isMinSignedValue()) |
Nuno Lopes | 1112eca | 2013-10-30 15:36:50 +0000 | [diff] [blame] | 569 | return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize)); |
| 570 | |
Nick Lewycky | a35462d | 2010-09-06 23:52:49 +0000 | [diff] [blame] | 571 | if (isFullSet() || isSignWrappedSet()) { |
Nick Lewycky | b89804f | 2007-04-07 15:41:33 +0000 | [diff] [blame] | 572 | return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1), |
Nick Lewycky | 5edc459 | 2009-07-13 04:17:23 +0000 | [diff] [blame] | 573 | APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1); |
Nick Lewycky | b89804f | 2007-04-07 15:41:33 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 576 | return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize)); |
Nick Lewycky | b89804f | 2007-04-07 15:41:33 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 579 | /// truncate - Return a new range in the specified integer type, which must be |
| 580 | /// strictly smaller than the current type. The returned range will |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 581 | /// correspond to the possible range of values as if the source range had been |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 582 | /// truncated to the specified type. |
Reid Spencer | 9b3d6ec | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 583 | ConstantRange ConstantRange::truncate(uint32_t DstTySize) const { |
Benjamin Kramer | 6709e05 | 2011-11-24 17:24:33 +0000 | [diff] [blame] | 584 | assert(getBitWidth() > DstTySize && "Not a value truncation"); |
Nuno Lopes | c14776d | 2012-07-19 16:27:45 +0000 | [diff] [blame] | 585 | if (isEmptySet()) |
| 586 | return ConstantRange(DstTySize, /*isFullSet=*/false); |
| 587 | if (isFullSet()) |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 588 | return ConstantRange(DstTySize, /*isFullSet=*/true); |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 589 | |
Nuno Lopes | c14776d | 2012-07-19 16:27:45 +0000 | [diff] [blame] | 590 | APInt MaxValue = APInt::getMaxValue(DstTySize).zext(getBitWidth()); |
| 591 | APInt MaxBitValue(getBitWidth(), 0); |
| 592 | MaxBitValue.setBit(DstTySize); |
| 593 | |
| 594 | APInt LowerDiv(Lower), UpperDiv(Upper); |
| 595 | ConstantRange Union(DstTySize, /*isFullSet=*/false); |
| 596 | |
| 597 | // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue] |
| 598 | // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and |
| 599 | // then we do the union with [MaxValue, Upper) |
| 600 | if (isWrappedSet()) { |
Nick Lewycky | bfad015 | 2016-06-06 01:51:23 +0000 | [diff] [blame] | 601 | // If Upper is greater than Max Value, it covers the whole truncated range. |
Nuno Lopes | c14776d | 2012-07-19 16:27:45 +0000 | [diff] [blame] | 602 | if (Upper.uge(MaxValue)) |
| 603 | return ConstantRange(DstTySize, /*isFullSet=*/true); |
| 604 | |
| 605 | Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize)); |
| 606 | UpperDiv = APInt::getMaxValue(getBitWidth()); |
| 607 | |
| 608 | // Union covers the MaxValue case, so return if the remaining range is just |
| 609 | // MaxValue. |
| 610 | if (LowerDiv == UpperDiv) |
| 611 | return Union; |
| 612 | } |
| 613 | |
| 614 | // Chop off the most significant bits that are past the destination bitwidth. |
| 615 | if (LowerDiv.uge(MaxValue)) { |
| 616 | APInt Div(getBitWidth(), 0); |
| 617 | APInt::udivrem(LowerDiv, MaxBitValue, Div, LowerDiv); |
| 618 | UpperDiv = UpperDiv - MaxBitValue * Div; |
| 619 | } |
| 620 | |
| 621 | if (UpperDiv.ule(MaxValue)) |
| 622 | return ConstantRange(LowerDiv.trunc(DstTySize), |
| 623 | UpperDiv.trunc(DstTySize)).unionWith(Union); |
| 624 | |
Nick Lewycky | bfad015 | 2016-06-06 01:51:23 +0000 | [diff] [blame] | 625 | // The truncated value wraps around. Check if we can do better than fullset. |
Nuno Lopes | c14776d | 2012-07-19 16:27:45 +0000 | [diff] [blame] | 626 | APInt UpperModulo = UpperDiv - MaxBitValue; |
| 627 | if (UpperModulo.ult(LowerDiv)) |
| 628 | return ConstantRange(LowerDiv.trunc(DstTySize), |
| 629 | UpperModulo.trunc(DstTySize)).unionWith(Union); |
| 630 | |
| 631 | return ConstantRange(DstTySize, /*isFullSet=*/true); |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Nuno Lopes | 640eb70 | 2009-11-09 15:36:28 +0000 | [diff] [blame] | 634 | /// zextOrTrunc - make this range have the bit width given by \p DstTySize. The |
| 635 | /// value is zero extended, truncated, or left alone to make it that width. |
| 636 | ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const { |
| 637 | unsigned SrcTySize = getBitWidth(); |
| 638 | if (SrcTySize > DstTySize) |
| 639 | return truncate(DstTySize); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 640 | if (SrcTySize < DstTySize) |
Nuno Lopes | 640eb70 | 2009-11-09 15:36:28 +0000 | [diff] [blame] | 641 | return zeroExtend(DstTySize); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 642 | return *this; |
Nuno Lopes | 640eb70 | 2009-11-09 15:36:28 +0000 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | /// sextOrTrunc - make this range have the bit width given by \p DstTySize. The |
| 646 | /// value is sign extended, truncated, or left alone to make it that width. |
| 647 | ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const { |
| 648 | unsigned SrcTySize = getBitWidth(); |
| 649 | if (SrcTySize > DstTySize) |
| 650 | return truncate(DstTySize); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 651 | if (SrcTySize < DstTySize) |
Nuno Lopes | 640eb70 | 2009-11-09 15:36:28 +0000 | [diff] [blame] | 652 | return signExtend(DstTySize); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 653 | return *this; |
Nuno Lopes | 640eb70 | 2009-11-09 15:36:28 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 656 | ConstantRange |
| 657 | ConstantRange::add(const ConstantRange &Other) const { |
| 658 | if (isEmptySet() || Other.isEmptySet()) |
| 659 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Nick Lewycky | 73b704d | 2009-07-13 02:49:08 +0000 | [diff] [blame] | 660 | if (isFullSet() || Other.isFullSet()) |
| 661 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 662 | |
| 663 | APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize(); |
| 664 | APInt NewLower = getLower() + Other.getLower(); |
| 665 | APInt NewUpper = getUpper() + Other.getUpper() - 1; |
| 666 | if (NewLower == NewUpper) |
| 667 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 668 | |
| 669 | ConstantRange X = ConstantRange(NewLower, NewUpper); |
| 670 | if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y)) |
| 671 | // We've wrapped, therefore, full set. |
| 672 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 673 | |
| 674 | return X; |
Chris Lattner | e0bb9eb | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Artur Pilipenko | ed84103 | 2016-10-19 14:44:23 +0000 | [diff] [blame] | 677 | ConstantRange ConstantRange::addWithNoSignedWrap(const APInt &Other) const { |
| 678 | // Calculate the subset of this range such that "X + Other" is |
| 679 | // guaranteed not to wrap (overflow) for all X in this subset. |
| 680 | // makeGuaranteedNoWrapRegion will produce an exact NSW range since we are |
| 681 | // passing a single element range. |
| 682 | auto NSWRange = ConstantRange::makeGuaranteedNoWrapRegion(BinaryOperator::Add, |
| 683 | ConstantRange(Other), |
| 684 | OverflowingBinaryOperator::NoSignedWrap); |
| 685 | auto NSWConstrainedRange = intersectWith(NSWRange); |
| 686 | |
| 687 | return NSWConstrainedRange.add(ConstantRange(Other)); |
| 688 | } |
| 689 | |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 690 | ConstantRange |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 691 | ConstantRange::sub(const ConstantRange &Other) const { |
| 692 | if (isEmptySet() || Other.isEmptySet()) |
| 693 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 694 | if (isFullSet() || Other.isFullSet()) |
| 695 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 696 | |
| 697 | APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize(); |
Nick Lewycky | 5dc6b79 | 2011-06-22 21:13:46 +0000 | [diff] [blame] | 698 | APInt NewLower = getLower() - Other.getUpper() + 1; |
| 699 | APInt NewUpper = getUpper() - Other.getLower(); |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 700 | if (NewLower == NewUpper) |
| 701 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 702 | |
| 703 | ConstantRange X = ConstantRange(NewLower, NewUpper); |
| 704 | if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y)) |
| 705 | // We've wrapped, therefore, full set. |
| 706 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 707 | |
| 708 | return X; |
| 709 | } |
| 710 | |
| 711 | ConstantRange |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 712 | ConstantRange::multiply(const ConstantRange &Other) const { |
Dan Gohman | 3f8ed9e | 2010-01-26 15:56:18 +0000 | [diff] [blame] | 713 | // TODO: If either operand is a single element and the multiply is known to |
| 714 | // be non-wrapping, round the result min and max value to the appropriate |
| 715 | // multiple of that element. If wrapping is possible, at least adjust the |
| 716 | // range according to the greatest power-of-two factor of the single element. |
Dan Gohman | 5325efc | 2010-01-26 04:13:15 +0000 | [diff] [blame] | 717 | |
Nick Lewycky | 2951c99 | 2009-07-12 02:19:05 +0000 | [diff] [blame] | 718 | if (isEmptySet() || Other.isEmptySet()) |
| 719 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Nuno Lopes | 986cc18 | 2012-07-16 20:47:16 +0000 | [diff] [blame] | 720 | |
James Molloy | dcc78ec | 2015-03-06 15:50:47 +0000 | [diff] [blame] | 721 | // Multiplication is signedness-independent. However different ranges can be |
| 722 | // obtained depending on how the input ranges are treated. These different |
| 723 | // ranges are all conservatively correct, but one might be better than the |
| 724 | // other. We calculate two ranges; one treating the inputs as unsigned |
| 725 | // and the other signed, then return the smallest of these ranges. |
| 726 | |
| 727 | // Unsigned range first. |
Nick Lewycky | 5302389 | 2009-07-13 03:27:41 +0000 | [diff] [blame] | 728 | APInt this_min = getUnsignedMin().zext(getBitWidth() * 2); |
| 729 | APInt this_max = getUnsignedMax().zext(getBitWidth() * 2); |
| 730 | APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2); |
| 731 | APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2); |
Nick Lewycky | 2951c99 | 2009-07-12 02:19:05 +0000 | [diff] [blame] | 732 | |
Nick Lewycky | 5302389 | 2009-07-13 03:27:41 +0000 | [diff] [blame] | 733 | ConstantRange Result_zext = ConstantRange(this_min * Other_min, |
| 734 | this_max * Other_max + 1); |
James Molloy | dcc78ec | 2015-03-06 15:50:47 +0000 | [diff] [blame] | 735 | ConstantRange UR = Result_zext.truncate(getBitWidth()); |
| 736 | |
Pete Cooper | 34a7a94 | 2016-05-27 17:06:50 +0000 | [diff] [blame] | 737 | // If the unsigned range doesn't wrap, and isn't negative then it's a range |
| 738 | // from one positive number to another which is as good as we can generate. |
| 739 | // In this case, skip the extra work of generating signed ranges which aren't |
| 740 | // going to be better than this range. |
| 741 | if (!UR.isWrappedSet() && UR.getLower().isNonNegative()) |
| 742 | return UR; |
| 743 | |
James Molloy | dcc78ec | 2015-03-06 15:50:47 +0000 | [diff] [blame] | 744 | // Now the signed range. Because we could be dealing with negative numbers |
| 745 | // here, the lower bound is the smallest of the cartesian product of the |
| 746 | // lower and upper ranges; for example: |
| 747 | // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6. |
| 748 | // Similarly for the upper bound, swapping min for max. |
| 749 | |
| 750 | this_min = getSignedMin().sext(getBitWidth() * 2); |
| 751 | this_max = getSignedMax().sext(getBitWidth() * 2); |
| 752 | Other_min = Other.getSignedMin().sext(getBitWidth() * 2); |
| 753 | Other_max = Other.getSignedMax().sext(getBitWidth() * 2); |
| 754 | |
| 755 | auto L = {this_min * Other_min, this_min * Other_max, |
| 756 | this_max * Other_min, this_max * Other_max}; |
| 757 | auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); }; |
| 758 | ConstantRange Result_sext(std::min(L, Compare), std::max(L, Compare) + 1); |
| 759 | ConstantRange SR = Result_sext.truncate(getBitWidth()); |
| 760 | |
| 761 | return UR.getSetSize().ult(SR.getSetSize()) ? UR : SR; |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | ConstantRange |
| 765 | ConstantRange::smax(const ConstantRange &Other) const { |
Dan Gohman | dc33ae2 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 766 | // X smax Y is: range(smax(X_smin, Y_smin), |
| 767 | // smax(X_smax, Y_smax)) |
| 768 | if (isEmptySet() || Other.isEmptySet()) |
| 769 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Dan Gohman | dc33ae2 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 770 | APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin()); |
| 771 | APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1; |
| 772 | if (NewU == NewL) |
| 773 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 774 | return ConstantRange(NewL, NewU); |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | ConstantRange |
| 778 | ConstantRange::umax(const ConstantRange &Other) const { |
| 779 | // X umax Y is: range(umax(X_umin, Y_umin), |
| 780 | // umax(X_umax, Y_umax)) |
| 781 | if (isEmptySet() || Other.isEmptySet()) |
| 782 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 783 | APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin()); |
| 784 | APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1; |
| 785 | if (NewU == NewL) |
| 786 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 787 | return ConstantRange(NewL, NewU); |
| 788 | } |
| 789 | |
| 790 | ConstantRange |
Philip Reames | ba31312 | 2016-02-26 22:08:18 +0000 | [diff] [blame] | 791 | ConstantRange::smin(const ConstantRange &Other) const { |
| 792 | // X smin Y is: range(smin(X_smin, Y_smin), |
| 793 | // smin(X_smax, Y_smax)) |
| 794 | if (isEmptySet() || Other.isEmptySet()) |
| 795 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 796 | APInt NewL = APIntOps::smin(getSignedMin(), Other.getSignedMin()); |
| 797 | APInt NewU = APIntOps::smin(getSignedMax(), Other.getSignedMax()) + 1; |
| 798 | if (NewU == NewL) |
| 799 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 800 | return ConstantRange(NewL, NewU); |
| 801 | } |
| 802 | |
| 803 | ConstantRange |
| 804 | ConstantRange::umin(const ConstantRange &Other) const { |
| 805 | // X umin Y is: range(umin(X_umin, Y_umin), |
| 806 | // umin(X_umax, Y_umax)) |
| 807 | if (isEmptySet() || Other.isEmptySet()) |
| 808 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 809 | APInt NewL = APIntOps::umin(getUnsignedMin(), Other.getUnsignedMin()); |
| 810 | APInt NewU = APIntOps::umin(getUnsignedMax(), Other.getUnsignedMax()) + 1; |
| 811 | if (NewU == NewL) |
| 812 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 813 | return ConstantRange(NewL, NewU); |
| 814 | } |
| 815 | |
| 816 | ConstantRange |
Nick Lewycky | f1b8cb3 | 2009-07-12 05:18:18 +0000 | [diff] [blame] | 817 | ConstantRange::udiv(const ConstantRange &RHS) const { |
| 818 | if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0) |
| 819 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 820 | if (RHS.isFullSet()) |
| 821 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 822 | |
| 823 | APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax()); |
| 824 | |
| 825 | APInt RHS_umin = RHS.getUnsignedMin(); |
| 826 | if (RHS_umin == 0) { |
| 827 | // We want the lowest value in RHS excluding zero. Usually that would be 1 |
| 828 | // except for a range in the form of [X, 1) in which case it would be X. |
| 829 | if (RHS.getUpper() == 1) |
| 830 | RHS_umin = RHS.getLower(); |
| 831 | else |
| 832 | RHS_umin = APInt(getBitWidth(), 1); |
| 833 | } |
| 834 | |
| 835 | APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1; |
| 836 | |
| 837 | // If the LHS is Full and the RHS is a wrapped interval containing 1 then |
| 838 | // this could occur. |
| 839 | if (Lower == Upper) |
| 840 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 841 | |
| 842 | return ConstantRange(Lower, Upper); |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Nuno Lopes | 60d5b1c | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 845 | ConstantRange |
Nick Lewycky | ad48e01 | 2010-09-07 05:39:02 +0000 | [diff] [blame] | 846 | ConstantRange::binaryAnd(const ConstantRange &Other) const { |
| 847 | if (isEmptySet() || Other.isEmptySet()) |
| 848 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 849 | |
| 850 | // TODO: replace this with something less conservative |
| 851 | |
| 852 | APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax()); |
| 853 | if (umin.isAllOnesValue()) |
| 854 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 855 | return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1); |
| 856 | } |
| 857 | |
| 858 | ConstantRange |
| 859 | ConstantRange::binaryOr(const ConstantRange &Other) const { |
| 860 | if (isEmptySet() || Other.isEmptySet()) |
| 861 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 862 | |
| 863 | // TODO: replace this with something less conservative |
| 864 | |
| 865 | APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin()); |
| 866 | if (umax.isMinValue()) |
| 867 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 868 | return ConstantRange(umax, APInt::getNullValue(getBitWidth())); |
| 869 | } |
| 870 | |
| 871 | ConstantRange |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 872 | ConstantRange::shl(const ConstantRange &Other) const { |
| 873 | if (isEmptySet() || Other.isEmptySet()) |
| 874 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Nuno Lopes | 60d5b1c | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 875 | |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 876 | APInt min = getUnsignedMin().shl(Other.getUnsignedMin()); |
| 877 | APInt max = getUnsignedMax().shl(Other.getUnsignedMax()); |
Nuno Lopes | 60d5b1c | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 878 | |
| 879 | // there's no overflow! |
Nuno Lopes | f8fcac7 | 2009-11-12 15:10:33 +0000 | [diff] [blame] | 880 | APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros()); |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 881 | if (Zeros.ugt(Other.getUnsignedMax())) |
| 882 | return ConstantRange(min, max + 1); |
Nuno Lopes | 60d5b1c | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 883 | |
| 884 | // FIXME: implement the other tricky cases |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 885 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
Nuno Lopes | 60d5b1c | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | ConstantRange |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 889 | ConstantRange::lshr(const ConstantRange &Other) const { |
| 890 | if (isEmptySet() || Other.isEmptySet()) |
| 891 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Nuno Lopes | 60d5b1c | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 892 | |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 893 | APInt max = getUnsignedMax().lshr(Other.getUnsignedMin()); |
| 894 | APInt min = getUnsignedMin().lshr(Other.getUnsignedMax()); |
| 895 | if (min == max + 1) |
| 896 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 897 | |
| 898 | return ConstantRange(min, max + 1); |
Nuno Lopes | 60d5b1c | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Owen Anderson | 1a9078b | 2010-08-07 05:47:46 +0000 | [diff] [blame] | 901 | ConstantRange ConstantRange::inverse() const { |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 902 | if (isFullSet()) |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 903 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 904 | if (isEmptySet()) |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 905 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
Owen Anderson | 1a9078b | 2010-08-07 05:47:46 +0000 | [diff] [blame] | 906 | return ConstantRange(Upper, Lower); |
| 907 | } |
| 908 | |
Dan Gohman | dc33ae2 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 909 | /// print - Print out the bounds to a stream... |
Chris Lattner | e0bb9eb | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 910 | /// |
Dan Gohman | dc33ae2 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 911 | void ConstantRange::print(raw_ostream &OS) const { |
Dan Gohman | 837ada7 | 2010-01-26 04:12:55 +0000 | [diff] [blame] | 912 | if (isFullSet()) |
| 913 | OS << "full-set"; |
| 914 | else if (isEmptySet()) |
| 915 | OS << "empty-set"; |
| 916 | else |
| 917 | OS << "[" << Lower << "," << Upper << ")"; |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Dan Gohman | dc33ae2 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 920 | /// dump - Allow printing from a debugger easily... |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 921 | /// |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 922 | LLVM_DUMP_METHOD void ConstantRange::dump() const { |
David Greene | de7b353 | 2010-01-05 01:28:32 +0000 | [diff] [blame] | 923 | print(dbgs()); |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 924 | } |
Peter Collingbourne | ecdd58f | 2016-10-21 19:59:26 +0000 | [diff] [blame] | 925 | |
| 926 | ConstantRange llvm::getConstantRangeFromMetadata(const MDNode &Ranges) { |
| 927 | const unsigned NumRanges = Ranges.getNumOperands() / 2; |
| 928 | assert(NumRanges >= 1 && "Must have at least one range!"); |
| 929 | assert(Ranges.getNumOperands() % 2 == 0 && "Must be a sequence of pairs"); |
| 930 | |
| 931 | auto *FirstLow = mdconst::extract<ConstantInt>(Ranges.getOperand(0)); |
| 932 | auto *FirstHigh = mdconst::extract<ConstantInt>(Ranges.getOperand(1)); |
| 933 | |
| 934 | ConstantRange CR(FirstLow->getValue(), FirstHigh->getValue()); |
| 935 | |
| 936 | for (unsigned i = 1; i < NumRanges; ++i) { |
| 937 | auto *Low = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0)); |
| 938 | auto *High = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1)); |
| 939 | |
| 940 | // Note: unionWith will potentially create a range that contains values not |
| 941 | // contained in any of the original N ranges. |
| 942 | CR = CR.unionWith(ConstantRange(Low->getValue(), High->getValue())); |
| 943 | } |
| 944 | |
| 945 | return CR; |
| 946 | } |