blob: a2eb1517f5708b5e0dc0644bf90ac499710b0c91 [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
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 Das5079f622016-02-22 16:13:02 +0000142ConstantRange
143ConstantRange::makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp,
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000144 const ConstantRange &Other,
145 unsigned NoWrapKind) {
Sanjoy Das6ed05302015-10-22 03:12:57 +0000146 typedef OverflowingBinaryOperator OBO;
147
148 // Computes the intersection of CR0 and CR1. It is different from
149 // intersectWith in that the ConstantRange returned will only contain elements
150 // in both CR0 and CR1 (i.e. SubsetIntersect(X, Y) is a *subset*, proper or
151 // not, of both X and Y).
152 auto SubsetIntersect =
153 [](const ConstantRange &CR0, const ConstantRange &CR1) {
154 return CR0.inverse().unionWith(CR1.inverse()).inverse();
155 };
156
157 assert(BinOp >= Instruction::BinaryOpsBegin &&
158 BinOp < Instruction::BinaryOpsEnd && "Binary operators only!");
159
160 assert((NoWrapKind == OBO::NoSignedWrap ||
161 NoWrapKind == OBO::NoUnsignedWrap ||
162 NoWrapKind == (OBO::NoUnsignedWrap | OBO::NoSignedWrap)) &&
163 "NoWrapKind invalid!");
164
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000165 unsigned BitWidth = Other.getBitWidth();
Sanjoy Das6ed05302015-10-22 03:12:57 +0000166 if (BinOp != Instruction::Add)
167 // Conservative answer: empty set
168 return ConstantRange(BitWidth, false);
169
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000170 if (auto *C = Other.getSingleElement())
171 if (C->isMinValue())
172 // Full set: nothing signed / unsigned wraps when added to 0.
173 return ConstantRange(BitWidth);
Sanjoy Das6ed05302015-10-22 03:12:57 +0000174
175 ConstantRange Result(BitWidth);
176
177 if (NoWrapKind & OBO::NoUnsignedWrap)
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000178 Result =
179 SubsetIntersect(Result, ConstantRange(APInt::getNullValue(BitWidth),
180 -Other.getUnsignedMax()));
Sanjoy Das6ed05302015-10-22 03:12:57 +0000181
182 if (NoWrapKind & OBO::NoSignedWrap) {
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000183 APInt SignedMin = Other.getSignedMin();
184 APInt SignedMax = Other.getSignedMax();
185
186 if (SignedMax.isStrictlyPositive())
Sanjoy Das6ed05302015-10-22 03:12:57 +0000187 Result = SubsetIntersect(
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000188 Result,
189 ConstantRange(APInt::getSignedMinValue(BitWidth),
190 APInt::getSignedMinValue(BitWidth) - SignedMax));
191
192 if (SignedMin.isNegative())
Sanjoy Das6ed05302015-10-22 03:12:57 +0000193 Result = SubsetIntersect(
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000194 Result, ConstantRange(APInt::getSignedMinValue(BitWidth) - SignedMin,
Sanjoy Das6ed05302015-10-22 03:12:57 +0000195 APInt::getSignedMinValue(BitWidth)));
196 }
197
198 return Result;
199}
200
Chris Lattner113f2ae2002-09-01 23:53:36 +0000201/// isFullSet - Return true if this set contains all of the elements possible
202/// for this data-type
203bool ConstantRange::isFullSet() const {
Zhou Sheng31787362007-04-26 16:42:07 +0000204 return Lower == Upper && Lower.isMaxValue();
Chris Lattner113f2ae2002-09-01 23:53:36 +0000205}
Misha Brukmanb1c93172005-04-21 23:48:37 +0000206
Chris Lattner113f2ae2002-09-01 23:53:36 +0000207/// isEmptySet - Return true if this set contains no members.
208///
209bool ConstantRange::isEmptySet() const {
Zhou Sheng31787362007-04-26 16:42:07 +0000210 return Lower == Upper && Lower.isMinValue();
Chris Lattner113f2ae2002-09-01 23:53:36 +0000211}
212
213/// isWrappedSet - Return true if this set wraps around the top of the range,
214/// for example: [100, 8)
215///
Reid Spencer6a440332007-03-01 07:54:15 +0000216bool ConstantRange::isWrappedSet() const {
Reid Spencere1f3f192007-02-28 17:36:23 +0000217 return Lower.ugt(Upper);
Chris Lattner113f2ae2002-09-01 23:53:36 +0000218}
219
Nick Lewyckya35462d2010-09-06 23:52:49 +0000220/// isSignWrappedSet - Return true if this set wraps around the INT_MIN of
221/// its bitwidth, for example: i8 [120, 140).
222///
223bool ConstantRange::isSignWrappedSet() const {
224 return contains(APInt::getSignedMaxValue(getBitWidth())) &&
225 contains(APInt::getSignedMinValue(getBitWidth()));
226}
227
Chris Lattner113f2ae2002-09-01 23:53:36 +0000228/// getSetSize - Return the number of elements in this set.
229///
Reid Spencere1f3f192007-02-28 17:36:23 +0000230APInt ConstantRange::getSetSize() const {
Nuno Lopes216d5712012-07-17 15:43:59 +0000231 if (isFullSet()) {
232 APInt Size(getBitWidth()+1, 0);
233 Size.setBit(getBitWidth());
234 return Size;
Chris Lattner113f2ae2002-09-01 23:53:36 +0000235 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000236
Nuno Lopes216d5712012-07-17 15:43:59 +0000237 // This is also correct for wrapped sets.
Nuno Lopes99504c52012-07-16 18:08:12 +0000238 return (Upper - Lower).zext(getBitWidth()+1);
Chris Lattner113f2ae2002-09-01 23:53:36 +0000239}
240
Nick Lewyckye4559372007-03-10 15:54:12 +0000241/// getUnsignedMax - Return the largest unsigned value contained in the
242/// ConstantRange.
243///
244APInt ConstantRange::getUnsignedMax() const {
245 if (isFullSet() || isWrappedSet())
246 return APInt::getMaxValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000247 return getUpper() - 1;
Nick Lewyckye4559372007-03-10 15:54:12 +0000248}
249
250/// getUnsignedMin - Return the smallest unsigned value contained in the
251/// ConstantRange.
252///
253APInt ConstantRange::getUnsignedMin() const {
254 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
255 return APInt::getMinValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000256 return getLower();
Nick Lewyckye4559372007-03-10 15:54:12 +0000257}
258
259/// getSignedMax - Return the largest signed value contained in the
260/// ConstantRange.
261///
262APInt ConstantRange::getSignedMax() const {
Zhou Sheng01c175e2007-04-13 05:57:32 +0000263 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
Nick Lewyckye4559372007-03-10 15:54:12 +0000264 if (!isWrappedSet()) {
Nick Lewyckyd18b1602007-06-09 04:20:33 +0000265 if (getLower().sle(getUpper() - 1))
Nick Lewyckye4559372007-03-10 15:54:12 +0000266 return getUpper() - 1;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000267 return SignedMax;
Nick Lewyckye4559372007-03-10 15:54:12 +0000268 }
Nick Lewycky228f5b42012-01-03 20:33:00 +0000269 if (getLower().isNegative() == getUpper().isNegative())
270 return SignedMax;
271 return getUpper() - 1;
Nick Lewyckye4559372007-03-10 15:54:12 +0000272}
273
274/// getSignedMin - Return the smallest signed value contained in the
275/// ConstantRange.
276///
277APInt ConstantRange::getSignedMin() const {
Zhou Sheng01c175e2007-04-13 05:57:32 +0000278 APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
Nick Lewyckye4559372007-03-10 15:54:12 +0000279 if (!isWrappedSet()) {
Nick Lewyckyd18b1602007-06-09 04:20:33 +0000280 if (getLower().sle(getUpper() - 1))
Nick Lewyckye4559372007-03-10 15:54:12 +0000281 return getLower();
Nick Lewycky228f5b42012-01-03 20:33:00 +0000282 return SignedMin;
Nick Lewyckye4559372007-03-10 15:54:12 +0000283 }
Nick Lewycky228f5b42012-01-03 20:33:00 +0000284 if ((getUpper() - 1).slt(getLower())) {
285 if (getUpper() != SignedMin)
286 return SignedMin;
287 }
288 return getLower();
Nick Lewyckye4559372007-03-10 15:54:12 +0000289}
290
Chris Lattner55481f72004-03-30 00:20:08 +0000291/// contains - Return true if the specified value is in the set.
292///
Reid Spencer6a440332007-03-01 07:54:15 +0000293bool ConstantRange::contains(const APInt &V) const {
294 if (Lower == Upper)
295 return isFullSet();
Chris Lattner113f2ae2002-09-01 23:53:36 +0000296
Reid Spencer6a440332007-03-01 07:54:15 +0000297 if (!isWrappedSet())
298 return Lower.ule(V) && V.ult(Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000299 return Lower.ule(V) || V.ult(Upper);
Chris Lattner55481f72004-03-30 00:20:08 +0000300}
301
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000302/// contains - Return true if the argument is a subset of this range.
Nick Lewyckyd385c222010-08-11 22:04:36 +0000303/// Two equal sets contain each other. The empty set contained by all other
304/// sets.
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000305///
306bool ConstantRange::contains(const ConstantRange &Other) const {
Nick Lewyckyd385c222010-08-11 22:04:36 +0000307 if (isFullSet() || Other.isEmptySet()) return true;
308 if (isEmptySet() || Other.isFullSet()) return false;
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000309
310 if (!isWrappedSet()) {
311 if (Other.isWrappedSet())
312 return false;
313
314 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
315 }
316
317 if (!Other.isWrappedSet())
318 return Other.getUpper().ule(Upper) ||
319 Lower.ule(Other.getLower());
320
321 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
322}
323
Chris Lattner55481f72004-03-30 00:20:08 +0000324/// subtract - Subtract the specified constant from the endpoints of this
325/// constant range.
Reid Spencer3a7e9d82007-02-28 19:57:34 +0000326ConstantRange ConstantRange::subtract(const APInt &Val) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000327 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
Chris Lattner55481f72004-03-30 00:20:08 +0000328 // If the set is empty or full, don't modify the endpoints.
Reid Spencere1f3f192007-02-28 17:36:23 +0000329 if (Lower == Upper)
330 return *this;
Reid Spencere1f3f192007-02-28 17:36:23 +0000331 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattner55481f72004-03-30 00:20:08 +0000332}
Chris Lattner113f2ae2002-09-01 23:53:36 +0000333
Nuno Lopes5020db22012-06-28 16:10:13 +0000334/// \brief Subtract the specified range from this range (aka relative complement
335/// of the sets).
336ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
337 return intersectWith(CR.inverse());
338}
339
Nick Lewycky63f11082007-02-11 00:58:49 +0000340/// intersectWith - Return the range that results from the intersection of this
Nick Lewycky0d139032009-07-18 06:34:42 +0000341/// range with another range. The resultant range is guaranteed to include all
342/// elements contained in both input ranges, and to have the smallest possible
343/// set size that does so. Because there may be two intersections with the
344/// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
Reid Spencer6a440332007-03-01 07:54:15 +0000345ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000346 assert(getBitWidth() == CR.getBitWidth() &&
347 "ConstantRange types don't agree!");
Nick Lewycky61b4a262007-07-14 02:51:34 +0000348
349 // Handle common cases.
350 if ( isEmptySet() || CR.isFullSet()) return *this;
351 if (CR.isEmptySet() || isFullSet()) return CR;
352
353 if (!isWrappedSet() && CR.isWrappedSet())
Nick Lewycky0d139032009-07-18 06:34:42 +0000354 return CR.intersectWith(*this);
Nick Lewycky61b4a262007-07-14 02:51:34 +0000355
356 if (!isWrappedSet() && !CR.isWrappedSet()) {
357 if (Lower.ult(CR.Lower)) {
358 if (Upper.ule(CR.Lower))
359 return ConstantRange(getBitWidth(), false);
360
361 if (Upper.ult(CR.Upper))
362 return ConstantRange(CR.Lower, Upper);
363
364 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000365 }
Nick Lewycky228f5b42012-01-03 20:33:00 +0000366 if (Upper.ult(CR.Upper))
367 return *this;
368
369 if (Lower.ult(CR.Upper))
370 return ConstantRange(Lower, CR.Upper);
371
372 return ConstantRange(getBitWidth(), false);
Nick Lewycky61b4a262007-07-14 02:51:34 +0000373 }
374
375 if (isWrappedSet() && !CR.isWrappedSet()) {
376 if (CR.Lower.ult(Upper)) {
377 if (CR.Upper.ult(Upper))
378 return CR;
379
Nuno Lopes63afc082012-05-18 00:14:36 +0000380 if (CR.Upper.ule(Lower))
Nick Lewycky61b4a262007-07-14 02:51:34 +0000381 return ConstantRange(CR.Lower, Upper);
382
383 if (getSetSize().ult(CR.getSetSize()))
384 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000385 return CR;
386 }
387 if (CR.Lower.ult(Lower)) {
Nick Lewycky61b4a262007-07-14 02:51:34 +0000388 if (CR.Upper.ule(Lower))
389 return ConstantRange(getBitWidth(), false);
390
391 return ConstantRange(Lower, CR.Upper);
392 }
393 return CR;
394 }
395
396 if (CR.Upper.ult(Upper)) {
397 if (CR.Lower.ult(Upper)) {
398 if (getSetSize().ult(CR.getSetSize()))
399 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000400 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000401 }
402
403 if (CR.Lower.ult(Lower))
404 return ConstantRange(Lower, CR.Upper);
405
406 return CR;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000407 }
Nuno Lopesebb0c942012-06-28 00:59:33 +0000408 if (CR.Upper.ule(Lower)) {
Nick Lewycky61b4a262007-07-14 02:51:34 +0000409 if (CR.Lower.ult(Lower))
410 return *this;
411
412 return ConstantRange(CR.Lower, Upper);
413 }
414 if (getSetSize().ult(CR.getSetSize()))
415 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000416 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000417}
418
419
Nick Lewycky63f11082007-02-11 00:58:49 +0000420/// unionWith - Return the range that results from the union of this range with
Chris Lattner113f2ae2002-09-01 23:53:36 +0000421/// another range. The resultant range is guaranteed to include the elements of
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000422/// both sets, but may contain more. For example, [3, 9) union [12,15) is
423/// [3, 15), which includes 9, 10, and 11, which were not included in either
424/// set before.
Chris Lattner113f2ae2002-09-01 23:53:36 +0000425///
Reid Spencer6a440332007-03-01 07:54:15 +0000426ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000427 assert(getBitWidth() == CR.getBitWidth() &&
428 "ConstantRange types don't agree!");
Chris Lattner113f2ae2002-09-01 23:53:36 +0000429
Nick Lewyckycf87f9e2007-03-02 03:33:05 +0000430 if ( isFullSet() || CR.isEmptySet()) return *this;
431 if (CR.isFullSet() || isEmptySet()) return CR;
Chris Lattner113f2ae2002-09-01 23:53:36 +0000432
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000433 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
434
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000435 if (!isWrappedSet() && !CR.isWrappedSet()) {
Nick Lewycky567daf32009-07-19 03:44:35 +0000436 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
437 // If the two ranges are disjoint, find the smaller gap and bridge it.
438 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
439 if (d1.ult(d2))
440 return ConstantRange(Lower, CR.Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000441 return ConstantRange(CR.Lower, Upper);
Nick Lewycky567daf32009-07-19 03:44:35 +0000442 }
443
444 APInt L = Lower, U = Upper;
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000445 if (CR.Lower.ult(L))
446 L = CR.Lower;
Nick Lewycky567daf32009-07-19 03:44:35 +0000447 if ((CR.Upper - 1).ugt(U - 1))
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000448 U = CR.Upper;
Nick Lewycky567daf32009-07-19 03:44:35 +0000449
450 if (L == 0 && U == 0)
451 return ConstantRange(getBitWidth());
452
453 return ConstantRange(L, U);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000454 }
455
Nick Lewycky567daf32009-07-19 03:44:35 +0000456 if (!CR.isWrappedSet()) {
457 // ------U L----- and ------U L----- : this
458 // L--U L--U : CR
459 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000460 return *this;
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000461
Nick Lewycky567daf32009-07-19 03:44:35 +0000462 // ------U L----- : this
463 // L---------U : CR
464 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000465 return ConstantRange(getBitWidth());
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000466
Nick Lewycky567daf32009-07-19 03:44:35 +0000467 // ----U L---- : this
468 // L---U : CR
469 // <d1> <d2>
470 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000471 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
Nick Lewycky567daf32009-07-19 03:44:35 +0000472 if (d1.ult(d2))
473 return ConstantRange(Lower, CR.Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000474 return ConstantRange(CR.Lower, Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000475 }
476
Nick Lewycky567daf32009-07-19 03:44:35 +0000477 // ----U L----- : this
478 // L----U : CR
479 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
480 return ConstantRange(CR.Lower, Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000481
Nick Lewycky567daf32009-07-19 03:44:35 +0000482 // ------U L---- : this
483 // L-----U : CR
Nick Lewycky228f5b42012-01-03 20:33:00 +0000484 assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) &&
485 "ConstantRange::unionWith missed a case with one range wrapped");
486 return ConstantRange(Lower, CR.Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000487 }
488
Nick Lewycky567daf32009-07-19 03:44:35 +0000489 // ------U L---- and ------U L---- : this
490 // -U L----------- and ------------U L : CR
491 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
492 return ConstantRange(getBitWidth());
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000493
Nick Lewycky567daf32009-07-19 03:44:35 +0000494 APInt L = Lower, U = Upper;
495 if (CR.Upper.ugt(U))
496 U = CR.Upper;
497 if (CR.Lower.ult(L))
498 L = CR.Lower;
Nick Lewyckycf87f9e2007-03-02 03:33:05 +0000499
500 return ConstantRange(L, U);
Chris Lattner113f2ae2002-09-01 23:53:36 +0000501}
Chris Lattnere0bb9eb2002-09-02 00:18:22 +0000502
Chris Lattner55481f72004-03-30 00:20:08 +0000503/// zeroExtend - Return a new range in the specified integer type, which must
504/// be strictly larger than the current type. The returned range will
Reid Spencer266e42b2006-12-23 06:05:41 +0000505/// correspond to the possible range of values as if the source range had been
Chris Lattner55481f72004-03-30 00:20:08 +0000506/// zero extended.
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000507ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000508 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
509
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000510 unsigned SrcTySize = getBitWidth();
Reid Spencere1f3f192007-02-28 17:36:23 +0000511 assert(SrcTySize < DstTySize && "Not a value extension");
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000512 if (isFullSet() || isWrappedSet()) {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000513 // Change into [0, 1 << src bit width)
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000514 APInt LowerExt(DstTySize, 0);
515 if (!Upper) // special case: [X, 0) -- not really wrapping around
516 LowerExt = Lower.zext(DstTySize);
Benjamin Kramerfc3ea6f2013-07-11 16:05:50 +0000517 return ConstantRange(LowerExt, APInt::getOneBitSet(DstTySize, SrcTySize));
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000518 }
Chris Lattner55481f72004-03-30 00:20:08 +0000519
Jay Foad583abbc2010-12-07 08:25:19 +0000520 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
Chris Lattner55481f72004-03-30 00:20:08 +0000521}
522
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000523/// signExtend - Return a new range in the specified integer type, which must
524/// be strictly larger than the current type. The returned range will
525/// correspond to the possible range of values as if the source range had been
526/// sign extended.
527ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000528 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
529
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000530 unsigned SrcTySize = getBitWidth();
531 assert(SrcTySize < DstTySize && "Not a value extension");
Nuno Lopes1112eca2013-10-30 15:36:50 +0000532
533 // special case: [X, INT_MIN) -- not really wrapping around
Nuno Lopes14d4b0c2013-10-31 19:53:53 +0000534 if (Upper.isMinSignedValue())
Nuno Lopes1112eca2013-10-30 15:36:50 +0000535 return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize));
536
Nick Lewyckya35462d2010-09-06 23:52:49 +0000537 if (isFullSet() || isSignWrappedSet()) {
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000538 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
Nick Lewycky5edc4592009-07-13 04:17:23 +0000539 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000540 }
541
Jay Foad583abbc2010-12-07 08:25:19 +0000542 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000543}
544
Chris Lattner55481f72004-03-30 00:20:08 +0000545/// truncate - Return a new range in the specified integer type, which must be
546/// strictly smaller than the current type. The returned range will
Reid Spencer266e42b2006-12-23 06:05:41 +0000547/// correspond to the possible range of values as if the source range had been
Chris Lattner55481f72004-03-30 00:20:08 +0000548/// truncated to the specified type.
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000549ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
Benjamin Kramer6709e052011-11-24 17:24:33 +0000550 assert(getBitWidth() > DstTySize && "Not a value truncation");
Nuno Lopesc14776d2012-07-19 16:27:45 +0000551 if (isEmptySet())
552 return ConstantRange(DstTySize, /*isFullSet=*/false);
553 if (isFullSet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000554 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattner55481f72004-03-30 00:20:08 +0000555
Nuno Lopesc14776d2012-07-19 16:27:45 +0000556 APInt MaxValue = APInt::getMaxValue(DstTySize).zext(getBitWidth());
557 APInt MaxBitValue(getBitWidth(), 0);
558 MaxBitValue.setBit(DstTySize);
559
560 APInt LowerDiv(Lower), UpperDiv(Upper);
561 ConstantRange Union(DstTySize, /*isFullSet=*/false);
562
563 // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue]
564 // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and
565 // then we do the union with [MaxValue, Upper)
566 if (isWrappedSet()) {
567 // if Upper is greater than Max Value, it covers the whole truncated range.
568 if (Upper.uge(MaxValue))
569 return ConstantRange(DstTySize, /*isFullSet=*/true);
570
571 Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
572 UpperDiv = APInt::getMaxValue(getBitWidth());
573
574 // Union covers the MaxValue case, so return if the remaining range is just
575 // MaxValue.
576 if (LowerDiv == UpperDiv)
577 return Union;
578 }
579
580 // Chop off the most significant bits that are past the destination bitwidth.
581 if (LowerDiv.uge(MaxValue)) {
582 APInt Div(getBitWidth(), 0);
583 APInt::udivrem(LowerDiv, MaxBitValue, Div, LowerDiv);
584 UpperDiv = UpperDiv - MaxBitValue * Div;
585 }
586
587 if (UpperDiv.ule(MaxValue))
588 return ConstantRange(LowerDiv.trunc(DstTySize),
589 UpperDiv.trunc(DstTySize)).unionWith(Union);
590
591 // The truncated value wrapps around. Check if we can do better than fullset.
592 APInt UpperModulo = UpperDiv - MaxBitValue;
593 if (UpperModulo.ult(LowerDiv))
594 return ConstantRange(LowerDiv.trunc(DstTySize),
595 UpperModulo.trunc(DstTySize)).unionWith(Union);
596
597 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattner55481f72004-03-30 00:20:08 +0000598}
599
Nuno Lopes640eb702009-11-09 15:36:28 +0000600/// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
601/// value is zero extended, truncated, or left alone to make it that width.
602ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
603 unsigned SrcTySize = getBitWidth();
604 if (SrcTySize > DstTySize)
605 return truncate(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000606 if (SrcTySize < DstTySize)
Nuno Lopes640eb702009-11-09 15:36:28 +0000607 return zeroExtend(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000608 return *this;
Nuno Lopes640eb702009-11-09 15:36:28 +0000609}
610
611/// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
612/// value is sign extended, truncated, or left alone to make it that width.
613ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
614 unsigned SrcTySize = getBitWidth();
615 if (SrcTySize > DstTySize)
616 return truncate(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000617 if (SrcTySize < DstTySize)
Nuno Lopes640eb702009-11-09 15:36:28 +0000618 return signExtend(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000619 return *this;
Nuno Lopes640eb702009-11-09 15:36:28 +0000620}
621
Dan Gohman5035fbf2009-07-09 22:07:27 +0000622ConstantRange
623ConstantRange::add(const ConstantRange &Other) const {
624 if (isEmptySet() || Other.isEmptySet())
625 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewycky73b704d2009-07-13 02:49:08 +0000626 if (isFullSet() || Other.isFullSet())
627 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000628
629 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
630 APInt NewLower = getLower() + Other.getLower();
631 APInt NewUpper = getUpper() + Other.getUpper() - 1;
632 if (NewLower == NewUpper)
633 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
634
635 ConstantRange X = ConstantRange(NewLower, NewUpper);
636 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
637 // We've wrapped, therefore, full set.
638 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
639
640 return X;
Chris Lattnere0bb9eb2002-09-02 00:18:22 +0000641}
642
Dan Gohman5035fbf2009-07-09 22:07:27 +0000643ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000644ConstantRange::sub(const ConstantRange &Other) const {
645 if (isEmptySet() || Other.isEmptySet())
646 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
647 if (isFullSet() || Other.isFullSet())
648 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
649
650 APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
Nick Lewycky5dc6b792011-06-22 21:13:46 +0000651 APInt NewLower = getLower() - Other.getUpper() + 1;
652 APInt NewUpper = getUpper() - Other.getLower();
Nick Lewyckyd385c222010-08-11 22:04:36 +0000653 if (NewLower == NewUpper)
654 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
655
656 ConstantRange X = ConstantRange(NewLower, NewUpper);
657 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
658 // We've wrapped, therefore, full set.
659 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
660
661 return X;
662}
663
664ConstantRange
Dan Gohman5035fbf2009-07-09 22:07:27 +0000665ConstantRange::multiply(const ConstantRange &Other) const {
Dan Gohman3f8ed9e2010-01-26 15:56:18 +0000666 // TODO: If either operand is a single element and the multiply is known to
667 // be non-wrapping, round the result min and max value to the appropriate
668 // multiple of that element. If wrapping is possible, at least adjust the
669 // range according to the greatest power-of-two factor of the single element.
Dan Gohman5325efc2010-01-26 04:13:15 +0000670
Nick Lewycky2951c992009-07-12 02:19:05 +0000671 if (isEmptySet() || Other.isEmptySet())
672 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes986cc182012-07-16 20:47:16 +0000673
James Molloydcc78ec2015-03-06 15:50:47 +0000674 // Multiplication is signedness-independent. However different ranges can be
675 // obtained depending on how the input ranges are treated. These different
676 // ranges are all conservatively correct, but one might be better than the
677 // other. We calculate two ranges; one treating the inputs as unsigned
678 // and the other signed, then return the smallest of these ranges.
679
680 // Unsigned range first.
Nick Lewycky53023892009-07-13 03:27:41 +0000681 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
682 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
683 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
684 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
Nick Lewycky2951c992009-07-12 02:19:05 +0000685
Nick Lewycky53023892009-07-13 03:27:41 +0000686 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
687 this_max * Other_max + 1);
James Molloydcc78ec2015-03-06 15:50:47 +0000688 ConstantRange UR = Result_zext.truncate(getBitWidth());
689
690 // Now the signed range. Because we could be dealing with negative numbers
691 // here, the lower bound is the smallest of the cartesian product of the
692 // lower and upper ranges; for example:
693 // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
694 // Similarly for the upper bound, swapping min for max.
695
696 this_min = getSignedMin().sext(getBitWidth() * 2);
697 this_max = getSignedMax().sext(getBitWidth() * 2);
698 Other_min = Other.getSignedMin().sext(getBitWidth() * 2);
699 Other_max = Other.getSignedMax().sext(getBitWidth() * 2);
700
701 auto L = {this_min * Other_min, this_min * Other_max,
702 this_max * Other_min, this_max * Other_max};
703 auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
704 ConstantRange Result_sext(std::min(L, Compare), std::max(L, Compare) + 1);
705 ConstantRange SR = Result_sext.truncate(getBitWidth());
706
707 return UR.getSetSize().ult(SR.getSetSize()) ? UR : SR;
Dan Gohman5035fbf2009-07-09 22:07:27 +0000708}
709
710ConstantRange
711ConstantRange::smax(const ConstantRange &Other) const {
Dan Gohmandc33ae22009-07-09 23:16:10 +0000712 // X smax Y is: range(smax(X_smin, Y_smin),
713 // smax(X_smax, Y_smax))
714 if (isEmptySet() || Other.isEmptySet())
715 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohmandc33ae22009-07-09 23:16:10 +0000716 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
717 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
718 if (NewU == NewL)
719 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
720 return ConstantRange(NewL, NewU);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000721}
722
723ConstantRange
724ConstantRange::umax(const ConstantRange &Other) const {
725 // X umax Y is: range(umax(X_umin, Y_umin),
726 // umax(X_umax, Y_umax))
727 if (isEmptySet() || Other.isEmptySet())
728 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000729 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
730 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
731 if (NewU == NewL)
732 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
733 return ConstantRange(NewL, NewU);
734}
735
736ConstantRange
Philip Reamesba313122016-02-26 22:08:18 +0000737ConstantRange::smin(const ConstantRange &Other) const {
738 // X smin Y is: range(smin(X_smin, Y_smin),
739 // smin(X_smax, Y_smax))
740 if (isEmptySet() || Other.isEmptySet())
741 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
742 APInt NewL = APIntOps::smin(getSignedMin(), Other.getSignedMin());
743 APInt NewU = APIntOps::smin(getSignedMax(), Other.getSignedMax()) + 1;
744 if (NewU == NewL)
745 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
746 return ConstantRange(NewL, NewU);
747}
748
749ConstantRange
750ConstantRange::umin(const ConstantRange &Other) const {
751 // X umin Y is: range(umin(X_umin, Y_umin),
752 // umin(X_umax, Y_umax))
753 if (isEmptySet() || Other.isEmptySet())
754 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
755 APInt NewL = APIntOps::umin(getUnsignedMin(), Other.getUnsignedMin());
756 APInt NewU = APIntOps::umin(getUnsignedMax(), Other.getUnsignedMax()) + 1;
757 if (NewU == NewL)
758 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
759 return ConstantRange(NewL, NewU);
760}
761
762ConstantRange
Nick Lewyckyf1b8cb32009-07-12 05:18:18 +0000763ConstantRange::udiv(const ConstantRange &RHS) const {
764 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
765 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
766 if (RHS.isFullSet())
767 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
768
769 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
770
771 APInt RHS_umin = RHS.getUnsignedMin();
772 if (RHS_umin == 0) {
773 // We want the lowest value in RHS excluding zero. Usually that would be 1
774 // except for a range in the form of [X, 1) in which case it would be X.
775 if (RHS.getUpper() == 1)
776 RHS_umin = RHS.getLower();
777 else
778 RHS_umin = APInt(getBitWidth(), 1);
779 }
780
781 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
782
783 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
784 // this could occur.
785 if (Lower == Upper)
786 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
787
788 return ConstantRange(Lower, Upper);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000789}
790
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000791ConstantRange
Nick Lewyckyad48e012010-09-07 05:39:02 +0000792ConstantRange::binaryAnd(const ConstantRange &Other) const {
793 if (isEmptySet() || Other.isEmptySet())
794 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
795
796 // TODO: replace this with something less conservative
797
798 APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax());
799 if (umin.isAllOnesValue())
800 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
801 return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1);
802}
803
804ConstantRange
805ConstantRange::binaryOr(const ConstantRange &Other) const {
806 if (isEmptySet() || Other.isEmptySet())
807 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
808
809 // TODO: replace this with something less conservative
810
811 APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
812 if (umax.isMinValue())
813 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
814 return ConstantRange(umax, APInt::getNullValue(getBitWidth()));
815}
816
817ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000818ConstantRange::shl(const ConstantRange &Other) const {
819 if (isEmptySet() || Other.isEmptySet())
820 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000821
Nick Lewyckyd385c222010-08-11 22:04:36 +0000822 APInt min = getUnsignedMin().shl(Other.getUnsignedMin());
823 APInt max = getUnsignedMax().shl(Other.getUnsignedMax());
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000824
825 // there's no overflow!
Nuno Lopesf8fcac72009-11-12 15:10:33 +0000826 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
Nick Lewyckyd385c222010-08-11 22:04:36 +0000827 if (Zeros.ugt(Other.getUnsignedMax()))
828 return ConstantRange(min, max + 1);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000829
830 // FIXME: implement the other tricky cases
Nick Lewyckyd385c222010-08-11 22:04:36 +0000831 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000832}
833
834ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000835ConstantRange::lshr(const ConstantRange &Other) const {
836 if (isEmptySet() || Other.isEmptySet())
837 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000838
Nick Lewyckyd385c222010-08-11 22:04:36 +0000839 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin());
840 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
841 if (min == max + 1)
842 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
843
844 return ConstantRange(min, max + 1);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000845}
846
Owen Anderson1a9078b2010-08-07 05:47:46 +0000847ConstantRange ConstantRange::inverse() const {
Nick Lewycky228f5b42012-01-03 20:33:00 +0000848 if (isFullSet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000849 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000850 if (isEmptySet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000851 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Owen Anderson1a9078b2010-08-07 05:47:46 +0000852 return ConstantRange(Upper, Lower);
853}
854
Dan Gohmandc33ae22009-07-09 23:16:10 +0000855/// print - Print out the bounds to a stream...
Chris Lattnere0bb9eb2002-09-02 00:18:22 +0000856///
Dan Gohmandc33ae22009-07-09 23:16:10 +0000857void ConstantRange::print(raw_ostream &OS) const {
Dan Gohman837ada72010-01-26 04:12:55 +0000858 if (isFullSet())
859 OS << "full-set";
860 else if (isEmptySet())
861 OS << "empty-set";
862 else
863 OS << "[" << Lower << "," << Upper << ")";
Dan Gohman5035fbf2009-07-09 22:07:27 +0000864}
865
Dan Gohmandc33ae22009-07-09 23:16:10 +0000866/// dump - Allow printing from a debugger easily...
Dan Gohman5035fbf2009-07-09 22:07:27 +0000867///
Yaron Kereneb2a2542016-01-29 20:50:44 +0000868LLVM_DUMP_METHOD void ConstantRange::dump() const {
David Greenede7b3532010-01-05 01:28:32 +0000869 print(dbgs());
Dan Gohman5035fbf2009-07-09 22:07:27 +0000870}