blob: b83dcccca67a3adb21c06d8cdb58c7d2937e48cc [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
Frits van Bommela09d5142011-07-27 15:20:06 +000024#include "llvm/InstrTypes.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"
Chris Lattner2cdd21c2003-12-14 21:35:53 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner645e00d2002-09-01 23:53:36 +000030/// Initialize a full (the default) or empty set for the specified type.
31///
Dan Gohman38b06442009-07-09 23:16:10 +000032ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) {
Chris Lattner645e00d2002-09-01 23:53:36 +000033 if (Full)
Reid Spencer663e7112007-02-28 17:36:23 +000034 Lower = Upper = APInt::getMaxValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000035 else
Reid Spencer663e7112007-02-28 17:36:23 +000036 Lower = Upper = APInt::getMinValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000037}
38
Chris Lattnerfc33d302004-03-30 00:20:08 +000039/// Initialize a range to hold the single specified value.
40///
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +000041ConstantRange::ConstantRange(const APInt &V) : Lower(V), Upper(V + 1) {}
Chris Lattner645e00d2002-09-01 23:53:36 +000042
Dan Gohman38b06442009-07-09 23:16:10 +000043ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
44 Lower(L), Upper(U) {
45 assert(L.getBitWidth() == U.getBitWidth() &&
46 "ConstantRange with unequal bit widths");
Zhou Shengc125c002007-04-26 16:42:07 +000047 assert((L != U || (L.isMaxValue() || L.isMinValue())) &&
Reid Spencer663e7112007-02-28 17:36:23 +000048 "Lower == Upper, but they aren't min or max value!");
49}
50
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000051ConstantRange ConstantRange::makeICmpRegion(unsigned Pred,
52 const ConstantRange &CR) {
Nick Lewyckyf2d7b7c2010-09-28 18:18:36 +000053 if (CR.isEmptySet())
54 return CR;
55
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000056 uint32_t W = CR.getBitWidth();
57 switch (Pred) {
Craig Topper85814382012-02-07 05:05:23 +000058 default: llvm_unreachable("Invalid ICmp predicate to makeICmpRegion()");
Frits van Bommela09d5142011-07-27 15:20:06 +000059 case CmpInst::ICMP_EQ:
Nick Lewyckyf067a232009-07-11 17:04:01 +000060 return CR;
Frits van Bommela09d5142011-07-27 15:20:06 +000061 case CmpInst::ICMP_NE:
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000062 if (CR.isSingleElement())
63 return ConstantRange(CR.getUpper(), CR.getLower());
64 return ConstantRange(W);
Frits van Bommela09d5142011-07-27 15:20:06 +000065 case CmpInst::ICMP_ULT: {
Nick Lewyckyf2d7b7c2010-09-28 18:18:36 +000066 APInt UMax(CR.getUnsignedMax());
67 if (UMax.isMinValue())
68 return ConstantRange(W, /* empty */ false);
69 return ConstantRange(APInt::getMinValue(W), UMax);
70 }
Frits van Bommela09d5142011-07-27 15:20:06 +000071 case CmpInst::ICMP_SLT: {
Nick Lewyckyf2d7b7c2010-09-28 18:18:36 +000072 APInt SMax(CR.getSignedMax());
73 if (SMax.isMinSignedValue())
74 return ConstantRange(W, /* empty */ false);
75 return ConstantRange(APInt::getSignedMinValue(W), SMax);
76 }
Frits van Bommela09d5142011-07-27 15:20:06 +000077 case CmpInst::ICMP_ULE: {
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000078 APInt UMax(CR.getUnsignedMax());
79 if (UMax.isMaxValue())
80 return ConstantRange(W);
81 return ConstantRange(APInt::getMinValue(W), UMax + 1);
82 }
Frits van Bommela09d5142011-07-27 15:20:06 +000083 case CmpInst::ICMP_SLE: {
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000084 APInt SMax(CR.getSignedMax());
Nick Lewyckyf2d7b7c2010-09-28 18:18:36 +000085 if (SMax.isMaxSignedValue())
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000086 return ConstantRange(W);
87 return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
88 }
Frits van Bommela09d5142011-07-27 15:20:06 +000089 case CmpInst::ICMP_UGT: {
Nick Lewyckyf2d7b7c2010-09-28 18:18:36 +000090 APInt UMin(CR.getUnsignedMin());
91 if (UMin.isMaxValue())
92 return ConstantRange(W, /* empty */ false);
93 return ConstantRange(UMin + 1, APInt::getNullValue(W));
94 }
Frits van Bommela09d5142011-07-27 15:20:06 +000095 case CmpInst::ICMP_SGT: {
Nick Lewyckyf2d7b7c2010-09-28 18:18:36 +000096 APInt SMin(CR.getSignedMin());
97 if (SMin.isMaxSignedValue())
98 return ConstantRange(W, /* empty */ false);
99 return ConstantRange(SMin + 1, APInt::getSignedMinValue(W));
100 }
Frits van Bommela09d5142011-07-27 15:20:06 +0000101 case CmpInst::ICMP_UGE: {
Nick Lewyckybf8c7f02009-07-11 06:15:39 +0000102 APInt UMin(CR.getUnsignedMin());
103 if (UMin.isMinValue())
104 return ConstantRange(W);
105 return ConstantRange(UMin, APInt::getNullValue(W));
106 }
Frits van Bommela09d5142011-07-27 15:20:06 +0000107 case CmpInst::ICMP_SGE: {
Nick Lewyckybf8c7f02009-07-11 06:15:39 +0000108 APInt SMin(CR.getSignedMin());
109 if (SMin.isMinSignedValue())
110 return ConstantRange(W);
111 return ConstantRange(SMin, APInt::getSignedMinValue(W));
112 }
113 }
114}
115
Chris Lattner645e00d2002-09-01 23:53:36 +0000116/// isFullSet - Return true if this set contains all of the elements possible
117/// for this data-type
118bool ConstantRange::isFullSet() const {
Zhou Shengc125c002007-04-26 16:42:07 +0000119 return Lower == Upper && Lower.isMaxValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000120}
Misha Brukmanfd939082005-04-21 23:48:37 +0000121
Chris Lattner645e00d2002-09-01 23:53:36 +0000122/// isEmptySet - Return true if this set contains no members.
123///
124bool ConstantRange::isEmptySet() const {
Zhou Shengc125c002007-04-26 16:42:07 +0000125 return Lower == Upper && Lower.isMinValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000126}
127
128/// isWrappedSet - Return true if this set wraps around the top of the range,
129/// for example: [100, 8)
130///
Reid Spencera6e8a952007-03-01 07:54:15 +0000131bool ConstantRange::isWrappedSet() const {
Reid Spencer663e7112007-02-28 17:36:23 +0000132 return Lower.ugt(Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000133}
134
Nick Lewycky32cda112010-09-06 23:52:49 +0000135/// isSignWrappedSet - Return true if this set wraps around the INT_MIN of
136/// its bitwidth, for example: i8 [120, 140).
137///
138bool ConstantRange::isSignWrappedSet() const {
139 return contains(APInt::getSignedMaxValue(getBitWidth())) &&
140 contains(APInt::getSignedMinValue(getBitWidth()));
141}
142
Chris Lattner645e00d2002-09-01 23:53:36 +0000143/// getSetSize - Return the number of elements in this set.
144///
Reid Spencer663e7112007-02-28 17:36:23 +0000145APInt ConstantRange::getSetSize() const {
Nuno Lopes367308f2012-07-16 18:08:12 +0000146 if (isEmptySet())
147 return APInt(getBitWidth()+1, 0);
148
149 if (isFullSet())
150 return APInt::getMaxValue(getBitWidth()).zext(getBitWidth()+1) + 1;
151
152 if (isWrappedSet()) {
153 APInt Result = Upper + (APInt::getMaxValue(getBitWidth()) - Lower + 1);
154 return Result.zext(getBitWidth()+1);
Chris Lattner645e00d2002-09-01 23:53:36 +0000155 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000156
Nuno Lopes367308f2012-07-16 18:08:12 +0000157 return (Upper - Lower).zext(getBitWidth()+1);
Chris Lattner645e00d2002-09-01 23:53:36 +0000158}
159
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000160/// getUnsignedMax - Return the largest unsigned value contained in the
161/// ConstantRange.
162///
163APInt ConstantRange::getUnsignedMax() const {
164 if (isFullSet() || isWrappedSet())
165 return APInt::getMaxValue(getBitWidth());
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000166 return getUpper() - 1;
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000167}
168
169/// getUnsignedMin - Return the smallest unsigned value contained in the
170/// ConstantRange.
171///
172APInt ConstantRange::getUnsignedMin() const {
173 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
174 return APInt::getMinValue(getBitWidth());
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000175 return getLower();
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000176}
177
178/// getSignedMax - Return the largest signed value contained in the
179/// ConstantRange.
180///
181APInt ConstantRange::getSignedMax() const {
Zhou Shengdaacf222007-04-13 05:57:32 +0000182 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000183 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000184 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000185 return getUpper() - 1;
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000186 return SignedMax;
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000187 }
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000188 if (getLower().isNegative() == getUpper().isNegative())
189 return SignedMax;
190 return getUpper() - 1;
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000191}
192
193/// getSignedMin - Return the smallest signed value contained in the
194/// ConstantRange.
195///
196APInt ConstantRange::getSignedMin() const {
Zhou Shengdaacf222007-04-13 05:57:32 +0000197 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000198 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000199 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000200 return getLower();
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000201 return SignedMin;
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000202 }
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000203 if ((getUpper() - 1).slt(getLower())) {
204 if (getUpper() != SignedMin)
205 return SignedMin;
206 }
207 return getLower();
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000208}
209
Chris Lattnerfc33d302004-03-30 00:20:08 +0000210/// contains - Return true if the specified value is in the set.
211///
Reid Spencera6e8a952007-03-01 07:54:15 +0000212bool ConstantRange::contains(const APInt &V) const {
213 if (Lower == Upper)
214 return isFullSet();
Chris Lattner645e00d2002-09-01 23:53:36 +0000215
Reid Spencera6e8a952007-03-01 07:54:15 +0000216 if (!isWrappedSet())
217 return Lower.ule(V) && V.ult(Upper);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000218 return Lower.ule(V) || V.ult(Upper);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000219}
220
Nick Lewyckybf8c7f02009-07-11 06:15:39 +0000221/// contains - Return true if the argument is a subset of this range.
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000222/// Two equal sets contain each other. The empty set contained by all other
223/// sets.
Nick Lewyckybf8c7f02009-07-11 06:15:39 +0000224///
225bool ConstantRange::contains(const ConstantRange &Other) const {
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000226 if (isFullSet() || Other.isEmptySet()) return true;
227 if (isEmptySet() || Other.isFullSet()) return false;
Nick Lewyckybf8c7f02009-07-11 06:15:39 +0000228
229 if (!isWrappedSet()) {
230 if (Other.isWrappedSet())
231 return false;
232
233 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
234 }
235
236 if (!Other.isWrappedSet())
237 return Other.getUpper().ule(Upper) ||
238 Lower.ule(Other.getLower());
239
240 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
241}
242
Chris Lattnerfc33d302004-03-30 00:20:08 +0000243/// subtract - Subtract the specified constant from the endpoints of this
244/// constant range.
Reid Spencer581b0d42007-02-28 19:57:34 +0000245ConstantRange ConstantRange::subtract(const APInt &Val) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000246 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
Chris Lattnerfc33d302004-03-30 00:20:08 +0000247 // If the set is empty or full, don't modify the endpoints.
Reid Spencer663e7112007-02-28 17:36:23 +0000248 if (Lower == Upper)
249 return *this;
Reid Spencer663e7112007-02-28 17:36:23 +0000250 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000251}
Chris Lattner645e00d2002-09-01 23:53:36 +0000252
Nuno Lopes62d7afa2012-06-28 16:10:13 +0000253/// \brief Subtract the specified range from this range (aka relative complement
254/// of the sets).
255ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
256 return intersectWith(CR.inverse());
257}
258
Nick Lewycky3e051642007-02-11 00:58:49 +0000259/// intersectWith - Return the range that results from the intersection of this
Nick Lewycky3a4a8842009-07-18 06:34:42 +0000260/// range with another range. The resultant range is guaranteed to include all
261/// elements contained in both input ranges, and to have the smallest possible
262/// set size that does so. Because there may be two intersections with the
263/// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
Reid Spencera6e8a952007-03-01 07:54:15 +0000264ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000265 assert(getBitWidth() == CR.getBitWidth() &&
266 "ConstantRange types don't agree!");
Nick Lewycky377b1192007-07-14 02:51:34 +0000267
268 // Handle common cases.
269 if ( isEmptySet() || CR.isFullSet()) return *this;
270 if (CR.isEmptySet() || isFullSet()) return CR;
271
272 if (!isWrappedSet() && CR.isWrappedSet())
Nick Lewycky3a4a8842009-07-18 06:34:42 +0000273 return CR.intersectWith(*this);
Nick Lewycky377b1192007-07-14 02:51:34 +0000274
275 if (!isWrappedSet() && !CR.isWrappedSet()) {
276 if (Lower.ult(CR.Lower)) {
277 if (Upper.ule(CR.Lower))
278 return ConstantRange(getBitWidth(), false);
279
280 if (Upper.ult(CR.Upper))
281 return ConstantRange(CR.Lower, Upper);
282
283 return CR;
Nick Lewycky377b1192007-07-14 02:51:34 +0000284 }
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000285 if (Upper.ult(CR.Upper))
286 return *this;
287
288 if (Lower.ult(CR.Upper))
289 return ConstantRange(Lower, CR.Upper);
290
291 return ConstantRange(getBitWidth(), false);
Nick Lewycky377b1192007-07-14 02:51:34 +0000292 }
293
294 if (isWrappedSet() && !CR.isWrappedSet()) {
295 if (CR.Lower.ult(Upper)) {
296 if (CR.Upper.ult(Upper))
297 return CR;
298
Nuno Lopesfbb7a732012-05-18 00:14:36 +0000299 if (CR.Upper.ule(Lower))
Nick Lewycky377b1192007-07-14 02:51:34 +0000300 return ConstantRange(CR.Lower, Upper);
301
302 if (getSetSize().ult(CR.getSetSize()))
303 return *this;
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000304 return CR;
305 }
306 if (CR.Lower.ult(Lower)) {
Nick Lewycky377b1192007-07-14 02:51:34 +0000307 if (CR.Upper.ule(Lower))
308 return ConstantRange(getBitWidth(), false);
309
310 return ConstantRange(Lower, CR.Upper);
311 }
312 return CR;
313 }
314
315 if (CR.Upper.ult(Upper)) {
316 if (CR.Lower.ult(Upper)) {
317 if (getSetSize().ult(CR.getSetSize()))
318 return *this;
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000319 return CR;
Nick Lewycky377b1192007-07-14 02:51:34 +0000320 }
321
322 if (CR.Lower.ult(Lower))
323 return ConstantRange(Lower, CR.Upper);
324
325 return CR;
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000326 }
Nuno Lopes532516a2012-06-28 00:59:33 +0000327 if (CR.Upper.ule(Lower)) {
Nick Lewycky377b1192007-07-14 02:51:34 +0000328 if (CR.Lower.ult(Lower))
329 return *this;
330
331 return ConstantRange(CR.Lower, Upper);
332 }
333 if (getSetSize().ult(CR.getSetSize()))
334 return *this;
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000335 return CR;
Nick Lewycky377b1192007-07-14 02:51:34 +0000336}
337
338
Nick Lewycky3e051642007-02-11 00:58:49 +0000339/// unionWith - Return the range that results from the union of this range with
Chris Lattner645e00d2002-09-01 23:53:36 +0000340/// another range. The resultant range is guaranteed to include the elements of
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000341/// both sets, but may contain more. For example, [3, 9) union [12,15) is
342/// [3, 15), which includes 9, 10, and 11, which were not included in either
343/// set before.
Chris Lattner645e00d2002-09-01 23:53:36 +0000344///
Reid Spencera6e8a952007-03-01 07:54:15 +0000345ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000346 assert(getBitWidth() == CR.getBitWidth() &&
347 "ConstantRange types don't agree!");
Chris Lattner645e00d2002-09-01 23:53:36 +0000348
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000349 if ( isFullSet() || CR.isEmptySet()) return *this;
350 if (CR.isFullSet() || isEmptySet()) return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000351
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000352 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
353
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000354 if (!isWrappedSet() && !CR.isWrappedSet()) {
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000355 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
356 // If the two ranges are disjoint, find the smaller gap and bridge it.
357 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
358 if (d1.ult(d2))
359 return ConstantRange(Lower, CR.Upper);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000360 return ConstantRange(CR.Lower, Upper);
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000361 }
362
363 APInt L = Lower, U = Upper;
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000364 if (CR.Lower.ult(L))
365 L = CR.Lower;
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000366 if ((CR.Upper - 1).ugt(U - 1))
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000367 U = CR.Upper;
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000368
369 if (L == 0 && U == 0)
370 return ConstantRange(getBitWidth());
371
372 return ConstantRange(L, U);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000373 }
374
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000375 if (!CR.isWrappedSet()) {
376 // ------U L----- and ------U L----- : this
377 // L--U L--U : CR
378 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000379 return *this;
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000380
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000381 // ------U L----- : this
382 // L---------U : CR
383 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000384 return ConstantRange(getBitWidth());
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000385
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000386 // ----U L---- : this
387 // L---U : CR
388 // <d1> <d2>
389 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000390 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000391 if (d1.ult(d2))
392 return ConstantRange(Lower, CR.Upper);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000393 return ConstantRange(CR.Lower, Upper);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000394 }
395
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000396 // ----U L----- : this
397 // L----U : CR
398 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
399 return ConstantRange(CR.Lower, Upper);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000400
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000401 // ------U L---- : this
402 // L-----U : CR
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000403 assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) &&
404 "ConstantRange::unionWith missed a case with one range wrapped");
405 return ConstantRange(Lower, CR.Upper);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000406 }
407
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000408 // ------U L---- and ------U L---- : this
409 // -U L----------- and ------------U L : CR
410 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
411 return ConstantRange(getBitWidth());
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000412
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000413 APInt L = Lower, U = Upper;
414 if (CR.Upper.ugt(U))
415 U = CR.Upper;
416 if (CR.Lower.ult(L))
417 L = CR.Lower;
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000418
419 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000420}
Chris Lattner96f9d722002-09-02 00:18:22 +0000421
Chris Lattnerfc33d302004-03-30 00:20:08 +0000422/// zeroExtend - Return a new range in the specified integer type, which must
423/// be strictly larger than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000424/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000425/// zero extended.
Reid Spencerbb626a62007-02-28 22:02:48 +0000426ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
Nick Lewycky32cda112010-09-06 23:52:49 +0000427 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
428
Reid Spencerbb626a62007-02-28 22:02:48 +0000429 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000430 assert(SrcTySize < DstTySize && "Not a value extension");
Nick Lewycky32cda112010-09-06 23:52:49 +0000431 if (isFullSet() || isWrappedSet())
432 // Change into [0, 1 << src bit width)
Reid Spencerdc5c1592007-02-28 18:57:32 +0000433 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000434
Jay Foad40f8f622010-12-07 08:25:19 +0000435 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000436}
437
Nick Lewyckye32157c2007-04-07 15:41:33 +0000438/// signExtend - Return a new range in the specified integer type, which must
439/// be strictly larger than the current type. The returned range will
440/// correspond to the possible range of values as if the source range had been
441/// sign extended.
442ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
Nick Lewycky32cda112010-09-06 23:52:49 +0000443 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
444
Nick Lewyckye32157c2007-04-07 15:41:33 +0000445 unsigned SrcTySize = getBitWidth();
446 assert(SrcTySize < DstTySize && "Not a value extension");
Nick Lewycky32cda112010-09-06 23:52:49 +0000447 if (isFullSet() || isSignWrappedSet()) {
Nick Lewyckye32157c2007-04-07 15:41:33 +0000448 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
Nick Lewyckyff84de72009-07-13 04:17:23 +0000449 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
Nick Lewyckye32157c2007-04-07 15:41:33 +0000450 }
451
Jay Foad40f8f622010-12-07 08:25:19 +0000452 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
Nick Lewyckye32157c2007-04-07 15:41:33 +0000453}
454
Chris Lattnerfc33d302004-03-30 00:20:08 +0000455/// truncate - Return a new range in the specified integer type, which must be
456/// strictly smaller than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000457/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000458/// truncated to the specified type.
Reid Spencerbb626a62007-02-28 22:02:48 +0000459ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
Benjamin Kramerb3ff49e2011-11-24 17:24:33 +0000460 assert(getBitWidth() > DstTySize && "Not a value truncation");
461 if (isFullSet() || getSetSize().getActiveBits() > DstTySize)
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000462 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000463
Jay Foad40f8f622010-12-07 08:25:19 +0000464 return ConstantRange(Lower.trunc(DstTySize), Upper.trunc(DstTySize));
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);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000473 if (SrcTySize < DstTySize)
Nuno Lopes95a3be02009-11-09 15:36:28 +0000474 return zeroExtend(DstTySize);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000475 return *this;
Nuno Lopes95a3be02009-11-09 15:36:28 +0000476}
477
478/// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
479/// value is sign extended, truncated, or left alone to make it that width.
480ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
481 unsigned SrcTySize = getBitWidth();
482 if (SrcTySize > DstTySize)
483 return truncate(DstTySize);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000484 if (SrcTySize < DstTySize)
Nuno Lopes95a3be02009-11-09 15:36:28 +0000485 return signExtend(DstTySize);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000486 return *this;
Nuno Lopes95a3be02009-11-09 15:36:28 +0000487}
488
Dan Gohmana3755d82009-07-09 22:07:27 +0000489ConstantRange
490ConstantRange::add(const ConstantRange &Other) const {
491 if (isEmptySet() || Other.isEmptySet())
492 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewyckycf9e07d2009-07-13 02:49:08 +0000493 if (isFullSet() || Other.isFullSet())
494 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohmana3755d82009-07-09 22:07:27 +0000495
496 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
497 APInt NewLower = getLower() + Other.getLower();
498 APInt NewUpper = getUpper() + Other.getUpper() - 1;
499 if (NewLower == NewUpper)
500 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
501
502 ConstantRange X = ConstantRange(NewLower, NewUpper);
503 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
504 // We've wrapped, therefore, full set.
505 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
506
507 return X;
Chris Lattner96f9d722002-09-02 00:18:22 +0000508}
509
Dan Gohmana3755d82009-07-09 22:07:27 +0000510ConstantRange
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000511ConstantRange::sub(const ConstantRange &Other) const {
512 if (isEmptySet() || Other.isEmptySet())
513 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
514 if (isFullSet() || Other.isFullSet())
515 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
516
517 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
Nick Lewyckye6240e82011-06-22 21:13:46 +0000518 APInt NewLower = getLower() - Other.getUpper() + 1;
519 APInt NewUpper = getUpper() - Other.getLower();
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000520 if (NewLower == NewUpper)
521 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
522
523 ConstantRange X = ConstantRange(NewLower, NewUpper);
524 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
525 // We've wrapped, therefore, full set.
526 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
527
528 return X;
529}
530
531ConstantRange
Dan Gohmana3755d82009-07-09 22:07:27 +0000532ConstantRange::multiply(const ConstantRange &Other) const {
Dan Gohman8dcc58e2010-01-26 15:56:18 +0000533 // TODO: If either operand is a single element and the multiply is known to
534 // be non-wrapping, round the result min and max value to the appropriate
535 // multiple of that element. If wrapping is possible, at least adjust the
536 // range according to the greatest power-of-two factor of the single element.
Dan Gohman153f1eb2010-01-26 04:13:15 +0000537
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000538 if (isEmptySet() || Other.isEmptySet())
539 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
540 if (isFullSet() || Other.isFullSet())
541 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
542
Nick Lewyckyf1db1202009-07-13 03:27:41 +0000543 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
544 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
545 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
546 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000547
Nick Lewyckyf1db1202009-07-13 03:27:41 +0000548 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
549 this_max * Other_max + 1);
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000550 return Result_zext.truncate(getBitWidth());
Dan Gohmana3755d82009-07-09 22:07:27 +0000551}
552
553ConstantRange
554ConstantRange::smax(const ConstantRange &Other) const {
Dan Gohman38b06442009-07-09 23:16:10 +0000555 // X smax Y is: range(smax(X_smin, Y_smin),
556 // smax(X_smax, Y_smax))
557 if (isEmptySet() || Other.isEmptySet())
558 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohman38b06442009-07-09 23:16:10 +0000559 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
560 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
561 if (NewU == NewL)
562 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
563 return ConstantRange(NewL, NewU);
Dan Gohmana3755d82009-07-09 22:07:27 +0000564}
565
566ConstantRange
567ConstantRange::umax(const ConstantRange &Other) const {
568 // X umax Y is: range(umax(X_umin, Y_umin),
569 // umax(X_umax, Y_umax))
570 if (isEmptySet() || Other.isEmptySet())
571 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohmana3755d82009-07-09 22:07:27 +0000572 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
573 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
574 if (NewU == NewL)
575 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
576 return ConstantRange(NewL, NewU);
577}
578
579ConstantRange
Nick Lewycky956daf02009-07-12 05:18:18 +0000580ConstantRange::udiv(const ConstantRange &RHS) const {
581 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
582 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
583 if (RHS.isFullSet())
584 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
585
586 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
587
588 APInt RHS_umin = RHS.getUnsignedMin();
589 if (RHS_umin == 0) {
590 // We want the lowest value in RHS excluding zero. Usually that would be 1
591 // except for a range in the form of [X, 1) in which case it would be X.
592 if (RHS.getUpper() == 1)
593 RHS_umin = RHS.getLower();
594 else
595 RHS_umin = APInt(getBitWidth(), 1);
596 }
597
598 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
599
600 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
601 // this could occur.
602 if (Lower == Upper)
603 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
604
605 return ConstantRange(Lower, Upper);
Dan Gohmana3755d82009-07-09 22:07:27 +0000606}
607
Nuno Lopes34e992d2009-11-12 14:53:53 +0000608ConstantRange
Nick Lewycky198381e2010-09-07 05:39:02 +0000609ConstantRange::binaryAnd(const ConstantRange &Other) const {
610 if (isEmptySet() || Other.isEmptySet())
611 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
612
613 // TODO: replace this with something less conservative
614
615 APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax());
616 if (umin.isAllOnesValue())
617 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
618 return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1);
619}
620
621ConstantRange
622ConstantRange::binaryOr(const ConstantRange &Other) const {
623 if (isEmptySet() || Other.isEmptySet())
624 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
625
626 // TODO: replace this with something less conservative
627
628 APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
629 if (umax.isMinValue())
630 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
631 return ConstantRange(umax, APInt::getNullValue(getBitWidth()));
632}
633
634ConstantRange
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000635ConstantRange::shl(const ConstantRange &Other) const {
636 if (isEmptySet() || Other.isEmptySet())
637 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000638
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000639 APInt min = getUnsignedMin().shl(Other.getUnsignedMin());
640 APInt max = getUnsignedMax().shl(Other.getUnsignedMax());
Nuno Lopes34e992d2009-11-12 14:53:53 +0000641
642 // there's no overflow!
Nuno Lopes44591452009-11-12 15:10:33 +0000643 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000644 if (Zeros.ugt(Other.getUnsignedMax()))
645 return ConstantRange(min, max + 1);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000646
647 // FIXME: implement the other tricky cases
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000648 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000649}
650
651ConstantRange
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000652ConstantRange::lshr(const ConstantRange &Other) const {
653 if (isEmptySet() || Other.isEmptySet())
654 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000655
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000656 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin());
657 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
658 if (min == max + 1)
659 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
660
661 return ConstantRange(min, max + 1);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000662}
663
Owen Anderson9773e452010-08-07 05:47:46 +0000664ConstantRange ConstantRange::inverse() const {
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000665 if (isFullSet())
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000666 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000667 if (isEmptySet())
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000668 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Owen Anderson9773e452010-08-07 05:47:46 +0000669 return ConstantRange(Upper, Lower);
670}
671
Dan Gohman38b06442009-07-09 23:16:10 +0000672/// print - Print out the bounds to a stream...
Chris Lattner96f9d722002-09-02 00:18:22 +0000673///
Dan Gohman38b06442009-07-09 23:16:10 +0000674void ConstantRange::print(raw_ostream &OS) const {
Dan Gohmandf6d5e02010-01-26 04:12:55 +0000675 if (isFullSet())
676 OS << "full-set";
677 else if (isEmptySet())
678 OS << "empty-set";
679 else
680 OS << "[" << Lower << "," << Upper << ")";
Dan Gohmana3755d82009-07-09 22:07:27 +0000681}
682
Dan Gohman38b06442009-07-09 23:16:10 +0000683/// dump - Allow printing from a debugger easily...
Dan Gohmana3755d82009-07-09 22:07:27 +0000684///
Dan Gohman38b06442009-07-09 23:16:10 +0000685void ConstantRange::dump() const {
David Greene7fd5fb42010-01-05 01:28:32 +0000686 print(dbgs());
Dan Gohmana3755d82009-07-09 22:07:27 +0000687}