1 //===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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):
17 // [F, F) = {} = Empty set
20 // [T, T) = {F, T} = Full set
22 //===----------------------------------------------------------------------===//
24 #include "llvm/Support/ConstantRange.h"
25 #include "llvm/Support/Streams.h"
29 /// Initialize a full (the default) or empty set for the specified type.
31 ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) :
32 Lower(BitWidth, 0), Upper(BitWidth, 0) {
34 Lower = Upper = APInt::getMaxValue(BitWidth);
36 Lower = Upper = APInt::getMinValue(BitWidth);
39 /// Initialize a range to hold the single specified value.
41 ConstantRange::ConstantRange(const APInt & V) : Lower(V), Upper(V + 1) { }
43 ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
45 assert(L.getBitWidth() == U.getBitWidth() &&
46 "ConstantRange with unequal bit widths");
47 uint32_t BitWidth = L.getBitWidth();
48 assert((L != U || (L == APInt::getMaxValue(BitWidth) ||
49 L == APInt::getMinValue(BitWidth))) &&
50 "Lower == Upper, but they aren't min or max value!");
53 /// isFullSet - Return true if this set contains all of the elements possible
54 /// for this data-type
55 bool ConstantRange::isFullSet() const {
56 return Lower == Upper && Lower == APInt::getMaxValue(getBitWidth());
59 /// isEmptySet - Return true if this set contains no members.
61 bool ConstantRange::isEmptySet() const {
62 return Lower == Upper && Lower == APInt::getMinValue(getBitWidth());
65 /// isWrappedSet - Return true if this set wraps around the top of the range,
66 /// for example: [100, 8)
68 bool ConstantRange::isWrappedSet() const {
69 return Lower.ugt(Upper);
72 /// getSetSize - Return the number of elements in this set.
74 APInt ConstantRange::getSetSize() const {
76 return APInt(getBitWidth(), 0);
77 if (getBitWidth() == 1) {
78 if (Lower != Upper) // One of T or F in the set...
80 return APInt(2, 2); // Must be full set...
83 // Simply subtract the bounds...
87 /// getUnsignedMax - Return the largest unsigned value contained in the
90 APInt ConstantRange::getUnsignedMax() const {
91 if (isFullSet() || isWrappedSet())
92 return APInt::getMaxValue(getBitWidth());
94 return getUpper() - 1;
97 /// getUnsignedMin - Return the smallest unsigned value contained in the
100 APInt ConstantRange::getUnsignedMin() const {
101 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
102 return APInt::getMinValue(getBitWidth());
107 /// getSignedMax - Return the largest signed value contained in the
110 APInt ConstantRange::getSignedMax() const {
111 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
112 if (!isWrappedSet()) {
113 if (getLower().slt(getUpper() - 1))
114 return getUpper() - 1;
118 if ((getUpper() - 1).slt(getLower())) {
119 if (getLower() != SignedMax)
122 return getUpper() - 1;
124 return getUpper() - 1;
129 /// getSignedMin - Return the smallest signed value contained in the
132 APInt ConstantRange::getSignedMin() const {
133 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
134 if (!isWrappedSet()) {
135 if (getLower().slt(getUpper() - 1))
140 if ((getUpper() - 1).slt(getLower())) {
141 if (getUpper() != SignedMin)
151 /// contains - Return true if the specified value is in the set.
153 bool ConstantRange::contains(const APInt &V) const {
158 return Lower.ule(V) && V.ult(Upper);
160 return Lower.ule(V) || V.ult(Upper);
163 /// subtract - Subtract the specified constant from the endpoints of this
165 ConstantRange ConstantRange::subtract(const APInt &Val) const {
166 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
167 // If the set is empty or full, don't modify the endpoints.
170 return ConstantRange(Lower - Val, Upper - Val);
174 // intersect1Wrapped - This helper function is used to intersect two ranges when
175 // it is known that LHS is wrapped and RHS isn't.
178 ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
179 const ConstantRange &RHS) {
180 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
182 // Check to see if we overlap on the Left side of RHS...
184 if (RHS.Lower.ult(LHS.Upper)) {
185 // We do overlap on the left side of RHS, see if we overlap on the right of
187 if (RHS.Upper.ugt(LHS.Lower)) {
188 // Ok, the result overlaps on both the left and right sides. See if the
189 // resultant interval will be smaller if we wrap or not...
191 if (LHS.getSetSize().ult(RHS.getSetSize()))
197 // No overlap on the right, just on the left.
198 return ConstantRange(RHS.Lower, LHS.Upper);
201 // We don't overlap on the left side of RHS, see if we overlap on the right
203 if (RHS.Upper.ugt(LHS.Lower)) {
205 return ConstantRange(LHS.Lower, RHS.Upper);
208 return ConstantRange(LHS.getBitWidth(), false);
213 /// intersectWith - Return the range that results from the intersection of this
214 /// range with another range.
216 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
217 assert(getBitWidth() == CR.getBitWidth() &&
218 "ConstantRange types don't agree!");
219 // Handle common special cases
220 if (isEmptySet() || CR.isFullSet())
222 if (isFullSet() || CR.isEmptySet())
225 if (!isWrappedSet()) {
226 if (!CR.isWrappedSet()) {
227 using namespace APIntOps;
228 APInt L = umax(Lower, CR.Lower);
229 APInt U = umin(Upper, CR.Upper);
231 if (L.ult(U)) // If range isn't empty...
232 return ConstantRange(L, U);
234 return ConstantRange(getBitWidth(), false);// Otherwise, empty set
236 return intersect1Wrapped(CR, *this);
237 } else { // We know "this" is wrapped...
238 if (!CR.isWrappedSet())
239 return intersect1Wrapped(*this, CR);
241 // Both ranges are wrapped...
242 using namespace APIntOps;
243 APInt L = umax(Lower, CR.Lower);
244 APInt U = umin(Upper, CR.Upper);
245 return ConstantRange(L, U);
251 /// unionWith - Return the range that results from the union of this range with
252 /// another range. The resultant range is guaranteed to include the elements of
253 /// both sets, but may contain more. For example, [3, 9) union [12,15) is
254 /// [3, 15), which includes 9, 10, and 11, which were not included in either
257 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
258 assert(getBitWidth() == CR.getBitWidth() &&
259 "ConstantRange types don't agree!");
261 if ( isFullSet() || CR.isEmptySet()) return *this;
262 if (CR.isFullSet() || isEmptySet()) return CR;
264 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
266 APInt L = Lower, U = Upper;
268 if (!isWrappedSet() && !CR.isWrappedSet()) {
276 if (isWrappedSet() && !CR.isWrappedSet()) {
277 if ((CR.Lower.ult(Upper) && CR.Upper.ult(Upper)) ||
278 (CR.Lower.ugt(Lower) && CR.Upper.ugt(Lower))) {
282 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) {
283 return ConstantRange(getBitWidth());
286 if (CR.Lower.ule(Upper) && CR.Upper.ule(Lower)) {
287 APInt d1 = CR.Upper - Upper, d2 = Lower - CR.Upper;
295 if (Upper.ult(CR.Lower) && CR.Upper.ult(Lower)) {
296 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
304 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) {
305 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Lower;
315 if (isWrappedSet() && CR.isWrappedSet()) {
316 if (Lower.ult(CR.Upper) || CR.Lower.ult(Upper))
317 return ConstantRange(getBitWidth());
319 if (CR.Upper.ugt(U)) {
323 if (CR.Lower.ult(L)) {
327 if (L == U) return ConstantRange(getBitWidth());
330 return ConstantRange(L, U);
333 /// zeroExtend - Return a new range in the specified integer type, which must
334 /// be strictly larger than the current type. The returned range will
335 /// correspond to the possible range of values as if the source range had been
337 ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
338 unsigned SrcTySize = getBitWidth();
339 assert(SrcTySize < DstTySize && "Not a value extension");
341 // Change a source full set into [0, 1 << 8*numbytes)
342 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
344 APInt L = Lower; L.zext(DstTySize);
345 APInt U = Upper; U.zext(DstTySize);
346 return ConstantRange(L, U);
349 /// signExtend - Return a new range in the specified integer type, which must
350 /// be strictly larger than the current type. The returned range will
351 /// correspond to the possible range of values as if the source range had been
353 ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
354 unsigned SrcTySize = getBitWidth();
355 assert(SrcTySize < DstTySize && "Not a value extension");
357 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
358 APInt::getLowBitsSet(DstTySize, SrcTySize-1));
361 APInt L = Lower; L.sext(DstTySize);
362 APInt U = Upper; U.sext(DstTySize);
363 return ConstantRange(L, U);
366 /// truncate - Return a new range in the specified integer type, which must be
367 /// strictly smaller than the current type. The returned range will
368 /// correspond to the possible range of values as if the source range had been
369 /// truncated to the specified type.
370 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
371 unsigned SrcTySize = getBitWidth();
372 assert(SrcTySize > DstTySize && "Not a value truncation");
373 APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
374 if (isFullSet() || getSetSize().ugt(Size))
375 return ConstantRange(DstTySize);
377 APInt L = Lower; L.trunc(DstTySize);
378 APInt U = Upper; U.trunc(DstTySize);
379 return ConstantRange(L, U);
382 /// print - Print out the bounds to a stream...
384 void ConstantRange::print(std::ostream &OS) const {
385 OS << "[" << Lower.toStringSigned(10) << ","
386 << Upper.toStringSigned(10) << " )";
389 /// dump - Allow printing from a debugger easily...
391 void ConstantRange::dump() const {