blob: 0f5c7128f3d09e871537a4b45ffc836f49aec1e6 [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()");
Sanjay Patelf8ee0e02016-06-19 17:20:27 +000063 case CmpInst::ICMP_EQ:
64 return CR;
65 case CmpInst::ICMP_NE:
66 if (CR.isSingleElement())
67 return ConstantRange(CR.getUpper(), CR.getLower());
68 return ConstantRange(W);
69 case CmpInst::ICMP_ULT: {
70 APInt UMax(CR.getUnsignedMax());
71 if (UMax.isMinValue())
72 return ConstantRange(W, /* empty */ false);
73 return ConstantRange(APInt::getMinValue(W), UMax);
74 }
75 case CmpInst::ICMP_SLT: {
76 APInt SMax(CR.getSignedMax());
77 if (SMax.isMinSignedValue())
78 return ConstantRange(W, /* empty */ false);
79 return ConstantRange(APInt::getSignedMinValue(W), SMax);
80 }
81 case CmpInst::ICMP_ULE: {
82 APInt UMax(CR.getUnsignedMax());
83 if (UMax.isMaxValue())
Nick Lewyckydcfdce92009-07-11 06:15:39 +000084 return ConstantRange(W);
Sanjay Patelf8ee0e02016-06-19 17:20:27 +000085 return ConstantRange(APInt::getMinValue(W), UMax + 1);
86 }
87 case CmpInst::ICMP_SLE: {
88 APInt SMax(CR.getSignedMax());
89 if (SMax.isMaxSignedValue())
90 return ConstantRange(W);
91 return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
92 }
93 case CmpInst::ICMP_UGT: {
94 APInt UMin(CR.getUnsignedMin());
95 if (UMin.isMaxValue())
96 return ConstantRange(W, /* empty */ false);
97 return ConstantRange(UMin + 1, APInt::getNullValue(W));
98 }
99 case CmpInst::ICMP_SGT: {
100 APInt SMin(CR.getSignedMin());
101 if (SMin.isMaxSignedValue())
102 return ConstantRange(W, /* empty */ false);
103 return ConstantRange(SMin + 1, APInt::getSignedMinValue(W));
104 }
105 case CmpInst::ICMP_UGE: {
106 APInt UMin(CR.getUnsignedMin());
107 if (UMin.isMinValue())
108 return ConstantRange(W);
109 return ConstantRange(UMin, APInt::getNullValue(W));
110 }
111 case CmpInst::ICMP_SGE: {
112 APInt SMin(CR.getSignedMin());
113 if (SMin.isMinSignedValue())
114 return ConstantRange(W);
115 return ConstantRange(SMin, APInt::getSignedMinValue(W));
116 }
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000117 }
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
Balaram Makam569eaec2016-05-04 21:32:14 +0000130ConstantRange ConstantRange::makeExactICmpRegion(CmpInst::Predicate Pred,
131 const APInt &C) {
132 // Computes the exact range that is equal to both the constant ranges returned
133 // by makeAllowedICmpRegion and makeSatisfyingICmpRegion. This is always true
134 // when RHS is a singleton such as an APInt and so the assert is valid.
135 // However for non-singleton RHS, for example ult [2,5) makeAllowedICmpRegion
136 // returns [0,4) but makeSatisfyICmpRegion returns [0,2).
137 //
138 assert(makeAllowedICmpRegion(Pred, C) == makeSatisfyingICmpRegion(Pred, C));
139 return makeAllowedICmpRegion(Pred, C);
140}
141
Sanjoy Das590614c2016-05-19 03:53:06 +0000142bool ConstantRange::getEquivalentICmp(CmpInst::Predicate &Pred,
143 APInt &RHS) const {
144 bool Success = false;
145
146 if (isFullSet() || isEmptySet()) {
147 Pred = isEmptySet() ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE;
148 RHS = APInt(getBitWidth(), 0);
149 Success = true;
150 } else if (getLower().isMinSignedValue() || getLower().isMinValue()) {
151 Pred =
152 getLower().isMinSignedValue() ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT;
153 RHS = getUpper();
154 Success = true;
155 } else if (getUpper().isMinSignedValue() || getUpper().isMinValue()) {
156 Pred =
157 getUpper().isMinSignedValue() ? CmpInst::ICMP_SGE : CmpInst::ICMP_UGE;
158 RHS = getLower();
159 Success = true;
160 }
161
162 assert((!Success || ConstantRange::makeExactICmpRegion(Pred, RHS) == *this) &&
163 "Bad result!");
164
165 return Success;
166}
167
Sanjoy Das5079f622016-02-22 16:13:02 +0000168ConstantRange
169ConstantRange::makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp,
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000170 const ConstantRange &Other,
171 unsigned NoWrapKind) {
Sanjoy Das6ed05302015-10-22 03:12:57 +0000172 typedef OverflowingBinaryOperator OBO;
173
174 // Computes the intersection of CR0 and CR1. It is different from
175 // intersectWith in that the ConstantRange returned will only contain elements
176 // in both CR0 and CR1 (i.e. SubsetIntersect(X, Y) is a *subset*, proper or
177 // not, of both X and Y).
178 auto SubsetIntersect =
179 [](const ConstantRange &CR0, const ConstantRange &CR1) {
180 return CR0.inverse().unionWith(CR1.inverse()).inverse();
181 };
182
183 assert(BinOp >= Instruction::BinaryOpsBegin &&
184 BinOp < Instruction::BinaryOpsEnd && "Binary operators only!");
185
186 assert((NoWrapKind == OBO::NoSignedWrap ||
187 NoWrapKind == OBO::NoUnsignedWrap ||
188 NoWrapKind == (OBO::NoUnsignedWrap | OBO::NoSignedWrap)) &&
189 "NoWrapKind invalid!");
190
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000191 unsigned BitWidth = Other.getBitWidth();
Sanjoy Das6ed05302015-10-22 03:12:57 +0000192 if (BinOp != Instruction::Add)
193 // Conservative answer: empty set
194 return ConstantRange(BitWidth, false);
195
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000196 if (auto *C = Other.getSingleElement())
197 if (C->isMinValue())
198 // Full set: nothing signed / unsigned wraps when added to 0.
199 return ConstantRange(BitWidth);
Sanjoy Das6ed05302015-10-22 03:12:57 +0000200
201 ConstantRange Result(BitWidth);
202
203 if (NoWrapKind & OBO::NoUnsignedWrap)
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000204 Result =
205 SubsetIntersect(Result, ConstantRange(APInt::getNullValue(BitWidth),
206 -Other.getUnsignedMax()));
Sanjoy Das6ed05302015-10-22 03:12:57 +0000207
208 if (NoWrapKind & OBO::NoSignedWrap) {
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000209 APInt SignedMin = Other.getSignedMin();
210 APInt SignedMax = Other.getSignedMax();
211
212 if (SignedMax.isStrictlyPositive())
Sanjoy Das6ed05302015-10-22 03:12:57 +0000213 Result = SubsetIntersect(
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000214 Result,
215 ConstantRange(APInt::getSignedMinValue(BitWidth),
216 APInt::getSignedMinValue(BitWidth) - SignedMax));
217
218 if (SignedMin.isNegative())
Sanjoy Das6ed05302015-10-22 03:12:57 +0000219 Result = SubsetIntersect(
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000220 Result, ConstantRange(APInt::getSignedMinValue(BitWidth) - SignedMin,
Sanjoy Das6ed05302015-10-22 03:12:57 +0000221 APInt::getSignedMinValue(BitWidth)));
222 }
223
224 return Result;
225}
226
Chris Lattner113f2ae2002-09-01 23:53:36 +0000227/// isFullSet - Return true if this set contains all of the elements possible
228/// for this data-type
229bool ConstantRange::isFullSet() const {
Zhou Sheng31787362007-04-26 16:42:07 +0000230 return Lower == Upper && Lower.isMaxValue();
Chris Lattner113f2ae2002-09-01 23:53:36 +0000231}
Misha Brukmanb1c93172005-04-21 23:48:37 +0000232
Chris Lattner113f2ae2002-09-01 23:53:36 +0000233/// isEmptySet - Return true if this set contains no members.
234///
235bool ConstantRange::isEmptySet() const {
Zhou Sheng31787362007-04-26 16:42:07 +0000236 return Lower == Upper && Lower.isMinValue();
Chris Lattner113f2ae2002-09-01 23:53:36 +0000237}
238
239/// isWrappedSet - Return true if this set wraps around the top of the range,
240/// for example: [100, 8)
241///
Reid Spencer6a440332007-03-01 07:54:15 +0000242bool ConstantRange::isWrappedSet() const {
Reid Spencere1f3f192007-02-28 17:36:23 +0000243 return Lower.ugt(Upper);
Chris Lattner113f2ae2002-09-01 23:53:36 +0000244}
245
Nick Lewyckya35462d2010-09-06 23:52:49 +0000246/// isSignWrappedSet - Return true if this set wraps around the INT_MIN of
247/// its bitwidth, for example: i8 [120, 140).
248///
249bool ConstantRange::isSignWrappedSet() const {
250 return contains(APInt::getSignedMaxValue(getBitWidth())) &&
251 contains(APInt::getSignedMinValue(getBitWidth()));
252}
253
Chris Lattner113f2ae2002-09-01 23:53:36 +0000254/// getSetSize - Return the number of elements in this set.
255///
Reid Spencere1f3f192007-02-28 17:36:23 +0000256APInt ConstantRange::getSetSize() const {
Nuno Lopes216d5712012-07-17 15:43:59 +0000257 if (isFullSet()) {
258 APInt Size(getBitWidth()+1, 0);
259 Size.setBit(getBitWidth());
260 return Size;
Chris Lattner113f2ae2002-09-01 23:53:36 +0000261 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000262
Nuno Lopes216d5712012-07-17 15:43:59 +0000263 // This is also correct for wrapped sets.
Nuno Lopes99504c52012-07-16 18:08:12 +0000264 return (Upper - Lower).zext(getBitWidth()+1);
Chris Lattner113f2ae2002-09-01 23:53:36 +0000265}
266
Nick Lewyckye4559372007-03-10 15:54:12 +0000267/// getUnsignedMax - Return the largest unsigned value contained in the
268/// ConstantRange.
269///
270APInt ConstantRange::getUnsignedMax() const {
271 if (isFullSet() || isWrappedSet())
272 return APInt::getMaxValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000273 return getUpper() - 1;
Nick Lewyckye4559372007-03-10 15:54:12 +0000274}
275
276/// getUnsignedMin - Return the smallest unsigned value contained in the
277/// ConstantRange.
278///
279APInt ConstantRange::getUnsignedMin() const {
280 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
281 return APInt::getMinValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000282 return getLower();
Nick Lewyckye4559372007-03-10 15:54:12 +0000283}
284
285/// getSignedMax - Return the largest signed value contained in the
286/// ConstantRange.
287///
288APInt ConstantRange::getSignedMax() const {
Zhou Sheng01c175e2007-04-13 05:57:32 +0000289 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
Nick Lewyckye4559372007-03-10 15:54:12 +0000290 if (!isWrappedSet()) {
Nick Lewyckyd18b1602007-06-09 04:20:33 +0000291 if (getLower().sle(getUpper() - 1))
Nick Lewyckye4559372007-03-10 15:54:12 +0000292 return getUpper() - 1;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000293 return SignedMax;
Nick Lewyckye4559372007-03-10 15:54:12 +0000294 }
Nick Lewycky228f5b42012-01-03 20:33:00 +0000295 if (getLower().isNegative() == getUpper().isNegative())
296 return SignedMax;
297 return getUpper() - 1;
Nick Lewyckye4559372007-03-10 15:54:12 +0000298}
299
300/// getSignedMin - Return the smallest signed value contained in the
301/// ConstantRange.
302///
303APInt ConstantRange::getSignedMin() const {
Zhou Sheng01c175e2007-04-13 05:57:32 +0000304 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
Nick Lewyckye4559372007-03-10 15:54:12 +0000305 if (!isWrappedSet()) {
Nick Lewyckyd18b1602007-06-09 04:20:33 +0000306 if (getLower().sle(getUpper() - 1))
Nick Lewyckye4559372007-03-10 15:54:12 +0000307 return getLower();
Nick Lewycky228f5b42012-01-03 20:33:00 +0000308 return SignedMin;
Nick Lewyckye4559372007-03-10 15:54:12 +0000309 }
Nick Lewycky228f5b42012-01-03 20:33:00 +0000310 if ((getUpper() - 1).slt(getLower())) {
311 if (getUpper() != SignedMin)
312 return SignedMin;
313 }
314 return getLower();
Nick Lewyckye4559372007-03-10 15:54:12 +0000315}
316
Chris Lattner55481f72004-03-30 00:20:08 +0000317/// contains - Return true if the specified value is in the set.
318///
Reid Spencer6a440332007-03-01 07:54:15 +0000319bool ConstantRange::contains(const APInt &V) const {
320 if (Lower == Upper)
321 return isFullSet();
Chris Lattner113f2ae2002-09-01 23:53:36 +0000322
Reid Spencer6a440332007-03-01 07:54:15 +0000323 if (!isWrappedSet())
324 return Lower.ule(V) && V.ult(Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000325 return Lower.ule(V) || V.ult(Upper);
Chris Lattner55481f72004-03-30 00:20:08 +0000326}
327
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000328/// contains - Return true if the argument is a subset of this range.
Nick Lewyckyd385c222010-08-11 22:04:36 +0000329/// Two equal sets contain each other. The empty set contained by all other
330/// sets.
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000331///
332bool ConstantRange::contains(const ConstantRange &Other) const {
Nick Lewyckyd385c222010-08-11 22:04:36 +0000333 if (isFullSet() || Other.isEmptySet()) return true;
334 if (isEmptySet() || Other.isFullSet()) return false;
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000335
336 if (!isWrappedSet()) {
337 if (Other.isWrappedSet())
338 return false;
339
340 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
341 }
342
343 if (!Other.isWrappedSet())
344 return Other.getUpper().ule(Upper) ||
345 Lower.ule(Other.getLower());
346
347 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
348}
349
Chris Lattner55481f72004-03-30 00:20:08 +0000350/// subtract - Subtract the specified constant from the endpoints of this
351/// constant range.
Reid Spencer3a7e9d82007-02-28 19:57:34 +0000352ConstantRange ConstantRange::subtract(const APInt &Val) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000353 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
Chris Lattner55481f72004-03-30 00:20:08 +0000354 // If the set is empty or full, don't modify the endpoints.
Reid Spencere1f3f192007-02-28 17:36:23 +0000355 if (Lower == Upper)
356 return *this;
Reid Spencere1f3f192007-02-28 17:36:23 +0000357 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattner55481f72004-03-30 00:20:08 +0000358}
Chris Lattner113f2ae2002-09-01 23:53:36 +0000359
Nuno Lopes5020db22012-06-28 16:10:13 +0000360/// \brief Subtract the specified range from this range (aka relative complement
361/// of the sets).
362ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
363 return intersectWith(CR.inverse());
364}
365
Nick Lewycky63f11082007-02-11 00:58:49 +0000366/// intersectWith - Return the range that results from the intersection of this
Nick Lewycky0d139032009-07-18 06:34:42 +0000367/// range with another range. The resultant range is guaranteed to include all
368/// elements contained in both input ranges, and to have the smallest possible
369/// set size that does so. Because there may be two intersections with the
370/// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
Reid Spencer6a440332007-03-01 07:54:15 +0000371ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000372 assert(getBitWidth() == CR.getBitWidth() &&
373 "ConstantRange types don't agree!");
Nick Lewycky61b4a262007-07-14 02:51:34 +0000374
375 // Handle common cases.
376 if ( isEmptySet() || CR.isFullSet()) return *this;
377 if (CR.isEmptySet() || isFullSet()) return CR;
378
379 if (!isWrappedSet() && CR.isWrappedSet())
Nick Lewycky0d139032009-07-18 06:34:42 +0000380 return CR.intersectWith(*this);
Nick Lewycky61b4a262007-07-14 02:51:34 +0000381
382 if (!isWrappedSet() && !CR.isWrappedSet()) {
383 if (Lower.ult(CR.Lower)) {
384 if (Upper.ule(CR.Lower))
385 return ConstantRange(getBitWidth(), false);
386
387 if (Upper.ult(CR.Upper))
388 return ConstantRange(CR.Lower, Upper);
389
390 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000391 }
Nick Lewycky228f5b42012-01-03 20:33:00 +0000392 if (Upper.ult(CR.Upper))
393 return *this;
394
395 if (Lower.ult(CR.Upper))
396 return ConstantRange(Lower, CR.Upper);
397
398 return ConstantRange(getBitWidth(), false);
Nick Lewycky61b4a262007-07-14 02:51:34 +0000399 }
400
401 if (isWrappedSet() && !CR.isWrappedSet()) {
402 if (CR.Lower.ult(Upper)) {
403 if (CR.Upper.ult(Upper))
404 return CR;
405
Nuno Lopes63afc082012-05-18 00:14:36 +0000406 if (CR.Upper.ule(Lower))
Nick Lewycky61b4a262007-07-14 02:51:34 +0000407 return ConstantRange(CR.Lower, Upper);
408
409 if (getSetSize().ult(CR.getSetSize()))
410 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000411 return CR;
412 }
413 if (CR.Lower.ult(Lower)) {
Nick Lewycky61b4a262007-07-14 02:51:34 +0000414 if (CR.Upper.ule(Lower))
415 return ConstantRange(getBitWidth(), false);
416
417 return ConstantRange(Lower, CR.Upper);
418 }
419 return CR;
420 }
421
422 if (CR.Upper.ult(Upper)) {
423 if (CR.Lower.ult(Upper)) {
424 if (getSetSize().ult(CR.getSetSize()))
425 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000426 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000427 }
428
429 if (CR.Lower.ult(Lower))
430 return ConstantRange(Lower, CR.Upper);
431
432 return CR;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000433 }
Nuno Lopesebb0c942012-06-28 00:59:33 +0000434 if (CR.Upper.ule(Lower)) {
Nick Lewycky61b4a262007-07-14 02:51:34 +0000435 if (CR.Lower.ult(Lower))
436 return *this;
437
438 return ConstantRange(CR.Lower, Upper);
439 }
440 if (getSetSize().ult(CR.getSetSize()))
441 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000442 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000443}
444
445
Nick Lewycky63f11082007-02-11 00:58:49 +0000446/// unionWith - Return the range that results from the union of this range with
Chris Lattner113f2ae2002-09-01 23:53:36 +0000447/// another range. The resultant range is guaranteed to include the elements of
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000448/// both sets, but may contain more. For example, [3, 9) union [12,15) is
449/// [3, 15), which includes 9, 10, and 11, which were not included in either
450/// set before.
Chris Lattner113f2ae2002-09-01 23:53:36 +0000451///
Reid Spencer6a440332007-03-01 07:54:15 +0000452ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000453 assert(getBitWidth() == CR.getBitWidth() &&
454 "ConstantRange types don't agree!");
Chris Lattner113f2ae2002-09-01 23:53:36 +0000455
Nick Lewyckycf87f9e2007-03-02 03:33:05 +0000456 if ( isFullSet() || CR.isEmptySet()) return *this;
457 if (CR.isFullSet() || isEmptySet()) return CR;
Chris Lattner113f2ae2002-09-01 23:53:36 +0000458
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000459 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
460
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000461 if (!isWrappedSet() && !CR.isWrappedSet()) {
Nick Lewycky567daf32009-07-19 03:44:35 +0000462 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
463 // If the two ranges are disjoint, find the smaller gap and bridge it.
464 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
465 if (d1.ult(d2))
466 return ConstantRange(Lower, CR.Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000467 return ConstantRange(CR.Lower, Upper);
Nick Lewycky567daf32009-07-19 03:44:35 +0000468 }
469
470 APInt L = Lower, U = Upper;
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000471 if (CR.Lower.ult(L))
472 L = CR.Lower;
Nick Lewycky567daf32009-07-19 03:44:35 +0000473 if ((CR.Upper - 1).ugt(U - 1))
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000474 U = CR.Upper;
Nick Lewycky567daf32009-07-19 03:44:35 +0000475
476 if (L == 0 && U == 0)
477 return ConstantRange(getBitWidth());
478
479 return ConstantRange(L, U);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000480 }
481
Nick Lewycky567daf32009-07-19 03:44:35 +0000482 if (!CR.isWrappedSet()) {
483 // ------U L----- and ------U L----- : this
484 // L--U L--U : CR
485 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000486 return *this;
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000487
Nick Lewycky567daf32009-07-19 03:44:35 +0000488 // ------U L----- : this
489 // L---------U : CR
490 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000491 return ConstantRange(getBitWidth());
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000492
Nick Lewycky567daf32009-07-19 03:44:35 +0000493 // ----U L---- : this
494 // L---U : CR
495 // <d1> <d2>
496 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000497 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
Nick Lewycky567daf32009-07-19 03:44:35 +0000498 if (d1.ult(d2))
499 return ConstantRange(Lower, CR.Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000500 return ConstantRange(CR.Lower, Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000501 }
502
Nick Lewycky567daf32009-07-19 03:44:35 +0000503 // ----U L----- : this
504 // L----U : CR
505 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
506 return ConstantRange(CR.Lower, Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000507
Nick Lewycky567daf32009-07-19 03:44:35 +0000508 // ------U L---- : this
509 // L-----U : CR
Nick Lewycky228f5b42012-01-03 20:33:00 +0000510 assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) &&
511 "ConstantRange::unionWith missed a case with one range wrapped");
512 return ConstantRange(Lower, CR.Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000513 }
514
Nick Lewycky567daf32009-07-19 03:44:35 +0000515 // ------U L---- and ------U L---- : this
516 // -U L----------- and ------------U L : CR
517 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
518 return ConstantRange(getBitWidth());
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000519
Nick Lewycky567daf32009-07-19 03:44:35 +0000520 APInt L = Lower, U = Upper;
521 if (CR.Upper.ugt(U))
522 U = CR.Upper;
523 if (CR.Lower.ult(L))
524 L = CR.Lower;
Nick Lewyckycf87f9e2007-03-02 03:33:05 +0000525
526 return ConstantRange(L, U);
Chris Lattner113f2ae2002-09-01 23:53:36 +0000527}
Chris Lattnere0bb9eb2002-09-02 00:18:22 +0000528
Chris Lattner55481f72004-03-30 00:20:08 +0000529/// zeroExtend - Return a new range in the specified integer type, which must
530/// be strictly larger than the current type. The returned range will
Reid Spencer266e42b2006-12-23 06:05:41 +0000531/// correspond to the possible range of values as if the source range had been
Chris Lattner55481f72004-03-30 00:20:08 +0000532/// zero extended.
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000533ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000534 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
535
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000536 unsigned SrcTySize = getBitWidth();
Reid Spencere1f3f192007-02-28 17:36:23 +0000537 assert(SrcTySize < DstTySize && "Not a value extension");
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000538 if (isFullSet() || isWrappedSet()) {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000539 // Change into [0, 1 << src bit width)
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000540 APInt LowerExt(DstTySize, 0);
541 if (!Upper) // special case: [X, 0) -- not really wrapping around
542 LowerExt = Lower.zext(DstTySize);
Benjamin Kramerfc3ea6f2013-07-11 16:05:50 +0000543 return ConstantRange(LowerExt, APInt::getOneBitSet(DstTySize, SrcTySize));
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000544 }
Chris Lattner55481f72004-03-30 00:20:08 +0000545
Jay Foad583abbc2010-12-07 08:25:19 +0000546 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
Chris Lattner55481f72004-03-30 00:20:08 +0000547}
548
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000549/// signExtend - Return a new range in the specified integer type, which must
550/// be strictly larger than the current type. The returned range will
551/// correspond to the possible range of values as if the source range had been
552/// sign extended.
553ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000554 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
555
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000556 unsigned SrcTySize = getBitWidth();
557 assert(SrcTySize < DstTySize && "Not a value extension");
Nuno Lopes1112eca2013-10-30 15:36:50 +0000558
559 // special case: [X, INT_MIN) -- not really wrapping around
Nuno Lopes14d4b0c2013-10-31 19:53:53 +0000560 if (Upper.isMinSignedValue())
Nuno Lopes1112eca2013-10-30 15:36:50 +0000561 return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize));
562
Nick Lewyckya35462d2010-09-06 23:52:49 +0000563 if (isFullSet() || isSignWrappedSet()) {
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000564 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
Nick Lewycky5edc4592009-07-13 04:17:23 +0000565 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000566 }
567
Jay Foad583abbc2010-12-07 08:25:19 +0000568 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000569}
570
Chris Lattner55481f72004-03-30 00:20:08 +0000571/// truncate - Return a new range in the specified integer type, which must be
572/// strictly smaller than the current type. The returned range will
Reid Spencer266e42b2006-12-23 06:05:41 +0000573/// correspond to the possible range of values as if the source range had been
Chris Lattner55481f72004-03-30 00:20:08 +0000574/// truncated to the specified type.
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000575ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
Benjamin Kramer6709e052011-11-24 17:24:33 +0000576 assert(getBitWidth() > DstTySize && "Not a value truncation");
Nuno Lopesc14776d2012-07-19 16:27:45 +0000577 if (isEmptySet())
578 return ConstantRange(DstTySize, /*isFullSet=*/false);
579 if (isFullSet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000580 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattner55481f72004-03-30 00:20:08 +0000581
Nuno Lopesc14776d2012-07-19 16:27:45 +0000582 APInt MaxValue = APInt::getMaxValue(DstTySize).zext(getBitWidth());
583 APInt MaxBitValue(getBitWidth(), 0);
584 MaxBitValue.setBit(DstTySize);
585
586 APInt LowerDiv(Lower), UpperDiv(Upper);
587 ConstantRange Union(DstTySize, /*isFullSet=*/false);
588
589 // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue]
590 // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and
591 // then we do the union with [MaxValue, Upper)
592 if (isWrappedSet()) {
Nick Lewyckybfad0152016-06-06 01:51:23 +0000593 // If Upper is greater than Max Value, it covers the whole truncated range.
Nuno Lopesc14776d2012-07-19 16:27:45 +0000594 if (Upper.uge(MaxValue))
595 return ConstantRange(DstTySize, /*isFullSet=*/true);
596
597 Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
598 UpperDiv = APInt::getMaxValue(getBitWidth());
599
600 // Union covers the MaxValue case, so return if the remaining range is just
601 // MaxValue.
602 if (LowerDiv == UpperDiv)
603 return Union;
604 }
605
606 // Chop off the most significant bits that are past the destination bitwidth.
607 if (LowerDiv.uge(MaxValue)) {
608 APInt Div(getBitWidth(), 0);
609 APInt::udivrem(LowerDiv, MaxBitValue, Div, LowerDiv);
610 UpperDiv = UpperDiv - MaxBitValue * Div;
611 }
612
613 if (UpperDiv.ule(MaxValue))
614 return ConstantRange(LowerDiv.trunc(DstTySize),
615 UpperDiv.trunc(DstTySize)).unionWith(Union);
616
Nick Lewyckybfad0152016-06-06 01:51:23 +0000617 // The truncated value wraps around. Check if we can do better than fullset.
Nuno Lopesc14776d2012-07-19 16:27:45 +0000618 APInt UpperModulo = UpperDiv - MaxBitValue;
619 if (UpperModulo.ult(LowerDiv))
620 return ConstantRange(LowerDiv.trunc(DstTySize),
621 UpperModulo.trunc(DstTySize)).unionWith(Union);
622
623 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattner55481f72004-03-30 00:20:08 +0000624}
625
Nuno Lopes640eb702009-11-09 15:36:28 +0000626/// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
627/// value is zero extended, truncated, or left alone to make it that width.
628ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
629 unsigned SrcTySize = getBitWidth();
630 if (SrcTySize > DstTySize)
631 return truncate(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000632 if (SrcTySize < DstTySize)
Nuno Lopes640eb702009-11-09 15:36:28 +0000633 return zeroExtend(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000634 return *this;
Nuno Lopes640eb702009-11-09 15:36:28 +0000635}
636
637/// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
638/// value is sign extended, truncated, or left alone to make it that width.
639ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
640 unsigned SrcTySize = getBitWidth();
641 if (SrcTySize > DstTySize)
642 return truncate(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000643 if (SrcTySize < DstTySize)
Nuno Lopes640eb702009-11-09 15:36:28 +0000644 return signExtend(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000645 return *this;
Nuno Lopes640eb702009-11-09 15:36:28 +0000646}
647
Dan Gohman5035fbf2009-07-09 22:07:27 +0000648ConstantRange
649ConstantRange::add(const ConstantRange &Other) const {
650 if (isEmptySet() || Other.isEmptySet())
651 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewycky73b704d2009-07-13 02:49:08 +0000652 if (isFullSet() || Other.isFullSet())
653 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000654
655 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
656 APInt NewLower = getLower() + Other.getLower();
657 APInt NewUpper = getUpper() + Other.getUpper() - 1;
658 if (NewLower == NewUpper)
659 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
660
661 ConstantRange X = ConstantRange(NewLower, NewUpper);
662 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
663 // We've wrapped, therefore, full set.
664 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
665
666 return X;
Chris Lattnere0bb9eb2002-09-02 00:18:22 +0000667}
668
Dan Gohman5035fbf2009-07-09 22:07:27 +0000669ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000670ConstantRange::sub(const ConstantRange &Other) const {
671 if (isEmptySet() || Other.isEmptySet())
672 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
673 if (isFullSet() || Other.isFullSet())
674 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
675
676 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
Nick Lewycky5dc6b792011-06-22 21:13:46 +0000677 APInt NewLower = getLower() - Other.getUpper() + 1;
678 APInt NewUpper = getUpper() - Other.getLower();
Nick Lewyckyd385c222010-08-11 22:04:36 +0000679 if (NewLower == NewUpper)
680 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
681
682 ConstantRange X = ConstantRange(NewLower, NewUpper);
683 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
684 // We've wrapped, therefore, full set.
685 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
686
687 return X;
688}
689
690ConstantRange
Dan Gohman5035fbf2009-07-09 22:07:27 +0000691ConstantRange::multiply(const ConstantRange &Other) const {
Dan Gohman3f8ed9e2010-01-26 15:56:18 +0000692 // TODO: If either operand is a single element and the multiply is known to
693 // be non-wrapping, round the result min and max value to the appropriate
694 // multiple of that element. If wrapping is possible, at least adjust the
695 // range according to the greatest power-of-two factor of the single element.
Dan Gohman5325efc2010-01-26 04:13:15 +0000696
Nick Lewycky2951c992009-07-12 02:19:05 +0000697 if (isEmptySet() || Other.isEmptySet())
698 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes986cc182012-07-16 20:47:16 +0000699
James Molloydcc78ec2015-03-06 15:50:47 +0000700 // Multiplication is signedness-independent. However different ranges can be
701 // obtained depending on how the input ranges are treated. These different
702 // ranges are all conservatively correct, but one might be better than the
703 // other. We calculate two ranges; one treating the inputs as unsigned
704 // and the other signed, then return the smallest of these ranges.
705
706 // Unsigned range first.
Nick Lewycky53023892009-07-13 03:27:41 +0000707 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
708 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
709 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
710 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
Nick Lewycky2951c992009-07-12 02:19:05 +0000711
Nick Lewycky53023892009-07-13 03:27:41 +0000712 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
713 this_max * Other_max + 1);
James Molloydcc78ec2015-03-06 15:50:47 +0000714 ConstantRange UR = Result_zext.truncate(getBitWidth());
715
Pete Cooper34a7a942016-05-27 17:06:50 +0000716 // If the unsigned range doesn't wrap, and isn't negative then it's a range
717 // from one positive number to another which is as good as we can generate.
718 // In this case, skip the extra work of generating signed ranges which aren't
719 // going to be better than this range.
720 if (!UR.isWrappedSet() && UR.getLower().isNonNegative())
721 return UR;
722
James Molloydcc78ec2015-03-06 15:50:47 +0000723 // Now the signed range. Because we could be dealing with negative numbers
724 // here, the lower bound is the smallest of the cartesian product of the
725 // lower and upper ranges; for example:
726 // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
727 // Similarly for the upper bound, swapping min for max.
728
729 this_min = getSignedMin().sext(getBitWidth() * 2);
730 this_max = getSignedMax().sext(getBitWidth() * 2);
731 Other_min = Other.getSignedMin().sext(getBitWidth() * 2);
732 Other_max = Other.getSignedMax().sext(getBitWidth() * 2);
733
734 auto L = {this_min * Other_min, this_min * Other_max,
735 this_max * Other_min, this_max * Other_max};
736 auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
737 ConstantRange Result_sext(std::min(L, Compare), std::max(L, Compare) + 1);
738 ConstantRange SR = Result_sext.truncate(getBitWidth());
739
740 return UR.getSetSize().ult(SR.getSetSize()) ? UR : SR;
Dan Gohman5035fbf2009-07-09 22:07:27 +0000741}
742
743ConstantRange
744ConstantRange::smax(const ConstantRange &Other) const {
Dan Gohmandc33ae22009-07-09 23:16:10 +0000745 // X smax Y is: range(smax(X_smin, Y_smin),
746 // smax(X_smax, Y_smax))
747 if (isEmptySet() || Other.isEmptySet())
748 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohmandc33ae22009-07-09 23:16:10 +0000749 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
750 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
751 if (NewU == NewL)
752 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
753 return ConstantRange(NewL, NewU);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000754}
755
756ConstantRange
757ConstantRange::umax(const ConstantRange &Other) const {
758 // X umax Y is: range(umax(X_umin, Y_umin),
759 // umax(X_umax, Y_umax))
760 if (isEmptySet() || Other.isEmptySet())
761 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000762 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
763 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
764 if (NewU == NewL)
765 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
766 return ConstantRange(NewL, NewU);
767}
768
769ConstantRange
Philip Reamesba313122016-02-26 22:08:18 +0000770ConstantRange::smin(const ConstantRange &Other) const {
771 // X smin Y is: range(smin(X_smin, Y_smin),
772 // smin(X_smax, Y_smax))
773 if (isEmptySet() || Other.isEmptySet())
774 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
775 APInt NewL = APIntOps::smin(getSignedMin(), Other.getSignedMin());
776 APInt NewU = APIntOps::smin(getSignedMax(), Other.getSignedMax()) + 1;
777 if (NewU == NewL)
778 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
779 return ConstantRange(NewL, NewU);
780}
781
782ConstantRange
783ConstantRange::umin(const ConstantRange &Other) const {
784 // X umin Y is: range(umin(X_umin, Y_umin),
785 // umin(X_umax, Y_umax))
786 if (isEmptySet() || Other.isEmptySet())
787 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
788 APInt NewL = APIntOps::umin(getUnsignedMin(), Other.getUnsignedMin());
789 APInt NewU = APIntOps::umin(getUnsignedMax(), Other.getUnsignedMax()) + 1;
790 if (NewU == NewL)
791 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
792 return ConstantRange(NewL, NewU);
793}
794
795ConstantRange
Nick Lewyckyf1b8cb32009-07-12 05:18:18 +0000796ConstantRange::udiv(const ConstantRange &RHS) const {
797 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
798 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
799 if (RHS.isFullSet())
800 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
801
802 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
803
804 APInt RHS_umin = RHS.getUnsignedMin();
805 if (RHS_umin == 0) {
806 // We want the lowest value in RHS excluding zero. Usually that would be 1
807 // except for a range in the form of [X, 1) in which case it would be X.
808 if (RHS.getUpper() == 1)
809 RHS_umin = RHS.getLower();
810 else
811 RHS_umin = APInt(getBitWidth(), 1);
812 }
813
814 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
815
816 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
817 // this could occur.
818 if (Lower == Upper)
819 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
820
821 return ConstantRange(Lower, Upper);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000822}
823
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000824ConstantRange
Nick Lewyckyad48e012010-09-07 05:39:02 +0000825ConstantRange::binaryAnd(const ConstantRange &Other) const {
826 if (isEmptySet() || Other.isEmptySet())
827 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
828
829 // TODO: replace this with something less conservative
830
831 APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax());
832 if (umin.isAllOnesValue())
833 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
834 return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1);
835}
836
837ConstantRange
838ConstantRange::binaryOr(const ConstantRange &Other) const {
839 if (isEmptySet() || Other.isEmptySet())
840 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
841
842 // TODO: replace this with something less conservative
843
844 APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
845 if (umax.isMinValue())
846 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
847 return ConstantRange(umax, APInt::getNullValue(getBitWidth()));
848}
849
850ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000851ConstantRange::shl(const ConstantRange &Other) const {
852 if (isEmptySet() || Other.isEmptySet())
853 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000854
Nick Lewyckyd385c222010-08-11 22:04:36 +0000855 APInt min = getUnsignedMin().shl(Other.getUnsignedMin());
856 APInt max = getUnsignedMax().shl(Other.getUnsignedMax());
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000857
858 // there's no overflow!
Nuno Lopesf8fcac72009-11-12 15:10:33 +0000859 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
Nick Lewyckyd385c222010-08-11 22:04:36 +0000860 if (Zeros.ugt(Other.getUnsignedMax()))
861 return ConstantRange(min, max + 1);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000862
863 // FIXME: implement the other tricky cases
Nick Lewyckyd385c222010-08-11 22:04:36 +0000864 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000865}
866
867ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000868ConstantRange::lshr(const ConstantRange &Other) const {
869 if (isEmptySet() || Other.isEmptySet())
870 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000871
Nick Lewyckyd385c222010-08-11 22:04:36 +0000872 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin());
873 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
874 if (min == max + 1)
875 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
876
877 return ConstantRange(min, max + 1);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000878}
879
Owen Anderson1a9078b2010-08-07 05:47:46 +0000880ConstantRange ConstantRange::inverse() const {
Nick Lewycky228f5b42012-01-03 20:33:00 +0000881 if (isFullSet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000882 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000883 if (isEmptySet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000884 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Owen Anderson1a9078b2010-08-07 05:47:46 +0000885 return ConstantRange(Upper, Lower);
886}
887
Dan Gohmandc33ae22009-07-09 23:16:10 +0000888/// print - Print out the bounds to a stream...
Chris Lattnere0bb9eb2002-09-02 00:18:22 +0000889///
Dan Gohmandc33ae22009-07-09 23:16:10 +0000890void ConstantRange::print(raw_ostream &OS) const {
Dan Gohman837ada72010-01-26 04:12:55 +0000891 if (isFullSet())
892 OS << "full-set";
893 else if (isEmptySet())
894 OS << "empty-set";
895 else
896 OS << "[" << Lower << "," << Upper << ")";
Dan Gohman5035fbf2009-07-09 22:07:27 +0000897}
898
Dan Gohmandc33ae22009-07-09 23:16:10 +0000899/// dump - Allow printing from a debugger easily...
Dan Gohman5035fbf2009-07-09 22:07:27 +0000900///
Yaron Kereneb2a2542016-01-29 20:50:44 +0000901LLVM_DUMP_METHOD void ConstantRange::dump() const {
David Greenede7b3532010-01-05 01:28:32 +0000902 print(dbgs());
Dan Gohman5035fbf2009-07-09 22:07:27 +0000903}