blob: 8ef3785f53318ef1576e6bad60b94b79a47e0e9b [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
Owen Anderson634bab12010-08-07 00:42:06 +000024#include "llvm/Constants.h"
Chris Lattner645e00d2002-09-01 23:53:36 +000025#include "llvm/Support/ConstantRange.h"
David Greene7fd5fb42010-01-05 01:28:32 +000026#include "llvm/Support/Debug.h"
Chris Lattner944fac72008-08-23 22:23:09 +000027#include "llvm/Support/raw_ostream.h"
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000028#include "llvm/Instructions.h"
Chris Lattner2cdd21c2003-12-14 21:35:53 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner645e00d2002-09-01 23:53:36 +000031/// Initialize a full (the default) or empty set for the specified type.
32///
Dan Gohman38b06442009-07-09 23:16:10 +000033ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) {
Chris Lattner645e00d2002-09-01 23:53:36 +000034 if (Full)
Reid Spencer663e7112007-02-28 17:36:23 +000035 Lower = Upper = APInt::getMaxValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000036 else
Reid Spencer663e7112007-02-28 17:36:23 +000037 Lower = Upper = APInt::getMinValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000038}
39
Chris Lattnerfc33d302004-03-30 00:20:08 +000040/// Initialize a range to hold the single specified value.
41///
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +000042ConstantRange::ConstantRange(const APInt &V) : Lower(V), Upper(V + 1) {}
Chris Lattner645e00d2002-09-01 23:53:36 +000043
Dan Gohman38b06442009-07-09 23:16:10 +000044ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
45 Lower(L), Upper(U) {
46 assert(L.getBitWidth() == U.getBitWidth() &&
47 "ConstantRange with unequal bit widths");
Zhou Shengc125c002007-04-26 16:42:07 +000048 assert((L != U || (L.isMaxValue() || L.isMinValue())) &&
Reid Spencer663e7112007-02-28 17:36:23 +000049 "Lower == Upper, but they aren't min or max value!");
50}
51
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000052ConstantRange ConstantRange::makeICmpRegion(unsigned Pred,
53 const ConstantRange &CR) {
54 uint32_t W = CR.getBitWidth();
55 switch (Pred) {
56 default: assert(!"Invalid ICmp predicate to makeICmpRegion()");
57 case ICmpInst::ICMP_EQ:
Nick Lewyckyf067a232009-07-11 17:04:01 +000058 return CR;
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000059 case ICmpInst::ICMP_NE:
60 if (CR.isSingleElement())
61 return ConstantRange(CR.getUpper(), CR.getLower());
62 return ConstantRange(W);
63 case ICmpInst::ICMP_ULT:
64 return ConstantRange(APInt::getMinValue(W), CR.getUnsignedMax());
65 case ICmpInst::ICMP_SLT:
66 return ConstantRange(APInt::getSignedMinValue(W), CR.getSignedMax());
67 case ICmpInst::ICMP_ULE: {
68 APInt UMax(CR.getUnsignedMax());
69 if (UMax.isMaxValue())
70 return ConstantRange(W);
71 return ConstantRange(APInt::getMinValue(W), UMax + 1);
72 }
73 case ICmpInst::ICMP_SLE: {
74 APInt SMax(CR.getSignedMax());
75 if (SMax.isMaxSignedValue() || (SMax+1).isMaxSignedValue())
76 return ConstantRange(W);
77 return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
78 }
79 case ICmpInst::ICMP_UGT:
80 return ConstantRange(CR.getUnsignedMin() + 1, APInt::getNullValue(W));
81 case ICmpInst::ICMP_SGT:
82 return ConstantRange(CR.getSignedMin() + 1,
83 APInt::getSignedMinValue(W));
84 case ICmpInst::ICMP_UGE: {
85 APInt UMin(CR.getUnsignedMin());
86 if (UMin.isMinValue())
87 return ConstantRange(W);
88 return ConstantRange(UMin, APInt::getNullValue(W));
89 }
90 case ICmpInst::ICMP_SGE: {
91 APInt SMin(CR.getSignedMin());
92 if (SMin.isMinSignedValue())
93 return ConstantRange(W);
94 return ConstantRange(SMin, APInt::getSignedMinValue(W));
95 }
96 }
97}
98
Chris Lattner645e00d2002-09-01 23:53:36 +000099/// isFullSet - Return true if this set contains all of the elements possible
100/// for this data-type
101bool ConstantRange::isFullSet() const {
Zhou Shengc125c002007-04-26 16:42:07 +0000102 return Lower == Upper && Lower.isMaxValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000103}
Misha Brukmanfd939082005-04-21 23:48:37 +0000104
Chris Lattner645e00d2002-09-01 23:53:36 +0000105/// isEmptySet - Return true if this set contains no members.
106///
107bool ConstantRange::isEmptySet() const {
Zhou Shengc125c002007-04-26 16:42:07 +0000108 return Lower == Upper && Lower.isMinValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000109}
110
111/// isWrappedSet - Return true if this set wraps around the top of the range,
112/// for example: [100, 8)
113///
Reid Spencera6e8a952007-03-01 07:54:15 +0000114bool ConstantRange::isWrappedSet() const {
Reid Spencer663e7112007-02-28 17:36:23 +0000115 return Lower.ugt(Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000116}
117
Chris Lattner645e00d2002-09-01 23:53:36 +0000118/// getSetSize - Return the number of elements in this set.
119///
Reid Spencer663e7112007-02-28 17:36:23 +0000120APInt ConstantRange::getSetSize() const {
121 if (isEmptySet())
Reid Spencerbb626a62007-02-28 22:02:48 +0000122 return APInt(getBitWidth(), 0);
123 if (getBitWidth() == 1) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000124 if (Lower != Upper) // One of T or F in the set...
Reid Spencerbb626a62007-02-28 22:02:48 +0000125 return APInt(2, 1);
126 return APInt(2, 2); // Must be full set...
Chris Lattner645e00d2002-09-01 23:53:36 +0000127 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000128
Chris Lattner645e00d2002-09-01 23:53:36 +0000129 // Simply subtract the bounds...
Reid Spencer663e7112007-02-28 17:36:23 +0000130 return Upper - Lower;
Chris Lattner645e00d2002-09-01 23:53:36 +0000131}
132
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000133/// getUnsignedMax - Return the largest unsigned value contained in the
134/// ConstantRange.
135///
136APInt ConstantRange::getUnsignedMax() const {
137 if (isFullSet() || isWrappedSet())
138 return APInt::getMaxValue(getBitWidth());
139 else
140 return getUpper() - 1;
141}
142
143/// getUnsignedMin - Return the smallest unsigned value contained in the
144/// ConstantRange.
145///
146APInt ConstantRange::getUnsignedMin() const {
147 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
148 return APInt::getMinValue(getBitWidth());
149 else
150 return getLower();
151}
152
153/// getSignedMax - Return the largest signed value contained in the
154/// ConstantRange.
155///
156APInt ConstantRange::getSignedMax() const {
Zhou Shengdaacf222007-04-13 05:57:32 +0000157 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000158 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000159 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000160 return getUpper() - 1;
161 else
162 return SignedMax;
163 } else {
Nick Lewycky780905e2009-07-13 04:50:21 +0000164 if (getLower().isNegative() == getUpper().isNegative())
165 return SignedMax;
166 else
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000167 return getUpper() - 1;
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000168 }
169}
170
171/// getSignedMin - Return the smallest signed value contained in the
172/// ConstantRange.
173///
174APInt ConstantRange::getSignedMin() const {
Zhou Shengdaacf222007-04-13 05:57:32 +0000175 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000176 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000177 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000178 return getLower();
179 else
180 return SignedMin;
181 } else {
182 if ((getUpper() - 1).slt(getLower())) {
183 if (getUpper() != SignedMin)
184 return SignedMin;
185 else
186 return getLower();
187 } else {
188 return getLower();
189 }
190 }
191}
192
Chris Lattnerfc33d302004-03-30 00:20:08 +0000193/// contains - Return true if the specified value is in the set.
194///
Reid Spencera6e8a952007-03-01 07:54:15 +0000195bool ConstantRange::contains(const APInt &V) const {
196 if (Lower == Upper)
197 return isFullSet();
Chris Lattner645e00d2002-09-01 23:53:36 +0000198
Reid Spencera6e8a952007-03-01 07:54:15 +0000199 if (!isWrappedSet())
200 return Lower.ule(V) && V.ult(Upper);
Reid Spencer663e7112007-02-28 17:36:23 +0000201 else
202 return Lower.ule(V) || V.ult(Upper);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000203}
204
Nick Lewyckybf8c7f02009-07-11 06:15:39 +0000205/// contains - Return true if the argument is a subset of this range.
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000206/// Two equal sets contain each other. The empty set contained by all other
207/// sets.
Nick Lewyckybf8c7f02009-07-11 06:15:39 +0000208///
209bool ConstantRange::contains(const ConstantRange &Other) const {
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000210 if (isFullSet() || Other.isEmptySet()) return true;
211 if (isEmptySet() || Other.isFullSet()) return false;
Nick Lewyckybf8c7f02009-07-11 06:15:39 +0000212
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
Chris Lattnerfc33d302004-03-30 00:20:08 +0000227/// subtract - Subtract the specified constant from the endpoints of this
228/// constant range.
Reid Spencer581b0d42007-02-28 19:57:34 +0000229ConstantRange ConstantRange::subtract(const APInt &Val) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000230 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
Chris Lattnerfc33d302004-03-30 00:20:08 +0000231 // If the set is empty or full, don't modify the endpoints.
Reid Spencer663e7112007-02-28 17:36:23 +0000232 if (Lower == Upper)
233 return *this;
Reid Spencer663e7112007-02-28 17:36:23 +0000234 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000235}
Chris Lattner645e00d2002-09-01 23:53:36 +0000236
Nick Lewycky3e051642007-02-11 00:58:49 +0000237/// intersectWith - Return the range that results from the intersection of this
Nick Lewycky3a4a8842009-07-18 06:34:42 +0000238/// range with another range. The resultant range is guaranteed to include all
239/// elements contained in both input ranges, and to have the smallest possible
240/// set size that does so. Because there may be two intersections with the
241/// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
Reid Spencera6e8a952007-03-01 07:54:15 +0000242ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000243 assert(getBitWidth() == CR.getBitWidth() &&
244 "ConstantRange types don't agree!");
Nick Lewycky377b1192007-07-14 02:51:34 +0000245
246 // Handle common cases.
247 if ( isEmptySet() || CR.isFullSet()) return *this;
248 if (CR.isEmptySet() || isFullSet()) return CR;
249
250 if (!isWrappedSet() && CR.isWrappedSet())
Nick Lewycky3a4a8842009-07-18 06:34:42 +0000251 return CR.intersectWith(*this);
Nick Lewycky377b1192007-07-14 02:51:34 +0000252
253 if (!isWrappedSet() && !CR.isWrappedSet()) {
254 if (Lower.ult(CR.Lower)) {
255 if (Upper.ule(CR.Lower))
256 return ConstantRange(getBitWidth(), false);
257
258 if (Upper.ult(CR.Upper))
259 return ConstantRange(CR.Lower, Upper);
260
261 return CR;
262 } else {
263 if (Upper.ult(CR.Upper))
264 return *this;
265
266 if (Lower.ult(CR.Upper))
267 return ConstantRange(Lower, CR.Upper);
268
269 return ConstantRange(getBitWidth(), false);
270 }
271 }
272
273 if (isWrappedSet() && !CR.isWrappedSet()) {
274 if (CR.Lower.ult(Upper)) {
275 if (CR.Upper.ult(Upper))
276 return CR;
277
278 if (CR.Upper.ult(Lower))
279 return ConstantRange(CR.Lower, Upper);
280
281 if (getSetSize().ult(CR.getSetSize()))
282 return *this;
283 else
284 return CR;
285 } else if (CR.Lower.ult(Lower)) {
286 if (CR.Upper.ule(Lower))
287 return ConstantRange(getBitWidth(), false);
288
289 return ConstantRange(Lower, CR.Upper);
290 }
291 return CR;
292 }
293
294 if (CR.Upper.ult(Upper)) {
295 if (CR.Lower.ult(Upper)) {
296 if (getSetSize().ult(CR.getSetSize()))
297 return *this;
298 else
299 return CR;
300 }
301
302 if (CR.Lower.ult(Lower))
303 return ConstantRange(Lower, CR.Upper);
304
305 return CR;
306 } else if (CR.Upper.ult(Lower)) {
307 if (CR.Lower.ult(Lower))
308 return *this;
309
310 return ConstantRange(CR.Lower, Upper);
311 }
312 if (getSetSize().ult(CR.getSetSize()))
313 return *this;
314 else
315 return CR;
316}
317
318
Nick Lewycky3e051642007-02-11 00:58:49 +0000319/// unionWith - Return the range that results from the union of this range with
Chris Lattner645e00d2002-09-01 23:53:36 +0000320/// another range. The resultant range is guaranteed to include the elements of
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000321/// both sets, but may contain more. For example, [3, 9) union [12,15) is
322/// [3, 15), which includes 9, 10, and 11, which were not included in either
323/// set before.
Chris Lattner645e00d2002-09-01 23:53:36 +0000324///
Reid Spencera6e8a952007-03-01 07:54:15 +0000325ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000326 assert(getBitWidth() == CR.getBitWidth() &&
327 "ConstantRange types don't agree!");
Chris Lattner645e00d2002-09-01 23:53:36 +0000328
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000329 if ( isFullSet() || CR.isEmptySet()) return *this;
330 if (CR.isFullSet() || isEmptySet()) return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000331
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000332 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
333
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000334 if (!isWrappedSet() && !CR.isWrappedSet()) {
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000335 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
336 // If the two ranges are disjoint, find the smaller gap and bridge it.
337 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
338 if (d1.ult(d2))
339 return ConstantRange(Lower, CR.Upper);
340 else
341 return ConstantRange(CR.Lower, Upper);
342 }
343
344 APInt L = Lower, U = Upper;
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000345 if (CR.Lower.ult(L))
346 L = CR.Lower;
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000347 if ((CR.Upper - 1).ugt(U - 1))
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000348 U = CR.Upper;
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000349
350 if (L == 0 && U == 0)
351 return ConstantRange(getBitWidth());
352
353 return ConstantRange(L, U);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000354 }
355
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000356 if (!CR.isWrappedSet()) {
357 // ------U L----- and ------U L----- : this
358 // L--U L--U : CR
359 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000360 return *this;
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000361
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000362 // ------U L----- : this
363 // L---------U : CR
364 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000365 return ConstantRange(getBitWidth());
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000366
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000367 // ----U L---- : this
368 // L---U : CR
369 // <d1> <d2>
370 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000371 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000372 if (d1.ult(d2))
373 return ConstantRange(Lower, CR.Upper);
374 else
375 return ConstantRange(CR.Lower, Upper);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000376 }
377
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000378 // ----U L----- : this
379 // L----U : CR
380 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
381 return ConstantRange(CR.Lower, Upper);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000382
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000383 // ------U L---- : this
384 // L-----U : CR
385 if (CR.Lower.ult(Upper) && CR.Upper.ult(Lower))
386 return ConstantRange(Lower, CR.Upper);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000387 }
388
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000389 assert(isWrappedSet() && CR.isWrappedSet() &&
390 "ConstantRange::unionWith missed wrapped union unwrapped case");
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000391
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000392 // ------U L---- and ------U L---- : this
393 // -U L----------- and ------------U L : CR
394 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
395 return ConstantRange(getBitWidth());
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000396
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000397 APInt L = Lower, U = Upper;
398 if (CR.Upper.ugt(U))
399 U = CR.Upper;
400 if (CR.Lower.ult(L))
401 L = CR.Lower;
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000402
403 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000404}
Chris Lattner96f9d722002-09-02 00:18:22 +0000405
Chris Lattnerfc33d302004-03-30 00:20:08 +0000406/// zeroExtend - Return a new range in the specified integer type, which must
407/// be strictly larger than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000408/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000409/// zero extended.
Reid Spencerbb626a62007-02-28 22:02:48 +0000410ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
411 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000412 assert(SrcTySize < DstTySize && "Not a value extension");
Reid Spencerdc5c1592007-02-28 18:57:32 +0000413 if (isFullSet())
Chris Lattnerfc33d302004-03-30 00:20:08 +0000414 // Change a source full set into [0, 1 << 8*numbytes)
Reid Spencerdc5c1592007-02-28 18:57:32 +0000415 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000416
Reid Spencer663e7112007-02-28 17:36:23 +0000417 APInt L = Lower; L.zext(DstTySize);
418 APInt U = Upper; U.zext(DstTySize);
419 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000420}
421
Nick Lewyckye32157c2007-04-07 15:41:33 +0000422/// signExtend - Return a new range in the specified integer type, which must
423/// be strictly larger than the current type. The returned range will
424/// correspond to the possible range of values as if the source range had been
425/// sign extended.
426ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
427 unsigned SrcTySize = getBitWidth();
428 assert(SrcTySize < DstTySize && "Not a value extension");
429 if (isFullSet()) {
430 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
Nick Lewyckyff84de72009-07-13 04:17:23 +0000431 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
Nick Lewyckye32157c2007-04-07 15:41:33 +0000432 }
433
434 APInt L = Lower; L.sext(DstTySize);
435 APInt U = Upper; U.sext(DstTySize);
436 return ConstantRange(L, U);
437}
438
Chris Lattnerfc33d302004-03-30 00:20:08 +0000439/// truncate - Return a new range in the specified integer type, which must be
440/// strictly smaller than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000441/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000442/// truncated to the specified type.
Reid Spencerbb626a62007-02-28 22:02:48 +0000443ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
444 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000445 assert(SrcTySize > DstTySize && "Not a value truncation");
Zhou Shengdaacf222007-04-13 05:57:32 +0000446 APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
Reid Spencer663e7112007-02-28 17:36:23 +0000447 if (isFullSet() || getSetSize().ugt(Size))
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000448 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000449
Reid Spencer663e7112007-02-28 17:36:23 +0000450 APInt L = Lower; L.trunc(DstTySize);
451 APInt U = Upper; U.trunc(DstTySize);
452 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000453}
454
Nuno Lopes95a3be02009-11-09 15:36:28 +0000455/// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
456/// value is zero extended, truncated, or left alone to make it that width.
457ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
458 unsigned SrcTySize = getBitWidth();
459 if (SrcTySize > DstTySize)
460 return truncate(DstTySize);
461 else if (SrcTySize < DstTySize)
462 return zeroExtend(DstTySize);
463 else
464 return *this;
465}
466
467/// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
468/// value is sign extended, truncated, or left alone to make it that width.
469ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
470 unsigned SrcTySize = getBitWidth();
471 if (SrcTySize > DstTySize)
472 return truncate(DstTySize);
473 else if (SrcTySize < DstTySize)
474 return signExtend(DstTySize);
475 else
476 return *this;
477}
478
Dan Gohmana3755d82009-07-09 22:07:27 +0000479ConstantRange
480ConstantRange::add(const ConstantRange &Other) const {
481 if (isEmptySet() || Other.isEmptySet())
482 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewyckycf9e07d2009-07-13 02:49:08 +0000483 if (isFullSet() || Other.isFullSet())
484 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohmana3755d82009-07-09 22:07:27 +0000485
486 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
487 APInt NewLower = getLower() + Other.getLower();
488 APInt NewUpper = getUpper() + Other.getUpper() - 1;
489 if (NewLower == NewUpper)
490 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
491
492 ConstantRange X = ConstantRange(NewLower, NewUpper);
493 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
494 // We've wrapped, therefore, full set.
495 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
496
497 return X;
Chris Lattner96f9d722002-09-02 00:18:22 +0000498}
499
Dan Gohmana3755d82009-07-09 22:07:27 +0000500ConstantRange
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000501ConstantRange::sub(const ConstantRange &Other) const {
502 if (isEmptySet() || Other.isEmptySet())
503 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
504 if (isFullSet() || Other.isFullSet())
505 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
506
507 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
508 APInt NewLower = getLower() - Other.getLower();
509 APInt NewUpper = getUpper() - Other.getUpper() + 1;
510 if (NewLower == NewUpper)
511 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
512
513 ConstantRange X = ConstantRange(NewLower, NewUpper);
514 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
515 // We've wrapped, therefore, full set.
516 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
517
518 return X;
519}
520
521ConstantRange
Dan Gohmana3755d82009-07-09 22:07:27 +0000522ConstantRange::multiply(const ConstantRange &Other) const {
Dan Gohman8dcc58e2010-01-26 15:56:18 +0000523 // TODO: If either operand is a single element and the multiply is known to
524 // be non-wrapping, round the result min and max value to the appropriate
525 // multiple of that element. If wrapping is possible, at least adjust the
526 // range according to the greatest power-of-two factor of the single element.
Dan Gohman153f1eb2010-01-26 04:13:15 +0000527
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000528 if (isEmptySet() || Other.isEmptySet())
529 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
530 if (isFullSet() || Other.isFullSet())
531 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
532
Nick Lewyckyf1db1202009-07-13 03:27:41 +0000533 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
534 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
535 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
536 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000537
Nick Lewyckyf1db1202009-07-13 03:27:41 +0000538 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
539 this_max * Other_max + 1);
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000540 return Result_zext.truncate(getBitWidth());
Dan Gohmana3755d82009-07-09 22:07:27 +0000541}
542
543ConstantRange
544ConstantRange::smax(const ConstantRange &Other) const {
Dan Gohman38b06442009-07-09 23:16:10 +0000545 // X smax Y is: range(smax(X_smin, Y_smin),
546 // smax(X_smax, Y_smax))
547 if (isEmptySet() || Other.isEmptySet())
548 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohman38b06442009-07-09 23:16:10 +0000549 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
550 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
551 if (NewU == NewL)
552 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
553 return ConstantRange(NewL, NewU);
Dan Gohmana3755d82009-07-09 22:07:27 +0000554}
555
556ConstantRange
557ConstantRange::umax(const ConstantRange &Other) const {
558 // X umax Y is: range(umax(X_umin, Y_umin),
559 // umax(X_umax, Y_umax))
560 if (isEmptySet() || Other.isEmptySet())
561 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohmana3755d82009-07-09 22:07:27 +0000562 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
563 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
564 if (NewU == NewL)
565 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
566 return ConstantRange(NewL, NewU);
567}
568
569ConstantRange
Nick Lewycky956daf02009-07-12 05:18:18 +0000570ConstantRange::udiv(const ConstantRange &RHS) const {
571 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
572 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
573 if (RHS.isFullSet())
574 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
575
576 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
577
578 APInt RHS_umin = RHS.getUnsignedMin();
579 if (RHS_umin == 0) {
580 // We want the lowest value in RHS excluding zero. Usually that would be 1
581 // except for a range in the form of [X, 1) in which case it would be X.
582 if (RHS.getUpper() == 1)
583 RHS_umin = RHS.getLower();
584 else
585 RHS_umin = APInt(getBitWidth(), 1);
586 }
587
588 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
589
590 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
591 // this could occur.
592 if (Lower == Upper)
593 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
594
595 return ConstantRange(Lower, Upper);
Dan Gohmana3755d82009-07-09 22:07:27 +0000596}
597
Nuno Lopes34e992d2009-11-12 14:53:53 +0000598ConstantRange
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000599ConstantRange::shl(const ConstantRange &Other) const {
600 if (isEmptySet() || Other.isEmptySet())
601 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000602
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000603 APInt min = getUnsignedMin().shl(Other.getUnsignedMin());
604 APInt max = getUnsignedMax().shl(Other.getUnsignedMax());
Nuno Lopes34e992d2009-11-12 14:53:53 +0000605
606 // there's no overflow!
Nuno Lopes44591452009-11-12 15:10:33 +0000607 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000608 if (Zeros.ugt(Other.getUnsignedMax()))
609 return ConstantRange(min, max + 1);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000610
611 // FIXME: implement the other tricky cases
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000612 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000613}
614
615ConstantRange
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000616ConstantRange::lshr(const ConstantRange &Other) const {
617 if (isEmptySet() || Other.isEmptySet())
618 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000619
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000620 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin());
621 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
622 if (min == max + 1)
623 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
624
625 return ConstantRange(min, max + 1);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000626}
627
Owen Anderson9773e452010-08-07 05:47:46 +0000628ConstantRange ConstantRange::inverse() const {
629 if (isFullSet()) {
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000630 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Owen Anderson9773e452010-08-07 05:47:46 +0000631 } else if (isEmptySet()) {
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000632 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Owen Anderson9773e452010-08-07 05:47:46 +0000633 }
634 return ConstantRange(Upper, Lower);
635}
636
Dan Gohman38b06442009-07-09 23:16:10 +0000637/// print - Print out the bounds to a stream...
Chris Lattner96f9d722002-09-02 00:18:22 +0000638///
Dan Gohman38b06442009-07-09 23:16:10 +0000639void ConstantRange::print(raw_ostream &OS) const {
Dan Gohmandf6d5e02010-01-26 04:12:55 +0000640 if (isFullSet())
641 OS << "full-set";
642 else if (isEmptySet())
643 OS << "empty-set";
644 else
645 OS << "[" << Lower << "," << Upper << ")";
Dan Gohmana3755d82009-07-09 22:07:27 +0000646}
647
Dan Gohman38b06442009-07-09 23:16:10 +0000648/// dump - Allow printing from a debugger easily...
Dan Gohmana3755d82009-07-09 22:07:27 +0000649///
Dan Gohman38b06442009-07-09 23:16:10 +0000650void ConstantRange::dump() const {
David Greene7fd5fb42010-01-05 01:28:32 +0000651 print(dbgs());
Dan Gohmana3755d82009-07-09 22:07:27 +0000652}