blob: 265b6e96a74698486da4881795f8d986bc985faf [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 Lopes5d2fada2012-07-17 15:43:59 +0000147 if (isFullSet()) {
148 APInt Size(getBitWidth()+1, 0);
149 Size.setBit(getBitWidth());
150 return Size;
Chris Lattner645e00d2002-09-01 23:53:36 +0000151 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000152
Nuno Lopes5d2fada2012-07-17 15:43:59 +0000153 // This is also correct for wrapped sets.
Nuno Lopes367308f2012-07-16 18:08:12 +0000154 return (Upper - Lower).zext(getBitWidth()+1);
Chris Lattner645e00d2002-09-01 23:53:36 +0000155}
156
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000157/// getUnsignedMax - Return the largest unsigned value contained in the
158/// ConstantRange.
159///
160APInt ConstantRange::getUnsignedMax() const {
161 if (isFullSet() || isWrappedSet())
162 return APInt::getMaxValue(getBitWidth());
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000163 return getUpper() - 1;
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000164}
165
166/// getUnsignedMin - Return the smallest unsigned value contained in the
167/// ConstantRange.
168///
169APInt ConstantRange::getUnsignedMin() const {
170 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
171 return APInt::getMinValue(getBitWidth());
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000172 return getLower();
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000173}
174
175/// getSignedMax - Return the largest signed value contained in the
176/// ConstantRange.
177///
178APInt ConstantRange::getSignedMax() const {
Zhou Shengdaacf222007-04-13 05:57:32 +0000179 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000180 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000181 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000182 return getUpper() - 1;
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000183 return SignedMax;
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000184 }
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000185 if (getLower().isNegative() == getUpper().isNegative())
186 return SignedMax;
187 return getUpper() - 1;
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000188}
189
190/// getSignedMin - Return the smallest signed value contained in the
191/// ConstantRange.
192///
193APInt ConstantRange::getSignedMin() const {
Zhou Shengdaacf222007-04-13 05:57:32 +0000194 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000195 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000196 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000197 return getLower();
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000198 return SignedMin;
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000199 }
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000200 if ((getUpper() - 1).slt(getLower())) {
201 if (getUpper() != SignedMin)
202 return SignedMin;
203 }
204 return getLower();
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000205}
206
Chris Lattnerfc33d302004-03-30 00:20:08 +0000207/// contains - Return true if the specified value is in the set.
208///
Reid Spencera6e8a952007-03-01 07:54:15 +0000209bool ConstantRange::contains(const APInt &V) const {
210 if (Lower == Upper)
211 return isFullSet();
Chris Lattner645e00d2002-09-01 23:53:36 +0000212
Reid Spencera6e8a952007-03-01 07:54:15 +0000213 if (!isWrappedSet())
214 return Lower.ule(V) && V.ult(Upper);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000215 return Lower.ule(V) || V.ult(Upper);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000216}
217
Nick Lewyckybf8c7f02009-07-11 06:15:39 +0000218/// contains - Return true if the argument is a subset of this range.
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000219/// Two equal sets contain each other. The empty set contained by all other
220/// sets.
Nick Lewyckybf8c7f02009-07-11 06:15:39 +0000221///
222bool ConstantRange::contains(const ConstantRange &Other) const {
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000223 if (isFullSet() || Other.isEmptySet()) return true;
224 if (isEmptySet() || Other.isFullSet()) return false;
Nick Lewyckybf8c7f02009-07-11 06:15:39 +0000225
226 if (!isWrappedSet()) {
227 if (Other.isWrappedSet())
228 return false;
229
230 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
231 }
232
233 if (!Other.isWrappedSet())
234 return Other.getUpper().ule(Upper) ||
235 Lower.ule(Other.getLower());
236
237 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
238}
239
Chris Lattnerfc33d302004-03-30 00:20:08 +0000240/// subtract - Subtract the specified constant from the endpoints of this
241/// constant range.
Reid Spencer581b0d42007-02-28 19:57:34 +0000242ConstantRange ConstantRange::subtract(const APInt &Val) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000243 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
Chris Lattnerfc33d302004-03-30 00:20:08 +0000244 // If the set is empty or full, don't modify the endpoints.
Reid Spencer663e7112007-02-28 17:36:23 +0000245 if (Lower == Upper)
246 return *this;
Reid Spencer663e7112007-02-28 17:36:23 +0000247 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000248}
Chris Lattner645e00d2002-09-01 23:53:36 +0000249
Nuno Lopes62d7afa2012-06-28 16:10:13 +0000250/// \brief Subtract the specified range from this range (aka relative complement
251/// of the sets).
252ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
253 return intersectWith(CR.inverse());
254}
255
Nick Lewycky3e051642007-02-11 00:58:49 +0000256/// intersectWith - Return the range that results from the intersection of this
Nick Lewycky3a4a8842009-07-18 06:34:42 +0000257/// range with another range. The resultant range is guaranteed to include all
258/// elements contained in both input ranges, and to have the smallest possible
259/// set size that does so. Because there may be two intersections with the
260/// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
Reid Spencera6e8a952007-03-01 07:54:15 +0000261ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000262 assert(getBitWidth() == CR.getBitWidth() &&
263 "ConstantRange types don't agree!");
Nick Lewycky377b1192007-07-14 02:51:34 +0000264
265 // Handle common cases.
266 if ( isEmptySet() || CR.isFullSet()) return *this;
267 if (CR.isEmptySet() || isFullSet()) return CR;
268
269 if (!isWrappedSet() && CR.isWrappedSet())
Nick Lewycky3a4a8842009-07-18 06:34:42 +0000270 return CR.intersectWith(*this);
Nick Lewycky377b1192007-07-14 02:51:34 +0000271
272 if (!isWrappedSet() && !CR.isWrappedSet()) {
273 if (Lower.ult(CR.Lower)) {
274 if (Upper.ule(CR.Lower))
275 return ConstantRange(getBitWidth(), false);
276
277 if (Upper.ult(CR.Upper))
278 return ConstantRange(CR.Lower, Upper);
279
280 return CR;
Nick Lewycky377b1192007-07-14 02:51:34 +0000281 }
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000282 if (Upper.ult(CR.Upper))
283 return *this;
284
285 if (Lower.ult(CR.Upper))
286 return ConstantRange(Lower, CR.Upper);
287
288 return ConstantRange(getBitWidth(), false);
Nick Lewycky377b1192007-07-14 02:51:34 +0000289 }
290
291 if (isWrappedSet() && !CR.isWrappedSet()) {
292 if (CR.Lower.ult(Upper)) {
293 if (CR.Upper.ult(Upper))
294 return CR;
295
Nuno Lopesfbb7a732012-05-18 00:14:36 +0000296 if (CR.Upper.ule(Lower))
Nick Lewycky377b1192007-07-14 02:51:34 +0000297 return ConstantRange(CR.Lower, Upper);
298
299 if (getSetSize().ult(CR.getSetSize()))
300 return *this;
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000301 return CR;
302 }
303 if (CR.Lower.ult(Lower)) {
Nick Lewycky377b1192007-07-14 02:51:34 +0000304 if (CR.Upper.ule(Lower))
305 return ConstantRange(getBitWidth(), false);
306
307 return ConstantRange(Lower, CR.Upper);
308 }
309 return CR;
310 }
311
312 if (CR.Upper.ult(Upper)) {
313 if (CR.Lower.ult(Upper)) {
314 if (getSetSize().ult(CR.getSetSize()))
315 return *this;
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000316 return CR;
Nick Lewycky377b1192007-07-14 02:51:34 +0000317 }
318
319 if (CR.Lower.ult(Lower))
320 return ConstantRange(Lower, CR.Upper);
321
322 return CR;
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000323 }
Nuno Lopes532516a2012-06-28 00:59:33 +0000324 if (CR.Upper.ule(Lower)) {
Nick Lewycky377b1192007-07-14 02:51:34 +0000325 if (CR.Lower.ult(Lower))
326 return *this;
327
328 return ConstantRange(CR.Lower, Upper);
329 }
330 if (getSetSize().ult(CR.getSetSize()))
331 return *this;
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000332 return CR;
Nick Lewycky377b1192007-07-14 02:51:34 +0000333}
334
335
Nick Lewycky3e051642007-02-11 00:58:49 +0000336/// unionWith - Return the range that results from the union of this range with
Chris Lattner645e00d2002-09-01 23:53:36 +0000337/// another range. The resultant range is guaranteed to include the elements of
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000338/// both sets, but may contain more. For example, [3, 9) union [12,15) is
339/// [3, 15), which includes 9, 10, and 11, which were not included in either
340/// set before.
Chris Lattner645e00d2002-09-01 23:53:36 +0000341///
Reid Spencera6e8a952007-03-01 07:54:15 +0000342ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000343 assert(getBitWidth() == CR.getBitWidth() &&
344 "ConstantRange types don't agree!");
Chris Lattner645e00d2002-09-01 23:53:36 +0000345
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000346 if ( isFullSet() || CR.isEmptySet()) return *this;
347 if (CR.isFullSet() || isEmptySet()) return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000348
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000349 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
350
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000351 if (!isWrappedSet() && !CR.isWrappedSet()) {
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000352 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
353 // If the two ranges are disjoint, find the smaller gap and bridge it.
354 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
355 if (d1.ult(d2))
356 return ConstantRange(Lower, CR.Upper);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000357 return ConstantRange(CR.Lower, Upper);
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000358 }
359
360 APInt L = Lower, U = Upper;
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000361 if (CR.Lower.ult(L))
362 L = CR.Lower;
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000363 if ((CR.Upper - 1).ugt(U - 1))
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000364 U = CR.Upper;
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000365
366 if (L == 0 && U == 0)
367 return ConstantRange(getBitWidth());
368
369 return ConstantRange(L, U);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000370 }
371
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000372 if (!CR.isWrappedSet()) {
373 // ------U L----- and ------U L----- : this
374 // L--U L--U : CR
375 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000376 return *this;
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000377
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000378 // ------U L----- : this
379 // L---------U : CR
380 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000381 return ConstantRange(getBitWidth());
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000382
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000383 // ----U L---- : this
384 // L---U : CR
385 // <d1> <d2>
386 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000387 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000388 if (d1.ult(d2))
389 return ConstantRange(Lower, CR.Upper);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000390 return ConstantRange(CR.Lower, Upper);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000391 }
392
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000393 // ----U L----- : this
394 // L----U : CR
395 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
396 return ConstantRange(CR.Lower, Upper);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000397
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000398 // ------U L---- : this
399 // L-----U : CR
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000400 assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) &&
401 "ConstantRange::unionWith missed a case with one range wrapped");
402 return ConstantRange(Lower, CR.Upper);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000403 }
404
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000405 // ------U L---- and ------U L---- : this
406 // -U L----------- and ------------U L : CR
407 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
408 return ConstantRange(getBitWidth());
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000409
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000410 APInt L = Lower, U = Upper;
411 if (CR.Upper.ugt(U))
412 U = CR.Upper;
413 if (CR.Lower.ult(L))
414 L = CR.Lower;
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000415
416 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000417}
Chris Lattner96f9d722002-09-02 00:18:22 +0000418
Chris Lattnerfc33d302004-03-30 00:20:08 +0000419/// zeroExtend - Return a new range in the specified integer type, which must
420/// be strictly larger than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000421/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000422/// zero extended.
Reid Spencerbb626a62007-02-28 22:02:48 +0000423ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
Nick Lewycky32cda112010-09-06 23:52:49 +0000424 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
425
Reid Spencerbb626a62007-02-28 22:02:48 +0000426 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000427 assert(SrcTySize < DstTySize && "Not a value extension");
Nuno Lopesb42729b2012-07-23 20:33:29 +0000428 if (isFullSet() || isWrappedSet()) {
Nick Lewycky32cda112010-09-06 23:52:49 +0000429 // Change into [0, 1 << src bit width)
Nuno Lopesb42729b2012-07-23 20:33:29 +0000430 APInt LowerExt(DstTySize, 0);
431 if (!Upper) // special case: [X, 0) -- not really wrapping around
432 LowerExt = Lower.zext(DstTySize);
Benjamin Kramer0a230e02013-07-11 16:05:50 +0000433 return ConstantRange(LowerExt, APInt::getOneBitSet(DstTySize, SrcTySize));
Nuno Lopesb42729b2012-07-23 20:33:29 +0000434 }
Chris Lattnerfc33d302004-03-30 00:20:08 +0000435
Jay Foad40f8f622010-12-07 08:25:19 +0000436 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000437}
438
Nick Lewyckye32157c2007-04-07 15:41:33 +0000439/// signExtend - Return a new range in the specified integer type, which must
440/// be strictly larger than the current type. The returned range will
441/// correspond to the possible range of values as if the source range had been
442/// sign extended.
443ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
Nick Lewycky32cda112010-09-06 23:52:49 +0000444 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
445
Nick Lewyckye32157c2007-04-07 15:41:33 +0000446 unsigned SrcTySize = getBitWidth();
447 assert(SrcTySize < DstTySize && "Not a value extension");
Nuno Lopesd3b64ef2013-10-30 15:36:50 +0000448
449 // special case: [X, INT_MIN) -- not really wrapping around
Nuno Lopes7de1b3b2013-10-31 19:53:53 +0000450 if (Upper.isMinSignedValue())
Nuno Lopesd3b64ef2013-10-30 15:36:50 +0000451 return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize));
452
Nick Lewycky32cda112010-09-06 23:52:49 +0000453 if (isFullSet() || isSignWrappedSet()) {
Nick Lewyckye32157c2007-04-07 15:41:33 +0000454 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
Nick Lewyckyff84de72009-07-13 04:17:23 +0000455 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
Nick Lewyckye32157c2007-04-07 15:41:33 +0000456 }
457
Jay Foad40f8f622010-12-07 08:25:19 +0000458 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
Nick Lewyckye32157c2007-04-07 15:41:33 +0000459}
460
Chris Lattnerfc33d302004-03-30 00:20:08 +0000461/// truncate - Return a new range in the specified integer type, which must be
462/// strictly smaller than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000463/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000464/// truncated to the specified type.
Reid Spencerbb626a62007-02-28 22:02:48 +0000465ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
Benjamin Kramerb3ff49e2011-11-24 17:24:33 +0000466 assert(getBitWidth() > DstTySize && "Not a value truncation");
Nuno Lopes55c9ecb2012-07-19 16:27:45 +0000467 if (isEmptySet())
468 return ConstantRange(DstTySize, /*isFullSet=*/false);
469 if (isFullSet())
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000470 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000471
Nuno Lopes55c9ecb2012-07-19 16:27:45 +0000472 APInt MaxValue = APInt::getMaxValue(DstTySize).zext(getBitWidth());
473 APInt MaxBitValue(getBitWidth(), 0);
474 MaxBitValue.setBit(DstTySize);
475
476 APInt LowerDiv(Lower), UpperDiv(Upper);
477 ConstantRange Union(DstTySize, /*isFullSet=*/false);
478
479 // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue]
480 // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and
481 // then we do the union with [MaxValue, Upper)
482 if (isWrappedSet()) {
483 // if Upper is greater than Max Value, it covers the whole truncated range.
484 if (Upper.uge(MaxValue))
485 return ConstantRange(DstTySize, /*isFullSet=*/true);
486
487 Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
488 UpperDiv = APInt::getMaxValue(getBitWidth());
489
490 // Union covers the MaxValue case, so return if the remaining range is just
491 // MaxValue.
492 if (LowerDiv == UpperDiv)
493 return Union;
494 }
495
496 // Chop off the most significant bits that are past the destination bitwidth.
497 if (LowerDiv.uge(MaxValue)) {
498 APInt Div(getBitWidth(), 0);
499 APInt::udivrem(LowerDiv, MaxBitValue, Div, LowerDiv);
500 UpperDiv = UpperDiv - MaxBitValue * Div;
501 }
502
503 if (UpperDiv.ule(MaxValue))
504 return ConstantRange(LowerDiv.trunc(DstTySize),
505 UpperDiv.trunc(DstTySize)).unionWith(Union);
506
507 // The truncated value wrapps around. Check if we can do better than fullset.
508 APInt UpperModulo = UpperDiv - MaxBitValue;
509 if (UpperModulo.ult(LowerDiv))
510 return ConstantRange(LowerDiv.trunc(DstTySize),
511 UpperModulo.trunc(DstTySize)).unionWith(Union);
512
513 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000514}
515
Nuno Lopes95a3be02009-11-09 15:36:28 +0000516/// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
517/// value is zero extended, truncated, or left alone to make it that width.
518ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
519 unsigned SrcTySize = getBitWidth();
520 if (SrcTySize > DstTySize)
521 return truncate(DstTySize);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000522 if (SrcTySize < DstTySize)
Nuno Lopes95a3be02009-11-09 15:36:28 +0000523 return zeroExtend(DstTySize);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000524 return *this;
Nuno Lopes95a3be02009-11-09 15:36:28 +0000525}
526
527/// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
528/// value is sign extended, truncated, or left alone to make it that width.
529ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
530 unsigned SrcTySize = getBitWidth();
531 if (SrcTySize > DstTySize)
532 return truncate(DstTySize);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000533 if (SrcTySize < DstTySize)
Nuno Lopes95a3be02009-11-09 15:36:28 +0000534 return signExtend(DstTySize);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000535 return *this;
Nuno Lopes95a3be02009-11-09 15:36:28 +0000536}
537
Dan Gohmana3755d82009-07-09 22:07:27 +0000538ConstantRange
539ConstantRange::add(const ConstantRange &Other) const {
540 if (isEmptySet() || Other.isEmptySet())
541 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewyckycf9e07d2009-07-13 02:49:08 +0000542 if (isFullSet() || Other.isFullSet())
543 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohmana3755d82009-07-09 22:07:27 +0000544
545 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
546 APInt NewLower = getLower() + Other.getLower();
547 APInt NewUpper = getUpper() + Other.getUpper() - 1;
548 if (NewLower == NewUpper)
549 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
550
551 ConstantRange X = ConstantRange(NewLower, NewUpper);
552 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
553 // We've wrapped, therefore, full set.
554 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
555
556 return X;
Chris Lattner96f9d722002-09-02 00:18:22 +0000557}
558
Dan Gohmana3755d82009-07-09 22:07:27 +0000559ConstantRange
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000560ConstantRange::sub(const ConstantRange &Other) const {
561 if (isEmptySet() || Other.isEmptySet())
562 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
563 if (isFullSet() || Other.isFullSet())
564 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
565
566 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
Nick Lewyckye6240e82011-06-22 21:13:46 +0000567 APInt NewLower = getLower() - Other.getUpper() + 1;
568 APInt NewUpper = getUpper() - Other.getLower();
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000569 if (NewLower == NewUpper)
570 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
571
572 ConstantRange X = ConstantRange(NewLower, NewUpper);
573 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
574 // We've wrapped, therefore, full set.
575 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
576
577 return X;
578}
579
580ConstantRange
Dan Gohmana3755d82009-07-09 22:07:27 +0000581ConstantRange::multiply(const ConstantRange &Other) const {
Dan Gohman8dcc58e2010-01-26 15:56:18 +0000582 // TODO: If either operand is a single element and the multiply is known to
583 // be non-wrapping, round the result min and max value to the appropriate
584 // multiple of that element. If wrapping is possible, at least adjust the
585 // range according to the greatest power-of-two factor of the single element.
Dan Gohman153f1eb2010-01-26 04:13:15 +0000586
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000587 if (isEmptySet() || Other.isEmptySet())
588 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes7e733ea2012-07-16 20:47:16 +0000589
Nick Lewyckyf1db1202009-07-13 03:27:41 +0000590 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
591 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
592 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
593 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000594
Nick Lewyckyf1db1202009-07-13 03:27:41 +0000595 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
596 this_max * Other_max + 1);
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000597 return Result_zext.truncate(getBitWidth());
Dan Gohmana3755d82009-07-09 22:07:27 +0000598}
599
600ConstantRange
601ConstantRange::smax(const ConstantRange &Other) const {
Dan Gohman38b06442009-07-09 23:16:10 +0000602 // X smax Y is: range(smax(X_smin, Y_smin),
603 // smax(X_smax, Y_smax))
604 if (isEmptySet() || Other.isEmptySet())
605 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohman38b06442009-07-09 23:16:10 +0000606 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
607 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
608 if (NewU == NewL)
609 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
610 return ConstantRange(NewL, NewU);
Dan Gohmana3755d82009-07-09 22:07:27 +0000611}
612
613ConstantRange
614ConstantRange::umax(const ConstantRange &Other) const {
615 // X umax Y is: range(umax(X_umin, Y_umin),
616 // umax(X_umax, Y_umax))
617 if (isEmptySet() || Other.isEmptySet())
618 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohmana3755d82009-07-09 22:07:27 +0000619 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
620 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
621 if (NewU == NewL)
622 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
623 return ConstantRange(NewL, NewU);
624}
625
626ConstantRange
Nick Lewycky956daf02009-07-12 05:18:18 +0000627ConstantRange::udiv(const ConstantRange &RHS) const {
628 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
629 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
630 if (RHS.isFullSet())
631 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
632
633 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
634
635 APInt RHS_umin = RHS.getUnsignedMin();
636 if (RHS_umin == 0) {
637 // We want the lowest value in RHS excluding zero. Usually that would be 1
638 // except for a range in the form of [X, 1) in which case it would be X.
639 if (RHS.getUpper() == 1)
640 RHS_umin = RHS.getLower();
641 else
642 RHS_umin = APInt(getBitWidth(), 1);
643 }
644
645 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
646
647 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
648 // this could occur.
649 if (Lower == Upper)
650 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
651
652 return ConstantRange(Lower, Upper);
Dan Gohmana3755d82009-07-09 22:07:27 +0000653}
654
Nuno Lopes34e992d2009-11-12 14:53:53 +0000655ConstantRange
Nick Lewycky198381e2010-09-07 05:39:02 +0000656ConstantRange::binaryAnd(const ConstantRange &Other) const {
657 if (isEmptySet() || Other.isEmptySet())
658 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
659
660 // TODO: replace this with something less conservative
661
662 APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax());
663 if (umin.isAllOnesValue())
664 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
665 return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1);
666}
667
668ConstantRange
669ConstantRange::binaryOr(const ConstantRange &Other) const {
670 if (isEmptySet() || Other.isEmptySet())
671 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
672
673 // TODO: replace this with something less conservative
674
675 APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
676 if (umax.isMinValue())
677 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
678 return ConstantRange(umax, APInt::getNullValue(getBitWidth()));
679}
680
681ConstantRange
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000682ConstantRange::shl(const ConstantRange &Other) const {
683 if (isEmptySet() || Other.isEmptySet())
684 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000685
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000686 APInt min = getUnsignedMin().shl(Other.getUnsignedMin());
687 APInt max = getUnsignedMax().shl(Other.getUnsignedMax());
Nuno Lopes34e992d2009-11-12 14:53:53 +0000688
689 // there's no overflow!
Nuno Lopes44591452009-11-12 15:10:33 +0000690 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000691 if (Zeros.ugt(Other.getUnsignedMax()))
692 return ConstantRange(min, max + 1);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000693
694 // FIXME: implement the other tricky cases
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000695 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000696}
697
698ConstantRange
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000699ConstantRange::lshr(const ConstantRange &Other) const {
700 if (isEmptySet() || Other.isEmptySet())
701 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000702
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000703 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin());
704 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
705 if (min == max + 1)
706 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
707
708 return ConstantRange(min, max + 1);
Nuno Lopes34e992d2009-11-12 14:53:53 +0000709}
710
Owen Anderson9773e452010-08-07 05:47:46 +0000711ConstantRange ConstantRange::inverse() const {
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000712 if (isFullSet())
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000713 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewycky48a09ae2012-01-03 20:33:00 +0000714 if (isEmptySet())
Nick Lewycky7f9ef4b2010-08-11 22:04:36 +0000715 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Owen Anderson9773e452010-08-07 05:47:46 +0000716 return ConstantRange(Upper, Lower);
717}
718
Dan Gohman38b06442009-07-09 23:16:10 +0000719/// print - Print out the bounds to a stream...
Chris Lattner96f9d722002-09-02 00:18:22 +0000720///
Dan Gohman38b06442009-07-09 23:16:10 +0000721void ConstantRange::print(raw_ostream &OS) const {
Dan Gohmandf6d5e02010-01-26 04:12:55 +0000722 if (isFullSet())
723 OS << "full-set";
724 else if (isEmptySet())
725 OS << "empty-set";
726 else
727 OS << "[" << Lower << "," << Upper << ")";
Dan Gohmana3755d82009-07-09 22:07:27 +0000728}
729
Dan Gohman38b06442009-07-09 23:16:10 +0000730/// dump - Allow printing from a debugger easily...
Dan Gohmana3755d82009-07-09 22:07:27 +0000731///
Dan Gohman38b06442009-07-09 23:16:10 +0000732void ConstantRange::dump() const {
David Greene7fd5fb42010-01-05 01:28:32 +0000733 print(dbgs());
Dan Gohmana3755d82009-07-09 22:07:27 +0000734}