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 /// contains - Return true if the specified value is in the set.
89 bool ConstantRange::contains(const APInt &V) const {
94 return Lower.ule(V) && V.ult(Upper);
96 return Lower.ule(V) || V.ult(Upper);
99 /// subtract - Subtract the specified constant from the endpoints of this
101 ConstantRange ConstantRange::subtract(const APInt &Val) const {
102 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
103 // If the set is empty or full, don't modify the endpoints.
106 return ConstantRange(Lower - Val, Upper - Val);
110 // intersect1Wrapped - This helper function is used to intersect two ranges when
111 // it is known that LHS is wrapped and RHS isn't.
114 ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
115 const ConstantRange &RHS) {
116 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
118 // Check to see if we overlap on the Left side of RHS...
120 if (RHS.Lower.ult(LHS.Upper)) {
121 // We do overlap on the left side of RHS, see if we overlap on the right of
123 if (RHS.Upper.ugt(LHS.Lower)) {
124 // Ok, the result overlaps on both the left and right sides. See if the
125 // resultant interval will be smaller if we wrap or not...
127 if (LHS.getSetSize().ult(RHS.getSetSize()))
133 // No overlap on the right, just on the left.
134 return ConstantRange(RHS.Lower, LHS.Upper);
137 // We don't overlap on the left side of RHS, see if we overlap on the right
139 if (RHS.Upper.ugt(LHS.Lower)) {
141 return ConstantRange(LHS.Lower, RHS.Upper);
144 return ConstantRange(LHS.getBitWidth(), false);
149 /// intersectWith - Return the range that results from the intersection of this
150 /// range with another range.
152 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
153 assert(getBitWidth() == CR.getBitWidth() &&
154 "ConstantRange types don't agree!");
155 // Handle common special cases
156 if (isEmptySet() || CR.isFullSet())
158 if (isFullSet() || CR.isEmptySet())
161 if (!isWrappedSet()) {
162 if (!CR.isWrappedSet()) {
163 using namespace APIntOps;
164 APInt L = umax(Lower, CR.Lower);
165 APInt U = umin(Upper, CR.Upper);
167 if (L.ult(U)) // If range isn't empty...
168 return ConstantRange(L, U);
170 return ConstantRange(getBitWidth(), false);// Otherwise, empty set
172 return intersect1Wrapped(CR, *this);
173 } else { // We know "this" is wrapped...
174 if (!CR.isWrappedSet())
175 return intersect1Wrapped(*this, CR);
177 // Both ranges are wrapped...
178 using namespace APIntOps;
179 APInt L = umax(Lower, CR.Lower);
180 APInt U = umin(Upper, CR.Upper);
181 return ConstantRange(L, U);
187 /// unionWith - Return the range that results from the union of this range with
188 /// another range. The resultant range is guaranteed to include the elements of
189 /// both sets, but may contain more. For example, [3, 9) union [12,15) is [3,
190 /// 15), which includes 9, 10, and 11, which were not included in either set
193 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
194 assert(getBitWidth() == CR.getBitWidth() &&
195 "ConstantRange types don't agree!");
197 assert(0 && "Range union not implemented yet!");
202 /// zeroExtend - Return a new range in the specified integer type, which must
203 /// be strictly larger than the current type. The returned range will
204 /// correspond to the possible range of values as if the source range had been
206 ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
207 unsigned SrcTySize = getBitWidth();
208 assert(SrcTySize < DstTySize && "Not a value extension");
210 // Change a source full set into [0, 1 << 8*numbytes)
211 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
213 APInt L = Lower; L.zext(DstTySize);
214 APInt U = Upper; U.zext(DstTySize);
215 return ConstantRange(L, U);
218 /// truncate - Return a new range in the specified integer type, which must be
219 /// strictly smaller than the current type. The returned range will
220 /// correspond to the possible range of values as if the source range had been
221 /// truncated to the specified type.
222 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
223 unsigned SrcTySize = getBitWidth();
224 assert(SrcTySize > DstTySize && "Not a value truncation");
225 APInt Size = APInt::getMaxValue(DstTySize).zext(SrcTySize);
226 if (isFullSet() || getSetSize().ugt(Size))
227 return ConstantRange(DstTySize);
229 APInt L = Lower; L.trunc(DstTySize);
230 APInt U = Upper; U.trunc(DstTySize);
231 return ConstantRange(L, U);
234 /// print - Print out the bounds to a stream...
236 void ConstantRange::print(std::ostream &OS) const {
237 OS << "[" << Lower.toStringSigned(10) << ","
238 << Upper.toStringSigned(10) << " )";
241 /// dump - Allow printing from a debugger easily...
243 void ConstantRange::dump() const {