LLVM API Documentation
00001 //===-- ConstantRange.cpp - ConstantRange implementation ------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Represent a range of possible values that may occur when the program is run 00011 // for an integral value. This keeps track of a lower and upper bound for the 00012 // constant, which MAY wrap around the end of the numeric range. To do this, it 00013 // keeps track of a [lower, upper) bound, which specifies an interval just like 00014 // STL iterators. When used with boolean values, the following are important 00015 // ranges (other integral ranges use min/max values for special range values): 00016 // 00017 // [F, F) = {} = Empty set 00018 // [T, F) = {T} 00019 // [F, T) = {F} 00020 // [T, T) = {F, T} = Full set 00021 // 00022 //===----------------------------------------------------------------------===// 00023 00024 #include "llvm/IR/InstrTypes.h" 00025 #include "llvm/Support/ConstantRange.h" 00026 #include "llvm/Support/Debug.h" 00027 #include "llvm/Support/raw_ostream.h" 00028 using namespace llvm; 00029 00030 /// Initialize a full (the default) or empty set for the specified type. 00031 /// 00032 ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) { 00033 if (Full) 00034 Lower = Upper = APInt::getMaxValue(BitWidth); 00035 else 00036 Lower = Upper = APInt::getMinValue(BitWidth); 00037 } 00038 00039 /// Initialize a range to hold the single specified value. 00040 /// 00041 ConstantRange::ConstantRange(const APInt &V) : Lower(V), Upper(V + 1) {} 00042 00043 ConstantRange::ConstantRange(const APInt &L, const APInt &U) : 00044 Lower(L), Upper(U) { 00045 assert(L.getBitWidth() == U.getBitWidth() && 00046 "ConstantRange with unequal bit widths"); 00047 assert((L != U || (L.isMaxValue() || L.isMinValue())) && 00048 "Lower == Upper, but they aren't min or max value!"); 00049 } 00050 00051 ConstantRange ConstantRange::makeICmpRegion(unsigned Pred, 00052 const ConstantRange &CR) { 00053 if (CR.isEmptySet()) 00054 return CR; 00055 00056 uint32_t W = CR.getBitWidth(); 00057 switch (Pred) { 00058 default: llvm_unreachable("Invalid ICmp predicate to makeICmpRegion()"); 00059 case CmpInst::ICMP_EQ: 00060 return CR; 00061 case CmpInst::ICMP_NE: 00062 if (CR.isSingleElement()) 00063 return ConstantRange(CR.getUpper(), CR.getLower()); 00064 return ConstantRange(W); 00065 case CmpInst::ICMP_ULT: { 00066 APInt UMax(CR.getUnsignedMax()); 00067 if (UMax.isMinValue()) 00068 return ConstantRange(W, /* empty */ false); 00069 return ConstantRange(APInt::getMinValue(W), UMax); 00070 } 00071 case CmpInst::ICMP_SLT: { 00072 APInt SMax(CR.getSignedMax()); 00073 if (SMax.isMinSignedValue()) 00074 return ConstantRange(W, /* empty */ false); 00075 return ConstantRange(APInt::getSignedMinValue(W), SMax); 00076 } 00077 case CmpInst::ICMP_ULE: { 00078 APInt UMax(CR.getUnsignedMax()); 00079 if (UMax.isMaxValue()) 00080 return ConstantRange(W); 00081 return ConstantRange(APInt::getMinValue(W), UMax + 1); 00082 } 00083 case CmpInst::ICMP_SLE: { 00084 APInt SMax(CR.getSignedMax()); 00085 if (SMax.isMaxSignedValue()) 00086 return ConstantRange(W); 00087 return ConstantRange(APInt::getSignedMinValue(W), SMax + 1); 00088 } 00089 case CmpInst::ICMP_UGT: { 00090 APInt UMin(CR.getUnsignedMin()); 00091 if (UMin.isMaxValue()) 00092 return ConstantRange(W, /* empty */ false); 00093 return ConstantRange(UMin + 1, APInt::getNullValue(W)); 00094 } 00095 case CmpInst::ICMP_SGT: { 00096 APInt SMin(CR.getSignedMin()); 00097 if (SMin.isMaxSignedValue()) 00098 return ConstantRange(W, /* empty */ false); 00099 return ConstantRange(SMin + 1, APInt::getSignedMinValue(W)); 00100 } 00101 case CmpInst::ICMP_UGE: { 00102 APInt UMin(CR.getUnsignedMin()); 00103 if (UMin.isMinValue()) 00104 return ConstantRange(W); 00105 return ConstantRange(UMin, APInt::getNullValue(W)); 00106 } 00107 case CmpInst::ICMP_SGE: { 00108 APInt SMin(CR.getSignedMin()); 00109 if (SMin.isMinSignedValue()) 00110 return ConstantRange(W); 00111 return ConstantRange(SMin, APInt::getSignedMinValue(W)); 00112 } 00113 } 00114 } 00115 00116 /// isFullSet - Return true if this set contains all of the elements possible 00117 /// for this data-type 00118 bool ConstantRange::isFullSet() const { 00119 return Lower == Upper && Lower.isMaxValue(); 00120 } 00121 00122 /// isEmptySet - Return true if this set contains no members. 00123 /// 00124 bool ConstantRange::isEmptySet() const { 00125 return Lower == Upper && Lower.isMinValue(); 00126 } 00127 00128 /// isWrappedSet - Return true if this set wraps around the top of the range, 00129 /// for example: [100, 8) 00130 /// 00131 bool ConstantRange::isWrappedSet() const { 00132 return Lower.ugt(Upper); 00133 } 00134 00135 /// isSignWrappedSet - Return true if this set wraps around the INT_MIN of 00136 /// its bitwidth, for example: i8 [120, 140). 00137 /// 00138 bool ConstantRange::isSignWrappedSet() const { 00139 return contains(APInt::getSignedMaxValue(getBitWidth())) && 00140 contains(APInt::getSignedMinValue(getBitWidth())); 00141 } 00142 00143 /// getSetSize - Return the number of elements in this set. 00144 /// 00145 APInt ConstantRange::getSetSize() const { 00146 if (isEmptySet()) 00147 return APInt(getBitWidth()+1, 0); 00148 00149 if (isFullSet()) { 00150 APInt Size(getBitWidth()+1, 0); 00151 Size.setBit(getBitWidth()); 00152 return Size; 00153 } 00154 00155 // This is also correct for wrapped sets. 00156 return (Upper - Lower).zext(getBitWidth()+1); 00157 } 00158 00159 /// getUnsignedMax - Return the largest unsigned value contained in the 00160 /// ConstantRange. 00161 /// 00162 APInt ConstantRange::getUnsignedMax() const { 00163 if (isFullSet() || isWrappedSet()) 00164 return APInt::getMaxValue(getBitWidth()); 00165 return getUpper() - 1; 00166 } 00167 00168 /// getUnsignedMin - Return the smallest unsigned value contained in the 00169 /// ConstantRange. 00170 /// 00171 APInt ConstantRange::getUnsignedMin() const { 00172 if (isFullSet() || (isWrappedSet() && getUpper() != 0)) 00173 return APInt::getMinValue(getBitWidth()); 00174 return getLower(); 00175 } 00176 00177 /// getSignedMax - Return the largest signed value contained in the 00178 /// ConstantRange. 00179 /// 00180 APInt ConstantRange::getSignedMax() const { 00181 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth())); 00182 if (!isWrappedSet()) { 00183 if (getLower().sle(getUpper() - 1)) 00184 return getUpper() - 1; 00185 return SignedMax; 00186 } 00187 if (getLower().isNegative() == getUpper().isNegative()) 00188 return SignedMax; 00189 return getUpper() - 1; 00190 } 00191 00192 /// getSignedMin - Return the smallest signed value contained in the 00193 /// ConstantRange. 00194 /// 00195 APInt ConstantRange::getSignedMin() const { 00196 APInt SignedMin(APInt::getSignedMinValue(getBitWidth())); 00197 if (!isWrappedSet()) { 00198 if (getLower().sle(getUpper() - 1)) 00199 return getLower(); 00200 return SignedMin; 00201 } 00202 if ((getUpper() - 1).slt(getLower())) { 00203 if (getUpper() != SignedMin) 00204 return SignedMin; 00205 } 00206 return getLower(); 00207 } 00208 00209 /// contains - Return true if the specified value is in the set. 00210 /// 00211 bool ConstantRange::contains(const APInt &V) const { 00212 if (Lower == Upper) 00213 return isFullSet(); 00214 00215 if (!isWrappedSet()) 00216 return Lower.ule(V) && V.ult(Upper); 00217 return Lower.ule(V) || V.ult(Upper); 00218 } 00219 00220 /// contains - Return true if the argument is a subset of this range. 00221 /// Two equal sets contain each other. The empty set contained by all other 00222 /// sets. 00223 /// 00224 bool ConstantRange::contains(const ConstantRange &Other) const { 00225 if (isFullSet() || Other.isEmptySet()) return true; 00226 if (isEmptySet() || Other.isFullSet()) return false; 00227 00228 if (!isWrappedSet()) { 00229 if (Other.isWrappedSet()) 00230 return false; 00231 00232 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper); 00233 } 00234 00235 if (!Other.isWrappedSet()) 00236 return Other.getUpper().ule(Upper) || 00237 Lower.ule(Other.getLower()); 00238 00239 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower()); 00240 } 00241 00242 /// subtract - Subtract the specified constant from the endpoints of this 00243 /// constant range. 00244 ConstantRange ConstantRange::subtract(const APInt &Val) const { 00245 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width"); 00246 // If the set is empty or full, don't modify the endpoints. 00247 if (Lower == Upper) 00248 return *this; 00249 return ConstantRange(Lower - Val, Upper - Val); 00250 } 00251 00252 /// \brief Subtract the specified range from this range (aka relative complement 00253 /// of the sets). 00254 ConstantRange ConstantRange::difference(const ConstantRange &CR) const { 00255 return intersectWith(CR.inverse()); 00256 } 00257 00258 /// intersectWith - Return the range that results from the intersection of this 00259 /// range with another range. The resultant range is guaranteed to include all 00260 /// elements contained in both input ranges, and to have the smallest possible 00261 /// set size that does so. Because there may be two intersections with the 00262 /// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A). 00263 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const { 00264 assert(getBitWidth() == CR.getBitWidth() && 00265 "ConstantRange types don't agree!"); 00266 00267 // Handle common cases. 00268 if ( isEmptySet() || CR.isFullSet()) return *this; 00269 if (CR.isEmptySet() || isFullSet()) return CR; 00270 00271 if (!isWrappedSet() && CR.isWrappedSet()) 00272 return CR.intersectWith(*this); 00273 00274 if (!isWrappedSet() && !CR.isWrappedSet()) { 00275 if (Lower.ult(CR.Lower)) { 00276 if (Upper.ule(CR.Lower)) 00277 return ConstantRange(getBitWidth(), false); 00278 00279 if (Upper.ult(CR.Upper)) 00280 return ConstantRange(CR.Lower, Upper); 00281 00282 return CR; 00283 } 00284 if (Upper.ult(CR.Upper)) 00285 return *this; 00286 00287 if (Lower.ult(CR.Upper)) 00288 return ConstantRange(Lower, CR.Upper); 00289 00290 return ConstantRange(getBitWidth(), false); 00291 } 00292 00293 if (isWrappedSet() && !CR.isWrappedSet()) { 00294 if (CR.Lower.ult(Upper)) { 00295 if (CR.Upper.ult(Upper)) 00296 return CR; 00297 00298 if (CR.Upper.ule(Lower)) 00299 return ConstantRange(CR.Lower, Upper); 00300 00301 if (getSetSize().ult(CR.getSetSize())) 00302 return *this; 00303 return CR; 00304 } 00305 if (CR.Lower.ult(Lower)) { 00306 if (CR.Upper.ule(Lower)) 00307 return ConstantRange(getBitWidth(), false); 00308 00309 return ConstantRange(Lower, CR.Upper); 00310 } 00311 return CR; 00312 } 00313 00314 if (CR.Upper.ult(Upper)) { 00315 if (CR.Lower.ult(Upper)) { 00316 if (getSetSize().ult(CR.getSetSize())) 00317 return *this; 00318 return CR; 00319 } 00320 00321 if (CR.Lower.ult(Lower)) 00322 return ConstantRange(Lower, CR.Upper); 00323 00324 return CR; 00325 } 00326 if (CR.Upper.ule(Lower)) { 00327 if (CR.Lower.ult(Lower)) 00328 return *this; 00329 00330 return ConstantRange(CR.Lower, Upper); 00331 } 00332 if (getSetSize().ult(CR.getSetSize())) 00333 return *this; 00334 return CR; 00335 } 00336 00337 00338 /// unionWith - Return the range that results from the union of this range with 00339 /// another range. The resultant range is guaranteed to include the elements of 00340 /// both sets, but may contain more. For example, [3, 9) union [12,15) is 00341 /// [3, 15), which includes 9, 10, and 11, which were not included in either 00342 /// set before. 00343 /// 00344 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const { 00345 assert(getBitWidth() == CR.getBitWidth() && 00346 "ConstantRange types don't agree!"); 00347 00348 if ( isFullSet() || CR.isEmptySet()) return *this; 00349 if (CR.isFullSet() || isEmptySet()) return CR; 00350 00351 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this); 00352 00353 if (!isWrappedSet() && !CR.isWrappedSet()) { 00354 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) { 00355 // If the two ranges are disjoint, find the smaller gap and bridge it. 00356 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper; 00357 if (d1.ult(d2)) 00358 return ConstantRange(Lower, CR.Upper); 00359 return ConstantRange(CR.Lower, Upper); 00360 } 00361 00362 APInt L = Lower, U = Upper; 00363 if (CR.Lower.ult(L)) 00364 L = CR.Lower; 00365 if ((CR.Upper - 1).ugt(U - 1)) 00366 U = CR.Upper; 00367 00368 if (L == 0 && U == 0) 00369 return ConstantRange(getBitWidth()); 00370 00371 return ConstantRange(L, U); 00372 } 00373 00374 if (!CR.isWrappedSet()) { 00375 // ------U L----- and ------U L----- : this 00376 // L--U L--U : CR 00377 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower)) 00378 return *this; 00379 00380 // ------U L----- : this 00381 // L---------U : CR 00382 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) 00383 return ConstantRange(getBitWidth()); 00384 00385 // ----U L---- : this 00386 // L---U : CR 00387 // <d1> <d2> 00388 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) { 00389 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper; 00390 if (d1.ult(d2)) 00391 return ConstantRange(Lower, CR.Upper); 00392 return ConstantRange(CR.Lower, Upper); 00393 } 00394 00395 // ----U L----- : this 00396 // L----U : CR 00397 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) 00398 return ConstantRange(CR.Lower, Upper); 00399 00400 // ------U L---- : this 00401 // L-----U : CR 00402 assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) && 00403 "ConstantRange::unionWith missed a case with one range wrapped"); 00404 return ConstantRange(Lower, CR.Upper); 00405 } 00406 00407 // ------U L---- and ------U L---- : this 00408 // -U L----------- and ------------U L : CR 00409 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper)) 00410 return ConstantRange(getBitWidth()); 00411 00412 APInt L = Lower, U = Upper; 00413 if (CR.Upper.ugt(U)) 00414 U = CR.Upper; 00415 if (CR.Lower.ult(L)) 00416 L = CR.Lower; 00417 00418 return ConstantRange(L, U); 00419 } 00420 00421 /// zeroExtend - Return a new range in the specified integer type, which must 00422 /// be strictly larger than the current type. The returned range will 00423 /// correspond to the possible range of values as if the source range had been 00424 /// zero extended. 00425 ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const { 00426 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false); 00427 00428 unsigned SrcTySize = getBitWidth(); 00429 assert(SrcTySize < DstTySize && "Not a value extension"); 00430 if (isFullSet() || isWrappedSet()) { 00431 // Change into [0, 1 << src bit width) 00432 APInt LowerExt(DstTySize, 0); 00433 if (!Upper) // special case: [X, 0) -- not really wrapping around 00434 LowerExt = Lower.zext(DstTySize); 00435 return ConstantRange(LowerExt, APInt(DstTySize, 1).shl(SrcTySize)); 00436 } 00437 00438 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize)); 00439 } 00440 00441 /// signExtend - Return a new range in the specified integer type, which must 00442 /// be strictly larger than the current type. The returned range will 00443 /// correspond to the possible range of values as if the source range had been 00444 /// sign extended. 00445 ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const { 00446 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false); 00447 00448 unsigned SrcTySize = getBitWidth(); 00449 assert(SrcTySize < DstTySize && "Not a value extension"); 00450 if (isFullSet() || isSignWrappedSet()) { 00451 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1), 00452 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1); 00453 } 00454 00455 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize)); 00456 } 00457 00458 /// truncate - Return a new range in the specified integer type, which must be 00459 /// strictly smaller than the current type. The returned range will 00460 /// correspond to the possible range of values as if the source range had been 00461 /// truncated to the specified type. 00462 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const { 00463 assert(getBitWidth() > DstTySize && "Not a value truncation"); 00464 if (isEmptySet()) 00465 return ConstantRange(DstTySize, /*isFullSet=*/false); 00466 if (isFullSet()) 00467 return ConstantRange(DstTySize, /*isFullSet=*/true); 00468 00469 APInt MaxValue = APInt::getMaxValue(DstTySize).zext(getBitWidth()); 00470 APInt MaxBitValue(getBitWidth(), 0); 00471 MaxBitValue.setBit(DstTySize); 00472 00473 APInt LowerDiv(Lower), UpperDiv(Upper); 00474 ConstantRange Union(DstTySize, /*isFullSet=*/false); 00475 00476 // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue] 00477 // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and 00478 // then we do the union with [MaxValue, Upper) 00479 if (isWrappedSet()) { 00480 // if Upper is greater than Max Value, it covers the whole truncated range. 00481 if (Upper.uge(MaxValue)) 00482 return ConstantRange(DstTySize, /*isFullSet=*/true); 00483 00484 Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize)); 00485 UpperDiv = APInt::getMaxValue(getBitWidth()); 00486 00487 // Union covers the MaxValue case, so return if the remaining range is just 00488 // MaxValue. 00489 if (LowerDiv == UpperDiv) 00490 return Union; 00491 } 00492 00493 // Chop off the most significant bits that are past the destination bitwidth. 00494 if (LowerDiv.uge(MaxValue)) { 00495 APInt Div(getBitWidth(), 0); 00496 APInt::udivrem(LowerDiv, MaxBitValue, Div, LowerDiv); 00497 UpperDiv = UpperDiv - MaxBitValue * Div; 00498 } 00499 00500 if (UpperDiv.ule(MaxValue)) 00501 return ConstantRange(LowerDiv.trunc(DstTySize), 00502 UpperDiv.trunc(DstTySize)).unionWith(Union); 00503 00504 // The truncated value wrapps around. Check if we can do better than fullset. 00505 APInt UpperModulo = UpperDiv - MaxBitValue; 00506 if (UpperModulo.ult(LowerDiv)) 00507 return ConstantRange(LowerDiv.trunc(DstTySize), 00508 UpperModulo.trunc(DstTySize)).unionWith(Union); 00509 00510 return ConstantRange(DstTySize, /*isFullSet=*/true); 00511 } 00512 00513 /// zextOrTrunc - make this range have the bit width given by \p DstTySize. The 00514 /// value is zero extended, truncated, or left alone to make it that width. 00515 ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const { 00516 unsigned SrcTySize = getBitWidth(); 00517 if (SrcTySize > DstTySize) 00518 return truncate(DstTySize); 00519 if (SrcTySize < DstTySize) 00520 return zeroExtend(DstTySize); 00521 return *this; 00522 } 00523 00524 /// sextOrTrunc - make this range have the bit width given by \p DstTySize. The 00525 /// value is sign extended, truncated, or left alone to make it that width. 00526 ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const { 00527 unsigned SrcTySize = getBitWidth(); 00528 if (SrcTySize > DstTySize) 00529 return truncate(DstTySize); 00530 if (SrcTySize < DstTySize) 00531 return signExtend(DstTySize); 00532 return *this; 00533 } 00534 00535 ConstantRange 00536 ConstantRange::add(const ConstantRange &Other) const { 00537 if (isEmptySet() || Other.isEmptySet()) 00538 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 00539 if (isFullSet() || Other.isFullSet()) 00540 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 00541 00542 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize(); 00543 APInt NewLower = getLower() + Other.getLower(); 00544 APInt NewUpper = getUpper() + Other.getUpper() - 1; 00545 if (NewLower == NewUpper) 00546 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 00547 00548 ConstantRange X = ConstantRange(NewLower, NewUpper); 00549 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y)) 00550 // We've wrapped, therefore, full set. 00551 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 00552 00553 return X; 00554 } 00555 00556 ConstantRange 00557 ConstantRange::sub(const ConstantRange &Other) const { 00558 if (isEmptySet() || Other.isEmptySet()) 00559 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 00560 if (isFullSet() || Other.isFullSet()) 00561 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 00562 00563 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize(); 00564 APInt NewLower = getLower() - Other.getUpper() + 1; 00565 APInt NewUpper = getUpper() - Other.getLower(); 00566 if (NewLower == NewUpper) 00567 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 00568 00569 ConstantRange X = ConstantRange(NewLower, NewUpper); 00570 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y)) 00571 // We've wrapped, therefore, full set. 00572 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 00573 00574 return X; 00575 } 00576 00577 ConstantRange 00578 ConstantRange::multiply(const ConstantRange &Other) const { 00579 // TODO: If either operand is a single element and the multiply is known to 00580 // be non-wrapping, round the result min and max value to the appropriate 00581 // multiple of that element. If wrapping is possible, at least adjust the 00582 // range according to the greatest power-of-two factor of the single element. 00583 00584 if (isEmptySet() || Other.isEmptySet()) 00585 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 00586 00587 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2); 00588 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2); 00589 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2); 00590 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2); 00591 00592 ConstantRange Result_zext = ConstantRange(this_min * Other_min, 00593 this_max * Other_max + 1); 00594 return Result_zext.truncate(getBitWidth()); 00595 } 00596 00597 ConstantRange 00598 ConstantRange::smax(const ConstantRange &Other) const { 00599 // X smax Y is: range(smax(X_smin, Y_smin), 00600 // smax(X_smax, Y_smax)) 00601 if (isEmptySet() || Other.isEmptySet()) 00602 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 00603 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin()); 00604 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1; 00605 if (NewU == NewL) 00606 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 00607 return ConstantRange(NewL, NewU); 00608 } 00609 00610 ConstantRange 00611 ConstantRange::umax(const ConstantRange &Other) const { 00612 // X umax Y is: range(umax(X_umin, Y_umin), 00613 // umax(X_umax, Y_umax)) 00614 if (isEmptySet() || Other.isEmptySet()) 00615 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 00616 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin()); 00617 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1; 00618 if (NewU == NewL) 00619 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 00620 return ConstantRange(NewL, NewU); 00621 } 00622 00623 ConstantRange 00624 ConstantRange::udiv(const ConstantRange &RHS) const { 00625 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0) 00626 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 00627 if (RHS.isFullSet()) 00628 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 00629 00630 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax()); 00631 00632 APInt RHS_umin = RHS.getUnsignedMin(); 00633 if (RHS_umin == 0) { 00634 // We want the lowest value in RHS excluding zero. Usually that would be 1 00635 // except for a range in the form of [X, 1) in which case it would be X. 00636 if (RHS.getUpper() == 1) 00637 RHS_umin = RHS.getLower(); 00638 else 00639 RHS_umin = APInt(getBitWidth(), 1); 00640 } 00641 00642 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1; 00643 00644 // If the LHS is Full and the RHS is a wrapped interval containing 1 then 00645 // this could occur. 00646 if (Lower == Upper) 00647 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 00648 00649 return ConstantRange(Lower, Upper); 00650 } 00651 00652 ConstantRange 00653 ConstantRange::binaryAnd(const ConstantRange &Other) const { 00654 if (isEmptySet() || Other.isEmptySet()) 00655 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 00656 00657 // TODO: replace this with something less conservative 00658 00659 APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax()); 00660 if (umin.isAllOnesValue()) 00661 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 00662 return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1); 00663 } 00664 00665 ConstantRange 00666 ConstantRange::binaryOr(const ConstantRange &Other) const { 00667 if (isEmptySet() || Other.isEmptySet()) 00668 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 00669 00670 // TODO: replace this with something less conservative 00671 00672 APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin()); 00673 if (umax.isMinValue()) 00674 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 00675 return ConstantRange(umax, APInt::getNullValue(getBitWidth())); 00676 } 00677 00678 ConstantRange 00679 ConstantRange::shl(const ConstantRange &Other) const { 00680 if (isEmptySet() || Other.isEmptySet()) 00681 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 00682 00683 APInt min = getUnsignedMin().shl(Other.getUnsignedMin()); 00684 APInt max = getUnsignedMax().shl(Other.getUnsignedMax()); 00685 00686 // there's no overflow! 00687 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros()); 00688 if (Zeros.ugt(Other.getUnsignedMax())) 00689 return ConstantRange(min, max + 1); 00690 00691 // FIXME: implement the other tricky cases 00692 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 00693 } 00694 00695 ConstantRange 00696 ConstantRange::lshr(const ConstantRange &Other) const { 00697 if (isEmptySet() || Other.isEmptySet()) 00698 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 00699 00700 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin()); 00701 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax()); 00702 if (min == max + 1) 00703 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 00704 00705 return ConstantRange(min, max + 1); 00706 } 00707 00708 ConstantRange ConstantRange::inverse() const { 00709 if (isFullSet()) 00710 return ConstantRange(getBitWidth(), /*isFullSet=*/false); 00711 if (isEmptySet()) 00712 return ConstantRange(getBitWidth(), /*isFullSet=*/true); 00713 return ConstantRange(Upper, Lower); 00714 } 00715 00716 /// print - Print out the bounds to a stream... 00717 /// 00718 void ConstantRange::print(raw_ostream &OS) const { 00719 if (isFullSet()) 00720 OS << "full-set"; 00721 else if (isEmptySet()) 00722 OS << "empty-set"; 00723 else 00724 OS << "[" << Lower << "," << Upper << ")"; 00725 } 00726 00727 /// dump - Allow printing from a debugger easily... 00728 /// 00729 void ConstantRange::dump() const { 00730 print(dbgs()); 00731 }