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