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 assert((L
!= U
|| (L
.isMaxValue() || L
.isMinValue())) &&
48 "Lower == Upper, but they aren't min or max value!");
51 /// isFullSet - Return true if this set contains all of the elements possible
52 /// for this data-type
53 bool ConstantRange::isFullSet() const {
54 return Lower
== Upper
&& Lower
.isMaxValue();
57 /// isEmptySet - Return true if this set contains no members.
59 bool ConstantRange::isEmptySet() const {
60 return Lower
== Upper
&& Lower
.isMinValue();
63 /// isWrappedSet - Return true if this set wraps around the top of the range,
64 /// for example: [100, 8)
66 bool ConstantRange::isWrappedSet() const {
67 return Lower
.ugt(Upper
);
70 /// getSetSize - Return the number of elements in this set.
72 APInt
ConstantRange::getSetSize() const {
74 return APInt(getBitWidth(), 0);
75 if (getBitWidth() == 1) {
76 if (Lower
!= Upper
) // One of T or F in the set...
78 return APInt(2, 2); // Must be full set...
81 // Simply subtract the bounds...
85 /// getUnsignedMax - Return the largest unsigned value contained in the
88 APInt
ConstantRange::getUnsignedMax() const {
89 if (isFullSet() || isWrappedSet())
90 return APInt::getMaxValue(getBitWidth());
92 return getUpper() - 1;
95 /// getUnsignedMin - Return the smallest unsigned value contained in the
98 APInt
ConstantRange::getUnsignedMin() const {
99 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
100 return APInt::getMinValue(getBitWidth());
105 /// getSignedMax - Return the largest signed value contained in the
108 APInt
ConstantRange::getSignedMax() const {
109 APInt
SignedMax(APInt::getSignedMaxValue(getBitWidth()));
110 if (!isWrappedSet()) {
111 if (getLower().sle(getUpper() - 1))
112 return getUpper() - 1;
116 if ((getUpper() - 1).slt(getLower())) {
117 if (getLower() != SignedMax
)
120 return getUpper() - 1;
122 return getUpper() - 1;
127 /// getSignedMin - Return the smallest signed value contained in the
130 APInt
ConstantRange::getSignedMin() const {
131 APInt
SignedMin(APInt::getSignedMinValue(getBitWidth()));
132 if (!isWrappedSet()) {
133 if (getLower().sle(getUpper() - 1))
138 if ((getUpper() - 1).slt(getLower())) {
139 if (getUpper() != SignedMin
)
149 /// contains - Return true if the specified value is in the set.
151 bool ConstantRange::contains(const APInt
&V
) const {
156 return Lower
.ule(V
) && V
.ult(Upper
);
158 return Lower
.ule(V
) || V
.ult(Upper
);
161 /// subtract - Subtract the specified constant from the endpoints of this
163 ConstantRange
ConstantRange::subtract(const APInt
&Val
) const {
164 assert(Val
.getBitWidth() == getBitWidth() && "Wrong bit width");
165 // If the set is empty or full, don't modify the endpoints.
168 return ConstantRange(Lower
- Val
, Upper
- Val
);
172 // intersect1Wrapped - This helper function is used to intersect two ranges when
173 // it is known that LHS is wrapped and RHS isn't.
176 ConstantRange::intersect1Wrapped(const ConstantRange
&LHS
,
177 const ConstantRange
&RHS
) {
178 assert(LHS
.isWrappedSet() && !RHS
.isWrappedSet());
180 // Check to see if we overlap on the Left side of RHS...
182 if (RHS
.Lower
.ult(LHS
.Upper
)) {
183 // We do overlap on the left side of RHS, see if we overlap on the right of
185 if (RHS
.Upper
.ugt(LHS
.Lower
)) {
186 // Ok, the result overlaps on both the left and right sides. See if the
187 // resultant interval will be smaller if we wrap or not...
189 if (LHS
.getSetSize().ult(RHS
.getSetSize()))
195 // No overlap on the right, just on the left.
196 return ConstantRange(RHS
.Lower
, LHS
.Upper
);
199 // We don't overlap on the left side of RHS, see if we overlap on the right
201 if (RHS
.Upper
.ugt(LHS
.Lower
)) {
203 return ConstantRange(LHS
.Lower
, RHS
.Upper
);
206 return ConstantRange(LHS
.getBitWidth(), false);
211 /// intersectWith - Return the range that results from the intersection of this
212 /// range with another range.
214 ConstantRange
ConstantRange::intersectWith(const ConstantRange
&CR
) const {
215 assert(getBitWidth() == CR
.getBitWidth() &&
216 "ConstantRange types don't agree!");
217 // Handle common special cases
218 if (isEmptySet() || CR
.isFullSet())
220 if (isFullSet() || CR
.isEmptySet())
223 if (!isWrappedSet()) {
224 if (!CR
.isWrappedSet()) {
225 using namespace APIntOps
;
226 APInt L
= umax(Lower
, CR
.Lower
);
227 APInt U
= umin(Upper
, CR
.Upper
);
229 if (L
.ult(U
)) // If range isn't empty...
230 return ConstantRange(L
, U
);
232 return ConstantRange(getBitWidth(), false);// Otherwise, empty set
234 return intersect1Wrapped(CR
, *this);
235 } else { // We know "this" is wrapped...
236 if (!CR
.isWrappedSet())
237 return intersect1Wrapped(*this, CR
);
239 // Both ranges are wrapped...
240 using namespace APIntOps
;
241 APInt L
= umax(Lower
, CR
.Lower
);
242 APInt U
= umin(Upper
, CR
.Upper
);
243 return ConstantRange(L
, U
);
249 /// maximalIntersectWith - Return the range that results from the intersection
250 /// of this range with another range. The resultant range is guaranteed to
251 /// include all elements contained in both input ranges, and to have the
252 /// smallest possible set size that does so. Because there may be two
253 /// intersections with the same set size, A.maximalIntersectWith(B) might not
254 /// be equal to B.maximalIntersect(A).
255 ConstantRange
ConstantRange::maximalIntersectWith(const ConstantRange
&CR
) const {
256 assert(getBitWidth() == CR
.getBitWidth() &&
257 "ConstantRange types don't agree!");
259 // Handle common cases.
260 if ( isEmptySet() || CR
.isFullSet()) return *this;
261 if (CR
.isEmptySet() || isFullSet()) return CR
;
263 if (!isWrappedSet() && CR
.isWrappedSet())
264 return CR
.maximalIntersectWith(*this);
266 if (!isWrappedSet() && !CR
.isWrappedSet()) {
267 if (Lower
.ult(CR
.Lower
)) {
268 if (Upper
.ule(CR
.Lower
))
269 return ConstantRange(getBitWidth(), false);
271 if (Upper
.ult(CR
.Upper
))
272 return ConstantRange(CR
.Lower
, Upper
);
276 if (Upper
.ult(CR
.Upper
))
279 if (Lower
.ult(CR
.Upper
))
280 return ConstantRange(Lower
, CR
.Upper
);
282 return ConstantRange(getBitWidth(), false);
286 if (isWrappedSet() && !CR
.isWrappedSet()) {
287 if (CR
.Lower
.ult(Upper
)) {
288 if (CR
.Upper
.ult(Upper
))
291 if (CR
.Upper
.ult(Lower
))
292 return ConstantRange(CR
.Lower
, Upper
);
294 if (getSetSize().ult(CR
.getSetSize()))
298 } else if (CR
.Lower
.ult(Lower
)) {
299 if (CR
.Upper
.ule(Lower
))
300 return ConstantRange(getBitWidth(), false);
302 return ConstantRange(Lower
, CR
.Upper
);
307 if (CR
.Upper
.ult(Upper
)) {
308 if (CR
.Lower
.ult(Upper
)) {
309 if (getSetSize().ult(CR
.getSetSize()))
315 if (CR
.Lower
.ult(Lower
))
316 return ConstantRange(Lower
, CR
.Upper
);
319 } else if (CR
.Upper
.ult(Lower
)) {
320 if (CR
.Lower
.ult(Lower
))
323 return ConstantRange(CR
.Lower
, Upper
);
325 if (getSetSize().ult(CR
.getSetSize()))
332 /// unionWith - Return the range that results from the union of this range with
333 /// another range. The resultant range is guaranteed to include the elements of
334 /// both sets, but may contain more. For example, [3, 9) union [12,15) is
335 /// [3, 15), which includes 9, 10, and 11, which were not included in either
338 ConstantRange
ConstantRange::unionWith(const ConstantRange
&CR
) const {
339 assert(getBitWidth() == CR
.getBitWidth() &&
340 "ConstantRange types don't agree!");
342 if ( isFullSet() || CR
.isEmptySet()) return *this;
343 if (CR
.isFullSet() || isEmptySet()) return CR
;
345 if (!isWrappedSet() && CR
.isWrappedSet()) return CR
.unionWith(*this);
347 APInt L
= Lower
, U
= Upper
;
349 if (!isWrappedSet() && !CR
.isWrappedSet()) {
357 if (isWrappedSet() && !CR
.isWrappedSet()) {
358 if ((CR
.Lower
.ult(Upper
) && CR
.Upper
.ult(Upper
)) ||
359 (CR
.Lower
.ugt(Lower
) && CR
.Upper
.ugt(Lower
))) {
363 if (CR
.Lower
.ule(Upper
) && Lower
.ule(CR
.Upper
)) {
364 return ConstantRange(getBitWidth());
367 if (CR
.Lower
.ule(Upper
) && CR
.Upper
.ule(Lower
)) {
368 APInt d1
= CR
.Upper
- Upper
, d2
= Lower
- CR
.Upper
;
376 if (Upper
.ult(CR
.Lower
) && CR
.Upper
.ult(Lower
)) {
377 APInt d1
= CR
.Lower
- Upper
, d2
= Lower
- CR
.Upper
;
385 if (Upper
.ult(CR
.Lower
) && Lower
.ult(CR
.Upper
)) {
386 APInt d1
= CR
.Lower
- Upper
, d2
= Lower
- CR
.Lower
;
396 if (isWrappedSet() && CR
.isWrappedSet()) {
397 if (Lower
.ult(CR
.Upper
) || CR
.Lower
.ult(Upper
))
398 return ConstantRange(getBitWidth());
400 if (CR
.Upper
.ugt(U
)) {
404 if (CR
.Lower
.ult(L
)) {
408 if (L
== U
) return ConstantRange(getBitWidth());
411 return ConstantRange(L
, U
);
414 /// zeroExtend - Return a new range in the specified integer type, which must
415 /// be strictly larger than the current type. The returned range will
416 /// correspond to the possible range of values as if the source range had been
418 ConstantRange
ConstantRange::zeroExtend(uint32_t DstTySize
) const {
419 unsigned SrcTySize
= getBitWidth();
420 assert(SrcTySize
< DstTySize
&& "Not a value extension");
422 // Change a source full set into [0, 1 << 8*numbytes)
423 return ConstantRange(APInt(DstTySize
,0), APInt(DstTySize
,1).shl(SrcTySize
));
425 APInt L
= Lower
; L
.zext(DstTySize
);
426 APInt U
= Upper
; U
.zext(DstTySize
);
427 return ConstantRange(L
, U
);
430 /// signExtend - Return a new range in the specified integer type, which must
431 /// be strictly larger than the current type. The returned range will
432 /// correspond to the possible range of values as if the source range had been
434 ConstantRange
ConstantRange::signExtend(uint32_t DstTySize
) const {
435 unsigned SrcTySize
= getBitWidth();
436 assert(SrcTySize
< DstTySize
&& "Not a value extension");
438 return ConstantRange(APInt::getHighBitsSet(DstTySize
,DstTySize
-SrcTySize
+1),
439 APInt::getLowBitsSet(DstTySize
, SrcTySize
-1));
442 APInt L
= Lower
; L
.sext(DstTySize
);
443 APInt U
= Upper
; U
.sext(DstTySize
);
444 return ConstantRange(L
, U
);
447 /// truncate - Return a new range in the specified integer type, which must be
448 /// strictly smaller than the current type. The returned range will
449 /// correspond to the possible range of values as if the source range had been
450 /// truncated to the specified type.
451 ConstantRange
ConstantRange::truncate(uint32_t DstTySize
) const {
452 unsigned SrcTySize
= getBitWidth();
453 assert(SrcTySize
> DstTySize
&& "Not a value truncation");
454 APInt
Size(APInt::getLowBitsSet(SrcTySize
, DstTySize
));
455 if (isFullSet() || getSetSize().ugt(Size
))
456 return ConstantRange(DstTySize
);
458 APInt L
= Lower
; L
.trunc(DstTySize
);
459 APInt U
= Upper
; U
.trunc(DstTySize
);
460 return ConstantRange(L
, U
);
463 /// print - Print out the bounds to a stream...
465 void ConstantRange::print(std::ostream
&OS
) const {
466 OS
<< "[" << Lower
.toStringSigned(10) << ","
467 << Upper
.toStringSigned(10) << " )";
470 /// dump - Allow printing from a debugger easily...
472 void ConstantRange::dump() const {