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 | /// |
Craig Topper | 37df018 | 2017-04-13 00:20:31 +0000 | [diff] [blame^] | 43 | ConstantRange::ConstantRange(APInt 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 | |
Craig Topper | 37df018 | 2017-04-13 00:20:31 +0000 | [diff] [blame^] | 46 | ConstantRange::ConstantRange(APInt L, APInt 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 | |
Michael Zolotukhin | c69955c | 2017-03-20 06:33:07 +0000 | [diff] [blame] | 275 | /// isSizeStrictlySmallerThanOf - Compare set size of this range with the range |
| 276 | /// CR. |
| 277 | /// This function is faster than comparing results of getSetSize for the two |
| 278 | /// ranges, because we don't need to extend bitwidth of APInts we're operating |
| 279 | /// with. |
| 280 | /// |
| 281 | bool |
| 282 | ConstantRange::isSizeStrictlySmallerThanOf(const ConstantRange &Other) const { |
| 283 | assert(getBitWidth() == Other.getBitWidth()); |
| 284 | if (isFullSet()) |
| 285 | return false; |
| 286 | if (Other.isFullSet()) |
| 287 | return true; |
| 288 | return (Upper - Lower).ult(Other.Upper - Other.Lower); |
| 289 | } |
| 290 | |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 291 | /// getUnsignedMax - Return the largest unsigned value contained in the |
| 292 | /// ConstantRange. |
| 293 | /// |
| 294 | APInt ConstantRange::getUnsignedMax() const { |
| 295 | if (isFullSet() || isWrappedSet()) |
| 296 | return APInt::getMaxValue(getBitWidth()); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 297 | return getUpper() - 1; |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /// getUnsignedMin - Return the smallest unsigned value contained in the |
| 301 | /// ConstantRange. |
| 302 | /// |
| 303 | APInt ConstantRange::getUnsignedMin() const { |
| 304 | if (isFullSet() || (isWrappedSet() && getUpper() != 0)) |
| 305 | return APInt::getMinValue(getBitWidth()); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 306 | return getLower(); |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /// getSignedMax - Return the largest signed value contained in the |
| 310 | /// ConstantRange. |
| 311 | /// |
| 312 | APInt ConstantRange::getSignedMax() const { |
Zhou Sheng | 01c175e | 2007-04-13 05:57:32 +0000 | [diff] [blame] | 313 | APInt SignedMax(APInt::getSignedMaxValue(getBitWidth())); |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 314 | if (!isWrappedSet()) { |
Nick Lewycky | d18b160 | 2007-06-09 04:20:33 +0000 | [diff] [blame] | 315 | if (getLower().sle(getUpper() - 1)) |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 316 | return getUpper() - 1; |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 317 | return SignedMax; |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 318 | } |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 319 | if (getLower().isNegative() == getUpper().isNegative()) |
| 320 | return SignedMax; |
| 321 | return getUpper() - 1; |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /// getSignedMin - Return the smallest signed value contained in the |
| 325 | /// ConstantRange. |
| 326 | /// |
| 327 | APInt ConstantRange::getSignedMin() const { |
Zhou Sheng | 01c175e | 2007-04-13 05:57:32 +0000 | [diff] [blame] | 328 | APInt SignedMin(APInt::getSignedMinValue(getBitWidth())); |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 329 | if (!isWrappedSet()) { |
Nick Lewycky | d18b160 | 2007-06-09 04:20:33 +0000 | [diff] [blame] | 330 | if (getLower().sle(getUpper() - 1)) |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 331 | return getLower(); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 332 | return SignedMin; |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 333 | } |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 334 | if ((getUpper() - 1).slt(getLower())) { |
| 335 | if (getUpper() != SignedMin) |
| 336 | return SignedMin; |
| 337 | } |
| 338 | return getLower(); |
Nick Lewycky | e455937 | 2007-03-10 15:54:12 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 341 | /// contains - Return true if the specified value is in the set. |
| 342 | /// |
Reid Spencer | 6a44033 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 343 | bool ConstantRange::contains(const APInt &V) const { |
| 344 | if (Lower == Upper) |
| 345 | return isFullSet(); |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 346 | |
Reid Spencer | 6a44033 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 347 | if (!isWrappedSet()) |
| 348 | return Lower.ule(V) && V.ult(Upper); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 349 | return Lower.ule(V) || V.ult(Upper); |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Nick Lewycky | dcfdce9 | 2009-07-11 06:15:39 +0000 | [diff] [blame] | 352 | /// contains - Return true if the argument is a subset of this range. |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 353 | /// Two equal sets contain each other. The empty set contained by all other |
| 354 | /// sets. |
Nick Lewycky | dcfdce9 | 2009-07-11 06:15:39 +0000 | [diff] [blame] | 355 | /// |
| 356 | bool ConstantRange::contains(const ConstantRange &Other) const { |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 357 | if (isFullSet() || Other.isEmptySet()) return true; |
| 358 | if (isEmptySet() || Other.isFullSet()) return false; |
Nick Lewycky | dcfdce9 | 2009-07-11 06:15:39 +0000 | [diff] [blame] | 359 | |
| 360 | if (!isWrappedSet()) { |
| 361 | if (Other.isWrappedSet()) |
| 362 | return false; |
| 363 | |
| 364 | return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper); |
| 365 | } |
| 366 | |
| 367 | if (!Other.isWrappedSet()) |
| 368 | return Other.getUpper().ule(Upper) || |
| 369 | Lower.ule(Other.getLower()); |
| 370 | |
| 371 | return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower()); |
| 372 | } |
| 373 | |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 374 | /// subtract - Subtract the specified constant from the endpoints of this |
| 375 | /// constant range. |
Reid Spencer | 3a7e9d8 | 2007-02-28 19:57:34 +0000 | [diff] [blame] | 376 | ConstantRange ConstantRange::subtract(const APInt &Val) const { |
Reid Spencer | 9b3d6ec | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 377 | assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width"); |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 378 | // If the set is empty or full, don't modify the endpoints. |
Reid Spencer | e1f3f19 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 379 | if (Lower == Upper) |
| 380 | return *this; |
Reid Spencer | e1f3f19 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 381 | return ConstantRange(Lower - Val, Upper - Val); |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 382 | } |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 383 | |
Nuno Lopes | 5020db2 | 2012-06-28 16:10:13 +0000 | [diff] [blame] | 384 | /// \brief Subtract the specified range from this range (aka relative complement |
| 385 | /// of the sets). |
| 386 | ConstantRange ConstantRange::difference(const ConstantRange &CR) const { |
| 387 | return intersectWith(CR.inverse()); |
| 388 | } |
| 389 | |
Nick Lewycky | 63f1108 | 2007-02-11 00:58:49 +0000 | [diff] [blame] | 390 | /// intersectWith - Return the range that results from the intersection of this |
Nick Lewycky | 0d13903 | 2009-07-18 06:34:42 +0000 | [diff] [blame] | 391 | /// range with another range. The resultant range is guaranteed to include all |
| 392 | /// elements contained in both input ranges, and to have the smallest possible |
| 393 | /// set size that does so. Because there may be two intersections with the |
| 394 | /// 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] | 395 | ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const { |
Reid Spencer | 9b3d6ec | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 396 | assert(getBitWidth() == CR.getBitWidth() && |
| 397 | "ConstantRange types don't agree!"); |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 398 | |
| 399 | // Handle common cases. |
| 400 | if ( isEmptySet() || CR.isFullSet()) return *this; |
| 401 | if (CR.isEmptySet() || isFullSet()) return CR; |
| 402 | |
| 403 | if (!isWrappedSet() && CR.isWrappedSet()) |
Nick Lewycky | 0d13903 | 2009-07-18 06:34:42 +0000 | [diff] [blame] | 404 | return CR.intersectWith(*this); |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 405 | |
| 406 | if (!isWrappedSet() && !CR.isWrappedSet()) { |
| 407 | if (Lower.ult(CR.Lower)) { |
| 408 | if (Upper.ule(CR.Lower)) |
| 409 | return ConstantRange(getBitWidth(), false); |
| 410 | |
| 411 | if (Upper.ult(CR.Upper)) |
| 412 | return ConstantRange(CR.Lower, Upper); |
| 413 | |
| 414 | return CR; |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 415 | } |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 416 | if (Upper.ult(CR.Upper)) |
| 417 | return *this; |
| 418 | |
| 419 | if (Lower.ult(CR.Upper)) |
| 420 | return ConstantRange(Lower, CR.Upper); |
| 421 | |
| 422 | return ConstantRange(getBitWidth(), false); |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | if (isWrappedSet() && !CR.isWrappedSet()) { |
| 426 | if (CR.Lower.ult(Upper)) { |
| 427 | if (CR.Upper.ult(Upper)) |
| 428 | return CR; |
| 429 | |
Nuno Lopes | 63afc08 | 2012-05-18 00:14:36 +0000 | [diff] [blame] | 430 | if (CR.Upper.ule(Lower)) |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 431 | return ConstantRange(CR.Lower, Upper); |
| 432 | |
Michael Zolotukhin | c69955c | 2017-03-20 06:33:07 +0000 | [diff] [blame] | 433 | if (isSizeStrictlySmallerThanOf(CR)) |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 434 | return *this; |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 435 | return CR; |
| 436 | } |
| 437 | if (CR.Lower.ult(Lower)) { |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 438 | if (CR.Upper.ule(Lower)) |
| 439 | return ConstantRange(getBitWidth(), false); |
| 440 | |
| 441 | return ConstantRange(Lower, CR.Upper); |
| 442 | } |
| 443 | return CR; |
| 444 | } |
| 445 | |
| 446 | if (CR.Upper.ult(Upper)) { |
| 447 | if (CR.Lower.ult(Upper)) { |
Michael Zolotukhin | c69955c | 2017-03-20 06:33:07 +0000 | [diff] [blame] | 448 | if (isSizeStrictlySmallerThanOf(CR)) |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 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 | if (CR.Lower.ult(Lower)) |
| 454 | return ConstantRange(Lower, CR.Upper); |
| 455 | |
| 456 | return CR; |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 457 | } |
Nuno Lopes | ebb0c94 | 2012-06-28 00:59:33 +0000 | [diff] [blame] | 458 | if (CR.Upper.ule(Lower)) { |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 459 | if (CR.Lower.ult(Lower)) |
| 460 | return *this; |
| 461 | |
| 462 | return ConstantRange(CR.Lower, Upper); |
| 463 | } |
Michael Zolotukhin | c69955c | 2017-03-20 06:33:07 +0000 | [diff] [blame] | 464 | if (isSizeStrictlySmallerThanOf(CR)) |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 465 | return *this; |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 466 | return CR; |
Nick Lewycky | 61b4a26 | 2007-07-14 02:51:34 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | |
Nick Lewycky | 63f1108 | 2007-02-11 00:58:49 +0000 | [diff] [blame] | 470 | /// 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] | 471 | /// another range. The resultant range is guaranteed to include the elements of |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 472 | /// both sets, but may contain more. For example, [3, 9) union [12,15) is |
| 473 | /// [3, 15), which includes 9, 10, and 11, which were not included in either |
| 474 | /// set before. |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 475 | /// |
Reid Spencer | 6a44033 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 476 | ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const { |
Reid Spencer | 9b3d6ec | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 477 | assert(getBitWidth() == CR.getBitWidth() && |
| 478 | "ConstantRange types don't agree!"); |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 479 | |
Nick Lewycky | cf87f9e | 2007-03-02 03:33:05 +0000 | [diff] [blame] | 480 | if ( isFullSet() || CR.isEmptySet()) return *this; |
| 481 | if (CR.isFullSet() || isEmptySet()) return CR; |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 482 | |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 483 | if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this); |
| 484 | |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 485 | if (!isWrappedSet() && !CR.isWrappedSet()) { |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 486 | if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) { |
| 487 | // If the two ranges are disjoint, find the smaller gap and bridge it. |
| 488 | APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper; |
| 489 | if (d1.ult(d2)) |
| 490 | return ConstantRange(Lower, CR.Upper); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 491 | return ConstantRange(CR.Lower, Upper); |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | APInt L = Lower, U = Upper; |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 495 | if (CR.Lower.ult(L)) |
| 496 | L = CR.Lower; |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 497 | if ((CR.Upper - 1).ugt(U - 1)) |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 498 | U = CR.Upper; |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 499 | |
| 500 | if (L == 0 && U == 0) |
| 501 | return ConstantRange(getBitWidth()); |
| 502 | |
| 503 | return ConstantRange(L, U); |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 506 | if (!CR.isWrappedSet()) { |
| 507 | // ------U L----- and ------U L----- : this |
| 508 | // L--U L--U : CR |
| 509 | if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower)) |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 510 | return *this; |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 511 | |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 512 | // ------U L----- : this |
| 513 | // L---------U : CR |
| 514 | if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 515 | return ConstantRange(getBitWidth()); |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 516 | |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 517 | // ----U L---- : this |
| 518 | // L---U : CR |
| 519 | // <d1> <d2> |
| 520 | if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) { |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 521 | APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper; |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 522 | if (d1.ult(d2)) |
| 523 | return ConstantRange(Lower, CR.Upper); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 524 | return ConstantRange(CR.Lower, Upper); |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 527 | // ----U L----- : this |
| 528 | // L----U : CR |
| 529 | if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) |
| 530 | return ConstantRange(CR.Lower, Upper); |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 531 | |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 532 | // ------U L---- : this |
| 533 | // L-----U : CR |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 534 | assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) && |
| 535 | "ConstantRange::unionWith missed a case with one range wrapped"); |
| 536 | return ConstantRange(Lower, CR.Upper); |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 539 | // ------U L---- and ------U L---- : this |
| 540 | // -U L----------- and ------------U L : CR |
| 541 | if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper)) |
| 542 | return ConstantRange(getBitWidth()); |
Nick Lewycky | f22938a | 2007-04-01 03:47:44 +0000 | [diff] [blame] | 543 | |
Nick Lewycky | 567daf3 | 2009-07-19 03:44:35 +0000 | [diff] [blame] | 544 | APInt L = Lower, U = Upper; |
| 545 | if (CR.Upper.ugt(U)) |
| 546 | U = CR.Upper; |
| 547 | if (CR.Lower.ult(L)) |
| 548 | L = CR.Lower; |
Nick Lewycky | cf87f9e | 2007-03-02 03:33:05 +0000 | [diff] [blame] | 549 | |
| 550 | return ConstantRange(L, U); |
Chris Lattner | 113f2ae | 2002-09-01 23:53:36 +0000 | [diff] [blame] | 551 | } |
Chris Lattner | e0bb9eb | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 552 | |
Philip Reames | 4d00af1 | 2016-12-01 20:08:47 +0000 | [diff] [blame] | 553 | ConstantRange ConstantRange::castOp(Instruction::CastOps CastOp, |
| 554 | uint32_t ResultBitWidth) const { |
| 555 | switch (CastOp) { |
| 556 | default: |
| 557 | llvm_unreachable("unsupported cast type"); |
| 558 | case Instruction::Trunc: |
| 559 | return truncate(ResultBitWidth); |
| 560 | case Instruction::SExt: |
| 561 | return signExtend(ResultBitWidth); |
| 562 | case Instruction::ZExt: |
| 563 | return zeroExtend(ResultBitWidth); |
| 564 | case Instruction::BitCast: |
| 565 | return *this; |
| 566 | case Instruction::FPToUI: |
| 567 | case Instruction::FPToSI: |
| 568 | if (getBitWidth() == ResultBitWidth) |
| 569 | return *this; |
| 570 | else |
| 571 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 572 | case Instruction::UIToFP: { |
| 573 | // TODO: use input range if available |
| 574 | auto BW = getBitWidth(); |
| 575 | APInt Min = APInt::getMinValue(BW).zextOrSelf(ResultBitWidth); |
| 576 | APInt Max = APInt::getMaxValue(BW).zextOrSelf(ResultBitWidth); |
| 577 | return ConstantRange(Min, Max); |
| 578 | } |
| 579 | case Instruction::SIToFP: { |
| 580 | // TODO: use input range if available |
| 581 | auto BW = getBitWidth(); |
| 582 | APInt SMin = APInt::getSignedMinValue(BW).sextOrSelf(ResultBitWidth); |
| 583 | APInt SMax = APInt::getSignedMaxValue(BW).sextOrSelf(ResultBitWidth); |
| 584 | return ConstantRange(SMin, SMax); |
| 585 | } |
| 586 | case Instruction::FPTrunc: |
| 587 | case Instruction::FPExt: |
| 588 | case Instruction::IntToPtr: |
| 589 | case Instruction::PtrToInt: |
| 590 | case Instruction::AddrSpaceCast: |
| 591 | // Conservatively return full set. |
| 592 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 593 | }; |
| 594 | } |
| 595 | |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 596 | /// zeroExtend - Return a new range in the specified integer type, which must |
| 597 | /// be strictly larger than the current type. The returned range will |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 598 | /// 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] | 599 | /// zero extended. |
Reid Spencer | 9b3d6ec | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 600 | ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const { |
Nick Lewycky | a35462d | 2010-09-06 23:52:49 +0000 | [diff] [blame] | 601 | if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false); |
| 602 | |
Reid Spencer | 9b3d6ec | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 603 | unsigned SrcTySize = getBitWidth(); |
Reid Spencer | e1f3f19 | 2007-02-28 17:36:23 +0000 | [diff] [blame] | 604 | assert(SrcTySize < DstTySize && "Not a value extension"); |
Nuno Lopes | eb9d275 | 2012-07-23 20:33:29 +0000 | [diff] [blame] | 605 | if (isFullSet() || isWrappedSet()) { |
Nick Lewycky | a35462d | 2010-09-06 23:52:49 +0000 | [diff] [blame] | 606 | // Change into [0, 1 << src bit width) |
Nuno Lopes | eb9d275 | 2012-07-23 20:33:29 +0000 | [diff] [blame] | 607 | APInt LowerExt(DstTySize, 0); |
| 608 | if (!Upper) // special case: [X, 0) -- not really wrapping around |
| 609 | LowerExt = Lower.zext(DstTySize); |
Benjamin Kramer | fc3ea6f | 2013-07-11 16:05:50 +0000 | [diff] [blame] | 610 | return ConstantRange(LowerExt, APInt::getOneBitSet(DstTySize, SrcTySize)); |
Nuno Lopes | eb9d275 | 2012-07-23 20:33:29 +0000 | [diff] [blame] | 611 | } |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 612 | |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 613 | return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize)); |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Nick Lewycky | b89804f | 2007-04-07 15:41:33 +0000 | [diff] [blame] | 616 | /// signExtend - Return a new range in the specified integer type, which must |
| 617 | /// be strictly larger than the current type. The returned range will |
| 618 | /// correspond to the possible range of values as if the source range had been |
| 619 | /// sign extended. |
| 620 | ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const { |
Nick Lewycky | a35462d | 2010-09-06 23:52:49 +0000 | [diff] [blame] | 621 | if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false); |
| 622 | |
Nick Lewycky | b89804f | 2007-04-07 15:41:33 +0000 | [diff] [blame] | 623 | unsigned SrcTySize = getBitWidth(); |
| 624 | assert(SrcTySize < DstTySize && "Not a value extension"); |
Nuno Lopes | 1112eca | 2013-10-30 15:36:50 +0000 | [diff] [blame] | 625 | |
| 626 | // special case: [X, INT_MIN) -- not really wrapping around |
Nuno Lopes | 14d4b0c | 2013-10-31 19:53:53 +0000 | [diff] [blame] | 627 | if (Upper.isMinSignedValue()) |
Nuno Lopes | 1112eca | 2013-10-30 15:36:50 +0000 | [diff] [blame] | 628 | return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize)); |
| 629 | |
Nick Lewycky | a35462d | 2010-09-06 23:52:49 +0000 | [diff] [blame] | 630 | if (isFullSet() || isSignWrappedSet()) { |
Nick Lewycky | b89804f | 2007-04-07 15:41:33 +0000 | [diff] [blame] | 631 | return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1), |
Nick Lewycky | 5edc459 | 2009-07-13 04:17:23 +0000 | [diff] [blame] | 632 | APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1); |
Nick Lewycky | b89804f | 2007-04-07 15:41:33 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 635 | return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize)); |
Nick Lewycky | b89804f | 2007-04-07 15:41:33 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 638 | /// truncate - Return a new range in the specified integer type, which must be |
| 639 | /// strictly smaller than the current type. The returned range will |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 640 | /// 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] | 641 | /// truncated to the specified type. |
Reid Spencer | 9b3d6ec | 2007-02-28 22:02:48 +0000 | [diff] [blame] | 642 | ConstantRange ConstantRange::truncate(uint32_t DstTySize) const { |
Benjamin Kramer | 6709e05 | 2011-11-24 17:24:33 +0000 | [diff] [blame] | 643 | assert(getBitWidth() > DstTySize && "Not a value truncation"); |
Nuno Lopes | c14776d | 2012-07-19 16:27:45 +0000 | [diff] [blame] | 644 | if (isEmptySet()) |
| 645 | return ConstantRange(DstTySize, /*isFullSet=*/false); |
| 646 | if (isFullSet()) |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 647 | return ConstantRange(DstTySize, /*isFullSet=*/true); |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 648 | |
Nuno Lopes | c14776d | 2012-07-19 16:27:45 +0000 | [diff] [blame] | 649 | APInt MaxValue = APInt::getMaxValue(DstTySize).zext(getBitWidth()); |
| 650 | APInt MaxBitValue(getBitWidth(), 0); |
| 651 | MaxBitValue.setBit(DstTySize); |
| 652 | |
| 653 | APInt LowerDiv(Lower), UpperDiv(Upper); |
| 654 | ConstantRange Union(DstTySize, /*isFullSet=*/false); |
| 655 | |
| 656 | // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue] |
| 657 | // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and |
| 658 | // then we do the union with [MaxValue, Upper) |
| 659 | if (isWrappedSet()) { |
Nick Lewycky | bfad015 | 2016-06-06 01:51:23 +0000 | [diff] [blame] | 660 | // 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] | 661 | if (Upper.uge(MaxValue)) |
| 662 | return ConstantRange(DstTySize, /*isFullSet=*/true); |
| 663 | |
| 664 | Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize)); |
| 665 | UpperDiv = APInt::getMaxValue(getBitWidth()); |
| 666 | |
| 667 | // Union covers the MaxValue case, so return if the remaining range is just |
| 668 | // MaxValue. |
| 669 | if (LowerDiv == UpperDiv) |
| 670 | return Union; |
| 671 | } |
| 672 | |
| 673 | // Chop off the most significant bits that are past the destination bitwidth. |
| 674 | if (LowerDiv.uge(MaxValue)) { |
| 675 | APInt Div(getBitWidth(), 0); |
| 676 | APInt::udivrem(LowerDiv, MaxBitValue, Div, LowerDiv); |
| 677 | UpperDiv = UpperDiv - MaxBitValue * Div; |
| 678 | } |
| 679 | |
| 680 | if (UpperDiv.ule(MaxValue)) |
| 681 | return ConstantRange(LowerDiv.trunc(DstTySize), |
| 682 | UpperDiv.trunc(DstTySize)).unionWith(Union); |
| 683 | |
Nick Lewycky | bfad015 | 2016-06-06 01:51:23 +0000 | [diff] [blame] | 684 | // 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] | 685 | APInt UpperModulo = UpperDiv - MaxBitValue; |
| 686 | if (UpperModulo.ult(LowerDiv)) |
| 687 | return ConstantRange(LowerDiv.trunc(DstTySize), |
| 688 | UpperModulo.trunc(DstTySize)).unionWith(Union); |
| 689 | |
| 690 | return ConstantRange(DstTySize, /*isFullSet=*/true); |
Chris Lattner | 55481f7 | 2004-03-30 00:20:08 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Nuno Lopes | 640eb70 | 2009-11-09 15:36:28 +0000 | [diff] [blame] | 693 | /// zextOrTrunc - make this range have the bit width given by \p DstTySize. The |
| 694 | /// value is zero extended, truncated, or left alone to make it that width. |
| 695 | ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const { |
| 696 | unsigned SrcTySize = getBitWidth(); |
| 697 | if (SrcTySize > DstTySize) |
| 698 | return truncate(DstTySize); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 699 | if (SrcTySize < DstTySize) |
Nuno Lopes | 640eb70 | 2009-11-09 15:36:28 +0000 | [diff] [blame] | 700 | return zeroExtend(DstTySize); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 701 | return *this; |
Nuno Lopes | 640eb70 | 2009-11-09 15:36:28 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /// sextOrTrunc - make this range have the bit width given by \p DstTySize. The |
| 705 | /// value is sign extended, truncated, or left alone to make it that width. |
| 706 | ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const { |
| 707 | unsigned SrcTySize = getBitWidth(); |
| 708 | if (SrcTySize > DstTySize) |
| 709 | return truncate(DstTySize); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 710 | if (SrcTySize < DstTySize) |
Nuno Lopes | 640eb70 | 2009-11-09 15:36:28 +0000 | [diff] [blame] | 711 | return signExtend(DstTySize); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 712 | return *this; |
Nuno Lopes | 640eb70 | 2009-11-09 15:36:28 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Philip Reames | 4d00af1 | 2016-12-01 20:08:47 +0000 | [diff] [blame] | 715 | ConstantRange ConstantRange::binaryOp(Instruction::BinaryOps BinOp, |
| 716 | const ConstantRange &Other) const { |
| 717 | assert(BinOp >= Instruction::BinaryOpsBegin && |
| 718 | BinOp < Instruction::BinaryOpsEnd && "Binary operators only!"); |
| 719 | |
| 720 | switch (BinOp) { |
| 721 | case Instruction::Add: |
| 722 | return add(Other); |
| 723 | case Instruction::Sub: |
| 724 | return sub(Other); |
| 725 | case Instruction::Mul: |
| 726 | return multiply(Other); |
| 727 | case Instruction::UDiv: |
| 728 | return udiv(Other); |
| 729 | case Instruction::Shl: |
| 730 | return shl(Other); |
| 731 | case Instruction::LShr: |
| 732 | return lshr(Other); |
| 733 | case Instruction::And: |
| 734 | return binaryAnd(Other); |
| 735 | case Instruction::Or: |
| 736 | return binaryOr(Other); |
| 737 | // Note: floating point operations applied to abstract ranges are just |
| 738 | // ideal integer operations with a lossy representation |
| 739 | case Instruction::FAdd: |
| 740 | return add(Other); |
| 741 | case Instruction::FSub: |
| 742 | return sub(Other); |
| 743 | case Instruction::FMul: |
| 744 | return multiply(Other); |
| 745 | default: |
| 746 | // Conservatively return full set. |
| 747 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 748 | } |
| 749 | } |
| 750 | |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 751 | ConstantRange |
| 752 | ConstantRange::add(const ConstantRange &Other) const { |
| 753 | if (isEmptySet() || Other.isEmptySet()) |
| 754 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Nick Lewycky | 73b704d | 2009-07-13 02:49:08 +0000 | [diff] [blame] | 755 | if (isFullSet() || Other.isFullSet()) |
| 756 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 757 | |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 758 | APInt NewLower = getLower() + Other.getLower(); |
| 759 | APInt NewUpper = getUpper() + Other.getUpper() - 1; |
| 760 | if (NewLower == NewUpper) |
| 761 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 762 | |
| 763 | ConstantRange X = ConstantRange(NewLower, NewUpper); |
Michael Zolotukhin | c69955c | 2017-03-20 06:33:07 +0000 | [diff] [blame] | 764 | if (X.isSizeStrictlySmallerThanOf(*this) || |
| 765 | X.isSizeStrictlySmallerThanOf(Other)) |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 766 | // We've wrapped, therefore, full set. |
| 767 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 768 | return X; |
Chris Lattner | e0bb9eb | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Artur Pilipenko | ed84103 | 2016-10-19 14:44:23 +0000 | [diff] [blame] | 771 | ConstantRange ConstantRange::addWithNoSignedWrap(const APInt &Other) const { |
| 772 | // Calculate the subset of this range such that "X + Other" is |
| 773 | // guaranteed not to wrap (overflow) for all X in this subset. |
| 774 | // makeGuaranteedNoWrapRegion will produce an exact NSW range since we are |
| 775 | // passing a single element range. |
| 776 | auto NSWRange = ConstantRange::makeGuaranteedNoWrapRegion(BinaryOperator::Add, |
| 777 | ConstantRange(Other), |
| 778 | OverflowingBinaryOperator::NoSignedWrap); |
| 779 | auto NSWConstrainedRange = intersectWith(NSWRange); |
| 780 | |
| 781 | return NSWConstrainedRange.add(ConstantRange(Other)); |
| 782 | } |
| 783 | |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 784 | ConstantRange |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 785 | ConstantRange::sub(const ConstantRange &Other) const { |
| 786 | if (isEmptySet() || Other.isEmptySet()) |
| 787 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 788 | if (isFullSet() || Other.isFullSet()) |
| 789 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 790 | |
Nick Lewycky | 5dc6b79 | 2011-06-22 21:13:46 +0000 | [diff] [blame] | 791 | APInt NewLower = getLower() - Other.getUpper() + 1; |
| 792 | APInt NewUpper = getUpper() - Other.getLower(); |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 793 | if (NewLower == NewUpper) |
| 794 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 795 | |
| 796 | ConstantRange X = ConstantRange(NewLower, NewUpper); |
Michael Zolotukhin | c69955c | 2017-03-20 06:33:07 +0000 | [diff] [blame] | 797 | if (X.isSizeStrictlySmallerThanOf(*this) || |
| 798 | X.isSizeStrictlySmallerThanOf(Other)) |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 799 | // We've wrapped, therefore, full set. |
| 800 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 801 | return X; |
| 802 | } |
| 803 | |
| 804 | ConstantRange |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 805 | ConstantRange::multiply(const ConstantRange &Other) const { |
Dan Gohman | 3f8ed9e | 2010-01-26 15:56:18 +0000 | [diff] [blame] | 806 | // TODO: If either operand is a single element and the multiply is known to |
| 807 | // be non-wrapping, round the result min and max value to the appropriate |
| 808 | // multiple of that element. If wrapping is possible, at least adjust the |
| 809 | // 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] | 810 | |
Nick Lewycky | 2951c99 | 2009-07-12 02:19:05 +0000 | [diff] [blame] | 811 | if (isEmptySet() || Other.isEmptySet()) |
| 812 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Nuno Lopes | 986cc18 | 2012-07-16 20:47:16 +0000 | [diff] [blame] | 813 | |
James Molloy | dcc78ec | 2015-03-06 15:50:47 +0000 | [diff] [blame] | 814 | // Multiplication is signedness-independent. However different ranges can be |
| 815 | // obtained depending on how the input ranges are treated. These different |
| 816 | // ranges are all conservatively correct, but one might be better than the |
| 817 | // other. We calculate two ranges; one treating the inputs as unsigned |
| 818 | // and the other signed, then return the smallest of these ranges. |
| 819 | |
| 820 | // Unsigned range first. |
Nick Lewycky | 5302389 | 2009-07-13 03:27:41 +0000 | [diff] [blame] | 821 | APInt this_min = getUnsignedMin().zext(getBitWidth() * 2); |
| 822 | APInt this_max = getUnsignedMax().zext(getBitWidth() * 2); |
| 823 | APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2); |
| 824 | APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2); |
Nick Lewycky | 2951c99 | 2009-07-12 02:19:05 +0000 | [diff] [blame] | 825 | |
Nick Lewycky | 5302389 | 2009-07-13 03:27:41 +0000 | [diff] [blame] | 826 | ConstantRange Result_zext = ConstantRange(this_min * Other_min, |
| 827 | this_max * Other_max + 1); |
James Molloy | dcc78ec | 2015-03-06 15:50:47 +0000 | [diff] [blame] | 828 | ConstantRange UR = Result_zext.truncate(getBitWidth()); |
| 829 | |
Pete Cooper | 34a7a94 | 2016-05-27 17:06:50 +0000 | [diff] [blame] | 830 | // If the unsigned range doesn't wrap, and isn't negative then it's a range |
| 831 | // from one positive number to another which is as good as we can generate. |
| 832 | // In this case, skip the extra work of generating signed ranges which aren't |
| 833 | // going to be better than this range. |
| 834 | if (!UR.isWrappedSet() && UR.getLower().isNonNegative()) |
| 835 | return UR; |
| 836 | |
James Molloy | dcc78ec | 2015-03-06 15:50:47 +0000 | [diff] [blame] | 837 | // Now the signed range. Because we could be dealing with negative numbers |
| 838 | // here, the lower bound is the smallest of the cartesian product of the |
| 839 | // lower and upper ranges; for example: |
| 840 | // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6. |
| 841 | // Similarly for the upper bound, swapping min for max. |
| 842 | |
| 843 | this_min = getSignedMin().sext(getBitWidth() * 2); |
| 844 | this_max = getSignedMax().sext(getBitWidth() * 2); |
| 845 | Other_min = Other.getSignedMin().sext(getBitWidth() * 2); |
| 846 | Other_max = Other.getSignedMax().sext(getBitWidth() * 2); |
| 847 | |
| 848 | auto L = {this_min * Other_min, this_min * Other_max, |
| 849 | this_max * Other_min, this_max * Other_max}; |
| 850 | auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); }; |
| 851 | ConstantRange Result_sext(std::min(L, Compare), std::max(L, Compare) + 1); |
| 852 | ConstantRange SR = Result_sext.truncate(getBitWidth()); |
| 853 | |
Michael Zolotukhin | c69955c | 2017-03-20 06:33:07 +0000 | [diff] [blame] | 854 | return UR.isSizeStrictlySmallerThanOf(SR) ? UR : SR; |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | ConstantRange |
| 858 | ConstantRange::smax(const ConstantRange &Other) const { |
Dan Gohman | dc33ae2 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 859 | // X smax Y is: range(smax(X_smin, Y_smin), |
| 860 | // smax(X_smax, Y_smax)) |
| 861 | if (isEmptySet() || Other.isEmptySet()) |
| 862 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Dan Gohman | dc33ae2 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 863 | APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin()); |
| 864 | APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1; |
| 865 | if (NewU == NewL) |
| 866 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 867 | return ConstantRange(NewL, NewU); |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | ConstantRange |
| 871 | ConstantRange::umax(const ConstantRange &Other) const { |
| 872 | // X umax Y is: range(umax(X_umin, Y_umin), |
| 873 | // umax(X_umax, Y_umax)) |
| 874 | if (isEmptySet() || Other.isEmptySet()) |
| 875 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 876 | APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin()); |
| 877 | APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1; |
| 878 | if (NewU == NewL) |
| 879 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 880 | return ConstantRange(NewL, NewU); |
| 881 | } |
| 882 | |
| 883 | ConstantRange |
Philip Reames | ba31312 | 2016-02-26 22:08:18 +0000 | [diff] [blame] | 884 | ConstantRange::smin(const ConstantRange &Other) const { |
| 885 | // X smin Y is: range(smin(X_smin, Y_smin), |
| 886 | // smin(X_smax, Y_smax)) |
| 887 | if (isEmptySet() || Other.isEmptySet()) |
| 888 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 889 | APInt NewL = APIntOps::smin(getSignedMin(), Other.getSignedMin()); |
| 890 | APInt NewU = APIntOps::smin(getSignedMax(), Other.getSignedMax()) + 1; |
| 891 | if (NewU == NewL) |
| 892 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 893 | return ConstantRange(NewL, NewU); |
| 894 | } |
| 895 | |
| 896 | ConstantRange |
| 897 | ConstantRange::umin(const ConstantRange &Other) const { |
| 898 | // X umin Y is: range(umin(X_umin, Y_umin), |
| 899 | // umin(X_umax, Y_umax)) |
| 900 | if (isEmptySet() || Other.isEmptySet()) |
| 901 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 902 | APInt NewL = APIntOps::umin(getUnsignedMin(), Other.getUnsignedMin()); |
| 903 | APInt NewU = APIntOps::umin(getUnsignedMax(), Other.getUnsignedMax()) + 1; |
| 904 | if (NewU == NewL) |
| 905 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 906 | return ConstantRange(NewL, NewU); |
| 907 | } |
| 908 | |
| 909 | ConstantRange |
Nick Lewycky | f1b8cb3 | 2009-07-12 05:18:18 +0000 | [diff] [blame] | 910 | ConstantRange::udiv(const ConstantRange &RHS) const { |
| 911 | if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0) |
| 912 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 913 | if (RHS.isFullSet()) |
| 914 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 915 | |
| 916 | APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax()); |
| 917 | |
| 918 | APInt RHS_umin = RHS.getUnsignedMin(); |
| 919 | if (RHS_umin == 0) { |
| 920 | // We want the lowest value in RHS excluding zero. Usually that would be 1 |
| 921 | // except for a range in the form of [X, 1) in which case it would be X. |
| 922 | if (RHS.getUpper() == 1) |
| 923 | RHS_umin = RHS.getLower(); |
| 924 | else |
| 925 | RHS_umin = APInt(getBitWidth(), 1); |
| 926 | } |
| 927 | |
| 928 | APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1; |
| 929 | |
| 930 | // If the LHS is Full and the RHS is a wrapped interval containing 1 then |
| 931 | // this could occur. |
| 932 | if (Lower == Upper) |
| 933 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 934 | |
| 935 | return ConstantRange(Lower, Upper); |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Nuno Lopes | 60d5b1c | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 938 | ConstantRange |
Nick Lewycky | ad48e01 | 2010-09-07 05:39:02 +0000 | [diff] [blame] | 939 | ConstantRange::binaryAnd(const ConstantRange &Other) const { |
| 940 | if (isEmptySet() || Other.isEmptySet()) |
| 941 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 942 | |
| 943 | // TODO: replace this with something less conservative |
| 944 | |
| 945 | APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax()); |
| 946 | if (umin.isAllOnesValue()) |
| 947 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 948 | return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1); |
| 949 | } |
| 950 | |
| 951 | ConstantRange |
| 952 | ConstantRange::binaryOr(const ConstantRange &Other) const { |
| 953 | if (isEmptySet() || Other.isEmptySet()) |
| 954 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
| 955 | |
| 956 | // TODO: replace this with something less conservative |
| 957 | |
| 958 | APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin()); |
| 959 | if (umax.isMinValue()) |
| 960 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 961 | return ConstantRange(umax, APInt::getNullValue(getBitWidth())); |
| 962 | } |
| 963 | |
| 964 | ConstantRange |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 965 | ConstantRange::shl(const ConstantRange &Other) const { |
| 966 | if (isEmptySet() || Other.isEmptySet()) |
| 967 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Nuno Lopes | 60d5b1c | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 968 | |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 969 | APInt min = getUnsignedMin().shl(Other.getUnsignedMin()); |
| 970 | APInt max = getUnsignedMax().shl(Other.getUnsignedMax()); |
Nuno Lopes | 60d5b1c | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 971 | |
| 972 | // there's no overflow! |
Nuno Lopes | f8fcac7 | 2009-11-12 15:10:33 +0000 | [diff] [blame] | 973 | APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros()); |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 974 | if (Zeros.ugt(Other.getUnsignedMax())) |
| 975 | return ConstantRange(min, max + 1); |
Nuno Lopes | 60d5b1c | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 976 | |
| 977 | // FIXME: implement the other tricky cases |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 978 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
Nuno Lopes | 60d5b1c | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | ConstantRange |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 982 | ConstantRange::lshr(const ConstantRange &Other) const { |
| 983 | if (isEmptySet() || Other.isEmptySet()) |
| 984 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Nuno Lopes | 60d5b1c | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 985 | |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 986 | APInt max = getUnsignedMax().lshr(Other.getUnsignedMin()); |
| 987 | APInt min = getUnsignedMin().lshr(Other.getUnsignedMax()); |
| 988 | if (min == max + 1) |
| 989 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
| 990 | |
| 991 | return ConstantRange(min, max + 1); |
Nuno Lopes | 60d5b1c | 2009-11-12 14:53:53 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Owen Anderson | 1a9078b | 2010-08-07 05:47:46 +0000 | [diff] [blame] | 994 | ConstantRange ConstantRange::inverse() const { |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 995 | if (isFullSet()) |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 996 | return ConstantRange(getBitWidth(), /*isFullSet=*/false); |
Nick Lewycky | 228f5b4 | 2012-01-03 20:33:00 +0000 | [diff] [blame] | 997 | if (isEmptySet()) |
Nick Lewycky | d385c22 | 2010-08-11 22:04:36 +0000 | [diff] [blame] | 998 | return ConstantRange(getBitWidth(), /*isFullSet=*/true); |
Owen Anderson | 1a9078b | 2010-08-07 05:47:46 +0000 | [diff] [blame] | 999 | return ConstantRange(Upper, Lower); |
| 1000 | } |
| 1001 | |
Dan Gohman | dc33ae2 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 1002 | /// print - Print out the bounds to a stream... |
Chris Lattner | e0bb9eb | 2002-09-02 00:18:22 +0000 | [diff] [blame] | 1003 | /// |
Dan Gohman | dc33ae2 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 1004 | void ConstantRange::print(raw_ostream &OS) const { |
Dan Gohman | 837ada7 | 2010-01-26 04:12:55 +0000 | [diff] [blame] | 1005 | if (isFullSet()) |
| 1006 | OS << "full-set"; |
| 1007 | else if (isEmptySet()) |
| 1008 | OS << "empty-set"; |
| 1009 | else |
| 1010 | OS << "[" << Lower << "," << Upper << ")"; |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 1013 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Dan Gohman | dc33ae2 | 2009-07-09 23:16:10 +0000 | [diff] [blame] | 1014 | /// dump - Allow printing from a debugger easily... |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 1015 | /// |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 1016 | LLVM_DUMP_METHOD void ConstantRange::dump() const { |
David Greene | de7b353 | 2010-01-05 01:28:32 +0000 | [diff] [blame] | 1017 | print(dbgs()); |
Dan Gohman | 5035fbf | 2009-07-09 22:07:27 +0000 | [diff] [blame] | 1018 | } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 1019 | #endif |
Peter Collingbourne | ecdd58f | 2016-10-21 19:59:26 +0000 | [diff] [blame] | 1020 | |
| 1021 | ConstantRange llvm::getConstantRangeFromMetadata(const MDNode &Ranges) { |
| 1022 | const unsigned NumRanges = Ranges.getNumOperands() / 2; |
| 1023 | assert(NumRanges >= 1 && "Must have at least one range!"); |
| 1024 | assert(Ranges.getNumOperands() % 2 == 0 && "Must be a sequence of pairs"); |
| 1025 | |
| 1026 | auto *FirstLow = mdconst::extract<ConstantInt>(Ranges.getOperand(0)); |
| 1027 | auto *FirstHigh = mdconst::extract<ConstantInt>(Ranges.getOperand(1)); |
| 1028 | |
| 1029 | ConstantRange CR(FirstLow->getValue(), FirstHigh->getValue()); |
| 1030 | |
| 1031 | for (unsigned i = 1; i < NumRanges; ++i) { |
| 1032 | auto *Low = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0)); |
| 1033 | auto *High = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1)); |
| 1034 | |
| 1035 | // Note: unionWith will potentially create a range that contains values not |
| 1036 | // contained in any of the original N ranges. |
| 1037 | CR = CR.unionWith(ConstantRange(Low->getValue(), High->getValue())); |
| 1038 | } |
| 1039 | |
| 1040 | return CR; |
| 1041 | } |