ConstantRange.cpp revision 224145
1254721Semaste//===-- ConstantRange.cpp - ConstantRange implementation ------------------===// 2254721Semaste// 3254721Semaste// The LLVM Compiler Infrastructure 4254721Semaste// 5254721Semaste// This file is distributed under the University of Illinois Open Source 6254721Semaste// License. See LICENSE.TXT for details. 7254721Semaste// 8254721Semaste//===----------------------------------------------------------------------===// 9254721Semaste// 10254721Semaste// Represent a range of possible values that may occur when the program is run 11254721Semaste// for an integral value. This keeps track of a lower and upper bound for the 12254721Semaste// constant, which MAY wrap around the end of the numeric range. To do this, it 13254721Semaste// keeps track of a [lower, upper) bound, which specifies an interval just like 14254721Semaste// STL iterators. When used with boolean values, the following are important 15254721Semaste// ranges (other integral ranges use min/max values for special range values): 16254721Semaste// 17254721Semaste// [F, F) = {} = Empty set 18254721Semaste// [T, F) = {T} 19254721Semaste// [F, T) = {F} 20254721Semaste// [T, T) = {F, T} = Full set 21254721Semaste// 22254721Semaste//===----------------------------------------------------------------------===// 23254721Semaste 24254721Semaste#include "llvm/Constants.h" 25254721Semaste#include "llvm/Support/ConstantRange.h" 26254721Semaste#include "llvm/Support/Debug.h" 27254721Semaste#include "llvm/Support/raw_ostream.h" 28254721Semaste#include "llvm/Instructions.h" 29254721Semasteusing namespace llvm; 30254721Semaste 31254721Semaste/// Initialize a full (the default) or empty set for the specified type. 32254721Semaste/// 33254721SemasteConstantRange::ConstantRange(uint32_t BitWidth, bool Full) { 34254721Semaste if (Full) 35254721Semaste Lower = Upper = APInt::getMaxValue(BitWidth); 36254721Semaste else 37254721Semaste Lower = Upper = APInt::getMinValue(BitWidth); 38254721Semaste} 39254721Semaste 40254721Semaste/// Initialize a range to hold the single specified value. 41254721Semaste/// 42254721SemasteConstantRange::ConstantRange(const APInt &V) : Lower(V), Upper(V + 1) {} 43254721Semaste 44254721SemasteConstantRange::ConstantRange(const APInt &L, const APInt &U) : 45254721Semaste Lower(L), Upper(U) { 46254721Semaste assert(L.getBitWidth() == U.getBitWidth() && 47254721Semaste "ConstantRange with unequal bit widths"); 48254721Semaste assert((L != U || (L.isMaxValue() || L.isMinValue())) && 49254721Semaste "Lower == Upper, but they aren't min or max value!"); 50254721Semaste} 51254721Semaste 52254721SemasteConstantRange ConstantRange::makeICmpRegion(unsigned Pred, 53254721Semaste const ConstantRange &CR) { 54254721Semaste if (CR.isEmptySet()) 55254721Semaste return CR; 56254721Semaste 57254721Semaste uint32_t W = CR.getBitWidth(); 58254721Semaste switch (Pred) { 59254721Semaste default: assert(!"Invalid ICmp predicate to makeICmpRegion()"); 60254721Semaste case ICmpInst::ICMP_EQ: 61254721Semaste return CR; 62254721Semaste case ICmpInst::ICMP_NE: 63254721Semaste if (CR.isSingleElement()) 64254721Semaste return ConstantRange(CR.getUpper(), CR.getLower()); 65254721Semaste return ConstantRange(W); 66254721Semaste case ICmpInst::ICMP_ULT: { 67254721Semaste APInt UMax(CR.getUnsignedMax()); 68254721Semaste if (UMax.isMinValue()) 69254721Semaste return ConstantRange(W, /* empty */ false); 70254721Semaste return ConstantRange(APInt::getMinValue(W), UMax); 71254721Semaste } 72254721Semaste case ICmpInst::ICMP_SLT: { 73254721Semaste APInt SMax(CR.getSignedMax()); 74254721Semaste if (SMax.isMinSignedValue()) 75254721Semaste return ConstantRange(W, /* empty */ false); 76254721Semaste return ConstantRange(APInt::getSignedMinValue(W), SMax); 77254721Semaste } 78254721Semaste case ICmpInst::ICMP_ULE: { 79254721Semaste APInt UMax(CR.getUnsignedMax()); 80254721Semaste if (UMax.isMaxValue()) 81254721Semaste return ConstantRange(W); 82254721Semaste return ConstantRange(APInt::getMinValue(W), UMax + 1); 83254721Semaste } 84254721Semaste case ICmpInst::ICMP_SLE: { 85254721Semaste APInt SMax(CR.getSignedMax()); 86254721Semaste if (SMax.isMaxSignedValue()) 87254721Semaste return ConstantRange(W); 88254721Semaste return ConstantRange(APInt::getSignedMinValue(W), SMax + 1); 89254721Semaste } 90254721Semaste case ICmpInst::ICMP_UGT: { 91254721Semaste APInt UMin(CR.getUnsignedMin()); 92254721Semaste if (UMin.isMaxValue()) 93254721Semaste return ConstantRange(W, /* empty */ false); 94254721Semaste return ConstantRange(UMin + 1, APInt::getNullValue(W)); 95254721Semaste } 96254721Semaste case ICmpInst::ICMP_SGT: { 97254721Semaste APInt SMin(CR.getSignedMin()); 98254721Semaste if (SMin.isMaxSignedValue()) 99254721Semaste return ConstantRange(W, /* empty */ false); 100254721Semaste return ConstantRange(SMin + 1, APInt::getSignedMinValue(W)); 101254721Semaste } 102254721Semaste case ICmpInst::ICMP_UGE: { 103254721Semaste APInt UMin(CR.getUnsignedMin()); 104254721Semaste if (UMin.isMinValue()) 105254721Semaste return ConstantRange(W); 106254721Semaste return ConstantRange(UMin, APInt::getNullValue(W)); 107254721Semaste } 108254721Semaste case ICmpInst::ICMP_SGE: { 109254721Semaste APInt SMin(CR.getSignedMin()); 110254721Semaste if (SMin.isMinSignedValue()) 111254721Semaste return ConstantRange(W); 112254721Semaste return ConstantRange(SMin, APInt::getSignedMinValue(W)); 113254721Semaste } 114254721Semaste } 115254721Semaste} 116254721Semaste 117254721Semaste/// isFullSet - Return true if this set contains all of the elements possible 118254721Semaste/// for this data-type 119254721Semastebool ConstantRange::isFullSet() const { 120254721Semaste return Lower == Upper && Lower.isMaxValue(); 121254721Semaste} 122254721Semaste 123254721Semaste/// isEmptySet - Return true if this set contains no members. 124254721Semaste/// 125254721Semastebool ConstantRange::isEmptySet() const { 126254721Semaste return Lower == Upper && Lower.isMinValue(); 127254721Semaste} 128254721Semaste 129254721Semaste/// isWrappedSet - Return true if this set wraps around the top of the range, 130254721Semaste/// for example: [100, 8) 131254721Semaste/// 132254721Semastebool ConstantRange::isWrappedSet() const { 133254721Semaste return Lower.ugt(Upper); 134254721Semaste} 135254721Semaste 136254721Semaste/// isSignWrappedSet - Return true if this set wraps around the INT_MIN of 137254721Semaste/// its bitwidth, for example: i8 [120, 140). 138254721Semaste/// 139254721Semastebool ConstantRange::isSignWrappedSet() const { 140254721Semaste return contains(APInt::getSignedMaxValue(getBitWidth())) && 141254721Semaste contains(APInt::getSignedMinValue(getBitWidth())); 142254721Semaste} 143254721Semaste 144254721Semaste/// getSetSize - Return the number of elements in this set. 145254721Semaste/// 146254721SemasteAPInt ConstantRange::getSetSize() const { 147254721Semaste if (isEmptySet()) 148254721Semaste return APInt(getBitWidth(), 0); 149254721Semaste if (getBitWidth() == 1) { 150254721Semaste if (Lower != Upper) // One of T or F in the set... 151254721Semaste return APInt(2, 1); 152254721Semaste return APInt(2, 2); // Must be full set... 153254721Semaste } 154254721Semaste 155254721Semaste // Simply subtract the bounds... 156254721Semaste return Upper - Lower; 157254721Semaste} 158254721Semaste 159254721Semaste/// getUnsignedMax - Return the largest unsigned value contained in the 160254721Semaste/// ConstantRange. 161254721Semaste/// 162254721SemasteAPInt ConstantRange::getUnsignedMax() const { 163254721Semaste if (isFullSet() || isWrappedSet()) 164254721Semaste return APInt::getMaxValue(getBitWidth()); 165254721Semaste else 166254721Semaste return getUpper() - 1; 167254721Semaste} 168254721Semaste 169254721Semaste/// getUnsignedMin - Return the smallest unsigned value contained in the 170254721Semaste/// ConstantRange. 171254721Semaste/// 172254721SemasteAPInt ConstantRange::getUnsignedMin() const { 173254721Semaste if (isFullSet() || (isWrappedSet() && getUpper() != 0)) 174254721Semaste return APInt::getMinValue(getBitWidth()); 175254721Semaste else 176254721Semaste return getLower(); 177254721Semaste} 178254721Semaste 179254721Semaste/// getSignedMax - Return the largest signed value contained in the 180254721Semaste/// ConstantRange. 181254721Semaste/// 182254721SemasteAPInt ConstantRange::getSignedMax() const { 183254721Semaste APInt SignedMax(APInt::getSignedMaxValue(getBitWidth())); 184254721Semaste if (!isWrappedSet()) { 185254721Semaste if (getLower().sle(getUpper() - 1)) 186254721Semaste return getUpper() - 1; 187254721Semaste else 188254721Semaste return SignedMax; 189254721Semaste } else { 190254721Semaste if (getLower().isNegative() == getUpper().isNegative()) 191254721Semaste return SignedMax; 192254721Semaste else 193254721Semaste return getUpper() - 1; 194254721Semaste } 195254721Semaste} 196254721Semaste 197254721Semaste/// getSignedMin - Return the smallest signed value contained in the 198254721Semaste/// ConstantRange. 199/// 200APInt ConstantRange::getSignedMin() const { 201 APInt SignedMin(APInt::getSignedMinValue(getBitWidth())); 202 if (!isWrappedSet()) { 203 if (getLower().sle(getUpper() - 1)) 204 return getLower(); 205 else 206 return SignedMin; 207 } else { 208 if ((getUpper() - 1).slt(getLower())) { 209 if (getUpper() != SignedMin) 210 return SignedMin; 211 else 212 return getLower(); 213 } else { 214 return getLower(); 215 } 216 } 217} 218 219/// contains - Return true if the specified value is in the set. 220/// 221bool ConstantRange::contains(const APInt &V) const { 222 if (Lower == Upper) 223 return isFullSet(); 224 225 if (!isWrappedSet()) 226 return Lower.ule(V) && V.ult(Upper); 227 else 228 return Lower.ule(V) || V.ult(Upper); 229} 230 231/// contains - Return true if the argument is a subset of this range. 232/// Two equal sets contain each other. The empty set contained by all other 233/// sets. 234/// 235bool ConstantRange::contains(const ConstantRange &Other) const { 236 if (isFullSet() || Other.isEmptySet()) return true; 237 if (isEmptySet() || Other.isFullSet()) return false; 238 239 if (!isWrappedSet()) { 240 if (Other.isWrappedSet()) 241 return false; 242 243 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper); 244 } 245 246 if (!Other.isWrappedSet()) 247 return Other.getUpper().ule(Upper) || 248 Lower.ule(Other.getLower()); 249 250 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower()); 251} 252 253/// subtract - Subtract the specified constant from the endpoints of this 254/// constant range. 255ConstantRange ConstantRange::subtract(const APInt &Val) const { 256 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width"); 257 // If the set is empty or full, don't modify the endpoints. 258 if (Lower == Upper) 259 return *this; 260 return ConstantRange(Lower - Val, Upper - Val); 261} 262 263/// intersectWith - Return the range that results from the intersection of this 264/// range with another range. The resultant range is guaranteed to include all 265/// elements contained in both input ranges, and to have the smallest possible 266/// set size that does so. Because there may be two intersections with the 267/// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A). 268ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const { 269 assert(getBitWidth() == CR.getBitWidth() && 270 "ConstantRange types don't agree!"); 271 272 // Handle common cases. 273 if ( isEmptySet() || CR.isFullSet()) return *this; 274 if (CR.isEmptySet() || isFullSet()) return CR; 275 276 if (!isWrappedSet() && CR.isWrappedSet()) 277 return CR.intersectWith(*this); 278 279 if (!isWrappedSet() && !CR.isWrappedSet()) { 280 if (Lower.ult(CR.Lower)) { 281 if (Upper.ule(CR.Lower)) 282 return ConstantRange(getBitWidth(), false); 283 284 if (Upper.ult(CR.Upper)) 285 return ConstantRange(CR.Lower, Upper); 286 287 return CR; 288 } else { 289 if (Upper.ult(CR.Upper)) 290 return *this; 291 292 if (Lower.ult(CR.Upper)) 293 return ConstantRange(Lower, CR.Upper); 294 295 return ConstantRange(getBitWidth(), false); 296 } 297 } 298 299 if (isWrappedSet() && !CR.isWrappedSet()) { 300 if (CR.Lower.ult(Upper)) { 301 if (CR.Upper.ult(Upper)) 302 return CR; 303 304 if (CR.Upper.ult(Lower)) 305 return ConstantRange(CR.Lower, Upper); 306 307 if (getSetSize().ult(CR.getSetSize())) 308 return *this; 309 else 310 return CR; 311 } else if (CR.Lower.ult(Lower)) { 312 if (CR.Upper.ule(Lower)) 313 return ConstantRange(getBitWidth(), false); 314 315 return ConstantRange(Lower, CR.Upper); 316 } 317 return CR; 318 } 319 320 if (CR.Upper.ult(Upper)) { 321 if (CR.Lower.ult(Upper)) { 322 if (getSetSize().ult(CR.getSetSize())) 323 return *this; 324 else 325 return CR; 326 } 327 328 if (CR.Lower.ult(Lower)) 329 return ConstantRange(Lower, CR.Upper); 330 331 return CR; 332 } else if (CR.Upper.ult(Lower)) { 333 if (CR.Lower.ult(Lower)) 334 return *this; 335 336 return ConstantRange(CR.Lower, Upper); 337 } 338 if (getSetSize().ult(CR.getSetSize())) 339 return *this; 340 else 341 return CR; 342} 343 344 345/// unionWith - Return the range that results from the union of this range with 346/// another range. The resultant range is guaranteed to include the elements of 347/// both sets, but may contain more. For example, [3, 9) union [12,15) is 348/// [3, 15), which includes 9, 10, and 11, which were not included in either 349/// set before. 350/// 351ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const { 352 assert(getBitWidth() == CR.getBitWidth() && 353 "ConstantRange types don't agree!"); 354 355 if ( isFullSet() || CR.isEmptySet()) return *this; 356 if (CR.isFullSet() || isEmptySet()) return CR; 357 358 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this); 359 360 if (!isWrappedSet() && !CR.isWrappedSet()) { 361 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) { 362 // If the two ranges are disjoint, find the smaller gap and bridge it. 363 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper; 364 if (d1.ult(d2)) 365 return ConstantRange(Lower, CR.Upper); 366 else 367 return ConstantRange(CR.Lower, Upper); 368 } 369 370 APInt L = Lower, U = Upper; 371 if (CR.Lower.ult(L)) 372 L = CR.Lower; 373 if ((CR.Upper - 1).ugt(U - 1)) 374 U = CR.Upper; 375 376 if (L == 0 && U == 0) 377 return ConstantRange(getBitWidth()); 378 379 return ConstantRange(L, U); 380 } 381 382 if (!CR.isWrappedSet()) { 383 // ------U L----- and ------U L----- : this 384 // L--U L--U : CR 385 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower)) 386 return *this; 387 388 // ------U L----- : this 389 // L---------U : CR 390 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) 391 return ConstantRange(getBitWidth()); 392 393 // ----U L---- : this 394 // L---U : CR 395 // <d1> <d2> 396 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) { 397 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper; 398 if (d1.ult(d2)) 399 return ConstantRange(Lower, CR.Upper); 400 else 401 return ConstantRange(CR.Lower, Upper); 402 } 403 404 // ----U L----- : this 405 // L----U : CR 406 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) 407 return ConstantRange(CR.Lower, Upper); 408 409 // ------U L---- : this 410 // L-----U : CR 411 if (CR.Lower.ult(Upper) && CR.Upper.ult(Lower)) 412 return ConstantRange(Lower, CR.Upper); 413 } 414 415 assert(isWrappedSet() && CR.isWrappedSet() && 416 "ConstantRange::unionWith missed wrapped union unwrapped case"); 417 418 // ------U L---- and ------U L---- : this 419 // -U L----------- and ------------U L : CR 420 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper)) 421 return ConstantRange(getBitWidth()); 422 423 APInt L = Lower, U = Upper; 424 if (CR.Upper.ugt(U)) 425 U = CR.Upper; 426 if (CR.Lower.ult(L)) 427 L = CR.Lower; 428 429 return ConstantRange(L, U); 430} 431 432/// zeroExtend - Return a new range in the specified integer type, which must 433/// be strictly larger than the current type. The returned range will 434/// correspond to the possible range of values as if the source range had been 435/// zero extended. 436ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const { 437 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false); 438 439 unsigned SrcTySize = getBitWidth(); 440 assert(SrcTySize < DstTySize && "Not a value extension"); 441 if (isFullSet() || isWrappedSet()) 442 // Change into [0, 1 << src bit width) 443 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize)); 444 445 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize)); 446} 447 448/// signExtend - Return a new range in the specified integer type, which must 449/// be strictly larger than the current type. The returned range will 450/// correspond to the possible range of values as if the source range had been 451/// sign extended. 452ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const { 453 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false); 454 455 unsigned SrcTySize = getBitWidth(); 456 assert(SrcTySize < DstTySize && "Not a value extension"); 457 if (isFullSet() || isSignWrappedSet()) { 458 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1), 459 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1); 460 } 461 462 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize)); 463} 464 465/// truncate - Return a new range in the specified integer type, which must be 466/// strictly smaller than the current type. The returned range will 467/// correspond to the possible range of values as if the source range had been 468/// truncated to the specified type. 469ConstantRange ConstantRange::truncate(uint32_t DstTySize) const { 470 unsigned SrcTySize = getBitWidth(); 471 assert(SrcTySize > DstTySize && "Not a value truncation"); 472 APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize)); 473 if (isFullSet() || getSetSize().ugt(Size)) 474 return ConstantRange(DstTySize, /*isFullSet=*/true); 475 476 return ConstantRange(Lower.trunc(DstTySize), Upper.trunc(DstTySize)); 477} 478 479/// zextOrTrunc - make this range have the bit width given by \p DstTySize. The 480/// value is zero extended, truncated, or left alone to make it that width. 481ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const { 482 unsigned SrcTySize = getBitWidth(); 483 if (SrcTySize > DstTySize) 484 return truncate(DstTySize); 485 else if (SrcTySize < DstTySize) 486 return zeroExtend(DstTySize); 487 else 488 return *this; 489} 490 491/// sextOrTrunc - make this range have the bit width given by \p DstTySize. The 492/// value is sign extended, truncated, or left alone to make it that width. 493ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const { 494 unsigned SrcTySize = getBitWidth(); 495 if (SrcTySize > DstTySize) 496 return truncate(DstTySize); 497 else if (SrcTySize < DstTySize) 498 return signExtend(DstTySize); 499 else 500 return *this; 501} 502 503ConstantRange 504ConstantRange::add(const ConstantRange &Other) const { 505 if (isEmptySet() || Other.isEmptySet()) 506 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 507 if (isFullSet() || Other.isFullSet()) 508 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 509 510 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize(); 511 APInt NewLower = getLower() + Other.getLower(); 512 APInt NewUpper = getUpper() + Other.getUpper() - 1; 513 if (NewLower == NewUpper) 514 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 515 516 ConstantRange X = ConstantRange(NewLower, NewUpper); 517 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y)) 518 // We've wrapped, therefore, full set. 519 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 520 521 return X; 522} 523 524ConstantRange 525ConstantRange::sub(const ConstantRange &Other) const { 526 if (isEmptySet() || Other.isEmptySet()) 527 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 528 if (isFullSet() || Other.isFullSet()) 529 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 530 531 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize(); 532 APInt NewLower = getLower() - Other.getUpper() + 1; 533 APInt NewUpper = getUpper() - Other.getLower(); 534 if (NewLower == NewUpper) 535 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 536 537 ConstantRange X = ConstantRange(NewLower, NewUpper); 538 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y)) 539 // We've wrapped, therefore, full set. 540 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 541 542 return X; 543} 544 545ConstantRange 546ConstantRange::multiply(const ConstantRange &Other) const { 547 // TODO: If either operand is a single element and the multiply is known to 548 // be non-wrapping, round the result min and max value to the appropriate 549 // multiple of that element. If wrapping is possible, at least adjust the 550 // range according to the greatest power-of-two factor of the single element. 551 552 if (isEmptySet() || Other.isEmptySet()) 553 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 554 if (isFullSet() || Other.isFullSet()) 555 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 556 557 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2); 558 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2); 559 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2); 560 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2); 561 562 ConstantRange Result_zext = ConstantRange(this_min * Other_min, 563 this_max * Other_max + 1); 564 return Result_zext.truncate(getBitWidth()); 565} 566 567ConstantRange 568ConstantRange::smax(const ConstantRange &Other) const { 569 // X smax Y is: range(smax(X_smin, Y_smin), 570 // smax(X_smax, Y_smax)) 571 if (isEmptySet() || Other.isEmptySet()) 572 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 573 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin()); 574 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1; 575 if (NewU == NewL) 576 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 577 return ConstantRange(NewL, NewU); 578} 579 580ConstantRange 581ConstantRange::umax(const ConstantRange &Other) const { 582 // X umax Y is: range(umax(X_umin, Y_umin), 583 // umax(X_umax, Y_umax)) 584 if (isEmptySet() || Other.isEmptySet()) 585 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 586 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin()); 587 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1; 588 if (NewU == NewL) 589 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 590 return ConstantRange(NewL, NewU); 591} 592 593ConstantRange 594ConstantRange::udiv(const ConstantRange &RHS) const { 595 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0) 596 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 597 if (RHS.isFullSet()) 598 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 599 600 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax()); 601 602 APInt RHS_umin = RHS.getUnsignedMin(); 603 if (RHS_umin == 0) { 604 // We want the lowest value in RHS excluding zero. Usually that would be 1 605 // except for a range in the form of [X, 1) in which case it would be X. 606 if (RHS.getUpper() == 1) 607 RHS_umin = RHS.getLower(); 608 else 609 RHS_umin = APInt(getBitWidth(), 1); 610 } 611 612 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1; 613 614 // If the LHS is Full and the RHS is a wrapped interval containing 1 then 615 // this could occur. 616 if (Lower == Upper) 617 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 618 619 return ConstantRange(Lower, Upper); 620} 621 622ConstantRange 623ConstantRange::binaryAnd(const ConstantRange &Other) const { 624 if (isEmptySet() || Other.isEmptySet()) 625 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 626 627 // TODO: replace this with something less conservative 628 629 APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax()); 630 if (umin.isAllOnesValue()) 631 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 632 return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1); 633} 634 635ConstantRange 636ConstantRange::binaryOr(const ConstantRange &Other) const { 637 if (isEmptySet() || Other.isEmptySet()) 638 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 639 640 // TODO: replace this with something less conservative 641 642 APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin()); 643 if (umax.isMinValue()) 644 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 645 return ConstantRange(umax, APInt::getNullValue(getBitWidth())); 646} 647 648ConstantRange 649ConstantRange::shl(const ConstantRange &Other) const { 650 if (isEmptySet() || Other.isEmptySet()) 651 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 652 653 APInt min = getUnsignedMin().shl(Other.getUnsignedMin()); 654 APInt max = getUnsignedMax().shl(Other.getUnsignedMax()); 655 656 // there's no overflow! 657 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros()); 658 if (Zeros.ugt(Other.getUnsignedMax())) 659 return ConstantRange(min, max + 1); 660 661 // FIXME: implement the other tricky cases 662 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 663} 664 665ConstantRange 666ConstantRange::lshr(const ConstantRange &Other) const { 667 if (isEmptySet() || Other.isEmptySet()) 668 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 669 670 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin()); 671 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax()); 672 if (min == max + 1) 673 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 674 675 return ConstantRange(min, max + 1); 676} 677 678ConstantRange ConstantRange::inverse() const { 679 if (isFullSet()) { 680 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 681 } else if (isEmptySet()) { 682 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 683 } 684 return ConstantRange(Upper, Lower); 685} 686 687/// print - Print out the bounds to a stream... 688/// 689void ConstantRange::print(raw_ostream &OS) const { 690 if (isFullSet()) 691 OS << "full-set"; 692 else if (isEmptySet()) 693 OS << "empty-set"; 694 else 695 OS << "[" << Lower << "," << Upper << ")"; 696} 697 698/// dump - Allow printing from a debugger easily... 699/// 700void ConstantRange::dump() const { 701 print(dbgs()); 702} 703