blob: bb38cd15e634144b987ba461e94e7c6338445fea [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
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000024#include "llvm/IR/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///
Benjamin Kramer318b7cc2013-07-11 15:37:27 +000041ConstantRange::ConstantRange(APIntMoveTy V)
42 : Lower(llvm_move(V)), Upper(Lower + 1) {}
Chris Lattner645e00d2002-09-01 23:53:36 +000043
Benjamin Kramer318b7cc2013-07-11 15:37:27 +000044ConstantRange::ConstantRange(APIntMoveTy L, APIntMoveTy U)
45 : Lower(llvm_move(L)), Upper(llvm_move(U)) {
46 assert(Lower.getBitWidth() == Upper.getBitWidth() &&
Dan Gohman38b06442009-07-09 23:16:10 +000047 "ConstantRange with unequal bit widths");
Benjamin Kramer318b7cc2013-07-11 15:37:27 +000048 assert((Lower != Upper || (Lower.isMaxValue() || Lower.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) {
Nick Lewyckyf2d7b7c2010-09-28 18:18:36 +000054 if (CR.isEmptySet())
55 return CR;
56
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000057 uint32_t W = CR.getBitWidth();
58 switch (Pred) {
Craig Topper85814382012-02-07 05:05:23 +000059 default: llvm_unreachable("Invalid ICmp predicate to makeICmpRegion()");
Frits van Bommela09d5142011-07-27 15:20:06 +000060 case CmpInst::ICMP_EQ:
Nick Lewyckyf067a232009-07-11 17:04:01 +000061 return CR;
Frits van Bommela09d5142011-07-27 15:20:06 +000062 case CmpInst::ICMP_NE:
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000063 if (CR.isSingleElement())
64 return ConstantRange(CR.getUpper(), CR.getLower());
65 return ConstantRange(W);
Frits van Bommela09d5142011-07-27 15:20:06 +000066 case CmpInst::ICMP_ULT: {
Nick Lewyckyf2d7b7c2010-09-28 18:18:36 +000067 APInt UMax(CR.getUnsignedMax());
68 if (UMax.isMinValue())
69 return ConstantRange(W, /* empty */ false);
70 return ConstantRange(APInt::getMinValue(W), UMax);
71 }
Frits van Bommela09d5142011-07-27 15:20:06 +000072 case CmpInst::ICMP_SLT: {
Nick Lewyckyf2d7b7c2010-09-28 18:18:36 +000073 APInt SMax(CR.getSignedMax());
74 if (SMax.isMinSignedValue())
75 return ConstantRange(W, /* empty */ false);
76 return ConstantRange(APInt::getSignedMinValue(W), SMax);
77 }
Frits van Bommela09d5142011-07-27 15:20:06 +000078 case CmpInst::ICMP_ULE: {
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000079 APInt UMax(CR.getUnsignedMax());
80 if (UMax.isMaxValue())
81 return ConstantRange(W);
82 return ConstantRange(APInt::getMinValue(W), UMax + 1);
83 }
Frits van Bommela09d5142011-07-27 15:20:06 +000084 case CmpInst::ICMP_SLE: {
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000085 APInt SMax(CR.getSignedMax());
Nick Lewyckyf2d7b7c2010-09-28 18:18:36 +000086 if (SMax.isMaxSignedValue())
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000087 return ConstantRange(W);
88 return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
89 }
Frits van Bommela09d5142011-07-27 15:20:06 +000090 case CmpInst::ICMP_UGT: {
Nick Lewyckyf2d7b7c2010-09-28 18:18:36 +000091 APInt UMin(CR.getUnsignedMin());
92 if (UMin.isMaxValue())
93 return ConstantRange(W, /* empty */ false);
94 return ConstantRange(UMin + 1, APInt::getNullValue(W));
95 }
Frits van Bommela09d5142011-07-27 15:20:06 +000096 case CmpInst::ICMP_SGT: {
Nick Lewyckyf2d7b7c2010-09-28 18:18:36 +000097 APInt SMin(CR.getSignedMin());
98 if (SMin.isMaxSignedValue())
99 return ConstantRange(W, /* empty */ false);
100 return ConstantRange(SMin + 1, APInt::getSignedMinValue(W));
101 }
Frits van Bommela09d5142011-07-27 15:20:06 +0000102 case CmpInst::ICMP_UGE: {
Nick Lewyckybf8c7f02009-07-11 06:15:39 +0000103 APInt UMin(CR.getUnsignedMin());
104 if (UMin.isMinValue())
105 return ConstantRange(W);
106 return ConstantRange(UMin, APInt::getNullValue(W));
107 }
Frits van Bommela09d5142011-07-27 15:20:06 +0000108 case CmpInst::ICMP_SGE: {
Nick Lewyckybf8c7f02009-07-11 06:15:39 +0000109 APInt SMin(CR.getSignedMin());
110 if (SMin.isMinSignedValue())
111 return ConstantRange(W);
112 return ConstantRange(SMin, APInt::getSignedMinValue(W));
113 }
114 }
115}
116
Chris Lattner645e00d2002-09-01 23:53:36 +0000117/// isFullSet - Return true if this set contains all of the elements possible
118/// for this data-type
119bool ConstantRange::isFullSet() const {
Zhou Shengc125c002007-04-26 16:42:07 +0000120 return Lower == Upper && Lower.isMaxValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000121}
Misha Brukmanfd939082005-04-21 23:48:37 +0000122
Chris Lattner645e00d2002-09-01 23:53:36 +0000123/// isEmptySet - Return true if this set contains no members.
124///
125bool ConstantRange::isEmptySet() const {
Zhou Shengc125c002007-04-26 16:42:07 +0000126 return Lower == Upper && Lower.isMinValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000127}
128
129/// isWrappedSet - Return true if this set wraps around the top of the range,
130/// for example: [100, 8)
131///
Reid Spencera6e8a952007-03-01 07:54:15 +0000132bool ConstantRange::isWrappedSet() const {
Reid Spencer663e7112007-02-28 17:36:23 +0000133 return Lower.ugt(Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000134}
135
Nick Lewycky32cda112010-09-06 23:52:49 +0000136/// isSignWrappedSet - Return true if this set wraps around the INT_MIN of
137/// its bitwidth, for example: i8 [120, 140).
138///
139bool ConstantRange::isSignWrappedSet() const {
140 return contains(APInt::getSignedMaxValue(getBitWidth())) &&
141 contains(APInt::getSignedMinValue(getBitWidth()));
142}
143
Chris Lattner645e00d2002-09-01 23:53:36 +0000144/// getSetSize - Return the number of elements in this set.
145///
Reid Spencer663e7112007-02-28 17:36:23 +0000146APInt ConstantRange::getSetSize() const {
Nuno Lopes367308f2012-07-16 18:08:12 +0000147 if (isEmptySet())
148 return APInt(getBitWidth()+1, 0);
149
Nuno Lopes5d2fada2012-07-17 15:43:59 +0000150 if (isFullSet()) {
151 APInt Size(getBitWidth()+1, 0);
152 Size.setBit(getBitWidth());
153 return Size;
Chris Lattner645e00d2002-09-01 23:53:36 +0000154 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000155
Nuno Lopes5d2fada2012-07-17 15:43:59 +0000156 // This is also correct for wrapped sets.
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");
Nuno Lopesb42729b2012-07-23 20:33:29 +0000431 if (isFullSet() || isWrappedSet()) {
Nick Lewycky32cda112010-09-06 23:52:49 +0000432 // Change into [0, 1 << src bit width)
Nuno Lopesb42729b2012-07-23 20:33:29 +0000433 APInt LowerExt(DstTySize, 0);
434 if (!Upper) // special case: [X, 0) -- not really wrapping around
435 LowerExt = Lower.zext(DstTySize);
Benjamin Kramer0a230e02013-07-11 16:05:50 +0000436 return ConstantRange(LowerExt, APInt::getOneBitSet(DstTySize, SrcTySize));
Nuno Lopesb42729b2012-07-23 20:33:29 +0000437 }
Chris Lattnerfc33d302004-03-30 00:20:08 +0000438
Jay Foad40f8f622010-12-07 08:25:19 +0000439 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000440}
441
Nick Lewyckye32157c2007-04-07 15:41:33 +0000442/// signExtend - Return a new range in the specified integer type, which must
443/// be strictly larger than the current type. The returned range will
444/// correspond to the possible range of values as if the source range had been
445/// sign extended.
446ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
Nick Lewycky32cda112010-09-06 23:52:49 +0000447 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
448
Nick Lewyckye32157c2007-04-07 15:41:33 +0000449 unsigned SrcTySize = getBitWidth();
450 assert(SrcTySize < DstTySize && "Not a value extension");
Nick Lewycky32cda112010-09-06 23:52:49 +0000451 if (isFullSet() || isSignWrappedSet()) {
Nick Lewyckye32157c2007-04-07 15:41:33 +0000452 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
Nick Lewyckyff84de72009-07-13 04:17:23 +0000453 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
Nick Lewyckye32157c2007-04-07 15:41:33 +0000454 }
455
Jay Foad40f8f622010-12-07 08:25:19 +0000456 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
Nick Lewyckye32157c2007-04-07 15:41:33 +0000457}
458
Chris Lattnerfc33d302004-03-30 00:20:08 +0000459/// truncate - Return a new range in the specified integer type, which must be
460/// strictly smaller than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000461/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000462/// truncated to the specified type.
Reid Spencerbb626a62007-02-28 22:02:48 +0000463ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
Benjamin Kramerb3ff49e2011-11-24 17:24:33 +0000464 assert(getBitWidth() > DstTySize && "Not a value truncation");
Nuno Lopes55c9ecb2012-07-19 16:27:45 +0000465 if (isEmptySet())
466 return ConstantRange(DstTySize, /*isFullSet=*/false);
467 if (isFullSet())
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000468 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000469
Nuno Lopes55c9ecb2012-07-19 16:27:45 +0000470 APInt MaxValue = APInt::getMaxValue(DstTySize).zext(getBitWidth());
471 APInt MaxBitValue(getBitWidth(), 0);
472 MaxBitValue.setBit(DstTySize);
473
474 APInt LowerDiv(Lower), UpperDiv(Upper);
475 ConstantRange Union(DstTySize, /*isFullSet=*/false);
476
477 // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue]
478 // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and
479 // then we do the union with [MaxValue, Upper)
480 if (isWrappedSet()) {
481 // if Upper is greater than Max Value, it covers the whole truncated range.
482 if (Upper.uge(MaxValue))
483 return ConstantRange(DstTySize, /*isFullSet=*/true);
484
485 Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
486 UpperDiv = APInt::getMaxValue(getBitWidth());
487
488 // Union covers the MaxValue case, so return if the remaining range is just
489 // MaxValue.
490 if (LowerDiv == UpperDiv)
491 return Union;
492 }
493
494 // Chop off the most significant bits that are past the destination bitwidth.
495 if (LowerDiv.uge(MaxValue)) {
496 APInt Div(getBitWidth(), 0);
497 APInt::udivrem(LowerDiv, MaxBitValue, Div, LowerDiv);
498 UpperDiv = UpperDiv - MaxBitValue * Div;
499 }
500
501 if (UpperDiv.ule(MaxValue))
502 return ConstantRange(LowerDiv.trunc(DstTySize),
503 UpperDiv.trunc(DstTySize)).unionWith(Union);
504
505 // The truncated value wrapps around. Check if we can do better than fullset.
506 APInt UpperModulo = UpperDiv - MaxBitValue;
507 if (UpperModulo.ult(LowerDiv))
508 return ConstantRange(LowerDiv.trunc(DstTySize),
509 UpperModulo.trunc(DstTySize)).unionWith(Union);
510
511 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000512}
513
Nuno Lopes95a3be02009-11-09 15:36:28 +0000514/// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
515/// value is zero extended, truncated, or left alone to make it that width.
516ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
517 unsigned SrcTySize = getBitWidth();
518 if (SrcTySize > DstTySize)
519 return truncate(DstTySize);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000520 if (SrcTySize < DstTySize)
Nuno Lopes95a3be02009-11-09 15:36:28 +0000521 return zeroExtend(DstTySize);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000522 return *this;
Nuno Lopes95a3be02009-11-09 15:36:28 +0000523}
524
525/// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
526/// value is sign extended, truncated, or left alone to make it that width.
527ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
528 unsigned SrcTySize = getBitWidth();
529 if (SrcTySize > DstTySize)
530 return truncate(DstTySize);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000531 if (SrcTySize < DstTySize)
Nuno Lopes95a3be02009-11-09 15:36:28 +0000532 return signExtend(DstTySize);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000533 return *this;
Nuno Lopes95a3be02009-11-09 15:36:28 +0000534}
535
Dan Gohmana3755d82009-07-09 22:07:27 +0000536ConstantRange
537ConstantRange::add(const ConstantRange &Other) const {
538 if (isEmptySet() || Other.isEmptySet())
539 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewyckycf9e07d2009-07-13 02:49:08 +0000540 if (isFullSet() || Other.isFullSet())
541 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohmana3755d82009-07-09 22:07:27 +0000542
543 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
544 APInt NewLower = getLower() + Other.getLower();
545 APInt NewUpper = getUpper() + Other.getUpper() - 1;
546 if (NewLower == NewUpper)
547 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
548
549 ConstantRange X = ConstantRange(NewLower, NewUpper);
550 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
551 // We've wrapped, therefore, full set.
552 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
553
554 return X;
Chris Lattner96f9d722002-09-02 00:18:22 +0000555}
556
Dan Gohmana3755d82009-07-09 22:07:27 +0000557ConstantRange
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000558ConstantRange::sub(const ConstantRange &Other) const {
559 if (isEmptySet() || Other.isEmptySet())
560 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
561 if (isFullSet() || Other.isFullSet())
562 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
563
564 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
Nick Lewyckye6240e82011-06-22 21:13:46 +0000565 APInt NewLower = getLower() - Other.getUpper() + 1;
566 APInt NewUpper = getUpper() - Other.getLower();
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000567 if (NewLower == NewUpper)
568 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
569
570 ConstantRange X = ConstantRange(NewLower, NewUpper);
571 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
572 // We've wrapped, therefore, full set.
573 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
574
575 return X;
576}
577
578ConstantRange
Dan Gohmana3755d82009-07-09 22:07:27 +0000579ConstantRange::multiply(const ConstantRange &Other) const {
Dan Gohman8dcc58e2010-01-26 15:56:18 +0000580 // TODO: If either operand is a single element and the multiply is known to
581 // be non-wrapping, round the result min and max value to the appropriate
582 // multiple of that element. If wrapping is possible, at least adjust the
583 // range according to the greatest power-of-two factor of the single element.
Dan Gohman153f1eb2010-01-26 04:13:15 +0000584
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000585 if (isEmptySet() || Other.isEmptySet())
586 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes7e733ea2012-07-16 20:47:16 +0000587
Nick Lewyckyf1db1202009-07-13 03:27:41 +0000588 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
589 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
590 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
591 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000592
Nick Lewyckyf1db1202009-07-13 03:27:41 +0000593 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
594 this_max * Other_max + 1);
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000595 return Result_zext.truncate(getBitWidth());
Dan Gohmana3755d82009-07-09 22:07:27 +0000596}
597
598ConstantRange
599ConstantRange::smax(const ConstantRange &Other) const {
Dan Gohman38b06442009-07-09 23:16:10 +0000600 // X smax Y is: range(smax(X_smin, Y_smin),
601 // smax(X_smax, Y_smax))
602 if (isEmptySet() || Other.isEmptySet())
603 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohman38b06442009-07-09 23:16:10 +0000604 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
605 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
606 if (NewU == NewL)
607 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
608 return ConstantRange(NewL, NewU);
Dan Gohmana3755d82009-07-09 22:07:27 +0000609}
610
611ConstantRange
612ConstantRange::umax(const ConstantRange &Other) const {
613 // X umax Y is: range(umax(X_umin, Y_umin),
614 // umax(X_umax, Y_umax))
615 if (isEmptySet() || Other.isEmptySet())
616 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohmana3755d82009-07-09 22:07:27 +0000617 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
618 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
619 if (NewU == NewL)
620 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
621 return ConstantRange(NewL, NewU);
622}
623
624ConstantRange
Nick Lewycky956daf02009-07-12 05:18:18 +0000625ConstantRange::udiv(const ConstantRange &RHS) const {
626 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
627 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
628 if (RHS.isFullSet())
629 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
630
631 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
632
633 APInt RHS_umin = RHS.getUnsignedMin();
634 if (RHS_umin == 0) {
635 // We want the lowest value in RHS excluding zero. Usually that would be 1
636 // except for a range in the form of [X, 1) in which case it would be X.
637 if (RHS.getUpper() == 1)
638 RHS_umin = RHS.getLower();
639 else
640 RHS_umin = APInt(getBitWidth(), 1);
641 }
642
643 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
644
645 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
646 // this could occur.
647 if (Lower == Upper)
648 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
649
650 return ConstantRange(Lower, Upper);
Dan Gohmana3755d82009-07-09 22:07:27 +0000651}
652
Nuno Lopes34e992d2009-11-12 14:53:53 +0000653ConstantRange
Nick Lewycky198381e2010-09-07 05:39:02 +0000654ConstantRange::binaryAnd(const ConstantRange &Other) const {
655 if (isEmptySet() || Other.isEmptySet())
656 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
657
658 // TODO: replace this with something less conservative
659
660 APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax());
661 if (umin.isAllOnesValue())
662 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
663 return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1);
664}
665
666ConstantRange
667ConstantRange::binaryOr(const ConstantRange &Other) const {
668 if (isEmptySet() || Other.isEmptySet())
669 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
670
671 // TODO: replace this with something less conservative
672
673 APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
674 if (umax.isMinValue())
675 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
676 return ConstantRange(umax, APInt::getNullValue(getBitWidth()));
677}
678
679ConstantRange
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000680ConstantRange::shl(const ConstantRange &Other) const {
681 if (isEmptySet() || Other.isEmptySet())
682 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000683
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000684 APInt min = getUnsignedMin().shl(Other.getUnsignedMin());
685 APInt max = getUnsignedMax().shl(Other.getUnsignedMax());
Nuno Lopes34e992d2009-11-12 14:53:53 +0000686
687 // there's no overflow!
Nuno Lopes44591452009-11-12 15:10:33 +0000688 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000689 if (Zeros.ugt(Other.getUnsignedMax()))
690 return ConstantRange(min, max + 1);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000691
692 // FIXME: implement the other tricky cases
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000693 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000694}
695
696ConstantRange
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000697ConstantRange::lshr(const ConstantRange &Other) const {
698 if (isEmptySet() || Other.isEmptySet())
699 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000700
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000701 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin());
702 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
703 if (min == max + 1)
704 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
705
706 return ConstantRange(min, max + 1);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000707}
708
Owen Anderson9773e452010-08-07 05:47:46 +0000709ConstantRange ConstantRange::inverse() const {
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000710 if (isFullSet())
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000711 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000712 if (isEmptySet())
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000713 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Owen Anderson9773e452010-08-07 05:47:46 +0000714 return ConstantRange(Upper, Lower);
715}
716
Dan Gohman38b06442009-07-09 23:16:10 +0000717/// print - Print out the bounds to a stream...
Chris Lattner96f9d722002-09-02 00:18:22 +0000718///
Dan Gohman38b06442009-07-09 23:16:10 +0000719void ConstantRange::print(raw_ostream &OS) const {
Dan Gohmandf6d5e02010-01-26 04:12:55 +0000720 if (isFullSet())
721 OS << "full-set";
722 else if (isEmptySet())
723 OS << "empty-set";
724 else
725 OS << "[" << Lower << "," << Upper << ")";
Dan Gohmana3755d82009-07-09 22:07:27 +0000726}
727
Dan Gohman38b06442009-07-09 23:16:10 +0000728/// dump - Allow printing from a debugger easily...
Dan Gohmana3755d82009-07-09 22:07:27 +0000729///
Dan Gohman38b06442009-07-09 23:16:10 +0000730void ConstantRange::dump() const {
David Greene7fd5fb42010-01-05 01:28:32 +0000731 print(dbgs());
Dan Gohmana3755d82009-07-09 22:07:27 +0000732}