1 //===- ConstantRange.cpp - ConstantRange implementation -------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Represent a range of possible values that may occur when the program is run 10 // for an integral value. This keeps track of a lower and upper bound for the 11 // constant, which MAY wrap around the end of the numeric range. To do this, it 12 // keeps track of a [lower, upper) bound, which specifies an interval just like 13 // STL iterators. When used with boolean values, the following are important 14 // ranges (other integral ranges use min/max values for special range values): 15 // 16 // [F, F) = {} = Empty set 17 // [T, F) = {T} 18 // [F, T) = {F} 19 // [T, T) = {F, T} = Full set 20 // 21 //===----------------------------------------------------------------------===// 22 23 #include "llvm/ADT/APInt.h" 24 #include "llvm/Config/llvm-config.h" 25 #include "llvm/IR/ConstantRange.h" 26 #include "llvm/IR/Constants.h" 27 #include "llvm/IR/InstrTypes.h" 28 #include "llvm/IR/Instruction.h" 29 #include "llvm/IR/Metadata.h" 30 #include "llvm/IR/Operator.h" 31 #include "llvm/Support/Compiler.h" 32 #include "llvm/Support/Debug.h" 33 #include "llvm/Support/ErrorHandling.h" 34 #include "llvm/Support/KnownBits.h" 35 #include "llvm/Support/raw_ostream.h" 36 #include <algorithm> 37 #include <cassert> 38 #include <cstdint> 39 40 using namespace llvm; 41 42 ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) 43 : Lower(Full ? APInt::getMaxValue(BitWidth) : APInt::getMinValue(BitWidth)), 44 Upper(Lower) {} 45 46 ConstantRange::ConstantRange(APInt V) 47 : Lower(std::move(V)), Upper(Lower + 1) {} 48 49 ConstantRange::ConstantRange(APInt L, APInt U) 50 : Lower(std::move(L)), Upper(std::move(U)) { 51 assert(Lower.getBitWidth() == Upper.getBitWidth() && 52 "ConstantRange with unequal bit widths"); 53 assert((Lower != Upper || (Lower.isMaxValue() || Lower.isMinValue())) && 54 "Lower == Upper, but they aren't min or max value!"); 55 } 56 57 ConstantRange ConstantRange::fromKnownBits(const KnownBits &Known, 58 bool IsSigned) { 59 assert(!Known.hasConflict() && "Expected valid KnownBits"); 60 61 if (Known.isUnknown()) 62 return getFull(Known.getBitWidth()); 63 64 // For unsigned ranges, or signed ranges with known sign bit, create a simple 65 // range between the smallest and largest possible value. 66 if (!IsSigned || Known.isNegative() || Known.isNonNegative()) 67 return ConstantRange(Known.One, ~Known.Zero + 1); 68 69 // If we don't know the sign bit, pick the lower bound as a negative number 70 // and the upper bound as a non-negative one. 71 APInt Lower = Known.One, Upper = ~Known.Zero; 72 Lower.setSignBit(); 73 Upper.clearSignBit(); 74 return ConstantRange(Lower, Upper + 1); 75 } 76 77 ConstantRange ConstantRange::makeAllowedICmpRegion(CmpInst::Predicate Pred, 78 const ConstantRange &CR) { 79 if (CR.isEmptySet()) 80 return CR; 81 82 uint32_t W = CR.getBitWidth(); 83 switch (Pred) { 84 default: 85 llvm_unreachable("Invalid ICmp predicate to makeAllowedICmpRegion()"); 86 case CmpInst::ICMP_EQ: 87 return CR; 88 case CmpInst::ICMP_NE: 89 if (CR.isSingleElement()) 90 return ConstantRange(CR.getUpper(), CR.getLower()); 91 return getFull(W); 92 case CmpInst::ICMP_ULT: { 93 APInt UMax(CR.getUnsignedMax()); 94 if (UMax.isMinValue()) 95 return getEmpty(W); 96 return ConstantRange(APInt::getMinValue(W), std::move(UMax)); 97 } 98 case CmpInst::ICMP_SLT: { 99 APInt SMax(CR.getSignedMax()); 100 if (SMax.isMinSignedValue()) 101 return getEmpty(W); 102 return ConstantRange(APInt::getSignedMinValue(W), std::move(SMax)); 103 } 104 case CmpInst::ICMP_ULE: { 105 APInt UMax(CR.getUnsignedMax()); 106 if (UMax.isMaxValue()) 107 return getFull(W); 108 return ConstantRange(APInt::getMinValue(W), std::move(UMax) + 1); 109 } 110 case CmpInst::ICMP_SLE: { 111 APInt SMax(CR.getSignedMax()); 112 if (SMax.isMaxSignedValue()) 113 return getFull(W); 114 return ConstantRange(APInt::getSignedMinValue(W), std::move(SMax) + 1); 115 } 116 case CmpInst::ICMP_UGT: { 117 APInt UMin(CR.getUnsignedMin()); 118 if (UMin.isMaxValue()) 119 return getEmpty(W); 120 return ConstantRange(std::move(UMin) + 1, APInt::getNullValue(W)); 121 } 122 case CmpInst::ICMP_SGT: { 123 APInt SMin(CR.getSignedMin()); 124 if (SMin.isMaxSignedValue()) 125 return getEmpty(W); 126 return ConstantRange(std::move(SMin) + 1, APInt::getSignedMinValue(W)); 127 } 128 case CmpInst::ICMP_UGE: { 129 APInt UMin(CR.getUnsignedMin()); 130 if (UMin.isMinValue()) 131 return getFull(W); 132 return ConstantRange(std::move(UMin), APInt::getNullValue(W)); 133 } 134 case CmpInst::ICMP_SGE: { 135 APInt SMin(CR.getSignedMin()); 136 if (SMin.isMinSignedValue()) 137 return getFull(W); 138 return ConstantRange(std::move(SMin), APInt::getSignedMinValue(W)); 139 } 140 } 141 } 142 143 ConstantRange ConstantRange::makeSatisfyingICmpRegion(CmpInst::Predicate Pred, 144 const ConstantRange &CR) { 145 // Follows from De-Morgan's laws: 146 // 147 // ~(~A union ~B) == A intersect B. 148 // 149 return makeAllowedICmpRegion(CmpInst::getInversePredicate(Pred), CR) 150 .inverse(); 151 } 152 153 ConstantRange ConstantRange::makeExactICmpRegion(CmpInst::Predicate Pred, 154 const APInt &C) { 155 // Computes the exact range that is equal to both the constant ranges returned 156 // by makeAllowedICmpRegion and makeSatisfyingICmpRegion. This is always true 157 // when RHS is a singleton such as an APInt and so the assert is valid. 158 // However for non-singleton RHS, for example ult [2,5) makeAllowedICmpRegion 159 // returns [0,4) but makeSatisfyICmpRegion returns [0,2). 160 // 161 assert(makeAllowedICmpRegion(Pred, C) == makeSatisfyingICmpRegion(Pred, C)); 162 return makeAllowedICmpRegion(Pred, C); 163 } 164 165 bool ConstantRange::getEquivalentICmp(CmpInst::Predicate &Pred, 166 APInt &RHS) const { 167 bool Success = false; 168 169 if (isFullSet() || isEmptySet()) { 170 Pred = isEmptySet() ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE; 171 RHS = APInt(getBitWidth(), 0); 172 Success = true; 173 } else if (auto *OnlyElt = getSingleElement()) { 174 Pred = CmpInst::ICMP_EQ; 175 RHS = *OnlyElt; 176 Success = true; 177 } else if (auto *OnlyMissingElt = getSingleMissingElement()) { 178 Pred = CmpInst::ICMP_NE; 179 RHS = *OnlyMissingElt; 180 Success = true; 181 } else if (getLower().isMinSignedValue() || getLower().isMinValue()) { 182 Pred = 183 getLower().isMinSignedValue() ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT; 184 RHS = getUpper(); 185 Success = true; 186 } else if (getUpper().isMinSignedValue() || getUpper().isMinValue()) { 187 Pred = 188 getUpper().isMinSignedValue() ? CmpInst::ICMP_SGE : CmpInst::ICMP_UGE; 189 RHS = getLower(); 190 Success = true; 191 } 192 193 assert((!Success || ConstantRange::makeExactICmpRegion(Pred, RHS) == *this) && 194 "Bad result!"); 195 196 return Success; 197 } 198 199 ConstantRange 200 ConstantRange::makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp, 201 const ConstantRange &Other, 202 unsigned NoWrapKind) { 203 using OBO = OverflowingBinaryOperator; 204 205 // Computes the intersection of CR0 and CR1. It is different from 206 // intersectWith in that the ConstantRange returned will only contain elements 207 // in both CR0 and CR1 (i.e. SubsetIntersect(X, Y) is a *subset*, proper or 208 // not, of both X and Y). 209 auto SubsetIntersect = 210 [](const ConstantRange &CR0, const ConstantRange &CR1) { 211 return CR0.inverse().unionWith(CR1.inverse()).inverse(); 212 }; 213 214 assert(Instruction::isBinaryOp(BinOp) && "Binary operators only!"); 215 216 assert((NoWrapKind == OBO::NoSignedWrap || 217 NoWrapKind == OBO::NoUnsignedWrap || 218 NoWrapKind == (OBO::NoUnsignedWrap | OBO::NoSignedWrap)) && 219 "NoWrapKind invalid!"); 220 221 unsigned BitWidth = Other.getBitWidth(); 222 ConstantRange Result(BitWidth, /* full */ true); 223 224 switch (BinOp) { 225 default: 226 // Conservative answer: empty set 227 return getEmpty(BitWidth); 228 229 case Instruction::Add: 230 if (auto *C = Other.getSingleElement()) 231 if (C->isNullValue()) 232 // Full set: nothing signed / unsigned wraps when added to 0. 233 return getFull(BitWidth); 234 if (NoWrapKind & OBO::NoUnsignedWrap) 235 Result = 236 SubsetIntersect(Result, ConstantRange(APInt::getNullValue(BitWidth), 237 -Other.getUnsignedMax())); 238 if (NoWrapKind & OBO::NoSignedWrap) { 239 const APInt &SignedMin = Other.getSignedMin(); 240 const APInt &SignedMax = Other.getSignedMax(); 241 if (SignedMax.isStrictlyPositive()) 242 Result = SubsetIntersect( 243 Result, 244 ConstantRange(APInt::getSignedMinValue(BitWidth), 245 APInt::getSignedMinValue(BitWidth) - SignedMax)); 246 if (SignedMin.isNegative()) 247 Result = SubsetIntersect( 248 Result, 249 ConstantRange(APInt::getSignedMinValue(BitWidth) - SignedMin, 250 APInt::getSignedMinValue(BitWidth))); 251 } 252 return Result; 253 254 case Instruction::Sub: 255 if (auto *C = Other.getSingleElement()) 256 if (C->isNullValue()) 257 // Full set: nothing signed / unsigned wraps when subtracting 0. 258 return getFull(BitWidth); 259 if (NoWrapKind & OBO::NoUnsignedWrap) 260 Result = 261 SubsetIntersect(Result, ConstantRange(Other.getUnsignedMax(), 262 APInt::getMinValue(BitWidth))); 263 if (NoWrapKind & OBO::NoSignedWrap) { 264 const APInt &SignedMin = Other.getSignedMin(); 265 const APInt &SignedMax = Other.getSignedMax(); 266 if (SignedMax.isStrictlyPositive()) 267 Result = SubsetIntersect( 268 Result, 269 ConstantRange(APInt::getSignedMinValue(BitWidth) + SignedMax, 270 APInt::getSignedMinValue(BitWidth))); 271 if (SignedMin.isNegative()) 272 Result = SubsetIntersect( 273 Result, 274 ConstantRange(APInt::getSignedMinValue(BitWidth), 275 APInt::getSignedMinValue(BitWidth) + SignedMin)); 276 } 277 return Result; 278 case Instruction::Mul: { 279 if (NoWrapKind == (OBO::NoSignedWrap | OBO::NoUnsignedWrap)) { 280 return SubsetIntersect( 281 makeGuaranteedNoWrapRegion(BinOp, Other, OBO::NoSignedWrap), 282 makeGuaranteedNoWrapRegion(BinOp, Other, OBO::NoUnsignedWrap)); 283 } 284 285 // Equivalent to calling makeGuaranteedNoWrapRegion() on [V, V+1). 286 const bool Unsigned = NoWrapKind == OBO::NoUnsignedWrap; 287 const auto makeSingleValueRegion = [Unsigned, 288 BitWidth](APInt V) -> ConstantRange { 289 // Handle special case for 0, -1 and 1. See the last for reason why we 290 // specialize -1 and 1. 291 if (V == 0 || V.isOneValue()) 292 return getFull(BitWidth); 293 294 APInt MinValue, MaxValue; 295 if (Unsigned) { 296 MinValue = APInt::getMinValue(BitWidth); 297 MaxValue = APInt::getMaxValue(BitWidth); 298 } else { 299 MinValue = APInt::getSignedMinValue(BitWidth); 300 MaxValue = APInt::getSignedMaxValue(BitWidth); 301 } 302 // e.g. Returning [-127, 127], represented as [-127, -128). 303 if (!Unsigned && V.isAllOnesValue()) 304 return ConstantRange(-MaxValue, MinValue); 305 306 APInt Lower, Upper; 307 if (!Unsigned && V.isNegative()) { 308 Lower = APIntOps::RoundingSDiv(MaxValue, V, APInt::Rounding::UP); 309 Upper = APIntOps::RoundingSDiv(MinValue, V, APInt::Rounding::DOWN); 310 } else if (Unsigned) { 311 Lower = APIntOps::RoundingUDiv(MinValue, V, APInt::Rounding::UP); 312 Upper = APIntOps::RoundingUDiv(MaxValue, V, APInt::Rounding::DOWN); 313 } else { 314 Lower = APIntOps::RoundingSDiv(MinValue, V, APInt::Rounding::UP); 315 Upper = APIntOps::RoundingSDiv(MaxValue, V, APInt::Rounding::DOWN); 316 } 317 if (Unsigned) { 318 Lower = Lower.zextOrSelf(BitWidth); 319 Upper = Upper.zextOrSelf(BitWidth); 320 } else { 321 Lower = Lower.sextOrSelf(BitWidth); 322 Upper = Upper.sextOrSelf(BitWidth); 323 } 324 // ConstantRange ctor take a half inclusive interval [Lower, Upper + 1). 325 // Upper + 1 is guanranteed not to overflow, because |divisor| > 1. 0, -1, 326 // and 1 are already handled as special cases. 327 return ConstantRange(Lower, Upper + 1); 328 }; 329 330 if (Unsigned) 331 return makeSingleValueRegion(Other.getUnsignedMax()); 332 333 return SubsetIntersect(makeSingleValueRegion(Other.getSignedMin()), 334 makeSingleValueRegion(Other.getSignedMax())); 335 } 336 } 337 } 338 339 bool ConstantRange::isFullSet() const { 340 return Lower == Upper && Lower.isMaxValue(); 341 } 342 343 bool ConstantRange::isEmptySet() const { 344 return Lower == Upper && Lower.isMinValue(); 345 } 346 347 bool ConstantRange::isWrappedSet() const { 348 return Lower.ugt(Upper); 349 } 350 351 bool ConstantRange::isSignWrappedSet() const { 352 return contains(APInt::getSignedMaxValue(getBitWidth())) && 353 contains(APInt::getSignedMinValue(getBitWidth())); 354 } 355 356 APInt ConstantRange::getSetSize() const { 357 if (isFullSet()) 358 return APInt::getOneBitSet(getBitWidth()+1, getBitWidth()); 359 360 // This is also correct for wrapped sets. 361 return (Upper - Lower).zext(getBitWidth()+1); 362 } 363 364 bool 365 ConstantRange::isSizeStrictlySmallerThan(const ConstantRange &Other) const { 366 assert(getBitWidth() == Other.getBitWidth()); 367 if (isFullSet()) 368 return false; 369 if (Other.isFullSet()) 370 return true; 371 return (Upper - Lower).ult(Other.Upper - Other.Lower); 372 } 373 374 bool 375 ConstantRange::isSizeLargerThan(uint64_t MaxSize) const { 376 assert(MaxSize && "MaxSize can't be 0."); 377 // If this a full set, we need special handling to avoid needing an extra bit 378 // to represent the size. 379 if (isFullSet()) 380 return APInt::getMaxValue(getBitWidth()).ugt(MaxSize - 1); 381 382 return (Upper - Lower).ugt(MaxSize); 383 } 384 385 APInt ConstantRange::getUnsignedMax() const { 386 if (isFullSet() || isWrappedSet()) 387 return APInt::getMaxValue(getBitWidth()); 388 return getUpper() - 1; 389 } 390 391 APInt ConstantRange::getUnsignedMin() const { 392 if (isFullSet() || (isWrappedSet() && !getUpper().isNullValue())) 393 return APInt::getMinValue(getBitWidth()); 394 return getLower(); 395 } 396 397 APInt ConstantRange::getSignedMax() const { 398 if (isFullSet() || Lower.sgt(Upper)) 399 return APInt::getSignedMaxValue(getBitWidth()); 400 return getUpper() - 1; 401 } 402 403 APInt ConstantRange::getSignedMin() const { 404 if (isFullSet() || (Lower.sgt(Upper) && !getUpper().isMinSignedValue())) 405 return APInt::getSignedMinValue(getBitWidth()); 406 return getLower(); 407 } 408 409 bool ConstantRange::contains(const APInt &V) const { 410 if (Lower == Upper) 411 return isFullSet(); 412 413 if (!isWrappedSet()) 414 return Lower.ule(V) && V.ult(Upper); 415 return Lower.ule(V) || V.ult(Upper); 416 } 417 418 bool ConstantRange::contains(const ConstantRange &Other) const { 419 if (isFullSet() || Other.isEmptySet()) return true; 420 if (isEmptySet() || Other.isFullSet()) return false; 421 422 if (!isWrappedSet()) { 423 if (Other.isWrappedSet()) 424 return false; 425 426 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper); 427 } 428 429 if (!Other.isWrappedSet()) 430 return Other.getUpper().ule(Upper) || 431 Lower.ule(Other.getLower()); 432 433 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower()); 434 } 435 436 ConstantRange ConstantRange::subtract(const APInt &Val) const { 437 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width"); 438 // If the set is empty or full, don't modify the endpoints. 439 if (Lower == Upper) 440 return *this; 441 return ConstantRange(Lower - Val, Upper - Val); 442 } 443 444 ConstantRange ConstantRange::difference(const ConstantRange &CR) const { 445 return intersectWith(CR.inverse()); 446 } 447 448 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const { 449 assert(getBitWidth() == CR.getBitWidth() && 450 "ConstantRange types don't agree!"); 451 452 // Handle common cases. 453 if ( isEmptySet() || CR.isFullSet()) return *this; 454 if (CR.isEmptySet() || isFullSet()) return CR; 455 456 if (!isWrappedSet() && CR.isWrappedSet()) 457 return CR.intersectWith(*this); 458 459 if (!isWrappedSet() && !CR.isWrappedSet()) { 460 if (Lower.ult(CR.Lower)) { 461 if (Upper.ule(CR.Lower)) 462 return getEmpty(); 463 464 if (Upper.ult(CR.Upper)) 465 return ConstantRange(CR.Lower, Upper); 466 467 return CR; 468 } 469 if (Upper.ult(CR.Upper)) 470 return *this; 471 472 if (Lower.ult(CR.Upper)) 473 return ConstantRange(Lower, CR.Upper); 474 475 return getEmpty(); 476 } 477 478 if (isWrappedSet() && !CR.isWrappedSet()) { 479 if (CR.Lower.ult(Upper)) { 480 if (CR.Upper.ult(Upper)) 481 return CR; 482 483 if (CR.Upper.ule(Lower)) 484 return ConstantRange(CR.Lower, Upper); 485 486 if (isSizeStrictlySmallerThan(CR)) 487 return *this; 488 return CR; 489 } 490 if (CR.Lower.ult(Lower)) { 491 if (CR.Upper.ule(Lower)) 492 return getEmpty(); 493 494 return ConstantRange(Lower, CR.Upper); 495 } 496 return CR; 497 } 498 499 if (CR.Upper.ult(Upper)) { 500 if (CR.Lower.ult(Upper)) { 501 if (isSizeStrictlySmallerThan(CR)) 502 return *this; 503 return CR; 504 } 505 506 if (CR.Lower.ult(Lower)) 507 return ConstantRange(Lower, CR.Upper); 508 509 return CR; 510 } 511 if (CR.Upper.ule(Lower)) { 512 if (CR.Lower.ult(Lower)) 513 return *this; 514 515 return ConstantRange(CR.Lower, Upper); 516 } 517 if (isSizeStrictlySmallerThan(CR)) 518 return *this; 519 return CR; 520 } 521 522 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const { 523 assert(getBitWidth() == CR.getBitWidth() && 524 "ConstantRange types don't agree!"); 525 526 if ( isFullSet() || CR.isEmptySet()) return *this; 527 if (CR.isFullSet() || isEmptySet()) return CR; 528 529 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this); 530 531 if (!isWrappedSet() && !CR.isWrappedSet()) { 532 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) { 533 // If the two ranges are disjoint, find the smaller gap and bridge it. 534 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper; 535 if (d1.ult(d2)) 536 return ConstantRange(Lower, CR.Upper); 537 return ConstantRange(CR.Lower, Upper); 538 } 539 540 APInt L = CR.Lower.ult(Lower) ? CR.Lower : Lower; 541 APInt U = (CR.Upper - 1).ugt(Upper - 1) ? CR.Upper : Upper; 542 543 if (L.isNullValue() && U.isNullValue()) 544 return getFull(); 545 546 return ConstantRange(std::move(L), std::move(U)); 547 } 548 549 if (!CR.isWrappedSet()) { 550 // ------U L----- and ------U L----- : this 551 // L--U L--U : CR 552 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower)) 553 return *this; 554 555 // ------U L----- : this 556 // L---------U : CR 557 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) 558 return getFull(); 559 560 // ----U L---- : this 561 // L---U : CR 562 // <d1> <d2> 563 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) { 564 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper; 565 if (d1.ult(d2)) 566 return ConstantRange(Lower, CR.Upper); 567 return ConstantRange(CR.Lower, Upper); 568 } 569 570 // ----U L----- : this 571 // L----U : CR 572 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) 573 return ConstantRange(CR.Lower, Upper); 574 575 // ------U L---- : this 576 // L-----U : CR 577 assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) && 578 "ConstantRange::unionWith missed a case with one range wrapped"); 579 return ConstantRange(Lower, CR.Upper); 580 } 581 582 // ------U L---- and ------U L---- : this 583 // -U L----------- and ------------U L : CR 584 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper)) 585 return getFull(); 586 587 APInt L = CR.Lower.ult(Lower) ? CR.Lower : Lower; 588 APInt U = CR.Upper.ugt(Upper) ? CR.Upper : Upper; 589 590 return ConstantRange(std::move(L), std::move(U)); 591 } 592 593 ConstantRange ConstantRange::castOp(Instruction::CastOps CastOp, 594 uint32_t ResultBitWidth) const { 595 switch (CastOp) { 596 default: 597 llvm_unreachable("unsupported cast type"); 598 case Instruction::Trunc: 599 return truncate(ResultBitWidth); 600 case Instruction::SExt: 601 return signExtend(ResultBitWidth); 602 case Instruction::ZExt: 603 return zeroExtend(ResultBitWidth); 604 case Instruction::BitCast: 605 return *this; 606 case Instruction::FPToUI: 607 case Instruction::FPToSI: 608 if (getBitWidth() == ResultBitWidth) 609 return *this; 610 else 611 return getFull(); 612 case Instruction::UIToFP: { 613 // TODO: use input range if available 614 auto BW = getBitWidth(); 615 APInt Min = APInt::getMinValue(BW).zextOrSelf(ResultBitWidth); 616 APInt Max = APInt::getMaxValue(BW).zextOrSelf(ResultBitWidth); 617 return ConstantRange(std::move(Min), std::move(Max)); 618 } 619 case Instruction::SIToFP: { 620 // TODO: use input range if available 621 auto BW = getBitWidth(); 622 APInt SMin = APInt::getSignedMinValue(BW).sextOrSelf(ResultBitWidth); 623 APInt SMax = APInt::getSignedMaxValue(BW).sextOrSelf(ResultBitWidth); 624 return ConstantRange(std::move(SMin), std::move(SMax)); 625 } 626 case Instruction::FPTrunc: 627 case Instruction::FPExt: 628 case Instruction::IntToPtr: 629 case Instruction::PtrToInt: 630 case Instruction::AddrSpaceCast: 631 // Conservatively return getFull set. 632 return getFull(); 633 }; 634 } 635 636 ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const { 637 if (isEmptySet()) return getEmpty(DstTySize); 638 639 unsigned SrcTySize = getBitWidth(); 640 assert(SrcTySize < DstTySize && "Not a value extension"); 641 if (isFullSet() || isWrappedSet()) { 642 // Change into [0, 1 << src bit width) 643 APInt LowerExt(DstTySize, 0); 644 if (!Upper) // special case: [X, 0) -- not really wrapping around 645 LowerExt = Lower.zext(DstTySize); 646 return ConstantRange(std::move(LowerExt), 647 APInt::getOneBitSet(DstTySize, SrcTySize)); 648 } 649 650 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize)); 651 } 652 653 ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const { 654 if (isEmptySet()) return getEmpty(DstTySize); 655 656 unsigned SrcTySize = getBitWidth(); 657 assert(SrcTySize < DstTySize && "Not a value extension"); 658 659 // special case: [X, INT_MIN) -- not really wrapping around 660 if (Upper.isMinSignedValue()) 661 return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize)); 662 663 if (isFullSet() || isSignWrappedSet()) { 664 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1), 665 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1); 666 } 667 668 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize)); 669 } 670 671 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const { 672 assert(getBitWidth() > DstTySize && "Not a value truncation"); 673 if (isEmptySet()) 674 return getEmpty(DstTySize); 675 if (isFullSet()) 676 return getFull(DstTySize); 677 678 APInt LowerDiv(Lower), UpperDiv(Upper); 679 ConstantRange Union(DstTySize, /*isFullSet=*/false); 680 681 // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue] 682 // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and 683 // then we do the union with [MaxValue, Upper) 684 if (isWrappedSet()) { 685 // If Upper is greater than or equal to MaxValue(DstTy), it covers the whole 686 // truncated range. 687 if (Upper.getActiveBits() > DstTySize || 688 Upper.countTrailingOnes() == DstTySize) 689 return getFull(DstTySize); 690 691 Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize)); 692 UpperDiv.setAllBits(); 693 694 // Union covers the MaxValue case, so return if the remaining range is just 695 // MaxValue(DstTy). 696 if (LowerDiv == UpperDiv) 697 return Union; 698 } 699 700 // Chop off the most significant bits that are past the destination bitwidth. 701 if (LowerDiv.getActiveBits() > DstTySize) { 702 // Mask to just the signficant bits and subtract from LowerDiv/UpperDiv. 703 APInt Adjust = LowerDiv & APInt::getBitsSetFrom(getBitWidth(), DstTySize); 704 LowerDiv -= Adjust; 705 UpperDiv -= Adjust; 706 } 707 708 unsigned UpperDivWidth = UpperDiv.getActiveBits(); 709 if (UpperDivWidth <= DstTySize) 710 return ConstantRange(LowerDiv.trunc(DstTySize), 711 UpperDiv.trunc(DstTySize)).unionWith(Union); 712 713 // The truncated value wraps around. Check if we can do better than fullset. 714 if (UpperDivWidth == DstTySize + 1) { 715 // Clear the MSB so that UpperDiv wraps around. 716 UpperDiv.clearBit(DstTySize); 717 if (UpperDiv.ult(LowerDiv)) 718 return ConstantRange(LowerDiv.trunc(DstTySize), 719 UpperDiv.trunc(DstTySize)).unionWith(Union); 720 } 721 722 return getFull(DstTySize); 723 } 724 725 ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const { 726 unsigned SrcTySize = getBitWidth(); 727 if (SrcTySize > DstTySize) 728 return truncate(DstTySize); 729 if (SrcTySize < DstTySize) 730 return zeroExtend(DstTySize); 731 return *this; 732 } 733 734 ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const { 735 unsigned SrcTySize = getBitWidth(); 736 if (SrcTySize > DstTySize) 737 return truncate(DstTySize); 738 if (SrcTySize < DstTySize) 739 return signExtend(DstTySize); 740 return *this; 741 } 742 743 ConstantRange ConstantRange::binaryOp(Instruction::BinaryOps BinOp, 744 const ConstantRange &Other) const { 745 assert(Instruction::isBinaryOp(BinOp) && "Binary operators only!"); 746 747 switch (BinOp) { 748 case Instruction::Add: 749 return add(Other); 750 case Instruction::Sub: 751 return sub(Other); 752 case Instruction::Mul: 753 return multiply(Other); 754 case Instruction::UDiv: 755 return udiv(Other); 756 case Instruction::Shl: 757 return shl(Other); 758 case Instruction::LShr: 759 return lshr(Other); 760 case Instruction::AShr: 761 return ashr(Other); 762 case Instruction::And: 763 return binaryAnd(Other); 764 case Instruction::Or: 765 return binaryOr(Other); 766 // Note: floating point operations applied to abstract ranges are just 767 // ideal integer operations with a lossy representation 768 case Instruction::FAdd: 769 return add(Other); 770 case Instruction::FSub: 771 return sub(Other); 772 case Instruction::FMul: 773 return multiply(Other); 774 default: 775 // Conservatively return getFull set. 776 return getFull(); 777 } 778 } 779 780 ConstantRange 781 ConstantRange::add(const ConstantRange &Other) const { 782 if (isEmptySet() || Other.isEmptySet()) 783 return getEmpty(); 784 if (isFullSet() || Other.isFullSet()) 785 return getFull(); 786 787 APInt NewLower = getLower() + Other.getLower(); 788 APInt NewUpper = getUpper() + Other.getUpper() - 1; 789 if (NewLower == NewUpper) 790 return getFull(); 791 792 ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper)); 793 if (X.isSizeStrictlySmallerThan(*this) || 794 X.isSizeStrictlySmallerThan(Other)) 795 // We've wrapped, therefore, full set. 796 return getFull(); 797 return X; 798 } 799 800 ConstantRange ConstantRange::addWithNoSignedWrap(const APInt &Other) const { 801 // Calculate the subset of this range such that "X + Other" is 802 // guaranteed not to wrap (overflow) for all X in this subset. 803 // makeGuaranteedNoWrapRegion will produce an exact NSW range since we are 804 // passing a single element range. 805 auto NSWRange = ConstantRange::makeGuaranteedNoWrapRegion(BinaryOperator::Add, 806 ConstantRange(Other), 807 OverflowingBinaryOperator::NoSignedWrap); 808 auto NSWConstrainedRange = intersectWith(NSWRange); 809 810 return NSWConstrainedRange.add(ConstantRange(Other)); 811 } 812 813 ConstantRange 814 ConstantRange::sub(const ConstantRange &Other) const { 815 if (isEmptySet() || Other.isEmptySet()) 816 return getEmpty(); 817 if (isFullSet() || Other.isFullSet()) 818 return getFull(); 819 820 APInt NewLower = getLower() - Other.getUpper() + 1; 821 APInt NewUpper = getUpper() - Other.getLower(); 822 if (NewLower == NewUpper) 823 return getFull(); 824 825 ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper)); 826 if (X.isSizeStrictlySmallerThan(*this) || 827 X.isSizeStrictlySmallerThan(Other)) 828 // We've wrapped, therefore, full set. 829 return getFull(); 830 return X; 831 } 832 833 ConstantRange 834 ConstantRange::multiply(const ConstantRange &Other) const { 835 // TODO: If either operand is a single element and the multiply is known to 836 // be non-wrapping, round the result min and max value to the appropriate 837 // multiple of that element. If wrapping is possible, at least adjust the 838 // range according to the greatest power-of-two factor of the single element. 839 840 if (isEmptySet() || Other.isEmptySet()) 841 return getEmpty(); 842 843 // Multiplication is signedness-independent. However different ranges can be 844 // obtained depending on how the input ranges are treated. These different 845 // ranges are all conservatively correct, but one might be better than the 846 // other. We calculate two ranges; one treating the inputs as unsigned 847 // and the other signed, then return the smallest of these ranges. 848 849 // Unsigned range first. 850 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2); 851 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2); 852 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2); 853 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2); 854 855 ConstantRange Result_zext = ConstantRange(this_min * Other_min, 856 this_max * Other_max + 1); 857 ConstantRange UR = Result_zext.truncate(getBitWidth()); 858 859 // If the unsigned range doesn't wrap, and isn't negative then it's a range 860 // from one positive number to another which is as good as we can generate. 861 // In this case, skip the extra work of generating signed ranges which aren't 862 // going to be better than this range. 863 if (!UR.isWrappedSet() && 864 (UR.getUpper().isNonNegative() || UR.getUpper().isMinSignedValue())) 865 return UR; 866 867 // Now the signed range. Because we could be dealing with negative numbers 868 // here, the lower bound is the smallest of the cartesian product of the 869 // lower and upper ranges; for example: 870 // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6. 871 // Similarly for the upper bound, swapping min for max. 872 873 this_min = getSignedMin().sext(getBitWidth() * 2); 874 this_max = getSignedMax().sext(getBitWidth() * 2); 875 Other_min = Other.getSignedMin().sext(getBitWidth() * 2); 876 Other_max = Other.getSignedMax().sext(getBitWidth() * 2); 877 878 auto L = {this_min * Other_min, this_min * Other_max, 879 this_max * Other_min, this_max * Other_max}; 880 auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); }; 881 ConstantRange Result_sext(std::min(L, Compare), std::max(L, Compare) + 1); 882 ConstantRange SR = Result_sext.truncate(getBitWidth()); 883 884 return UR.isSizeStrictlySmallerThan(SR) ? UR : SR; 885 } 886 887 ConstantRange 888 ConstantRange::smax(const ConstantRange &Other) const { 889 // X smax Y is: range(smax(X_smin, Y_smin), 890 // smax(X_smax, Y_smax)) 891 if (isEmptySet() || Other.isEmptySet()) 892 return getEmpty(); 893 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin()); 894 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1; 895 if (NewU == NewL) 896 return getFull(); 897 return ConstantRange(std::move(NewL), std::move(NewU)); 898 } 899 900 ConstantRange 901 ConstantRange::umax(const ConstantRange &Other) const { 902 // X umax Y is: range(umax(X_umin, Y_umin), 903 // umax(X_umax, Y_umax)) 904 if (isEmptySet() || Other.isEmptySet()) 905 return getEmpty(); 906 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin()); 907 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1; 908 if (NewU == NewL) 909 return getFull(); 910 return ConstantRange(std::move(NewL), std::move(NewU)); 911 } 912 913 ConstantRange 914 ConstantRange::smin(const ConstantRange &Other) const { 915 // X smin Y is: range(smin(X_smin, Y_smin), 916 // smin(X_smax, Y_smax)) 917 if (isEmptySet() || Other.isEmptySet()) 918 return getEmpty(); 919 APInt NewL = APIntOps::smin(getSignedMin(), Other.getSignedMin()); 920 APInt NewU = APIntOps::smin(getSignedMax(), Other.getSignedMax()) + 1; 921 if (NewU == NewL) 922 return getFull(); 923 return ConstantRange(std::move(NewL), std::move(NewU)); 924 } 925 926 ConstantRange 927 ConstantRange::umin(const ConstantRange &Other) const { 928 // X umin Y is: range(umin(X_umin, Y_umin), 929 // umin(X_umax, Y_umax)) 930 if (isEmptySet() || Other.isEmptySet()) 931 return getEmpty(); 932 APInt NewL = APIntOps::umin(getUnsignedMin(), Other.getUnsignedMin()); 933 APInt NewU = APIntOps::umin(getUnsignedMax(), Other.getUnsignedMax()) + 1; 934 if (NewU == NewL) 935 return getFull(); 936 return ConstantRange(std::move(NewL), std::move(NewU)); 937 } 938 939 ConstantRange 940 ConstantRange::udiv(const ConstantRange &RHS) const { 941 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax().isNullValue()) 942 return getEmpty(); 943 if (RHS.isFullSet()) 944 return getFull(); 945 946 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax()); 947 948 APInt RHS_umin = RHS.getUnsignedMin(); 949 if (RHS_umin.isNullValue()) { 950 // We want the lowest value in RHS excluding zero. Usually that would be 1 951 // except for a range in the form of [X, 1) in which case it would be X. 952 if (RHS.getUpper() == 1) 953 RHS_umin = RHS.getLower(); 954 else 955 RHS_umin = 1; 956 } 957 958 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1; 959 960 // If the LHS is Full and the RHS is a wrapped interval containing 1 then 961 // this could occur. 962 if (Lower == Upper) 963 return getFull(); 964 965 return ConstantRange(std::move(Lower), std::move(Upper)); 966 } 967 968 ConstantRange 969 ConstantRange::binaryAnd(const ConstantRange &Other) const { 970 if (isEmptySet() || Other.isEmptySet()) 971 return getEmpty(); 972 973 // TODO: replace this with something less conservative 974 975 APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax()); 976 if (umin.isAllOnesValue()) 977 return getFull(); 978 return ConstantRange(APInt::getNullValue(getBitWidth()), std::move(umin) + 1); 979 } 980 981 ConstantRange 982 ConstantRange::binaryOr(const ConstantRange &Other) const { 983 if (isEmptySet() || Other.isEmptySet()) 984 return getEmpty(); 985 986 // TODO: replace this with something less conservative 987 988 APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin()); 989 if (umax.isNullValue()) 990 return getFull(); 991 return ConstantRange(std::move(umax), APInt::getNullValue(getBitWidth())); 992 } 993 994 ConstantRange 995 ConstantRange::shl(const ConstantRange &Other) const { 996 if (isEmptySet() || Other.isEmptySet()) 997 return getEmpty(); 998 999 APInt max = getUnsignedMax(); 1000 APInt Other_umax = Other.getUnsignedMax(); 1001 1002 // there's overflow! 1003 if (Other_umax.uge(max.countLeadingZeros())) 1004 return getFull(); 1005 1006 // FIXME: implement the other tricky cases 1007 1008 APInt min = getUnsignedMin(); 1009 min <<= Other.getUnsignedMin(); 1010 max <<= Other_umax; 1011 1012 return ConstantRange(std::move(min), std::move(max) + 1); 1013 } 1014 1015 ConstantRange 1016 ConstantRange::lshr(const ConstantRange &Other) const { 1017 if (isEmptySet() || Other.isEmptySet()) 1018 return getEmpty(); 1019 1020 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin()) + 1; 1021 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax()); 1022 if (min == max) 1023 return getFull(); 1024 1025 return ConstantRange(std::move(min), std::move(max)); 1026 } 1027 1028 ConstantRange 1029 ConstantRange::ashr(const ConstantRange &Other) const { 1030 if (isEmptySet() || Other.isEmptySet()) 1031 return getEmpty(); 1032 1033 // May straddle zero, so handle both positive and negative cases. 1034 // 'PosMax' is the upper bound of the result of the ashr 1035 // operation, when Upper of the LHS of ashr is a non-negative. 1036 // number. Since ashr of a non-negative number will result in a 1037 // smaller number, the Upper value of LHS is shifted right with 1038 // the minimum value of 'Other' instead of the maximum value. 1039 APInt PosMax = getSignedMax().ashr(Other.getUnsignedMin()) + 1; 1040 1041 // 'PosMin' is the lower bound of the result of the ashr 1042 // operation, when Lower of the LHS is a non-negative number. 1043 // Since ashr of a non-negative number will result in a smaller 1044 // number, the Lower value of LHS is shifted right with the 1045 // maximum value of 'Other'. 1046 APInt PosMin = getSignedMin().ashr(Other.getUnsignedMax()); 1047 1048 // 'NegMax' is the upper bound of the result of the ashr 1049 // operation, when Upper of the LHS of ashr is a negative number. 1050 // Since 'ashr' of a negative number will result in a bigger 1051 // number, the Upper value of LHS is shifted right with the 1052 // maximum value of 'Other'. 1053 APInt NegMax = getSignedMax().ashr(Other.getUnsignedMax()) + 1; 1054 1055 // 'NegMin' is the lower bound of the result of the ashr 1056 // operation, when Lower of the LHS of ashr is a negative number. 1057 // Since 'ashr' of a negative number will result in a bigger 1058 // number, the Lower value of LHS is shifted right with the 1059 // minimum value of 'Other'. 1060 APInt NegMin = getSignedMin().ashr(Other.getUnsignedMin()); 1061 1062 APInt max, min; 1063 if (getSignedMin().isNonNegative()) { 1064 // Upper and Lower of LHS are non-negative. 1065 min = PosMin; 1066 max = PosMax; 1067 } else if (getSignedMax().isNegative()) { 1068 // Upper and Lower of LHS are negative. 1069 min = NegMin; 1070 max = NegMax; 1071 } else { 1072 // Upper is non-negative and Lower is negative. 1073 min = NegMin; 1074 max = PosMax; 1075 } 1076 if (min == max) 1077 return getFull(); 1078 1079 return ConstantRange(std::move(min), std::move(max)); 1080 } 1081 1082 ConstantRange ConstantRange::inverse() const { 1083 if (isFullSet()) 1084 return getEmpty(); 1085 if (isEmptySet()) 1086 return getFull(); 1087 return ConstantRange(Upper, Lower); 1088 } 1089 1090 ConstantRange::OverflowResult ConstantRange::unsignedAddMayOverflow( 1091 const ConstantRange &Other) const { 1092 if (isEmptySet() || Other.isEmptySet()) 1093 return OverflowResult::MayOverflow; 1094 1095 APInt Min = getUnsignedMin(), Max = getUnsignedMax(); 1096 APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax(); 1097 1098 // a u+ b overflows iff a u> ~b. 1099 if (Min.ugt(~OtherMin)) 1100 return OverflowResult::AlwaysOverflows; 1101 if (Max.ugt(~OtherMax)) 1102 return OverflowResult::MayOverflow; 1103 return OverflowResult::NeverOverflows; 1104 } 1105 1106 ConstantRange::OverflowResult ConstantRange::signedAddMayOverflow( 1107 const ConstantRange &Other) const { 1108 if (isEmptySet() || Other.isEmptySet()) 1109 return OverflowResult::MayOverflow; 1110 1111 APInt Min = getSignedMin(), Max = getSignedMax(); 1112 APInt OtherMin = Other.getSignedMin(), OtherMax = Other.getSignedMax(); 1113 1114 APInt SignedMin = APInt::getSignedMinValue(getBitWidth()); 1115 APInt SignedMax = APInt::getSignedMaxValue(getBitWidth()); 1116 1117 // a s+ b overflows high iff a s>=0 && b s>= 0 && a s> smax - b. 1118 // a s+ b overflows low iff a s< 0 && b s< 0 && a s< smin - b. 1119 if (Min.isNonNegative() && OtherMin.isNonNegative() && 1120 Min.sgt(SignedMax - OtherMin)) 1121 return OverflowResult::AlwaysOverflows; 1122 if (Max.isNegative() && OtherMax.isNegative() && 1123 Max.slt(SignedMin - OtherMax)) 1124 return OverflowResult::AlwaysOverflows; 1125 1126 if (Max.isNonNegative() && OtherMax.isNonNegative() && 1127 Max.sgt(SignedMax - OtherMax)) 1128 return OverflowResult::MayOverflow; 1129 if (Min.isNegative() && OtherMin.isNegative() && 1130 Min.slt(SignedMin - OtherMin)) 1131 return OverflowResult::MayOverflow; 1132 1133 return OverflowResult::NeverOverflows; 1134 } 1135 1136 ConstantRange::OverflowResult ConstantRange::unsignedSubMayOverflow( 1137 const ConstantRange &Other) const { 1138 if (isEmptySet() || Other.isEmptySet()) 1139 return OverflowResult::MayOverflow; 1140 1141 APInt Min = getUnsignedMin(), Max = getUnsignedMax(); 1142 APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax(); 1143 1144 // a u- b overflows iff a u< b. 1145 if (Max.ult(OtherMin)) 1146 return OverflowResult::AlwaysOverflows; 1147 if (Min.ult(OtherMax)) 1148 return OverflowResult::MayOverflow; 1149 return OverflowResult::NeverOverflows; 1150 } 1151 1152 ConstantRange::OverflowResult ConstantRange::signedSubMayOverflow( 1153 const ConstantRange &Other) const { 1154 if (isEmptySet() || Other.isEmptySet()) 1155 return OverflowResult::MayOverflow; 1156 1157 APInt Min = getSignedMin(), Max = getSignedMax(); 1158 APInt OtherMin = Other.getSignedMin(), OtherMax = Other.getSignedMax(); 1159 1160 APInt SignedMin = APInt::getSignedMinValue(getBitWidth()); 1161 APInt SignedMax = APInt::getSignedMaxValue(getBitWidth()); 1162 1163 // a s- b overflows high iff a s>=0 && b s< 0 && a s> smax + b. 1164 // a s- b overflows low iff a s< 0 && b s>= 0 && a s< smin + b. 1165 if (Min.isNonNegative() && OtherMax.isNegative() && 1166 Min.sgt(SignedMax + OtherMax)) 1167 return OverflowResult::AlwaysOverflows; 1168 if (Max.isNegative() && OtherMin.isNonNegative() && 1169 Max.slt(SignedMin + OtherMin)) 1170 return OverflowResult::AlwaysOverflows; 1171 1172 if (Max.isNonNegative() && OtherMin.isNegative() && 1173 Max.sgt(SignedMax + OtherMin)) 1174 return OverflowResult::MayOverflow; 1175 if (Min.isNegative() && OtherMax.isNonNegative() && 1176 Min.slt(SignedMin + OtherMax)) 1177 return OverflowResult::MayOverflow; 1178 1179 return OverflowResult::NeverOverflows; 1180 } 1181 1182 void ConstantRange::print(raw_ostream &OS) const { 1183 if (isFullSet()) 1184 OS << "full-set"; 1185 else if (isEmptySet()) 1186 OS << "empty-set"; 1187 else 1188 OS << "[" << Lower << "," << Upper << ")"; 1189 } 1190 1191 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1192 LLVM_DUMP_METHOD void ConstantRange::dump() const { 1193 print(dbgs()); 1194 } 1195 #endif 1196 1197 ConstantRange llvm::getConstantRangeFromMetadata(const MDNode &Ranges) { 1198 const unsigned NumRanges = Ranges.getNumOperands() / 2; 1199 assert(NumRanges >= 1 && "Must have at least one range!"); 1200 assert(Ranges.getNumOperands() % 2 == 0 && "Must be a sequence of pairs"); 1201 1202 auto *FirstLow = mdconst::extract<ConstantInt>(Ranges.getOperand(0)); 1203 auto *FirstHigh = mdconst::extract<ConstantInt>(Ranges.getOperand(1)); 1204 1205 ConstantRange CR(FirstLow->getValue(), FirstHigh->getValue()); 1206 1207 for (unsigned i = 1; i < NumRanges; ++i) { 1208 auto *Low = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0)); 1209 auto *High = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1)); 1210 1211 // Note: unionWith will potentially create a range that contains values not 1212 // contained in any of the original N ranges. 1213 CR = CR.unionWith(ConstantRange(Low->getValue(), High->getValue())); 1214 } 1215 1216 return CR; 1217 } 1218