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