// intersect1Wrapped - This helper function is used to intersect two ranges when // it is known that LHS is wrapped and RHS isn't. // ConstantRange ConstantRange::intersect1Wrapped(const ConstantRange &LHS, const ConstantRange &RHS) { assert(LHS.isWrappedSet() && !RHS.isWrappedSet()); // Check to see if we overlap on the Left side of RHS... // if (RHS.Lower.ult(LHS.Upper)) { // We do overlap on the left side of RHS, see if we overlap on the right of // RHS... if (RHS.Upper.ugt(LHS.Lower)) { // Ok, the result overlaps on both the left and right sides. See if the // resultant interval will be smaller if we wrap or not... // if (LHS.getSetSize().ult(RHS.getSetSize())) return LHS; else return RHS; } else { // No overlap on the right, just on the left. return ConstantRange(RHS.Lower, LHS.Upper); } } else { // We don't overlap on the left side of RHS, see if we overlap on the right // of RHS... if (RHS.Upper.ugt(LHS.Lower)) { // Simple overlap... return ConstantRange(LHS.Lower, RHS.Upper); } else { // No overlap... return ConstantRange(LHS.getBitWidth(), false); } } }
/// intersectWith - Return the range that results from the intersection of this /// range with another range. /// ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const { assert(getBitWidth() == CR.getBitWidth() && "ConstantRange types don't agree!"); // Handle common special cases if (isEmptySet() || CR.isFullSet()) return *this; if (isFullSet() || CR.isEmptySet()) return CR; if (!isWrappedSet()) { if (!CR.isWrappedSet()) { using namespace APIntOps; APInt L = umax(Lower, CR.Lower); APInt U = umin(Upper, CR.Upper); if (L.ult(U)) // If range isn't empty... return ConstantRange(L, U); else return ConstantRange(getBitWidth(), false);// Otherwise, empty set } else return intersect1Wrapped(CR, *this); } else { // We know "this" is wrapped... if (!CR.isWrappedSet()) return intersect1Wrapped(*this, CR); else { // Both ranges are wrapped... using namespace APIntOps; APInt L = umax(Lower, CR.Lower); APInt U = umin(Upper, CR.Upper); return ConstantRange(L, U); } } return *this; }
/// contains - Return true if the argument is a subset of this range. /// Two equal sets contain each other. The empty set contained by all other /// sets. /// bool ConstantRange::contains(const ConstantRange &Other) const { if (isFullSet() || Other.isEmptySet()) return true; if (isEmptySet() || Other.isFullSet()) return false; if (!isWrappedSet()) { if (Other.isWrappedSet()) return false; return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper); } if (!Other.isWrappedSet()) return Other.getUpper().ule(Upper) || Lower.ule(Other.getLower()); return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower()); }
ConstantRange ConstantRange::multiply(const ConstantRange &Other) const { // TODO: If either operand is a single element and the multiply is known to // be non-wrapping, round the result min and max value to the appropriate // multiple of that element. If wrapping is possible, at least adjust the // range according to the greatest power-of-two factor of the single element. if (isEmptySet() || Other.isEmptySet()) return ConstantRange(getBitWidth(), /*isFullSet=*/false); // Multiplication is signedness-independent. However different ranges can be // obtained depending on how the input ranges are treated. These different // ranges are all conservatively correct, but one might be better than the // other. We calculate two ranges; one treating the inputs as unsigned // and the other signed, then return the smallest of these ranges. // Unsigned range first. APInt this_min = getUnsignedMin().zext(getBitWidth() * 2); APInt this_max = getUnsignedMax().zext(getBitWidth() * 2); APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2); APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2); ConstantRange Result_zext = ConstantRange(this_min * Other_min, this_max * Other_max + 1); ConstantRange UR = Result_zext.truncate(getBitWidth()); // If the unsigned range doesn't wrap, and isn't negative then it's a range // from one positive number to another which is as good as we can generate. // In this case, skip the extra work of generating signed ranges which aren't // going to be better than this range. if (!UR.isWrappedSet() && (UR.getUpper().isNonNegative() || UR.getUpper().isMinSignedValue())) return UR; // Now the signed range. Because we could be dealing with negative numbers // here, the lower bound is the smallest of the cartesian product of the // lower and upper ranges; for example: // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6. // Similarly for the upper bound, swapping min for max. this_min = getSignedMin().sext(getBitWidth() * 2); this_max = getSignedMax().sext(getBitWidth() * 2); Other_min = Other.getSignedMin().sext(getBitWidth() * 2); Other_max = Other.getSignedMax().sext(getBitWidth() * 2); auto L = {this_min * Other_min, this_min * Other_max, this_max * Other_min, this_max * Other_max}; auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); }; ConstantRange Result_sext(std::min(L, Compare), std::max(L, Compare) + 1); ConstantRange SR = Result_sext.truncate(getBitWidth()); return UR.isSizeStrictlySmallerThan(SR) ? UR : SR; }
MyConstantRange binaryAnd(const ConstantRange &Other) const { if (isEmptySet() || Other.isEmptySet()) return MyConstantRange(getBitWidth(), /*isFullSet=*/false); if (!isWrappedSet() && !Other.isWrappedSet() && !isFullSet() && !Other.isFullSet()) { unsigned width1 = ((getUpper() - 1) ^ getLower()).logBase2() + 1; unsigned width2 = ((Other.getUpper() - 1) ^ Other.getLower()).logBase2() + 1; APInt res1 = getLower().lshr(width1) << width1; APInt res2 = Other.getLower().lshr(width2) << width2; APInt res_high1 = getLower(); APInt res_high2 = Other.getLower(); res_high1.setLowBits(width1); res_high2.setLowBits(width2); if ((res1 & res2).isNullValue() && (res_high1 & res_high2).isAllOnesValue()) { return MyConstantRange(getBitWidth(), /*isFullSet=*/true); } return MyConstantRange(res1 & res2, (res_high1 & res_high2) + 1); } APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax()); if (umin.isAllOnesValue()) return MyConstantRange(getBitWidth(), /*isFullSet=*/true); return MyConstantRange(APInt::getNullValue(getBitWidth()), std::move(umin) + 1); }
/// unionWith - Return the range that results from the union of this range with /// another range. The resultant range is guaranteed to include the elements of /// both sets, but may contain more. For example, [3, 9) union [12,15) is /// [3, 15), which includes 9, 10, and 11, which were not included in either /// set before. /// ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const { assert(getBitWidth() == CR.getBitWidth() && "ConstantRange types don't agree!"); if ( isFullSet() || CR.isEmptySet()) return *this; if (CR.isFullSet() || isEmptySet()) return CR; if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this); if (!isWrappedSet() && !CR.isWrappedSet()) { if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) { // If the two ranges are disjoint, find the smaller gap and bridge it. APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper; if (d1.ult(d2)) return ConstantRange(Lower, CR.Upper); return ConstantRange(CR.Lower, Upper); } APInt L = Lower, U = Upper; if (CR.Lower.ult(L)) L = CR.Lower; if ((CR.Upper - 1).ugt(U - 1)) U = CR.Upper; if (L == 0 && U == 0) return ConstantRange(getBitWidth()); return ConstantRange(L, U); } if (!CR.isWrappedSet()) { // ------U L----- and ------U L----- : this // L--U L--U : CR if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower)) return *this; // ------U L----- : this // L---------U : CR if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) return ConstantRange(getBitWidth()); // ----U L---- : this // L---U : CR // <d1> <d2> if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) { APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper; if (d1.ult(d2)) return ConstantRange(Lower, CR.Upper); return ConstantRange(CR.Lower, Upper); } // ----U L----- : this // L----U : CR if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) return ConstantRange(CR.Lower, Upper); // ------U L---- : this // L-----U : CR assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) && "ConstantRange::unionWith missed a case with one range wrapped"); return ConstantRange(Lower, CR.Upper); } // ------U L---- and ------U L---- : this // -U L----------- and ------------U L : CR if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper)) return ConstantRange(getBitWidth()); APInt L = Lower, U = Upper; if (CR.Upper.ugt(U)) U = CR.Upper; if (CR.Lower.ult(L)) L = CR.Lower; return ConstantRange(L, U); }
/// intersectWith - Return the range that results from the intersection of this /// range with another range. The resultant range is guaranteed to include all /// elements contained in both input ranges, and to have the smallest possible /// set size that does so. Because there may be two intersections with the /// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A). ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const { assert(getBitWidth() == CR.getBitWidth() && "ConstantRange types don't agree!"); // Handle common cases. if ( isEmptySet() || CR.isFullSet()) return *this; if (CR.isEmptySet() || isFullSet()) return CR; if (!isWrappedSet() && CR.isWrappedSet()) return CR.intersectWith(*this); if (!isWrappedSet() && !CR.isWrappedSet()) { if (Lower.ult(CR.Lower)) { if (Upper.ule(CR.Lower)) return ConstantRange(getBitWidth(), false); if (Upper.ult(CR.Upper)) return ConstantRange(CR.Lower, Upper); return CR; } if (Upper.ult(CR.Upper)) return *this; if (Lower.ult(CR.Upper)) return ConstantRange(Lower, CR.Upper); return ConstantRange(getBitWidth(), false); } if (isWrappedSet() && !CR.isWrappedSet()) { if (CR.Lower.ult(Upper)) { if (CR.Upper.ult(Upper)) return CR; if (CR.Upper.ule(Lower)) return ConstantRange(CR.Lower, Upper); if (getSetSize().ult(CR.getSetSize())) return *this; return CR; } if (CR.Lower.ult(Lower)) { if (CR.Upper.ule(Lower)) return ConstantRange(getBitWidth(), false); return ConstantRange(Lower, CR.Upper); } return CR; } if (CR.Upper.ult(Upper)) { if (CR.Lower.ult(Upper)) { if (getSetSize().ult(CR.getSetSize())) return *this; return CR; } if (CR.Lower.ult(Lower)) return ConstantRange(Lower, CR.Upper); return CR; } if (CR.Upper.ule(Lower)) { if (CR.Lower.ult(Lower)) return *this; return ConstantRange(CR.Lower, Upper); } if (getSetSize().ult(CR.getSetSize())) return *this; return CR; }
/// unionWith - Return the range that results from the union of this range with /// another range. The resultant range is guaranteed to include the elements of /// both sets, but may contain more. For example, [3, 9) union [12,15) is /// [3, 15), which includes 9, 10, and 11, which were not included in either /// set before. /// ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const { assert(getBitWidth() == CR.getBitWidth() && "ConstantRange types don't agree!"); if ( isFullSet() || CR.isEmptySet()) return *this; if (CR.isFullSet() || isEmptySet()) return CR; if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this); APInt L = Lower, U = Upper; if (!isWrappedSet() && !CR.isWrappedSet()) { if (CR.Lower.ult(L)) L = CR.Lower; if (CR.Upper.ugt(U)) U = CR.Upper; } if (isWrappedSet() && !CR.isWrappedSet()) { if ((CR.Lower.ult(Upper) && CR.Upper.ult(Upper)) || (CR.Lower.ugt(Lower) && CR.Upper.ugt(Lower))) { return *this; } if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) { return ConstantRange(getBitWidth()); } if (CR.Lower.ule(Upper) && CR.Upper.ule(Lower)) { APInt d1 = CR.Upper - Upper, d2 = Lower - CR.Upper; if (d1.ult(d2)) { U = CR.Upper; } else { L = CR.Upper; } } if (Upper.ult(CR.Lower) && CR.Upper.ult(Lower)) { APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper; if (d1.ult(d2)) { U = CR.Lower + 1; } else { L = CR.Upper - 1; } } if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) { APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Lower; if (d1.ult(d2)) { U = CR.Lower + 1; } else { L = CR.Lower; } } } if (isWrappedSet() && CR.isWrappedSet()) { if (Lower.ult(CR.Upper) || CR.Lower.ult(Upper)) return ConstantRange(getBitWidth()); if (CR.Upper.ugt(U)) { U = CR.Upper; } if (CR.Lower.ult(L)) { L = CR.Lower; } if (L == U) return ConstantRange(getBitWidth()); } return ConstantRange(L, U); }