blob: ddf14e33eed8b3ac76db05a4c8fb0183c5ed1cf2 [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"
David Greenec8c0d622010-01-05 01:28:32 +000025#include "llvm/Support/Debug.h"
Chris Lattner1fefaac2008-08-23 22:23:09 +000026#include "llvm/Support/raw_ostream.h"
Nick Lewyckyc21feac2009-07-11 06:15:39 +000027#include "llvm/Instructions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028using namespace llvm;
29
30/// Initialize a full (the default) or empty set for the specified type.
31///
Dan Gohman5d0fb0b2009-07-09 23:16:10 +000032ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033 if (Full)
34 Lower = Upper = APInt::getMaxValue(BitWidth);
35 else
36 Lower = Upper = APInt::getMinValue(BitWidth);
37}
38
39/// Initialize a range to hold the single specified value.
40///
Dan Gohman5d0fb0b2009-07-09 23:16:10 +000041ConstantRange::ConstantRange(const APInt & V) : Lower(V), Upper(V + 1) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042
Dan Gohman5d0fb0b2009-07-09 23:16:10 +000043ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
44 Lower(L), Upper(U) {
45 assert(L.getBitWidth() == U.getBitWidth() &&
46 "ConstantRange with unequal bit widths");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 assert((L != U || (L.isMaxValue() || L.isMinValue())) &&
48 "Lower == Upper, but they aren't min or max value!");
49}
50
Nick Lewyckyc21feac2009-07-11 06:15:39 +000051ConstantRange ConstantRange::makeICmpRegion(unsigned Pred,
52 const ConstantRange &CR) {
53 uint32_t W = CR.getBitWidth();
54 switch (Pred) {
55 default: assert(!"Invalid ICmp predicate to makeICmpRegion()");
56 case ICmpInst::ICMP_EQ:
Nick Lewyckyc1d93112009-07-11 17:04:01 +000057 return CR;
Nick Lewyckyc21feac2009-07-11 06:15:39 +000058 case ICmpInst::ICMP_NE:
59 if (CR.isSingleElement())
60 return ConstantRange(CR.getUpper(), CR.getLower());
61 return ConstantRange(W);
62 case ICmpInst::ICMP_ULT:
63 return ConstantRange(APInt::getMinValue(W), CR.getUnsignedMax());
64 case ICmpInst::ICMP_SLT:
65 return ConstantRange(APInt::getSignedMinValue(W), CR.getSignedMax());
66 case ICmpInst::ICMP_ULE: {
67 APInt UMax(CR.getUnsignedMax());
68 if (UMax.isMaxValue())
69 return ConstantRange(W);
70 return ConstantRange(APInt::getMinValue(W), UMax + 1);
71 }
72 case ICmpInst::ICMP_SLE: {
73 APInt SMax(CR.getSignedMax());
74 if (SMax.isMaxSignedValue() || (SMax+1).isMaxSignedValue())
75 return ConstantRange(W);
76 return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
77 }
78 case ICmpInst::ICMP_UGT:
79 return ConstantRange(CR.getUnsignedMin() + 1, APInt::getNullValue(W));
80 case ICmpInst::ICMP_SGT:
81 return ConstantRange(CR.getSignedMin() + 1,
82 APInt::getSignedMinValue(W));
83 case ICmpInst::ICMP_UGE: {
84 APInt UMin(CR.getUnsignedMin());
85 if (UMin.isMinValue())
86 return ConstantRange(W);
87 return ConstantRange(UMin, APInt::getNullValue(W));
88 }
89 case ICmpInst::ICMP_SGE: {
90 APInt SMin(CR.getSignedMin());
91 if (SMin.isMinSignedValue())
92 return ConstantRange(W);
93 return ConstantRange(SMin, APInt::getSignedMinValue(W));
94 }
95 }
96}
97
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098/// isFullSet - Return true if this set contains all of the elements possible
99/// for this data-type
100bool ConstantRange::isFullSet() const {
101 return Lower == Upper && Lower.isMaxValue();
102}
103
104/// isEmptySet - Return true if this set contains no members.
105///
106bool ConstantRange::isEmptySet() const {
107 return Lower == Upper && Lower.isMinValue();
108}
109
110/// isWrappedSet - Return true if this set wraps around the top of the range,
111/// for example: [100, 8)
112///
113bool ConstantRange::isWrappedSet() const {
114 return Lower.ugt(Upper);
115}
116
117/// getSetSize - Return the number of elements in this set.
118///
119APInt ConstantRange::getSetSize() const {
120 if (isEmptySet())
121 return APInt(getBitWidth(), 0);
122 if (getBitWidth() == 1) {
123 if (Lower != Upper) // One of T or F in the set...
124 return APInt(2, 1);
125 return APInt(2, 2); // Must be full set...
126 }
127
128 // Simply subtract the bounds...
129 return Upper - Lower;
130}
131
132/// getUnsignedMax - Return the largest unsigned value contained in the
133/// ConstantRange.
134///
135APInt ConstantRange::getUnsignedMax() const {
136 if (isFullSet() || isWrappedSet())
137 return APInt::getMaxValue(getBitWidth());
138 else
139 return getUpper() - 1;
140}
141
142/// getUnsignedMin - Return the smallest unsigned value contained in the
143/// ConstantRange.
144///
145APInt ConstantRange::getUnsignedMin() const {
146 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
147 return APInt::getMinValue(getBitWidth());
148 else
149 return getLower();
150}
151
152/// getSignedMax - Return the largest signed value contained in the
153/// ConstantRange.
154///
155APInt ConstantRange::getSignedMax() const {
156 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
157 if (!isWrappedSet()) {
158 if (getLower().sle(getUpper() - 1))
159 return getUpper() - 1;
160 else
161 return SignedMax;
162 } else {
Nick Lewycky337b3c22009-07-13 04:50:21 +0000163 if (getLower().isNegative() == getUpper().isNegative())
164 return SignedMax;
165 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 return getUpper() - 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 }
168}
169
170/// getSignedMin - Return the smallest signed value contained in the
171/// ConstantRange.
172///
173APInt ConstantRange::getSignedMin() const {
174 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
175 if (!isWrappedSet()) {
176 if (getLower().sle(getUpper() - 1))
177 return getLower();
178 else
179 return SignedMin;
180 } else {
181 if ((getUpper() - 1).slt(getLower())) {
182 if (getUpper() != SignedMin)
183 return SignedMin;
184 else
185 return getLower();
186 } else {
187 return getLower();
188 }
189 }
190}
191
192/// contains - Return true if the specified value is in the set.
193///
194bool ConstantRange::contains(const APInt &V) const {
195 if (Lower == Upper)
196 return isFullSet();
197
198 if (!isWrappedSet())
199 return Lower.ule(V) && V.ult(Upper);
200 else
201 return Lower.ule(V) || V.ult(Upper);
202}
203
Nick Lewyckyc21feac2009-07-11 06:15:39 +0000204/// contains - Return true if the argument is a subset of this range.
205/// Two equal set contain each other. The empty set is considered to be
206/// contained by all other sets.
207///
208bool ConstantRange::contains(const ConstantRange &Other) const {
209 if (isFullSet()) return true;
210 if (Other.isFullSet()) return false;
211 if (Other.isEmptySet()) return true;
212 if (isEmptySet()) return false;
213
214 if (!isWrappedSet()) {
215 if (Other.isWrappedSet())
216 return false;
217
218 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
219 }
220
221 if (!Other.isWrappedSet())
222 return Other.getUpper().ule(Upper) ||
223 Lower.ule(Other.getLower());
224
225 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
226}
227
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228/// subtract - Subtract the specified constant from the endpoints of this
229/// constant range.
230ConstantRange ConstantRange::subtract(const APInt &Val) const {
231 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
232 // If the set is empty or full, don't modify the endpoints.
233 if (Lower == Upper)
234 return *this;
235 return ConstantRange(Lower - Val, Upper - Val);
236}
237
238
239// intersect1Wrapped - This helper function is used to intersect two ranges when
240// it is known that LHS is wrapped and RHS isn't.
241//
242ConstantRange
243ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
244 const ConstantRange &RHS) {
245 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
246
247 // Check to see if we overlap on the Left side of RHS...
248 //
249 if (RHS.Lower.ult(LHS.Upper)) {
250 // We do overlap on the left side of RHS, see if we overlap on the right of
251 // RHS...
252 if (RHS.Upper.ugt(LHS.Lower)) {
253 // Ok, the result overlaps on both the left and right sides. See if the
254 // resultant interval will be smaller if we wrap or not...
255 //
256 if (LHS.getSetSize().ult(RHS.getSetSize()))
257 return LHS;
258 else
259 return RHS;
260
261 } else {
262 // No overlap on the right, just on the left.
263 return ConstantRange(RHS.Lower, LHS.Upper);
264 }
265 } else {
266 // We don't overlap on the left side of RHS, see if we overlap on the right
267 // of RHS...
268 if (RHS.Upper.ugt(LHS.Lower)) {
269 // Simple overlap...
270 return ConstantRange(LHS.Lower, RHS.Upper);
271 } else {
272 // No overlap...
273 return ConstantRange(LHS.getBitWidth(), false);
274 }
275 }
276}
277
278/// intersectWith - Return the range that results from the intersection of this
Nick Lewycky50695f12009-07-18 06:34:42 +0000279/// range with another range. The resultant range is guaranteed to include all
280/// elements contained in both input ranges, and to have the smallest possible
281/// set size that does so. Because there may be two intersections with the
282/// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
284 assert(getBitWidth() == CR.getBitWidth() &&
285 "ConstantRange types don't agree!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286
287 // Handle common cases.
288 if ( isEmptySet() || CR.isFullSet()) return *this;
289 if (CR.isEmptySet() || isFullSet()) return CR;
290
291 if (!isWrappedSet() && CR.isWrappedSet())
Nick Lewycky50695f12009-07-18 06:34:42 +0000292 return CR.intersectWith(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293
294 if (!isWrappedSet() && !CR.isWrappedSet()) {
295 if (Lower.ult(CR.Lower)) {
296 if (Upper.ule(CR.Lower))
297 return ConstantRange(getBitWidth(), false);
298
299 if (Upper.ult(CR.Upper))
300 return ConstantRange(CR.Lower, Upper);
301
302 return CR;
303 } else {
304 if (Upper.ult(CR.Upper))
305 return *this;
306
307 if (Lower.ult(CR.Upper))
308 return ConstantRange(Lower, CR.Upper);
309
310 return ConstantRange(getBitWidth(), false);
311 }
312 }
313
314 if (isWrappedSet() && !CR.isWrappedSet()) {
315 if (CR.Lower.ult(Upper)) {
316 if (CR.Upper.ult(Upper))
317 return CR;
318
319 if (CR.Upper.ult(Lower))
320 return ConstantRange(CR.Lower, Upper);
321
322 if (getSetSize().ult(CR.getSetSize()))
323 return *this;
324 else
325 return CR;
326 } else if (CR.Lower.ult(Lower)) {
327 if (CR.Upper.ule(Lower))
328 return ConstantRange(getBitWidth(), false);
329
330 return ConstantRange(Lower, CR.Upper);
331 }
332 return CR;
333 }
334
335 if (CR.Upper.ult(Upper)) {
336 if (CR.Lower.ult(Upper)) {
337 if (getSetSize().ult(CR.getSetSize()))
338 return *this;
339 else
340 return CR;
341 }
342
343 if (CR.Lower.ult(Lower))
344 return ConstantRange(Lower, CR.Upper);
345
346 return CR;
347 } else if (CR.Upper.ult(Lower)) {
348 if (CR.Lower.ult(Lower))
349 return *this;
350
351 return ConstantRange(CR.Lower, Upper);
352 }
353 if (getSetSize().ult(CR.getSetSize()))
354 return *this;
355 else
356 return CR;
357}
358
359
360/// unionWith - Return the range that results from the union of this range with
361/// another range. The resultant range is guaranteed to include the elements of
362/// both sets, but may contain more. For example, [3, 9) union [12,15) is
363/// [3, 15), which includes 9, 10, and 11, which were not included in either
364/// set before.
365///
366ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
367 assert(getBitWidth() == CR.getBitWidth() &&
368 "ConstantRange types don't agree!");
369
370 if ( isFullSet() || CR.isEmptySet()) return *this;
371 if (CR.isFullSet() || isEmptySet()) return CR;
372
373 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
374
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 if (!isWrappedSet() && !CR.isWrappedSet()) {
Nick Lewyckyf08b8242009-07-19 03:44:35 +0000376 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
377 // If the two ranges are disjoint, find the smaller gap and bridge it.
378 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
379 if (d1.ult(d2))
380 return ConstantRange(Lower, CR.Upper);
381 else
382 return ConstantRange(CR.Lower, Upper);
383 }
384
385 APInt L = Lower, U = Upper;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 if (CR.Lower.ult(L))
387 L = CR.Lower;
Nick Lewyckyf08b8242009-07-19 03:44:35 +0000388 if ((CR.Upper - 1).ugt(U - 1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389 U = CR.Upper;
Nick Lewyckyf08b8242009-07-19 03:44:35 +0000390
391 if (L == 0 && U == 0)
392 return ConstantRange(getBitWidth());
393
394 return ConstantRange(L, U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 }
396
Nick Lewyckyf08b8242009-07-19 03:44:35 +0000397 if (!CR.isWrappedSet()) {
398 // ------U L----- and ------U L----- : this
399 // L--U L--U : CR
400 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 return *this;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402
Nick Lewyckyf08b8242009-07-19 03:44:35 +0000403 // ------U L----- : this
404 // L---------U : CR
405 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406 return ConstantRange(getBitWidth());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407
Nick Lewyckyf08b8242009-07-19 03:44:35 +0000408 // ----U L---- : this
409 // L---U : CR
410 // <d1> <d2>
411 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
Nick Lewyckyf08b8242009-07-19 03:44:35 +0000413 if (d1.ult(d2))
414 return ConstantRange(Lower, CR.Upper);
415 else
416 return ConstantRange(CR.Lower, Upper);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 }
418
Nick Lewyckyf08b8242009-07-19 03:44:35 +0000419 // ----U L----- : this
420 // L----U : CR
421 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
422 return ConstantRange(CR.Lower, Upper);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423
Nick Lewyckyf08b8242009-07-19 03:44:35 +0000424 // ------U L---- : this
425 // L-----U : CR
426 if (CR.Lower.ult(Upper) && CR.Upper.ult(Lower))
427 return ConstantRange(Lower, CR.Upper);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 }
429
Nick Lewyckyf08b8242009-07-19 03:44:35 +0000430 assert(isWrappedSet() && CR.isWrappedSet() &&
431 "ConstantRange::unionWith missed wrapped union unwrapped case");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432
Nick Lewyckyf08b8242009-07-19 03:44:35 +0000433 // ------U L---- and ------U L---- : this
434 // -U L----------- and ------------U L : CR
435 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
436 return ConstantRange(getBitWidth());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437
Nick Lewyckyf08b8242009-07-19 03:44:35 +0000438 APInt L = Lower, U = Upper;
439 if (CR.Upper.ugt(U))
440 U = CR.Upper;
441 if (CR.Lower.ult(L))
442 L = CR.Lower;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443
444 return ConstantRange(L, U);
445}
446
447/// zeroExtend - Return a new range in the specified integer type, which must
448/// be strictly larger than the current type. The returned range will
449/// correspond to the possible range of values as if the source range had been
450/// zero extended.
451ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
452 unsigned SrcTySize = getBitWidth();
453 assert(SrcTySize < DstTySize && "Not a value extension");
454 if (isFullSet())
455 // Change a source full set into [0, 1 << 8*numbytes)
456 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
457
458 APInt L = Lower; L.zext(DstTySize);
459 APInt U = Upper; U.zext(DstTySize);
460 return ConstantRange(L, U);
461}
462
463/// signExtend - Return a new range in the specified integer type, which must
464/// be strictly larger than the current type. The returned range will
465/// correspond to the possible range of values as if the source range had been
466/// sign extended.
467ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
468 unsigned SrcTySize = getBitWidth();
469 assert(SrcTySize < DstTySize && "Not a value extension");
470 if (isFullSet()) {
471 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
Nick Lewycky5c94ac92009-07-13 04:17:23 +0000472 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473 }
474
475 APInt L = Lower; L.sext(DstTySize);
476 APInt U = Upper; U.sext(DstTySize);
477 return ConstantRange(L, U);
478}
479
480/// truncate - Return a new range in the specified integer type, which must be
481/// strictly smaller than the current type. The returned range will
482/// correspond to the possible range of values as if the source range had been
483/// truncated to the specified type.
484ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
485 unsigned SrcTySize = getBitWidth();
486 assert(SrcTySize > DstTySize && "Not a value truncation");
487 APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
488 if (isFullSet() || getSetSize().ugt(Size))
489 return ConstantRange(DstTySize);
490
491 APInt L = Lower; L.trunc(DstTySize);
492 APInt U = Upper; U.trunc(DstTySize);
493 return ConstantRange(L, U);
494}
495
Nuno Lopesd49d0fd2009-11-09 15:36:28 +0000496/// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
497/// value is zero extended, truncated, or left alone to make it that width.
498ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
499 unsigned SrcTySize = getBitWidth();
500 if (SrcTySize > DstTySize)
501 return truncate(DstTySize);
502 else if (SrcTySize < DstTySize)
503 return zeroExtend(DstTySize);
504 else
505 return *this;
506}
507
508/// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
509/// value is sign extended, truncated, or left alone to make it that width.
510ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
511 unsigned SrcTySize = getBitWidth();
512 if (SrcTySize > DstTySize)
513 return truncate(DstTySize);
514 else if (SrcTySize < DstTySize)
515 return signExtend(DstTySize);
516 else
517 return *this;
518}
519
Dan Gohman73670eb2009-07-09 22:07:27 +0000520ConstantRange
521ConstantRange::add(const ConstantRange &Other) const {
522 if (isEmptySet() || Other.isEmptySet())
523 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewycky70c5ab82009-07-13 02:49:08 +0000524 if (isFullSet() || Other.isFullSet())
525 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohman73670eb2009-07-09 22:07:27 +0000526
527 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
528 APInt NewLower = getLower() + Other.getLower();
529 APInt NewUpper = getUpper() + Other.getUpper() - 1;
530 if (NewLower == NewUpper)
531 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
532
533 ConstantRange X = ConstantRange(NewLower, NewUpper);
534 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
535 // We've wrapped, therefore, full set.
536 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
537
538 return X;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539}
540
Dan Gohman73670eb2009-07-09 22:07:27 +0000541ConstantRange
542ConstantRange::multiply(const ConstantRange &Other) const {
Nick Lewycky5c21d712009-07-12 02:19:05 +0000543 if (isEmptySet() || Other.isEmptySet())
544 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
545 if (isFullSet() || Other.isFullSet())
546 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
547
Nick Lewycky08ee8812009-07-13 03:27:41 +0000548 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
549 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
550 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
551 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
Nick Lewycky5c21d712009-07-12 02:19:05 +0000552
Nick Lewycky08ee8812009-07-13 03:27:41 +0000553 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
554 this_max * Other_max + 1);
Nick Lewycky5c21d712009-07-12 02:19:05 +0000555 return Result_zext.truncate(getBitWidth());
Dan Gohman73670eb2009-07-09 22:07:27 +0000556}
557
558ConstantRange
559ConstantRange::smax(const ConstantRange &Other) const {
Dan Gohman5d0fb0b2009-07-09 23:16:10 +0000560 // X smax Y is: range(smax(X_smin, Y_smin),
561 // smax(X_smax, Y_smax))
562 if (isEmptySet() || Other.isEmptySet())
563 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohman5d0fb0b2009-07-09 23:16:10 +0000564 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
565 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
566 if (NewU == NewL)
567 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
568 return ConstantRange(NewL, NewU);
Dan Gohman73670eb2009-07-09 22:07:27 +0000569}
570
571ConstantRange
572ConstantRange::umax(const ConstantRange &Other) const {
573 // X umax Y is: range(umax(X_umin, Y_umin),
574 // umax(X_umax, Y_umax))
575 if (isEmptySet() || Other.isEmptySet())
576 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohman73670eb2009-07-09 22:07:27 +0000577 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
578 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
579 if (NewU == NewL)
580 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
581 return ConstantRange(NewL, NewU);
582}
583
584ConstantRange
Nick Lewyckya78be792009-07-12 05:18:18 +0000585ConstantRange::udiv(const ConstantRange &RHS) const {
586 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
587 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
588 if (RHS.isFullSet())
589 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
590
591 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
592
593 APInt RHS_umin = RHS.getUnsignedMin();
594 if (RHS_umin == 0) {
595 // We want the lowest value in RHS excluding zero. Usually that would be 1
596 // except for a range in the form of [X, 1) in which case it would be X.
597 if (RHS.getUpper() == 1)
598 RHS_umin = RHS.getLower();
599 else
600 RHS_umin = APInt(getBitWidth(), 1);
601 }
602
603 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
604
605 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
606 // this could occur.
607 if (Lower == Upper)
608 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
609
610 return ConstantRange(Lower, Upper);
Dan Gohman73670eb2009-07-09 22:07:27 +0000611}
612
Nuno Lopes1e03e852009-11-12 14:53:53 +0000613ConstantRange
614ConstantRange::shl(const ConstantRange &Amount) const {
615 if (isEmptySet())
616 return *this;
617
618 APInt min = getUnsignedMin() << Amount.getUnsignedMin();
619 APInt max = getUnsignedMax() << Amount.getUnsignedMax();
620
621 // there's no overflow!
Nuno Lopesc07cafe2009-11-12 15:10:33 +0000622 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
Nuno Lopes1e03e852009-11-12 14:53:53 +0000623 if (Zeros.uge(Amount.getUnsignedMax()))
624 return ConstantRange(min, max);
625
626 // FIXME: implement the other tricky cases
627 return ConstantRange(getBitWidth());
628}
629
630ConstantRange
631ConstantRange::ashr(const ConstantRange &Amount) const {
632 if (isEmptySet())
633 return *this;
634
635 APInt min = getUnsignedMax().ashr(Amount.getUnsignedMin());
636 APInt max = getUnsignedMin().ashr(Amount.getUnsignedMax());
637 return ConstantRange(min, max);
638}
639
640ConstantRange
641ConstantRange::lshr(const ConstantRange &Amount) const {
642 if (isEmptySet())
643 return *this;
644
645 APInt min = getUnsignedMax().lshr(Amount.getUnsignedMin());
646 APInt max = getUnsignedMin().lshr(Amount.getUnsignedMax());
647 return ConstantRange(min, max);
648}
649
Dan Gohman5d0fb0b2009-07-09 23:16:10 +0000650/// print - Print out the bounds to a stream...
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651///
Dan Gohman5d0fb0b2009-07-09 23:16:10 +0000652void ConstantRange::print(raw_ostream &OS) const {
653 OS << "[" << Lower << "," << Upper << ")";
Dan Gohman73670eb2009-07-09 22:07:27 +0000654}
655
Dan Gohman5d0fb0b2009-07-09 23:16:10 +0000656/// dump - Allow printing from a debugger easily...
Dan Gohman73670eb2009-07-09 22:07:27 +0000657///
Dan Gohman5d0fb0b2009-07-09 23:16:10 +0000658void ConstantRange::dump() const {
David Greenec8c0d622010-01-05 01:28:32 +0000659 print(dbgs());
Dan Gohman73670eb2009-07-09 22:07:27 +0000660}
661
Chris Lattner397f4562009-08-23 06:03:38 +0000662