blob: f419edcf9b94b94455441b56f1965f7d34dabc1c [file] [log] [blame]
Chris Lattner645e00d2002-09-01 23:53:36 +00001//===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner645e00d2002-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
24#include "llvm/Support/ConstantRange.h"
Chris Lattner645e00d2002-09-01 23:53:36 +000025#include "llvm/Instruction.h"
Reid Spencere4d87aa2006-12-23 06:05:41 +000026#include "llvm/Instructions.h"
Chris Lattner67bb7602004-01-12 20:13:04 +000027#include "llvm/Type.h"
Reid Spencerc1030572007-01-19 21:13:56 +000028#include "llvm/DerivedTypes.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000029#include "llvm/Support/Streams.h"
30#include <ostream>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattner645e00d2002-09-01 23:53:36 +000033/// Initialize a full (the default) or empty set for the specified type.
34///
Reid Spencer663e7112007-02-28 17:36:23 +000035ConstantRange::ConstantRange(const Type *Ty, bool Full) :
36 Lower(cast<IntegerType>(Ty)->getBitWidth(), 0),
37 Upper(cast<IntegerType>(Ty)->getBitWidth(), 0) {
38 uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth();
Chris Lattner645e00d2002-09-01 23:53:36 +000039 if (Full)
Reid Spencer663e7112007-02-28 17:36:23 +000040 Lower = Upper = APInt::getMaxValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000041 else
Reid Spencer663e7112007-02-28 17:36:23 +000042 Lower = Upper = APInt::getMinValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000043}
44
Chris Lattnerfc33d302004-03-30 00:20:08 +000045/// Initialize a range to hold the single specified value.
46///
Reid Spencerdc5c1592007-02-28 18:57:32 +000047ConstantRange::ConstantRange(const APInt & V) : Lower(V), Upper(V + 1) { }
Chris Lattner645e00d2002-09-01 23:53:36 +000048
Reid Spencer663e7112007-02-28 17:36:23 +000049ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
50 Lower(L), Upper(U) {
51 assert(L.getBitWidth() == U.getBitWidth() &&
52 "ConstantRange with unequal bit widths");
53 uint32_t BitWidth = L.getBitWidth();
54 assert((L != U || (L == APInt::getMaxValue(BitWidth) ||
55 L == APInt::getMinValue(BitWidth))) &&
56 "Lower == Upper, but they aren't min or max value!");
57}
58
Chris Lattner645e00d2002-09-01 23:53:36 +000059/// Initialize a set of values that all satisfy the condition with C.
60///
Reid Spencerdc5c1592007-02-28 18:57:32 +000061ConstantRange::ConstantRange(unsigned short ICmpOpcode, const APInt &C)
62 : Lower(C.getBitWidth(), 0), Upper(C.getBitWidth(), 0) {
63 uint32_t BitWidth = C.getBitWidth();
Reid Spencere4d87aa2006-12-23 06:05:41 +000064 switch (ICmpOpcode) {
65 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencerdc5c1592007-02-28 18:57:32 +000066 case ICmpInst::ICMP_EQ: Lower = C; Upper = C + 1; return;
67 case ICmpInst::ICMP_NE: Upper = C; Lower = C + 1; return;
Reid Spencere4d87aa2006-12-23 06:05:41 +000068 case ICmpInst::ICMP_ULT:
Reid Spencer663e7112007-02-28 17:36:23 +000069 Lower = APInt::getMinValue(BitWidth);
Reid Spencerdc5c1592007-02-28 18:57:32 +000070 Upper = C;
Chris Lattner645e00d2002-09-01 23:53:36 +000071 return;
Reid Spencere4d87aa2006-12-23 06:05:41 +000072 case ICmpInst::ICMP_SLT:
Reid Spencer663e7112007-02-28 17:36:23 +000073 Lower = APInt::getSignedMinValue(BitWidth);
Reid Spencerdc5c1592007-02-28 18:57:32 +000074 Upper = C;
Chris Lattner645e00d2002-09-01 23:53:36 +000075 return;
Reid Spencere4d87aa2006-12-23 06:05:41 +000076 case ICmpInst::ICMP_UGT:
Reid Spencerdc5c1592007-02-28 18:57:32 +000077 Lower = C + 1;
Reid Spencer663e7112007-02-28 17:36:23 +000078 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Reid Spencere4d87aa2006-12-23 06:05:41 +000079 return;
80 case ICmpInst::ICMP_SGT:
Reid Spencerdc5c1592007-02-28 18:57:32 +000081 Lower = C + 1;
Reid Spencer663e7112007-02-28 17:36:23 +000082 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Reid Spencere4d87aa2006-12-23 06:05:41 +000083 return;
84 case ICmpInst::ICMP_ULE:
Reid Spencer663e7112007-02-28 17:36:23 +000085 Lower = APInt::getMinValue(BitWidth);
Reid Spencerdc5c1592007-02-28 18:57:32 +000086 Upper = C + 1;
Chris Lattner645e00d2002-09-01 23:53:36 +000087 return;
Reid Spencere4d87aa2006-12-23 06:05:41 +000088 case ICmpInst::ICMP_SLE:
Reid Spencer663e7112007-02-28 17:36:23 +000089 Lower = APInt::getSignedMinValue(BitWidth);
Reid Spencerdc5c1592007-02-28 18:57:32 +000090 Upper = C + 1;
Reid Spencere4d87aa2006-12-23 06:05:41 +000091 return;
92 case ICmpInst::ICMP_UGE:
Reid Spencerdc5c1592007-02-28 18:57:32 +000093 Lower = C;
Reid Spencer663e7112007-02-28 17:36:23 +000094 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Reid Spencere4d87aa2006-12-23 06:05:41 +000095 return;
96 case ICmpInst::ICMP_SGE:
Reid Spencerdc5c1592007-02-28 18:57:32 +000097 Lower = C;
Reid Spencer663e7112007-02-28 17:36:23 +000098 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Chris Lattner645e00d2002-09-01 23:53:36 +000099 return;
100 }
101}
102
103/// getType - Return the LLVM data type of this range.
104///
Reid Spencer663e7112007-02-28 17:36:23 +0000105const Type *ConstantRange::getType() const {
106 return IntegerType::get(Lower.getBitWidth());
107}
108
Chris Lattner645e00d2002-09-01 23:53:36 +0000109/// isFullSet - Return true if this set contains all of the elements possible
110/// for this data-type
111bool ConstantRange::isFullSet() const {
Reid Spencer663e7112007-02-28 17:36:23 +0000112 return Lower == Upper && Lower == APInt::getMaxValue(Lower.getBitWidth());
Chris Lattner645e00d2002-09-01 23:53:36 +0000113}
Misha Brukmanfd939082005-04-21 23:48:37 +0000114
Chris Lattner645e00d2002-09-01 23:53:36 +0000115/// isEmptySet - Return true if this set contains no members.
116///
117bool ConstantRange::isEmptySet() const {
Reid Spencer663e7112007-02-28 17:36:23 +0000118 return Lower == Upper && Lower == APInt::getMinValue(Lower.getBitWidth());
Chris Lattner645e00d2002-09-01 23:53:36 +0000119}
120
121/// isWrappedSet - Return true if this set wraps around the top of the range,
122/// for example: [100, 8)
123///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000124bool ConstantRange::isWrappedSet(bool isSigned) const {
Reid Spencer663e7112007-02-28 17:36:23 +0000125 if (isSigned)
126 return Lower.sgt(Upper);
127 return Lower.ugt(Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000128}
129
Chris Lattner645e00d2002-09-01 23:53:36 +0000130/// getSetSize - Return the number of elements in this set.
131///
Reid Spencer663e7112007-02-28 17:36:23 +0000132APInt ConstantRange::getSetSize() const {
133 if (isEmptySet())
134 return APInt(Lower.getBitWidth(), 0);
Reid Spencer4fe16d62007-01-11 18:21:29 +0000135 if (getType() == Type::Int1Ty) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000136 if (Lower != Upper) // One of T or F in the set...
Reid Spencer663e7112007-02-28 17:36:23 +0000137 return APInt(Lower.getBitWidth(), 1);
138 return APInt(Lower.getBitWidth(), 2); // Must be full set...
Chris Lattner645e00d2002-09-01 23:53:36 +0000139 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000140
Chris Lattner645e00d2002-09-01 23:53:36 +0000141 // Simply subtract the bounds...
Reid Spencer663e7112007-02-28 17:36:23 +0000142 return Upper - Lower;
Chris Lattner645e00d2002-09-01 23:53:36 +0000143}
144
Chris Lattnerfc33d302004-03-30 00:20:08 +0000145/// contains - Return true if the specified value is in the set.
146///
Reid Spencer581b0d42007-02-28 19:57:34 +0000147bool ConstantRange::contains(const APInt &V, bool isSigned) const {
Chris Lattnerfc33d302004-03-30 00:20:08 +0000148 if (Lower == Upper) {
Reid Spencer663e7112007-02-28 17:36:23 +0000149 if (isFullSet())
150 return true;
Chris Lattnerfc33d302004-03-30 00:20:08 +0000151 return false;
152 }
Chris Lattner645e00d2002-09-01 23:53:36 +0000153
Reid Spencere4d87aa2006-12-23 06:05:41 +0000154 if (!isWrappedSet(isSigned))
Reid Spencer663e7112007-02-28 17:36:23 +0000155 if (isSigned)
156 return Lower.sle(V) && V.slt(Upper);
157 else
158 return Lower.ule(V) && V.ult(Upper);
159 if (isSigned)
160 return Lower.sle(V) || V.slt(Upper);
161 else
162 return Lower.ule(V) || V.ult(Upper);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000163}
164
Chris Lattnerfc33d302004-03-30 00:20:08 +0000165/// subtract - Subtract the specified constant from the endpoints of this
166/// constant range.
Reid Spencer581b0d42007-02-28 19:57:34 +0000167ConstantRange ConstantRange::subtract(const APInt &Val) const {
168 assert(Val.getBitWidth() == Lower.getBitWidth() && "Wrong bit width");
Chris Lattnerfc33d302004-03-30 00:20:08 +0000169 // If the set is empty or full, don't modify the endpoints.
Reid Spencer663e7112007-02-28 17:36:23 +0000170 if (Lower == Upper)
171 return *this;
Reid Spencer663e7112007-02-28 17:36:23 +0000172 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000173}
Chris Lattner645e00d2002-09-01 23:53:36 +0000174
175
176// intersect1Wrapped - This helper function is used to intersect two ranges when
177// it is known that LHS is wrapped and RHS isn't.
178//
Reid Spencer663e7112007-02-28 17:36:23 +0000179ConstantRange
180ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
181 const ConstantRange &RHS, bool isSigned) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000182 assert(LHS.isWrappedSet(isSigned) && !RHS.isWrappedSet(isSigned));
Chris Lattner645e00d2002-09-01 23:53:36 +0000183
Chris Lattner645e00d2002-09-01 23:53:36 +0000184 // Check to see if we overlap on the Left side of RHS...
185 //
Reid Spencer663e7112007-02-28 17:36:23 +0000186 bool LT = (isSigned ? RHS.Lower.slt(LHS.Upper) : RHS.Lower.ult(LHS.Upper));
187 bool GT = (isSigned ? RHS.Upper.sgt(LHS.Lower) : RHS.Upper.ugt(LHS.Lower));
188 if (LT) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000189 // We do overlap on the left side of RHS, see if we overlap on the right of
190 // RHS...
Reid Spencer663e7112007-02-28 17:36:23 +0000191 if (GT) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000192 // Ok, the result overlaps on both the left and right sides. See if the
193 // resultant interval will be smaller if we wrap or not...
194 //
Reid Spencer663e7112007-02-28 17:36:23 +0000195 if (LHS.getSetSize().ult(RHS.getSetSize()))
Chris Lattner645e00d2002-09-01 23:53:36 +0000196 return LHS;
197 else
198 return RHS;
199
200 } else {
201 // No overlap on the right, just on the left.
Reid Spencerdc5c1592007-02-28 18:57:32 +0000202 return ConstantRange(RHS.Lower, LHS.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000203 }
Chris Lattner645e00d2002-09-01 23:53:36 +0000204 } else {
205 // We don't overlap on the left side of RHS, see if we overlap on the right
206 // of RHS...
Reid Spencer663e7112007-02-28 17:36:23 +0000207 if (GT) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000208 // Simple overlap...
Reid Spencerdc5c1592007-02-28 18:57:32 +0000209 return ConstantRange(LHS.Lower, RHS.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000210 } else {
211 // No overlap...
212 return ConstantRange(LHS.getType(), false);
213 }
214 }
215}
216
Nick Lewycky3e051642007-02-11 00:58:49 +0000217/// intersectWith - Return the range that results from the intersection of this
Chris Lattner645e00d2002-09-01 23:53:36 +0000218/// range with another range.
219///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000220ConstantRange ConstantRange::intersectWith(const ConstantRange &CR,
221 bool isSigned) const {
Chris Lattner645e00d2002-09-01 23:53:36 +0000222 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000223 // Handle common special cases
Reid Spencer663e7112007-02-28 17:36:23 +0000224 if (isEmptySet() || CR.isFullSet())
225 return *this;
226 if (isFullSet() || CR.isEmptySet())
227 return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000228
Reid Spencere4d87aa2006-12-23 06:05:41 +0000229 if (!isWrappedSet(isSigned)) {
230 if (!CR.isWrappedSet(isSigned)) {
Reid Spencer663e7112007-02-28 17:36:23 +0000231 using namespace APIntOps;
232 APInt L = isSigned ? smax(Lower, CR.Lower) : umax(Lower, CR.Lower);
233 APInt U = isSigned ? smin(Upper, CR.Upper) : umin(Upper, CR.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000234
Reid Spencer663e7112007-02-28 17:36:23 +0000235 if (isSigned ? L.slt(U) : L.ult(U)) // If range isn't empty...
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000236 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000237 else
238 return ConstantRange(getType(), false); // Otherwise, return empty set
239 } else
Reid Spencere4d87aa2006-12-23 06:05:41 +0000240 return intersect1Wrapped(CR, *this, isSigned);
Chris Lattner645e00d2002-09-01 23:53:36 +0000241 } else { // We know "this" is wrapped...
Reid Spencere4d87aa2006-12-23 06:05:41 +0000242 if (!CR.isWrappedSet(isSigned))
243 return intersect1Wrapped(*this, CR, isSigned);
Chris Lattner645e00d2002-09-01 23:53:36 +0000244 else {
245 // Both ranges are wrapped...
Reid Spencer663e7112007-02-28 17:36:23 +0000246 using namespace APIntOps;
247 APInt L = isSigned ? smax(Lower, CR.Lower) : umax(Lower, CR.Lower);
248 APInt U = isSigned ? smin(Upper, CR.Upper) : umin(Upper, CR.Upper);
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000249 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000250 }
251 }
252 return *this;
253}
254
Nick Lewycky3e051642007-02-11 00:58:49 +0000255/// unionWith - Return the range that results from the union of this range with
Chris Lattner645e00d2002-09-01 23:53:36 +0000256/// another range. The resultant range is guaranteed to include the elements of
257/// both sets, but may contain more. For example, [3, 9) union [12,15) is [3,
258/// 15), which includes 9, 10, and 11, which were not included in either set
259/// before.
260///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000261ConstantRange ConstantRange::unionWith(const ConstantRange &CR,
262 bool isSigned) const {
Chris Lattner645e00d2002-09-01 23:53:36 +0000263 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
264
265 assert(0 && "Range union not implemented yet!");
266
267 return *this;
268}
Chris Lattner96f9d722002-09-02 00:18:22 +0000269
Chris Lattnerfc33d302004-03-30 00:20:08 +0000270/// zeroExtend - Return a new range in the specified integer type, which must
271/// be strictly larger than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000272/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000273/// zero extended.
274ConstantRange ConstantRange::zeroExtend(const Type *Ty) const {
Reid Spencer663e7112007-02-28 17:36:23 +0000275 unsigned SrcTySize = Lower.getBitWidth();
276 unsigned DstTySize = Ty->getPrimitiveSizeInBits();
277 assert(SrcTySize < DstTySize && "Not a value extension");
Reid Spencerdc5c1592007-02-28 18:57:32 +0000278 if (isFullSet())
Chris Lattnerfc33d302004-03-30 00:20:08 +0000279 // Change a source full set into [0, 1 << 8*numbytes)
Reid Spencerdc5c1592007-02-28 18:57:32 +0000280 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000281
Reid Spencer663e7112007-02-28 17:36:23 +0000282 APInt L = Lower; L.zext(DstTySize);
283 APInt U = Upper; U.zext(DstTySize);
284 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000285}
286
287/// truncate - Return a new range in the specified integer type, which must be
288/// strictly smaller than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000289/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000290/// truncated to the specified type.
291ConstantRange ConstantRange::truncate(const Type *Ty) const {
Reid Spencer663e7112007-02-28 17:36:23 +0000292 unsigned SrcTySize = Lower.getBitWidth();
293 unsigned DstTySize = Ty->getPrimitiveSizeInBits();
294 assert(SrcTySize > DstTySize && "Not a value truncation");
295 APInt Size = APInt::getMaxValue(DstTySize).zext(SrcTySize);
296 if (isFullSet() || getSetSize().ugt(Size))
Chris Lattnerfc33d302004-03-30 00:20:08 +0000297 return ConstantRange(getType());
298
Reid Spencer663e7112007-02-28 17:36:23 +0000299 APInt L = Lower; L.trunc(DstTySize);
300 APInt U = Upper; U.trunc(DstTySize);
301 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000302}
303
Chris Lattner96f9d722002-09-02 00:18:22 +0000304/// print - Print out the bounds to a stream...
305///
306void ConstantRange::print(std::ostream &OS) const {
Reid Spencer663e7112007-02-28 17:36:23 +0000307 OS << "[" << Lower.toStringSigned(10) << ","
308 << Upper.toStringSigned(10) << " )";
Chris Lattner96f9d722002-09-02 00:18:22 +0000309}
310
311/// dump - Allow printing from a debugger easily...
312///
313void ConstantRange::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000314 print(cerr);
Chris Lattner96f9d722002-09-02 00:18:22 +0000315}