blob: 521511ade5c80ebf57c6073036f77804860363f1 [file] [log] [blame]
Chris Lattner113f2ae2002-09-01 23:53:36 +00001//===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner113f2ae2002-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
Sanjoy Das6ed05302015-10-22 03:12:57 +000024#include "llvm/IR/Instruction.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/InstrTypes.h"
Sanjoy Das6ed05302015-10-22 03:12:57 +000026#include "llvm/IR/Operator.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000027#include "llvm/IR/ConstantRange.h"
David Greenede7b3532010-01-05 01:28:32 +000028#include "llvm/Support/Debug.h"
Chris Lattner0c19df42008-08-23 22:23:09 +000029#include "llvm/Support/raw_ostream.h"
Chris Lattnerc9499b62003-12-14 21:35:53 +000030using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000031
Chris Lattner113f2ae2002-09-01 23:53:36 +000032/// Initialize a full (the default) or empty set for the specified type.
33///
Dan Gohmandc33ae22009-07-09 23:16:10 +000034ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) {
Chris Lattner113f2ae2002-09-01 23:53:36 +000035 if (Full)
Reid Spencere1f3f192007-02-28 17:36:23 +000036 Lower = Upper = APInt::getMaxValue(BitWidth);
Chris Lattner113f2ae2002-09-01 23:53:36 +000037 else
Reid Spencere1f3f192007-02-28 17:36:23 +000038 Lower = Upper = APInt::getMinValue(BitWidth);
Chris Lattner113f2ae2002-09-01 23:53:36 +000039}
40
Chris Lattner55481f72004-03-30 00:20:08 +000041/// Initialize a range to hold the single specified value.
42///
Benjamin Kramercce97be2013-07-11 15:37:27 +000043ConstantRange::ConstantRange(APIntMoveTy V)
Chandler Carruth002da5d2014-03-02 04:08:41 +000044 : Lower(std::move(V)), Upper(Lower + 1) {}
Chris Lattner113f2ae2002-09-01 23:53:36 +000045
Benjamin Kramercce97be2013-07-11 15:37:27 +000046ConstantRange::ConstantRange(APIntMoveTy L, APIntMoveTy U)
Chandler Carruth002da5d2014-03-02 04:08:41 +000047 : Lower(std::move(L)), Upper(std::move(U)) {
Benjamin Kramercce97be2013-07-11 15:37:27 +000048 assert(Lower.getBitWidth() == Upper.getBitWidth() &&
Dan Gohmandc33ae22009-07-09 23:16:10 +000049 "ConstantRange with unequal bit widths");
Benjamin Kramercce97be2013-07-11 15:37:27 +000050 assert((Lower != Upper || (Lower.isMaxValue() || Lower.isMinValue())) &&
Reid Spencere1f3f192007-02-28 17:36:23 +000051 "Lower == Upper, but they aren't min or max value!");
52}
53
Sanjoy Das7182d362015-03-18 00:41:24 +000054ConstantRange ConstantRange::makeAllowedICmpRegion(CmpInst::Predicate Pred,
55 const ConstantRange &CR) {
Nick Lewycky5154ee02010-09-28 18:18:36 +000056 if (CR.isEmptySet())
57 return CR;
58
Nick Lewyckydcfdce92009-07-11 06:15:39 +000059 uint32_t W = CR.getBitWidth();
60 switch (Pred) {
Sanjoy Das7182d362015-03-18 00:41:24 +000061 default:
62 llvm_unreachable("Invalid ICmp predicate to makeAllowedICmpRegion()");
Frits van Bommelec9cd832011-07-27 15:20:06 +000063 case CmpInst::ICMP_EQ:
Nick Lewycky9c5fc412009-07-11 17:04:01 +000064 return CR;
Frits van Bommelec9cd832011-07-27 15:20:06 +000065 case CmpInst::ICMP_NE:
Nick Lewyckydcfdce92009-07-11 06:15:39 +000066 if (CR.isSingleElement())
67 return ConstantRange(CR.getUpper(), CR.getLower());
68 return ConstantRange(W);
Frits van Bommelec9cd832011-07-27 15:20:06 +000069 case CmpInst::ICMP_ULT: {
Nick Lewycky5154ee02010-09-28 18:18:36 +000070 APInt UMax(CR.getUnsignedMax());
71 if (UMax.isMinValue())
72 return ConstantRange(W, /* empty */ false);
73 return ConstantRange(APInt::getMinValue(W), UMax);
74 }
Frits van Bommelec9cd832011-07-27 15:20:06 +000075 case CmpInst::ICMP_SLT: {
Nick Lewycky5154ee02010-09-28 18:18:36 +000076 APInt SMax(CR.getSignedMax());
77 if (SMax.isMinSignedValue())
78 return ConstantRange(W, /* empty */ false);
79 return ConstantRange(APInt::getSignedMinValue(W), SMax);
80 }
Frits van Bommelec9cd832011-07-27 15:20:06 +000081 case CmpInst::ICMP_ULE: {
Nick Lewyckydcfdce92009-07-11 06:15:39 +000082 APInt UMax(CR.getUnsignedMax());
83 if (UMax.isMaxValue())
84 return ConstantRange(W);
85 return ConstantRange(APInt::getMinValue(W), UMax + 1);
86 }
Frits van Bommelec9cd832011-07-27 15:20:06 +000087 case CmpInst::ICMP_SLE: {
Nick Lewyckydcfdce92009-07-11 06:15:39 +000088 APInt SMax(CR.getSignedMax());
Nick Lewycky5154ee02010-09-28 18:18:36 +000089 if (SMax.isMaxSignedValue())
Nick Lewyckydcfdce92009-07-11 06:15:39 +000090 return ConstantRange(W);
91 return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
92 }
Frits van Bommelec9cd832011-07-27 15:20:06 +000093 case CmpInst::ICMP_UGT: {
Nick Lewycky5154ee02010-09-28 18:18:36 +000094 APInt UMin(CR.getUnsignedMin());
95 if (UMin.isMaxValue())
96 return ConstantRange(W, /* empty */ false);
97 return ConstantRange(UMin + 1, APInt::getNullValue(W));
98 }
Frits van Bommelec9cd832011-07-27 15:20:06 +000099 case CmpInst::ICMP_SGT: {
Nick Lewycky5154ee02010-09-28 18:18:36 +0000100 APInt SMin(CR.getSignedMin());
101 if (SMin.isMaxSignedValue())
102 return ConstantRange(W, /* empty */ false);
103 return ConstantRange(SMin + 1, APInt::getSignedMinValue(W));
104 }
Frits van Bommelec9cd832011-07-27 15:20:06 +0000105 case CmpInst::ICMP_UGE: {
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000106 APInt UMin(CR.getUnsignedMin());
107 if (UMin.isMinValue())
108 return ConstantRange(W);
109 return ConstantRange(UMin, APInt::getNullValue(W));
110 }
Frits van Bommelec9cd832011-07-27 15:20:06 +0000111 case CmpInst::ICMP_SGE: {
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000112 APInt SMin(CR.getSignedMin());
113 if (SMin.isMinSignedValue())
114 return ConstantRange(W);
115 return ConstantRange(SMin, APInt::getSignedMinValue(W));
116 }
117 }
118}
119
Sanjoy Das7182d362015-03-18 00:41:24 +0000120ConstantRange ConstantRange::makeSatisfyingICmpRegion(CmpInst::Predicate Pred,
121 const ConstantRange &CR) {
122 // Follows from De-Morgan's laws:
123 //
124 // ~(~A union ~B) == A intersect B.
125 //
126 return makeAllowedICmpRegion(CmpInst::getInversePredicate(Pred), CR)
127 .inverse();
128}
129
Sanjoy Das5079f622016-02-22 16:13:02 +0000130ConstantRange
131ConstantRange::makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp,
132 const APInt &C, unsigned NoWrapKind) {
Sanjoy Das6ed05302015-10-22 03:12:57 +0000133 typedef OverflowingBinaryOperator OBO;
134
135 // Computes the intersection of CR0 and CR1. It is different from
136 // intersectWith in that the ConstantRange returned will only contain elements
137 // in both CR0 and CR1 (i.e. SubsetIntersect(X, Y) is a *subset*, proper or
138 // not, of both X and Y).
139 auto SubsetIntersect =
140 [](const ConstantRange &CR0, const ConstantRange &CR1) {
141 return CR0.inverse().unionWith(CR1.inverse()).inverse();
142 };
143
144 assert(BinOp >= Instruction::BinaryOpsBegin &&
145 BinOp < Instruction::BinaryOpsEnd && "Binary operators only!");
146
147 assert((NoWrapKind == OBO::NoSignedWrap ||
148 NoWrapKind == OBO::NoUnsignedWrap ||
149 NoWrapKind == (OBO::NoUnsignedWrap | OBO::NoSignedWrap)) &&
150 "NoWrapKind invalid!");
151
152 unsigned BitWidth = C.getBitWidth();
153 if (BinOp != Instruction::Add)
154 // Conservative answer: empty set
155 return ConstantRange(BitWidth, false);
156
157 if (C.isMinValue())
158 // Full set: nothing signed / unsigned wraps when added to 0.
159 return ConstantRange(BitWidth);
160
161 ConstantRange Result(BitWidth);
162
163 if (NoWrapKind & OBO::NoUnsignedWrap)
164 Result = SubsetIntersect(Result,
165 ConstantRange(APInt::getNullValue(BitWidth), -C));
166
167 if (NoWrapKind & OBO::NoSignedWrap) {
168 if (C.isStrictlyPositive())
169 Result = SubsetIntersect(
170 Result, ConstantRange(APInt::getSignedMinValue(BitWidth),
171 APInt::getSignedMinValue(BitWidth) - C));
172 else
173 Result = SubsetIntersect(
174 Result, ConstantRange(APInt::getSignedMinValue(BitWidth) - C,
175 APInt::getSignedMinValue(BitWidth)));
176 }
177
178 return Result;
179}
180
Chris Lattner113f2ae2002-09-01 23:53:36 +0000181/// isFullSet - Return true if this set contains all of the elements possible
182/// for this data-type
183bool ConstantRange::isFullSet() const {
Zhou Sheng31787362007-04-26 16:42:07 +0000184 return Lower == Upper && Lower.isMaxValue();
Chris Lattner113f2ae2002-09-01 23:53:36 +0000185}
Misha Brukmanb1c93172005-04-21 23:48:37 +0000186
Chris Lattner113f2ae2002-09-01 23:53:36 +0000187/// isEmptySet - Return true if this set contains no members.
188///
189bool ConstantRange::isEmptySet() const {
Zhou Sheng31787362007-04-26 16:42:07 +0000190 return Lower == Upper && Lower.isMinValue();
Chris Lattner113f2ae2002-09-01 23:53:36 +0000191}
192
193/// isWrappedSet - Return true if this set wraps around the top of the range,
194/// for example: [100, 8)
195///
Reid Spencer6a440332007-03-01 07:54:15 +0000196bool ConstantRange::isWrappedSet() const {
Reid Spencere1f3f192007-02-28 17:36:23 +0000197 return Lower.ugt(Upper);
Chris Lattner113f2ae2002-09-01 23:53:36 +0000198}
199
Nick Lewyckya35462d2010-09-06 23:52:49 +0000200/// isSignWrappedSet - Return true if this set wraps around the INT_MIN of
201/// its bitwidth, for example: i8 [120, 140).
202///
203bool ConstantRange::isSignWrappedSet() const {
204 return contains(APInt::getSignedMaxValue(getBitWidth())) &&
205 contains(APInt::getSignedMinValue(getBitWidth()));
206}
207
Chris Lattner113f2ae2002-09-01 23:53:36 +0000208/// getSetSize - Return the number of elements in this set.
209///
Reid Spencere1f3f192007-02-28 17:36:23 +0000210APInt ConstantRange::getSetSize() const {
Nuno Lopes216d5712012-07-17 15:43:59 +0000211 if (isFullSet()) {
212 APInt Size(getBitWidth()+1, 0);
213 Size.setBit(getBitWidth());
214 return Size;
Chris Lattner113f2ae2002-09-01 23:53:36 +0000215 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000216
Nuno Lopes216d5712012-07-17 15:43:59 +0000217 // This is also correct for wrapped sets.
Nuno Lopes99504c52012-07-16 18:08:12 +0000218 return (Upper - Lower).zext(getBitWidth()+1);
Chris Lattner113f2ae2002-09-01 23:53:36 +0000219}
220
Nick Lewyckye4559372007-03-10 15:54:12 +0000221/// getUnsignedMax - Return the largest unsigned value contained in the
222/// ConstantRange.
223///
224APInt ConstantRange::getUnsignedMax() const {
225 if (isFullSet() || isWrappedSet())
226 return APInt::getMaxValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000227 return getUpper() - 1;
Nick Lewyckye4559372007-03-10 15:54:12 +0000228}
229
230/// getUnsignedMin - Return the smallest unsigned value contained in the
231/// ConstantRange.
232///
233APInt ConstantRange::getUnsignedMin() const {
234 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
235 return APInt::getMinValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000236 return getLower();
Nick Lewyckye4559372007-03-10 15:54:12 +0000237}
238
239/// getSignedMax - Return the largest signed value contained in the
240/// ConstantRange.
241///
242APInt ConstantRange::getSignedMax() const {
Zhou Sheng01c175e2007-04-13 05:57:32 +0000243 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
Nick Lewyckye4559372007-03-10 15:54:12 +0000244 if (!isWrappedSet()) {
Nick Lewyckyd18b1602007-06-09 04:20:33 +0000245 if (getLower().sle(getUpper() - 1))
Nick Lewyckye4559372007-03-10 15:54:12 +0000246 return getUpper() - 1;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000247 return SignedMax;
Nick Lewyckye4559372007-03-10 15:54:12 +0000248 }
Nick Lewycky228f5b42012-01-03 20:33:00 +0000249 if (getLower().isNegative() == getUpper().isNegative())
250 return SignedMax;
251 return getUpper() - 1;
Nick Lewyckye4559372007-03-10 15:54:12 +0000252}
253
254/// getSignedMin - Return the smallest signed value contained in the
255/// ConstantRange.
256///
257APInt ConstantRange::getSignedMin() const {
Zhou Sheng01c175e2007-04-13 05:57:32 +0000258 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
Nick Lewyckye4559372007-03-10 15:54:12 +0000259 if (!isWrappedSet()) {
Nick Lewyckyd18b1602007-06-09 04:20:33 +0000260 if (getLower().sle(getUpper() - 1))
Nick Lewyckye4559372007-03-10 15:54:12 +0000261 return getLower();
Nick Lewycky228f5b42012-01-03 20:33:00 +0000262 return SignedMin;
Nick Lewyckye4559372007-03-10 15:54:12 +0000263 }
Nick Lewycky228f5b42012-01-03 20:33:00 +0000264 if ((getUpper() - 1).slt(getLower())) {
265 if (getUpper() != SignedMin)
266 return SignedMin;
267 }
268 return getLower();
Nick Lewyckye4559372007-03-10 15:54:12 +0000269}
270
Chris Lattner55481f72004-03-30 00:20:08 +0000271/// contains - Return true if the specified value is in the set.
272///
Reid Spencer6a440332007-03-01 07:54:15 +0000273bool ConstantRange::contains(const APInt &V) const {
274 if (Lower == Upper)
275 return isFullSet();
Chris Lattner113f2ae2002-09-01 23:53:36 +0000276
Reid Spencer6a440332007-03-01 07:54:15 +0000277 if (!isWrappedSet())
278 return Lower.ule(V) && V.ult(Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000279 return Lower.ule(V) || V.ult(Upper);
Chris Lattner55481f72004-03-30 00:20:08 +0000280}
281
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000282/// contains - Return true if the argument is a subset of this range.
Nick Lewyckyd385c222010-08-11 22:04:36 +0000283/// Two equal sets contain each other. The empty set contained by all other
284/// sets.
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000285///
286bool ConstantRange::contains(const ConstantRange &Other) const {
Nick Lewyckyd385c222010-08-11 22:04:36 +0000287 if (isFullSet() || Other.isEmptySet()) return true;
288 if (isEmptySet() || Other.isFullSet()) return false;
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000289
290 if (!isWrappedSet()) {
291 if (Other.isWrappedSet())
292 return false;
293
294 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
295 }
296
297 if (!Other.isWrappedSet())
298 return Other.getUpper().ule(Upper) ||
299 Lower.ule(Other.getLower());
300
301 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
302}
303
Chris Lattner55481f72004-03-30 00:20:08 +0000304/// subtract - Subtract the specified constant from the endpoints of this
305/// constant range.
Reid Spencer3a7e9d82007-02-28 19:57:34 +0000306ConstantRange ConstantRange::subtract(const APInt &Val) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000307 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
Chris Lattner55481f72004-03-30 00:20:08 +0000308 // If the set is empty or full, don't modify the endpoints.
Reid Spencere1f3f192007-02-28 17:36:23 +0000309 if (Lower == Upper)
310 return *this;
Reid Spencere1f3f192007-02-28 17:36:23 +0000311 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattner55481f72004-03-30 00:20:08 +0000312}
Chris Lattner113f2ae2002-09-01 23:53:36 +0000313
Nuno Lopes5020db22012-06-28 16:10:13 +0000314/// \brief Subtract the specified range from this range (aka relative complement
315/// of the sets).
316ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
317 return intersectWith(CR.inverse());
318}
319
Nick Lewycky63f11082007-02-11 00:58:49 +0000320/// intersectWith - Return the range that results from the intersection of this
Nick Lewycky0d139032009-07-18 06:34:42 +0000321/// range with another range. The resultant range is guaranteed to include all
322/// elements contained in both input ranges, and to have the smallest possible
323/// set size that does so. Because there may be two intersections with the
324/// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
Reid Spencer6a440332007-03-01 07:54:15 +0000325ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000326 assert(getBitWidth() == CR.getBitWidth() &&
327 "ConstantRange types don't agree!");
Nick Lewycky61b4a262007-07-14 02:51:34 +0000328
329 // Handle common cases.
330 if ( isEmptySet() || CR.isFullSet()) return *this;
331 if (CR.isEmptySet() || isFullSet()) return CR;
332
333 if (!isWrappedSet() && CR.isWrappedSet())
Nick Lewycky0d139032009-07-18 06:34:42 +0000334 return CR.intersectWith(*this);
Nick Lewycky61b4a262007-07-14 02:51:34 +0000335
336 if (!isWrappedSet() && !CR.isWrappedSet()) {
337 if (Lower.ult(CR.Lower)) {
338 if (Upper.ule(CR.Lower))
339 return ConstantRange(getBitWidth(), false);
340
341 if (Upper.ult(CR.Upper))
342 return ConstantRange(CR.Lower, Upper);
343
344 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000345 }
Nick Lewycky228f5b42012-01-03 20:33:00 +0000346 if (Upper.ult(CR.Upper))
347 return *this;
348
349 if (Lower.ult(CR.Upper))
350 return ConstantRange(Lower, CR.Upper);
351
352 return ConstantRange(getBitWidth(), false);
Nick Lewycky61b4a262007-07-14 02:51:34 +0000353 }
354
355 if (isWrappedSet() && !CR.isWrappedSet()) {
356 if (CR.Lower.ult(Upper)) {
357 if (CR.Upper.ult(Upper))
358 return CR;
359
Nuno Lopes63afc082012-05-18 00:14:36 +0000360 if (CR.Upper.ule(Lower))
Nick Lewycky61b4a262007-07-14 02:51:34 +0000361 return ConstantRange(CR.Lower, Upper);
362
363 if (getSetSize().ult(CR.getSetSize()))
364 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000365 return CR;
366 }
367 if (CR.Lower.ult(Lower)) {
Nick Lewycky61b4a262007-07-14 02:51:34 +0000368 if (CR.Upper.ule(Lower))
369 return ConstantRange(getBitWidth(), false);
370
371 return ConstantRange(Lower, CR.Upper);
372 }
373 return CR;
374 }
375
376 if (CR.Upper.ult(Upper)) {
377 if (CR.Lower.ult(Upper)) {
378 if (getSetSize().ult(CR.getSetSize()))
379 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000380 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000381 }
382
383 if (CR.Lower.ult(Lower))
384 return ConstantRange(Lower, CR.Upper);
385
386 return CR;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000387 }
Nuno Lopesebb0c942012-06-28 00:59:33 +0000388 if (CR.Upper.ule(Lower)) {
Nick Lewycky61b4a262007-07-14 02:51:34 +0000389 if (CR.Lower.ult(Lower))
390 return *this;
391
392 return ConstantRange(CR.Lower, Upper);
393 }
394 if (getSetSize().ult(CR.getSetSize()))
395 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000396 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000397}
398
399
Nick Lewycky63f11082007-02-11 00:58:49 +0000400/// unionWith - Return the range that results from the union of this range with
Chris Lattner113f2ae2002-09-01 23:53:36 +0000401/// another range. The resultant range is guaranteed to include the elements of
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000402/// both sets, but may contain more. For example, [3, 9) union [12,15) is
403/// [3, 15), which includes 9, 10, and 11, which were not included in either
404/// set before.
Chris Lattner113f2ae2002-09-01 23:53:36 +0000405///
Reid Spencer6a440332007-03-01 07:54:15 +0000406ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000407 assert(getBitWidth() == CR.getBitWidth() &&
408 "ConstantRange types don't agree!");
Chris Lattner113f2ae2002-09-01 23:53:36 +0000409
Nick Lewyckycf87f9e2007-03-02 03:33:05 +0000410 if ( isFullSet() || CR.isEmptySet()) return *this;
411 if (CR.isFullSet() || isEmptySet()) return CR;
Chris Lattner113f2ae2002-09-01 23:53:36 +0000412
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000413 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
414
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000415 if (!isWrappedSet() && !CR.isWrappedSet()) {
Nick Lewycky567daf32009-07-19 03:44:35 +0000416 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
417 // If the two ranges are disjoint, find the smaller gap and bridge it.
418 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
419 if (d1.ult(d2))
420 return ConstantRange(Lower, CR.Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000421 return ConstantRange(CR.Lower, Upper);
Nick Lewycky567daf32009-07-19 03:44:35 +0000422 }
423
424 APInt L = Lower, U = Upper;
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000425 if (CR.Lower.ult(L))
426 L = CR.Lower;
Nick Lewycky567daf32009-07-19 03:44:35 +0000427 if ((CR.Upper - 1).ugt(U - 1))
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000428 U = CR.Upper;
Nick Lewycky567daf32009-07-19 03:44:35 +0000429
430 if (L == 0 && U == 0)
431 return ConstantRange(getBitWidth());
432
433 return ConstantRange(L, U);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000434 }
435
Nick Lewycky567daf32009-07-19 03:44:35 +0000436 if (!CR.isWrappedSet()) {
437 // ------U L----- and ------U L----- : this
438 // L--U L--U : CR
439 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000440 return *this;
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000441
Nick Lewycky567daf32009-07-19 03:44:35 +0000442 // ------U L----- : this
443 // L---------U : CR
444 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000445 return ConstantRange(getBitWidth());
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000446
Nick Lewycky567daf32009-07-19 03:44:35 +0000447 // ----U L---- : this
448 // L---U : CR
449 // <d1> <d2>
450 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000451 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
Nick Lewycky567daf32009-07-19 03:44:35 +0000452 if (d1.ult(d2))
453 return ConstantRange(Lower, CR.Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000454 return ConstantRange(CR.Lower, Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000455 }
456
Nick Lewycky567daf32009-07-19 03:44:35 +0000457 // ----U L----- : this
458 // L----U : CR
459 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
460 return ConstantRange(CR.Lower, Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000461
Nick Lewycky567daf32009-07-19 03:44:35 +0000462 // ------U L---- : this
463 // L-----U : CR
Nick Lewycky228f5b42012-01-03 20:33:00 +0000464 assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) &&
465 "ConstantRange::unionWith missed a case with one range wrapped");
466 return ConstantRange(Lower, CR.Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000467 }
468
Nick Lewycky567daf32009-07-19 03:44:35 +0000469 // ------U L---- and ------U L---- : this
470 // -U L----------- and ------------U L : CR
471 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
472 return ConstantRange(getBitWidth());
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000473
Nick Lewycky567daf32009-07-19 03:44:35 +0000474 APInt L = Lower, U = Upper;
475 if (CR.Upper.ugt(U))
476 U = CR.Upper;
477 if (CR.Lower.ult(L))
478 L = CR.Lower;
Nick Lewyckycf87f9e2007-03-02 03:33:05 +0000479
480 return ConstantRange(L, U);
Chris Lattner113f2ae2002-09-01 23:53:36 +0000481}
Chris Lattnere0bb9eb2002-09-02 00:18:22 +0000482
Chris Lattner55481f72004-03-30 00:20:08 +0000483/// zeroExtend - Return a new range in the specified integer type, which must
484/// be strictly larger than the current type. The returned range will
Reid Spencer266e42b2006-12-23 06:05:41 +0000485/// correspond to the possible range of values as if the source range had been
Chris Lattner55481f72004-03-30 00:20:08 +0000486/// zero extended.
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000487ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000488 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
489
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000490 unsigned SrcTySize = getBitWidth();
Reid Spencere1f3f192007-02-28 17:36:23 +0000491 assert(SrcTySize < DstTySize && "Not a value extension");
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000492 if (isFullSet() || isWrappedSet()) {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000493 // Change into [0, 1 << src bit width)
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000494 APInt LowerExt(DstTySize, 0);
495 if (!Upper) // special case: [X, 0) -- not really wrapping around
496 LowerExt = Lower.zext(DstTySize);
Benjamin Kramerfc3ea6f2013-07-11 16:05:50 +0000497 return ConstantRange(LowerExt, APInt::getOneBitSet(DstTySize, SrcTySize));
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000498 }
Chris Lattner55481f72004-03-30 00:20:08 +0000499
Jay Foad583abbc2010-12-07 08:25:19 +0000500 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
Chris Lattner55481f72004-03-30 00:20:08 +0000501}
502
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000503/// signExtend - Return a new range in the specified integer type, which must
504/// be strictly larger than the current type. The returned range will
505/// correspond to the possible range of values as if the source range had been
506/// sign extended.
507ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000508 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
509
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000510 unsigned SrcTySize = getBitWidth();
511 assert(SrcTySize < DstTySize && "Not a value extension");
Nuno Lopes1112eca2013-10-30 15:36:50 +0000512
513 // special case: [X, INT_MIN) -- not really wrapping around
Nuno Lopes14d4b0c2013-10-31 19:53:53 +0000514 if (Upper.isMinSignedValue())
Nuno Lopes1112eca2013-10-30 15:36:50 +0000515 return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize));
516
Nick Lewyckya35462d2010-09-06 23:52:49 +0000517 if (isFullSet() || isSignWrappedSet()) {
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000518 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
Nick Lewycky5edc4592009-07-13 04:17:23 +0000519 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000520 }
521
Jay Foad583abbc2010-12-07 08:25:19 +0000522 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000523}
524
Chris Lattner55481f72004-03-30 00:20:08 +0000525/// truncate - Return a new range in the specified integer type, which must be
526/// strictly smaller than the current type. The returned range will
Reid Spencer266e42b2006-12-23 06:05:41 +0000527/// correspond to the possible range of values as if the source range had been
Chris Lattner55481f72004-03-30 00:20:08 +0000528/// truncated to the specified type.
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000529ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
Benjamin Kramer6709e052011-11-24 17:24:33 +0000530 assert(getBitWidth() > DstTySize && "Not a value truncation");
Nuno Lopesc14776d2012-07-19 16:27:45 +0000531 if (isEmptySet())
532 return ConstantRange(DstTySize, /*isFullSet=*/false);
533 if (isFullSet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000534 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattner55481f72004-03-30 00:20:08 +0000535
Nuno Lopesc14776d2012-07-19 16:27:45 +0000536 APInt MaxValue = APInt::getMaxValue(DstTySize).zext(getBitWidth());
537 APInt MaxBitValue(getBitWidth(), 0);
538 MaxBitValue.setBit(DstTySize);
539
540 APInt LowerDiv(Lower), UpperDiv(Upper);
541 ConstantRange Union(DstTySize, /*isFullSet=*/false);
542
543 // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue]
544 // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and
545 // then we do the union with [MaxValue, Upper)
546 if (isWrappedSet()) {
547 // if Upper is greater than Max Value, it covers the whole truncated range.
548 if (Upper.uge(MaxValue))
549 return ConstantRange(DstTySize, /*isFullSet=*/true);
550
551 Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
552 UpperDiv = APInt::getMaxValue(getBitWidth());
553
554 // Union covers the MaxValue case, so return if the remaining range is just
555 // MaxValue.
556 if (LowerDiv == UpperDiv)
557 return Union;
558 }
559
560 // Chop off the most significant bits that are past the destination bitwidth.
561 if (LowerDiv.uge(MaxValue)) {
562 APInt Div(getBitWidth(), 0);
563 APInt::udivrem(LowerDiv, MaxBitValue, Div, LowerDiv);
564 UpperDiv = UpperDiv - MaxBitValue * Div;
565 }
566
567 if (UpperDiv.ule(MaxValue))
568 return ConstantRange(LowerDiv.trunc(DstTySize),
569 UpperDiv.trunc(DstTySize)).unionWith(Union);
570
571 // The truncated value wrapps around. Check if we can do better than fullset.
572 APInt UpperModulo = UpperDiv - MaxBitValue;
573 if (UpperModulo.ult(LowerDiv))
574 return ConstantRange(LowerDiv.trunc(DstTySize),
575 UpperModulo.trunc(DstTySize)).unionWith(Union);
576
577 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattner55481f72004-03-30 00:20:08 +0000578}
579
Nuno Lopes640eb702009-11-09 15:36:28 +0000580/// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
581/// value is zero extended, truncated, or left alone to make it that width.
582ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
583 unsigned SrcTySize = getBitWidth();
584 if (SrcTySize > DstTySize)
585 return truncate(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000586 if (SrcTySize < DstTySize)
Nuno Lopes640eb702009-11-09 15:36:28 +0000587 return zeroExtend(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000588 return *this;
Nuno Lopes640eb702009-11-09 15:36:28 +0000589}
590
591/// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
592/// value is sign extended, truncated, or left alone to make it that width.
593ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
594 unsigned SrcTySize = getBitWidth();
595 if (SrcTySize > DstTySize)
596 return truncate(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000597 if (SrcTySize < DstTySize)
Nuno Lopes640eb702009-11-09 15:36:28 +0000598 return signExtend(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000599 return *this;
Nuno Lopes640eb702009-11-09 15:36:28 +0000600}
601
Dan Gohman5035fbf2009-07-09 22:07:27 +0000602ConstantRange
603ConstantRange::add(const ConstantRange &Other) const {
604 if (isEmptySet() || Other.isEmptySet())
605 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewycky73b704d2009-07-13 02:49:08 +0000606 if (isFullSet() || Other.isFullSet())
607 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000608
609 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
610 APInt NewLower = getLower() + Other.getLower();
611 APInt NewUpper = getUpper() + Other.getUpper() - 1;
612 if (NewLower == NewUpper)
613 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
614
615 ConstantRange X = ConstantRange(NewLower, NewUpper);
616 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
617 // We've wrapped, therefore, full set.
618 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
619
620 return X;
Chris Lattnere0bb9eb2002-09-02 00:18:22 +0000621}
622
Dan Gohman5035fbf2009-07-09 22:07:27 +0000623ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000624ConstantRange::sub(const ConstantRange &Other) const {
625 if (isEmptySet() || Other.isEmptySet())
626 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
627 if (isFullSet() || Other.isFullSet())
628 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
629
630 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
Nick Lewycky5dc6b792011-06-22 21:13:46 +0000631 APInt NewLower = getLower() - Other.getUpper() + 1;
632 APInt NewUpper = getUpper() - Other.getLower();
Nick Lewyckyd385c222010-08-11 22:04:36 +0000633 if (NewLower == NewUpper)
634 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
635
636 ConstantRange X = ConstantRange(NewLower, NewUpper);
637 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
638 // We've wrapped, therefore, full set.
639 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
640
641 return X;
642}
643
644ConstantRange
Dan Gohman5035fbf2009-07-09 22:07:27 +0000645ConstantRange::multiply(const ConstantRange &Other) const {
Dan Gohman3f8ed9e2010-01-26 15:56:18 +0000646 // TODO: If either operand is a single element and the multiply is known to
647 // be non-wrapping, round the result min and max value to the appropriate
648 // multiple of that element. If wrapping is possible, at least adjust the
649 // range according to the greatest power-of-two factor of the single element.
Dan Gohman5325efc2010-01-26 04:13:15 +0000650
Nick Lewycky2951c992009-07-12 02:19:05 +0000651 if (isEmptySet() || Other.isEmptySet())
652 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes986cc182012-07-16 20:47:16 +0000653
James Molloydcc78ec2015-03-06 15:50:47 +0000654 // Multiplication is signedness-independent. However different ranges can be
655 // obtained depending on how the input ranges are treated. These different
656 // ranges are all conservatively correct, but one might be better than the
657 // other. We calculate two ranges; one treating the inputs as unsigned
658 // and the other signed, then return the smallest of these ranges.
659
660 // Unsigned range first.
Nick Lewycky53023892009-07-13 03:27:41 +0000661 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
662 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
663 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
664 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
Nick Lewycky2951c992009-07-12 02:19:05 +0000665
Nick Lewycky53023892009-07-13 03:27:41 +0000666 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
667 this_max * Other_max + 1);
James Molloydcc78ec2015-03-06 15:50:47 +0000668 ConstantRange UR = Result_zext.truncate(getBitWidth());
669
670 // Now the signed range. Because we could be dealing with negative numbers
671 // here, the lower bound is the smallest of the cartesian product of the
672 // lower and upper ranges; for example:
673 // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
674 // Similarly for the upper bound, swapping min for max.
675
676 this_min = getSignedMin().sext(getBitWidth() * 2);
677 this_max = getSignedMax().sext(getBitWidth() * 2);
678 Other_min = Other.getSignedMin().sext(getBitWidth() * 2);
679 Other_max = Other.getSignedMax().sext(getBitWidth() * 2);
680
681 auto L = {this_min * Other_min, this_min * Other_max,
682 this_max * Other_min, this_max * Other_max};
683 auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
684 ConstantRange Result_sext(std::min(L, Compare), std::max(L, Compare) + 1);
685 ConstantRange SR = Result_sext.truncate(getBitWidth());
686
687 return UR.getSetSize().ult(SR.getSetSize()) ? UR : SR;
Dan Gohman5035fbf2009-07-09 22:07:27 +0000688}
689
690ConstantRange
691ConstantRange::smax(const ConstantRange &Other) const {
Dan Gohmandc33ae22009-07-09 23:16:10 +0000692 // X smax Y is: range(smax(X_smin, Y_smin),
693 // smax(X_smax, Y_smax))
694 if (isEmptySet() || Other.isEmptySet())
695 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohmandc33ae22009-07-09 23:16:10 +0000696 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
697 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
698 if (NewU == NewL)
699 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
700 return ConstantRange(NewL, NewU);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000701}
702
703ConstantRange
704ConstantRange::umax(const ConstantRange &Other) const {
705 // X umax Y is: range(umax(X_umin, Y_umin),
706 // umax(X_umax, Y_umax))
707 if (isEmptySet() || Other.isEmptySet())
708 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000709 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
710 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
711 if (NewU == NewL)
712 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
713 return ConstantRange(NewL, NewU);
714}
715
716ConstantRange
Philip Reamesba313122016-02-26 22:08:18 +0000717ConstantRange::smin(const ConstantRange &Other) const {
718 // X smin Y is: range(smin(X_smin, Y_smin),
719 // smin(X_smax, Y_smax))
720 if (isEmptySet() || Other.isEmptySet())
721 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
722 APInt NewL = APIntOps::smin(getSignedMin(), Other.getSignedMin());
723 APInt NewU = APIntOps::smin(getSignedMax(), Other.getSignedMax()) + 1;
724 if (NewU == NewL)
725 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
726 return ConstantRange(NewL, NewU);
727}
728
729ConstantRange
730ConstantRange::umin(const ConstantRange &Other) const {
731 // X umin Y is: range(umin(X_umin, Y_umin),
732 // umin(X_umax, Y_umax))
733 if (isEmptySet() || Other.isEmptySet())
734 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
735 APInt NewL = APIntOps::umin(getUnsignedMin(), Other.getUnsignedMin());
736 APInt NewU = APIntOps::umin(getUnsignedMax(), Other.getUnsignedMax()) + 1;
737 if (NewU == NewL)
738 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
739 return ConstantRange(NewL, NewU);
740}
741
742ConstantRange
Nick Lewyckyf1b8cb32009-07-12 05:18:18 +0000743ConstantRange::udiv(const ConstantRange &RHS) const {
744 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
745 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
746 if (RHS.isFullSet())
747 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
748
749 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
750
751 APInt RHS_umin = RHS.getUnsignedMin();
752 if (RHS_umin == 0) {
753 // We want the lowest value in RHS excluding zero. Usually that would be 1
754 // except for a range in the form of [X, 1) in which case it would be X.
755 if (RHS.getUpper() == 1)
756 RHS_umin = RHS.getLower();
757 else
758 RHS_umin = APInt(getBitWidth(), 1);
759 }
760
761 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
762
763 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
764 // this could occur.
765 if (Lower == Upper)
766 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
767
768 return ConstantRange(Lower, Upper);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000769}
770
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000771ConstantRange
Nick Lewyckyad48e012010-09-07 05:39:02 +0000772ConstantRange::binaryAnd(const ConstantRange &Other) const {
773 if (isEmptySet() || Other.isEmptySet())
774 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
775
776 // TODO: replace this with something less conservative
777
778 APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax());
779 if (umin.isAllOnesValue())
780 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
781 return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1);
782}
783
784ConstantRange
785ConstantRange::binaryOr(const ConstantRange &Other) const {
786 if (isEmptySet() || Other.isEmptySet())
787 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
788
789 // TODO: replace this with something less conservative
790
791 APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
792 if (umax.isMinValue())
793 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
794 return ConstantRange(umax, APInt::getNullValue(getBitWidth()));
795}
796
797ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000798ConstantRange::shl(const ConstantRange &Other) const {
799 if (isEmptySet() || Other.isEmptySet())
800 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000801
Nick Lewyckyd385c222010-08-11 22:04:36 +0000802 APInt min = getUnsignedMin().shl(Other.getUnsignedMin());
803 APInt max = getUnsignedMax().shl(Other.getUnsignedMax());
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000804
805 // there's no overflow!
Nuno Lopesf8fcac72009-11-12 15:10:33 +0000806 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
Nick Lewyckyd385c222010-08-11 22:04:36 +0000807 if (Zeros.ugt(Other.getUnsignedMax()))
808 return ConstantRange(min, max + 1);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000809
810 // FIXME: implement the other tricky cases
Nick Lewyckyd385c222010-08-11 22:04:36 +0000811 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000812}
813
814ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000815ConstantRange::lshr(const ConstantRange &Other) const {
816 if (isEmptySet() || Other.isEmptySet())
817 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000818
Nick Lewyckyd385c222010-08-11 22:04:36 +0000819 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin());
820 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
821 if (min == max + 1)
822 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
823
824 return ConstantRange(min, max + 1);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000825}
826
Owen Anderson1a9078b2010-08-07 05:47:46 +0000827ConstantRange ConstantRange::inverse() const {
Nick Lewycky228f5b42012-01-03 20:33:00 +0000828 if (isFullSet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000829 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000830 if (isEmptySet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000831 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Owen Anderson1a9078b2010-08-07 05:47:46 +0000832 return ConstantRange(Upper, Lower);
833}
834
Dan Gohmandc33ae22009-07-09 23:16:10 +0000835/// print - Print out the bounds to a stream...
Chris Lattnere0bb9eb2002-09-02 00:18:22 +0000836///
Dan Gohmandc33ae22009-07-09 23:16:10 +0000837void ConstantRange::print(raw_ostream &OS) const {
Dan Gohman837ada72010-01-26 04:12:55 +0000838 if (isFullSet())
839 OS << "full-set";
840 else if (isEmptySet())
841 OS << "empty-set";
842 else
843 OS << "[" << Lower << "," << Upper << ")";
Dan Gohman5035fbf2009-07-09 22:07:27 +0000844}
845
Dan Gohmandc33ae22009-07-09 23:16:10 +0000846/// dump - Allow printing from a debugger easily...
Dan Gohman5035fbf2009-07-09 22:07:27 +0000847///
Yaron Kereneb2a2542016-01-29 20:50:44 +0000848LLVM_DUMP_METHOD void ConstantRange::dump() const {
David Greenede7b3532010-01-05 01:28:32 +0000849 print(dbgs());
Dan Gohman5035fbf2009-07-09 22:07:27 +0000850}