blob: 2746f7aaaa5e81328e1edc279b8e686b8b45e9c2 [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
24#include "llvm/Support/ConstantRange.h"
David Greene7fd5fb42010-01-05 01:28:32 +000025#include "llvm/Support/Debug.h"
Chris Lattner944fac72008-08-23 22:23:09 +000026#include "llvm/Support/raw_ostream.h"
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000027#include "llvm/Instructions.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///
Dan Gohman38b06442009-07-09 23:16:10 +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) {
53 uint32_t W = CR.getBitWidth();
54 switch (Pred) {
55 default: assert(!"Invalid ICmp predicate to makeICmpRegion()");
56 case ICmpInst::ICMP_EQ:
Nick Lewyckyf067a232009-07-11 17:04:01 +000057 return CR;
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000058 case ICmpInst::ICMP_NE:
59 if (CR.isSingleElement())
60 return ConstantRange(CR.getUpper(), CR.getLower());
61 return ConstantRange(W);
62 case ICmpInst::ICMP_ULT:
63 return ConstantRange(APInt::getMinValue(W), CR.getUnsignedMax());
64 case ICmpInst::ICMP_SLT:
65 return ConstantRange(APInt::getSignedMinValue(W), CR.getSignedMax());
66 case ICmpInst::ICMP_ULE: {
67 APInt UMax(CR.getUnsignedMax());
68 if (UMax.isMaxValue())
69 return ConstantRange(W);
70 return ConstantRange(APInt::getMinValue(W), UMax + 1);
71 }
72 case ICmpInst::ICMP_SLE: {
73 APInt SMax(CR.getSignedMax());
74 if (SMax.isMaxSignedValue() || (SMax+1).isMaxSignedValue())
75 return ConstantRange(W);
76 return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
77 }
78 case ICmpInst::ICMP_UGT:
79 return ConstantRange(CR.getUnsignedMin() + 1, APInt::getNullValue(W));
80 case ICmpInst::ICMP_SGT:
81 return ConstantRange(CR.getSignedMin() + 1,
82 APInt::getSignedMinValue(W));
83 case ICmpInst::ICMP_UGE: {
84 APInt UMin(CR.getUnsignedMin());
85 if (UMin.isMinValue())
86 return ConstantRange(W);
87 return ConstantRange(UMin, APInt::getNullValue(W));
88 }
89 case ICmpInst::ICMP_SGE: {
90 APInt SMin(CR.getSignedMin());
91 if (SMin.isMinSignedValue())
92 return ConstantRange(W);
93 return ConstantRange(SMin, APInt::getSignedMinValue(W));
94 }
95 }
96}
97
Chris Lattner645e00d2002-09-01 23:53:36 +000098/// isFullSet - Return true if this set contains all of the elements possible
99/// for this data-type
100bool ConstantRange::isFullSet() const {
Zhou Shengc125c002007-04-26 16:42:07 +0000101 return Lower == Upper && Lower.isMaxValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000102}
Misha Brukmanfd939082005-04-21 23:48:37 +0000103
Chris Lattner645e00d2002-09-01 23:53:36 +0000104/// isEmptySet - Return true if this set contains no members.
105///
106bool ConstantRange::isEmptySet() const {
Zhou Shengc125c002007-04-26 16:42:07 +0000107 return Lower == Upper && Lower.isMinValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000108}
109
110/// isWrappedSet - Return true if this set wraps around the top of the range,
111/// for example: [100, 8)
112///
Reid Spencera6e8a952007-03-01 07:54:15 +0000113bool ConstantRange::isWrappedSet() const {
Reid Spencer663e7112007-02-28 17:36:23 +0000114 return Lower.ugt(Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000115}
116
Chris Lattner645e00d2002-09-01 23:53:36 +0000117/// getSetSize - Return the number of elements in this set.
118///
Reid Spencer663e7112007-02-28 17:36:23 +0000119APInt ConstantRange::getSetSize() const {
120 if (isEmptySet())
Reid Spencerbb626a62007-02-28 22:02:48 +0000121 return APInt(getBitWidth(), 0);
122 if (getBitWidth() == 1) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000123 if (Lower != Upper) // One of T or F in the set...
Reid Spencerbb626a62007-02-28 22:02:48 +0000124 return APInt(2, 1);
125 return APInt(2, 2); // Must be full set...
Chris Lattner645e00d2002-09-01 23:53:36 +0000126 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000127
Chris Lattner645e00d2002-09-01 23:53:36 +0000128 // Simply subtract the bounds...
Reid Spencer663e7112007-02-28 17:36:23 +0000129 return Upper - Lower;
Chris Lattner645e00d2002-09-01 23:53:36 +0000130}
131
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000132/// getUnsignedMax - Return the largest unsigned value contained in the
133/// ConstantRange.
134///
135APInt ConstantRange::getUnsignedMax() const {
136 if (isFullSet() || isWrappedSet())
137 return APInt::getMaxValue(getBitWidth());
138 else
139 return getUpper() - 1;
140}
141
142/// getUnsignedMin - Return the smallest unsigned value contained in the
143/// ConstantRange.
144///
145APInt ConstantRange::getUnsignedMin() const {
146 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
147 return APInt::getMinValue(getBitWidth());
148 else
149 return getLower();
150}
151
152/// getSignedMax - Return the largest signed value contained in the
153/// ConstantRange.
154///
155APInt ConstantRange::getSignedMax() const {
Zhou Shengdaacf222007-04-13 05:57:32 +0000156 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000157 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000158 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000159 return getUpper() - 1;
160 else
161 return SignedMax;
162 } else {
Nick Lewycky780905e2009-07-13 04:50:21 +0000163 if (getLower().isNegative() == getUpper().isNegative())
164 return SignedMax;
165 else
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000166 return getUpper() - 1;
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000167 }
168}
169
170/// getSignedMin - Return the smallest signed value contained in the
171/// ConstantRange.
172///
173APInt ConstantRange::getSignedMin() const {
Zhou Shengdaacf222007-04-13 05:57:32 +0000174 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000175 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000176 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000177 return getLower();
178 else
179 return SignedMin;
180 } else {
181 if ((getUpper() - 1).slt(getLower())) {
182 if (getUpper() != SignedMin)
183 return SignedMin;
184 else
185 return getLower();
186 } else {
187 return getLower();
188 }
189 }
190}
191
Chris Lattnerfc33d302004-03-30 00:20:08 +0000192/// contains - Return true if the specified value is in the set.
193///
Reid Spencera6e8a952007-03-01 07:54:15 +0000194bool ConstantRange::contains(const APInt &V) const {
195 if (Lower == Upper)
196 return isFullSet();
Chris Lattner645e00d2002-09-01 23:53:36 +0000197
Reid Spencera6e8a952007-03-01 07:54:15 +0000198 if (!isWrappedSet())
199 return Lower.ule(V) && V.ult(Upper);
Reid Spencer663e7112007-02-28 17:36:23 +0000200 else
201 return Lower.ule(V) || V.ult(Upper);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000202}
203
Nick Lewyckybf8c7f02009-07-11 06:15:39 +0000204/// contains - Return true if the argument is a subset of this range.
205/// Two equal set contain each other. The empty set is considered to be
206/// contained by all other sets.
207///
208bool ConstantRange::contains(const ConstantRange &Other) const {
209 if (isFullSet()) return true;
210 if (Other.isFullSet()) return false;
211 if (Other.isEmptySet()) return true;
212 if (isEmptySet()) return false;
213
214 if (!isWrappedSet()) {
215 if (Other.isWrappedSet())
216 return false;
217
218 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
219 }
220
221 if (!Other.isWrappedSet())
222 return Other.getUpper().ule(Upper) ||
223 Lower.ule(Other.getLower());
224
225 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
226}
227
Chris Lattnerfc33d302004-03-30 00:20:08 +0000228/// subtract - Subtract the specified constant from the endpoints of this
229/// constant range.
Reid Spencer581b0d42007-02-28 19:57:34 +0000230ConstantRange ConstantRange::subtract(const APInt &Val) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000231 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
Chris Lattnerfc33d302004-03-30 00:20:08 +0000232 // If the set is empty or full, don't modify the endpoints.
Reid Spencer663e7112007-02-28 17:36:23 +0000233 if (Lower == Upper)
234 return *this;
Reid Spencer663e7112007-02-28 17:36:23 +0000235 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000236}
Chris Lattner645e00d2002-09-01 23:53:36 +0000237
238
239// intersect1Wrapped - This helper function is used to intersect two ranges when
240// it is known that LHS is wrapped and RHS isn't.
241//
Reid Spencer663e7112007-02-28 17:36:23 +0000242ConstantRange
243ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
Reid Spencera6e8a952007-03-01 07:54:15 +0000244 const ConstantRange &RHS) {
245 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
Chris Lattner645e00d2002-09-01 23:53:36 +0000246
Chris Lattner645e00d2002-09-01 23:53:36 +0000247 // Check to see if we overlap on the Left side of RHS...
248 //
Reid Spencera6e8a952007-03-01 07:54:15 +0000249 if (RHS.Lower.ult(LHS.Upper)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000250 // We do overlap on the left side of RHS, see if we overlap on the right of
251 // RHS...
Reid Spencera6e8a952007-03-01 07:54:15 +0000252 if (RHS.Upper.ugt(LHS.Lower)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000253 // Ok, the result overlaps on both the left and right sides. See if the
254 // resultant interval will be smaller if we wrap or not...
255 //
Reid Spencer663e7112007-02-28 17:36:23 +0000256 if (LHS.getSetSize().ult(RHS.getSetSize()))
Chris Lattner645e00d2002-09-01 23:53:36 +0000257 return LHS;
258 else
259 return RHS;
260
261 } else {
262 // No overlap on the right, just on the left.
Reid Spencerdc5c1592007-02-28 18:57:32 +0000263 return ConstantRange(RHS.Lower, LHS.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000264 }
Chris Lattner645e00d2002-09-01 23:53:36 +0000265 } else {
266 // We don't overlap on the left side of RHS, see if we overlap on the right
267 // of RHS...
Reid Spencera6e8a952007-03-01 07:54:15 +0000268 if (RHS.Upper.ugt(LHS.Lower)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000269 // Simple overlap...
Reid Spencerdc5c1592007-02-28 18:57:32 +0000270 return ConstantRange(LHS.Lower, RHS.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000271 } else {
272 // No overlap...
Reid Spencerbb626a62007-02-28 22:02:48 +0000273 return ConstantRange(LHS.getBitWidth(), false);
Chris Lattner645e00d2002-09-01 23:53:36 +0000274 }
275 }
276}
277
Nick Lewycky3e051642007-02-11 00:58:49 +0000278/// intersectWith - Return the range that results from the intersection of this
Nick Lewycky3a4a8842009-07-18 06:34:42 +0000279/// range with another range. The resultant range is guaranteed to include all
280/// elements contained in both input ranges, and to have the smallest possible
281/// set size that does so. Because there may be two intersections with the
282/// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
Reid Spencera6e8a952007-03-01 07:54:15 +0000283ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000284 assert(getBitWidth() == CR.getBitWidth() &&
285 "ConstantRange types don't agree!");
Nick Lewycky377b1192007-07-14 02:51:34 +0000286
287 // Handle common cases.
288 if ( isEmptySet() || CR.isFullSet()) return *this;
289 if (CR.isEmptySet() || isFullSet()) return CR;
290
291 if (!isWrappedSet() && CR.isWrappedSet())
Nick Lewycky3a4a8842009-07-18 06:34:42 +0000292 return CR.intersectWith(*this);
Nick Lewycky377b1192007-07-14 02:51:34 +0000293
294 if (!isWrappedSet() && !CR.isWrappedSet()) {
295 if (Lower.ult(CR.Lower)) {
296 if (Upper.ule(CR.Lower))
297 return ConstantRange(getBitWidth(), false);
298
299 if (Upper.ult(CR.Upper))
300 return ConstantRange(CR.Lower, Upper);
301
302 return CR;
303 } else {
304 if (Upper.ult(CR.Upper))
305 return *this;
306
307 if (Lower.ult(CR.Upper))
308 return ConstantRange(Lower, CR.Upper);
309
310 return ConstantRange(getBitWidth(), false);
311 }
312 }
313
314 if (isWrappedSet() && !CR.isWrappedSet()) {
315 if (CR.Lower.ult(Upper)) {
316 if (CR.Upper.ult(Upper))
317 return CR;
318
319 if (CR.Upper.ult(Lower))
320 return ConstantRange(CR.Lower, Upper);
321
322 if (getSetSize().ult(CR.getSetSize()))
323 return *this;
324 else
325 return CR;
326 } else if (CR.Lower.ult(Lower)) {
327 if (CR.Upper.ule(Lower))
328 return ConstantRange(getBitWidth(), false);
329
330 return ConstantRange(Lower, CR.Upper);
331 }
332 return CR;
333 }
334
335 if (CR.Upper.ult(Upper)) {
336 if (CR.Lower.ult(Upper)) {
337 if (getSetSize().ult(CR.getSetSize()))
338 return *this;
339 else
340 return CR;
341 }
342
343 if (CR.Lower.ult(Lower))
344 return ConstantRange(Lower, CR.Upper);
345
346 return CR;
347 } else if (CR.Upper.ult(Lower)) {
348 if (CR.Lower.ult(Lower))
349 return *this;
350
351 return ConstantRange(CR.Lower, Upper);
352 }
353 if (getSetSize().ult(CR.getSetSize()))
354 return *this;
355 else
356 return CR;
357}
358
359
Nick Lewycky3e051642007-02-11 00:58:49 +0000360/// unionWith - Return the range that results from the union of this range with
Chris Lattner645e00d2002-09-01 23:53:36 +0000361/// another range. The resultant range is guaranteed to include the elements of
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000362/// both sets, but may contain more. For example, [3, 9) union [12,15) is
363/// [3, 15), which includes 9, 10, and 11, which were not included in either
364/// set before.
Chris Lattner645e00d2002-09-01 23:53:36 +0000365///
Reid Spencera6e8a952007-03-01 07:54:15 +0000366ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000367 assert(getBitWidth() == CR.getBitWidth() &&
368 "ConstantRange types don't agree!");
Chris Lattner645e00d2002-09-01 23:53:36 +0000369
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000370 if ( isFullSet() || CR.isEmptySet()) return *this;
371 if (CR.isFullSet() || isEmptySet()) return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000372
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000373 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
374
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000375 if (!isWrappedSet() && !CR.isWrappedSet()) {
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000376 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
377 // If the two ranges are disjoint, find the smaller gap and bridge it.
378 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
379 if (d1.ult(d2))
380 return ConstantRange(Lower, CR.Upper);
381 else
382 return ConstantRange(CR.Lower, Upper);
383 }
384
385 APInt L = Lower, U = Upper;
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000386 if (CR.Lower.ult(L))
387 L = CR.Lower;
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000388 if ((CR.Upper - 1).ugt(U - 1))
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000389 U = CR.Upper;
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000390
391 if (L == 0 && U == 0)
392 return ConstantRange(getBitWidth());
393
394 return ConstantRange(L, U);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000395 }
396
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000397 if (!CR.isWrappedSet()) {
398 // ------U L----- and ------U L----- : this
399 // L--U L--U : CR
400 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000401 return *this;
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000402
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000403 // ------U L----- : this
404 // L---------U : CR
405 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000406 return ConstantRange(getBitWidth());
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000407
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000408 // ----U L---- : this
409 // L---U : CR
410 // <d1> <d2>
411 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000412 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000413 if (d1.ult(d2))
414 return ConstantRange(Lower, CR.Upper);
415 else
416 return ConstantRange(CR.Lower, Upper);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000417 }
418
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000419 // ----U L----- : this
420 // L----U : CR
421 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
422 return ConstantRange(CR.Lower, Upper);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000423
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000424 // ------U L---- : this
425 // L-----U : CR
426 if (CR.Lower.ult(Upper) && CR.Upper.ult(Lower))
427 return ConstantRange(Lower, CR.Upper);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000428 }
429
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000430 assert(isWrappedSet() && CR.isWrappedSet() &&
431 "ConstantRange::unionWith missed wrapped union unwrapped case");
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000432
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000433 // ------U L---- and ------U L---- : this
434 // -U L----------- and ------------U L : CR
435 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
436 return ConstantRange(getBitWidth());
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000437
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000438 APInt L = Lower, U = Upper;
439 if (CR.Upper.ugt(U))
440 U = CR.Upper;
441 if (CR.Lower.ult(L))
442 L = CR.Lower;
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000443
444 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000445}
Chris Lattner96f9d722002-09-02 00:18:22 +0000446
Chris Lattnerfc33d302004-03-30 00:20:08 +0000447/// zeroExtend - Return a new range in the specified integer type, which must
448/// be strictly larger than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000449/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000450/// zero extended.
Reid Spencerbb626a62007-02-28 22:02:48 +0000451ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
452 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000453 assert(SrcTySize < DstTySize && "Not a value extension");
Reid Spencerdc5c1592007-02-28 18:57:32 +0000454 if (isFullSet())
Chris Lattnerfc33d302004-03-30 00:20:08 +0000455 // Change a source full set into [0, 1 << 8*numbytes)
Reid Spencerdc5c1592007-02-28 18:57:32 +0000456 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000457
Reid Spencer663e7112007-02-28 17:36:23 +0000458 APInt L = Lower; L.zext(DstTySize);
459 APInt U = Upper; U.zext(DstTySize);
460 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000461}
462
Nick Lewyckye32157c2007-04-07 15:41:33 +0000463/// signExtend - Return a new range in the specified integer type, which must
464/// be strictly larger than the current type. The returned range will
465/// correspond to the possible range of values as if the source range had been
466/// sign extended.
467ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
468 unsigned SrcTySize = getBitWidth();
469 assert(SrcTySize < DstTySize && "Not a value extension");
470 if (isFullSet()) {
471 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
Nick Lewyckyff84de72009-07-13 04:17:23 +0000472 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
Nick Lewyckye32157c2007-04-07 15:41:33 +0000473 }
474
475 APInt L = Lower; L.sext(DstTySize);
476 APInt U = Upper; U.sext(DstTySize);
477 return ConstantRange(L, U);
478}
479
Chris Lattnerfc33d302004-03-30 00:20:08 +0000480/// truncate - Return a new range in the specified integer type, which must be
481/// strictly smaller than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000482/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000483/// truncated to the specified type.
Reid Spencerbb626a62007-02-28 22:02:48 +0000484ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
485 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000486 assert(SrcTySize > DstTySize && "Not a value truncation");
Zhou Shengdaacf222007-04-13 05:57:32 +0000487 APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
Reid Spencer663e7112007-02-28 17:36:23 +0000488 if (isFullSet() || getSetSize().ugt(Size))
Reid Spencerbb626a62007-02-28 22:02:48 +0000489 return ConstantRange(DstTySize);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000490
Reid Spencer663e7112007-02-28 17:36:23 +0000491 APInt L = Lower; L.trunc(DstTySize);
492 APInt U = Upper; U.trunc(DstTySize);
493 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000494}
495
Nuno Lopes95a3be02009-11-09 15:36:28 +0000496/// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
497/// value is zero extended, truncated, or left alone to make it that width.
498ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
499 unsigned SrcTySize = getBitWidth();
500 if (SrcTySize > DstTySize)
501 return truncate(DstTySize);
502 else if (SrcTySize < DstTySize)
503 return zeroExtend(DstTySize);
504 else
505 return *this;
506}
507
508/// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
509/// value is sign extended, truncated, or left alone to make it that width.
510ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
511 unsigned SrcTySize = getBitWidth();
512 if (SrcTySize > DstTySize)
513 return truncate(DstTySize);
514 else if (SrcTySize < DstTySize)
515 return signExtend(DstTySize);
516 else
517 return *this;
518}
519
Dan Gohmana3755d82009-07-09 22:07:27 +0000520ConstantRange
521ConstantRange::add(const ConstantRange &Other) const {
522 if (isEmptySet() || Other.isEmptySet())
523 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewyckycf9e07d2009-07-13 02:49:08 +0000524 if (isFullSet() || Other.isFullSet())
525 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohmana3755d82009-07-09 22:07:27 +0000526
527 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
528 APInt NewLower = getLower() + Other.getLower();
529 APInt NewUpper = getUpper() + Other.getUpper() - 1;
530 if (NewLower == NewUpper)
531 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
532
533 ConstantRange X = ConstantRange(NewLower, NewUpper);
534 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
535 // We've wrapped, therefore, full set.
536 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
537
538 return X;
Chris Lattner96f9d722002-09-02 00:18:22 +0000539}
540
Dan Gohmana3755d82009-07-09 22:07:27 +0000541ConstantRange
542ConstantRange::multiply(const ConstantRange &Other) const {
Dan Gohman8dcc58e2010-01-26 15:56:18 +0000543 // TODO: If either operand is a single element and the multiply is known to
544 // be non-wrapping, round the result min and max value to the appropriate
545 // multiple of that element. If wrapping is possible, at least adjust the
546 // range according to the greatest power-of-two factor of the single element.
Dan Gohman153f1eb2010-01-26 04:13:15 +0000547
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000548 if (isEmptySet() || Other.isEmptySet())
549 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
550 if (isFullSet() || Other.isFullSet())
551 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
552
Nick Lewyckyf1db1202009-07-13 03:27:41 +0000553 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
554 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
555 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
556 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000557
Nick Lewyckyf1db1202009-07-13 03:27:41 +0000558 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
559 this_max * Other_max + 1);
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000560 return Result_zext.truncate(getBitWidth());
Dan Gohmana3755d82009-07-09 22:07:27 +0000561}
562
563ConstantRange
564ConstantRange::smax(const ConstantRange &Other) const {
Dan Gohman38b06442009-07-09 23:16:10 +0000565 // X smax Y is: range(smax(X_smin, Y_smin),
566 // smax(X_smax, Y_smax))
567 if (isEmptySet() || Other.isEmptySet())
568 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohman38b06442009-07-09 23:16:10 +0000569 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
570 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
571 if (NewU == NewL)
572 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
573 return ConstantRange(NewL, NewU);
Dan Gohmana3755d82009-07-09 22:07:27 +0000574}
575
576ConstantRange
577ConstantRange::umax(const ConstantRange &Other) const {
578 // X umax Y is: range(umax(X_umin, Y_umin),
579 // umax(X_umax, Y_umax))
580 if (isEmptySet() || Other.isEmptySet())
581 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohmana3755d82009-07-09 22:07:27 +0000582 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
583 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
584 if (NewU == NewL)
585 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
586 return ConstantRange(NewL, NewU);
587}
588
589ConstantRange
Nick Lewycky956daf02009-07-12 05:18:18 +0000590ConstantRange::udiv(const ConstantRange &RHS) const {
591 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
592 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
593 if (RHS.isFullSet())
594 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
595
596 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
597
598 APInt RHS_umin = RHS.getUnsignedMin();
599 if (RHS_umin == 0) {
600 // We want the lowest value in RHS excluding zero. Usually that would be 1
601 // except for a range in the form of [X, 1) in which case it would be X.
602 if (RHS.getUpper() == 1)
603 RHS_umin = RHS.getLower();
604 else
605 RHS_umin = APInt(getBitWidth(), 1);
606 }
607
608 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
609
610 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
611 // this could occur.
612 if (Lower == Upper)
613 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
614
615 return ConstantRange(Lower, Upper);
Dan Gohmana3755d82009-07-09 22:07:27 +0000616}
617
Nuno Lopes34e992d2009-11-12 14:53:53 +0000618ConstantRange
619ConstantRange::shl(const ConstantRange &Amount) const {
620 if (isEmptySet())
621 return *this;
622
623 APInt min = getUnsignedMin() << Amount.getUnsignedMin();
624 APInt max = getUnsignedMax() << Amount.getUnsignedMax();
625
626 // there's no overflow!
Nuno Lopes44591452009-11-12 15:10:33 +0000627 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
Nuno Lopes34e992d2009-11-12 14:53:53 +0000628 if (Zeros.uge(Amount.getUnsignedMax()))
629 return ConstantRange(min, max);
630
631 // FIXME: implement the other tricky cases
632 return ConstantRange(getBitWidth());
633}
634
635ConstantRange
636ConstantRange::ashr(const ConstantRange &Amount) const {
637 if (isEmptySet())
638 return *this;
639
640 APInt min = getUnsignedMax().ashr(Amount.getUnsignedMin());
641 APInt max = getUnsignedMin().ashr(Amount.getUnsignedMax());
642 return ConstantRange(min, max);
643}
644
645ConstantRange
646ConstantRange::lshr(const ConstantRange &Amount) const {
647 if (isEmptySet())
648 return *this;
649
650 APInt min = getUnsignedMax().lshr(Amount.getUnsignedMin());
651 APInt max = getUnsignedMin().lshr(Amount.getUnsignedMax());
652 return ConstantRange(min, max);
653}
654
Dan Gohman38b06442009-07-09 23:16:10 +0000655/// print - Print out the bounds to a stream...
Chris Lattner96f9d722002-09-02 00:18:22 +0000656///
Dan Gohman38b06442009-07-09 23:16:10 +0000657void ConstantRange::print(raw_ostream &OS) const {
Dan Gohmandf6d5e02010-01-26 04:12:55 +0000658 if (isFullSet())
659 OS << "full-set";
660 else if (isEmptySet())
661 OS << "empty-set";
662 else
663 OS << "[" << Lower << "," << Upper << ")";
Dan Gohmana3755d82009-07-09 22:07:27 +0000664}
665
Dan Gohman38b06442009-07-09 23:16:10 +0000666/// dump - Allow printing from a debugger easily...
Dan Gohmana3755d82009-07-09 22:07:27 +0000667///
Dan Gohman38b06442009-07-09 23:16:10 +0000668void ConstantRange::dump() const {
David Greene7fd5fb42010-01-05 01:28:32 +0000669 print(dbgs());
Dan Gohmana3755d82009-07-09 22:07:27 +0000670}
671
Chris Lattner45cfe542009-08-23 06:03:38 +0000672