add some missing quotes in debug output
[llvm/avr.git] / lib / Support / ConstantRange.cpp
blob423e90d993527e818dae72259aa65f0b297965fc
1 //===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
18 // [T, F) = {T}
19 // [F, T) = {F}
20 // [T, T) = {F, T} = Full set
22 //===----------------------------------------------------------------------===//
24 #include "llvm/Support/ConstantRange.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Instructions.h"
27 using namespace llvm;
29 /// Initialize a full (the default) or empty set for the specified type.
30 ///
31 ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) {
32 if (Full)
33 Lower = Upper = APInt::getMaxValue(BitWidth);
34 else
35 Lower = Upper = APInt::getMinValue(BitWidth);
38 /// Initialize a range to hold the single specified value.
39 ///
40 ConstantRange::ConstantRange(const APInt & V) : Lower(V), Upper(V + 1) {}
42 ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
43 Lower(L), Upper(U) {
44 assert(L.getBitWidth() == U.getBitWidth() &&
45 "ConstantRange with unequal bit widths");
46 assert((L != U || (L.isMaxValue() || L.isMinValue())) &&
47 "Lower == Upper, but they aren't min or max value!");
50 ConstantRange ConstantRange::makeICmpRegion(unsigned Pred,
51 const ConstantRange &CR) {
52 uint32_t W = CR.getBitWidth();
53 switch (Pred) {
54 default: assert(!"Invalid ICmp predicate to makeICmpRegion()");
55 case ICmpInst::ICMP_EQ:
56 return CR;
57 case ICmpInst::ICMP_NE:
58 if (CR.isSingleElement())
59 return ConstantRange(CR.getUpper(), CR.getLower());
60 return ConstantRange(W);
61 case ICmpInst::ICMP_ULT:
62 return ConstantRange(APInt::getMinValue(W), CR.getUnsignedMax());
63 case ICmpInst::ICMP_SLT:
64 return ConstantRange(APInt::getSignedMinValue(W), CR.getSignedMax());
65 case ICmpInst::ICMP_ULE: {
66 APInt UMax(CR.getUnsignedMax());
67 if (UMax.isMaxValue())
68 return ConstantRange(W);
69 return ConstantRange(APInt::getMinValue(W), UMax + 1);
71 case ICmpInst::ICMP_SLE: {
72 APInt SMax(CR.getSignedMax());
73 if (SMax.isMaxSignedValue() || (SMax+1).isMaxSignedValue())
74 return ConstantRange(W);
75 return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
77 case ICmpInst::ICMP_UGT:
78 return ConstantRange(CR.getUnsignedMin() + 1, APInt::getNullValue(W));
79 case ICmpInst::ICMP_SGT:
80 return ConstantRange(CR.getSignedMin() + 1,
81 APInt::getSignedMinValue(W));
82 case ICmpInst::ICMP_UGE: {
83 APInt UMin(CR.getUnsignedMin());
84 if (UMin.isMinValue())
85 return ConstantRange(W);
86 return ConstantRange(UMin, APInt::getNullValue(W));
88 case ICmpInst::ICMP_SGE: {
89 APInt SMin(CR.getSignedMin());
90 if (SMin.isMinSignedValue())
91 return ConstantRange(W);
92 return ConstantRange(SMin, APInt::getSignedMinValue(W));
97 /// isFullSet - Return true if this set contains all of the elements possible
98 /// for this data-type
99 bool ConstantRange::isFullSet() const {
100 return Lower == Upper && Lower.isMaxValue();
103 /// isEmptySet - Return true if this set contains no members.
105 bool ConstantRange::isEmptySet() const {
106 return Lower == Upper && Lower.isMinValue();
109 /// isWrappedSet - Return true if this set wraps around the top of the range,
110 /// for example: [100, 8)
112 bool ConstantRange::isWrappedSet() const {
113 return Lower.ugt(Upper);
116 /// getSetSize - Return the number of elements in this set.
118 APInt ConstantRange::getSetSize() const {
119 if (isEmptySet())
120 return APInt(getBitWidth(), 0);
121 if (getBitWidth() == 1) {
122 if (Lower != Upper) // One of T or F in the set...
123 return APInt(2, 1);
124 return APInt(2, 2); // Must be full set...
127 // Simply subtract the bounds...
128 return Upper - Lower;
131 /// getUnsignedMax - Return the largest unsigned value contained in the
132 /// ConstantRange.
134 APInt ConstantRange::getUnsignedMax() const {
135 if (isFullSet() || isWrappedSet())
136 return APInt::getMaxValue(getBitWidth());
137 else
138 return getUpper() - 1;
141 /// getUnsignedMin - Return the smallest unsigned value contained in the
142 /// ConstantRange.
144 APInt ConstantRange::getUnsignedMin() const {
145 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
146 return APInt::getMinValue(getBitWidth());
147 else
148 return getLower();
151 /// getSignedMax - Return the largest signed value contained in the
152 /// ConstantRange.
154 APInt ConstantRange::getSignedMax() const {
155 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
156 if (!isWrappedSet()) {
157 if (getLower().sle(getUpper() - 1))
158 return getUpper() - 1;
159 else
160 return SignedMax;
161 } else {
162 if (getLower().isNegative() == getUpper().isNegative())
163 return SignedMax;
164 else
165 return getUpper() - 1;
169 /// getSignedMin - Return the smallest signed value contained in the
170 /// ConstantRange.
172 APInt ConstantRange::getSignedMin() const {
173 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
174 if (!isWrappedSet()) {
175 if (getLower().sle(getUpper() - 1))
176 return getLower();
177 else
178 return SignedMin;
179 } else {
180 if ((getUpper() - 1).slt(getLower())) {
181 if (getUpper() != SignedMin)
182 return SignedMin;
183 else
184 return getLower();
185 } else {
186 return getLower();
191 /// contains - Return true if the specified value is in the set.
193 bool ConstantRange::contains(const APInt &V) const {
194 if (Lower == Upper)
195 return isFullSet();
197 if (!isWrappedSet())
198 return Lower.ule(V) && V.ult(Upper);
199 else
200 return Lower.ule(V) || V.ult(Upper);
203 /// contains - Return true if the argument is a subset of this range.
204 /// Two equal set contain each other. The empty set is considered to be
205 /// contained by all other sets.
207 bool ConstantRange::contains(const ConstantRange &Other) const {
208 if (isFullSet()) return true;
209 if (Other.isFullSet()) return false;
210 if (Other.isEmptySet()) return true;
211 if (isEmptySet()) return false;
213 if (!isWrappedSet()) {
214 if (Other.isWrappedSet())
215 return false;
217 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
220 if (!Other.isWrappedSet())
221 return Other.getUpper().ule(Upper) ||
222 Lower.ule(Other.getLower());
224 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
227 /// subtract - Subtract the specified constant from the endpoints of this
228 /// constant range.
229 ConstantRange ConstantRange::subtract(const APInt &Val) const {
230 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
231 // If the set is empty or full, don't modify the endpoints.
232 if (Lower == Upper)
233 return *this;
234 return ConstantRange(Lower - Val, Upper - Val);
238 // intersect1Wrapped - This helper function is used to intersect two ranges when
239 // it is known that LHS is wrapped and RHS isn't.
241 ConstantRange
242 ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
243 const ConstantRange &RHS) {
244 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
246 // Check to see if we overlap on the Left side of RHS...
248 if (RHS.Lower.ult(LHS.Upper)) {
249 // We do overlap on the left side of RHS, see if we overlap on the right of
250 // RHS...
251 if (RHS.Upper.ugt(LHS.Lower)) {
252 // Ok, the result overlaps on both the left and right sides. See if the
253 // resultant interval will be smaller if we wrap or not...
255 if (LHS.getSetSize().ult(RHS.getSetSize()))
256 return LHS;
257 else
258 return RHS;
260 } else {
261 // No overlap on the right, just on the left.
262 return ConstantRange(RHS.Lower, LHS.Upper);
264 } else {
265 // We don't overlap on the left side of RHS, see if we overlap on the right
266 // of RHS...
267 if (RHS.Upper.ugt(LHS.Lower)) {
268 // Simple overlap...
269 return ConstantRange(LHS.Lower, RHS.Upper);
270 } else {
271 // No overlap...
272 return ConstantRange(LHS.getBitWidth(), false);
277 /// intersectWith - Return the range that results from the intersection of this
278 /// range with another range. The resultant range is guaranteed to include all
279 /// elements contained in both input ranges, and to have the smallest possible
280 /// set size that does so. Because there may be two intersections with the
281 /// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
282 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
283 assert(getBitWidth() == CR.getBitWidth() &&
284 "ConstantRange types don't agree!");
286 // Handle common cases.
287 if ( isEmptySet() || CR.isFullSet()) return *this;
288 if (CR.isEmptySet() || isFullSet()) return CR;
290 if (!isWrappedSet() && CR.isWrappedSet())
291 return CR.intersectWith(*this);
293 if (!isWrappedSet() && !CR.isWrappedSet()) {
294 if (Lower.ult(CR.Lower)) {
295 if (Upper.ule(CR.Lower))
296 return ConstantRange(getBitWidth(), false);
298 if (Upper.ult(CR.Upper))
299 return ConstantRange(CR.Lower, Upper);
301 return CR;
302 } else {
303 if (Upper.ult(CR.Upper))
304 return *this;
306 if (Lower.ult(CR.Upper))
307 return ConstantRange(Lower, CR.Upper);
309 return ConstantRange(getBitWidth(), false);
313 if (isWrappedSet() && !CR.isWrappedSet()) {
314 if (CR.Lower.ult(Upper)) {
315 if (CR.Upper.ult(Upper))
316 return CR;
318 if (CR.Upper.ult(Lower))
319 return ConstantRange(CR.Lower, Upper);
321 if (getSetSize().ult(CR.getSetSize()))
322 return *this;
323 else
324 return CR;
325 } else if (CR.Lower.ult(Lower)) {
326 if (CR.Upper.ule(Lower))
327 return ConstantRange(getBitWidth(), false);
329 return ConstantRange(Lower, CR.Upper);
331 return CR;
334 if (CR.Upper.ult(Upper)) {
335 if (CR.Lower.ult(Upper)) {
336 if (getSetSize().ult(CR.getSetSize()))
337 return *this;
338 else
339 return CR;
342 if (CR.Lower.ult(Lower))
343 return ConstantRange(Lower, CR.Upper);
345 return CR;
346 } else if (CR.Upper.ult(Lower)) {
347 if (CR.Lower.ult(Lower))
348 return *this;
350 return ConstantRange(CR.Lower, Upper);
352 if (getSetSize().ult(CR.getSetSize()))
353 return *this;
354 else
355 return CR;
359 /// unionWith - Return the range that results from the union of this range with
360 /// another range. The resultant range is guaranteed to include the elements of
361 /// both sets, but may contain more. For example, [3, 9) union [12,15) is
362 /// [3, 15), which includes 9, 10, and 11, which were not included in either
363 /// set before.
365 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
366 assert(getBitWidth() == CR.getBitWidth() &&
367 "ConstantRange types don't agree!");
369 if ( isFullSet() || CR.isEmptySet()) return *this;
370 if (CR.isFullSet() || isEmptySet()) return CR;
372 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
374 if (!isWrappedSet() && !CR.isWrappedSet()) {
375 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
376 // If the two ranges are disjoint, find the smaller gap and bridge it.
377 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
378 if (d1.ult(d2))
379 return ConstantRange(Lower, CR.Upper);
380 else
381 return ConstantRange(CR.Lower, Upper);
384 APInt L = Lower, U = Upper;
385 if (CR.Lower.ult(L))
386 L = CR.Lower;
387 if ((CR.Upper - 1).ugt(U - 1))
388 U = CR.Upper;
390 if (L == 0 && U == 0)
391 return ConstantRange(getBitWidth());
393 return ConstantRange(L, U);
396 if (!CR.isWrappedSet()) {
397 // ------U L----- and ------U L----- : this
398 // L--U L--U : CR
399 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
400 return *this;
402 // ------U L----- : this
403 // L---------U : CR
404 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
405 return ConstantRange(getBitWidth());
407 // ----U L---- : this
408 // L---U : CR
409 // <d1> <d2>
410 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
411 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
412 if (d1.ult(d2))
413 return ConstantRange(Lower, CR.Upper);
414 else
415 return ConstantRange(CR.Lower, Upper);
418 // ----U L----- : this
419 // L----U : CR
420 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
421 return ConstantRange(CR.Lower, Upper);
423 // ------U L---- : this
424 // L-----U : CR
425 if (CR.Lower.ult(Upper) && CR.Upper.ult(Lower))
426 return ConstantRange(Lower, CR.Upper);
429 assert(isWrappedSet() && CR.isWrappedSet() &&
430 "ConstantRange::unionWith missed wrapped union unwrapped case");
432 // ------U L---- and ------U L---- : this
433 // -U L----------- and ------------U L : CR
434 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
435 return ConstantRange(getBitWidth());
437 APInt L = Lower, U = Upper;
438 if (CR.Upper.ugt(U))
439 U = CR.Upper;
440 if (CR.Lower.ult(L))
441 L = CR.Lower;
443 return ConstantRange(L, U);
446 /// zeroExtend - Return a new range in the specified integer type, which must
447 /// be strictly larger than the current type. The returned range will
448 /// correspond to the possible range of values as if the source range had been
449 /// zero extended.
450 ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
451 unsigned SrcTySize = getBitWidth();
452 assert(SrcTySize < DstTySize && "Not a value extension");
453 if (isFullSet())
454 // Change a source full set into [0, 1 << 8*numbytes)
455 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
457 APInt L = Lower; L.zext(DstTySize);
458 APInt U = Upper; U.zext(DstTySize);
459 return ConstantRange(L, U);
462 /// signExtend - Return a new range in the specified integer type, which must
463 /// be strictly larger than the current type. The returned range will
464 /// correspond to the possible range of values as if the source range had been
465 /// sign extended.
466 ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
467 unsigned SrcTySize = getBitWidth();
468 assert(SrcTySize < DstTySize && "Not a value extension");
469 if (isFullSet()) {
470 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
471 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
474 APInt L = Lower; L.sext(DstTySize);
475 APInt U = Upper; U.sext(DstTySize);
476 return ConstantRange(L, U);
479 /// truncate - Return a new range in the specified integer type, which must be
480 /// strictly smaller than the current type. The returned range will
481 /// correspond to the possible range of values as if the source range had been
482 /// truncated to the specified type.
483 ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
484 unsigned SrcTySize = getBitWidth();
485 assert(SrcTySize > DstTySize && "Not a value truncation");
486 APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
487 if (isFullSet() || getSetSize().ugt(Size))
488 return ConstantRange(DstTySize);
490 APInt L = Lower; L.trunc(DstTySize);
491 APInt U = Upper; U.trunc(DstTySize);
492 return ConstantRange(L, U);
495 ConstantRange
496 ConstantRange::add(const ConstantRange &Other) const {
497 if (isEmptySet() || Other.isEmptySet())
498 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
499 if (isFullSet() || Other.isFullSet())
500 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
502 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
503 APInt NewLower = getLower() + Other.getLower();
504 APInt NewUpper = getUpper() + Other.getUpper() - 1;
505 if (NewLower == NewUpper)
506 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
508 ConstantRange X = ConstantRange(NewLower, NewUpper);
509 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
510 // We've wrapped, therefore, full set.
511 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
513 return X;
516 ConstantRange
517 ConstantRange::multiply(const ConstantRange &Other) const {
518 if (isEmptySet() || Other.isEmptySet())
519 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
520 if (isFullSet() || Other.isFullSet())
521 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
523 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
524 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
525 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
526 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
528 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
529 this_max * Other_max + 1);
530 return Result_zext.truncate(getBitWidth());
533 ConstantRange
534 ConstantRange::smax(const ConstantRange &Other) const {
535 // X smax Y is: range(smax(X_smin, Y_smin),
536 // smax(X_smax, Y_smax))
537 if (isEmptySet() || Other.isEmptySet())
538 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
539 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
540 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
541 if (NewU == NewL)
542 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
543 return ConstantRange(NewL, NewU);
546 ConstantRange
547 ConstantRange::umax(const ConstantRange &Other) const {
548 // X umax Y is: range(umax(X_umin, Y_umin),
549 // umax(X_umax, Y_umax))
550 if (isEmptySet() || Other.isEmptySet())
551 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
552 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
553 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
554 if (NewU == NewL)
555 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
556 return ConstantRange(NewL, NewU);
559 ConstantRange
560 ConstantRange::udiv(const ConstantRange &RHS) const {
561 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
562 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
563 if (RHS.isFullSet())
564 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
566 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
568 APInt RHS_umin = RHS.getUnsignedMin();
569 if (RHS_umin == 0) {
570 // We want the lowest value in RHS excluding zero. Usually that would be 1
571 // except for a range in the form of [X, 1) in which case it would be X.
572 if (RHS.getUpper() == 1)
573 RHS_umin = RHS.getLower();
574 else
575 RHS_umin = APInt(getBitWidth(), 1);
578 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
580 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
581 // this could occur.
582 if (Lower == Upper)
583 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
585 return ConstantRange(Lower, Upper);
588 /// print - Print out the bounds to a stream...
590 void ConstantRange::print(raw_ostream &OS) const {
591 OS << "[" << Lower << "," << Upper << ")";
594 /// dump - Allow printing from a debugger easily...
596 void ConstantRange::dump() const {
597 print(errs());