blob: 754844e371a8872e87d883b09083bc26b70727ca [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 {
Nuno Lopes216d5712012-07-17 15:43:59 +0000246 if (isFullSet()) {
247 APInt Size(getBitWidth()+1, 0);
248 Size.setBit(getBitWidth());
249 return Size;
Chris Lattner113f2ae2002-09-01 23:53:36 +0000250 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000251
Nuno Lopes216d5712012-07-17 15:43:59 +0000252 // This is also correct for wrapped sets.
Nuno Lopes99504c52012-07-16 18:08:12 +0000253 return (Upper - Lower).zext(getBitWidth()+1);
Chris Lattner113f2ae2002-09-01 23:53:36 +0000254}
255
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000256bool
257ConstantRange::isSizeStrictlySmallerThanOf(const ConstantRange &Other) const {
258 assert(getBitWidth() == Other.getBitWidth());
259 if (isFullSet())
260 return false;
261 if (Other.isFullSet())
262 return true;
263 return (Upper - Lower).ult(Other.Upper - Other.Lower);
264}
265
Nick Lewyckye4559372007-03-10 15:54:12 +0000266APInt ConstantRange::getUnsignedMax() const {
267 if (isFullSet() || isWrappedSet())
268 return APInt::getMaxValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000269 return getUpper() - 1;
Nick Lewyckye4559372007-03-10 15:54:12 +0000270}
271
Nick Lewyckye4559372007-03-10 15:54:12 +0000272APInt ConstantRange::getUnsignedMin() const {
273 if (isFullSet() || (isWrappedSet() && getUpper() != 0))
274 return APInt::getMinValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000275 return getLower();
Nick Lewyckye4559372007-03-10 15:54:12 +0000276}
277
Nick Lewyckye4559372007-03-10 15:54:12 +0000278APInt ConstantRange::getSignedMax() const {
Nick Lewyckye4559372007-03-10 15:54:12 +0000279 if (!isWrappedSet()) {
Craig Topper88c64f32017-04-18 23:02:39 +0000280 APInt UpperMinusOne = getUpper() - 1;
281 if (getLower().sle(UpperMinusOne))
282 return UpperMinusOne;
283 return APInt::getSignedMaxValue(getBitWidth());
Nick Lewyckye4559372007-03-10 15:54:12 +0000284 }
Nick Lewycky228f5b42012-01-03 20:33:00 +0000285 if (getLower().isNegative() == getUpper().isNegative())
Craig Topper88c64f32017-04-18 23:02:39 +0000286 return APInt::getSignedMaxValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000287 return getUpper() - 1;
Nick Lewyckye4559372007-03-10 15:54:12 +0000288}
289
Nick Lewyckye4559372007-03-10 15:54:12 +0000290APInt ConstantRange::getSignedMin() const {
Nick Lewyckye4559372007-03-10 15:54:12 +0000291 if (!isWrappedSet()) {
Nick Lewyckyd18b1602007-06-09 04:20:33 +0000292 if (getLower().sle(getUpper() - 1))
Nick Lewyckye4559372007-03-10 15:54:12 +0000293 return getLower();
Craig Topper88c64f32017-04-18 23:02:39 +0000294 return APInt::getSignedMinValue(getBitWidth());
Nick Lewyckye4559372007-03-10 15:54:12 +0000295 }
Nick Lewycky228f5b42012-01-03 20:33:00 +0000296 if ((getUpper() - 1).slt(getLower())) {
Craig Topper88c64f32017-04-18 23:02:39 +0000297 if (!getUpper().isMinSignedValue())
298 return APInt::getSignedMinValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000299 }
300 return getLower();
Nick Lewyckye4559372007-03-10 15:54:12 +0000301}
302
Reid Spencer6a440332007-03-01 07:54:15 +0000303bool ConstantRange::contains(const APInt &V) const {
304 if (Lower == Upper)
305 return isFullSet();
Chris Lattner113f2ae2002-09-01 23:53:36 +0000306
Reid Spencer6a440332007-03-01 07:54:15 +0000307 if (!isWrappedSet())
308 return Lower.ule(V) && V.ult(Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000309 return Lower.ule(V) || V.ult(Upper);
Chris Lattner55481f72004-03-30 00:20:08 +0000310}
311
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000312bool ConstantRange::contains(const ConstantRange &Other) const {
Nick Lewyckyd385c222010-08-11 22:04:36 +0000313 if (isFullSet() || Other.isEmptySet()) return true;
314 if (isEmptySet() || Other.isFullSet()) return false;
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000315
316 if (!isWrappedSet()) {
317 if (Other.isWrappedSet())
318 return false;
319
320 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
321 }
322
323 if (!Other.isWrappedSet())
324 return Other.getUpper().ule(Upper) ||
325 Lower.ule(Other.getLower());
326
327 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
328}
329
Reid Spencer3a7e9d82007-02-28 19:57:34 +0000330ConstantRange ConstantRange::subtract(const APInt &Val) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000331 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
Chris Lattner55481f72004-03-30 00:20:08 +0000332 // If the set is empty or full, don't modify the endpoints.
Reid Spencere1f3f192007-02-28 17:36:23 +0000333 if (Lower == Upper)
334 return *this;
Reid Spencere1f3f192007-02-28 17:36:23 +0000335 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattner55481f72004-03-30 00:20:08 +0000336}
Chris Lattner113f2ae2002-09-01 23:53:36 +0000337
Nuno Lopes5020db22012-06-28 16:10:13 +0000338ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
339 return intersectWith(CR.inverse());
340}
341
Reid Spencer6a440332007-03-01 07:54:15 +0000342ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000343 assert(getBitWidth() == CR.getBitWidth() &&
344 "ConstantRange types don't agree!");
Nick Lewycky61b4a262007-07-14 02:51:34 +0000345
346 // Handle common cases.
347 if ( isEmptySet() || CR.isFullSet()) return *this;
348 if (CR.isEmptySet() || isFullSet()) return CR;
349
350 if (!isWrappedSet() && CR.isWrappedSet())
Nick Lewycky0d139032009-07-18 06:34:42 +0000351 return CR.intersectWith(*this);
Nick Lewycky61b4a262007-07-14 02:51:34 +0000352
353 if (!isWrappedSet() && !CR.isWrappedSet()) {
354 if (Lower.ult(CR.Lower)) {
355 if (Upper.ule(CR.Lower))
356 return ConstantRange(getBitWidth(), false);
357
358 if (Upper.ult(CR.Upper))
359 return ConstantRange(CR.Lower, Upper);
360
361 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000362 }
Nick Lewycky228f5b42012-01-03 20:33:00 +0000363 if (Upper.ult(CR.Upper))
364 return *this;
365
366 if (Lower.ult(CR.Upper))
367 return ConstantRange(Lower, CR.Upper);
368
369 return ConstantRange(getBitWidth(), false);
Nick Lewycky61b4a262007-07-14 02:51:34 +0000370 }
371
372 if (isWrappedSet() && !CR.isWrappedSet()) {
373 if (CR.Lower.ult(Upper)) {
374 if (CR.Upper.ult(Upper))
375 return CR;
376
Nuno Lopes63afc082012-05-18 00:14:36 +0000377 if (CR.Upper.ule(Lower))
Nick Lewycky61b4a262007-07-14 02:51:34 +0000378 return ConstantRange(CR.Lower, Upper);
379
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000380 if (isSizeStrictlySmallerThanOf(CR))
Nick Lewycky61b4a262007-07-14 02:51:34 +0000381 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000382 return CR;
383 }
384 if (CR.Lower.ult(Lower)) {
Nick Lewycky61b4a262007-07-14 02:51:34 +0000385 if (CR.Upper.ule(Lower))
386 return ConstantRange(getBitWidth(), false);
387
388 return ConstantRange(Lower, CR.Upper);
389 }
390 return CR;
391 }
392
393 if (CR.Upper.ult(Upper)) {
394 if (CR.Lower.ult(Upper)) {
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000395 if (isSizeStrictlySmallerThanOf(CR))
Nick Lewycky61b4a262007-07-14 02:51:34 +0000396 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000397 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000398 }
399
400 if (CR.Lower.ult(Lower))
401 return ConstantRange(Lower, CR.Upper);
402
403 return CR;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000404 }
Nuno Lopesebb0c942012-06-28 00:59:33 +0000405 if (CR.Upper.ule(Lower)) {
Nick Lewycky61b4a262007-07-14 02:51:34 +0000406 if (CR.Lower.ult(Lower))
407 return *this;
408
409 return ConstantRange(CR.Lower, Upper);
410 }
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000411 if (isSizeStrictlySmallerThanOf(CR))
Nick Lewycky61b4a262007-07-14 02:51:34 +0000412 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000413 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000414}
415
Reid Spencer6a440332007-03-01 07:54:15 +0000416ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000417 assert(getBitWidth() == CR.getBitWidth() &&
418 "ConstantRange types don't agree!");
Chris Lattner113f2ae2002-09-01 23:53:36 +0000419
Nick Lewyckycf87f9e2007-03-02 03:33:05 +0000420 if ( isFullSet() || CR.isEmptySet()) return *this;
421 if (CR.isFullSet() || isEmptySet()) return CR;
Chris Lattner113f2ae2002-09-01 23:53:36 +0000422
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000423 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
424
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000425 if (!isWrappedSet() && !CR.isWrappedSet()) {
Nick Lewycky567daf32009-07-19 03:44:35 +0000426 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
427 // If the two ranges are disjoint, find the smaller gap and bridge it.
428 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
429 if (d1.ult(d2))
430 return ConstantRange(Lower, CR.Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000431 return ConstantRange(CR.Lower, Upper);
Nick Lewycky567daf32009-07-19 03:44:35 +0000432 }
433
Craig Topper8fb5a142017-04-29 07:24:13 +0000434 APInt L = CR.Lower.ult(Lower) ? CR.Lower : Lower;
435 APInt U = (CR.Upper - 1).ugt(Upper - 1) ? CR.Upper : Upper;
Nick Lewycky567daf32009-07-19 03:44:35 +0000436
437 if (L == 0 && U == 0)
438 return ConstantRange(getBitWidth());
439
Craig Topperb7920252017-04-29 06:40:47 +0000440 return ConstantRange(std::move(L), std::move(U));
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000441 }
442
Nick Lewycky567daf32009-07-19 03:44:35 +0000443 if (!CR.isWrappedSet()) {
444 // ------U L----- and ------U L----- : this
445 // L--U L--U : CR
446 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000447 return *this;
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000448
Nick Lewycky567daf32009-07-19 03:44:35 +0000449 // ------U L----- : this
450 // L---------U : CR
451 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000452 return ConstantRange(getBitWidth());
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000453
Nick Lewycky567daf32009-07-19 03:44:35 +0000454 // ----U L---- : this
455 // L---U : CR
456 // <d1> <d2>
457 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000458 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
Nick Lewycky567daf32009-07-19 03:44:35 +0000459 if (d1.ult(d2))
460 return ConstantRange(Lower, CR.Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000461 return ConstantRange(CR.Lower, Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000462 }
463
Nick Lewycky567daf32009-07-19 03:44:35 +0000464 // ----U L----- : this
465 // L----U : CR
466 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
467 return ConstantRange(CR.Lower, Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000468
Nick Lewycky567daf32009-07-19 03:44:35 +0000469 // ------U L---- : this
470 // L-----U : CR
Nick Lewycky228f5b42012-01-03 20:33:00 +0000471 assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) &&
472 "ConstantRange::unionWith missed a case with one range wrapped");
473 return ConstantRange(Lower, CR.Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000474 }
475
Nick Lewycky567daf32009-07-19 03:44:35 +0000476 // ------U L---- and ------U L---- : this
477 // -U L----------- and ------------U L : CR
478 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
479 return ConstantRange(getBitWidth());
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000480
Craig Topper8fb5a142017-04-29 07:24:13 +0000481 APInt L = CR.Lower.ult(Lower) ? CR.Lower : Lower;
482 APInt U = CR.Upper.ugt(Upper) ? CR.Upper : Upper;
Nick Lewyckycf87f9e2007-03-02 03:33:05 +0000483
Craig Topperb7920252017-04-29 06:40:47 +0000484 return ConstantRange(std::move(L), std::move(U));
Chris Lattner113f2ae2002-09-01 23:53:36 +0000485}
Chris Lattnere0bb9eb2002-09-02 00:18:22 +0000486
Philip Reames4d00af12016-12-01 20:08:47 +0000487ConstantRange ConstantRange::castOp(Instruction::CastOps CastOp,
488 uint32_t ResultBitWidth) const {
489 switch (CastOp) {
490 default:
491 llvm_unreachable("unsupported cast type");
492 case Instruction::Trunc:
493 return truncate(ResultBitWidth);
494 case Instruction::SExt:
495 return signExtend(ResultBitWidth);
496 case Instruction::ZExt:
497 return zeroExtend(ResultBitWidth);
498 case Instruction::BitCast:
499 return *this;
500 case Instruction::FPToUI:
501 case Instruction::FPToSI:
502 if (getBitWidth() == ResultBitWidth)
503 return *this;
504 else
505 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
506 case Instruction::UIToFP: {
507 // TODO: use input range if available
508 auto BW = getBitWidth();
509 APInt Min = APInt::getMinValue(BW).zextOrSelf(ResultBitWidth);
510 APInt Max = APInt::getMaxValue(BW).zextOrSelf(ResultBitWidth);
Craig Topperb7920252017-04-29 06:40:47 +0000511 return ConstantRange(std::move(Min), std::move(Max));
Philip Reames4d00af12016-12-01 20:08:47 +0000512 }
513 case Instruction::SIToFP: {
514 // TODO: use input range if available
515 auto BW = getBitWidth();
516 APInt SMin = APInt::getSignedMinValue(BW).sextOrSelf(ResultBitWidth);
517 APInt SMax = APInt::getSignedMaxValue(BW).sextOrSelf(ResultBitWidth);
Craig Topperb7920252017-04-29 06:40:47 +0000518 return ConstantRange(std::move(SMin), std::move(SMax));
Philip Reames4d00af12016-12-01 20:08:47 +0000519 }
520 case Instruction::FPTrunc:
521 case Instruction::FPExt:
522 case Instruction::IntToPtr:
523 case Instruction::PtrToInt:
524 case Instruction::AddrSpaceCast:
525 // Conservatively return full set.
526 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
527 };
528}
529
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000530ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000531 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
532
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000533 unsigned SrcTySize = getBitWidth();
Reid Spencere1f3f192007-02-28 17:36:23 +0000534 assert(SrcTySize < DstTySize && "Not a value extension");
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000535 if (isFullSet() || isWrappedSet()) {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000536 // Change into [0, 1 << src bit width)
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000537 APInt LowerExt(DstTySize, 0);
538 if (!Upper) // special case: [X, 0) -- not really wrapping around
539 LowerExt = Lower.zext(DstTySize);
Craig Topperb7920252017-04-29 06:40:47 +0000540 return ConstantRange(std::move(LowerExt),
541 APInt::getOneBitSet(DstTySize, SrcTySize));
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000542 }
Chris Lattner55481f72004-03-30 00:20:08 +0000543
Jay Foad583abbc2010-12-07 08:25:19 +0000544 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
Chris Lattner55481f72004-03-30 00:20:08 +0000545}
546
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000547ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000548 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
549
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000550 unsigned SrcTySize = getBitWidth();
551 assert(SrcTySize < DstTySize && "Not a value extension");
Nuno Lopes1112eca2013-10-30 15:36:50 +0000552
553 // special case: [X, INT_MIN) -- not really wrapping around
Nuno Lopes14d4b0c2013-10-31 19:53:53 +0000554 if (Upper.isMinSignedValue())
Nuno Lopes1112eca2013-10-30 15:36:50 +0000555 return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize));
556
Nick Lewyckya35462d2010-09-06 23:52:49 +0000557 if (isFullSet() || isSignWrappedSet()) {
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000558 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
Nick Lewycky5edc4592009-07-13 04:17:23 +0000559 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000560 }
561
Jay Foad583abbc2010-12-07 08:25:19 +0000562 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000563}
564
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000565ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
Benjamin Kramer6709e052011-11-24 17:24:33 +0000566 assert(getBitWidth() > DstTySize && "Not a value truncation");
Nuno Lopesc14776d2012-07-19 16:27:45 +0000567 if (isEmptySet())
568 return ConstantRange(DstTySize, /*isFullSet=*/false);
569 if (isFullSet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000570 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattner55481f72004-03-30 00:20:08 +0000571
Nuno Lopesc14776d2012-07-19 16:27:45 +0000572 APInt MaxValue = APInt::getMaxValue(DstTySize).zext(getBitWidth());
573 APInt MaxBitValue(getBitWidth(), 0);
574 MaxBitValue.setBit(DstTySize);
575
576 APInt LowerDiv(Lower), UpperDiv(Upper);
577 ConstantRange Union(DstTySize, /*isFullSet=*/false);
578
579 // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue]
580 // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and
581 // then we do the union with [MaxValue, Upper)
582 if (isWrappedSet()) {
Nick Lewyckybfad0152016-06-06 01:51:23 +0000583 // If Upper is greater than Max Value, it covers the whole truncated range.
Nuno Lopesc14776d2012-07-19 16:27:45 +0000584 if (Upper.uge(MaxValue))
585 return ConstantRange(DstTySize, /*isFullSet=*/true);
586
587 Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
588 UpperDiv = APInt::getMaxValue(getBitWidth());
589
590 // Union covers the MaxValue case, so return if the remaining range is just
591 // MaxValue.
592 if (LowerDiv == UpperDiv)
593 return Union;
594 }
595
596 // Chop off the most significant bits that are past the destination bitwidth.
597 if (LowerDiv.uge(MaxValue)) {
598 APInt Div(getBitWidth(), 0);
599 APInt::udivrem(LowerDiv, MaxBitValue, Div, LowerDiv);
Craig Topper685327d2017-04-29 17:46:11 +0000600 UpperDiv -= MaxBitValue * Div;
Nuno Lopesc14776d2012-07-19 16:27:45 +0000601 }
602
603 if (UpperDiv.ule(MaxValue))
604 return ConstantRange(LowerDiv.trunc(DstTySize),
605 UpperDiv.trunc(DstTySize)).unionWith(Union);
606
Nick Lewyckybfad0152016-06-06 01:51:23 +0000607 // The truncated value wraps around. Check if we can do better than fullset.
Craig Topper685327d2017-04-29 17:46:11 +0000608 UpperDiv -= MaxBitValue;
609 if (UpperDiv.ult(LowerDiv))
Nuno Lopesc14776d2012-07-19 16:27:45 +0000610 return ConstantRange(LowerDiv.trunc(DstTySize),
Craig Topper685327d2017-04-29 17:46:11 +0000611 UpperDiv.trunc(DstTySize)).unionWith(Union);
Nuno Lopesc14776d2012-07-19 16:27:45 +0000612
613 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattner55481f72004-03-30 00:20:08 +0000614}
615
Nuno Lopes640eb702009-11-09 15:36:28 +0000616ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
617 unsigned SrcTySize = getBitWidth();
618 if (SrcTySize > DstTySize)
619 return truncate(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000620 if (SrcTySize < DstTySize)
Nuno Lopes640eb702009-11-09 15:36:28 +0000621 return zeroExtend(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000622 return *this;
Nuno Lopes640eb702009-11-09 15:36:28 +0000623}
624
Nuno Lopes640eb702009-11-09 15:36:28 +0000625ConstantRange ConstantRange::sextOrTrunc(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 signExtend(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000631 return *this;
Nuno Lopes640eb702009-11-09 15:36:28 +0000632}
633
Philip Reames4d00af12016-12-01 20:08:47 +0000634ConstantRange ConstantRange::binaryOp(Instruction::BinaryOps BinOp,
635 const ConstantRange &Other) const {
636 assert(BinOp >= Instruction::BinaryOpsBegin &&
637 BinOp < Instruction::BinaryOpsEnd && "Binary operators only!");
638
639 switch (BinOp) {
640 case Instruction::Add:
641 return add(Other);
642 case Instruction::Sub:
643 return sub(Other);
644 case Instruction::Mul:
645 return multiply(Other);
646 case Instruction::UDiv:
647 return udiv(Other);
648 case Instruction::Shl:
649 return shl(Other);
650 case Instruction::LShr:
651 return lshr(Other);
652 case Instruction::And:
653 return binaryAnd(Other);
654 case Instruction::Or:
655 return binaryOr(Other);
656 // Note: floating point operations applied to abstract ranges are just
657 // ideal integer operations with a lossy representation
658 case Instruction::FAdd:
659 return add(Other);
660 case Instruction::FSub:
661 return sub(Other);
662 case Instruction::FMul:
663 return multiply(Other);
664 default:
665 // Conservatively return full set.
666 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
667 }
668}
669
Dan Gohman5035fbf2009-07-09 22:07:27 +0000670ConstantRange
671ConstantRange::add(const ConstantRange &Other) const {
672 if (isEmptySet() || Other.isEmptySet())
673 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewycky73b704d2009-07-13 02:49:08 +0000674 if (isFullSet() || Other.isFullSet())
675 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000676
Dan Gohman5035fbf2009-07-09 22:07:27 +0000677 APInt NewLower = getLower() + Other.getLower();
678 APInt NewUpper = getUpper() + Other.getUpper() - 1;
679 if (NewLower == NewUpper)
680 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
681
Craig Topperb7920252017-04-29 06:40:47 +0000682 ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper));
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000683 if (X.isSizeStrictlySmallerThanOf(*this) ||
684 X.isSizeStrictlySmallerThanOf(Other))
Dan Gohman5035fbf2009-07-09 22:07:27 +0000685 // We've wrapped, therefore, full set.
686 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000687 return X;
Chris Lattnere0bb9eb2002-09-02 00:18:22 +0000688}
689
Artur Pilipenkoed841032016-10-19 14:44:23 +0000690ConstantRange ConstantRange::addWithNoSignedWrap(const APInt &Other) const {
691 // Calculate the subset of this range such that "X + Other" is
692 // guaranteed not to wrap (overflow) for all X in this subset.
693 // makeGuaranteedNoWrapRegion will produce an exact NSW range since we are
694 // passing a single element range.
695 auto NSWRange = ConstantRange::makeGuaranteedNoWrapRegion(BinaryOperator::Add,
696 ConstantRange(Other),
697 OverflowingBinaryOperator::NoSignedWrap);
698 auto NSWConstrainedRange = intersectWith(NSWRange);
699
700 return NSWConstrainedRange.add(ConstantRange(Other));
701}
702
Dan Gohman5035fbf2009-07-09 22:07:27 +0000703ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000704ConstantRange::sub(const ConstantRange &Other) const {
705 if (isEmptySet() || Other.isEmptySet())
706 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
707 if (isFullSet() || Other.isFullSet())
708 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
709
Nick Lewycky5dc6b792011-06-22 21:13:46 +0000710 APInt NewLower = getLower() - Other.getUpper() + 1;
711 APInt NewUpper = getUpper() - Other.getLower();
Nick Lewyckyd385c222010-08-11 22:04:36 +0000712 if (NewLower == NewUpper)
713 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
714
Craig Topperb7920252017-04-29 06:40:47 +0000715 ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper));
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000716 if (X.isSizeStrictlySmallerThanOf(*this) ||
717 X.isSizeStrictlySmallerThanOf(Other))
Nick Lewyckyd385c222010-08-11 22:04:36 +0000718 // We've wrapped, therefore, full set.
719 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Nick Lewyckyd385c222010-08-11 22:04:36 +0000720 return X;
721}
722
723ConstantRange
Dan Gohman5035fbf2009-07-09 22:07:27 +0000724ConstantRange::multiply(const ConstantRange &Other) const {
Dan Gohman3f8ed9e2010-01-26 15:56:18 +0000725 // TODO: If either operand is a single element and the multiply is known to
726 // be non-wrapping, round the result min and max value to the appropriate
727 // multiple of that element. If wrapping is possible, at least adjust the
728 // range according to the greatest power-of-two factor of the single element.
Dan Gohman5325efc2010-01-26 04:13:15 +0000729
Nick Lewycky2951c992009-07-12 02:19:05 +0000730 if (isEmptySet() || Other.isEmptySet())
731 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes986cc182012-07-16 20:47:16 +0000732
James Molloydcc78ec2015-03-06 15:50:47 +0000733 // Multiplication is signedness-independent. However different ranges can be
734 // obtained depending on how the input ranges are treated. These different
735 // ranges are all conservatively correct, but one might be better than the
736 // other. We calculate two ranges; one treating the inputs as unsigned
737 // and the other signed, then return the smallest of these ranges.
738
739 // Unsigned range first.
Nick Lewycky53023892009-07-13 03:27:41 +0000740 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
741 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
742 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
743 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
Nick Lewycky2951c992009-07-12 02:19:05 +0000744
Nick Lewycky53023892009-07-13 03:27:41 +0000745 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
746 this_max * Other_max + 1);
James Molloydcc78ec2015-03-06 15:50:47 +0000747 ConstantRange UR = Result_zext.truncate(getBitWidth());
748
Pete Cooper34a7a942016-05-27 17:06:50 +0000749 // If the unsigned range doesn't wrap, and isn't negative then it's a range
750 // from one positive number to another which is as good as we can generate.
751 // In this case, skip the extra work of generating signed ranges which aren't
752 // going to be better than this range.
753 if (!UR.isWrappedSet() && UR.getLower().isNonNegative())
754 return UR;
755
James Molloydcc78ec2015-03-06 15:50:47 +0000756 // Now the signed range. Because we could be dealing with negative numbers
757 // here, the lower bound is the smallest of the cartesian product of the
758 // lower and upper ranges; for example:
759 // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
760 // Similarly for the upper bound, swapping min for max.
761
762 this_min = getSignedMin().sext(getBitWidth() * 2);
763 this_max = getSignedMax().sext(getBitWidth() * 2);
764 Other_min = Other.getSignedMin().sext(getBitWidth() * 2);
765 Other_max = Other.getSignedMax().sext(getBitWidth() * 2);
766
767 auto L = {this_min * Other_min, this_min * Other_max,
768 this_max * Other_min, this_max * Other_max};
769 auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
770 ConstantRange Result_sext(std::min(L, Compare), std::max(L, Compare) + 1);
771 ConstantRange SR = Result_sext.truncate(getBitWidth());
772
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000773 return UR.isSizeStrictlySmallerThanOf(SR) ? UR : SR;
Dan Gohman5035fbf2009-07-09 22:07:27 +0000774}
775
776ConstantRange
777ConstantRange::smax(const ConstantRange &Other) const {
Dan Gohmandc33ae22009-07-09 23:16:10 +0000778 // X smax Y is: range(smax(X_smin, Y_smin),
779 // smax(X_smax, Y_smax))
780 if (isEmptySet() || Other.isEmptySet())
781 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohmandc33ae22009-07-09 23:16:10 +0000782 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
783 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
784 if (NewU == NewL)
785 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Craig Topperb7920252017-04-29 06:40:47 +0000786 return ConstantRange(std::move(NewL), std::move(NewU));
Dan Gohman5035fbf2009-07-09 22:07:27 +0000787}
788
789ConstantRange
790ConstantRange::umax(const ConstantRange &Other) const {
791 // X umax Y is: range(umax(X_umin, Y_umin),
792 // umax(X_umax, Y_umax))
793 if (isEmptySet() || Other.isEmptySet())
794 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000795 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
796 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
797 if (NewU == NewL)
798 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Craig Topperb7920252017-04-29 06:40:47 +0000799 return ConstantRange(std::move(NewL), std::move(NewU));
Dan Gohman5035fbf2009-07-09 22:07:27 +0000800}
801
802ConstantRange
Philip Reamesba313122016-02-26 22:08:18 +0000803ConstantRange::smin(const ConstantRange &Other) const {
804 // X smin Y is: range(smin(X_smin, Y_smin),
805 // smin(X_smax, Y_smax))
806 if (isEmptySet() || Other.isEmptySet())
807 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
808 APInt NewL = APIntOps::smin(getSignedMin(), Other.getSignedMin());
809 APInt NewU = APIntOps::smin(getSignedMax(), Other.getSignedMax()) + 1;
810 if (NewU == NewL)
811 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Craig Topperb7920252017-04-29 06:40:47 +0000812 return ConstantRange(std::move(NewL), std::move(NewU));
Philip Reamesba313122016-02-26 22:08:18 +0000813}
814
815ConstantRange
816ConstantRange::umin(const ConstantRange &Other) const {
817 // X umin Y is: range(umin(X_umin, Y_umin),
818 // umin(X_umax, Y_umax))
819 if (isEmptySet() || Other.isEmptySet())
820 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
821 APInt NewL = APIntOps::umin(getUnsignedMin(), Other.getUnsignedMin());
822 APInt NewU = APIntOps::umin(getUnsignedMax(), Other.getUnsignedMax()) + 1;
823 if (NewU == NewL)
824 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Craig Topperb7920252017-04-29 06:40:47 +0000825 return ConstantRange(std::move(NewL), std::move(NewU));
Philip Reamesba313122016-02-26 22:08:18 +0000826}
827
828ConstantRange
Nick Lewyckyf1b8cb32009-07-12 05:18:18 +0000829ConstantRange::udiv(const ConstantRange &RHS) const {
830 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
831 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
832 if (RHS.isFullSet())
833 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
834
835 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
836
837 APInt RHS_umin = RHS.getUnsignedMin();
838 if (RHS_umin == 0) {
839 // We want the lowest value in RHS excluding zero. Usually that would be 1
840 // except for a range in the form of [X, 1) in which case it would be X.
841 if (RHS.getUpper() == 1)
842 RHS_umin = RHS.getLower();
843 else
844 RHS_umin = APInt(getBitWidth(), 1);
845 }
846
847 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
848
849 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
850 // this could occur.
851 if (Lower == Upper)
852 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
853
Craig Topperb7920252017-04-29 06:40:47 +0000854 return ConstantRange(std::move(Lower), std::move(Upper));
Dan Gohman5035fbf2009-07-09 22:07:27 +0000855}
856
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000857ConstantRange
Nick Lewyckyad48e012010-09-07 05:39:02 +0000858ConstantRange::binaryAnd(const ConstantRange &Other) const {
859 if (isEmptySet() || Other.isEmptySet())
860 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
861
862 // TODO: replace this with something less conservative
863
864 APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax());
865 if (umin.isAllOnesValue())
866 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Craig Topperb7920252017-04-29 06:40:47 +0000867 return ConstantRange(APInt::getNullValue(getBitWidth()), std::move(umin) + 1);
Nick Lewyckyad48e012010-09-07 05:39:02 +0000868}
869
870ConstantRange
871ConstantRange::binaryOr(const ConstantRange &Other) const {
872 if (isEmptySet() || Other.isEmptySet())
873 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
874
875 // TODO: replace this with something less conservative
876
877 APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
Craig Toppere8dea1b2017-04-28 21:48:09 +0000878 if (umax.isNullValue())
Nick Lewyckyad48e012010-09-07 05:39:02 +0000879 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Craig Topperb7920252017-04-29 06:40:47 +0000880 return ConstantRange(std::move(umax), APInt::getNullValue(getBitWidth()));
Nick Lewyckyad48e012010-09-07 05:39:02 +0000881}
882
883ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000884ConstantRange::shl(const ConstantRange &Other) const {
885 if (isEmptySet() || Other.isEmptySet())
886 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000887
Nick Lewyckyd385c222010-08-11 22:04:36 +0000888 APInt min = getUnsignedMin().shl(Other.getUnsignedMin());
889 APInt max = getUnsignedMax().shl(Other.getUnsignedMax());
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000890
891 // there's no overflow!
Nuno Lopesf8fcac72009-11-12 15:10:33 +0000892 APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
Nick Lewyckyd385c222010-08-11 22:04:36 +0000893 if (Zeros.ugt(Other.getUnsignedMax()))
Craig Topperb7920252017-04-29 06:40:47 +0000894 return ConstantRange(std::move(min), std::move(max) + 1);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000895
896 // FIXME: implement the other tricky cases
Nick Lewyckyd385c222010-08-11 22:04:36 +0000897 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000898}
899
900ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000901ConstantRange::lshr(const ConstantRange &Other) const {
902 if (isEmptySet() || Other.isEmptySet())
903 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000904
Nick Lewyckyd385c222010-08-11 22:04:36 +0000905 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin());
906 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
907 if (min == max + 1)
908 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
909
Craig Topperb7920252017-04-29 06:40:47 +0000910 return ConstantRange(std::move(min), std::move(max) + 1);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000911}
912
Owen Anderson1a9078b2010-08-07 05:47:46 +0000913ConstantRange ConstantRange::inverse() const {
Nick Lewycky228f5b42012-01-03 20:33:00 +0000914 if (isFullSet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000915 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000916 if (isEmptySet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000917 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Owen Anderson1a9078b2010-08-07 05:47:46 +0000918 return ConstantRange(Upper, Lower);
919}
920
Dan Gohmandc33ae22009-07-09 23:16:10 +0000921void ConstantRange::print(raw_ostream &OS) const {
Dan Gohman837ada72010-01-26 04:12:55 +0000922 if (isFullSet())
923 OS << "full-set";
924 else if (isEmptySet())
925 OS << "empty-set";
926 else
927 OS << "[" << Lower << "," << Upper << ")";
Dan Gohman5035fbf2009-07-09 22:07:27 +0000928}
929
Matthias Braun8c209aa2017-01-28 02:02:38 +0000930#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000931LLVM_DUMP_METHOD void ConstantRange::dump() const {
David Greenede7b3532010-01-05 01:28:32 +0000932 print(dbgs());
Dan Gohman5035fbf2009-07-09 22:07:27 +0000933}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000934#endif
Peter Collingbourneecdd58f2016-10-21 19:59:26 +0000935
936ConstantRange llvm::getConstantRangeFromMetadata(const MDNode &Ranges) {
937 const unsigned NumRanges = Ranges.getNumOperands() / 2;
938 assert(NumRanges >= 1 && "Must have at least one range!");
939 assert(Ranges.getNumOperands() % 2 == 0 && "Must be a sequence of pairs");
940
941 auto *FirstLow = mdconst::extract<ConstantInt>(Ranges.getOperand(0));
942 auto *FirstHigh = mdconst::extract<ConstantInt>(Ranges.getOperand(1));
943
944 ConstantRange CR(FirstLow->getValue(), FirstHigh->getValue());
945
946 for (unsigned i = 1; i < NumRanges; ++i) {
947 auto *Low = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
948 auto *High = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
949
950 // Note: unionWith will potentially create a range that contains values not
951 // contained in any of the original N ranges.
952 CR = CR.unionWith(ConstantRange(Low->getValue(), High->getValue()));
953 }
954
955 return CR;
956}