blob: 5425676e4edcfe6074f0463cb1f003b2d1dfa91d [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
Craig Topperee4f22d2017-04-29 05:08:52 +000032ConstantRange::ConstantRange(uint32_t BitWidth, bool Full)
33 : Lower(Full ? APInt::getMaxValue(BitWidth) : APInt::getMinValue(BitWidth)),
34 Upper(Lower) {}
Chris Lattner113f2ae2002-09-01 23:53:36 +000035
Craig Topper37df0182017-04-13 00:20:31 +000036ConstantRange::ConstantRange(APInt V)
Chandler Carruth002da5d2014-03-02 04:08:41 +000037 : Lower(std::move(V)), Upper(Lower + 1) {}
Chris Lattner113f2ae2002-09-01 23:53:36 +000038
Craig Topper37df0182017-04-13 00:20:31 +000039ConstantRange::ConstantRange(APInt L, APInt U)
Chandler Carruth002da5d2014-03-02 04:08:41 +000040 : Lower(std::move(L)), Upper(std::move(U)) {
Benjamin Kramercce97be2013-07-11 15:37:27 +000041 assert(Lower.getBitWidth() == Upper.getBitWidth() &&
Dan Gohmandc33ae22009-07-09 23:16:10 +000042 "ConstantRange with unequal bit widths");
Benjamin Kramercce97be2013-07-11 15:37:27 +000043 assert((Lower != Upper || (Lower.isMaxValue() || Lower.isMinValue())) &&
Reid Spencere1f3f192007-02-28 17:36:23 +000044 "Lower == Upper, but they aren't min or max value!");
45}
46
Sanjoy Das7182d362015-03-18 00:41:24 +000047ConstantRange ConstantRange::makeAllowedICmpRegion(CmpInst::Predicate Pred,
48 const ConstantRange &CR) {
Nick Lewycky5154ee02010-09-28 18:18:36 +000049 if (CR.isEmptySet())
50 return CR;
51
Nick Lewyckydcfdce92009-07-11 06:15:39 +000052 uint32_t W = CR.getBitWidth();
53 switch (Pred) {
Sanjoy Das7182d362015-03-18 00:41:24 +000054 default:
55 llvm_unreachable("Invalid ICmp predicate to makeAllowedICmpRegion()");
Sanjay Patelf8ee0e02016-06-19 17:20:27 +000056 case CmpInst::ICMP_EQ:
57 return CR;
58 case CmpInst::ICMP_NE:
59 if (CR.isSingleElement())
60 return ConstantRange(CR.getUpper(), CR.getLower());
61 return ConstantRange(W);
62 case CmpInst::ICMP_ULT: {
63 APInt UMax(CR.getUnsignedMax());
64 if (UMax.isMinValue())
65 return ConstantRange(W, /* empty */ false);
Craig Topperb7920252017-04-29 06:40:47 +000066 return ConstantRange(APInt::getMinValue(W), std::move(UMax));
Sanjay Patelf8ee0e02016-06-19 17:20:27 +000067 }
68 case CmpInst::ICMP_SLT: {
69 APInt SMax(CR.getSignedMax());
70 if (SMax.isMinSignedValue())
71 return ConstantRange(W, /* empty */ false);
Craig Topperb7920252017-04-29 06:40:47 +000072 return ConstantRange(APInt::getSignedMinValue(W), std::move(SMax));
Sanjay Patelf8ee0e02016-06-19 17:20:27 +000073 }
74 case CmpInst::ICMP_ULE: {
75 APInt UMax(CR.getUnsignedMax());
76 if (UMax.isMaxValue())
Nick Lewyckydcfdce92009-07-11 06:15:39 +000077 return ConstantRange(W);
Craig Topperb7920252017-04-29 06:40:47 +000078 return ConstantRange(APInt::getMinValue(W), std::move(UMax) + 1);
Sanjay Patelf8ee0e02016-06-19 17:20:27 +000079 }
80 case CmpInst::ICMP_SLE: {
81 APInt SMax(CR.getSignedMax());
82 if (SMax.isMaxSignedValue())
83 return ConstantRange(W);
Craig Topperb7920252017-04-29 06:40:47 +000084 return ConstantRange(APInt::getSignedMinValue(W), std::move(SMax) + 1);
Sanjay Patelf8ee0e02016-06-19 17:20:27 +000085 }
86 case CmpInst::ICMP_UGT: {
87 APInt UMin(CR.getUnsignedMin());
88 if (UMin.isMaxValue())
89 return ConstantRange(W, /* empty */ false);
Craig Topperb7920252017-04-29 06:40:47 +000090 return ConstantRange(std::move(UMin) + 1, APInt::getNullValue(W));
Sanjay Patelf8ee0e02016-06-19 17:20:27 +000091 }
92 case CmpInst::ICMP_SGT: {
93 APInt SMin(CR.getSignedMin());
94 if (SMin.isMaxSignedValue())
95 return ConstantRange(W, /* empty */ false);
Craig Topperb7920252017-04-29 06:40:47 +000096 return ConstantRange(std::move(SMin) + 1, APInt::getSignedMinValue(W));
Sanjay Patelf8ee0e02016-06-19 17:20:27 +000097 }
98 case CmpInst::ICMP_UGE: {
99 APInt UMin(CR.getUnsignedMin());
100 if (UMin.isMinValue())
101 return ConstantRange(W);
Craig Topperb7920252017-04-29 06:40:47 +0000102 return ConstantRange(std::move(UMin), APInt::getNullValue(W));
Sanjay Patelf8ee0e02016-06-19 17:20:27 +0000103 }
104 case CmpInst::ICMP_SGE: {
105 APInt SMin(CR.getSignedMin());
106 if (SMin.isMinSignedValue())
107 return ConstantRange(W);
Craig Topperb7920252017-04-29 06:40:47 +0000108 return ConstantRange(std::move(SMin), APInt::getSignedMinValue(W));
Sanjay Patelf8ee0e02016-06-19 17:20:27 +0000109 }
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000110 }
111}
112
Sanjoy Das7182d362015-03-18 00:41:24 +0000113ConstantRange ConstantRange::makeSatisfyingICmpRegion(CmpInst::Predicate Pred,
114 const ConstantRange &CR) {
115 // Follows from De-Morgan's laws:
116 //
117 // ~(~A union ~B) == A intersect B.
118 //
119 return makeAllowedICmpRegion(CmpInst::getInversePredicate(Pred), CR)
120 .inverse();
121}
122
Balaram Makam569eaec2016-05-04 21:32:14 +0000123ConstantRange ConstantRange::makeExactICmpRegion(CmpInst::Predicate Pred,
124 const APInt &C) {
125 // Computes the exact range that is equal to both the constant ranges returned
126 // by makeAllowedICmpRegion and makeSatisfyingICmpRegion. This is always true
127 // when RHS is a singleton such as an APInt and so the assert is valid.
128 // However for non-singleton RHS, for example ult [2,5) makeAllowedICmpRegion
129 // returns [0,4) but makeSatisfyICmpRegion returns [0,2).
130 //
131 assert(makeAllowedICmpRegion(Pred, C) == makeSatisfyingICmpRegion(Pred, C));
132 return makeAllowedICmpRegion(Pred, C);
133}
134
Sanjoy Das590614c2016-05-19 03:53:06 +0000135bool ConstantRange::getEquivalentICmp(CmpInst::Predicate &Pred,
136 APInt &RHS) const {
137 bool Success = false;
138
139 if (isFullSet() || isEmptySet()) {
140 Pred = isEmptySet() ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE;
141 RHS = APInt(getBitWidth(), 0);
142 Success = true;
Sanjoy Dasc7d32912016-10-02 20:59:05 +0000143 } else if (auto *OnlyElt = getSingleElement()) {
144 Pred = CmpInst::ICMP_EQ;
145 RHS = *OnlyElt;
146 Success = true;
147 } else if (auto *OnlyMissingElt = getSingleMissingElement()) {
148 Pred = CmpInst::ICMP_NE;
149 RHS = *OnlyMissingElt;
150 Success = true;
Sanjoy Das590614c2016-05-19 03:53:06 +0000151 } else if (getLower().isMinSignedValue() || getLower().isMinValue()) {
152 Pred =
153 getLower().isMinSignedValue() ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT;
154 RHS = getUpper();
155 Success = true;
156 } else if (getUpper().isMinSignedValue() || getUpper().isMinValue()) {
157 Pred =
158 getUpper().isMinSignedValue() ? CmpInst::ICMP_SGE : CmpInst::ICMP_UGE;
159 RHS = getLower();
160 Success = true;
161 }
162
163 assert((!Success || ConstantRange::makeExactICmpRegion(Pred, RHS) == *this) &&
164 "Bad result!");
165
166 return Success;
167}
168
Sanjoy Das5079f622016-02-22 16:13:02 +0000169ConstantRange
170ConstantRange::makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp,
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000171 const ConstantRange &Other,
172 unsigned NoWrapKind) {
Sanjoy Das6ed05302015-10-22 03:12:57 +0000173 typedef OverflowingBinaryOperator OBO;
174
175 // Computes the intersection of CR0 and CR1. It is different from
176 // intersectWith in that the ConstantRange returned will only contain elements
177 // in both CR0 and CR1 (i.e. SubsetIntersect(X, Y) is a *subset*, proper or
178 // not, of both X and Y).
179 auto SubsetIntersect =
180 [](const ConstantRange &CR0, const ConstantRange &CR1) {
181 return CR0.inverse().unionWith(CR1.inverse()).inverse();
182 };
183
184 assert(BinOp >= Instruction::BinaryOpsBegin &&
185 BinOp < Instruction::BinaryOpsEnd && "Binary operators only!");
186
187 assert((NoWrapKind == OBO::NoSignedWrap ||
188 NoWrapKind == OBO::NoUnsignedWrap ||
189 NoWrapKind == (OBO::NoUnsignedWrap | OBO::NoSignedWrap)) &&
190 "NoWrapKind invalid!");
191
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000192 unsigned BitWidth = Other.getBitWidth();
Sanjoy Das6ed05302015-10-22 03:12:57 +0000193 if (BinOp != Instruction::Add)
194 // Conservative answer: empty set
195 return ConstantRange(BitWidth, false);
196
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000197 if (auto *C = Other.getSingleElement())
Craig Toppere8dea1b2017-04-28 21:48:09 +0000198 if (C->isNullValue())
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000199 // Full set: nothing signed / unsigned wraps when added to 0.
200 return ConstantRange(BitWidth);
Sanjoy Das6ed05302015-10-22 03:12:57 +0000201
202 ConstantRange Result(BitWidth);
203
204 if (NoWrapKind & OBO::NoUnsignedWrap)
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000205 Result =
206 SubsetIntersect(Result, ConstantRange(APInt::getNullValue(BitWidth),
207 -Other.getUnsignedMax()));
Sanjoy Das6ed05302015-10-22 03:12:57 +0000208
209 if (NoWrapKind & OBO::NoSignedWrap) {
Craig Topper72235d02017-04-28 21:48:03 +0000210 const APInt &SignedMin = Other.getSignedMin();
211 const APInt &SignedMax = Other.getSignedMax();
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000212
213 if (SignedMax.isStrictlyPositive())
Sanjoy Das6ed05302015-10-22 03:12:57 +0000214 Result = SubsetIntersect(
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000215 Result,
216 ConstantRange(APInt::getSignedMinValue(BitWidth),
217 APInt::getSignedMinValue(BitWidth) - SignedMax));
218
219 if (SignedMin.isNegative())
Sanjoy Das6ed05302015-10-22 03:12:57 +0000220 Result = SubsetIntersect(
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000221 Result, ConstantRange(APInt::getSignedMinValue(BitWidth) - SignedMin,
Sanjoy Das6ed05302015-10-22 03:12:57 +0000222 APInt::getSignedMinValue(BitWidth)));
223 }
224
225 return Result;
226}
227
Chris Lattner113f2ae2002-09-01 23:53:36 +0000228bool ConstantRange::isFullSet() const {
Zhou Sheng31787362007-04-26 16:42:07 +0000229 return Lower == Upper && Lower.isMaxValue();
Chris Lattner113f2ae2002-09-01 23:53:36 +0000230}
Misha Brukmanb1c93172005-04-21 23:48:37 +0000231
Chris Lattner113f2ae2002-09-01 23:53:36 +0000232bool ConstantRange::isEmptySet() const {
Zhou Sheng31787362007-04-26 16:42:07 +0000233 return Lower == Upper && Lower.isMinValue();
Chris Lattner113f2ae2002-09-01 23:53:36 +0000234}
235
Reid Spencer6a440332007-03-01 07:54:15 +0000236bool ConstantRange::isWrappedSet() const {
Reid Spencere1f3f192007-02-28 17:36:23 +0000237 return Lower.ugt(Upper);
Chris Lattner113f2ae2002-09-01 23:53:36 +0000238}
239
Nick Lewyckya35462d2010-09-06 23:52:49 +0000240bool ConstantRange::isSignWrappedSet() const {
241 return contains(APInt::getSignedMaxValue(getBitWidth())) &&
242 contains(APInt::getSignedMinValue(getBitWidth()));
243}
244
Reid Spencere1f3f192007-02-28 17:36:23 +0000245APInt ConstantRange::getSetSize() const {
Craig Topper8c5c6fe2017-04-29 17:59:41 +0000246 if (isFullSet())
247 return APInt::getOneBitSet(getBitWidth()+1, getBitWidth());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000248
Nuno Lopes216d5712012-07-17 15:43:59 +0000249 // This is also correct for wrapped sets.
Nuno Lopes99504c52012-07-16 18:08:12 +0000250 return (Upper - Lower).zext(getBitWidth()+1);
Chris Lattner113f2ae2002-09-01 23:53:36 +0000251}
252
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000253bool
254ConstantRange::isSizeStrictlySmallerThanOf(const ConstantRange &Other) const {
255 assert(getBitWidth() == Other.getBitWidth());
256 if (isFullSet())
257 return false;
258 if (Other.isFullSet())
259 return true;
260 return (Upper - Lower).ult(Other.Upper - Other.Lower);
261}
262
Nick Lewyckye4559372007-03-10 15:54:12 +0000263APInt ConstantRange::getUnsignedMax() const {
264 if (isFullSet() || isWrappedSet())
265 return APInt::getMaxValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000266 return getUpper() - 1;
Nick Lewyckye4559372007-03-10 15:54:12 +0000267}
268
Nick Lewyckye4559372007-03-10 15:54:12 +0000269APInt ConstantRange::getUnsignedMin() const {
270 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
271 return APInt::getMinValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000272 return getLower();
Nick Lewyckye4559372007-03-10 15:54:12 +0000273}
274
Nick Lewyckye4559372007-03-10 15:54:12 +0000275APInt ConstantRange::getSignedMax() const {
Nick Lewyckye4559372007-03-10 15:54:12 +0000276 if (!isWrappedSet()) {
Craig Topper88c64f32017-04-18 23:02:39 +0000277 APInt UpperMinusOne = getUpper() - 1;
278 if (getLower().sle(UpperMinusOne))
279 return UpperMinusOne;
280 return APInt::getSignedMaxValue(getBitWidth());
Nick Lewyckye4559372007-03-10 15:54:12 +0000281 }
Nick Lewycky228f5b42012-01-03 20:33:00 +0000282 if (getLower().isNegative() == getUpper().isNegative())
Craig Topper88c64f32017-04-18 23:02:39 +0000283 return APInt::getSignedMaxValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000284 return getUpper() - 1;
Nick Lewyckye4559372007-03-10 15:54:12 +0000285}
286
Nick Lewyckye4559372007-03-10 15:54:12 +0000287APInt ConstantRange::getSignedMin() const {
Nick Lewyckye4559372007-03-10 15:54:12 +0000288 if (!isWrappedSet()) {
Nick Lewyckyd18b1602007-06-09 04:20:33 +0000289 if (getLower().sle(getUpper() - 1))
Nick Lewyckye4559372007-03-10 15:54:12 +0000290 return getLower();
Craig Topper88c64f32017-04-18 23:02:39 +0000291 return APInt::getSignedMinValue(getBitWidth());
Nick Lewyckye4559372007-03-10 15:54:12 +0000292 }
Nick Lewycky228f5b42012-01-03 20:33:00 +0000293 if ((getUpper() - 1).slt(getLower())) {
Craig Topper88c64f32017-04-18 23:02:39 +0000294 if (!getUpper().isMinSignedValue())
295 return APInt::getSignedMinValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000296 }
297 return getLower();
Nick Lewyckye4559372007-03-10 15:54:12 +0000298}
299
Reid Spencer6a440332007-03-01 07:54:15 +0000300bool ConstantRange::contains(const APInt &V) const {
301 if (Lower == Upper)
302 return isFullSet();
Chris Lattner113f2ae2002-09-01 23:53:36 +0000303
Reid Spencer6a440332007-03-01 07:54:15 +0000304 if (!isWrappedSet())
305 return Lower.ule(V) && V.ult(Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000306 return Lower.ule(V) || V.ult(Upper);
Chris Lattner55481f72004-03-30 00:20:08 +0000307}
308
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000309bool ConstantRange::contains(const ConstantRange &Other) const {
Nick Lewyckyd385c222010-08-11 22:04:36 +0000310 if (isFullSet() || Other.isEmptySet()) return true;
311 if (isEmptySet() || Other.isFullSet()) return false;
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000312
313 if (!isWrappedSet()) {
314 if (Other.isWrappedSet())
315 return false;
316
317 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
318 }
319
320 if (!Other.isWrappedSet())
321 return Other.getUpper().ule(Upper) ||
322 Lower.ule(Other.getLower());
323
324 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
325}
326
Reid Spencer3a7e9d82007-02-28 19:57:34 +0000327ConstantRange ConstantRange::subtract(const APInt &Val) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000328 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
Chris Lattner55481f72004-03-30 00:20:08 +0000329 // If the set is empty or full, don't modify the endpoints.
Reid Spencere1f3f192007-02-28 17:36:23 +0000330 if (Lower == Upper)
331 return *this;
Reid Spencere1f3f192007-02-28 17:36:23 +0000332 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattner55481f72004-03-30 00:20:08 +0000333}
Chris Lattner113f2ae2002-09-01 23:53:36 +0000334
Nuno Lopes5020db22012-06-28 16:10:13 +0000335ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
336 return intersectWith(CR.inverse());
337}
338
Reid Spencer6a440332007-03-01 07:54:15 +0000339ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000340 assert(getBitWidth() == CR.getBitWidth() &&
341 "ConstantRange types don't agree!");
Nick Lewycky61b4a262007-07-14 02:51:34 +0000342
343 // Handle common cases.
344 if ( isEmptySet() || CR.isFullSet()) return *this;
345 if (CR.isEmptySet() || isFullSet()) return CR;
346
347 if (!isWrappedSet() && CR.isWrappedSet())
Nick Lewycky0d139032009-07-18 06:34:42 +0000348 return CR.intersectWith(*this);
Nick Lewycky61b4a262007-07-14 02:51:34 +0000349
350 if (!isWrappedSet() && !CR.isWrappedSet()) {
351 if (Lower.ult(CR.Lower)) {
352 if (Upper.ule(CR.Lower))
353 return ConstantRange(getBitWidth(), false);
354
355 if (Upper.ult(CR.Upper))
356 return ConstantRange(CR.Lower, Upper);
357
358 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000359 }
Nick Lewycky228f5b42012-01-03 20:33:00 +0000360 if (Upper.ult(CR.Upper))
361 return *this;
362
363 if (Lower.ult(CR.Upper))
364 return ConstantRange(Lower, CR.Upper);
365
366 return ConstantRange(getBitWidth(), false);
Nick Lewycky61b4a262007-07-14 02:51:34 +0000367 }
368
369 if (isWrappedSet() && !CR.isWrappedSet()) {
370 if (CR.Lower.ult(Upper)) {
371 if (CR.Upper.ult(Upper))
372 return CR;
373
Nuno Lopes63afc082012-05-18 00:14:36 +0000374 if (CR.Upper.ule(Lower))
Nick Lewycky61b4a262007-07-14 02:51:34 +0000375 return ConstantRange(CR.Lower, Upper);
376
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000377 if (isSizeStrictlySmallerThanOf(CR))
Nick Lewycky61b4a262007-07-14 02:51:34 +0000378 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000379 return CR;
380 }
381 if (CR.Lower.ult(Lower)) {
Nick Lewycky61b4a262007-07-14 02:51:34 +0000382 if (CR.Upper.ule(Lower))
383 return ConstantRange(getBitWidth(), false);
384
385 return ConstantRange(Lower, CR.Upper);
386 }
387 return CR;
388 }
389
390 if (CR.Upper.ult(Upper)) {
391 if (CR.Lower.ult(Upper)) {
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000392 if (isSizeStrictlySmallerThanOf(CR))
Nick Lewycky61b4a262007-07-14 02:51:34 +0000393 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000394 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000395 }
396
397 if (CR.Lower.ult(Lower))
398 return ConstantRange(Lower, CR.Upper);
399
400 return CR;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000401 }
Nuno Lopesebb0c942012-06-28 00:59:33 +0000402 if (CR.Upper.ule(Lower)) {
Nick Lewycky61b4a262007-07-14 02:51:34 +0000403 if (CR.Lower.ult(Lower))
404 return *this;
405
406 return ConstantRange(CR.Lower, Upper);
407 }
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000408 if (isSizeStrictlySmallerThanOf(CR))
Nick Lewycky61b4a262007-07-14 02:51:34 +0000409 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000410 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000411}
412
Reid Spencer6a440332007-03-01 07:54:15 +0000413ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000414 assert(getBitWidth() == CR.getBitWidth() &&
415 "ConstantRange types don't agree!");
Chris Lattner113f2ae2002-09-01 23:53:36 +0000416
Nick Lewyckycf87f9e2007-03-02 03:33:05 +0000417 if ( isFullSet() || CR.isEmptySet()) return *this;
418 if (CR.isFullSet() || isEmptySet()) return CR;
Chris Lattner113f2ae2002-09-01 23:53:36 +0000419
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000420 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
421
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000422 if (!isWrappedSet() && !CR.isWrappedSet()) {
Nick Lewycky567daf32009-07-19 03:44:35 +0000423 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
424 // If the two ranges are disjoint, find the smaller gap and bridge it.
425 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
426 if (d1.ult(d2))
427 return ConstantRange(Lower, CR.Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000428 return ConstantRange(CR.Lower, Upper);
Nick Lewycky567daf32009-07-19 03:44:35 +0000429 }
430
Craig Topper8fb5a142017-04-29 07:24:13 +0000431 APInt L = CR.Lower.ult(Lower) ? CR.Lower : Lower;
432 APInt U = (CR.Upper - 1).ugt(Upper - 1) ? CR.Upper : Upper;
Nick Lewycky567daf32009-07-19 03:44:35 +0000433
434 if (L == 0 && U == 0)
435 return ConstantRange(getBitWidth());
436
Craig Topperb7920252017-04-29 06:40:47 +0000437 return ConstantRange(std::move(L), std::move(U));
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000438 }
439
Nick Lewycky567daf32009-07-19 03:44:35 +0000440 if (!CR.isWrappedSet()) {
441 // ------U L----- and ------U L----- : this
442 // L--U L--U : CR
443 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000444 return *this;
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000445
Nick Lewycky567daf32009-07-19 03:44:35 +0000446 // ------U L----- : this
447 // L---------U : CR
448 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000449 return ConstantRange(getBitWidth());
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000450
Nick Lewycky567daf32009-07-19 03:44:35 +0000451 // ----U L---- : this
452 // L---U : CR
453 // <d1> <d2>
454 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000455 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
Nick Lewycky567daf32009-07-19 03:44:35 +0000456 if (d1.ult(d2))
457 return ConstantRange(Lower, CR.Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000458 return ConstantRange(CR.Lower, Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000459 }
460
Nick Lewycky567daf32009-07-19 03:44:35 +0000461 // ----U L----- : this
462 // L----U : CR
463 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
464 return ConstantRange(CR.Lower, Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000465
Nick Lewycky567daf32009-07-19 03:44:35 +0000466 // ------U L---- : this
467 // L-----U : CR
Nick Lewycky228f5b42012-01-03 20:33:00 +0000468 assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) &&
469 "ConstantRange::unionWith missed a case with one range wrapped");
470 return ConstantRange(Lower, CR.Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000471 }
472
Nick Lewycky567daf32009-07-19 03:44:35 +0000473 // ------U L---- and ------U L---- : this
474 // -U L----------- and ------------U L : CR
475 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
476 return ConstantRange(getBitWidth());
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000477
Craig Topper8fb5a142017-04-29 07:24:13 +0000478 APInt L = CR.Lower.ult(Lower) ? CR.Lower : Lower;
479 APInt U = CR.Upper.ugt(Upper) ? CR.Upper : Upper;
Nick Lewyckycf87f9e2007-03-02 03:33:05 +0000480
Craig Topperb7920252017-04-29 06:40:47 +0000481 return ConstantRange(std::move(L), std::move(U));
Chris Lattner113f2ae2002-09-01 23:53:36 +0000482}
Chris Lattnere0bb9eb2002-09-02 00:18:22 +0000483
Philip Reames4d00af12016-12-01 20:08:47 +0000484ConstantRange ConstantRange::castOp(Instruction::CastOps CastOp,
485 uint32_t ResultBitWidth) const {
486 switch (CastOp) {
487 default:
488 llvm_unreachable("unsupported cast type");
489 case Instruction::Trunc:
490 return truncate(ResultBitWidth);
491 case Instruction::SExt:
492 return signExtend(ResultBitWidth);
493 case Instruction::ZExt:
494 return zeroExtend(ResultBitWidth);
495 case Instruction::BitCast:
496 return *this;
497 case Instruction::FPToUI:
498 case Instruction::FPToSI:
499 if (getBitWidth() == ResultBitWidth)
500 return *this;
501 else
502 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
503 case Instruction::UIToFP: {
504 // TODO: use input range if available
505 auto BW = getBitWidth();
506 APInt Min = APInt::getMinValue(BW).zextOrSelf(ResultBitWidth);
507 APInt Max = APInt::getMaxValue(BW).zextOrSelf(ResultBitWidth);
Craig Topperb7920252017-04-29 06:40:47 +0000508 return ConstantRange(std::move(Min), std::move(Max));
Philip Reames4d00af12016-12-01 20:08:47 +0000509 }
510 case Instruction::SIToFP: {
511 // TODO: use input range if available
512 auto BW = getBitWidth();
513 APInt SMin = APInt::getSignedMinValue(BW).sextOrSelf(ResultBitWidth);
514 APInt SMax = APInt::getSignedMaxValue(BW).sextOrSelf(ResultBitWidth);
Craig Topperb7920252017-04-29 06:40:47 +0000515 return ConstantRange(std::move(SMin), std::move(SMax));
Philip Reames4d00af12016-12-01 20:08:47 +0000516 }
517 case Instruction::FPTrunc:
518 case Instruction::FPExt:
519 case Instruction::IntToPtr:
520 case Instruction::PtrToInt:
521 case Instruction::AddrSpaceCast:
522 // Conservatively return full set.
523 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
524 };
525}
526
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000527ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000528 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
529
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000530 unsigned SrcTySize = getBitWidth();
Reid Spencere1f3f192007-02-28 17:36:23 +0000531 assert(SrcTySize < DstTySize && "Not a value extension");
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000532 if (isFullSet() || isWrappedSet()) {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000533 // Change into [0, 1 << src bit width)
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000534 APInt LowerExt(DstTySize, 0);
535 if (!Upper) // special case: [X, 0) -- not really wrapping around
536 LowerExt = Lower.zext(DstTySize);
Craig Topperb7920252017-04-29 06:40:47 +0000537 return ConstantRange(std::move(LowerExt),
538 APInt::getOneBitSet(DstTySize, SrcTySize));
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000539 }
Chris Lattner55481f72004-03-30 00:20:08 +0000540
Jay Foad583abbc2010-12-07 08:25:19 +0000541 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
Chris Lattner55481f72004-03-30 00:20:08 +0000542}
543
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000544ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000545 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
546
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000547 unsigned SrcTySize = getBitWidth();
548 assert(SrcTySize < DstTySize && "Not a value extension");
Nuno Lopes1112eca2013-10-30 15:36:50 +0000549
550 // special case: [X, INT_MIN) -- not really wrapping around
Nuno Lopes14d4b0c2013-10-31 19:53:53 +0000551 if (Upper.isMinSignedValue())
Nuno Lopes1112eca2013-10-30 15:36:50 +0000552 return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize));
553
Nick Lewyckya35462d2010-09-06 23:52:49 +0000554 if (isFullSet() || isSignWrappedSet()) {
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000555 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
Nick Lewycky5edc4592009-07-13 04:17:23 +0000556 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000557 }
558
Jay Foad583abbc2010-12-07 08:25:19 +0000559 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000560}
561
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000562ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
Benjamin Kramer6709e052011-11-24 17:24:33 +0000563 assert(getBitWidth() > DstTySize && "Not a value truncation");
Nuno Lopesc14776d2012-07-19 16:27:45 +0000564 if (isEmptySet())
565 return ConstantRange(DstTySize, /*isFullSet=*/false);
566 if (isFullSet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000567 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattner55481f72004-03-30 00:20:08 +0000568
Craig Topper335597f2017-04-29 17:46:13 +0000569 APInt MaxValue = APInt::getLowBitsSet(getBitWidth(), DstTySize);
570 APInt MaxBitValue = APInt::getOneBitSet(getBitWidth(), DstTySize);
Nuno Lopesc14776d2012-07-19 16:27:45 +0000571
572 APInt LowerDiv(Lower), UpperDiv(Upper);
573 ConstantRange Union(DstTySize, /*isFullSet=*/false);
574
575 // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue]
576 // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and
577 // then we do the union with [MaxValue, Upper)
578 if (isWrappedSet()) {
Nick Lewyckybfad0152016-06-06 01:51:23 +0000579 // If Upper is greater than Max Value, it covers the whole truncated range.
Nuno Lopesc14776d2012-07-19 16:27:45 +0000580 if (Upper.uge(MaxValue))
581 return ConstantRange(DstTySize, /*isFullSet=*/true);
582
583 Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
Craig Topper86616532017-04-30 00:44:05 +0000584 UpperDiv.setAllBits();
Nuno Lopesc14776d2012-07-19 16:27:45 +0000585
586 // Union covers the MaxValue case, so return if the remaining range is just
587 // MaxValue.
588 if (LowerDiv == UpperDiv)
589 return Union;
590 }
591
592 // Chop off the most significant bits that are past the destination bitwidth.
593 if (LowerDiv.uge(MaxValue)) {
594 APInt Div(getBitWidth(), 0);
595 APInt::udivrem(LowerDiv, MaxBitValue, Div, LowerDiv);
Craig Topper685327d2017-04-29 17:46:11 +0000596 UpperDiv -= MaxBitValue * Div;
Nuno Lopesc14776d2012-07-19 16:27:45 +0000597 }
598
599 if (UpperDiv.ule(MaxValue))
600 return ConstantRange(LowerDiv.trunc(DstTySize),
601 UpperDiv.trunc(DstTySize)).unionWith(Union);
602
Nick Lewyckybfad0152016-06-06 01:51:23 +0000603 // The truncated value wraps around. Check if we can do better than fullset.
Craig Topper685327d2017-04-29 17:46:11 +0000604 UpperDiv -= MaxBitValue;
605 if (UpperDiv.ult(LowerDiv))
Nuno Lopesc14776d2012-07-19 16:27:45 +0000606 return ConstantRange(LowerDiv.trunc(DstTySize),
Craig Topper685327d2017-04-29 17:46:11 +0000607 UpperDiv.trunc(DstTySize)).unionWith(Union);
Nuno Lopesc14776d2012-07-19 16:27:45 +0000608
609 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattner55481f72004-03-30 00:20:08 +0000610}
611
Nuno Lopes640eb702009-11-09 15:36:28 +0000612ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
613 unsigned SrcTySize = getBitWidth();
614 if (SrcTySize > DstTySize)
615 return truncate(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000616 if (SrcTySize < DstTySize)
Nuno Lopes640eb702009-11-09 15:36:28 +0000617 return zeroExtend(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000618 return *this;
Nuno Lopes640eb702009-11-09 15:36:28 +0000619}
620
Nuno Lopes640eb702009-11-09 15:36:28 +0000621ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
622 unsigned SrcTySize = getBitWidth();
623 if (SrcTySize > DstTySize)
624 return truncate(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000625 if (SrcTySize < DstTySize)
Nuno Lopes640eb702009-11-09 15:36:28 +0000626 return signExtend(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000627 return *this;
Nuno Lopes640eb702009-11-09 15:36:28 +0000628}
629
Philip Reames4d00af12016-12-01 20:08:47 +0000630ConstantRange ConstantRange::binaryOp(Instruction::BinaryOps BinOp,
631 const ConstantRange &Other) const {
632 assert(BinOp >= Instruction::BinaryOpsBegin &&
633 BinOp < Instruction::BinaryOpsEnd && "Binary operators only!");
634
635 switch (BinOp) {
636 case Instruction::Add:
637 return add(Other);
638 case Instruction::Sub:
639 return sub(Other);
640 case Instruction::Mul:
641 return multiply(Other);
642 case Instruction::UDiv:
643 return udiv(Other);
644 case Instruction::Shl:
645 return shl(Other);
646 case Instruction::LShr:
647 return lshr(Other);
648 case Instruction::And:
649 return binaryAnd(Other);
650 case Instruction::Or:
651 return binaryOr(Other);
652 // Note: floating point operations applied to abstract ranges are just
653 // ideal integer operations with a lossy representation
654 case Instruction::FAdd:
655 return add(Other);
656 case Instruction::FSub:
657 return sub(Other);
658 case Instruction::FMul:
659 return multiply(Other);
660 default:
661 // Conservatively return full set.
662 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
663 }
664}
665
Dan Gohman5035fbf2009-07-09 22:07:27 +0000666ConstantRange
667ConstantRange::add(const ConstantRange &Other) const {
668 if (isEmptySet() || Other.isEmptySet())
669 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewycky73b704d2009-07-13 02:49:08 +0000670 if (isFullSet() || Other.isFullSet())
671 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000672
Dan Gohman5035fbf2009-07-09 22:07:27 +0000673 APInt NewLower = getLower() + Other.getLower();
674 APInt NewUpper = getUpper() + Other.getUpper() - 1;
675 if (NewLower == NewUpper)
676 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
677
Craig Topperb7920252017-04-29 06:40:47 +0000678 ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper));
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000679 if (X.isSizeStrictlySmallerThanOf(*this) ||
680 X.isSizeStrictlySmallerThanOf(Other))
Dan Gohman5035fbf2009-07-09 22:07:27 +0000681 // We've wrapped, therefore, full set.
682 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000683 return X;
Chris Lattnere0bb9eb2002-09-02 00:18:22 +0000684}
685
Artur Pilipenkoed841032016-10-19 14:44:23 +0000686ConstantRange ConstantRange::addWithNoSignedWrap(const APInt &Other) const {
687 // Calculate the subset of this range such that "X + Other" is
688 // guaranteed not to wrap (overflow) for all X in this subset.
689 // makeGuaranteedNoWrapRegion will produce an exact NSW range since we are
690 // passing a single element range.
691 auto NSWRange = ConstantRange::makeGuaranteedNoWrapRegion(BinaryOperator::Add,
692 ConstantRange(Other),
693 OverflowingBinaryOperator::NoSignedWrap);
694 auto NSWConstrainedRange = intersectWith(NSWRange);
695
696 return NSWConstrainedRange.add(ConstantRange(Other));
697}
698
Dan Gohman5035fbf2009-07-09 22:07:27 +0000699ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000700ConstantRange::sub(const ConstantRange &Other) const {
701 if (isEmptySet() || Other.isEmptySet())
702 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
703 if (isFullSet() || Other.isFullSet())
704 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
705
Nick Lewycky5dc6b792011-06-22 21:13:46 +0000706 APInt NewLower = getLower() - Other.getUpper() + 1;
707 APInt NewUpper = getUpper() - Other.getLower();
Nick Lewyckyd385c222010-08-11 22:04:36 +0000708 if (NewLower == NewUpper)
709 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
710
Craig Topperb7920252017-04-29 06:40:47 +0000711 ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper));
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000712 if (X.isSizeStrictlySmallerThanOf(*this) ||
713 X.isSizeStrictlySmallerThanOf(Other))
Nick Lewyckyd385c222010-08-11 22:04:36 +0000714 // We've wrapped, therefore, full set.
715 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Nick Lewyckyd385c222010-08-11 22:04:36 +0000716 return X;
717}
718
719ConstantRange
Dan Gohman5035fbf2009-07-09 22:07:27 +0000720ConstantRange::multiply(const ConstantRange &Other) const {
Dan Gohman3f8ed9e2010-01-26 15:56:18 +0000721 // TODO: If either operand is a single element and the multiply is known to
722 // be non-wrapping, round the result min and max value to the appropriate
723 // multiple of that element. If wrapping is possible, at least adjust the
724 // range according to the greatest power-of-two factor of the single element.
Dan Gohman5325efc2010-01-26 04:13:15 +0000725
Nick Lewycky2951c992009-07-12 02:19:05 +0000726 if (isEmptySet() || Other.isEmptySet())
727 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes986cc182012-07-16 20:47:16 +0000728
James Molloydcc78ec2015-03-06 15:50:47 +0000729 // Multiplication is signedness-independent. However different ranges can be
730 // obtained depending on how the input ranges are treated. These different
731 // ranges are all conservatively correct, but one might be better than the
732 // other. We calculate two ranges; one treating the inputs as unsigned
733 // and the other signed, then return the smallest of these ranges.
734
735 // Unsigned range first.
Nick Lewycky53023892009-07-13 03:27:41 +0000736 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
737 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
738 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
739 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
Nick Lewycky2951c992009-07-12 02:19:05 +0000740
Nick Lewycky53023892009-07-13 03:27:41 +0000741 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
742 this_max * Other_max + 1);
James Molloydcc78ec2015-03-06 15:50:47 +0000743 ConstantRange UR = Result_zext.truncate(getBitWidth());
744
Pete Cooper34a7a942016-05-27 17:06:50 +0000745 // If the unsigned range doesn't wrap, and isn't negative then it's a range
746 // from one positive number to another which is as good as we can generate.
747 // In this case, skip the extra work of generating signed ranges which aren't
748 // going to be better than this range.
749 if (!UR.isWrappedSet() && UR.getLower().isNonNegative())
750 return UR;
751
James Molloydcc78ec2015-03-06 15:50:47 +0000752 // Now the signed range. Because we could be dealing with negative numbers
753 // here, the lower bound is the smallest of the cartesian product of the
754 // lower and upper ranges; for example:
755 // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
756 // Similarly for the upper bound, swapping min for max.
757
758 this_min = getSignedMin().sext(getBitWidth() * 2);
759 this_max = getSignedMax().sext(getBitWidth() * 2);
760 Other_min = Other.getSignedMin().sext(getBitWidth() * 2);
761 Other_max = Other.getSignedMax().sext(getBitWidth() * 2);
762
763 auto L = {this_min * Other_min, this_min * Other_max,
764 this_max * Other_min, this_max * Other_max};
765 auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
766 ConstantRange Result_sext(std::min(L, Compare), std::max(L, Compare) + 1);
767 ConstantRange SR = Result_sext.truncate(getBitWidth());
768
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000769 return UR.isSizeStrictlySmallerThanOf(SR) ? UR : SR;
Dan Gohman5035fbf2009-07-09 22:07:27 +0000770}
771
772ConstantRange
773ConstantRange::smax(const ConstantRange &Other) const {
Dan Gohmandc33ae22009-07-09 23:16:10 +0000774 // X smax Y is: range(smax(X_smin, Y_smin),
775 // smax(X_smax, Y_smax))
776 if (isEmptySet() || Other.isEmptySet())
777 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohmandc33ae22009-07-09 23:16:10 +0000778 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
779 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
780 if (NewU == NewL)
781 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Craig Topperb7920252017-04-29 06:40:47 +0000782 return ConstantRange(std::move(NewL), std::move(NewU));
Dan Gohman5035fbf2009-07-09 22:07:27 +0000783}
784
785ConstantRange
786ConstantRange::umax(const ConstantRange &Other) const {
787 // X umax Y is: range(umax(X_umin, Y_umin),
788 // umax(X_umax, Y_umax))
789 if (isEmptySet() || Other.isEmptySet())
790 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000791 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
792 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
793 if (NewU == NewL)
794 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Craig Topperb7920252017-04-29 06:40:47 +0000795 return ConstantRange(std::move(NewL), std::move(NewU));
Dan Gohman5035fbf2009-07-09 22:07:27 +0000796}
797
798ConstantRange
Philip Reamesba313122016-02-26 22:08:18 +0000799ConstantRange::smin(const ConstantRange &Other) const {
800 // X smin Y is: range(smin(X_smin, Y_smin),
801 // smin(X_smax, Y_smax))
802 if (isEmptySet() || Other.isEmptySet())
803 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
804 APInt NewL = APIntOps::smin(getSignedMin(), Other.getSignedMin());
805 APInt NewU = APIntOps::smin(getSignedMax(), Other.getSignedMax()) + 1;
806 if (NewU == NewL)
807 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Craig Topperb7920252017-04-29 06:40:47 +0000808 return ConstantRange(std::move(NewL), std::move(NewU));
Philip Reamesba313122016-02-26 22:08:18 +0000809}
810
811ConstantRange
812ConstantRange::umin(const ConstantRange &Other) const {
813 // X umin Y is: range(umin(X_umin, Y_umin),
814 // umin(X_umax, Y_umax))
815 if (isEmptySet() || Other.isEmptySet())
816 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
817 APInt NewL = APIntOps::umin(getUnsignedMin(), Other.getUnsignedMin());
818 APInt NewU = APIntOps::umin(getUnsignedMax(), Other.getUnsignedMax()) + 1;
819 if (NewU == NewL)
820 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Craig Topperb7920252017-04-29 06:40:47 +0000821 return ConstantRange(std::move(NewL), std::move(NewU));
Philip Reamesba313122016-02-26 22:08:18 +0000822}
823
824ConstantRange
Nick Lewyckyf1b8cb32009-07-12 05:18:18 +0000825ConstantRange::udiv(const ConstantRange &RHS) const {
826 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
827 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
828 if (RHS.isFullSet())
829 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
830
831 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
832
833 APInt RHS_umin = RHS.getUnsignedMin();
834 if (RHS_umin == 0) {
835 // We want the lowest value in RHS excluding zero. Usually that would be 1
836 // except for a range in the form of [X, 1) in which case it would be X.
837 if (RHS.getUpper() == 1)
838 RHS_umin = RHS.getLower();
839 else
Craig Topper86616532017-04-30 00:44:05 +0000840 RHS_umin = 1;
Nick Lewyckyf1b8cb32009-07-12 05:18:18 +0000841 }
842
843 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
844
845 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
846 // this could occur.
847 if (Lower == Upper)
848 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
849
Craig Topperb7920252017-04-29 06:40:47 +0000850 return ConstantRange(std::move(Lower), std::move(Upper));
Dan Gohman5035fbf2009-07-09 22:07:27 +0000851}
852
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000853ConstantRange
Nick Lewyckyad48e012010-09-07 05:39:02 +0000854ConstantRange::binaryAnd(const ConstantRange &Other) const {
855 if (isEmptySet() || Other.isEmptySet())
856 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
857
858 // TODO: replace this with something less conservative
859
860 APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax());
861 if (umin.isAllOnesValue())
862 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Craig Topperb7920252017-04-29 06:40:47 +0000863 return ConstantRange(APInt::getNullValue(getBitWidth()), std::move(umin) + 1);
Nick Lewyckyad48e012010-09-07 05:39:02 +0000864}
865
866ConstantRange
867ConstantRange::binaryOr(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 umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
Craig Toppere8dea1b2017-04-28 21:48:09 +0000874 if (umax.isNullValue())
Nick Lewyckyad48e012010-09-07 05:39:02 +0000875 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Craig Topperb7920252017-04-29 06:40:47 +0000876 return ConstantRange(std::move(umax), APInt::getNullValue(getBitWidth()));
Nick Lewyckyad48e012010-09-07 05:39:02 +0000877}
878
879ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000880ConstantRange::shl(const ConstantRange &Other) const {
881 if (isEmptySet() || Other.isEmptySet())
882 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000883
Nick Lewyckyd385c222010-08-11 22:04:36 +0000884 APInt min = getUnsignedMin().shl(Other.getUnsignedMin());
885 APInt max = getUnsignedMax().shl(Other.getUnsignedMax());
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000886
887 // there's no overflow!
Nuno Lopesf8fcac72009-11-12 15:10:33 +0000888 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
Nick Lewyckyd385c222010-08-11 22:04:36 +0000889 if (Zeros.ugt(Other.getUnsignedMax()))
Craig Topperb7920252017-04-29 06:40:47 +0000890 return ConstantRange(std::move(min), std::move(max) + 1);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000891
892 // FIXME: implement the other tricky cases
Nick Lewyckyd385c222010-08-11 22:04:36 +0000893 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000894}
895
896ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000897ConstantRange::lshr(const ConstantRange &Other) const {
898 if (isEmptySet() || Other.isEmptySet())
899 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000900
Nick Lewyckyd385c222010-08-11 22:04:36 +0000901 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin());
902 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
903 if (min == max + 1)
904 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
905
Craig Topperb7920252017-04-29 06:40:47 +0000906 return ConstantRange(std::move(min), std::move(max) + 1);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000907}
908
Owen Anderson1a9078b2010-08-07 05:47:46 +0000909ConstantRange ConstantRange::inverse() const {
Nick Lewycky228f5b42012-01-03 20:33:00 +0000910 if (isFullSet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000911 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000912 if (isEmptySet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000913 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Owen Anderson1a9078b2010-08-07 05:47:46 +0000914 return ConstantRange(Upper, Lower);
915}
916
Dan Gohmandc33ae22009-07-09 23:16:10 +0000917void ConstantRange::print(raw_ostream &OS) const {
Dan Gohman837ada72010-01-26 04:12:55 +0000918 if (isFullSet())
919 OS << "full-set";
920 else if (isEmptySet())
921 OS << "empty-set";
922 else
923 OS << "[" << Lower << "," << Upper << ")";
Dan Gohman5035fbf2009-07-09 22:07:27 +0000924}
925
Matthias Braun8c209aa2017-01-28 02:02:38 +0000926#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000927LLVM_DUMP_METHOD void ConstantRange::dump() const {
David Greenede7b3532010-01-05 01:28:32 +0000928 print(dbgs());
Dan Gohman5035fbf2009-07-09 22:07:27 +0000929}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000930#endif
Peter Collingbourneecdd58f2016-10-21 19:59:26 +0000931
932ConstantRange llvm::getConstantRangeFromMetadata(const MDNode &Ranges) {
933 const unsigned NumRanges = Ranges.getNumOperands() / 2;
934 assert(NumRanges >= 1 && "Must have at least one range!");
935 assert(Ranges.getNumOperands() % 2 == 0 && "Must be a sequence of pairs");
936
937 auto *FirstLow = mdconst::extract<ConstantInt>(Ranges.getOperand(0));
938 auto *FirstHigh = mdconst::extract<ConstantInt>(Ranges.getOperand(1));
939
940 ConstantRange CR(FirstLow->getValue(), FirstHigh->getValue());
941
942 for (unsigned i = 1; i < NumRanges; ++i) {
943 auto *Low = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
944 auto *High = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
945
946 // Note: unionWith will potentially create a range that contains values not
947 // contained in any of the original N ranges.
948 CR = CR.unionWith(ConstantRange(Low->getValue(), High->getValue()));
949 }
950
951 return CR;
952}