blob: 072a9a96635ff55d47bbe174d1c2b2f6ab26290c [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
Dan Gohmandc33ae22009-07-09 23:16:10 +000032ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) {
Chris Lattner113f2ae2002-09-01 23:53:36 +000033 if (Full)
Reid Spencere1f3f192007-02-28 17:36:23 +000034 Lower = Upper = APInt::getMaxValue(BitWidth);
Chris Lattner113f2ae2002-09-01 23:53:36 +000035 else
Reid Spencere1f3f192007-02-28 17:36:23 +000036 Lower = Upper = APInt::getMinValue(BitWidth);
Chris Lattner113f2ae2002-09-01 23:53:36 +000037}
38
Craig Topper37df0182017-04-13 00:20:31 +000039ConstantRange::ConstantRange(APInt V)
Chandler Carruth002da5d2014-03-02 04:08:41 +000040 : Lower(std::move(V)), Upper(Lower + 1) {}
Chris Lattner113f2ae2002-09-01 23:53:36 +000041
Craig Topper37df0182017-04-13 00:20:31 +000042ConstantRange::ConstantRange(APInt L, APInt U)
Chandler Carruth002da5d2014-03-02 04:08:41 +000043 : Lower(std::move(L)), Upper(std::move(U)) {
Benjamin Kramercce97be2013-07-11 15:37:27 +000044 assert(Lower.getBitWidth() == Upper.getBitWidth() &&
Dan Gohmandc33ae22009-07-09 23:16:10 +000045 "ConstantRange with unequal bit widths");
Benjamin Kramercce97be2013-07-11 15:37:27 +000046 assert((Lower != Upper || (Lower.isMaxValue() || Lower.isMinValue())) &&
Reid Spencere1f3f192007-02-28 17:36:23 +000047 "Lower == Upper, but they aren't min or max value!");
48}
49
Sanjoy Das7182d362015-03-18 00:41:24 +000050ConstantRange ConstantRange::makeAllowedICmpRegion(CmpInst::Predicate Pred,
51 const ConstantRange &CR) {
Nick Lewycky5154ee02010-09-28 18:18:36 +000052 if (CR.isEmptySet())
53 return CR;
54
Nick Lewyckydcfdce92009-07-11 06:15:39 +000055 uint32_t W = CR.getBitWidth();
56 switch (Pred) {
Sanjoy Das7182d362015-03-18 00:41:24 +000057 default:
58 llvm_unreachable("Invalid ICmp predicate to makeAllowedICmpRegion()");
Sanjay Patelf8ee0e02016-06-19 17:20:27 +000059 case CmpInst::ICMP_EQ:
60 return CR;
61 case CmpInst::ICMP_NE:
62 if (CR.isSingleElement())
63 return ConstantRange(CR.getUpper(), CR.getLower());
64 return ConstantRange(W);
65 case CmpInst::ICMP_ULT: {
66 APInt UMax(CR.getUnsignedMax());
67 if (UMax.isMinValue())
68 return ConstantRange(W, /* empty */ false);
69 return ConstantRange(APInt::getMinValue(W), UMax);
70 }
71 case CmpInst::ICMP_SLT: {
72 APInt SMax(CR.getSignedMax());
73 if (SMax.isMinSignedValue())
74 return ConstantRange(W, /* empty */ false);
75 return ConstantRange(APInt::getSignedMinValue(W), SMax);
76 }
77 case CmpInst::ICMP_ULE: {
78 APInt UMax(CR.getUnsignedMax());
79 if (UMax.isMaxValue())
Nick Lewyckydcfdce92009-07-11 06:15:39 +000080 return ConstantRange(W);
Sanjay Patelf8ee0e02016-06-19 17:20:27 +000081 return ConstantRange(APInt::getMinValue(W), UMax + 1);
82 }
83 case CmpInst::ICMP_SLE: {
84 APInt SMax(CR.getSignedMax());
85 if (SMax.isMaxSignedValue())
86 return ConstantRange(W);
87 return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
88 }
89 case CmpInst::ICMP_UGT: {
90 APInt UMin(CR.getUnsignedMin());
91 if (UMin.isMaxValue())
92 return ConstantRange(W, /* empty */ false);
93 return ConstantRange(UMin + 1, APInt::getNullValue(W));
94 }
95 case CmpInst::ICMP_SGT: {
96 APInt SMin(CR.getSignedMin());
97 if (SMin.isMaxSignedValue())
98 return ConstantRange(W, /* empty */ false);
99 return ConstantRange(SMin + 1, APInt::getSignedMinValue(W));
100 }
101 case CmpInst::ICMP_UGE: {
102 APInt UMin(CR.getUnsignedMin());
103 if (UMin.isMinValue())
104 return ConstantRange(W);
105 return ConstantRange(UMin, APInt::getNullValue(W));
106 }
107 case CmpInst::ICMP_SGE: {
108 APInt SMin(CR.getSignedMin());
109 if (SMin.isMinSignedValue())
110 return ConstantRange(W);
111 return ConstantRange(SMin, APInt::getSignedMinValue(W));
112 }
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000113 }
114}
115
Sanjoy Das7182d362015-03-18 00:41:24 +0000116ConstantRange ConstantRange::makeSatisfyingICmpRegion(CmpInst::Predicate Pred,
117 const ConstantRange &CR) {
118 // Follows from De-Morgan's laws:
119 //
120 // ~(~A union ~B) == A intersect B.
121 //
122 return makeAllowedICmpRegion(CmpInst::getInversePredicate(Pred), CR)
123 .inverse();
124}
125
Balaram Makam569eaec2016-05-04 21:32:14 +0000126ConstantRange ConstantRange::makeExactICmpRegion(CmpInst::Predicate Pred,
127 const APInt &C) {
128 // Computes the exact range that is equal to both the constant ranges returned
129 // by makeAllowedICmpRegion and makeSatisfyingICmpRegion. This is always true
130 // when RHS is a singleton such as an APInt and so the assert is valid.
131 // However for non-singleton RHS, for example ult [2,5) makeAllowedICmpRegion
132 // returns [0,4) but makeSatisfyICmpRegion returns [0,2).
133 //
134 assert(makeAllowedICmpRegion(Pred, C) == makeSatisfyingICmpRegion(Pred, C));
135 return makeAllowedICmpRegion(Pred, C);
136}
137
Sanjoy Das590614c2016-05-19 03:53:06 +0000138bool ConstantRange::getEquivalentICmp(CmpInst::Predicate &Pred,
139 APInt &RHS) const {
140 bool Success = false;
141
142 if (isFullSet() || isEmptySet()) {
143 Pred = isEmptySet() ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE;
144 RHS = APInt(getBitWidth(), 0);
145 Success = true;
Sanjoy Dasc7d32912016-10-02 20:59:05 +0000146 } else if (auto *OnlyElt = getSingleElement()) {
147 Pred = CmpInst::ICMP_EQ;
148 RHS = *OnlyElt;
149 Success = true;
150 } else if (auto *OnlyMissingElt = getSingleMissingElement()) {
151 Pred = CmpInst::ICMP_NE;
152 RHS = *OnlyMissingElt;
153 Success = true;
Sanjoy Das590614c2016-05-19 03:53:06 +0000154 } else if (getLower().isMinSignedValue() || getLower().isMinValue()) {
155 Pred =
156 getLower().isMinSignedValue() ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT;
157 RHS = getUpper();
158 Success = true;
159 } else if (getUpper().isMinSignedValue() || getUpper().isMinValue()) {
160 Pred =
161 getUpper().isMinSignedValue() ? CmpInst::ICMP_SGE : CmpInst::ICMP_UGE;
162 RHS = getLower();
163 Success = true;
164 }
165
166 assert((!Success || ConstantRange::makeExactICmpRegion(Pred, RHS) == *this) &&
167 "Bad result!");
168
169 return Success;
170}
171
Sanjoy Das5079f622016-02-22 16:13:02 +0000172ConstantRange
173ConstantRange::makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp,
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000174 const ConstantRange &Other,
175 unsigned NoWrapKind) {
Sanjoy Das6ed05302015-10-22 03:12:57 +0000176 typedef OverflowingBinaryOperator OBO;
177
178 // Computes the intersection of CR0 and CR1. It is different from
179 // intersectWith in that the ConstantRange returned will only contain elements
180 // in both CR0 and CR1 (i.e. SubsetIntersect(X, Y) is a *subset*, proper or
181 // not, of both X and Y).
182 auto SubsetIntersect =
183 [](const ConstantRange &CR0, const ConstantRange &CR1) {
184 return CR0.inverse().unionWith(CR1.inverse()).inverse();
185 };
186
187 assert(BinOp >= Instruction::BinaryOpsBegin &&
188 BinOp < Instruction::BinaryOpsEnd && "Binary operators only!");
189
190 assert((NoWrapKind == OBO::NoSignedWrap ||
191 NoWrapKind == OBO::NoUnsignedWrap ||
192 NoWrapKind == (OBO::NoUnsignedWrap | OBO::NoSignedWrap)) &&
193 "NoWrapKind invalid!");
194
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000195 unsigned BitWidth = Other.getBitWidth();
Sanjoy Das6ed05302015-10-22 03:12:57 +0000196 if (BinOp != Instruction::Add)
197 // Conservative answer: empty set
198 return ConstantRange(BitWidth, false);
199
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000200 if (auto *C = Other.getSingleElement())
Craig Toppere8dea1b2017-04-28 21:48:09 +0000201 if (C->isNullValue())
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000202 // Full set: nothing signed / unsigned wraps when added to 0.
203 return ConstantRange(BitWidth);
Sanjoy Das6ed05302015-10-22 03:12:57 +0000204
205 ConstantRange Result(BitWidth);
206
207 if (NoWrapKind & OBO::NoUnsignedWrap)
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000208 Result =
209 SubsetIntersect(Result, ConstantRange(APInt::getNullValue(BitWidth),
210 -Other.getUnsignedMax()));
Sanjoy Das6ed05302015-10-22 03:12:57 +0000211
212 if (NoWrapKind & OBO::NoSignedWrap) {
Craig Topper72235d02017-04-28 21:48:03 +0000213 const APInt &SignedMin = Other.getSignedMin();
214 const APInt &SignedMax = Other.getSignedMax();
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000215
216 if (SignedMax.isStrictlyPositive())
Sanjoy Das6ed05302015-10-22 03:12:57 +0000217 Result = SubsetIntersect(
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000218 Result,
219 ConstantRange(APInt::getSignedMinValue(BitWidth),
220 APInt::getSignedMinValue(BitWidth) - SignedMax));
221
222 if (SignedMin.isNegative())
Sanjoy Das6ed05302015-10-22 03:12:57 +0000223 Result = SubsetIntersect(
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000224 Result, ConstantRange(APInt::getSignedMinValue(BitWidth) - SignedMin,
Sanjoy Das6ed05302015-10-22 03:12:57 +0000225 APInt::getSignedMinValue(BitWidth)));
226 }
227
228 return Result;
229}
230
Chris Lattner113f2ae2002-09-01 23:53:36 +0000231bool ConstantRange::isFullSet() const {
Zhou Sheng31787362007-04-26 16:42:07 +0000232 return Lower == Upper && Lower.isMaxValue();
Chris Lattner113f2ae2002-09-01 23:53:36 +0000233}
Misha Brukmanb1c93172005-04-21 23:48:37 +0000234
Chris Lattner113f2ae2002-09-01 23:53:36 +0000235bool 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
Reid Spencer6a440332007-03-01 07:54:15 +0000239bool ConstantRange::isWrappedSet() const {
Reid Spencere1f3f192007-02-28 17:36:23 +0000240 return Lower.ugt(Upper);
Chris Lattner113f2ae2002-09-01 23:53:36 +0000241}
242
Nick Lewyckya35462d2010-09-06 23:52:49 +0000243bool ConstantRange::isSignWrappedSet() const {
244 return contains(APInt::getSignedMaxValue(getBitWidth())) &&
245 contains(APInt::getSignedMinValue(getBitWidth()));
246}
247
Reid Spencere1f3f192007-02-28 17:36:23 +0000248APInt ConstantRange::getSetSize() const {
Nuno Lopes216d5712012-07-17 15:43:59 +0000249 if (isFullSet()) {
250 APInt Size(getBitWidth()+1, 0);
251 Size.setBit(getBitWidth());
252 return Size;
Chris Lattner113f2ae2002-09-01 23:53:36 +0000253 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000254
Nuno Lopes216d5712012-07-17 15:43:59 +0000255 // This is also correct for wrapped sets.
Nuno Lopes99504c52012-07-16 18:08:12 +0000256 return (Upper - Lower).zext(getBitWidth()+1);
Chris Lattner113f2ae2002-09-01 23:53:36 +0000257}
258
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000259bool
260ConstantRange::isSizeStrictlySmallerThanOf(const ConstantRange &Other) const {
261 assert(getBitWidth() == Other.getBitWidth());
262 if (isFullSet())
263 return false;
264 if (Other.isFullSet())
265 return true;
266 return (Upper - Lower).ult(Other.Upper - Other.Lower);
267}
268
Nick Lewyckye4559372007-03-10 15:54:12 +0000269APInt ConstantRange::getUnsignedMax() const {
270 if (isFullSet() || isWrappedSet())
271 return APInt::getMaxValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000272 return getUpper() - 1;
Nick Lewyckye4559372007-03-10 15:54:12 +0000273}
274
Nick Lewyckye4559372007-03-10 15:54:12 +0000275APInt ConstantRange::getUnsignedMin() const {
276 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
277 return APInt::getMinValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000278 return getLower();
Nick Lewyckye4559372007-03-10 15:54:12 +0000279}
280
Nick Lewyckye4559372007-03-10 15:54:12 +0000281APInt ConstantRange::getSignedMax() const {
Zhou Sheng01c175e2007-04-13 05:57:32 +0000282 APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
Nick Lewyckye4559372007-03-10 15:54:12 +0000283 if (!isWrappedSet()) {
Craig Topper88c64f32017-04-18 23:02:39 +0000284 APInt UpperMinusOne = getUpper() - 1;
285 if (getLower().sle(UpperMinusOne))
286 return UpperMinusOne;
287 return APInt::getSignedMaxValue(getBitWidth());
Nick Lewyckye4559372007-03-10 15:54:12 +0000288 }
Nick Lewycky228f5b42012-01-03 20:33:00 +0000289 if (getLower().isNegative() == getUpper().isNegative())
Craig Topper88c64f32017-04-18 23:02:39 +0000290 return APInt::getSignedMaxValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000291 return getUpper() - 1;
Nick Lewyckye4559372007-03-10 15:54:12 +0000292}
293
Nick Lewyckye4559372007-03-10 15:54:12 +0000294APInt ConstantRange::getSignedMin() const {
Nick Lewyckye4559372007-03-10 15:54:12 +0000295 if (!isWrappedSet()) {
Nick Lewyckyd18b1602007-06-09 04:20:33 +0000296 if (getLower().sle(getUpper() - 1))
Nick Lewyckye4559372007-03-10 15:54:12 +0000297 return getLower();
Craig Topper88c64f32017-04-18 23:02:39 +0000298 return APInt::getSignedMinValue(getBitWidth());
Nick Lewyckye4559372007-03-10 15:54:12 +0000299 }
Nick Lewycky228f5b42012-01-03 20:33:00 +0000300 if ((getUpper() - 1).slt(getLower())) {
Craig Topper88c64f32017-04-18 23:02:39 +0000301 if (!getUpper().isMinSignedValue())
302 return APInt::getSignedMinValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000303 }
304 return getLower();
Nick Lewyckye4559372007-03-10 15:54:12 +0000305}
306
Reid Spencer6a440332007-03-01 07:54:15 +0000307bool ConstantRange::contains(const APInt &V) const {
308 if (Lower == Upper)
309 return isFullSet();
Chris Lattner113f2ae2002-09-01 23:53:36 +0000310
Reid Spencer6a440332007-03-01 07:54:15 +0000311 if (!isWrappedSet())
312 return Lower.ule(V) && V.ult(Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000313 return Lower.ule(V) || V.ult(Upper);
Chris Lattner55481f72004-03-30 00:20:08 +0000314}
315
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000316bool ConstantRange::contains(const ConstantRange &Other) const {
Nick Lewyckyd385c222010-08-11 22:04:36 +0000317 if (isFullSet() || Other.isEmptySet()) return true;
318 if (isEmptySet() || Other.isFullSet()) return false;
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000319
320 if (!isWrappedSet()) {
321 if (Other.isWrappedSet())
322 return false;
323
324 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
325 }
326
327 if (!Other.isWrappedSet())
328 return Other.getUpper().ule(Upper) ||
329 Lower.ule(Other.getLower());
330
331 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
332}
333
Reid Spencer3a7e9d82007-02-28 19:57:34 +0000334ConstantRange ConstantRange::subtract(const APInt &Val) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000335 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
Chris Lattner55481f72004-03-30 00:20:08 +0000336 // If the set is empty or full, don't modify the endpoints.
Reid Spencere1f3f192007-02-28 17:36:23 +0000337 if (Lower == Upper)
338 return *this;
Reid Spencere1f3f192007-02-28 17:36:23 +0000339 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattner55481f72004-03-30 00:20:08 +0000340}
Chris Lattner113f2ae2002-09-01 23:53:36 +0000341
Nuno Lopes5020db22012-06-28 16:10:13 +0000342ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
343 return intersectWith(CR.inverse());
344}
345
Reid Spencer6a440332007-03-01 07:54:15 +0000346ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000347 assert(getBitWidth() == CR.getBitWidth() &&
348 "ConstantRange types don't agree!");
Nick Lewycky61b4a262007-07-14 02:51:34 +0000349
350 // Handle common cases.
351 if ( isEmptySet() || CR.isFullSet()) return *this;
352 if (CR.isEmptySet() || isFullSet()) return CR;
353
354 if (!isWrappedSet() && CR.isWrappedSet())
Nick Lewycky0d139032009-07-18 06:34:42 +0000355 return CR.intersectWith(*this);
Nick Lewycky61b4a262007-07-14 02:51:34 +0000356
357 if (!isWrappedSet() && !CR.isWrappedSet()) {
358 if (Lower.ult(CR.Lower)) {
359 if (Upper.ule(CR.Lower))
360 return ConstantRange(getBitWidth(), false);
361
362 if (Upper.ult(CR.Upper))
363 return ConstantRange(CR.Lower, Upper);
364
365 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000366 }
Nick Lewycky228f5b42012-01-03 20:33:00 +0000367 if (Upper.ult(CR.Upper))
368 return *this;
369
370 if (Lower.ult(CR.Upper))
371 return ConstantRange(Lower, CR.Upper);
372
373 return ConstantRange(getBitWidth(), false);
Nick Lewycky61b4a262007-07-14 02:51:34 +0000374 }
375
376 if (isWrappedSet() && !CR.isWrappedSet()) {
377 if (CR.Lower.ult(Upper)) {
378 if (CR.Upper.ult(Upper))
379 return CR;
380
Nuno Lopes63afc082012-05-18 00:14:36 +0000381 if (CR.Upper.ule(Lower))
Nick Lewycky61b4a262007-07-14 02:51:34 +0000382 return ConstantRange(CR.Lower, Upper);
383
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000384 if (isSizeStrictlySmallerThanOf(CR))
Nick Lewycky61b4a262007-07-14 02:51:34 +0000385 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000386 return CR;
387 }
388 if (CR.Lower.ult(Lower)) {
Nick Lewycky61b4a262007-07-14 02:51:34 +0000389 if (CR.Upper.ule(Lower))
390 return ConstantRange(getBitWidth(), false);
391
392 return ConstantRange(Lower, CR.Upper);
393 }
394 return CR;
395 }
396
397 if (CR.Upper.ult(Upper)) {
398 if (CR.Lower.ult(Upper)) {
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000399 if (isSizeStrictlySmallerThanOf(CR))
Nick Lewycky61b4a262007-07-14 02:51:34 +0000400 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000401 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000402 }
403
404 if (CR.Lower.ult(Lower))
405 return ConstantRange(Lower, CR.Upper);
406
407 return CR;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000408 }
Nuno Lopesebb0c942012-06-28 00:59:33 +0000409 if (CR.Upper.ule(Lower)) {
Nick Lewycky61b4a262007-07-14 02:51:34 +0000410 if (CR.Lower.ult(Lower))
411 return *this;
412
413 return ConstantRange(CR.Lower, Upper);
414 }
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000415 if (isSizeStrictlySmallerThanOf(CR))
Nick Lewycky61b4a262007-07-14 02:51:34 +0000416 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000417 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000418}
419
Reid Spencer6a440332007-03-01 07:54:15 +0000420ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000421 assert(getBitWidth() == CR.getBitWidth() &&
422 "ConstantRange types don't agree!");
Chris Lattner113f2ae2002-09-01 23:53:36 +0000423
Nick Lewyckycf87f9e2007-03-02 03:33:05 +0000424 if ( isFullSet() || CR.isEmptySet()) return *this;
425 if (CR.isFullSet() || isEmptySet()) return CR;
Chris Lattner113f2ae2002-09-01 23:53:36 +0000426
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000427 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
428
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000429 if (!isWrappedSet() && !CR.isWrappedSet()) {
Nick Lewycky567daf32009-07-19 03:44:35 +0000430 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
431 // If the two ranges are disjoint, find the smaller gap and bridge it.
432 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
433 if (d1.ult(d2))
434 return ConstantRange(Lower, CR.Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000435 return ConstantRange(CR.Lower, Upper);
Nick Lewycky567daf32009-07-19 03:44:35 +0000436 }
437
438 APInt L = Lower, U = Upper;
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000439 if (CR.Lower.ult(L))
440 L = CR.Lower;
Nick Lewycky567daf32009-07-19 03:44:35 +0000441 if ((CR.Upper - 1).ugt(U - 1))
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000442 U = CR.Upper;
Nick Lewycky567daf32009-07-19 03:44:35 +0000443
444 if (L == 0 && U == 0)
445 return ConstantRange(getBitWidth());
446
447 return ConstantRange(L, U);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000448 }
449
Nick Lewycky567daf32009-07-19 03:44:35 +0000450 if (!CR.isWrappedSet()) {
451 // ------U L----- and ------U L----- : this
452 // L--U L--U : CR
453 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000454 return *this;
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000455
Nick Lewycky567daf32009-07-19 03:44:35 +0000456 // ------U L----- : this
457 // L---------U : CR
458 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000459 return ConstantRange(getBitWidth());
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000460
Nick Lewycky567daf32009-07-19 03:44:35 +0000461 // ----U L---- : this
462 // L---U : CR
463 // <d1> <d2>
464 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000465 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
Nick Lewycky567daf32009-07-19 03:44:35 +0000466 if (d1.ult(d2))
467 return ConstantRange(Lower, CR.Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000468 return ConstantRange(CR.Lower, Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000469 }
470
Nick Lewycky567daf32009-07-19 03:44:35 +0000471 // ----U L----- : this
472 // L----U : CR
473 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
474 return ConstantRange(CR.Lower, Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000475
Nick Lewycky567daf32009-07-19 03:44:35 +0000476 // ------U L---- : this
477 // L-----U : CR
Nick Lewycky228f5b42012-01-03 20:33:00 +0000478 assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) &&
479 "ConstantRange::unionWith missed a case with one range wrapped");
480 return ConstantRange(Lower, CR.Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000481 }
482
Nick Lewycky567daf32009-07-19 03:44:35 +0000483 // ------U L---- and ------U L---- : this
484 // -U L----------- and ------------U L : CR
485 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
486 return ConstantRange(getBitWidth());
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000487
Nick Lewycky567daf32009-07-19 03:44:35 +0000488 APInt L = Lower, U = Upper;
489 if (CR.Upper.ugt(U))
490 U = CR.Upper;
491 if (CR.Lower.ult(L))
492 L = CR.Lower;
Nick Lewyckycf87f9e2007-03-02 03:33:05 +0000493
494 return ConstantRange(L, U);
Chris Lattner113f2ae2002-09-01 23:53:36 +0000495}
Chris Lattnere0bb9eb2002-09-02 00:18:22 +0000496
Philip Reames4d00af12016-12-01 20:08:47 +0000497ConstantRange ConstantRange::castOp(Instruction::CastOps CastOp,
498 uint32_t ResultBitWidth) const {
499 switch (CastOp) {
500 default:
501 llvm_unreachable("unsupported cast type");
502 case Instruction::Trunc:
503 return truncate(ResultBitWidth);
504 case Instruction::SExt:
505 return signExtend(ResultBitWidth);
506 case Instruction::ZExt:
507 return zeroExtend(ResultBitWidth);
508 case Instruction::BitCast:
509 return *this;
510 case Instruction::FPToUI:
511 case Instruction::FPToSI:
512 if (getBitWidth() == ResultBitWidth)
513 return *this;
514 else
515 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
516 case Instruction::UIToFP: {
517 // TODO: use input range if available
518 auto BW = getBitWidth();
519 APInt Min = APInt::getMinValue(BW).zextOrSelf(ResultBitWidth);
520 APInt Max = APInt::getMaxValue(BW).zextOrSelf(ResultBitWidth);
521 return ConstantRange(Min, Max);
522 }
523 case Instruction::SIToFP: {
524 // TODO: use input range if available
525 auto BW = getBitWidth();
526 APInt SMin = APInt::getSignedMinValue(BW).sextOrSelf(ResultBitWidth);
527 APInt SMax = APInt::getSignedMaxValue(BW).sextOrSelf(ResultBitWidth);
528 return ConstantRange(SMin, SMax);
529 }
530 case Instruction::FPTrunc:
531 case Instruction::FPExt:
532 case Instruction::IntToPtr:
533 case Instruction::PtrToInt:
534 case Instruction::AddrSpaceCast:
535 // Conservatively return full set.
536 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
537 };
538}
539
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000540ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000541 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
542
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000543 unsigned SrcTySize = getBitWidth();
Reid Spencere1f3f192007-02-28 17:36:23 +0000544 assert(SrcTySize < DstTySize && "Not a value extension");
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000545 if (isFullSet() || isWrappedSet()) {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000546 // Change into [0, 1 << src bit width)
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000547 APInt LowerExt(DstTySize, 0);
548 if (!Upper) // special case: [X, 0) -- not really wrapping around
549 LowerExt = Lower.zext(DstTySize);
Benjamin Kramerfc3ea6f2013-07-11 16:05:50 +0000550 return ConstantRange(LowerExt, APInt::getOneBitSet(DstTySize, SrcTySize));
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000551 }
Chris Lattner55481f72004-03-30 00:20:08 +0000552
Jay Foad583abbc2010-12-07 08:25:19 +0000553 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
Chris Lattner55481f72004-03-30 00:20:08 +0000554}
555
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000556ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000557 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
558
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000559 unsigned SrcTySize = getBitWidth();
560 assert(SrcTySize < DstTySize && "Not a value extension");
Nuno Lopes1112eca2013-10-30 15:36:50 +0000561
562 // special case: [X, INT_MIN) -- not really wrapping around
Nuno Lopes14d4b0c2013-10-31 19:53:53 +0000563 if (Upper.isMinSignedValue())
Nuno Lopes1112eca2013-10-30 15:36:50 +0000564 return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize));
565
Nick Lewyckya35462d2010-09-06 23:52:49 +0000566 if (isFullSet() || isSignWrappedSet()) {
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000567 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
Nick Lewycky5edc4592009-07-13 04:17:23 +0000568 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000569 }
570
Jay Foad583abbc2010-12-07 08:25:19 +0000571 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000572}
573
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000574ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
Benjamin Kramer6709e052011-11-24 17:24:33 +0000575 assert(getBitWidth() > DstTySize && "Not a value truncation");
Nuno Lopesc14776d2012-07-19 16:27:45 +0000576 if (isEmptySet())
577 return ConstantRange(DstTySize, /*isFullSet=*/false);
578 if (isFullSet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000579 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattner55481f72004-03-30 00:20:08 +0000580
Nuno Lopesc14776d2012-07-19 16:27:45 +0000581 APInt MaxValue = APInt::getMaxValue(DstTySize).zext(getBitWidth());
582 APInt MaxBitValue(getBitWidth(), 0);
583 MaxBitValue.setBit(DstTySize);
584
585 APInt LowerDiv(Lower), UpperDiv(Upper);
586 ConstantRange Union(DstTySize, /*isFullSet=*/false);
587
588 // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue]
589 // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and
590 // then we do the union with [MaxValue, Upper)
591 if (isWrappedSet()) {
Nick Lewyckybfad0152016-06-06 01:51:23 +0000592 // If Upper is greater than Max Value, it covers the whole truncated range.
Nuno Lopesc14776d2012-07-19 16:27:45 +0000593 if (Upper.uge(MaxValue))
594 return ConstantRange(DstTySize, /*isFullSet=*/true);
595
596 Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
597 UpperDiv = APInt::getMaxValue(getBitWidth());
598
599 // Union covers the MaxValue case, so return if the remaining range is just
600 // MaxValue.
601 if (LowerDiv == UpperDiv)
602 return Union;
603 }
604
605 // Chop off the most significant bits that are past the destination bitwidth.
606 if (LowerDiv.uge(MaxValue)) {
607 APInt Div(getBitWidth(), 0);
608 APInt::udivrem(LowerDiv, MaxBitValue, Div, LowerDiv);
609 UpperDiv = UpperDiv - MaxBitValue * Div;
610 }
611
612 if (UpperDiv.ule(MaxValue))
613 return ConstantRange(LowerDiv.trunc(DstTySize),
614 UpperDiv.trunc(DstTySize)).unionWith(Union);
615
Nick Lewyckybfad0152016-06-06 01:51:23 +0000616 // The truncated value wraps around. Check if we can do better than fullset.
Nuno Lopesc14776d2012-07-19 16:27:45 +0000617 APInt UpperModulo = UpperDiv - MaxBitValue;
618 if (UpperModulo.ult(LowerDiv))
619 return ConstantRange(LowerDiv.trunc(DstTySize),
620 UpperModulo.trunc(DstTySize)).unionWith(Union);
621
622 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattner55481f72004-03-30 00:20:08 +0000623}
624
Nuno Lopes640eb702009-11-09 15:36:28 +0000625ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
626 unsigned SrcTySize = getBitWidth();
627 if (SrcTySize > DstTySize)
628 return truncate(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000629 if (SrcTySize < DstTySize)
Nuno Lopes640eb702009-11-09 15:36:28 +0000630 return zeroExtend(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000631 return *this;
Nuno Lopes640eb702009-11-09 15:36:28 +0000632}
633
Nuno Lopes640eb702009-11-09 15:36:28 +0000634ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
635 unsigned SrcTySize = getBitWidth();
636 if (SrcTySize > DstTySize)
637 return truncate(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000638 if (SrcTySize < DstTySize)
Nuno Lopes640eb702009-11-09 15:36:28 +0000639 return signExtend(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000640 return *this;
Nuno Lopes640eb702009-11-09 15:36:28 +0000641}
642
Philip Reames4d00af12016-12-01 20:08:47 +0000643ConstantRange ConstantRange::binaryOp(Instruction::BinaryOps BinOp,
644 const ConstantRange &Other) const {
645 assert(BinOp >= Instruction::BinaryOpsBegin &&
646 BinOp < Instruction::BinaryOpsEnd && "Binary operators only!");
647
648 switch (BinOp) {
649 case Instruction::Add:
650 return add(Other);
651 case Instruction::Sub:
652 return sub(Other);
653 case Instruction::Mul:
654 return multiply(Other);
655 case Instruction::UDiv:
656 return udiv(Other);
657 case Instruction::Shl:
658 return shl(Other);
659 case Instruction::LShr:
660 return lshr(Other);
661 case Instruction::And:
662 return binaryAnd(Other);
663 case Instruction::Or:
664 return binaryOr(Other);
665 // Note: floating point operations applied to abstract ranges are just
666 // ideal integer operations with a lossy representation
667 case Instruction::FAdd:
668 return add(Other);
669 case Instruction::FSub:
670 return sub(Other);
671 case Instruction::FMul:
672 return multiply(Other);
673 default:
674 // Conservatively return full set.
675 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
676 }
677}
678
Dan Gohman5035fbf2009-07-09 22:07:27 +0000679ConstantRange
680ConstantRange::add(const ConstantRange &Other) const {
681 if (isEmptySet() || Other.isEmptySet())
682 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewycky73b704d2009-07-13 02:49:08 +0000683 if (isFullSet() || Other.isFullSet())
684 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000685
Dan Gohman5035fbf2009-07-09 22:07:27 +0000686 APInt NewLower = getLower() + Other.getLower();
687 APInt NewUpper = getUpper() + Other.getUpper() - 1;
688 if (NewLower == NewUpper)
689 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
690
691 ConstantRange X = ConstantRange(NewLower, NewUpper);
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000692 if (X.isSizeStrictlySmallerThanOf(*this) ||
693 X.isSizeStrictlySmallerThanOf(Other))
Dan Gohman5035fbf2009-07-09 22:07:27 +0000694 // We've wrapped, therefore, full set.
695 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000696 return X;
Chris Lattnere0bb9eb2002-09-02 00:18:22 +0000697}
698
Artur Pilipenkoed841032016-10-19 14:44:23 +0000699ConstantRange ConstantRange::addWithNoSignedWrap(const APInt &Other) const {
700 // Calculate the subset of this range such that "X + Other" is
701 // guaranteed not to wrap (overflow) for all X in this subset.
702 // makeGuaranteedNoWrapRegion will produce an exact NSW range since we are
703 // passing a single element range.
704 auto NSWRange = ConstantRange::makeGuaranteedNoWrapRegion(BinaryOperator::Add,
705 ConstantRange(Other),
706 OverflowingBinaryOperator::NoSignedWrap);
707 auto NSWConstrainedRange = intersectWith(NSWRange);
708
709 return NSWConstrainedRange.add(ConstantRange(Other));
710}
711
Dan Gohman5035fbf2009-07-09 22:07:27 +0000712ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000713ConstantRange::sub(const ConstantRange &Other) const {
714 if (isEmptySet() || Other.isEmptySet())
715 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
716 if (isFullSet() || Other.isFullSet())
717 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
718
Nick Lewycky5dc6b792011-06-22 21:13:46 +0000719 APInt NewLower = getLower() - Other.getUpper() + 1;
720 APInt NewUpper = getUpper() - Other.getLower();
Nick Lewyckyd385c222010-08-11 22:04:36 +0000721 if (NewLower == NewUpper)
722 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
723
724 ConstantRange X = ConstantRange(NewLower, NewUpper);
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000725 if (X.isSizeStrictlySmallerThanOf(*this) ||
726 X.isSizeStrictlySmallerThanOf(Other))
Nick Lewyckyd385c222010-08-11 22:04:36 +0000727 // We've wrapped, therefore, full set.
728 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Nick Lewyckyd385c222010-08-11 22:04:36 +0000729 return X;
730}
731
732ConstantRange
Dan Gohman5035fbf2009-07-09 22:07:27 +0000733ConstantRange::multiply(const ConstantRange &Other) const {
Dan Gohman3f8ed9e2010-01-26 15:56:18 +0000734 // TODO: If either operand is a single element and the multiply is known to
735 // be non-wrapping, round the result min and max value to the appropriate
736 // multiple of that element. If wrapping is possible, at least adjust the
737 // range according to the greatest power-of-two factor of the single element.
Dan Gohman5325efc2010-01-26 04:13:15 +0000738
Nick Lewycky2951c992009-07-12 02:19:05 +0000739 if (isEmptySet() || Other.isEmptySet())
740 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes986cc182012-07-16 20:47:16 +0000741
James Molloydcc78ec2015-03-06 15:50:47 +0000742 // Multiplication is signedness-independent. However different ranges can be
743 // obtained depending on how the input ranges are treated. These different
744 // ranges are all conservatively correct, but one might be better than the
745 // other. We calculate two ranges; one treating the inputs as unsigned
746 // and the other signed, then return the smallest of these ranges.
747
748 // Unsigned range first.
Nick Lewycky53023892009-07-13 03:27:41 +0000749 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
750 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
751 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
752 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
Nick Lewycky2951c992009-07-12 02:19:05 +0000753
Nick Lewycky53023892009-07-13 03:27:41 +0000754 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
755 this_max * Other_max + 1);
James Molloydcc78ec2015-03-06 15:50:47 +0000756 ConstantRange UR = Result_zext.truncate(getBitWidth());
757
Pete Cooper34a7a942016-05-27 17:06:50 +0000758 // If the unsigned range doesn't wrap, and isn't negative then it's a range
759 // from one positive number to another which is as good as we can generate.
760 // In this case, skip the extra work of generating signed ranges which aren't
761 // going to be better than this range.
762 if (!UR.isWrappedSet() && UR.getLower().isNonNegative())
763 return UR;
764
James Molloydcc78ec2015-03-06 15:50:47 +0000765 // Now the signed range. Because we could be dealing with negative numbers
766 // here, the lower bound is the smallest of the cartesian product of the
767 // lower and upper ranges; for example:
768 // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
769 // Similarly for the upper bound, swapping min for max.
770
771 this_min = getSignedMin().sext(getBitWidth() * 2);
772 this_max = getSignedMax().sext(getBitWidth() * 2);
773 Other_min = Other.getSignedMin().sext(getBitWidth() * 2);
774 Other_max = Other.getSignedMax().sext(getBitWidth() * 2);
775
776 auto L = {this_min * Other_min, this_min * Other_max,
777 this_max * Other_min, this_max * Other_max};
778 auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
779 ConstantRange Result_sext(std::min(L, Compare), std::max(L, Compare) + 1);
780 ConstantRange SR = Result_sext.truncate(getBitWidth());
781
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000782 return UR.isSizeStrictlySmallerThanOf(SR) ? UR : SR;
Dan Gohman5035fbf2009-07-09 22:07:27 +0000783}
784
785ConstantRange
786ConstantRange::smax(const ConstantRange &Other) const {
Dan Gohmandc33ae22009-07-09 23:16:10 +0000787 // X smax Y is: range(smax(X_smin, Y_smin),
788 // smax(X_smax, Y_smax))
789 if (isEmptySet() || Other.isEmptySet())
790 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohmandc33ae22009-07-09 23:16:10 +0000791 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
792 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
793 if (NewU == NewL)
794 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
795 return ConstantRange(NewL, NewU);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000796}
797
798ConstantRange
799ConstantRange::umax(const ConstantRange &Other) const {
800 // X umax Y is: range(umax(X_umin, Y_umin),
801 // umax(X_umax, Y_umax))
802 if (isEmptySet() || Other.isEmptySet())
803 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000804 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
805 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
806 if (NewU == NewL)
807 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
808 return ConstantRange(NewL, NewU);
809}
810
811ConstantRange
Philip Reamesba313122016-02-26 22:08:18 +0000812ConstantRange::smin(const ConstantRange &Other) const {
813 // X smin Y is: range(smin(X_smin, Y_smin),
814 // smin(X_smax, Y_smax))
815 if (isEmptySet() || Other.isEmptySet())
816 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
817 APInt NewL = APIntOps::smin(getSignedMin(), Other.getSignedMin());
818 APInt NewU = APIntOps::smin(getSignedMax(), Other.getSignedMax()) + 1;
819 if (NewU == NewL)
820 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
821 return ConstantRange(NewL, NewU);
822}
823
824ConstantRange
825ConstantRange::umin(const ConstantRange &Other) const {
826 // X umin Y is: range(umin(X_umin, Y_umin),
827 // umin(X_umax, Y_umax))
828 if (isEmptySet() || Other.isEmptySet())
829 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
830 APInt NewL = APIntOps::umin(getUnsignedMin(), Other.getUnsignedMin());
831 APInt NewU = APIntOps::umin(getUnsignedMax(), Other.getUnsignedMax()) + 1;
832 if (NewU == NewL)
833 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
834 return ConstantRange(NewL, NewU);
835}
836
837ConstantRange
Nick Lewyckyf1b8cb32009-07-12 05:18:18 +0000838ConstantRange::udiv(const ConstantRange &RHS) const {
839 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
840 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
841 if (RHS.isFullSet())
842 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
843
844 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
845
846 APInt RHS_umin = RHS.getUnsignedMin();
847 if (RHS_umin == 0) {
848 // We want the lowest value in RHS excluding zero. Usually that would be 1
849 // except for a range in the form of [X, 1) in which case it would be X.
850 if (RHS.getUpper() == 1)
851 RHS_umin = RHS.getLower();
852 else
853 RHS_umin = APInt(getBitWidth(), 1);
854 }
855
856 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
857
858 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
859 // this could occur.
860 if (Lower == Upper)
861 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
862
863 return ConstantRange(Lower, Upper);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000864}
865
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000866ConstantRange
Nick Lewyckyad48e012010-09-07 05:39:02 +0000867ConstantRange::binaryAnd(const ConstantRange &Other) const {
868 if (isEmptySet() || Other.isEmptySet())
869 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
870
871 // TODO: replace this with something less conservative
872
873 APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax());
874 if (umin.isAllOnesValue())
875 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
876 return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1);
877}
878
879ConstantRange
880ConstantRange::binaryOr(const ConstantRange &Other) const {
881 if (isEmptySet() || Other.isEmptySet())
882 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
883
884 // TODO: replace this with something less conservative
885
886 APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
Craig Toppere8dea1b2017-04-28 21:48:09 +0000887 if (umax.isNullValue())
Nick Lewyckyad48e012010-09-07 05:39:02 +0000888 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
889 return ConstantRange(umax, APInt::getNullValue(getBitWidth()));
890}
891
892ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000893ConstantRange::shl(const ConstantRange &Other) const {
894 if (isEmptySet() || Other.isEmptySet())
895 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000896
Nick Lewyckyd385c222010-08-11 22:04:36 +0000897 APInt min = getUnsignedMin().shl(Other.getUnsignedMin());
898 APInt max = getUnsignedMax().shl(Other.getUnsignedMax());
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000899
900 // there's no overflow!
Nuno Lopesf8fcac72009-11-12 15:10:33 +0000901 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
Nick Lewyckyd385c222010-08-11 22:04:36 +0000902 if (Zeros.ugt(Other.getUnsignedMax()))
903 return ConstantRange(min, max + 1);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000904
905 // FIXME: implement the other tricky cases
Nick Lewyckyd385c222010-08-11 22:04:36 +0000906 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000907}
908
909ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000910ConstantRange::lshr(const ConstantRange &Other) const {
911 if (isEmptySet() || Other.isEmptySet())
912 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000913
Nick Lewyckyd385c222010-08-11 22:04:36 +0000914 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin());
915 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
916 if (min == max + 1)
917 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
918
919 return ConstantRange(min, max + 1);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000920}
921
Owen Anderson1a9078b2010-08-07 05:47:46 +0000922ConstantRange ConstantRange::inverse() const {
Nick Lewycky228f5b42012-01-03 20:33:00 +0000923 if (isFullSet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000924 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000925 if (isEmptySet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000926 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Owen Anderson1a9078b2010-08-07 05:47:46 +0000927 return ConstantRange(Upper, Lower);
928}
929
Dan Gohmandc33ae22009-07-09 23:16:10 +0000930void ConstantRange::print(raw_ostream &OS) const {
Dan Gohman837ada72010-01-26 04:12:55 +0000931 if (isFullSet())
932 OS << "full-set";
933 else if (isEmptySet())
934 OS << "empty-set";
935 else
936 OS << "[" << Lower << "," << Upper << ")";
Dan Gohman5035fbf2009-07-09 22:07:27 +0000937}
938
Matthias Braun8c209aa2017-01-28 02:02:38 +0000939#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000940LLVM_DUMP_METHOD void ConstantRange::dump() const {
David Greenede7b3532010-01-05 01:28:32 +0000941 print(dbgs());
Dan Gohman5035fbf2009-07-09 22:07:27 +0000942}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000943#endif
Peter Collingbourneecdd58f2016-10-21 19:59:26 +0000944
945ConstantRange llvm::getConstantRangeFromMetadata(const MDNode &Ranges) {
946 const unsigned NumRanges = Ranges.getNumOperands() / 2;
947 assert(NumRanges >= 1 && "Must have at least one range!");
948 assert(Ranges.getNumOperands() % 2 == 0 && "Must be a sequence of pairs");
949
950 auto *FirstLow = mdconst::extract<ConstantInt>(Ranges.getOperand(0));
951 auto *FirstHigh = mdconst::extract<ConstantInt>(Ranges.getOperand(1));
952
953 ConstantRange CR(FirstLow->getValue(), FirstHigh->getValue());
954
955 for (unsigned i = 1; i < NumRanges; ++i) {
956 auto *Low = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
957 auto *High = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
958
959 // Note: unionWith will potentially create a range that contains values not
960 // contained in any of the original N ranges.
961 CR = CR.unionWith(ConstantRange(Low->getValue(), High->getValue()));
962 }
963
964 return CR;
965}