blob: c89ec9a2aacd9f2ff285fb4f9f8a694084b5178f [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
Nick Lewycky32cda112010-09-06 23:52:49 +0000118/// isSignWrappedSet - Return true if this set wraps around the INT_MIN of
119/// its bitwidth, for example: i8 [120, 140).
120///
121bool ConstantRange::isSignWrappedSet() const {
122 return contains(APInt::getSignedMaxValue(getBitWidth())) &&
123 contains(APInt::getSignedMinValue(getBitWidth()));
124}
125
Chris Lattner645e00d2002-09-01 23:53:36 +0000126/// getSetSize - Return the number of elements in this set.
127///
Reid Spencer663e7112007-02-28 17:36:23 +0000128APInt ConstantRange::getSetSize() const {
129 if (isEmptySet())
Reid Spencerbb626a62007-02-28 22:02:48 +0000130 return APInt(getBitWidth(), 0);
131 if (getBitWidth() == 1) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000132 if (Lower != Upper) // One of T or F in the set...
Reid Spencerbb626a62007-02-28 22:02:48 +0000133 return APInt(2, 1);
134 return APInt(2, 2); // Must be full set...
Chris Lattner645e00d2002-09-01 23:53:36 +0000135 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000136
Chris Lattner645e00d2002-09-01 23:53:36 +0000137 // Simply subtract the bounds...
Reid Spencer663e7112007-02-28 17:36:23 +0000138 return Upper - Lower;
Chris Lattner645e00d2002-09-01 23:53:36 +0000139}
140
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000141/// getUnsignedMax - Return the largest unsigned value contained in the
142/// ConstantRange.
143///
144APInt ConstantRange::getUnsignedMax() const {
145 if (isFullSet() || isWrappedSet())
146 return APInt::getMaxValue(getBitWidth());
147 else
148 return getUpper() - 1;
149}
150
151/// getUnsignedMin - Return the smallest unsigned value contained in the
152/// ConstantRange.
153///
154APInt ConstantRange::getUnsignedMin() const {
155 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
156 return APInt::getMinValue(getBitWidth());
157 else
158 return getLower();
159}
160
161/// getSignedMax - Return the largest signed value contained in the
162/// ConstantRange.
163///
164APInt ConstantRange::getSignedMax() const {
Zhou Shengdaacf222007-04-13 05:57:32 +0000165 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000166 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000167 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000168 return getUpper() - 1;
169 else
170 return SignedMax;
171 } else {
Nick Lewycky780905e2009-07-13 04:50:21 +0000172 if (getLower().isNegative() == getUpper().isNegative())
173 return SignedMax;
174 else
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000175 return getUpper() - 1;
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000176 }
177}
178
179/// getSignedMin - Return the smallest signed value contained in the
180/// ConstantRange.
181///
182APInt ConstantRange::getSignedMin() const {
Zhou Shengdaacf222007-04-13 05:57:32 +0000183 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000184 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000185 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000186 return getLower();
187 else
188 return SignedMin;
189 } else {
190 if ((getUpper() - 1).slt(getLower())) {
191 if (getUpper() != SignedMin)
192 return SignedMin;
193 else
194 return getLower();
195 } else {
196 return getLower();
197 }
198 }
199}
200
Chris Lattnerfc33d302004-03-30 00:20:08 +0000201/// contains - Return true if the specified value is in the set.
202///
Reid Spencera6e8a952007-03-01 07:54:15 +0000203bool ConstantRange::contains(const APInt &V) const {
204 if (Lower == Upper)
205 return isFullSet();
Chris Lattner645e00d2002-09-01 23:53:36 +0000206
Reid Spencera6e8a952007-03-01 07:54:15 +0000207 if (!isWrappedSet())
208 return Lower.ule(V) && V.ult(Upper);
Reid Spencer663e7112007-02-28 17:36:23 +0000209 else
210 return Lower.ule(V) || V.ult(Upper);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000211}
212
Nick Lewyckybf8c7f02009-07-11 06:15:39 +0000213/// contains - Return true if the argument is a subset of this range.
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000214/// Two equal sets contain each other. The empty set contained by all other
215/// sets.
Nick Lewyckybf8c7f02009-07-11 06:15:39 +0000216///
217bool ConstantRange::contains(const ConstantRange &Other) const {
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000218 if (isFullSet() || Other.isEmptySet()) return true;
219 if (isEmptySet() || Other.isFullSet()) return false;
Nick Lewyckybf8c7f02009-07-11 06:15:39 +0000220
221 if (!isWrappedSet()) {
222 if (Other.isWrappedSet())
223 return false;
224
225 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
226 }
227
228 if (!Other.isWrappedSet())
229 return Other.getUpper().ule(Upper) ||
230 Lower.ule(Other.getLower());
231
232 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
233}
234
Chris Lattnerfc33d302004-03-30 00:20:08 +0000235/// subtract - Subtract the specified constant from the endpoints of this
236/// constant range.
Reid Spencer581b0d42007-02-28 19:57:34 +0000237ConstantRange ConstantRange::subtract(const APInt &Val) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000238 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
Chris Lattnerfc33d302004-03-30 00:20:08 +0000239 // If the set is empty or full, don't modify the endpoints.
Reid Spencer663e7112007-02-28 17:36:23 +0000240 if (Lower == Upper)
241 return *this;
Reid Spencer663e7112007-02-28 17:36:23 +0000242 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000243}
Chris Lattner645e00d2002-09-01 23:53:36 +0000244
Nick Lewycky3e051642007-02-11 00:58:49 +0000245/// intersectWith - Return the range that results from the intersection of this
Nick Lewycky3a4a8842009-07-18 06:34:42 +0000246/// range with another range. The resultant range is guaranteed to include all
247/// elements contained in both input ranges, and to have the smallest possible
248/// set size that does so. Because there may be two intersections with the
249/// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
Reid Spencera6e8a952007-03-01 07:54:15 +0000250ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000251 assert(getBitWidth() == CR.getBitWidth() &&
252 "ConstantRange types don't agree!");
Nick Lewycky377b1192007-07-14 02:51:34 +0000253
254 // Handle common cases.
255 if ( isEmptySet() || CR.isFullSet()) return *this;
256 if (CR.isEmptySet() || isFullSet()) return CR;
257
258 if (!isWrappedSet() && CR.isWrappedSet())
Nick Lewycky3a4a8842009-07-18 06:34:42 +0000259 return CR.intersectWith(*this);
Nick Lewycky377b1192007-07-14 02:51:34 +0000260
261 if (!isWrappedSet() && !CR.isWrappedSet()) {
262 if (Lower.ult(CR.Lower)) {
263 if (Upper.ule(CR.Lower))
264 return ConstantRange(getBitWidth(), false);
265
266 if (Upper.ult(CR.Upper))
267 return ConstantRange(CR.Lower, Upper);
268
269 return CR;
270 } else {
271 if (Upper.ult(CR.Upper))
272 return *this;
273
274 if (Lower.ult(CR.Upper))
275 return ConstantRange(Lower, CR.Upper);
276
277 return ConstantRange(getBitWidth(), false);
278 }
279 }
280
281 if (isWrappedSet() && !CR.isWrappedSet()) {
282 if (CR.Lower.ult(Upper)) {
283 if (CR.Upper.ult(Upper))
284 return CR;
285
286 if (CR.Upper.ult(Lower))
287 return ConstantRange(CR.Lower, Upper);
288
289 if (getSetSize().ult(CR.getSetSize()))
290 return *this;
291 else
292 return CR;
293 } else if (CR.Lower.ult(Lower)) {
294 if (CR.Upper.ule(Lower))
295 return ConstantRange(getBitWidth(), false);
296
297 return ConstantRange(Lower, CR.Upper);
298 }
299 return CR;
300 }
301
302 if (CR.Upper.ult(Upper)) {
303 if (CR.Lower.ult(Upper)) {
304 if (getSetSize().ult(CR.getSetSize()))
305 return *this;
306 else
307 return CR;
308 }
309
310 if (CR.Lower.ult(Lower))
311 return ConstantRange(Lower, CR.Upper);
312
313 return CR;
314 } else if (CR.Upper.ult(Lower)) {
315 if (CR.Lower.ult(Lower))
316 return *this;
317
318 return ConstantRange(CR.Lower, Upper);
319 }
320 if (getSetSize().ult(CR.getSetSize()))
321 return *this;
322 else
323 return CR;
324}
325
326
Nick Lewycky3e051642007-02-11 00:58:49 +0000327/// unionWith - Return the range that results from the union of this range with
Chris Lattner645e00d2002-09-01 23:53:36 +0000328/// another range. The resultant range is guaranteed to include the elements of
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000329/// both sets, but may contain more. For example, [3, 9) union [12,15) is
330/// [3, 15), which includes 9, 10, and 11, which were not included in either
331/// set before.
Chris Lattner645e00d2002-09-01 23:53:36 +0000332///
Reid Spencera6e8a952007-03-01 07:54:15 +0000333ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000334 assert(getBitWidth() == CR.getBitWidth() &&
335 "ConstantRange types don't agree!");
Chris Lattner645e00d2002-09-01 23:53:36 +0000336
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000337 if ( isFullSet() || CR.isEmptySet()) return *this;
338 if (CR.isFullSet() || isEmptySet()) return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000339
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000340 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
341
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000342 if (!isWrappedSet() && !CR.isWrappedSet()) {
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000343 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
344 // If the two ranges are disjoint, find the smaller gap and bridge it.
345 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
346 if (d1.ult(d2))
347 return ConstantRange(Lower, CR.Upper);
348 else
349 return ConstantRange(CR.Lower, Upper);
350 }
351
352 APInt L = Lower, U = Upper;
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000353 if (CR.Lower.ult(L))
354 L = CR.Lower;
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000355 if ((CR.Upper - 1).ugt(U - 1))
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000356 U = CR.Upper;
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000357
358 if (L == 0 && U == 0)
359 return ConstantRange(getBitWidth());
360
361 return ConstantRange(L, U);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000362 }
363
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000364 if (!CR.isWrappedSet()) {
365 // ------U L----- and ------U L----- : this
366 // L--U L--U : CR
367 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000368 return *this;
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000369
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000370 // ------U L----- : this
371 // L---------U : CR
372 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000373 return ConstantRange(getBitWidth());
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000374
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000375 // ----U L---- : this
376 // L---U : CR
377 // <d1> <d2>
378 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000379 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000380 if (d1.ult(d2))
381 return ConstantRange(Lower, CR.Upper);
382 else
383 return ConstantRange(CR.Lower, Upper);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000384 }
385
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000386 // ----U L----- : this
387 // L----U : CR
388 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
389 return ConstantRange(CR.Lower, Upper);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000390
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000391 // ------U L---- : this
392 // L-----U : CR
393 if (CR.Lower.ult(Upper) && CR.Upper.ult(Lower))
394 return ConstantRange(Lower, CR.Upper);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000395 }
396
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000397 assert(isWrappedSet() && CR.isWrappedSet() &&
398 "ConstantRange::unionWith missed wrapped union unwrapped case");
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000399
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000400 // ------U L---- and ------U L---- : this
401 // -U L----------- and ------------U L : CR
402 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
403 return ConstantRange(getBitWidth());
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000404
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000405 APInt L = Lower, U = Upper;
406 if (CR.Upper.ugt(U))
407 U = CR.Upper;
408 if (CR.Lower.ult(L))
409 L = CR.Lower;
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000410
411 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000412}
Chris Lattner96f9d722002-09-02 00:18:22 +0000413
Chris Lattnerfc33d302004-03-30 00:20:08 +0000414/// zeroExtend - Return a new range in the specified integer type, which must
415/// be strictly larger than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000416/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000417/// zero extended.
Reid Spencerbb626a62007-02-28 22:02:48 +0000418ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
Nick Lewycky32cda112010-09-06 23:52:49 +0000419 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
420
Reid Spencerbb626a62007-02-28 22:02:48 +0000421 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000422 assert(SrcTySize < DstTySize && "Not a value extension");
Nick Lewycky32cda112010-09-06 23:52:49 +0000423 if (isFullSet() || isWrappedSet())
424 // Change into [0, 1 << src bit width)
Reid Spencerdc5c1592007-02-28 18:57:32 +0000425 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000426
Reid Spencer663e7112007-02-28 17:36:23 +0000427 APInt L = Lower; L.zext(DstTySize);
428 APInt U = Upper; U.zext(DstTySize);
429 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000430}
431
Nick Lewyckye32157c2007-04-07 15:41:33 +0000432/// signExtend - Return a new range in the specified integer type, which must
433/// be strictly larger than the current type. The returned range will
434/// correspond to the possible range of values as if the source range had been
435/// sign extended.
436ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
Nick Lewycky32cda112010-09-06 23:52:49 +0000437 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
438
Nick Lewyckye32157c2007-04-07 15:41:33 +0000439 unsigned SrcTySize = getBitWidth();
440 assert(SrcTySize < DstTySize && "Not a value extension");
Nick Lewycky32cda112010-09-06 23:52:49 +0000441 if (isFullSet() || isSignWrappedSet()) {
Nick Lewyckye32157c2007-04-07 15:41:33 +0000442 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
Nick Lewyckyff84de72009-07-13 04:17:23 +0000443 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
Nick Lewyckye32157c2007-04-07 15:41:33 +0000444 }
445
446 APInt L = Lower; L.sext(DstTySize);
447 APInt U = Upper; U.sext(DstTySize);
448 return ConstantRange(L, U);
449}
450
Chris Lattnerfc33d302004-03-30 00:20:08 +0000451/// truncate - Return a new range in the specified integer type, which must be
452/// strictly smaller than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000453/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000454/// truncated to the specified type.
Reid Spencerbb626a62007-02-28 22:02:48 +0000455ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
456 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000457 assert(SrcTySize > DstTySize && "Not a value truncation");
Zhou Shengdaacf222007-04-13 05:57:32 +0000458 APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
Reid Spencer663e7112007-02-28 17:36:23 +0000459 if (isFullSet() || getSetSize().ugt(Size))
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000460 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000461
Reid Spencer663e7112007-02-28 17:36:23 +0000462 APInt L = Lower; L.trunc(DstTySize);
463 APInt U = Upper; U.trunc(DstTySize);
464 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000465}
466
Nuno Lopes95a3be02009-11-09 15:36:28 +0000467/// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
468/// value is zero extended, truncated, or left alone to make it that width.
469ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
470 unsigned SrcTySize = getBitWidth();
471 if (SrcTySize > DstTySize)
472 return truncate(DstTySize);
473 else if (SrcTySize < DstTySize)
474 return zeroExtend(DstTySize);
475 else
476 return *this;
477}
478
479/// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
480/// value is sign extended, truncated, or left alone to make it that width.
481ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
482 unsigned SrcTySize = getBitWidth();
483 if (SrcTySize > DstTySize)
484 return truncate(DstTySize);
485 else if (SrcTySize < DstTySize)
486 return signExtend(DstTySize);
487 else
488 return *this;
489}
490
Dan Gohmana3755d82009-07-09 22:07:27 +0000491ConstantRange
492ConstantRange::add(const ConstantRange &Other) const {
493 if (isEmptySet() || Other.isEmptySet())
494 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewyckycf9e07d2009-07-13 02:49:08 +0000495 if (isFullSet() || Other.isFullSet())
496 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohmana3755d82009-07-09 22:07:27 +0000497
498 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
499 APInt NewLower = getLower() + Other.getLower();
500 APInt NewUpper = getUpper() + Other.getUpper() - 1;
501 if (NewLower == NewUpper)
502 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
503
504 ConstantRange X = ConstantRange(NewLower, NewUpper);
505 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
506 // We've wrapped, therefore, full set.
507 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
508
509 return X;
Chris Lattner96f9d722002-09-02 00:18:22 +0000510}
511
Dan Gohmana3755d82009-07-09 22:07:27 +0000512ConstantRange
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000513ConstantRange::sub(const ConstantRange &Other) const {
514 if (isEmptySet() || Other.isEmptySet())
515 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
516 if (isFullSet() || Other.isFullSet())
517 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
518
519 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
520 APInt NewLower = getLower() - Other.getLower();
521 APInt NewUpper = getUpper() - Other.getUpper() + 1;
522 if (NewLower == NewUpper)
523 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
524
525 ConstantRange X = ConstantRange(NewLower, NewUpper);
526 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
527 // We've wrapped, therefore, full set.
528 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
529
530 return X;
531}
532
533ConstantRange
Dan Gohmana3755d82009-07-09 22:07:27 +0000534ConstantRange::multiply(const ConstantRange &Other) const {
Dan Gohman8dcc58e2010-01-26 15:56:18 +0000535 // TODO: If either operand is a single element and the multiply is known to
536 // be non-wrapping, round the result min and max value to the appropriate
537 // multiple of that element. If wrapping is possible, at least adjust the
538 // range according to the greatest power-of-two factor of the single element.
Dan Gohman153f1eb2010-01-26 04:13:15 +0000539
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000540 if (isEmptySet() || Other.isEmptySet())
541 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
542 if (isFullSet() || Other.isFullSet())
543 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
544
Nick Lewyckyf1db1202009-07-13 03:27:41 +0000545 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
546 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
547 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
548 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000549
Nick Lewyckyf1db1202009-07-13 03:27:41 +0000550 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
551 this_max * Other_max + 1);
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000552 return Result_zext.truncate(getBitWidth());
Dan Gohmana3755d82009-07-09 22:07:27 +0000553}
554
555ConstantRange
556ConstantRange::smax(const ConstantRange &Other) const {
Dan Gohman38b06442009-07-09 23:16:10 +0000557 // X smax Y is: range(smax(X_smin, Y_smin),
558 // smax(X_smax, Y_smax))
559 if (isEmptySet() || Other.isEmptySet())
560 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohman38b06442009-07-09 23:16:10 +0000561 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
562 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
563 if (NewU == NewL)
564 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
565 return ConstantRange(NewL, NewU);
Dan Gohmana3755d82009-07-09 22:07:27 +0000566}
567
568ConstantRange
569ConstantRange::umax(const ConstantRange &Other) const {
570 // X umax Y is: range(umax(X_umin, Y_umin),
571 // umax(X_umax, Y_umax))
572 if (isEmptySet() || Other.isEmptySet())
573 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohmana3755d82009-07-09 22:07:27 +0000574 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
575 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
576 if (NewU == NewL)
577 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
578 return ConstantRange(NewL, NewU);
579}
580
581ConstantRange
Nick Lewycky956daf02009-07-12 05:18:18 +0000582ConstantRange::udiv(const ConstantRange &RHS) const {
583 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
584 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
585 if (RHS.isFullSet())
586 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
587
588 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
589
590 APInt RHS_umin = RHS.getUnsignedMin();
591 if (RHS_umin == 0) {
592 // We want the lowest value in RHS excluding zero. Usually that would be 1
593 // except for a range in the form of [X, 1) in which case it would be X.
594 if (RHS.getUpper() == 1)
595 RHS_umin = RHS.getLower();
596 else
597 RHS_umin = APInt(getBitWidth(), 1);
598 }
599
600 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
601
602 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
603 // this could occur.
604 if (Lower == Upper)
605 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
606
607 return ConstantRange(Lower, Upper);
Dan Gohmana3755d82009-07-09 22:07:27 +0000608}
609
Nuno Lopes34e992d2009-11-12 14:53:53 +0000610ConstantRange
Nick Lewycky198381e2010-09-07 05:39:02 +0000611ConstantRange::binaryAnd(const ConstantRange &Other) const {
612 if (isEmptySet() || Other.isEmptySet())
613 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
614
615 // TODO: replace this with something less conservative
616
617 APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax());
618 if (umin.isAllOnesValue())
619 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
620 return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1);
621}
622
623ConstantRange
624ConstantRange::binaryOr(const ConstantRange &Other) const {
625 if (isEmptySet() || Other.isEmptySet())
626 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
627
628 // TODO: replace this with something less conservative
629
630 APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
631 if (umax.isMinValue())
632 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
633 return ConstantRange(umax, APInt::getNullValue(getBitWidth()));
634}
635
636ConstantRange
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000637ConstantRange::shl(const ConstantRange &Other) const {
638 if (isEmptySet() || Other.isEmptySet())
639 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000640
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000641 APInt min = getUnsignedMin().shl(Other.getUnsignedMin());
642 APInt max = getUnsignedMax().shl(Other.getUnsignedMax());
Nuno Lopes34e992d2009-11-12 14:53:53 +0000643
644 // there's no overflow!
Nuno Lopes44591452009-11-12 15:10:33 +0000645 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000646 if (Zeros.ugt(Other.getUnsignedMax()))
647 return ConstantRange(min, max + 1);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000648
649 // FIXME: implement the other tricky cases
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000650 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000651}
652
653ConstantRange
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000654ConstantRange::lshr(const ConstantRange &Other) const {
655 if (isEmptySet() || Other.isEmptySet())
656 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000657
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000658 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin());
659 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
660 if (min == max + 1)
661 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
662
663 return ConstantRange(min, max + 1);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000664}
665
Owen Anderson9773e452010-08-07 05:47:46 +0000666ConstantRange ConstantRange::inverse() const {
667 if (isFullSet()) {
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000668 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Owen Anderson9773e452010-08-07 05:47:46 +0000669 } else if (isEmptySet()) {
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000670 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Owen Anderson9773e452010-08-07 05:47:46 +0000671 }
672 return ConstantRange(Upper, Lower);
673}
674
Dan Gohman38b06442009-07-09 23:16:10 +0000675/// print - Print out the bounds to a stream...
Chris Lattner96f9d722002-09-02 00:18:22 +0000676///
Dan Gohman38b06442009-07-09 23:16:10 +0000677void ConstantRange::print(raw_ostream &OS) const {
Dan Gohmandf6d5e02010-01-26 04:12:55 +0000678 if (isFullSet())
679 OS << "full-set";
680 else if (isEmptySet())
681 OS << "empty-set";
682 else
683 OS << "[" << Lower << "," << Upper << ")";
Dan Gohmana3755d82009-07-09 22:07:27 +0000684}
685
Dan Gohman38b06442009-07-09 23:16:10 +0000686/// dump - Allow printing from a debugger easily...
Dan Gohmana3755d82009-07-09 22:07:27 +0000687///
Dan Gohman38b06442009-07-09 23:16:10 +0000688void ConstantRange::dump() const {
David Greene7fd5fb42010-01-05 01:28:32 +0000689 print(dbgs());
Dan Gohmana3755d82009-07-09 22:07:27 +0000690}