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