blob: dc6fe3177176377d66cf07f6a718d68eefdbae1a [file] [log] [blame]
Eugene Zelenkode6cce22017-06-19 22:05:08 +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
Eugene Zelenkode6cce22017-06-19 22:05:08 +000024#include "llvm/ADT/APInt.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000025#include "llvm/IR/ConstantRange.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000026#include "llvm/IR/Constants.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000027#include "llvm/IR/InstrTypes.h"
28#include "llvm/IR/Instruction.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000029#include "llvm/IR/Metadata.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000030#include "llvm/IR/Operator.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000031#include "llvm/Support/Compiler.h"
David Greenede7b3532010-01-05 01:28:32 +000032#include "llvm/Support/Debug.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000033#include "llvm/Support/ErrorHandling.h"
Chris Lattner0c19df42008-08-23 22:23:09 +000034#include "llvm/Support/raw_ostream.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000035#include <algorithm>
36#include <cassert>
37#include <cstdint>
38
Chris Lattnerc9499b62003-12-14 21:35:53 +000039using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000040
Craig Topperee4f22d2017-04-29 05:08:52 +000041ConstantRange::ConstantRange(uint32_t BitWidth, bool Full)
42 : Lower(Full ? APInt::getMaxValue(BitWidth) : APInt::getMinValue(BitWidth)),
43 Upper(Lower) {}
Chris Lattner113f2ae2002-09-01 23:53:36 +000044
Craig Topper37df0182017-04-13 00:20:31 +000045ConstantRange::ConstantRange(APInt V)
Chandler Carruth002da5d2014-03-02 04:08:41 +000046 : Lower(std::move(V)), Upper(Lower + 1) {}
Chris Lattner113f2ae2002-09-01 23:53:36 +000047
Craig Topper37df0182017-04-13 00:20:31 +000048ConstantRange::ConstantRange(APInt L, APInt U)
Chandler Carruth002da5d2014-03-02 04:08:41 +000049 : Lower(std::move(L)), Upper(std::move(U)) {
Benjamin Kramercce97be2013-07-11 15:37:27 +000050 assert(Lower.getBitWidth() == Upper.getBitWidth() &&
Dan Gohmandc33ae22009-07-09 23:16:10 +000051 "ConstantRange with unequal bit widths");
Benjamin Kramercce97be2013-07-11 15:37:27 +000052 assert((Lower != Upper || (Lower.isMaxValue() || Lower.isMinValue())) &&
Reid Spencere1f3f192007-02-28 17:36:23 +000053 "Lower == Upper, but they aren't min or max value!");
54}
55
Sanjoy Das7182d362015-03-18 00:41:24 +000056ConstantRange ConstantRange::makeAllowedICmpRegion(CmpInst::Predicate Pred,
57 const ConstantRange &CR) {
Nick Lewycky5154ee02010-09-28 18:18:36 +000058 if (CR.isEmptySet())
59 return CR;
60
Nick Lewyckydcfdce92009-07-11 06:15:39 +000061 uint32_t W = CR.getBitWidth();
62 switch (Pred) {
Sanjoy Das7182d362015-03-18 00:41:24 +000063 default:
64 llvm_unreachable("Invalid ICmp predicate to makeAllowedICmpRegion()");
Sanjay Patelf8ee0e02016-06-19 17:20:27 +000065 case CmpInst::ICMP_EQ:
66 return CR;
67 case CmpInst::ICMP_NE:
68 if (CR.isSingleElement())
69 return ConstantRange(CR.getUpper(), CR.getLower());
70 return ConstantRange(W);
71 case CmpInst::ICMP_ULT: {
72 APInt UMax(CR.getUnsignedMax());
73 if (UMax.isMinValue())
74 return ConstantRange(W, /* empty */ false);
Craig Topperb7920252017-04-29 06:40:47 +000075 return ConstantRange(APInt::getMinValue(W), std::move(UMax));
Sanjay Patelf8ee0e02016-06-19 17:20:27 +000076 }
77 case CmpInst::ICMP_SLT: {
78 APInt SMax(CR.getSignedMax());
79 if (SMax.isMinSignedValue())
80 return ConstantRange(W, /* empty */ false);
Craig Topperb7920252017-04-29 06:40:47 +000081 return ConstantRange(APInt::getSignedMinValue(W), std::move(SMax));
Sanjay Patelf8ee0e02016-06-19 17:20:27 +000082 }
83 case CmpInst::ICMP_ULE: {
84 APInt UMax(CR.getUnsignedMax());
85 if (UMax.isMaxValue())
Nick Lewyckydcfdce92009-07-11 06:15:39 +000086 return ConstantRange(W);
Craig Topperb7920252017-04-29 06:40:47 +000087 return ConstantRange(APInt::getMinValue(W), std::move(UMax) + 1);
Sanjay Patelf8ee0e02016-06-19 17:20:27 +000088 }
89 case CmpInst::ICMP_SLE: {
90 APInt SMax(CR.getSignedMax());
91 if (SMax.isMaxSignedValue())
92 return ConstantRange(W);
Craig Topperb7920252017-04-29 06:40:47 +000093 return ConstantRange(APInt::getSignedMinValue(W), std::move(SMax) + 1);
Sanjay Patelf8ee0e02016-06-19 17:20:27 +000094 }
95 case CmpInst::ICMP_UGT: {
96 APInt UMin(CR.getUnsignedMin());
97 if (UMin.isMaxValue())
98 return ConstantRange(W, /* empty */ false);
Craig Topperb7920252017-04-29 06:40:47 +000099 return ConstantRange(std::move(UMin) + 1, APInt::getNullValue(W));
Sanjay Patelf8ee0e02016-06-19 17:20:27 +0000100 }
101 case CmpInst::ICMP_SGT: {
102 APInt SMin(CR.getSignedMin());
103 if (SMin.isMaxSignedValue())
104 return ConstantRange(W, /* empty */ false);
Craig Topperb7920252017-04-29 06:40:47 +0000105 return ConstantRange(std::move(SMin) + 1, APInt::getSignedMinValue(W));
Sanjay Patelf8ee0e02016-06-19 17:20:27 +0000106 }
107 case CmpInst::ICMP_UGE: {
108 APInt UMin(CR.getUnsignedMin());
109 if (UMin.isMinValue())
110 return ConstantRange(W);
Craig Topperb7920252017-04-29 06:40:47 +0000111 return ConstantRange(std::move(UMin), APInt::getNullValue(W));
Sanjay Patelf8ee0e02016-06-19 17:20:27 +0000112 }
113 case CmpInst::ICMP_SGE: {
114 APInt SMin(CR.getSignedMin());
115 if (SMin.isMinSignedValue())
116 return ConstantRange(W);
Craig Topperb7920252017-04-29 06:40:47 +0000117 return ConstantRange(std::move(SMin), APInt::getSignedMinValue(W));
Sanjay Patelf8ee0e02016-06-19 17:20:27 +0000118 }
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000119 }
120}
121
Sanjoy Das7182d362015-03-18 00:41:24 +0000122ConstantRange ConstantRange::makeSatisfyingICmpRegion(CmpInst::Predicate Pred,
123 const ConstantRange &CR) {
124 // Follows from De-Morgan's laws:
125 //
126 // ~(~A union ~B) == A intersect B.
127 //
128 return makeAllowedICmpRegion(CmpInst::getInversePredicate(Pred), CR)
129 .inverse();
130}
131
Balaram Makam569eaec2016-05-04 21:32:14 +0000132ConstantRange ConstantRange::makeExactICmpRegion(CmpInst::Predicate Pred,
133 const APInt &C) {
134 // Computes the exact range that is equal to both the constant ranges returned
135 // by makeAllowedICmpRegion and makeSatisfyingICmpRegion. This is always true
136 // when RHS is a singleton such as an APInt and so the assert is valid.
137 // However for non-singleton RHS, for example ult [2,5) makeAllowedICmpRegion
138 // returns [0,4) but makeSatisfyICmpRegion returns [0,2).
139 //
140 assert(makeAllowedICmpRegion(Pred, C) == makeSatisfyingICmpRegion(Pred, C));
141 return makeAllowedICmpRegion(Pred, C);
142}
143
Sanjoy Das590614c2016-05-19 03:53:06 +0000144bool ConstantRange::getEquivalentICmp(CmpInst::Predicate &Pred,
145 APInt &RHS) const {
146 bool Success = false;
147
148 if (isFullSet() || isEmptySet()) {
149 Pred = isEmptySet() ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE;
150 RHS = APInt(getBitWidth(), 0);
151 Success = true;
Sanjoy Dasc7d32912016-10-02 20:59:05 +0000152 } else if (auto *OnlyElt = getSingleElement()) {
153 Pred = CmpInst::ICMP_EQ;
154 RHS = *OnlyElt;
155 Success = true;
156 } else if (auto *OnlyMissingElt = getSingleMissingElement()) {
157 Pred = CmpInst::ICMP_NE;
158 RHS = *OnlyMissingElt;
159 Success = true;
Sanjoy Das590614c2016-05-19 03:53:06 +0000160 } else if (getLower().isMinSignedValue() || getLower().isMinValue()) {
161 Pred =
162 getLower().isMinSignedValue() ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT;
163 RHS = getUpper();
164 Success = true;
165 } else if (getUpper().isMinSignedValue() || getUpper().isMinValue()) {
166 Pred =
167 getUpper().isMinSignedValue() ? CmpInst::ICMP_SGE : CmpInst::ICMP_UGE;
168 RHS = getLower();
169 Success = true;
170 }
171
172 assert((!Success || ConstantRange::makeExactICmpRegion(Pred, RHS) == *this) &&
173 "Bad result!");
174
175 return Success;
176}
177
Sanjoy Das5079f622016-02-22 16:13:02 +0000178ConstantRange
179ConstantRange::makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp,
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000180 const ConstantRange &Other,
181 unsigned NoWrapKind) {
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000182 using OBO = OverflowingBinaryOperator;
Sanjoy Das6ed05302015-10-22 03:12:57 +0000183
184 // Computes the intersection of CR0 and CR1. It is different from
185 // intersectWith in that the ConstantRange returned will only contain elements
186 // in both CR0 and CR1 (i.e. SubsetIntersect(X, Y) is a *subset*, proper or
187 // not, of both X and Y).
188 auto SubsetIntersect =
189 [](const ConstantRange &CR0, const ConstantRange &CR1) {
190 return CR0.inverse().unionWith(CR1.inverse()).inverse();
191 };
192
193 assert(BinOp >= Instruction::BinaryOpsBegin &&
194 BinOp < Instruction::BinaryOpsEnd && "Binary operators only!");
195
196 assert((NoWrapKind == OBO::NoSignedWrap ||
197 NoWrapKind == OBO::NoUnsignedWrap ||
198 NoWrapKind == (OBO::NoUnsignedWrap | OBO::NoSignedWrap)) &&
199 "NoWrapKind invalid!");
200
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000201 unsigned BitWidth = Other.getBitWidth();
Sanjoy Das6ed05302015-10-22 03:12:57 +0000202 if (BinOp != Instruction::Add)
203 // Conservative answer: empty set
204 return ConstantRange(BitWidth, false);
205
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000206 if (auto *C = Other.getSingleElement())
Craig Toppere8dea1b2017-04-28 21:48:09 +0000207 if (C->isNullValue())
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000208 // Full set: nothing signed / unsigned wraps when added to 0.
209 return ConstantRange(BitWidth);
Sanjoy Das6ed05302015-10-22 03:12:57 +0000210
211 ConstantRange Result(BitWidth);
212
213 if (NoWrapKind & OBO::NoUnsignedWrap)
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000214 Result =
215 SubsetIntersect(Result, ConstantRange(APInt::getNullValue(BitWidth),
216 -Other.getUnsignedMax()));
Sanjoy Das6ed05302015-10-22 03:12:57 +0000217
218 if (NoWrapKind & OBO::NoSignedWrap) {
Craig Topper72235d02017-04-28 21:48:03 +0000219 const APInt &SignedMin = Other.getSignedMin();
220 const APInt &SignedMax = Other.getSignedMax();
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000221
222 if (SignedMax.isStrictlyPositive())
Sanjoy Das6ed05302015-10-22 03:12:57 +0000223 Result = SubsetIntersect(
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000224 Result,
225 ConstantRange(APInt::getSignedMinValue(BitWidth),
226 APInt::getSignedMinValue(BitWidth) - SignedMax));
227
228 if (SignedMin.isNegative())
Sanjoy Das6ed05302015-10-22 03:12:57 +0000229 Result = SubsetIntersect(
Sanjoy Dasf3867e62016-03-03 18:31:16 +0000230 Result, ConstantRange(APInt::getSignedMinValue(BitWidth) - SignedMin,
Sanjoy Das6ed05302015-10-22 03:12:57 +0000231 APInt::getSignedMinValue(BitWidth)));
232 }
233
234 return Result;
235}
236
Chris Lattner113f2ae2002-09-01 23:53:36 +0000237bool ConstantRange::isFullSet() const {
Zhou Sheng31787362007-04-26 16:42:07 +0000238 return Lower == Upper && Lower.isMaxValue();
Chris Lattner113f2ae2002-09-01 23:53:36 +0000239}
Misha Brukmanb1c93172005-04-21 23:48:37 +0000240
Chris Lattner113f2ae2002-09-01 23:53:36 +0000241bool ConstantRange::isEmptySet() const {
Zhou Sheng31787362007-04-26 16:42:07 +0000242 return Lower == Upper && Lower.isMinValue();
Chris Lattner113f2ae2002-09-01 23:53:36 +0000243}
244
Reid Spencer6a440332007-03-01 07:54:15 +0000245bool ConstantRange::isWrappedSet() const {
Reid Spencere1f3f192007-02-28 17:36:23 +0000246 return Lower.ugt(Upper);
Chris Lattner113f2ae2002-09-01 23:53:36 +0000247}
248
Nick Lewyckya35462d2010-09-06 23:52:49 +0000249bool ConstantRange::isSignWrappedSet() const {
250 return contains(APInt::getSignedMaxValue(getBitWidth())) &&
251 contains(APInt::getSignedMinValue(getBitWidth()));
252}
253
Reid Spencere1f3f192007-02-28 17:36:23 +0000254APInt ConstantRange::getSetSize() const {
Craig Topper8c5c6fe2017-04-29 17:59:41 +0000255 if (isFullSet())
256 return APInt::getOneBitSet(getBitWidth()+1, getBitWidth());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000257
Nuno Lopes216d5712012-07-17 15:43:59 +0000258 // This is also correct for wrapped sets.
Nuno Lopes99504c52012-07-16 18:08:12 +0000259 return (Upper - Lower).zext(getBitWidth()+1);
Chris Lattner113f2ae2002-09-01 23:53:36 +0000260}
261
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000262bool
Craig Topperd29549e2017-05-07 21:48:08 +0000263ConstantRange::isSizeStrictlySmallerThan(const ConstantRange &Other) const {
Michael Zolotukhinc69955c2017-03-20 06:33:07 +0000264 assert(getBitWidth() == Other.getBitWidth());
265 if (isFullSet())
266 return false;
267 if (Other.isFullSet())
268 return true;
269 return (Upper - Lower).ult(Other.Upper - Other.Lower);
270}
271
Craig Topper7e3e7af2017-05-07 22:22:11 +0000272bool
273ConstantRange::isSizeLargerThan(uint64_t MaxSize) const {
274 assert(MaxSize && "MaxSize can't be 0.");
275 // If this a full set, we need special handling to avoid needing an extra bit
276 // to represent the size.
277 if (isFullSet())
278 return APInt::getMaxValue(getBitWidth()).ugt(MaxSize - 1);
279
280 return (Upper - Lower).ugt(MaxSize);
281}
282
Nick Lewyckye4559372007-03-10 15:54:12 +0000283APInt ConstantRange::getUnsignedMax() const {
284 if (isFullSet() || isWrappedSet())
285 return APInt::getMaxValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000286 return getUpper() - 1;
Nick Lewyckye4559372007-03-10 15:54:12 +0000287}
288
Nick Lewyckye4559372007-03-10 15:54:12 +0000289APInt ConstantRange::getUnsignedMin() const {
Craig Topper61729fd2017-05-09 05:01:29 +0000290 if (isFullSet() || (isWrappedSet() && !getUpper().isNullValue()))
Nick Lewyckye4559372007-03-10 15:54:12 +0000291 return APInt::getMinValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000292 return getLower();
Nick Lewyckye4559372007-03-10 15:54:12 +0000293}
294
Nick Lewyckye4559372007-03-10 15:54:12 +0000295APInt ConstantRange::getSignedMax() const {
Craig Topper61e684a2017-06-16 23:26:23 +0000296 if (isFullSet() || Lower.sgt(Upper))
Craig Topper88c64f32017-04-18 23:02:39 +0000297 return APInt::getSignedMaxValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000298 return getUpper() - 1;
Nick Lewyckye4559372007-03-10 15:54:12 +0000299}
300
Nick Lewyckye4559372007-03-10 15:54:12 +0000301APInt ConstantRange::getSignedMin() const {
Craig Topper61e684a2017-06-16 23:26:23 +0000302 if (isFullSet() || (Lower.sgt(Upper) && !getUpper().isMinSignedValue()))
Craig Topper88c64f32017-04-18 23:02:39 +0000303 return APInt::getSignedMinValue(getBitWidth());
Nick Lewycky228f5b42012-01-03 20:33:00 +0000304 return getLower();
Nick Lewyckye4559372007-03-10 15:54:12 +0000305}
306
Reid Spencer6a440332007-03-01 07:54:15 +0000307bool ConstantRange::contains(const APInt &V) const {
308 if (Lower == Upper)
309 return isFullSet();
Chris Lattner113f2ae2002-09-01 23:53:36 +0000310
Reid Spencer6a440332007-03-01 07:54:15 +0000311 if (!isWrappedSet())
312 return Lower.ule(V) && V.ult(Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000313 return Lower.ule(V) || V.ult(Upper);
Chris Lattner55481f72004-03-30 00:20:08 +0000314}
315
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000316bool ConstantRange::contains(const ConstantRange &Other) const {
Nick Lewyckyd385c222010-08-11 22:04:36 +0000317 if (isFullSet() || Other.isEmptySet()) return true;
318 if (isEmptySet() || Other.isFullSet()) return false;
Nick Lewyckydcfdce92009-07-11 06:15:39 +0000319
320 if (!isWrappedSet()) {
321 if (Other.isWrappedSet())
322 return false;
323
324 return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
325 }
326
327 if (!Other.isWrappedSet())
328 return Other.getUpper().ule(Upper) ||
329 Lower.ule(Other.getLower());
330
331 return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
332}
333
Reid Spencer3a7e9d82007-02-28 19:57:34 +0000334ConstantRange ConstantRange::subtract(const APInt &Val) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000335 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
Chris Lattner55481f72004-03-30 00:20:08 +0000336 // If the set is empty or full, don't modify the endpoints.
Reid Spencere1f3f192007-02-28 17:36:23 +0000337 if (Lower == Upper)
338 return *this;
Reid Spencere1f3f192007-02-28 17:36:23 +0000339 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattner55481f72004-03-30 00:20:08 +0000340}
Chris Lattner113f2ae2002-09-01 23:53:36 +0000341
Nuno Lopes5020db22012-06-28 16:10:13 +0000342ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
343 return intersectWith(CR.inverse());
344}
345
Reid Spencer6a440332007-03-01 07:54:15 +0000346ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000347 assert(getBitWidth() == CR.getBitWidth() &&
348 "ConstantRange types don't agree!");
Nick Lewycky61b4a262007-07-14 02:51:34 +0000349
350 // Handle common cases.
351 if ( isEmptySet() || CR.isFullSet()) return *this;
352 if (CR.isEmptySet() || isFullSet()) return CR;
353
354 if (!isWrappedSet() && CR.isWrappedSet())
Nick Lewycky0d139032009-07-18 06:34:42 +0000355 return CR.intersectWith(*this);
Nick Lewycky61b4a262007-07-14 02:51:34 +0000356
357 if (!isWrappedSet() && !CR.isWrappedSet()) {
358 if (Lower.ult(CR.Lower)) {
359 if (Upper.ule(CR.Lower))
360 return ConstantRange(getBitWidth(), false);
361
362 if (Upper.ult(CR.Upper))
363 return ConstantRange(CR.Lower, Upper);
364
365 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000366 }
Nick Lewycky228f5b42012-01-03 20:33:00 +0000367 if (Upper.ult(CR.Upper))
368 return *this;
369
370 if (Lower.ult(CR.Upper))
371 return ConstantRange(Lower, CR.Upper);
372
373 return ConstantRange(getBitWidth(), false);
Nick Lewycky61b4a262007-07-14 02:51:34 +0000374 }
375
376 if (isWrappedSet() && !CR.isWrappedSet()) {
377 if (CR.Lower.ult(Upper)) {
378 if (CR.Upper.ult(Upper))
379 return CR;
380
Nuno Lopes63afc082012-05-18 00:14:36 +0000381 if (CR.Upper.ule(Lower))
Nick Lewycky61b4a262007-07-14 02:51:34 +0000382 return ConstantRange(CR.Lower, Upper);
383
Craig Topperd29549e2017-05-07 21:48:08 +0000384 if (isSizeStrictlySmallerThan(CR))
Nick Lewycky61b4a262007-07-14 02:51:34 +0000385 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000386 return CR;
387 }
388 if (CR.Lower.ult(Lower)) {
Nick Lewycky61b4a262007-07-14 02:51:34 +0000389 if (CR.Upper.ule(Lower))
390 return ConstantRange(getBitWidth(), false);
391
392 return ConstantRange(Lower, CR.Upper);
393 }
394 return CR;
395 }
396
397 if (CR.Upper.ult(Upper)) {
398 if (CR.Lower.ult(Upper)) {
Craig Topperd29549e2017-05-07 21:48:08 +0000399 if (isSizeStrictlySmallerThan(CR))
Nick Lewycky61b4a262007-07-14 02:51:34 +0000400 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000401 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000402 }
403
404 if (CR.Lower.ult(Lower))
405 return ConstantRange(Lower, CR.Upper);
406
407 return CR;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000408 }
Nuno Lopesebb0c942012-06-28 00:59:33 +0000409 if (CR.Upper.ule(Lower)) {
Nick Lewycky61b4a262007-07-14 02:51:34 +0000410 if (CR.Lower.ult(Lower))
411 return *this;
412
413 return ConstantRange(CR.Lower, Upper);
414 }
Craig Topperd29549e2017-05-07 21:48:08 +0000415 if (isSizeStrictlySmallerThan(CR))
Nick Lewycky61b4a262007-07-14 02:51:34 +0000416 return *this;
Nick Lewycky228f5b42012-01-03 20:33:00 +0000417 return CR;
Nick Lewycky61b4a262007-07-14 02:51:34 +0000418}
419
Reid Spencer6a440332007-03-01 07:54:15 +0000420ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000421 assert(getBitWidth() == CR.getBitWidth() &&
422 "ConstantRange types don't agree!");
Chris Lattner113f2ae2002-09-01 23:53:36 +0000423
Nick Lewyckycf87f9e2007-03-02 03:33:05 +0000424 if ( isFullSet() || CR.isEmptySet()) return *this;
425 if (CR.isFullSet() || isEmptySet()) return CR;
Chris Lattner113f2ae2002-09-01 23:53:36 +0000426
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000427 if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
428
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000429 if (!isWrappedSet() && !CR.isWrappedSet()) {
Nick Lewycky567daf32009-07-19 03:44:35 +0000430 if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
431 // If the two ranges are disjoint, find the smaller gap and bridge it.
432 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
433 if (d1.ult(d2))
434 return ConstantRange(Lower, CR.Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000435 return ConstantRange(CR.Lower, Upper);
Nick Lewycky567daf32009-07-19 03:44:35 +0000436 }
437
Craig Topper8fb5a142017-04-29 07:24:13 +0000438 APInt L = CR.Lower.ult(Lower) ? CR.Lower : Lower;
439 APInt U = (CR.Upper - 1).ugt(Upper - 1) ? CR.Upper : Upper;
Nick Lewycky567daf32009-07-19 03:44:35 +0000440
Craig Topper61729fd2017-05-09 05:01:29 +0000441 if (L.isNullValue() && U.isNullValue())
Nick Lewycky567daf32009-07-19 03:44:35 +0000442 return ConstantRange(getBitWidth());
443
Craig Topperb7920252017-04-29 06:40:47 +0000444 return ConstantRange(std::move(L), std::move(U));
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000445 }
446
Nick Lewycky567daf32009-07-19 03:44:35 +0000447 if (!CR.isWrappedSet()) {
448 // ------U L----- and ------U L----- : this
449 // L--U L--U : CR
450 if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000451 return *this;
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000452
Nick Lewycky567daf32009-07-19 03:44:35 +0000453 // ------U L----- : this
454 // L---------U : CR
455 if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000456 return ConstantRange(getBitWidth());
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000457
Nick Lewycky567daf32009-07-19 03:44:35 +0000458 // ----U L---- : this
459 // L---U : CR
460 // <d1> <d2>
461 if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000462 APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
Nick Lewycky567daf32009-07-19 03:44:35 +0000463 if (d1.ult(d2))
464 return ConstantRange(Lower, CR.Upper);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000465 return ConstantRange(CR.Lower, Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000466 }
467
Nick Lewycky567daf32009-07-19 03:44:35 +0000468 // ----U L----- : this
469 // L----U : CR
470 if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
471 return ConstantRange(CR.Lower, Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000472
Nick Lewycky567daf32009-07-19 03:44:35 +0000473 // ------U L---- : this
474 // L-----U : CR
Nick Lewycky228f5b42012-01-03 20:33:00 +0000475 assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) &&
476 "ConstantRange::unionWith missed a case with one range wrapped");
477 return ConstantRange(Lower, CR.Upper);
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000478 }
479
Nick Lewycky567daf32009-07-19 03:44:35 +0000480 // ------U L---- and ------U L---- : this
481 // -U L----------- and ------------U L : CR
482 if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
483 return ConstantRange(getBitWidth());
Nick Lewyckyf22938a2007-04-01 03:47:44 +0000484
Craig Topper8fb5a142017-04-29 07:24:13 +0000485 APInt L = CR.Lower.ult(Lower) ? CR.Lower : Lower;
486 APInt U = CR.Upper.ugt(Upper) ? CR.Upper : Upper;
Nick Lewyckycf87f9e2007-03-02 03:33:05 +0000487
Craig Topperb7920252017-04-29 06:40:47 +0000488 return ConstantRange(std::move(L), std::move(U));
Chris Lattner113f2ae2002-09-01 23:53:36 +0000489}
Chris Lattnere0bb9eb2002-09-02 00:18:22 +0000490
Philip Reames4d00af12016-12-01 20:08:47 +0000491ConstantRange ConstantRange::castOp(Instruction::CastOps CastOp,
492 uint32_t ResultBitWidth) const {
493 switch (CastOp) {
494 default:
495 llvm_unreachable("unsupported cast type");
496 case Instruction::Trunc:
497 return truncate(ResultBitWidth);
498 case Instruction::SExt:
499 return signExtend(ResultBitWidth);
500 case Instruction::ZExt:
501 return zeroExtend(ResultBitWidth);
502 case Instruction::BitCast:
503 return *this;
504 case Instruction::FPToUI:
505 case Instruction::FPToSI:
506 if (getBitWidth() == ResultBitWidth)
507 return *this;
508 else
509 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
510 case Instruction::UIToFP: {
511 // TODO: use input range if available
512 auto BW = getBitWidth();
513 APInt Min = APInt::getMinValue(BW).zextOrSelf(ResultBitWidth);
514 APInt Max = APInt::getMaxValue(BW).zextOrSelf(ResultBitWidth);
Craig Topperb7920252017-04-29 06:40:47 +0000515 return ConstantRange(std::move(Min), std::move(Max));
Philip Reames4d00af12016-12-01 20:08:47 +0000516 }
517 case Instruction::SIToFP: {
518 // TODO: use input range if available
519 auto BW = getBitWidth();
520 APInt SMin = APInt::getSignedMinValue(BW).sextOrSelf(ResultBitWidth);
521 APInt SMax = APInt::getSignedMaxValue(BW).sextOrSelf(ResultBitWidth);
Craig Topperb7920252017-04-29 06:40:47 +0000522 return ConstantRange(std::move(SMin), std::move(SMax));
Philip Reames4d00af12016-12-01 20:08:47 +0000523 }
524 case Instruction::FPTrunc:
525 case Instruction::FPExt:
526 case Instruction::IntToPtr:
527 case Instruction::PtrToInt:
528 case Instruction::AddrSpaceCast:
529 // Conservatively return full set.
530 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
531 };
532}
533
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000534ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000535 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
536
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000537 unsigned SrcTySize = getBitWidth();
Reid Spencere1f3f192007-02-28 17:36:23 +0000538 assert(SrcTySize < DstTySize && "Not a value extension");
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000539 if (isFullSet() || isWrappedSet()) {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000540 // Change into [0, 1 << src bit width)
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000541 APInt LowerExt(DstTySize, 0);
542 if (!Upper) // special case: [X, 0) -- not really wrapping around
543 LowerExt = Lower.zext(DstTySize);
Craig Topperb7920252017-04-29 06:40:47 +0000544 return ConstantRange(std::move(LowerExt),
545 APInt::getOneBitSet(DstTySize, SrcTySize));
Nuno Lopeseb9d2752012-07-23 20:33:29 +0000546 }
Chris Lattner55481f72004-03-30 00:20:08 +0000547
Jay Foad583abbc2010-12-07 08:25:19 +0000548 return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
Chris Lattner55481f72004-03-30 00:20:08 +0000549}
550
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000551ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
Nick Lewyckya35462d2010-09-06 23:52:49 +0000552 if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
553
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000554 unsigned SrcTySize = getBitWidth();
555 assert(SrcTySize < DstTySize && "Not a value extension");
Nuno Lopes1112eca2013-10-30 15:36:50 +0000556
557 // special case: [X, INT_MIN) -- not really wrapping around
Nuno Lopes14d4b0c2013-10-31 19:53:53 +0000558 if (Upper.isMinSignedValue())
Nuno Lopes1112eca2013-10-30 15:36:50 +0000559 return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize));
560
Nick Lewyckya35462d2010-09-06 23:52:49 +0000561 if (isFullSet() || isSignWrappedSet()) {
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000562 return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
Nick Lewycky5edc4592009-07-13 04:17:23 +0000563 APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000564 }
565
Jay Foad583abbc2010-12-07 08:25:19 +0000566 return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
Nick Lewyckyb89804f2007-04-07 15:41:33 +0000567}
568
Reid Spencer9b3d6ec2007-02-28 22:02:48 +0000569ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
Benjamin Kramer6709e052011-11-24 17:24:33 +0000570 assert(getBitWidth() > DstTySize && "Not a value truncation");
Nuno Lopesc14776d2012-07-19 16:27:45 +0000571 if (isEmptySet())
572 return ConstantRange(DstTySize, /*isFullSet=*/false);
573 if (isFullSet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000574 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattner55481f72004-03-30 00:20:08 +0000575
Nuno Lopesc14776d2012-07-19 16:27:45 +0000576 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()) {
Craig Topperf6e138d2017-06-05 20:48:05 +0000583 // If Upper is greater than or equal to MaxValue(DstTy), it covers the whole
584 // truncated range.
585 if (Upper.getActiveBits() > DstTySize ||
586 Upper.countTrailingOnes() == DstTySize)
Nuno Lopesc14776d2012-07-19 16:27:45 +0000587 return ConstantRange(DstTySize, /*isFullSet=*/true);
588
589 Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
Craig Topper86616532017-04-30 00:44:05 +0000590 UpperDiv.setAllBits();
Nuno Lopesc14776d2012-07-19 16:27:45 +0000591
592 // Union covers the MaxValue case, so return if the remaining range is just
Craig Topperf6e138d2017-06-05 20:48:05 +0000593 // MaxValue(DstTy).
Nuno Lopesc14776d2012-07-19 16:27:45 +0000594 if (LowerDiv == UpperDiv)
595 return Union;
596 }
597
598 // Chop off the most significant bits that are past the destination bitwidth.
Craig Topperf6e138d2017-06-05 20:48:05 +0000599 if (LowerDiv.getActiveBits() > DstTySize) {
600 // Mask to just the signficant bits and subtract from LowerDiv/UpperDiv.
601 APInt Adjust = LowerDiv & APInt::getBitsSetFrom(getBitWidth(), DstTySize);
602 LowerDiv -= Adjust;
603 UpperDiv -= Adjust;
Nuno Lopesc14776d2012-07-19 16:27:45 +0000604 }
605
Craig Topperf6e138d2017-06-05 20:48:05 +0000606 unsigned UpperDivWidth = UpperDiv.getActiveBits();
607 if (UpperDivWidth <= DstTySize)
Nuno Lopesc14776d2012-07-19 16:27:45 +0000608 return ConstantRange(LowerDiv.trunc(DstTySize),
609 UpperDiv.trunc(DstTySize)).unionWith(Union);
610
Nick Lewyckybfad0152016-06-06 01:51:23 +0000611 // The truncated value wraps around. Check if we can do better than fullset.
Craig Topperf6e138d2017-06-05 20:48:05 +0000612 if (UpperDivWidth == DstTySize + 1) {
613 // Clear the MSB so that UpperDiv wraps around.
614 UpperDiv.clearBit(DstTySize);
615 if (UpperDiv.ult(LowerDiv))
616 return ConstantRange(LowerDiv.trunc(DstTySize),
617 UpperDiv.trunc(DstTySize)).unionWith(Union);
618 }
Nuno Lopesc14776d2012-07-19 16:27:45 +0000619
620 return ConstantRange(DstTySize, /*isFullSet=*/true);
Chris Lattner55481f72004-03-30 00:20:08 +0000621}
622
Nuno Lopes640eb702009-11-09 15:36:28 +0000623ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
624 unsigned SrcTySize = getBitWidth();
625 if (SrcTySize > DstTySize)
626 return truncate(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000627 if (SrcTySize < DstTySize)
Nuno Lopes640eb702009-11-09 15:36:28 +0000628 return zeroExtend(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000629 return *this;
Nuno Lopes640eb702009-11-09 15:36:28 +0000630}
631
Nuno Lopes640eb702009-11-09 15:36:28 +0000632ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
633 unsigned SrcTySize = getBitWidth();
634 if (SrcTySize > DstTySize)
635 return truncate(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000636 if (SrcTySize < DstTySize)
Nuno Lopes640eb702009-11-09 15:36:28 +0000637 return signExtend(DstTySize);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000638 return *this;
Nuno Lopes640eb702009-11-09 15:36:28 +0000639}
640
Philip Reames4d00af12016-12-01 20:08:47 +0000641ConstantRange ConstantRange::binaryOp(Instruction::BinaryOps BinOp,
642 const ConstantRange &Other) const {
643 assert(BinOp >= Instruction::BinaryOpsBegin &&
644 BinOp < Instruction::BinaryOpsEnd && "Binary operators only!");
645
646 switch (BinOp) {
647 case Instruction::Add:
648 return add(Other);
649 case Instruction::Sub:
650 return sub(Other);
651 case Instruction::Mul:
652 return multiply(Other);
653 case Instruction::UDiv:
654 return udiv(Other);
655 case Instruction::Shl:
656 return shl(Other);
657 case Instruction::LShr:
658 return lshr(Other);
659 case Instruction::And:
660 return binaryAnd(Other);
661 case Instruction::Or:
662 return binaryOr(Other);
663 // Note: floating point operations applied to abstract ranges are just
664 // ideal integer operations with a lossy representation
665 case Instruction::FAdd:
666 return add(Other);
667 case Instruction::FSub:
668 return sub(Other);
669 case Instruction::FMul:
670 return multiply(Other);
671 default:
672 // Conservatively return full set.
673 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
674 }
675}
676
Dan Gohman5035fbf2009-07-09 22:07:27 +0000677ConstantRange
678ConstantRange::add(const ConstantRange &Other) const {
679 if (isEmptySet() || Other.isEmptySet())
680 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewycky73b704d2009-07-13 02:49:08 +0000681 if (isFullSet() || Other.isFullSet())
682 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000683
Dan Gohman5035fbf2009-07-09 22:07:27 +0000684 APInt NewLower = getLower() + Other.getLower();
685 APInt NewUpper = getUpper() + Other.getUpper() - 1;
686 if (NewLower == NewUpper)
687 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
688
Craig Topperb7920252017-04-29 06:40:47 +0000689 ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper));
Craig Topperd29549e2017-05-07 21:48:08 +0000690 if (X.isSizeStrictlySmallerThan(*this) ||
691 X.isSizeStrictlySmallerThan(Other))
Dan Gohman5035fbf2009-07-09 22:07:27 +0000692 // We've wrapped, therefore, full set.
693 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000694 return X;
Chris Lattnere0bb9eb2002-09-02 00:18:22 +0000695}
696
Artur Pilipenkoed841032016-10-19 14:44:23 +0000697ConstantRange ConstantRange::addWithNoSignedWrap(const APInt &Other) const {
698 // Calculate the subset of this range such that "X + Other" is
699 // guaranteed not to wrap (overflow) for all X in this subset.
700 // makeGuaranteedNoWrapRegion will produce an exact NSW range since we are
701 // passing a single element range.
702 auto NSWRange = ConstantRange::makeGuaranteedNoWrapRegion(BinaryOperator::Add,
703 ConstantRange(Other),
704 OverflowingBinaryOperator::NoSignedWrap);
705 auto NSWConstrainedRange = intersectWith(NSWRange);
706
707 return NSWConstrainedRange.add(ConstantRange(Other));
708}
709
Dan Gohman5035fbf2009-07-09 22:07:27 +0000710ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000711ConstantRange::sub(const ConstantRange &Other) const {
712 if (isEmptySet() || Other.isEmptySet())
713 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
714 if (isFullSet() || Other.isFullSet())
715 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
716
Nick Lewycky5dc6b792011-06-22 21:13:46 +0000717 APInt NewLower = getLower() - Other.getUpper() + 1;
718 APInt NewUpper = getUpper() - Other.getLower();
Nick Lewyckyd385c222010-08-11 22:04:36 +0000719 if (NewLower == NewUpper)
720 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
721
Craig Topperb7920252017-04-29 06:40:47 +0000722 ConstantRange X = ConstantRange(std::move(NewLower), std::move(NewUpper));
Craig Topperd29549e2017-05-07 21:48:08 +0000723 if (X.isSizeStrictlySmallerThan(*this) ||
724 X.isSizeStrictlySmallerThan(Other))
Nick Lewyckyd385c222010-08-11 22:04:36 +0000725 // We've wrapped, therefore, full set.
726 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Nick Lewyckyd385c222010-08-11 22:04:36 +0000727 return X;
728}
729
730ConstantRange
Dan Gohman5035fbf2009-07-09 22:07:27 +0000731ConstantRange::multiply(const ConstantRange &Other) const {
Dan Gohman3f8ed9e2010-01-26 15:56:18 +0000732 // TODO: If either operand is a single element and the multiply is known to
733 // be non-wrapping, round the result min and max value to the appropriate
734 // multiple of that element. If wrapping is possible, at least adjust the
735 // range according to the greatest power-of-two factor of the single element.
Dan Gohman5325efc2010-01-26 04:13:15 +0000736
Nick Lewycky2951c992009-07-12 02:19:05 +0000737 if (isEmptySet() || Other.isEmptySet())
738 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes986cc182012-07-16 20:47:16 +0000739
James Molloydcc78ec2015-03-06 15:50:47 +0000740 // Multiplication is signedness-independent. However different ranges can be
741 // obtained depending on how the input ranges are treated. These different
742 // ranges are all conservatively correct, but one might be better than the
743 // other. We calculate two ranges; one treating the inputs as unsigned
744 // and the other signed, then return the smallest of these ranges.
745
746 // Unsigned range first.
Nick Lewycky53023892009-07-13 03:27:41 +0000747 APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
748 APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
749 APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
750 APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
Nick Lewycky2951c992009-07-12 02:19:05 +0000751
Nick Lewycky53023892009-07-13 03:27:41 +0000752 ConstantRange Result_zext = ConstantRange(this_min * Other_min,
753 this_max * Other_max + 1);
James Molloydcc78ec2015-03-06 15:50:47 +0000754 ConstantRange UR = Result_zext.truncate(getBitWidth());
755
Pete Cooper34a7a942016-05-27 17:06:50 +0000756 // If the unsigned range doesn't wrap, and isn't negative then it's a range
757 // from one positive number to another which is as good as we can generate.
758 // In this case, skip the extra work of generating signed ranges which aren't
759 // going to be better than this range.
Craig Topperc51d0532017-05-10 20:01:48 +0000760 if (!UR.isWrappedSet() &&
761 (UR.getUpper().isNonNegative() || UR.getUpper().isMinSignedValue()))
Pete Cooper34a7a942016-05-27 17:06:50 +0000762 return UR;
763
James Molloydcc78ec2015-03-06 15:50:47 +0000764 // Now the signed range. Because we could be dealing with negative numbers
765 // here, the lower bound is the smallest of the cartesian product of the
766 // lower and upper ranges; for example:
767 // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
768 // Similarly for the upper bound, swapping min for max.
769
770 this_min = getSignedMin().sext(getBitWidth() * 2);
771 this_max = getSignedMax().sext(getBitWidth() * 2);
772 Other_min = Other.getSignedMin().sext(getBitWidth() * 2);
773 Other_max = Other.getSignedMax().sext(getBitWidth() * 2);
774
775 auto L = {this_min * Other_min, this_min * Other_max,
776 this_max * Other_min, this_max * Other_max};
777 auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
778 ConstantRange Result_sext(std::min(L, Compare), std::max(L, Compare) + 1);
779 ConstantRange SR = Result_sext.truncate(getBitWidth());
780
Craig Topperd29549e2017-05-07 21:48:08 +0000781 return UR.isSizeStrictlySmallerThan(SR) ? UR : SR;
Dan Gohman5035fbf2009-07-09 22:07:27 +0000782}
783
784ConstantRange
785ConstantRange::smax(const ConstantRange &Other) const {
Dan Gohmandc33ae22009-07-09 23:16:10 +0000786 // X smax Y is: range(smax(X_smin, Y_smin),
787 // smax(X_smax, Y_smax))
788 if (isEmptySet() || Other.isEmptySet())
789 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohmandc33ae22009-07-09 23:16:10 +0000790 APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
791 APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
792 if (NewU == NewL)
793 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Craig Topperb7920252017-04-29 06:40:47 +0000794 return ConstantRange(std::move(NewL), std::move(NewU));
Dan Gohman5035fbf2009-07-09 22:07:27 +0000795}
796
797ConstantRange
798ConstantRange::umax(const ConstantRange &Other) const {
799 // X umax Y is: range(umax(X_umin, Y_umin),
800 // umax(X_umax, Y_umax))
801 if (isEmptySet() || Other.isEmptySet())
802 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Dan Gohman5035fbf2009-07-09 22:07:27 +0000803 APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
804 APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
805 if (NewU == NewL)
806 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Craig Topperb7920252017-04-29 06:40:47 +0000807 return ConstantRange(std::move(NewL), std::move(NewU));
Dan Gohman5035fbf2009-07-09 22:07:27 +0000808}
809
810ConstantRange
Philip Reamesba313122016-02-26 22:08:18 +0000811ConstantRange::smin(const ConstantRange &Other) const {
812 // X smin Y is: range(smin(X_smin, Y_smin),
813 // smin(X_smax, Y_smax))
814 if (isEmptySet() || Other.isEmptySet())
815 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
816 APInt NewL = APIntOps::smin(getSignedMin(), Other.getSignedMin());
817 APInt NewU = APIntOps::smin(getSignedMax(), Other.getSignedMax()) + 1;
818 if (NewU == NewL)
819 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Craig Topperb7920252017-04-29 06:40:47 +0000820 return ConstantRange(std::move(NewL), std::move(NewU));
Philip Reamesba313122016-02-26 22:08:18 +0000821}
822
823ConstantRange
824ConstantRange::umin(const ConstantRange &Other) const {
825 // X umin Y is: range(umin(X_umin, Y_umin),
826 // umin(X_umax, Y_umax))
827 if (isEmptySet() || Other.isEmptySet())
828 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
829 APInt NewL = APIntOps::umin(getUnsignedMin(), Other.getUnsignedMin());
830 APInt NewU = APIntOps::umin(getUnsignedMax(), Other.getUnsignedMax()) + 1;
831 if (NewU == NewL)
832 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Craig Topperb7920252017-04-29 06:40:47 +0000833 return ConstantRange(std::move(NewL), std::move(NewU));
Philip Reamesba313122016-02-26 22:08:18 +0000834}
835
836ConstantRange
Nick Lewyckyf1b8cb32009-07-12 05:18:18 +0000837ConstantRange::udiv(const ConstantRange &RHS) const {
Craig Topper61729fd2017-05-09 05:01:29 +0000838 if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax().isNullValue())
Nick Lewyckyf1b8cb32009-07-12 05:18:18 +0000839 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
840 if (RHS.isFullSet())
841 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
842
843 APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
844
845 APInt RHS_umin = RHS.getUnsignedMin();
Craig Topper61729fd2017-05-09 05:01:29 +0000846 if (RHS_umin.isNullValue()) {
Nick Lewyckyf1b8cb32009-07-12 05:18:18 +0000847 // We want the lowest value in RHS excluding zero. Usually that would be 1
848 // except for a range in the form of [X, 1) in which case it would be X.
849 if (RHS.getUpper() == 1)
850 RHS_umin = RHS.getLower();
851 else
Craig Topper86616532017-04-30 00:44:05 +0000852 RHS_umin = 1;
Nick Lewyckyf1b8cb32009-07-12 05:18:18 +0000853 }
854
855 APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
856
857 // If the LHS is Full and the RHS is a wrapped interval containing 1 then
858 // this could occur.
859 if (Lower == Upper)
860 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
861
Craig Topperb7920252017-04-29 06:40:47 +0000862 return ConstantRange(std::move(Lower), std::move(Upper));
Dan Gohman5035fbf2009-07-09 22:07:27 +0000863}
864
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000865ConstantRange
Nick Lewyckyad48e012010-09-07 05:39:02 +0000866ConstantRange::binaryAnd(const ConstantRange &Other) const {
867 if (isEmptySet() || Other.isEmptySet())
868 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
869
870 // TODO: replace this with something less conservative
871
872 APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax());
873 if (umin.isAllOnesValue())
874 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Craig Topperb7920252017-04-29 06:40:47 +0000875 return ConstantRange(APInt::getNullValue(getBitWidth()), std::move(umin) + 1);
Nick Lewyckyad48e012010-09-07 05:39:02 +0000876}
877
878ConstantRange
879ConstantRange::binaryOr(const ConstantRange &Other) const {
880 if (isEmptySet() || Other.isEmptySet())
881 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
882
883 // TODO: replace this with something less conservative
884
885 APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
Craig Toppere8dea1b2017-04-28 21:48:09 +0000886 if (umax.isNullValue())
Nick Lewyckyad48e012010-09-07 05:39:02 +0000887 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Craig Topperb7920252017-04-29 06:40:47 +0000888 return ConstantRange(std::move(umax), APInt::getNullValue(getBitWidth()));
Nick Lewyckyad48e012010-09-07 05:39:02 +0000889}
890
891ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000892ConstantRange::shl(const ConstantRange &Other) const {
893 if (isEmptySet() || Other.isEmptySet())
894 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000895
Craig Topperef028032017-05-09 07:04:04 +0000896 APInt max = getUnsignedMax();
897 APInt Other_umax = Other.getUnsignedMax();
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000898
Craig Topperef028032017-05-09 07:04:04 +0000899 // there's overflow!
900 if (Other_umax.uge(max.countLeadingZeros()))
901 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000902
903 // FIXME: implement the other tricky cases
Craig Topperef028032017-05-09 07:04:04 +0000904
905 APInt min = getUnsignedMin();
906 min <<= Other.getUnsignedMin();
907 max <<= Other_umax;
908
909 return ConstantRange(std::move(min), std::move(max) + 1);
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000910}
911
912ConstantRange
Nick Lewyckyd385c222010-08-11 22:04:36 +0000913ConstantRange::lshr(const ConstantRange &Other) const {
914 if (isEmptySet() || Other.isEmptySet())
915 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Craig Topper79b76662017-05-09 07:04:02 +0000916
917 APInt max = getUnsignedMax().lshr(Other.getUnsignedMin()) + 1;
Nick Lewyckyd385c222010-08-11 22:04:36 +0000918 APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
Craig Topper79b76662017-05-09 07:04:02 +0000919 if (min == max)
Nick Lewyckyd385c222010-08-11 22:04:36 +0000920 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
921
Craig Topper79b76662017-05-09 07:04:02 +0000922 return ConstantRange(std::move(min), std::move(max));
Nuno Lopes60d5b1c2009-11-12 14:53:53 +0000923}
924
Owen Anderson1a9078b2010-08-07 05:47:46 +0000925ConstantRange ConstantRange::inverse() const {
Nick Lewycky228f5b42012-01-03 20:33:00 +0000926 if (isFullSet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000927 return ConstantRange(getBitWidth(), /*isFullSet=*/false);
Nick Lewycky228f5b42012-01-03 20:33:00 +0000928 if (isEmptySet())
Nick Lewyckyd385c222010-08-11 22:04:36 +0000929 return ConstantRange(getBitWidth(), /*isFullSet=*/true);
Owen Anderson1a9078b2010-08-07 05:47:46 +0000930 return ConstantRange(Upper, Lower);
931}
932
Dan Gohmandc33ae22009-07-09 23:16:10 +0000933void ConstantRange::print(raw_ostream &OS) const {
Dan Gohman837ada72010-01-26 04:12:55 +0000934 if (isFullSet())
935 OS << "full-set";
936 else if (isEmptySet())
937 OS << "empty-set";
938 else
939 OS << "[" << Lower << "," << Upper << ")";
Dan Gohman5035fbf2009-07-09 22:07:27 +0000940}
941
Don Hinton3e0199f2017-10-12 16:16:06 +0000942#ifdef LLVM_ENABLE_DUMP
Yaron Kereneb2a2542016-01-29 20:50:44 +0000943LLVM_DUMP_METHOD void ConstantRange::dump() const {
David Greenede7b3532010-01-05 01:28:32 +0000944 print(dbgs());
Dan Gohman5035fbf2009-07-09 22:07:27 +0000945}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000946#endif
Peter Collingbourneecdd58f2016-10-21 19:59:26 +0000947
948ConstantRange llvm::getConstantRangeFromMetadata(const MDNode &Ranges) {
949 const unsigned NumRanges = Ranges.getNumOperands() / 2;
950 assert(NumRanges >= 1 && "Must have at least one range!");
951 assert(Ranges.getNumOperands() % 2 == 0 && "Must be a sequence of pairs");
952
953 auto *FirstLow = mdconst::extract<ConstantInt>(Ranges.getOperand(0));
954 auto *FirstHigh = mdconst::extract<ConstantInt>(Ranges.getOperand(1));
955
956 ConstantRange CR(FirstLow->getValue(), FirstHigh->getValue());
957
958 for (unsigned i = 1; i < NumRanges; ++i) {
959 auto *Low = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
960 auto *High = mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
961
962 // Note: unionWith will potentially create a range that contains values not
963 // contained in any of the original N ranges.
964 CR = CR.unionWith(ConstantRange(Low->getValue(), High->getValue()));
965 }
966
967 return CR;
968}