blob: 5f3d6f8db24e87627b1c00e065f08d5c1988d372 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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):
16//
17// [F, F) = {} = Empty set
18// [T, F) = {T}
19// [F, T) = {F}
20// [T, T) = {F, T} = Full set
21//
22//===----------------------------------------------------------------------===//
23
24#include "llvm/Support/ConstantRange.h"
Chris Lattner1fefaac2008-08-23 22:23:09 +000025#include "llvm/Support/raw_ostream.h"
Nick Lewyckyc21feac2009-07-11 06:15:39 +000026#include "llvm/Instructions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027using namespace llvm;
28
29/// Initialize a full (the default) or empty set for the specified type.
30///
Dan Gohman5d0fb0b2009-07-09 23:16:10 +000031ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032 if (Full)
33 Lower = Upper = APInt::getMaxValue(BitWidth);
34 else
35 Lower = Upper = APInt::getMinValue(BitWidth);
36}
37
38/// Initialize a range to hold the single specified value.
39///
Dan Gohman5d0fb0b2009-07-09 23:16:10 +000040ConstantRange::ConstantRange(const APInt & V) : Lower(V), Upper(V + 1) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041
Dan Gohman5d0fb0b2009-07-09 23:16:10 +000042ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
43 Lower(L), Upper(U) {
44 assert(L.getBitWidth() == U.getBitWidth() &&
45 "ConstantRange with unequal bit widths");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 assert((L != U || (L.isMaxValue() || L.isMinValue())) &&
47 "Lower == Upper, but they aren't min or max value!");
48}
49
Nick Lewyckyc21feac2009-07-11 06:15:39 +000050ConstantRange 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:
Nick Lewyckyc1d93112009-07-11 17:04:01 +000056 return CR;
Nick Lewyckyc21feac2009-07-11 06:15:39 +000057 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);
70 }
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);
76 }
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));
87 }
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));
93 }
94 }
95}
96
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097/// isFullSet - Return true if this set contains all of the elements possible
98/// for this data-type
99bool ConstantRange::isFullSet() const {
100 return Lower == Upper && Lower.isMaxValue();
101}
102
103/// isEmptySet - Return true if this set contains no members.
104///
105bool ConstantRange::isEmptySet() const {
106 return Lower == Upper && Lower.isMinValue();
107}
108
109/// isWrappedSet - Return true if this set wraps around the top of the range,
110/// for example: [100, 8)
111///
112bool ConstantRange::isWrappedSet() const {
113 return Lower.ugt(Upper);
114}
115
116/// getSetSize - Return the number of elements in this set.
117///
118APInt 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...
125 }
126
127 // Simply subtract the bounds...
128 return Upper - Lower;
129}
130
131/// getUnsignedMax - Return the largest unsigned value contained in the
132/// ConstantRange.
133///
134APInt ConstantRange::getUnsignedMax() const {
135 if (isFullSet() || isWrappedSet())
136 return APInt::getMaxValue(getBitWidth());
137 else
138 return getUpper() - 1;
139}
140
141/// getUnsignedMin - Return the smallest unsigned value contained in the
142/// ConstantRange.
143///
144APInt ConstantRange::getUnsignedMin() const {
145 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
146 return APInt::getMinValue(getBitWidth());
147 else
148 return getLower();
149}
150
151/// getSignedMax - Return the largest signed value contained in the
152/// ConstantRange.
153///
154APInt 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 {
Nick Lewycky337b3c22009-07-13 04:50:21 +0000162 if (getLower().isNegative() == getUpper().isNegative())
163 return SignedMax;
164 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 return getUpper() - 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 }
167}
168
169/// getSignedMin - Return the smallest signed value contained in the
170/// ConstantRange.
171///
172APInt 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();
187 }
188 }
189}
190
191/// contains - Return true if the specified value is in the set.
192///
193bool ConstantRange::contains(const APInt &V) const {
194 if (Lower == Upper)
195 return isFullSet();
196
197 if (!isWrappedSet())
198 return Lower.ule(V) && V.ult(Upper);
199 else
200 return Lower.ule(V) || V.ult(Upper);
201}
202
Nick Lewyckyc21feac2009-07-11 06:15:39 +0000203/// 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.
206///
207bool 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;
212
213 if (!isWrappedSet()) {
214 if (Other.isWrappedSet())
215 return false;
216
217 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
218 }
219
220 if (!Other.isWrappedSet())
221 return Other.getUpper().ule(Upper) ||
222 Lower.ule(Other.getLower());
223
224 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
225}
226
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227/// subtract - Subtract the specified constant from the endpoints of this
228/// constant range.
229ConstantRange 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);
235}
236
237
238// intersect1Wrapped - This helper function is used to intersect two ranges when
239// it is known that LHS is wrapped and RHS isn't.
240//
241ConstantRange
242ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
243 const ConstantRange &RHS) {
244 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
245
246 // Check to see if we overlap on the Left side of RHS...
247 //
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...
254 //
255 if (LHS.getSetSize().ult(RHS.getSetSize()))
256 return LHS;
257 else
258 return RHS;
259
260 } else {
261 // No overlap on the right, just on the left.
262 return ConstantRange(RHS.Lower, LHS.Upper);
263 }
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);
273 }
274 }
275}
276
277/// intersectWith - Return the range that results from the intersection of this
Nick Lewycky50695f12009-07-18 06:34:42 +0000278/// 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).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
283 assert(getBitWidth() == CR.getBitWidth() &&
284 "ConstantRange types don't agree!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285
286 // Handle common cases.
287 if ( isEmptySet() || CR.isFullSet()) return *this;
288 if (CR.isEmptySet() || isFullSet()) return CR;
289
290 if (!isWrappedSet() && CR.isWrappedSet())
Nick Lewycky50695f12009-07-18 06:34:42 +0000291 return CR.intersectWith(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292
293 if (!isWrappedSet() && !CR.isWrappedSet()) {
294 if (Lower.ult(CR.Lower)) {
295 if (Upper.ule(CR.Lower))
296 return ConstantRange(getBitWidth(), false);
297
298 if (Upper.ult(CR.Upper))
299 return ConstantRange(CR.Lower, Upper);
300
301 return CR;
302 } else {
303 if (Upper.ult(CR.Upper))
304 return *this;
305
306 if (Lower.ult(CR.Upper))
307 return ConstantRange(Lower, CR.Upper);
308
309 return ConstantRange(getBitWidth(), false);
310 }
311 }
312
313 if (isWrappedSet() && !CR.isWrappedSet()) {
314 if (CR.Lower.ult(Upper)) {
315 if (CR.Upper.ult(Upper))
316 return CR;
317
318 if (CR.Upper.ult(Lower))
319 return ConstantRange(CR.Lower, Upper);
320
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);
328
329 return ConstantRange(Lower, CR.Upper);
330 }
331 return CR;
332 }
333
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;
340 }
341
342 if (CR.Lower.ult(Lower))
343 return ConstantRange(Lower, CR.Upper);
344
345 return CR;
346 } else if (CR.Upper.ult(Lower)) {
347 if (CR.Lower.ult(Lower))
348 return *this;
349
350 return ConstantRange(CR.Lower, Upper);
351 }
352 if (getSetSize().ult(CR.getSetSize()))
353 return *this;
354 else
355 return CR;
356}
357
358
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.
364///
365ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
366 assert(getBitWidth() == CR.getBitWidth() &&
367 "ConstantRange types don't agree!");
368
369 if ( isFullSet() || CR.isEmptySet()) return *this;
370 if (CR.isFullSet() || isEmptySet()) return CR;
371
372 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
373
374 APInt L = Lower, U = Upper;
375
376 if (!isWrappedSet() && !CR.isWrappedSet()) {
377 if (CR.Lower.ult(L))
378 L = CR.Lower;
379
380 if (CR.Upper.ugt(U))
381 U = CR.Upper;
382 }
383
384 if (isWrappedSet() && !CR.isWrappedSet()) {
385 if ((CR.Lower.ult(Upper) && CR.Upper.ult(Upper)) ||
386 (CR.Lower.ugt(Lower) && CR.Upper.ugt(Lower))) {
387 return *this;
388 }
389
390 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) {
391 return ConstantRange(getBitWidth());
392 }
393
394 if (CR.Lower.ule(Upper) && CR.Upper.ule(Lower)) {
395 APInt d1 = CR.Upper - Upper, d2 = Lower - CR.Upper;
396 if (d1.ult(d2)) {
397 U = CR.Upper;
398 } else {
399 L = CR.Upper;
400 }
401 }
402
403 if (Upper.ult(CR.Lower) && CR.Upper.ult(Lower)) {
404 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
405 if (d1.ult(d2)) {
406 U = CR.Lower + 1;
407 } else {
408 L = CR.Upper - 1;
409 }
410 }
411
412 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) {
413 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Lower;
414
415 if (d1.ult(d2)) {
416 U = CR.Lower + 1;
417 } else {
418 L = CR.Lower;
419 }
420 }
421 }
422
423 if (isWrappedSet() && CR.isWrappedSet()) {
424 if (Lower.ult(CR.Upper) || CR.Lower.ult(Upper))
425 return ConstantRange(getBitWidth());
426
427 if (CR.Upper.ugt(U)) {
428 U = CR.Upper;
429 }
430
431 if (CR.Lower.ult(L)) {
432 L = CR.Lower;
433 }
434
435 if (L == U) return ConstantRange(getBitWidth());
436 }
437
438 return ConstantRange(L, U);
439}
440
441/// zeroExtend - Return a new range in the specified integer type, which must
442/// be strictly larger than the current type. The returned range will
443/// correspond to the possible range of values as if the source range had been
444/// zero extended.
445ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
446 unsigned SrcTySize = getBitWidth();
447 assert(SrcTySize < DstTySize && "Not a value extension");
448 if (isFullSet())
449 // Change a source full set into [0, 1 << 8*numbytes)
450 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
451
452 APInt L = Lower; L.zext(DstTySize);
453 APInt U = Upper; U.zext(DstTySize);
454 return ConstantRange(L, U);
455}
456
457/// signExtend - Return a new range in the specified integer type, which must
458/// be strictly larger than the current type. The returned range will
459/// correspond to the possible range of values as if the source range had been
460/// sign extended.
461ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
462 unsigned SrcTySize = getBitWidth();
463 assert(SrcTySize < DstTySize && "Not a value extension");
464 if (isFullSet()) {
465 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
Nick Lewycky5c94ac92009-07-13 04:17:23 +0000466 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 }
468
469 APInt L = Lower; L.sext(DstTySize);
470 APInt U = Upper; U.sext(DstTySize);
471 return ConstantRange(L, U);
472}
473
474/// truncate - Return a new range in the specified integer type, which must be
475/// strictly smaller than the current type. The returned range will
476/// correspond to the possible range of values as if the source range had been
477/// truncated to the specified type.
478ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
479 unsigned SrcTySize = getBitWidth();
480 assert(SrcTySize > DstTySize && "Not a value truncation");
481 APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
482 if (isFullSet() || getSetSize().ugt(Size))
483 return ConstantRange(DstTySize);
484
485 APInt L = Lower; L.trunc(DstTySize);
486 APInt U = Upper; U.trunc(DstTySize);
487 return ConstantRange(L, U);
488}
489
Dan Gohman73670eb2009-07-09 22:07:27 +0000490ConstantRange
491ConstantRange::add(const ConstantRange &Other) const {
492 if (isEmptySet() || Other.isEmptySet())
493 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewycky70c5ab82009-07-13 02:49:08 +0000494 if (isFullSet() || Other.isFullSet())
495 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohman73670eb2009-07-09 22:07:27 +0000496
497 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
498 APInt NewLower = getLower() + Other.getLower();
499 APInt NewUpper = getUpper() + Other.getUpper() - 1;
500 if (NewLower == NewUpper)
501 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
502
503 ConstantRange X = ConstantRange(NewLower, NewUpper);
504 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
505 // We've wrapped, therefore, full set.
506 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
507
508 return X;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509}
510
Dan Gohman73670eb2009-07-09 22:07:27 +0000511ConstantRange
512ConstantRange::multiply(const ConstantRange &Other) const {
Nick Lewycky5c21d712009-07-12 02:19:05 +0000513 if (isEmptySet() || Other.isEmptySet())
514 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
515 if (isFullSet() || Other.isFullSet())
516 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
517
Nick Lewycky08ee8812009-07-13 03:27:41 +0000518 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
519 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
520 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
521 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
Nick Lewycky5c21d712009-07-12 02:19:05 +0000522
Nick Lewycky08ee8812009-07-13 03:27:41 +0000523 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
524 this_max * Other_max + 1);
Nick Lewycky5c21d712009-07-12 02:19:05 +0000525 return Result_zext.truncate(getBitWidth());
Dan Gohman73670eb2009-07-09 22:07:27 +0000526}
527
528ConstantRange
529ConstantRange::smax(const ConstantRange &Other) const {
Dan Gohman5d0fb0b2009-07-09 23:16:10 +0000530 // X smax Y is: range(smax(X_smin, Y_smin),
531 // smax(X_smax, Y_smax))
532 if (isEmptySet() || Other.isEmptySet())
533 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohman5d0fb0b2009-07-09 23:16:10 +0000534 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
535 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
536 if (NewU == NewL)
537 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
538 return ConstantRange(NewL, NewU);
Dan Gohman73670eb2009-07-09 22:07:27 +0000539}
540
541ConstantRange
542ConstantRange::umax(const ConstantRange &Other) const {
543 // X umax Y is: range(umax(X_umin, Y_umin),
544 // umax(X_umax, Y_umax))
545 if (isEmptySet() || Other.isEmptySet())
546 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohman73670eb2009-07-09 22:07:27 +0000547 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
548 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
549 if (NewU == NewL)
550 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
551 return ConstantRange(NewL, NewU);
552}
553
554ConstantRange
Nick Lewyckya78be792009-07-12 05:18:18 +0000555ConstantRange::udiv(const ConstantRange &RHS) const {
556 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
557 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
558 if (RHS.isFullSet())
559 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
560
561 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
562
563 APInt RHS_umin = RHS.getUnsignedMin();
564 if (RHS_umin == 0) {
565 // We want the lowest value in RHS excluding zero. Usually that would be 1
566 // except for a range in the form of [X, 1) in which case it would be X.
567 if (RHS.getUpper() == 1)
568 RHS_umin = RHS.getLower();
569 else
570 RHS_umin = APInt(getBitWidth(), 1);
571 }
572
573 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
574
575 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
576 // this could occur.
577 if (Lower == Upper)
578 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
579
580 return ConstantRange(Lower, Upper);
Dan Gohman73670eb2009-07-09 22:07:27 +0000581}
582
Dan Gohman5d0fb0b2009-07-09 23:16:10 +0000583/// print - Print out the bounds to a stream...
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584///
Dan Gohman5d0fb0b2009-07-09 23:16:10 +0000585void ConstantRange::print(raw_ostream &OS) const {
586 OS << "[" << Lower << "," << Upper << ")";
Dan Gohman73670eb2009-07-09 22:07:27 +0000587}
588
Dan Gohman5d0fb0b2009-07-09 23:16:10 +0000589/// dump - Allow printing from a debugger easily...
Dan Gohman73670eb2009-07-09 22:07:27 +0000590///
Dan Gohman5d0fb0b2009-07-09 23:16:10 +0000591void ConstantRange::dump() const {
592 print(errs());
Dan Gohman73670eb2009-07-09 22:07:27 +0000593}
594
Dan Gohman5d0fb0b2009-07-09 23:16:10 +0000595std::ostream &llvm::operator<<(std::ostream &o,
596 const ConstantRange &CR) {
597 raw_os_ostream OS(o);
598 OS << CR;
599 return o;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600}