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/InstrTypes.h" 25#include "llvm/Support/ConstantRange.h" 26#include "llvm/Support/Debug.h" 27#include "llvm/Support/raw_ostream.h" 28using namespace llvm; 29 30/// Initialize a full (the default) or empty set for the specified type. 31/// 32ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) { 33 if (Full) 34 Lower = Upper = APInt::getMaxValue(BitWidth); 35 else 36 Lower = Upper = APInt::getMinValue(BitWidth); 37} 38 39/// Initialize a range to hold the single specified value. 40/// 41ConstantRange::ConstantRange(const APInt &V) : Lower(V), Upper(V + 1) {} 42 43ConstantRange::ConstantRange(const APInt &L, const APInt &U) : 44 Lower(L), Upper(U) { 45 assert(L.getBitWidth() == U.getBitWidth() && 46 "ConstantRange with unequal bit widths"); 47 assert((L != U || (L.isMaxValue() || L.isMinValue())) && 48 "Lower == Upper, but they aren't min or max value!"); 49} 50 51ConstantRange ConstantRange::makeICmpRegion(unsigned Pred, 52 const ConstantRange &CR) { 53 if (CR.isEmptySet()) 54 return CR; 55 56 uint32_t W = CR.getBitWidth(); 57 switch (Pred) { 58 default: llvm_unreachable("Invalid ICmp predicate to makeICmpRegion()"); 59 case CmpInst::ICMP_EQ: 60 return CR; 61 case CmpInst::ICMP_NE: 62 if (CR.isSingleElement()) 63 return ConstantRange(CR.getUpper(), CR.getLower()); 64 return ConstantRange(W); 65 case CmpInst::ICMP_ULT: { 66 APInt UMax(CR.getUnsignedMax()); 67 if (UMax.isMinValue()) 68 return ConstantRange(W, /* empty */ false); 69 return ConstantRange(APInt::getMinValue(W), UMax); 70 } 71 case CmpInst::ICMP_SLT: { 72 APInt SMax(CR.getSignedMax()); 73 if (SMax.isMinSignedValue()) 74 return ConstantRange(W, /* empty */ false); 75 return ConstantRange(APInt::getSignedMinValue(W), SMax); 76 } 77 case CmpInst::ICMP_ULE: { 78 APInt UMax(CR.getUnsignedMax()); 79 if (UMax.isMaxValue()) 80 return ConstantRange(W); 81 return ConstantRange(APInt::getMinValue(W), UMax + 1); 82 } 83 case CmpInst::ICMP_SLE: { 84 APInt SMax(CR.getSignedMax()); 85 if (SMax.isMaxSignedValue()) 86 return ConstantRange(W); 87 return ConstantRange(APInt::getSignedMinValue(W), SMax + 1); 88 } 89 case CmpInst::ICMP_UGT: { 90 APInt UMin(CR.getUnsignedMin()); 91 if (UMin.isMaxValue()) 92 return ConstantRange(W, /* empty */ false); 93 return ConstantRange(UMin + 1, APInt::getNullValue(W)); 94 } 95 case CmpInst::ICMP_SGT: { 96 APInt SMin(CR.getSignedMin()); 97 if (SMin.isMaxSignedValue()) 98 return ConstantRange(W, /* empty */ false); 99 return ConstantRange(SMin + 1, APInt::getSignedMinValue(W)); 100 } 101 case CmpInst::ICMP_UGE: { 102 APInt UMin(CR.getUnsignedMin()); 103 if (UMin.isMinValue()) 104 return ConstantRange(W); 105 return ConstantRange(UMin, APInt::getNullValue(W)); 106 } 107 case CmpInst::ICMP_SGE: { 108 APInt SMin(CR.getSignedMin()); 109 if (SMin.isMinSignedValue()) 110 return ConstantRange(W); 111 return ConstantRange(SMin, APInt::getSignedMinValue(W)); 112 } 113 } 114} 115 116/// isFullSet - Return true if this set contains all of the elements possible 117/// for this data-type 118bool ConstantRange::isFullSet() const { 119 return Lower == Upper && Lower.isMaxValue(); 120} 121 122/// isEmptySet - Return true if this set contains no members. 123/// 124bool ConstantRange::isEmptySet() const { 125 return Lower == Upper && Lower.isMinValue(); 126} 127 128/// isWrappedSet - Return true if this set wraps around the top of the range, 129/// for example: [100, 8) 130/// 131bool ConstantRange::isWrappedSet() const { 132 return Lower.ugt(Upper); 133} 134 135/// isSignWrappedSet - Return true if this set wraps around the INT_MIN of 136/// its bitwidth, for example: i8 [120, 140). 137/// 138bool ConstantRange::isSignWrappedSet() const { 139 return contains(APInt::getSignedMaxValue(getBitWidth())) && 140 contains(APInt::getSignedMinValue(getBitWidth())); 141} 142 143/// getSetSize - Return the number of elements in this set. 144/// 145APInt ConstantRange::getSetSize() const { 146 if (isEmptySet()) 147 return APInt(getBitWidth(), 0); 148 if (getBitWidth() == 1) { 149 if (Lower != Upper) // One of T or F in the set... 150 return APInt(2, 1); 151 return APInt(2, 2); // Must be full set... 152 } 153 154 // Simply subtract the bounds... 155 return Upper - Lower; 156} 157 158/// getUnsignedMax - Return the largest unsigned value contained in the 159/// ConstantRange. 160/// 161APInt ConstantRange::getUnsignedMax() const { 162 if (isFullSet() || isWrappedSet()) 163 return APInt::getMaxValue(getBitWidth()); 164 return getUpper() - 1; 165} 166 167/// getUnsignedMin - Return the smallest unsigned value contained in the 168/// ConstantRange. 169/// 170APInt ConstantRange::getUnsignedMin() const { 171 if (isFullSet() || (isWrappedSet() && getUpper() != 0)) 172 return APInt::getMinValue(getBitWidth()); 173 return getLower(); 174} 175 176/// getSignedMax - Return the largest signed value contained in the 177/// ConstantRange. 178/// 179APInt ConstantRange::getSignedMax() const { 180 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth())); 181 if (!isWrappedSet()) { 182 if (getLower().sle(getUpper() - 1)) 183 return getUpper() - 1; 184 return SignedMax; 185 } 186 if (getLower().isNegative() == getUpper().isNegative()) 187 return SignedMax; 188 return getUpper() - 1; 189} 190 191/// getSignedMin - Return the smallest signed value contained in the 192/// ConstantRange. 193/// 194APInt ConstantRange::getSignedMin() const { 195 APInt SignedMin(APInt::getSignedMinValue(getBitWidth())); 196 if (!isWrappedSet()) { 197 if (getLower().sle(getUpper() - 1)) 198 return getLower(); 199 return SignedMin; 200 } 201 if ((getUpper() - 1).slt(getLower())) { 202 if (getUpper() != SignedMin) 203 return SignedMin; 204 } 205 return getLower(); 206} 207 208/// contains - Return true if the specified value is in the set. 209/// 210bool ConstantRange::contains(const APInt &V) const { 211 if (Lower == Upper) 212 return isFullSet(); 213 214 if (!isWrappedSet()) 215 return Lower.ule(V) && V.ult(Upper); 216 return Lower.ule(V) || V.ult(Upper); 217} 218 219/// contains - Return true if the argument is a subset of this range. 220/// Two equal sets contain each other. The empty set contained by all other 221/// sets. 222/// 223bool ConstantRange::contains(const ConstantRange &Other) const { 224 if (isFullSet() || Other.isEmptySet()) return true; 225 if (isEmptySet() || Other.isFullSet()) return false; 226 227 if (!isWrappedSet()) { 228 if (Other.isWrappedSet()) 229 return false; 230 231 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper); 232 } 233 234 if (!Other.isWrappedSet()) 235 return Other.getUpper().ule(Upper) || 236 Lower.ule(Other.getLower()); 237 238 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower()); 239} 240 241/// subtract - Subtract the specified constant from the endpoints of this 242/// constant range. 243ConstantRange ConstantRange::subtract(const APInt &Val) const { 244 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width"); 245 // If the set is empty or full, don't modify the endpoints. 246 if (Lower == Upper) 247 return *this; 248 return ConstantRange(Lower - Val, Upper - Val); 249} 250 251/// intersectWith - Return the range that results from the intersection of this 252/// range with another range. The resultant range is guaranteed to include all 253/// elements contained in both input ranges, and to have the smallest possible 254/// set size that does so. Because there may be two intersections with the 255/// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A). 256ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const { 257 assert(getBitWidth() == CR.getBitWidth() && 258 "ConstantRange types don't agree!"); 259 260 // Handle common cases. 261 if ( isEmptySet() || CR.isFullSet()) return *this; 262 if (CR.isEmptySet() || isFullSet()) return CR; 263 264 if (!isWrappedSet() && CR.isWrappedSet()) 265 return CR.intersectWith(*this); 266 267 if (!isWrappedSet() && !CR.isWrappedSet()) { 268 if (Lower.ult(CR.Lower)) { 269 if (Upper.ule(CR.Lower)) 270 return ConstantRange(getBitWidth(), false); 271 272 if (Upper.ult(CR.Upper)) 273 return ConstantRange(CR.Lower, Upper); 274 275 return CR; 276 } 277 if (Upper.ult(CR.Upper)) 278 return *this; 279 280 if (Lower.ult(CR.Upper)) 281 return ConstantRange(Lower, CR.Upper); 282 283 return ConstantRange(getBitWidth(), false); 284 } 285 286 if (isWrappedSet() && !CR.isWrappedSet()) { 287 if (CR.Lower.ult(Upper)) { 288 if (CR.Upper.ult(Upper)) 289 return CR; 290 291 if (CR.Upper.ult(Lower)) 292 return ConstantRange(CR.Lower, Upper); 293 294 if (getSetSize().ult(CR.getSetSize())) 295 return *this; 296 return CR; 297 } 298 if (CR.Lower.ult(Lower)) { 299 if (CR.Upper.ule(Lower)) 300 return ConstantRange(getBitWidth(), false); 301 302 return ConstantRange(Lower, CR.Upper); 303 } 304 return CR; 305 } 306 307 if (CR.Upper.ult(Upper)) { 308 if (CR.Lower.ult(Upper)) { 309 if (getSetSize().ult(CR.getSetSize())) 310 return *this; 311 return CR; 312 } 313 314 if (CR.Lower.ult(Lower)) 315 return ConstantRange(Lower, CR.Upper); 316 317 return CR; 318 } 319 if (CR.Upper.ult(Lower)) { 320 if (CR.Lower.ult(Lower)) 321 return *this; 322 323 return ConstantRange(CR.Lower, Upper); 324 } 325 if (getSetSize().ult(CR.getSetSize())) 326 return *this; 327 return CR; 328} 329 330 331/// unionWith - Return the range that results from the union of this range with 332/// another range. The resultant range is guaranteed to include the elements of 333/// both sets, but may contain more. For example, [3, 9) union [12,15) is 334/// [3, 15), which includes 9, 10, and 11, which were not included in either 335/// set before. 336/// 337ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const { 338 assert(getBitWidth() == CR.getBitWidth() && 339 "ConstantRange types don't agree!"); 340 341 if ( isFullSet() || CR.isEmptySet()) return *this; 342 if (CR.isFullSet() || isEmptySet()) return CR; 343 344 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this); 345 346 if (!isWrappedSet() && !CR.isWrappedSet()) { 347 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) { 348 // If the two ranges are disjoint, find the smaller gap and bridge it. 349 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper; 350 if (d1.ult(d2)) 351 return ConstantRange(Lower, CR.Upper); 352 return ConstantRange(CR.Lower, Upper); 353 } 354 355 APInt L = Lower, U = Upper; 356 if (CR.Lower.ult(L)) 357 L = CR.Lower; 358 if ((CR.Upper - 1).ugt(U - 1)) 359 U = CR.Upper; 360 361 if (L == 0 && U == 0) 362 return ConstantRange(getBitWidth()); 363 364 return ConstantRange(L, U); 365 } 366 367 if (!CR.isWrappedSet()) { 368 // ------U L----- and ------U L----- : this 369 // L--U L--U : CR 370 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower)) 371 return *this; 372 373 // ------U L----- : this 374 // L---------U : CR 375 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) 376 return ConstantRange(getBitWidth()); 377 378 // ----U L---- : this 379 // L---U : CR 380 // <d1> <d2> 381 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) { 382 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper; 383 if (d1.ult(d2)) 384 return ConstantRange(Lower, CR.Upper); 385 return ConstantRange(CR.Lower, Upper); 386 } 387 388 // ----U L----- : this 389 // L----U : CR 390 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) 391 return ConstantRange(CR.Lower, Upper); 392 393 // ------U L---- : this 394 // L-----U : CR 395 assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) && 396 "ConstantRange::unionWith missed a case with one range wrapped"); 397 return ConstantRange(Lower, CR.Upper); 398 } 399 400 // ------U L---- and ------U L---- : this 401 // -U L----------- and ------------U L : CR 402 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper)) 403 return ConstantRange(getBitWidth()); 404 405 APInt L = Lower, U = Upper; 406 if (CR.Upper.ugt(U)) 407 U = CR.Upper; 408 if (CR.Lower.ult(L)) 409 L = CR.Lower; 410 411 return ConstantRange(L, U); 412} 413 414/// zeroExtend - Return a new range in the specified integer type, which must 415/// be strictly larger than the current type. The returned range will 416/// correspond to the possible range of values as if the source range had been 417/// zero extended. 418ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const { 419 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false); 420 421 unsigned SrcTySize = getBitWidth(); 422 assert(SrcTySize < DstTySize && "Not a value extension"); 423 if (isFullSet() || isWrappedSet()) 424 // Change into [0, 1 << src bit width) 425 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize)); 426 427 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize)); 428} 429 430/// signExtend - Return a new range in the specified integer type, which must 431/// be strictly larger than the current type. The returned range will 432/// correspond to the possible range of values as if the source range had been 433/// sign extended. 434ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const { 435 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false); 436 437 unsigned SrcTySize = getBitWidth(); 438 assert(SrcTySize < DstTySize && "Not a value extension"); 439 if (isFullSet() || isSignWrappedSet()) { 440 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1), 441 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1); 442 } 443 444 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize)); 445} 446 447/// truncate - Return a new range in the specified integer type, which must be 448/// strictly smaller than the current type. The returned range will 449/// correspond to the possible range of values as if the source range had been 450/// truncated to the specified type. 451ConstantRange ConstantRange::truncate(uint32_t DstTySize) const { 452 assert(getBitWidth() > DstTySize && "Not a value truncation"); 453 if (isFullSet() || getSetSize().getActiveBits() > DstTySize) 454 return ConstantRange(DstTySize, /*isFullSet=*/true); 455 456 return ConstantRange(Lower.trunc(DstTySize), Upper.trunc(DstTySize)); 457} 458 459/// zextOrTrunc - make this range have the bit width given by \p DstTySize. The 460/// value is zero extended, truncated, or left alone to make it that width. 461ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const { 462 unsigned SrcTySize = getBitWidth(); 463 if (SrcTySize > DstTySize) 464 return truncate(DstTySize); 465 if (SrcTySize < DstTySize) 466 return zeroExtend(DstTySize); 467 return *this; 468} 469 470/// sextOrTrunc - make this range have the bit width given by \p DstTySize. The 471/// value is sign extended, truncated, or left alone to make it that width. 472ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const { 473 unsigned SrcTySize = getBitWidth(); 474 if (SrcTySize > DstTySize) 475 return truncate(DstTySize); 476 if (SrcTySize < DstTySize) 477 return signExtend(DstTySize); 478 return *this; 479} 480 481ConstantRange 482ConstantRange::add(const ConstantRange &Other) const { 483 if (isEmptySet() || Other.isEmptySet()) 484 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 485 if (isFullSet() || Other.isFullSet()) 486 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 487 488 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize(); 489 APInt NewLower = getLower() + Other.getLower(); 490 APInt NewUpper = getUpper() + Other.getUpper() - 1; 491 if (NewLower == NewUpper) 492 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 493 494 ConstantRange X = ConstantRange(NewLower, NewUpper); 495 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y)) 496 // We've wrapped, therefore, full set. 497 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 498 499 return X; 500} 501 502ConstantRange 503ConstantRange::sub(const ConstantRange &Other) const { 504 if (isEmptySet() || Other.isEmptySet()) 505 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 506 if (isFullSet() || Other.isFullSet()) 507 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 508 509 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize(); 510 APInt NewLower = getLower() - Other.getUpper() + 1; 511 APInt NewUpper = getUpper() - Other.getLower(); 512 if (NewLower == NewUpper) 513 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 514 515 ConstantRange X = ConstantRange(NewLower, NewUpper); 516 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y)) 517 // We've wrapped, therefore, full set. 518 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 519 520 return X; 521} 522 523ConstantRange 524ConstantRange::multiply(const ConstantRange &Other) const { 525 // TODO: If either operand is a single element and the multiply is known to 526 // be non-wrapping, round the result min and max value to the appropriate 527 // multiple of that element. If wrapping is possible, at least adjust the 528 // range according to the greatest power-of-two factor of the single element. 529 530 if (isEmptySet() || Other.isEmptySet()) 531 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 532 if (isFullSet() || Other.isFullSet()) 533 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 534 535 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2); 536 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2); 537 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2); 538 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2); 539 540 ConstantRange Result_zext = ConstantRange(this_min * Other_min, 541 this_max * Other_max + 1); 542 return Result_zext.truncate(getBitWidth()); 543} 544 545ConstantRange 546ConstantRange::smax(const ConstantRange &Other) const { 547 // X smax Y is: range(smax(X_smin, Y_smin), 548 // smax(X_smax, Y_smax)) 549 if (isEmptySet() || Other.isEmptySet()) 550 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 551 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin()); 552 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1; 553 if (NewU == NewL) 554 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 555 return ConstantRange(NewL, NewU); 556} 557 558ConstantRange 559ConstantRange::umax(const ConstantRange &Other) const { 560 // X umax Y is: range(umax(X_umin, Y_umin), 561 // umax(X_umax, Y_umax)) 562 if (isEmptySet() || Other.isEmptySet()) 563 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 564 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin()); 565 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1; 566 if (NewU == NewL) 567 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 568 return ConstantRange(NewL, NewU); 569} 570 571ConstantRange 572ConstantRange::udiv(const ConstantRange &RHS) const { 573 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0) 574 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 575 if (RHS.isFullSet()) 576 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 577 578 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax()); 579 580 APInt RHS_umin = RHS.getUnsignedMin(); 581 if (RHS_umin == 0) { 582 // We want the lowest value in RHS excluding zero. Usually that would be 1 583 // except for a range in the form of [X, 1) in which case it would be X. 584 if (RHS.getUpper() == 1) 585 RHS_umin = RHS.getLower(); 586 else 587 RHS_umin = APInt(getBitWidth(), 1); 588 } 589 590 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1; 591 592 // If the LHS is Full and the RHS is a wrapped interval containing 1 then 593 // this could occur. 594 if (Lower == Upper) 595 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 596 597 return ConstantRange(Lower, Upper); 598} 599 600ConstantRange 601ConstantRange::binaryAnd(const ConstantRange &Other) const { 602 if (isEmptySet() || Other.isEmptySet()) 603 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 604 605 // TODO: replace this with something less conservative 606 607 APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax()); 608 if (umin.isAllOnesValue()) 609 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 610 return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1); 611} 612 613ConstantRange 614ConstantRange::binaryOr(const ConstantRange &Other) const { 615 if (isEmptySet() || Other.isEmptySet()) 616 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 617 618 // TODO: replace this with something less conservative 619 620 APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin()); 621 if (umax.isMinValue()) 622 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 623 return ConstantRange(umax, APInt::getNullValue(getBitWidth())); 624} 625 626ConstantRange 627ConstantRange::shl(const ConstantRange &Other) const { 628 if (isEmptySet() || Other.isEmptySet()) 629 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 630 631 APInt min = getUnsignedMin().shl(Other.getUnsignedMin()); 632 APInt max = getUnsignedMax().shl(Other.getUnsignedMax()); 633 634 // there's no overflow! 635 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros()); 636 if (Zeros.ugt(Other.getUnsignedMax())) 637 return ConstantRange(min, max + 1); 638 639 // FIXME: implement the other tricky cases 640 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 641} 642 643ConstantRange 644ConstantRange::lshr(const ConstantRange &Other) const { 645 if (isEmptySet() || Other.isEmptySet()) 646 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 647 648 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin()); 649 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax()); 650 if (min == max + 1) 651 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 652 653 return ConstantRange(min, max + 1); 654} 655 656ConstantRange ConstantRange::inverse() const { 657 if (isFullSet()) 658 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 659 if (isEmptySet()) 660 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 661 return ConstantRange(Upper, Lower); 662} 663 664/// print - Print out the bounds to a stream... 665/// 666void ConstantRange::print(raw_ostream &OS) const { 667 if (isFullSet()) 668 OS << "full-set"; 669 else if (isEmptySet()) 670 OS << "empty-set"; 671 else 672 OS << "[" << Lower << "," << Upper << ")"; 673} 674 675/// dump - Allow printing from a debugger easily... 676/// 677void ConstantRange::dump() const { 678 print(dbgs()); 679} 680