blob: fb0442823c4c5b6f3d0f6e223eed721d811c4602 [file] [log] [blame]
Chris Lattner645e00d2002-09-01 23:53:36 +00001//===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner645e00d2002-09-01 23:53:36 +00009//
10// Represent a range of possible values that may occur when the program is run
11// for an integral value. This keeps track of a lower and upper bound for the
12// constant, which MAY wrap around the end of the numeric range. To do this, it
13// keeps track of a [lower, upper) bound, which specifies an interval just like
14// STL iterators. When used with boolean values, the following are important
15// ranges (other integral ranges use min/max values for special range values):
16//
17// [F, F) = {} = Empty set
18// [T, F) = {T}
19// [F, T) = {F}
20// [T, T) = {F, T} = Full set
21//
22//===----------------------------------------------------------------------===//
23
Owen Anderson634bab12010-08-07 00:42:06 +000024#include "llvm/Constants.h"
Chris Lattner645e00d2002-09-01 23:53:36 +000025#include "llvm/Support/ConstantRange.h"
David Greene7fd5fb42010-01-05 01:28:32 +000026#include "llvm/Support/Debug.h"
Chris Lattner944fac72008-08-23 22:23:09 +000027#include "llvm/Support/raw_ostream.h"
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000028#include "llvm/Instructions.h"
Chris Lattner2cdd21c2003-12-14 21:35:53 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner645e00d2002-09-01 23:53:36 +000031/// Initialize a full (the default) or empty set for the specified type.
32///
Dan Gohman38b06442009-07-09 23:16:10 +000033ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) {
Chris Lattner645e00d2002-09-01 23:53:36 +000034 if (Full)
Reid Spencer663e7112007-02-28 17:36:23 +000035 Lower = Upper = APInt::getMaxValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000036 else
Reid Spencer663e7112007-02-28 17:36:23 +000037 Lower = Upper = APInt::getMinValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000038}
39
Chris Lattnerfc33d302004-03-30 00:20:08 +000040/// Initialize a range to hold the single specified value.
41///
Dan Gohman38b06442009-07-09 23:16:10 +000042ConstantRange::ConstantRange(const APInt & V) : Lower(V), Upper(V + 1) {}
Owen Anderson634bab12010-08-07 00:42:06 +000043ConstantRange::ConstantRange(const ConstantInt *V)
44 : Lower(V->getValue()), Upper(V->getValue() + 1) {}
Chris Lattner645e00d2002-09-01 23:53:36 +000045
Dan Gohman38b06442009-07-09 23:16:10 +000046ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
47 Lower(L), Upper(U) {
48 assert(L.getBitWidth() == U.getBitWidth() &&
49 "ConstantRange with unequal bit widths");
Zhou Shengc125c002007-04-26 16:42:07 +000050 assert((L != U || (L.isMaxValue() || L.isMinValue())) &&
Reid Spencer663e7112007-02-28 17:36:23 +000051 "Lower == Upper, but they aren't min or max value!");
52}
53
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000054ConstantRange ConstantRange::makeICmpRegion(unsigned Pred,
55 const ConstantRange &CR) {
56 uint32_t W = CR.getBitWidth();
57 switch (Pred) {
58 default: assert(!"Invalid ICmp predicate to makeICmpRegion()");
59 case ICmpInst::ICMP_EQ:
Nick Lewyckyf067a232009-07-11 17:04:01 +000060 return CR;
Nick Lewyckybf8c7f02009-07-11 06:15:39 +000061 case ICmpInst::ICMP_NE:
62 if (CR.isSingleElement())
63 return ConstantRange(CR.getUpper(), CR.getLower());
64 return ConstantRange(W);
65 case ICmpInst::ICMP_ULT:
66 return ConstantRange(APInt::getMinValue(W), CR.getUnsignedMax());
67 case ICmpInst::ICMP_SLT:
68 return ConstantRange(APInt::getSignedMinValue(W), CR.getSignedMax());
69 case ICmpInst::ICMP_ULE: {
70 APInt UMax(CR.getUnsignedMax());
71 if (UMax.isMaxValue())
72 return ConstantRange(W);
73 return ConstantRange(APInt::getMinValue(W), UMax + 1);
74 }
75 case ICmpInst::ICMP_SLE: {
76 APInt SMax(CR.getSignedMax());
77 if (SMax.isMaxSignedValue() || (SMax+1).isMaxSignedValue())
78 return ConstantRange(W);
79 return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
80 }
81 case ICmpInst::ICMP_UGT:
82 return ConstantRange(CR.getUnsignedMin() + 1, APInt::getNullValue(W));
83 case ICmpInst::ICMP_SGT:
84 return ConstantRange(CR.getSignedMin() + 1,
85 APInt::getSignedMinValue(W));
86 case ICmpInst::ICMP_UGE: {
87 APInt UMin(CR.getUnsignedMin());
88 if (UMin.isMinValue())
89 return ConstantRange(W);
90 return ConstantRange(UMin, APInt::getNullValue(W));
91 }
92 case ICmpInst::ICMP_SGE: {
93 APInt SMin(CR.getSignedMin());
94 if (SMin.isMinSignedValue())
95 return ConstantRange(W);
96 return ConstantRange(SMin, APInt::getSignedMinValue(W));
97 }
98 }
99}
100
Chris Lattner645e00d2002-09-01 23:53:36 +0000101/// isFullSet - Return true if this set contains all of the elements possible
102/// for this data-type
103bool ConstantRange::isFullSet() const {
Zhou Shengc125c002007-04-26 16:42:07 +0000104 return Lower == Upper && Lower.isMaxValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000105}
Misha Brukmanfd939082005-04-21 23:48:37 +0000106
Chris Lattner645e00d2002-09-01 23:53:36 +0000107/// isEmptySet - Return true if this set contains no members.
108///
109bool ConstantRange::isEmptySet() const {
Zhou Shengc125c002007-04-26 16:42:07 +0000110 return Lower == Upper && Lower.isMinValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000111}
112
113/// isWrappedSet - Return true if this set wraps around the top of the range,
114/// for example: [100, 8)
115///
Reid Spencera6e8a952007-03-01 07:54:15 +0000116bool ConstantRange::isWrappedSet() const {
Reid Spencer663e7112007-02-28 17:36:23 +0000117 return Lower.ugt(Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000118}
119
Chris Lattner645e00d2002-09-01 23:53:36 +0000120/// getSetSize - Return the number of elements in this set.
121///
Reid Spencer663e7112007-02-28 17:36:23 +0000122APInt ConstantRange::getSetSize() const {
123 if (isEmptySet())
Reid Spencerbb626a62007-02-28 22:02:48 +0000124 return APInt(getBitWidth(), 0);
125 if (getBitWidth() == 1) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000126 if (Lower != Upper) // One of T or F in the set...
Reid Spencerbb626a62007-02-28 22:02:48 +0000127 return APInt(2, 1);
128 return APInt(2, 2); // Must be full set...
Chris Lattner645e00d2002-09-01 23:53:36 +0000129 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000130
Chris Lattner645e00d2002-09-01 23:53:36 +0000131 // Simply subtract the bounds...
Reid Spencer663e7112007-02-28 17:36:23 +0000132 return Upper - Lower;
Chris Lattner645e00d2002-09-01 23:53:36 +0000133}
134
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000135/// getUnsignedMax - Return the largest unsigned value contained in the
136/// ConstantRange.
137///
138APInt ConstantRange::getUnsignedMax() const {
139 if (isFullSet() || isWrappedSet())
140 return APInt::getMaxValue(getBitWidth());
141 else
142 return getUpper() - 1;
143}
144
145/// getUnsignedMin - Return the smallest unsigned value contained in the
146/// ConstantRange.
147///
148APInt ConstantRange::getUnsignedMin() const {
149 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
150 return APInt::getMinValue(getBitWidth());
151 else
152 return getLower();
153}
154
155/// getSignedMax - Return the largest signed value contained in the
156/// ConstantRange.
157///
158APInt ConstantRange::getSignedMax() const {
Zhou Shengdaacf222007-04-13 05:57:32 +0000159 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000160 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000161 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000162 return getUpper() - 1;
163 else
164 return SignedMax;
165 } else {
Nick Lewycky780905e2009-07-13 04:50:21 +0000166 if (getLower().isNegative() == getUpper().isNegative())
167 return SignedMax;
168 else
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000169 return getUpper() - 1;
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000170 }
171}
172
173/// getSignedMin - Return the smallest signed value contained in the
174/// ConstantRange.
175///
176APInt ConstantRange::getSignedMin() const {
Zhou Shengdaacf222007-04-13 05:57:32 +0000177 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000178 if (!isWrappedSet()) {
Nick Lewyckyae5eb7a2007-06-09 04:20:33 +0000179 if (getLower().sle(getUpper() - 1))
Nick Lewycky3400e6a2007-03-10 15:54:12 +0000180 return getLower();
181 else
182 return SignedMin;
183 } else {
184 if ((getUpper() - 1).slt(getLower())) {
185 if (getUpper() != SignedMin)
186 return SignedMin;
187 else
188 return getLower();
189 } else {
190 return getLower();
191 }
192 }
193}
194
Chris Lattnerfc33d302004-03-30 00:20:08 +0000195/// contains - Return true if the specified value is in the set.
196///
Reid Spencera6e8a952007-03-01 07:54:15 +0000197bool ConstantRange::contains(const APInt &V) const {
198 if (Lower == Upper)
199 return isFullSet();
Chris Lattner645e00d2002-09-01 23:53:36 +0000200
Reid Spencera6e8a952007-03-01 07:54:15 +0000201 if (!isWrappedSet())
202 return Lower.ule(V) && V.ult(Upper);
Reid Spencer663e7112007-02-28 17:36:23 +0000203 else
204 return Lower.ule(V) || V.ult(Upper);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000205}
206
Nick Lewyckybf8c7f02009-07-11 06:15:39 +0000207/// contains - Return true if the argument is a subset of this range.
208/// Two equal set contain each other. The empty set is considered to be
209/// contained by all other sets.
210///
211bool ConstantRange::contains(const ConstantRange &Other) const {
212 if (isFullSet()) return true;
213 if (Other.isFullSet()) return false;
214 if (Other.isEmptySet()) return true;
215 if (isEmptySet()) return false;
216
217 if (!isWrappedSet()) {
218 if (Other.isWrappedSet())
219 return false;
220
221 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
222 }
223
224 if (!Other.isWrappedSet())
225 return Other.getUpper().ule(Upper) ||
226 Lower.ule(Other.getLower());
227
228 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
229}
230
Chris Lattnerfc33d302004-03-30 00:20:08 +0000231/// subtract - Subtract the specified constant from the endpoints of this
232/// constant range.
Reid Spencer581b0d42007-02-28 19:57:34 +0000233ConstantRange ConstantRange::subtract(const APInt &Val) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000234 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
Chris Lattnerfc33d302004-03-30 00:20:08 +0000235 // If the set is empty or full, don't modify the endpoints.
Reid Spencer663e7112007-02-28 17:36:23 +0000236 if (Lower == Upper)
237 return *this;
Reid Spencer663e7112007-02-28 17:36:23 +0000238 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000239}
Chris Lattner645e00d2002-09-01 23:53:36 +0000240
241
242// intersect1Wrapped - This helper function is used to intersect two ranges when
243// it is known that LHS is wrapped and RHS isn't.
244//
Reid Spencer663e7112007-02-28 17:36:23 +0000245ConstantRange
246ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
Reid Spencera6e8a952007-03-01 07:54:15 +0000247 const ConstantRange &RHS) {
248 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
Chris Lattner645e00d2002-09-01 23:53:36 +0000249
Chris Lattner645e00d2002-09-01 23:53:36 +0000250 // Check to see if we overlap on the Left side of RHS...
251 //
Reid Spencera6e8a952007-03-01 07:54:15 +0000252 if (RHS.Lower.ult(LHS.Upper)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000253 // We do overlap on the left side of RHS, see if we overlap on the right of
254 // RHS...
Reid Spencera6e8a952007-03-01 07:54:15 +0000255 if (RHS.Upper.ugt(LHS.Lower)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000256 // Ok, the result overlaps on both the left and right sides. See if the
257 // resultant interval will be smaller if we wrap or not...
258 //
Reid Spencer663e7112007-02-28 17:36:23 +0000259 if (LHS.getSetSize().ult(RHS.getSetSize()))
Chris Lattner645e00d2002-09-01 23:53:36 +0000260 return LHS;
261 else
262 return RHS;
263
264 } else {
265 // No overlap on the right, just on the left.
Reid Spencerdc5c1592007-02-28 18:57:32 +0000266 return ConstantRange(RHS.Lower, LHS.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000267 }
Chris Lattner645e00d2002-09-01 23:53:36 +0000268 } else {
269 // We don't overlap on the left side of RHS, see if we overlap on the right
270 // of RHS...
Reid Spencera6e8a952007-03-01 07:54:15 +0000271 if (RHS.Upper.ugt(LHS.Lower)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000272 // Simple overlap...
Reid Spencerdc5c1592007-02-28 18:57:32 +0000273 return ConstantRange(LHS.Lower, RHS.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000274 } else {
275 // No overlap...
Reid Spencerbb626a62007-02-28 22:02:48 +0000276 return ConstantRange(LHS.getBitWidth(), false);
Chris Lattner645e00d2002-09-01 23:53:36 +0000277 }
278 }
279}
280
Nick Lewycky3e051642007-02-11 00:58:49 +0000281/// intersectWith - Return the range that results from the intersection of this
Nick Lewycky3a4a8842009-07-18 06:34:42 +0000282/// range with another range. The resultant range is guaranteed to include all
283/// elements contained in both input ranges, and to have the smallest possible
284/// set size that does so. Because there may be two intersections with the
285/// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
Reid Spencera6e8a952007-03-01 07:54:15 +0000286ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000287 assert(getBitWidth() == CR.getBitWidth() &&
288 "ConstantRange types don't agree!");
Nick Lewycky377b1192007-07-14 02:51:34 +0000289
290 // Handle common cases.
291 if ( isEmptySet() || CR.isFullSet()) return *this;
292 if (CR.isEmptySet() || isFullSet()) return CR;
293
294 if (!isWrappedSet() && CR.isWrappedSet())
Nick Lewycky3a4a8842009-07-18 06:34:42 +0000295 return CR.intersectWith(*this);
Nick Lewycky377b1192007-07-14 02:51:34 +0000296
297 if (!isWrappedSet() && !CR.isWrappedSet()) {
298 if (Lower.ult(CR.Lower)) {
299 if (Upper.ule(CR.Lower))
300 return ConstantRange(getBitWidth(), false);
301
302 if (Upper.ult(CR.Upper))
303 return ConstantRange(CR.Lower, Upper);
304
305 return CR;
306 } else {
307 if (Upper.ult(CR.Upper))
308 return *this;
309
310 if (Lower.ult(CR.Upper))
311 return ConstantRange(Lower, CR.Upper);
312
313 return ConstantRange(getBitWidth(), false);
314 }
315 }
316
317 if (isWrappedSet() && !CR.isWrappedSet()) {
318 if (CR.Lower.ult(Upper)) {
319 if (CR.Upper.ult(Upper))
320 return CR;
321
322 if (CR.Upper.ult(Lower))
323 return ConstantRange(CR.Lower, Upper);
324
325 if (getSetSize().ult(CR.getSetSize()))
326 return *this;
327 else
328 return CR;
329 } else if (CR.Lower.ult(Lower)) {
330 if (CR.Upper.ule(Lower))
331 return ConstantRange(getBitWidth(), false);
332
333 return ConstantRange(Lower, CR.Upper);
334 }
335 return CR;
336 }
337
338 if (CR.Upper.ult(Upper)) {
339 if (CR.Lower.ult(Upper)) {
340 if (getSetSize().ult(CR.getSetSize()))
341 return *this;
342 else
343 return CR;
344 }
345
346 if (CR.Lower.ult(Lower))
347 return ConstantRange(Lower, CR.Upper);
348
349 return CR;
350 } else if (CR.Upper.ult(Lower)) {
351 if (CR.Lower.ult(Lower))
352 return *this;
353
354 return ConstantRange(CR.Lower, Upper);
355 }
356 if (getSetSize().ult(CR.getSetSize()))
357 return *this;
358 else
359 return CR;
360}
361
362
Nick Lewycky3e051642007-02-11 00:58:49 +0000363/// unionWith - Return the range that results from the union of this range with
Chris Lattner645e00d2002-09-01 23:53:36 +0000364/// another range. The resultant range is guaranteed to include the elements of
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000365/// both sets, but may contain more. For example, [3, 9) union [12,15) is
366/// [3, 15), which includes 9, 10, and 11, which were not included in either
367/// set before.
Chris Lattner645e00d2002-09-01 23:53:36 +0000368///
Reid Spencera6e8a952007-03-01 07:54:15 +0000369ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000370 assert(getBitWidth() == CR.getBitWidth() &&
371 "ConstantRange types don't agree!");
Chris Lattner645e00d2002-09-01 23:53:36 +0000372
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000373 if ( isFullSet() || CR.isEmptySet()) return *this;
374 if (CR.isFullSet() || isEmptySet()) return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000375
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000376 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
377
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000378 if (!isWrappedSet() && !CR.isWrappedSet()) {
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000379 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
380 // If the two ranges are disjoint, find the smaller gap and bridge it.
381 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
382 if (d1.ult(d2))
383 return ConstantRange(Lower, CR.Upper);
384 else
385 return ConstantRange(CR.Lower, Upper);
386 }
387
388 APInt L = Lower, U = Upper;
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000389 if (CR.Lower.ult(L))
390 L = CR.Lower;
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000391 if ((CR.Upper - 1).ugt(U - 1))
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000392 U = CR.Upper;
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000393
394 if (L == 0 && U == 0)
395 return ConstantRange(getBitWidth());
396
397 return ConstantRange(L, U);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000398 }
399
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000400 if (!CR.isWrappedSet()) {
401 // ------U L----- and ------U L----- : this
402 // L--U L--U : CR
403 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000404 return *this;
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000405
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000406 // ------U L----- : this
407 // L---------U : CR
408 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000409 return ConstantRange(getBitWidth());
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000410
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000411 // ----U L---- : this
412 // L---U : CR
413 // <d1> <d2>
414 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000415 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000416 if (d1.ult(d2))
417 return ConstantRange(Lower, CR.Upper);
418 else
419 return ConstantRange(CR.Lower, Upper);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000420 }
421
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000422 // ----U L----- : this
423 // L----U : CR
424 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
425 return ConstantRange(CR.Lower, Upper);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000426
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000427 // ------U L---- : this
428 // L-----U : CR
429 if (CR.Lower.ult(Upper) && CR.Upper.ult(Lower))
430 return ConstantRange(Lower, CR.Upper);
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000431 }
432
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000433 assert(isWrappedSet() && CR.isWrappedSet() &&
434 "ConstantRange::unionWith missed wrapped union unwrapped case");
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000435
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000436 // ------U L---- and ------U L---- : this
437 // -U L----------- and ------------U L : CR
438 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
439 return ConstantRange(getBitWidth());
Nick Lewycky9babd0e2007-04-01 03:47:44 +0000440
Nick Lewycky7e7dc452009-07-19 03:44:35 +0000441 APInt L = Lower, U = Upper;
442 if (CR.Upper.ugt(U))
443 U = CR.Upper;
444 if (CR.Lower.ult(L))
445 L = CR.Lower;
Nick Lewyckyc6a28fc2007-03-02 03:33:05 +0000446
447 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000448}
Chris Lattner96f9d722002-09-02 00:18:22 +0000449
Chris Lattnerfc33d302004-03-30 00:20:08 +0000450/// zeroExtend - Return a new range in the specified integer type, which must
451/// be strictly larger than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000452/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000453/// zero extended.
Reid Spencerbb626a62007-02-28 22:02:48 +0000454ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
455 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000456 assert(SrcTySize < DstTySize && "Not a value extension");
Reid Spencerdc5c1592007-02-28 18:57:32 +0000457 if (isFullSet())
Chris Lattnerfc33d302004-03-30 00:20:08 +0000458 // Change a source full set into [0, 1 << 8*numbytes)
Reid Spencerdc5c1592007-02-28 18:57:32 +0000459 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000460
Reid Spencer663e7112007-02-28 17:36:23 +0000461 APInt L = Lower; L.zext(DstTySize);
462 APInt U = Upper; U.zext(DstTySize);
463 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000464}
465
Nick Lewyckye32157c2007-04-07 15:41:33 +0000466/// signExtend - Return a new range in the specified integer type, which must
467/// be strictly larger than the current type. The returned range will
468/// correspond to the possible range of values as if the source range had been
469/// sign extended.
470ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
471 unsigned SrcTySize = getBitWidth();
472 assert(SrcTySize < DstTySize && "Not a value extension");
473 if (isFullSet()) {
474 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
Nick Lewyckyff84de72009-07-13 04:17:23 +0000475 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
Nick Lewyckye32157c2007-04-07 15:41:33 +0000476 }
477
478 APInt L = Lower; L.sext(DstTySize);
479 APInt U = Upper; U.sext(DstTySize);
480 return ConstantRange(L, U);
481}
482
Chris Lattnerfc33d302004-03-30 00:20:08 +0000483/// truncate - Return a new range in the specified integer type, which must be
484/// strictly smaller than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000485/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000486/// truncated to the specified type.
Reid Spencerbb626a62007-02-28 22:02:48 +0000487ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
488 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000489 assert(SrcTySize > DstTySize && "Not a value truncation");
Zhou Shengdaacf222007-04-13 05:57:32 +0000490 APInt Size(APInt::getLowBitsSet(SrcTySize, DstTySize));
Reid Spencer663e7112007-02-28 17:36:23 +0000491 if (isFullSet() || getSetSize().ugt(Size))
Reid Spencerbb626a62007-02-28 22:02:48 +0000492 return ConstantRange(DstTySize);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000493
Reid Spencer663e7112007-02-28 17:36:23 +0000494 APInt L = Lower; L.trunc(DstTySize);
495 APInt U = Upper; U.trunc(DstTySize);
496 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000497}
498
Nuno Lopes95a3be02009-11-09 15:36:28 +0000499/// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
500/// value is zero extended, truncated, or left alone to make it that width.
501ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
502 unsigned SrcTySize = getBitWidth();
503 if (SrcTySize > DstTySize)
504 return truncate(DstTySize);
505 else if (SrcTySize < DstTySize)
506 return zeroExtend(DstTySize);
507 else
508 return *this;
509}
510
511/// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
512/// value is sign extended, truncated, or left alone to make it that width.
513ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
514 unsigned SrcTySize = getBitWidth();
515 if (SrcTySize > DstTySize)
516 return truncate(DstTySize);
517 else if (SrcTySize < DstTySize)
518 return signExtend(DstTySize);
519 else
520 return *this;
521}
522
Dan Gohmana3755d82009-07-09 22:07:27 +0000523ConstantRange
524ConstantRange::add(const ConstantRange &Other) const {
525 if (isEmptySet() || Other.isEmptySet())
526 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewyckycf9e07d2009-07-13 02:49:08 +0000527 if (isFullSet() || Other.isFullSet())
528 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohmana3755d82009-07-09 22:07:27 +0000529
530 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
531 APInt NewLower = getLower() + Other.getLower();
532 APInt NewUpper = getUpper() + Other.getUpper() - 1;
533 if (NewLower == NewUpper)
534 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
535
536 ConstantRange X = ConstantRange(NewLower, NewUpper);
537 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
538 // We've wrapped, therefore, full set.
539 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
540
541 return X;
Chris Lattner96f9d722002-09-02 00:18:22 +0000542}
543
Dan Gohmana3755d82009-07-09 22:07:27 +0000544ConstantRange
545ConstantRange::multiply(const ConstantRange &Other) const {
Dan Gohman8dcc58e2010-01-26 15:56:18 +0000546 // TODO: If either operand is a single element and the multiply is known to
547 // be non-wrapping, round the result min and max value to the appropriate
548 // multiple of that element. If wrapping is possible, at least adjust the
549 // range according to the greatest power-of-two factor of the single element.
Dan Gohman153f1eb2010-01-26 04:13:15 +0000550
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000551 if (isEmptySet() || Other.isEmptySet())
552 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
553 if (isFullSet() || Other.isFullSet())
554 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
555
Nick Lewyckyf1db1202009-07-13 03:27:41 +0000556 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
557 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
558 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
559 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000560
Nick Lewyckyf1db1202009-07-13 03:27:41 +0000561 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
562 this_max * Other_max + 1);
Nick Lewycky2ff893f2009-07-12 02:19:05 +0000563 return Result_zext.truncate(getBitWidth());
Dan Gohmana3755d82009-07-09 22:07:27 +0000564}
565
566ConstantRange
567ConstantRange::smax(const ConstantRange &Other) const {
Dan Gohman38b06442009-07-09 23:16:10 +0000568 // X smax Y is: range(smax(X_smin, Y_smin),
569 // smax(X_smax, Y_smax))
570 if (isEmptySet() || Other.isEmptySet())
571 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohman38b06442009-07-09 23:16:10 +0000572 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
573 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
574 if (NewU == NewL)
575 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
576 return ConstantRange(NewL, NewU);
Dan Gohmana3755d82009-07-09 22:07:27 +0000577}
578
579ConstantRange
580ConstantRange::umax(const ConstantRange &Other) const {
581 // X umax Y is: range(umax(X_umin, Y_umin),
582 // umax(X_umax, Y_umax))
583 if (isEmptySet() || Other.isEmptySet())
584 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohmana3755d82009-07-09 22:07:27 +0000585 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
586 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
587 if (NewU == NewL)
588 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
589 return ConstantRange(NewL, NewU);
590}
591
592ConstantRange
Nick Lewycky956daf02009-07-12 05:18:18 +0000593ConstantRange::udiv(const ConstantRange &RHS) const {
594 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
595 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
596 if (RHS.isFullSet())
597 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
598
599 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
600
601 APInt RHS_umin = RHS.getUnsignedMin();
602 if (RHS_umin == 0) {
603 // We want the lowest value in RHS excluding zero. Usually that would be 1
604 // except for a range in the form of [X, 1) in which case it would be X.
605 if (RHS.getUpper() == 1)
606 RHS_umin = RHS.getLower();
607 else
608 RHS_umin = APInt(getBitWidth(), 1);
609 }
610
611 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
612
613 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
614 // this could occur.
615 if (Lower == Upper)
616 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
617
618 return ConstantRange(Lower, Upper);
Dan Gohmana3755d82009-07-09 22:07:27 +0000619}
620
Nuno Lopes34e992d2009-11-12 14:53:53 +0000621ConstantRange
622ConstantRange::shl(const ConstantRange &Amount) const {
623 if (isEmptySet())
624 return *this;
625
626 APInt min = getUnsignedMin() << Amount.getUnsignedMin();
627 APInt max = getUnsignedMax() << Amount.getUnsignedMax();
628
629 // there's no overflow!
Nuno Lopes44591452009-11-12 15:10:33 +0000630 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
Nuno Lopes34e992d2009-11-12 14:53:53 +0000631 if (Zeros.uge(Amount.getUnsignedMax()))
632 return ConstantRange(min, max);
633
634 // FIXME: implement the other tricky cases
635 return ConstantRange(getBitWidth());
636}
637
638ConstantRange
639ConstantRange::ashr(const ConstantRange &Amount) const {
640 if (isEmptySet())
641 return *this;
642
643 APInt min = getUnsignedMax().ashr(Amount.getUnsignedMin());
644 APInt max = getUnsignedMin().ashr(Amount.getUnsignedMax());
645 return ConstantRange(min, max);
646}
647
648ConstantRange
649ConstantRange::lshr(const ConstantRange &Amount) const {
650 if (isEmptySet())
651 return *this;
652
653 APInt min = getUnsignedMax().lshr(Amount.getUnsignedMin());
654 APInt max = getUnsignedMin().lshr(Amount.getUnsignedMax());
655 return ConstantRange(min, max);
656}
657
Dan Gohman38b06442009-07-09 23:16:10 +0000658/// print - Print out the bounds to a stream...
Chris Lattner96f9d722002-09-02 00:18:22 +0000659///
Dan Gohman38b06442009-07-09 23:16:10 +0000660void ConstantRange::print(raw_ostream &OS) const {
Dan Gohmandf6d5e02010-01-26 04:12:55 +0000661 if (isFullSet())
662 OS << "full-set";
663 else if (isEmptySet())
664 OS << "empty-set";
665 else
666 OS << "[" << Lower << "," << Upper << ")";
Dan Gohmana3755d82009-07-09 22:07:27 +0000667}
668
Dan Gohman38b06442009-07-09 23:16:10 +0000669/// dump - Allow printing from a debugger easily...
Dan Gohmana3755d82009-07-09 22:07:27 +0000670///
Dan Gohman38b06442009-07-09 23:16:10 +0000671void ConstantRange::dump() const {
David Greene7fd5fb42010-01-05 01:28:32 +0000672 print(dbgs());
Dan Gohmana3755d82009-07-09 22:07:27 +0000673}
674
Chris Lattner45cfe542009-08-23 06:03:38 +0000675