blob: c000c73e8b2c9011b323f9fad47c9fb932f58a68 [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 Lattner67bb7602004-01-12 20:13:04 +000025#include "llvm/Constants.h"
Chris Lattner645e00d2002-09-01 23:53:36 +000026#include "llvm/Instruction.h"
Reid Spencere4d87aa2006-12-23 06:05:41 +000027#include "llvm/Instructions.h"
Chris Lattner67bb7602004-01-12 20:13:04 +000028#include "llvm/Type.h"
Reid Spencerc1030572007-01-19 21:13:56 +000029#include "llvm/DerivedTypes.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000030#include "llvm/Support/Streams.h"
31#include <ostream>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattner645e00d2002-09-01 23:53:36 +000034/// Initialize a full (the default) or empty set for the specified type.
35///
Reid Spencer663e7112007-02-28 17:36:23 +000036ConstantRange::ConstantRange(const Type *Ty, bool Full) :
37 Lower(cast<IntegerType>(Ty)->getBitWidth(), 0),
38 Upper(cast<IntegerType>(Ty)->getBitWidth(), 0) {
39 uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth();
Chris Lattner645e00d2002-09-01 23:53:36 +000040 if (Full)
Reid Spencer663e7112007-02-28 17:36:23 +000041 Lower = Upper = APInt::getMaxValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000042 else
Reid Spencer663e7112007-02-28 17:36:23 +000043 Lower = Upper = APInt::getMinValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000044}
45
Chris Lattnerfc33d302004-03-30 00:20:08 +000046/// Initialize a range to hold the single specified value.
47///
Reid Spencerdc5c1592007-02-28 18:57:32 +000048ConstantRange::ConstantRange(const APInt & V) : Lower(V), Upper(V + 1) { }
Chris Lattner645e00d2002-09-01 23:53:36 +000049
Reid Spencer663e7112007-02-28 17:36:23 +000050ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
51 Lower(L), Upper(U) {
52 assert(L.getBitWidth() == U.getBitWidth() &&
53 "ConstantRange with unequal bit widths");
54 uint32_t BitWidth = L.getBitWidth();
55 assert((L != U || (L == APInt::getMaxValue(BitWidth) ||
56 L == APInt::getMinValue(BitWidth))) &&
57 "Lower == Upper, but they aren't min or max value!");
58}
59
Chris Lattner645e00d2002-09-01 23:53:36 +000060/// Initialize a set of values that all satisfy the condition with C.
61///
Reid Spencerdc5c1592007-02-28 18:57:32 +000062ConstantRange::ConstantRange(unsigned short ICmpOpcode, const APInt &C)
63 : Lower(C.getBitWidth(), 0), Upper(C.getBitWidth(), 0) {
64 uint32_t BitWidth = C.getBitWidth();
Reid Spencere4d87aa2006-12-23 06:05:41 +000065 switch (ICmpOpcode) {
66 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencerdc5c1592007-02-28 18:57:32 +000067 case ICmpInst::ICMP_EQ: Lower = C; Upper = C + 1; return;
68 case ICmpInst::ICMP_NE: Upper = C; Lower = C + 1; return;
Reid Spencere4d87aa2006-12-23 06:05:41 +000069 case ICmpInst::ICMP_ULT:
Reid Spencer663e7112007-02-28 17:36:23 +000070 Lower = APInt::getMinValue(BitWidth);
Reid Spencerdc5c1592007-02-28 18:57:32 +000071 Upper = C;
Chris Lattner645e00d2002-09-01 23:53:36 +000072 return;
Reid Spencere4d87aa2006-12-23 06:05:41 +000073 case ICmpInst::ICMP_SLT:
Reid Spencer663e7112007-02-28 17:36:23 +000074 Lower = APInt::getSignedMinValue(BitWidth);
Reid Spencerdc5c1592007-02-28 18:57:32 +000075 Upper = C;
Chris Lattner645e00d2002-09-01 23:53:36 +000076 return;
Reid Spencere4d87aa2006-12-23 06:05:41 +000077 case ICmpInst::ICMP_UGT:
Reid Spencerdc5c1592007-02-28 18:57:32 +000078 Lower = C + 1;
Reid Spencer663e7112007-02-28 17:36:23 +000079 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Reid Spencere4d87aa2006-12-23 06:05:41 +000080 return;
81 case ICmpInst::ICMP_SGT:
Reid Spencerdc5c1592007-02-28 18:57:32 +000082 Lower = C + 1;
Reid Spencer663e7112007-02-28 17:36:23 +000083 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Reid Spencere4d87aa2006-12-23 06:05:41 +000084 return;
85 case ICmpInst::ICMP_ULE:
Reid Spencer663e7112007-02-28 17:36:23 +000086 Lower = APInt::getMinValue(BitWidth);
Reid Spencerdc5c1592007-02-28 18:57:32 +000087 Upper = C + 1;
Chris Lattner645e00d2002-09-01 23:53:36 +000088 return;
Reid Spencere4d87aa2006-12-23 06:05:41 +000089 case ICmpInst::ICMP_SLE:
Reid Spencer663e7112007-02-28 17:36:23 +000090 Lower = APInt::getSignedMinValue(BitWidth);
Reid Spencerdc5c1592007-02-28 18:57:32 +000091 Upper = C + 1;
Reid Spencere4d87aa2006-12-23 06:05:41 +000092 return;
93 case ICmpInst::ICMP_UGE:
Reid Spencerdc5c1592007-02-28 18:57:32 +000094 Lower = C;
Reid Spencer663e7112007-02-28 17:36:23 +000095 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Reid Spencere4d87aa2006-12-23 06:05:41 +000096 return;
97 case ICmpInst::ICMP_SGE:
Reid Spencerdc5c1592007-02-28 18:57:32 +000098 Lower = C;
Reid Spencer663e7112007-02-28 17:36:23 +000099 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Chris Lattner645e00d2002-09-01 23:53:36 +0000100 return;
101 }
102}
103
104/// getType - Return the LLVM data type of this range.
105///
Reid Spencer663e7112007-02-28 17:36:23 +0000106const Type *ConstantRange::getType() const {
107 return IntegerType::get(Lower.getBitWidth());
108}
109
110ConstantInt *ConstantRange::getLower() const {
111 return ConstantInt::get(getType(), Lower);
112}
113
114ConstantInt *ConstantRange::getUpper() const {
115 return ConstantInt::get(getType(), Upper);
116}
Chris Lattner645e00d2002-09-01 23:53:36 +0000117
118/// isFullSet - Return true if this set contains all of the elements possible
119/// for this data-type
120bool ConstantRange::isFullSet() const {
Reid Spencer663e7112007-02-28 17:36:23 +0000121 return Lower == Upper && Lower == APInt::getMaxValue(Lower.getBitWidth());
Chris Lattner645e00d2002-09-01 23:53:36 +0000122}
Misha Brukmanfd939082005-04-21 23:48:37 +0000123
Chris Lattner645e00d2002-09-01 23:53:36 +0000124/// isEmptySet - Return true if this set contains no members.
125///
126bool ConstantRange::isEmptySet() const {
Reid Spencer663e7112007-02-28 17:36:23 +0000127 return Lower == Upper && Lower == APInt::getMinValue(Lower.getBitWidth());
Chris Lattner645e00d2002-09-01 23:53:36 +0000128}
129
130/// isWrappedSet - Return true if this set wraps around the top of the range,
131/// for example: [100, 8)
132///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000133bool ConstantRange::isWrappedSet(bool isSigned) const {
Reid Spencer663e7112007-02-28 17:36:23 +0000134 if (isSigned)
135 return Lower.sgt(Upper);
136 return Lower.ugt(Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000137}
138
Chris Lattner645e00d2002-09-01 23:53:36 +0000139/// getSingleElement - If this set contains a single element, return it,
140/// otherwise return null.
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000141ConstantInt *ConstantRange::getSingleElement() const {
Reid Spencer663e7112007-02-28 17:36:23 +0000142 if (Upper == Lower + 1) // Is it a single element range?
143 return ConstantInt::get(getType(), Lower);
Chris Lattner645e00d2002-09-01 23:53:36 +0000144 return 0;
145}
146
147/// getSetSize - Return the number of elements in this set.
148///
Reid Spencer663e7112007-02-28 17:36:23 +0000149APInt ConstantRange::getSetSize() const {
150 if (isEmptySet())
151 return APInt(Lower.getBitWidth(), 0);
Reid Spencer4fe16d62007-01-11 18:21:29 +0000152 if (getType() == Type::Int1Ty) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000153 if (Lower != Upper) // One of T or F in the set...
Reid Spencer663e7112007-02-28 17:36:23 +0000154 return APInt(Lower.getBitWidth(), 1);
155 return APInt(Lower.getBitWidth(), 2); // Must be full set...
Chris Lattner645e00d2002-09-01 23:53:36 +0000156 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000157
Chris Lattner645e00d2002-09-01 23:53:36 +0000158 // Simply subtract the bounds...
Reid Spencer663e7112007-02-28 17:36:23 +0000159 return Upper - Lower;
Chris Lattner645e00d2002-09-01 23:53:36 +0000160}
161
Chris Lattnerfc33d302004-03-30 00:20:08 +0000162/// contains - Return true if the specified value is in the set.
163///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000164bool ConstantRange::contains(ConstantInt *Val, bool isSigned) const {
Chris Lattnerfc33d302004-03-30 00:20:08 +0000165 if (Lower == Upper) {
Reid Spencer663e7112007-02-28 17:36:23 +0000166 if (isFullSet())
167 return true;
Chris Lattnerfc33d302004-03-30 00:20:08 +0000168 return false;
169 }
Chris Lattner645e00d2002-09-01 23:53:36 +0000170
Reid Spencer663e7112007-02-28 17:36:23 +0000171 const APInt &V = Val->getValue();
Reid Spencere4d87aa2006-12-23 06:05:41 +0000172 if (!isWrappedSet(isSigned))
Reid Spencer663e7112007-02-28 17:36:23 +0000173 if (isSigned)
174 return Lower.sle(V) && V.slt(Upper);
175 else
176 return Lower.ule(V) && V.ult(Upper);
177 if (isSigned)
178 return Lower.sle(V) || V.slt(Upper);
179 else
180 return Lower.ule(V) || V.ult(Upper);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000181}
182
Chris Lattnerfc33d302004-03-30 00:20:08 +0000183/// subtract - Subtract the specified constant from the endpoints of this
184/// constant range.
185ConstantRange ConstantRange::subtract(ConstantInt *CI) const {
Reid Spencer663e7112007-02-28 17:36:23 +0000186 assert(CI->getType() == getType() &&
Chris Lattnerfc33d302004-03-30 00:20:08 +0000187 "Cannot subtract from different type range or non-integer!");
188 // If the set is empty or full, don't modify the endpoints.
Reid Spencer663e7112007-02-28 17:36:23 +0000189 if (Lower == Upper)
190 return *this;
191
192 const APInt &Val = CI->getValue();
193 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000194}
Chris Lattner645e00d2002-09-01 23:53:36 +0000195
196
197// intersect1Wrapped - This helper function is used to intersect two ranges when
198// it is known that LHS is wrapped and RHS isn't.
199//
Reid Spencer663e7112007-02-28 17:36:23 +0000200ConstantRange
201ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
202 const ConstantRange &RHS, bool isSigned) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000203 assert(LHS.isWrappedSet(isSigned) && !RHS.isWrappedSet(isSigned));
Chris Lattner645e00d2002-09-01 23:53:36 +0000204
Chris Lattner645e00d2002-09-01 23:53:36 +0000205 // Check to see if we overlap on the Left side of RHS...
206 //
Reid Spencer663e7112007-02-28 17:36:23 +0000207 bool LT = (isSigned ? RHS.Lower.slt(LHS.Upper) : RHS.Lower.ult(LHS.Upper));
208 bool GT = (isSigned ? RHS.Upper.sgt(LHS.Lower) : RHS.Upper.ugt(LHS.Lower));
209 if (LT) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000210 // We do overlap on the left side of RHS, see if we overlap on the right of
211 // RHS...
Reid Spencer663e7112007-02-28 17:36:23 +0000212 if (GT) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000213 // Ok, the result overlaps on both the left and right sides. See if the
214 // resultant interval will be smaller if we wrap or not...
215 //
Reid Spencer663e7112007-02-28 17:36:23 +0000216 if (LHS.getSetSize().ult(RHS.getSetSize()))
Chris Lattner645e00d2002-09-01 23:53:36 +0000217 return LHS;
218 else
219 return RHS;
220
221 } else {
222 // No overlap on the right, just on the left.
Reid Spencerdc5c1592007-02-28 18:57:32 +0000223 return ConstantRange(RHS.Lower, LHS.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000224 }
Chris Lattner645e00d2002-09-01 23:53:36 +0000225 } else {
226 // We don't overlap on the left side of RHS, see if we overlap on the right
227 // of RHS...
Reid Spencer663e7112007-02-28 17:36:23 +0000228 if (GT) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000229 // Simple overlap...
Reid Spencerdc5c1592007-02-28 18:57:32 +0000230 return ConstantRange(LHS.Lower, RHS.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000231 } else {
232 // No overlap...
233 return ConstantRange(LHS.getType(), false);
234 }
235 }
236}
237
Nick Lewycky3e051642007-02-11 00:58:49 +0000238/// intersectWith - Return the range that results from the intersection of this
Chris Lattner645e00d2002-09-01 23:53:36 +0000239/// range with another range.
240///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000241ConstantRange ConstantRange::intersectWith(const ConstantRange &CR,
242 bool isSigned) const {
Chris Lattner645e00d2002-09-01 23:53:36 +0000243 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000244 // Handle common special cases
Reid Spencer663e7112007-02-28 17:36:23 +0000245 if (isEmptySet() || CR.isFullSet())
246 return *this;
247 if (isFullSet() || CR.isEmptySet())
248 return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000249
Reid Spencere4d87aa2006-12-23 06:05:41 +0000250 if (!isWrappedSet(isSigned)) {
251 if (!CR.isWrappedSet(isSigned)) {
Reid Spencer663e7112007-02-28 17:36:23 +0000252 using namespace APIntOps;
253 APInt L = isSigned ? smax(Lower, CR.Lower) : umax(Lower, CR.Lower);
254 APInt U = isSigned ? smin(Upper, CR.Upper) : umin(Upper, CR.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000255
Reid Spencer663e7112007-02-28 17:36:23 +0000256 if (isSigned ? L.slt(U) : L.ult(U)) // If range isn't empty...
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000257 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000258 else
259 return ConstantRange(getType(), false); // Otherwise, return empty set
260 } else
Reid Spencere4d87aa2006-12-23 06:05:41 +0000261 return intersect1Wrapped(CR, *this, isSigned);
Chris Lattner645e00d2002-09-01 23:53:36 +0000262 } else { // We know "this" is wrapped...
Reid Spencere4d87aa2006-12-23 06:05:41 +0000263 if (!CR.isWrappedSet(isSigned))
264 return intersect1Wrapped(*this, CR, isSigned);
Chris Lattner645e00d2002-09-01 23:53:36 +0000265 else {
266 // Both ranges are wrapped...
Reid Spencer663e7112007-02-28 17:36:23 +0000267 using namespace APIntOps;
268 APInt L = isSigned ? smax(Lower, CR.Lower) : umax(Lower, CR.Lower);
269 APInt U = isSigned ? smin(Upper, CR.Upper) : umin(Upper, CR.Upper);
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000270 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000271 }
272 }
273 return *this;
274}
275
Nick Lewycky3e051642007-02-11 00:58:49 +0000276/// unionWith - Return the range that results from the union of this range with
Chris Lattner645e00d2002-09-01 23:53:36 +0000277/// another range. The resultant range is guaranteed to include the elements of
278/// both sets, but may contain more. For example, [3, 9) union [12,15) is [3,
279/// 15), which includes 9, 10, and 11, which were not included in either set
280/// before.
281///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000282ConstantRange ConstantRange::unionWith(const ConstantRange &CR,
283 bool isSigned) const {
Chris Lattner645e00d2002-09-01 23:53:36 +0000284 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
285
286 assert(0 && "Range union not implemented yet!");
287
288 return *this;
289}
Chris Lattner96f9d722002-09-02 00:18:22 +0000290
Chris Lattnerfc33d302004-03-30 00:20:08 +0000291/// zeroExtend - Return a new range in the specified integer type, which must
292/// be strictly larger than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000293/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000294/// zero extended.
295ConstantRange ConstantRange::zeroExtend(const Type *Ty) const {
Reid Spencer663e7112007-02-28 17:36:23 +0000296 unsigned SrcTySize = Lower.getBitWidth();
297 unsigned DstTySize = Ty->getPrimitiveSizeInBits();
298 assert(SrcTySize < DstTySize && "Not a value extension");
Reid Spencerdc5c1592007-02-28 18:57:32 +0000299 if (isFullSet())
Chris Lattnerfc33d302004-03-30 00:20:08 +0000300 // Change a source full set into [0, 1 << 8*numbytes)
Reid Spencerdc5c1592007-02-28 18:57:32 +0000301 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000302
Reid Spencer663e7112007-02-28 17:36:23 +0000303 APInt L = Lower; L.zext(DstTySize);
304 APInt U = Upper; U.zext(DstTySize);
305 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000306}
307
308/// truncate - Return a new range in the specified integer type, which must be
309/// strictly smaller than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000310/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000311/// truncated to the specified type.
312ConstantRange ConstantRange::truncate(const Type *Ty) const {
Reid Spencer663e7112007-02-28 17:36:23 +0000313 unsigned SrcTySize = Lower.getBitWidth();
314 unsigned DstTySize = Ty->getPrimitiveSizeInBits();
315 assert(SrcTySize > DstTySize && "Not a value truncation");
316 APInt Size = APInt::getMaxValue(DstTySize).zext(SrcTySize);
317 if (isFullSet() || getSetSize().ugt(Size))
Chris Lattnerfc33d302004-03-30 00:20:08 +0000318 return ConstantRange(getType());
319
Reid Spencer663e7112007-02-28 17:36:23 +0000320 APInt L = Lower; L.trunc(DstTySize);
321 APInt U = Upper; U.trunc(DstTySize);
322 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000323}
324
Chris Lattner96f9d722002-09-02 00:18:22 +0000325/// print - Print out the bounds to a stream...
326///
327void ConstantRange::print(std::ostream &OS) const {
Reid Spencer663e7112007-02-28 17:36:23 +0000328 OS << "[" << Lower.toStringSigned(10) << ","
329 << Upper.toStringSigned(10) << " )";
Chris Lattner96f9d722002-09-02 00:18:22 +0000330}
331
332/// dump - Allow printing from a debugger easily...
333///
334void ConstantRange::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000335 print(cerr);
Chris Lattner96f9d722002-09-02 00:18:22 +0000336}