blob: 8bab53779528a98b89e6b063b7fce63852ef29bb [file] [log] [blame]
Chris Lattner645e00d2002-09-01 23:53:36 +00001//===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner645e00d2002-09-01 23:53:36 +00009//
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 Lattner944fac72008-08-23 22:23:09 +000025#include "llvm/Support/raw_ostream.h"
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000026#include "llvm/Instructions.h"
Chris Lattner2cdd21c2003-12-14 21:35:53 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner645e00d2002-09-01 23:53:36 +000029/// Initialize a full (the default) or empty set for the specified type.
30///
Dan Gohman38b06442009-07-09 23:16:10 +000031ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) {
Chris Lattner645e00d2002-09-01 23:53:36 +000032 if (Full)
Reid Spencer663e7112007-02-28 17:36:23 +000033 Lower = Upper = APInt::getMaxValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000034 else
Reid Spencer663e7112007-02-28 17:36:23 +000035 Lower = Upper = APInt::getMinValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000036}
37
Chris Lattnerfc33d302004-03-30 00:20:08 +000038/// Initialize a range to hold the single specified value.
39///
Dan Gohman38b06442009-07-09 23:16:10 +000040ConstantRange::ConstantRange(const APInt & V) : Lower(V), Upper(V + 1) {}
Chris Lattner645e00d2002-09-01 23:53:36 +000041
Dan Gohman38b06442009-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");
Zhou Shengc125c002007-04-26 16:42:07 +000046 assert((L != U || (L.isMaxValue() || L.isMinValue())) &&
Reid Spencer663e7112007-02-28 17:36:23 +000047 "Lower == Upper, but they aren't min or max value!");
48}
49
Nick Lewyckybf8c7f02009-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 Lewyckyf067a232009-07-11 17:04:01 +000056 return CR;
Nick Lewyckybf8c7f02009-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
Chris Lattner645e00d2002-09-01 23:53:36 +000097/// isFullSet - Return true if this set contains all of the elements possible
98/// for this data-type
99bool ConstantRange::isFullSet() const {
Zhou Shengc125c002007-04-26 16:42:07 +0000100 return Lower == Upper && Lower.isMaxValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000101}
Misha Brukmanfd939082005-04-21 23:48:37 +0000102
Chris Lattner645e00d2002-09-01 23:53:36 +0000103/// isEmptySet - Return true if this set contains no members.
104///
105bool ConstantRange::isEmptySet() const {
Zhou Shengc125c002007-04-26 16:42:07 +0000106 return Lower == Upper && Lower.isMinValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000107}
108
109/// isWrappedSet - Return true if this set wraps around the top of the range,
110/// for example: [100, 8)
111///
Reid Spencera6e8a952007-03-01 07:54:15 +0000112bool ConstantRange::isWrappedSet() const {
Reid Spencer663e7112007-02-28 17:36:23 +0000113 return Lower.ugt(Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000114}
115
Chris Lattner645e00d2002-09-01 23:53:36 +0000116/// getSetSize - Return the number of elements in this set.
117///
Reid Spencer663e7112007-02-28 17:36:23 +0000118APInt ConstantRange::getSetSize() const {
119 if (isEmptySet())
Reid Spencerbb626a62007-02-28 22:02:48 +0000120 return APInt(getBitWidth(), 0);
121 if (getBitWidth() == 1) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000122 if (Lower != Upper) // One of T or F in the set...
Reid Spencerbb626a62007-02-28 22:02:48 +0000123 return APInt(2, 1);
124 return APInt(2, 2); // Must be full set...
Chris Lattner645e00d2002-09-01 23:53:36 +0000125 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000126
Chris Lattner645e00d2002-09-01 23:53:36 +0000127 // Simply subtract the bounds...
Reid Spencer663e7112007-02-28 17:36:23 +0000128 return Upper - Lower;
Chris Lattner645e00d2002-09-01 23:53:36 +0000129}
130
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000131/// 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 {
Zhou Shengdaacf222007-04-13 05:57:32 +0000155 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000156 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000157 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000158 return getUpper() - 1;
159 else
160 return SignedMax;
161 } else {
162 if ((getUpper() - 1).slt(getLower())) {
163 if (getLower() != SignedMax)
164 return SignedMax;
165 else
166 return getUpper() - 1;
167 } else {
168 return getUpper() - 1;
169 }
170 }
171}
172
173/// getSignedMin - Return the smallest signed value contained in the
174/// ConstantRange.
175///
176APInt ConstantRange::getSignedMin() const {
Zhou Shengdaacf222007-04-13 05:57:32 +0000177 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000178 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000179 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000180 return getLower();
181 else
182 return SignedMin;
183 } else {
184 if ((getUpper() - 1).slt(getLower())) {
185 if (getUpper() != SignedMin)
186 return SignedMin;
187 else
188 return getLower();
189 } else {
190 return getLower();
191 }
192 }
193}
194
Chris Lattnerfc33d302004-03-30 00:20:08 +0000195/// contains - Return true if the specified value is in the set.
196///
Reid Spencera6e8a952007-03-01 07:54:15 +0000197bool ConstantRange::contains(const APInt &V) const {
198 if (Lower == Upper)
199 return isFullSet();
Chris Lattner645e00d2002-09-01 23:53:36 +0000200
Reid Spencera6e8a952007-03-01 07:54:15 +0000201 if (!isWrappedSet())
202 return Lower.ule(V) && V.ult(Upper);
Reid Spencer663e7112007-02-28 17:36:23 +0000203 else
204 return Lower.ule(V) || V.ult(Upper);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000205}
206
Nick Lewyckybf8c7f02009-07-11 06:15:39 +0000207/// contains - Return true if the argument is a subset of this range.
208/// Two equal set contain each other. The empty set is considered to be
209/// contained by all other sets.
210///
211bool ConstantRange::contains(const ConstantRange &Other) const {
212 if (isFullSet()) return true;
213 if (Other.isFullSet()) return false;
214 if (Other.isEmptySet()) return true;
215 if (isEmptySet()) return false;
216
217 if (!isWrappedSet()) {
218 if (Other.isWrappedSet())
219 return false;
220
221 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
222 }
223
224 if (!Other.isWrappedSet())
225 return Other.getUpper().ule(Upper) ||
226 Lower.ule(Other.getLower());
227
228 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
229}
230
Chris Lattnerfc33d302004-03-30 00:20:08 +0000231/// subtract - Subtract the specified constant from the endpoints of this
232/// constant range.
Reid Spencer581b0d42007-02-28 19:57:34 +0000233ConstantRange ConstantRange::subtract(const APInt &Val) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000234 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
Chris Lattnerfc33d302004-03-30 00:20:08 +0000235 // If the set is empty or full, don't modify the endpoints.
Reid Spencer663e7112007-02-28 17:36:23 +0000236 if (Lower == Upper)
237 return *this;
Reid Spencer663e7112007-02-28 17:36:23 +0000238 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000239}
Chris Lattner645e00d2002-09-01 23:53:36 +0000240
241
242// intersect1Wrapped - This helper function is used to intersect two ranges when
243// it is known that LHS is wrapped and RHS isn't.
244//
Reid Spencer663e7112007-02-28 17:36:23 +0000245ConstantRange
246ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
Reid Spencera6e8a952007-03-01 07:54:15 +0000247 const ConstantRange &RHS) {
248 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
Chris Lattner645e00d2002-09-01 23:53:36 +0000249
Chris Lattner645e00d2002-09-01 23:53:36 +0000250 // Check to see if we overlap on the Left side of RHS...
251 //
Reid Spencera6e8a952007-03-01 07:54:15 +0000252 if (RHS.Lower.ult(LHS.Upper)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000253 // We do overlap on the left side of RHS, see if we overlap on the right of
254 // RHS...
Reid Spencera6e8a952007-03-01 07:54:15 +0000255 if (RHS.Upper.ugt(LHS.Lower)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000256 // Ok, the result overlaps on both the left and right sides. See if the
257 // resultant interval will be smaller if we wrap or not...
258 //
Reid Spencer663e7112007-02-28 17:36:23 +0000259 if (LHS.getSetSize().ult(RHS.getSetSize()))
Chris Lattner645e00d2002-09-01 23:53:36 +0000260 return LHS;
261 else
262 return RHS;
263
264 } else {
265 // No overlap on the right, just on the left.
Reid Spencerdc5c1592007-02-28 18:57:32 +0000266 return ConstantRange(RHS.Lower, LHS.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000267 }
Chris Lattner645e00d2002-09-01 23:53:36 +0000268 } else {
269 // We don't overlap on the left side of RHS, see if we overlap on the right
270 // of RHS...
Reid Spencera6e8a952007-03-01 07:54:15 +0000271 if (RHS.Upper.ugt(LHS.Lower)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000272 // Simple overlap...
Reid Spencerdc5c1592007-02-28 18:57:32 +0000273 return ConstantRange(LHS.Lower, RHS.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000274 } else {
275 // No overlap...
Reid Spencerbb626a62007-02-28 22:02:48 +0000276 return ConstantRange(LHS.getBitWidth(), false);
Chris Lattner645e00d2002-09-01 23:53:36 +0000277 }
278 }
279}
280
Nick Lewycky3e051642007-02-11 00:58:49 +0000281/// intersectWith - Return the range that results from the intersection of this
Chris Lattner645e00d2002-09-01 23:53:36 +0000282/// range with another range.
283///
Reid Spencera6e8a952007-03-01 07:54:15 +0000284ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000285 assert(getBitWidth() == CR.getBitWidth() &&
286 "ConstantRange types don't agree!");
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000287 // Handle common special cases
Reid Spencer663e7112007-02-28 17:36:23 +0000288 if (isEmptySet() || CR.isFullSet())
289 return *this;
290 if (isFullSet() || CR.isEmptySet())
291 return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000292
Reid Spencera6e8a952007-03-01 07:54:15 +0000293 if (!isWrappedSet()) {
294 if (!CR.isWrappedSet()) {
Dan Gohmana3755d82009-07-09 22:07:27 +0000295 APInt L = APIntOps::umax(Lower, CR.Lower);
296 APInt U = APIntOps::umin(Upper, CR.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000297
Reid Spencera6e8a952007-03-01 07:54:15 +0000298 if (L.ult(U)) // If range isn't empty...
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000299 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000300 else
Reid Spencerbb626a62007-02-28 22:02:48 +0000301 return ConstantRange(getBitWidth(), false);// Otherwise, empty set
Chris Lattner645e00d2002-09-01 23:53:36 +0000302 } else
Reid Spencera6e8a952007-03-01 07:54:15 +0000303 return intersect1Wrapped(CR, *this);
Chris Lattner645e00d2002-09-01 23:53:36 +0000304 } else { // We know "this" is wrapped...
Reid Spencera6e8a952007-03-01 07:54:15 +0000305 if (!CR.isWrappedSet())
306 return intersect1Wrapped(*this, CR);
Chris Lattner645e00d2002-09-01 23:53:36 +0000307 else {
308 // Both ranges are wrapped...
Dan Gohmana3755d82009-07-09 22:07:27 +0000309 APInt L = APIntOps::umax(Lower, CR.Lower);
310 APInt U = APIntOps::umin(Upper, CR.Upper);
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000311 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000312 }
313 }
314 return *this;
315}
316
Nick Lewycky377b1192007-07-14 02:51:34 +0000317/// maximalIntersectWith - Return the range that results from the intersection
318/// of this range with another range. The resultant range is guaranteed to
Nick Lewycky28753f82007-07-14 17:41:03 +0000319/// include all elements contained in both input ranges, and to have the
320/// smallest possible set size that does so. Because there may be two
321/// intersections with the same set size, A.maximalIntersectWith(B) might not
322/// be equal to B.maximalIntersect(A).
Dan Gohmana3755d82009-07-09 22:07:27 +0000323ConstantRange
324ConstantRange::maximalIntersectWith(const ConstantRange &CR) const {
Nick Lewycky377b1192007-07-14 02:51:34 +0000325 assert(getBitWidth() == CR.getBitWidth() &&
326 "ConstantRange types don't agree!");
327
328 // Handle common cases.
329 if ( isEmptySet() || CR.isFullSet()) return *this;
330 if (CR.isEmptySet() || isFullSet()) return CR;
331
332 if (!isWrappedSet() && CR.isWrappedSet())
333 return CR.maximalIntersectWith(*this);
334
335 if (!isWrappedSet() && !CR.isWrappedSet()) {
336 if (Lower.ult(CR.Lower)) {
337 if (Upper.ule(CR.Lower))
338 return ConstantRange(getBitWidth(), false);
339
340 if (Upper.ult(CR.Upper))
341 return ConstantRange(CR.Lower, Upper);
342
343 return CR;
344 } else {
345 if (Upper.ult(CR.Upper))
346 return *this;
347
348 if (Lower.ult(CR.Upper))
349 return ConstantRange(Lower, CR.Upper);
350
351 return ConstantRange(getBitWidth(), false);
352 }
353 }
354
355 if (isWrappedSet() && !CR.isWrappedSet()) {
356 if (CR.Lower.ult(Upper)) {
357 if (CR.Upper.ult(Upper))
358 return CR;
359
360 if (CR.Upper.ult(Lower))
361 return ConstantRange(CR.Lower, Upper);
362
363 if (getSetSize().ult(CR.getSetSize()))
364 return *this;
365 else
366 return CR;
367 } else if (CR.Lower.ult(Lower)) {
368 if (CR.Upper.ule(Lower))
369 return ConstantRange(getBitWidth(), false);
370
371 return ConstantRange(Lower, CR.Upper);
372 }
373 return CR;
374 }
375
376 if (CR.Upper.ult(Upper)) {
377 if (CR.Lower.ult(Upper)) {
378 if (getSetSize().ult(CR.getSetSize()))
379 return *this;
380 else
381 return CR;
382 }
383
384 if (CR.Lower.ult(Lower))
385 return ConstantRange(Lower, CR.Upper);
386
387 return CR;
388 } else if (CR.Upper.ult(Lower)) {
389 if (CR.Lower.ult(Lower))
390 return *this;
391
392 return ConstantRange(CR.Lower, Upper);
393 }
394 if (getSetSize().ult(CR.getSetSize()))
395 return *this;
396 else
397 return CR;
398}
399
400
Nick Lewycky3e051642007-02-11 00:58:49 +0000401/// unionWith - Return the range that results from the union of this range with
Chris Lattner645e00d2002-09-01 23:53:36 +0000402/// another range. The resultant range is guaranteed to include the elements of
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000403/// both sets, but may contain more. For example, [3, 9) union [12,15) is
404/// [3, 15), which includes 9, 10, and 11, which were not included in either
405/// set before.
Chris Lattner645e00d2002-09-01 23:53:36 +0000406///
Reid Spencera6e8a952007-03-01 07:54:15 +0000407ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000408 assert(getBitWidth() == CR.getBitWidth() &&
409 "ConstantRange types don't agree!");
Chris Lattner645e00d2002-09-01 23:53:36 +0000410
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000411 if ( isFullSet() || CR.isEmptySet()) return *this;
412 if (CR.isFullSet() || isEmptySet()) return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000413
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000414 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
415
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000416 APInt L = Lower, U = Upper;
417
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000418 if (!isWrappedSet() && !CR.isWrappedSet()) {
419 if (CR.Lower.ult(L))
420 L = CR.Lower;
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000421
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000422 if (CR.Upper.ugt(U))
423 U = CR.Upper;
424 }
425
426 if (isWrappedSet() && !CR.isWrappedSet()) {
427 if ((CR.Lower.ult(Upper) && CR.Upper.ult(Upper)) ||
428 (CR.Lower.ugt(Lower) && CR.Upper.ugt(Lower))) {
429 return *this;
430 }
431
432 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper)) {
433 return ConstantRange(getBitWidth());
434 }
435
436 if (CR.Lower.ule(Upper) && CR.Upper.ule(Lower)) {
437 APInt d1 = CR.Upper - Upper, d2 = Lower - CR.Upper;
438 if (d1.ult(d2)) {
439 U = CR.Upper;
440 } else {
441 L = CR.Upper;
442 }
443 }
444
445 if (Upper.ult(CR.Lower) && CR.Upper.ult(Lower)) {
446 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
447 if (d1.ult(d2)) {
448 U = CR.Lower + 1;
449 } else {
450 L = CR.Upper - 1;
451 }
452 }
453
454 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper)) {
455 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Lower;
456
457 if (d1.ult(d2)) {
458 U = CR.Lower + 1;
459 } else {
460 L = CR.Lower;
461 }
462 }
463 }
464
465 if (isWrappedSet() && CR.isWrappedSet()) {
466 if (Lower.ult(CR.Upper) || CR.Lower.ult(Upper))
467 return ConstantRange(getBitWidth());
468
469 if (CR.Upper.ugt(U)) {
470 U = CR.Upper;
471 }
472
473 if (CR.Lower.ult(L)) {
474 L = CR.Lower;
475 }
476
477 if (L == U) return ConstantRange(getBitWidth());
478 }
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000479
480 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000481}
Chris Lattner96f9d722002-09-02 00:18:22 +0000482
Chris Lattnerfc33d302004-03-30 00:20:08 +0000483/// zeroExtend - Return a new range in the specified integer type, which must
484/// be strictly larger than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000485/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000486/// zero extended.
Reid Spencerbb626a62007-02-28 22:02:48 +0000487ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
488 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000489 assert(SrcTySize < DstTySize && "Not a value extension");
Reid Spencerdc5c1592007-02-28 18:57:32 +0000490 if (isFullSet())
Chris Lattnerfc33d302004-03-30 00:20:08 +0000491 // Change a source full set into [0, 1 << 8*numbytes)
Reid Spencerdc5c1592007-02-28 18:57:32 +0000492 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000493
Reid Spencer663e7112007-02-28 17:36:23 +0000494 APInt L = Lower; L.zext(DstTySize);
495 APInt U = Upper; U.zext(DstTySize);
496 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000497}
498
Nick Lewyckye32157c2007-04-07 15:41:33 +0000499/// signExtend - Return a new range in the specified integer type, which must
500/// be strictly larger than the current type. The returned range will
501/// correspond to the possible range of values as if the source range had been
502/// sign extended.
503ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
504 unsigned SrcTySize = getBitWidth();
505 assert(SrcTySize < DstTySize && "Not a value extension");
506 if (isFullSet()) {
507 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
508 APInt::getLowBitsSet(DstTySize, SrcTySize-1));
509 }
510
511 APInt L = Lower; L.sext(DstTySize);
512 APInt U = Upper; U.sext(DstTySize);
513 return ConstantRange(L, U);
514}
515
Chris Lattnerfc33d302004-03-30 00:20:08 +0000516/// truncate - Return a new range in the specified integer type, which must be
517/// strictly smaller than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000518/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000519/// truncated to the specified type.
Reid Spencerbb626a62007-02-28 22:02:48 +0000520ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
521 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000522 assert(SrcTySize > DstTySize && "Not a value truncation");
Zhou Shengdaacf222007-04-13 05:57:32 +0000523 APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
Reid Spencer663e7112007-02-28 17:36:23 +0000524 if (isFullSet() || getSetSize().ugt(Size))
Reid Spencerbb626a62007-02-28 22:02:48 +0000525 return ConstantRange(DstTySize);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000526
Reid Spencer663e7112007-02-28 17:36:23 +0000527 APInt L = Lower; L.trunc(DstTySize);
528 APInt U = Upper; U.trunc(DstTySize);
529 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000530}
531
Dan Gohmana3755d82009-07-09 22:07:27 +0000532ConstantRange
533ConstantRange::add(const ConstantRange &Other) const {
534 if (isEmptySet() || Other.isEmptySet())
535 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewyckycf9e07d2009-07-13 02:49:08 +0000536 if (isFullSet() || Other.isFullSet())
537 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohmana3755d82009-07-09 22:07:27 +0000538
539 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
540 APInt NewLower = getLower() + Other.getLower();
541 APInt NewUpper = getUpper() + Other.getUpper() - 1;
542 if (NewLower == NewUpper)
543 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
544
545 ConstantRange X = ConstantRange(NewLower, NewUpper);
546 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
547 // We've wrapped, therefore, full set.
548 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
549
550 return X;
Chris Lattner96f9d722002-09-02 00:18:22 +0000551}
552
Dan Gohmana3755d82009-07-09 22:07:27 +0000553ConstantRange
554ConstantRange::multiply(const ConstantRange &Other) const {
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000555 if (isEmptySet() || Other.isEmptySet())
556 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
557 if (isFullSet() || Other.isFullSet())
558 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
559
560 ConstantRange this_zext = zeroExtend(getBitWidth() * 2);
561 ConstantRange Other_zext = Other.zeroExtend(getBitWidth() * 2);
562
563 ConstantRange Result_zext = ConstantRange(
564 this_zext.getLower() * Other_zext.getLower(),
565 ((this_zext.getUpper()-1) * (Other_zext.getUpper()-1)) + 1);
566
567 return Result_zext.truncate(getBitWidth());
Dan Gohmana3755d82009-07-09 22:07:27 +0000568}
569
570ConstantRange
571ConstantRange::smax(const ConstantRange &Other) const {
Dan Gohman38b06442009-07-09 23:16:10 +0000572 // X smax Y is: range(smax(X_smin, Y_smin),
573 // smax(X_smax, Y_smax))
574 if (isEmptySet() || Other.isEmptySet())
575 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohman38b06442009-07-09 23:16:10 +0000576 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
577 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
578 if (NewU == NewL)
579 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
580 return ConstantRange(NewL, NewU);
Dan Gohmana3755d82009-07-09 22:07:27 +0000581}
582
583ConstantRange
584ConstantRange::umax(const ConstantRange &Other) const {
585 // X umax Y is: range(umax(X_umin, Y_umin),
586 // umax(X_umax, Y_umax))
587 if (isEmptySet() || Other.isEmptySet())
588 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohmana3755d82009-07-09 22:07:27 +0000589 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
590 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
591 if (NewU == NewL)
592 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
593 return ConstantRange(NewL, NewU);
594}
595
596ConstantRange
Nick Lewycky956daf02009-07-12 05:18:18 +0000597ConstantRange::udiv(const ConstantRange &RHS) const {
598 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
599 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
600 if (RHS.isFullSet())
601 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
602
603 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
604
605 APInt RHS_umin = RHS.getUnsignedMin();
606 if (RHS_umin == 0) {
607 // We want the lowest value in RHS excluding zero. Usually that would be 1
608 // except for a range in the form of [X, 1) in which case it would be X.
609 if (RHS.getUpper() == 1)
610 RHS_umin = RHS.getLower();
611 else
612 RHS_umin = APInt(getBitWidth(), 1);
613 }
614
615 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
616
617 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
618 // this could occur.
619 if (Lower == Upper)
620 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
621
622 return ConstantRange(Lower, Upper);
Dan Gohmana3755d82009-07-09 22:07:27 +0000623}
624
Dan Gohman38b06442009-07-09 23:16:10 +0000625/// print - Print out the bounds to a stream...
Chris Lattner96f9d722002-09-02 00:18:22 +0000626///
Dan Gohman38b06442009-07-09 23:16:10 +0000627void ConstantRange::print(raw_ostream &OS) const {
628 OS << "[" << Lower << "," << Upper << ")";
Dan Gohmana3755d82009-07-09 22:07:27 +0000629}
630
Dan Gohman38b06442009-07-09 23:16:10 +0000631/// dump - Allow printing from a debugger easily...
Dan Gohmana3755d82009-07-09 22:07:27 +0000632///
Dan Gohman38b06442009-07-09 23:16:10 +0000633void ConstantRange::dump() const {
634 print(errs());
Dan Gohmana3755d82009-07-09 22:07:27 +0000635}
636
Dan Gohman38b06442009-07-09 23:16:10 +0000637std::ostream &llvm::operator<<(std::ostream &o,
638 const ConstantRange &CR) {
639 raw_os_ostream OS(o);
640 OS << CR;
641 return o;
Chris Lattner96f9d722002-09-02 00:18:22 +0000642}