blob: ba3c4723b46d3d2d89bef6d40881073cca706525 [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"
Bill Wendling6f81b512006-11-28 22:46:12 +000025#include "llvm/Support/Streams.h"
26#include <ostream>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner645e00d2002-09-01 23:53:36 +000029/// Initialize a full (the default) or empty set for the specified type.
30///
Reid Spencerbb626a62007-02-28 22:02:48 +000031ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) :
32 Lower(BitWidth, 0), Upper(BitWidth, 0) {
Chris Lattner645e00d2002-09-01 23:53:36 +000033 if (Full)
Reid Spencer663e7112007-02-28 17:36:23 +000034 Lower = Upper = APInt::getMaxValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000035 else
Reid Spencer663e7112007-02-28 17:36:23 +000036 Lower = Upper = APInt::getMinValue(BitWidth);
Chris Lattner645e00d2002-09-01 23:53:36 +000037}
38
Chris Lattnerfc33d302004-03-30 00:20:08 +000039/// Initialize a range to hold the single specified value.
40///
Reid Spencerdc5c1592007-02-28 18:57:32 +000041ConstantRange::ConstantRange(const APInt & V) : Lower(V), Upper(V + 1) { }
Chris Lattner645e00d2002-09-01 23:53:36 +000042
Reid Spencer663e7112007-02-28 17:36:23 +000043ConstantRange::ConstantRange(const APInt &L, const APInt &U) :
44 Lower(L), Upper(U) {
45 assert(L.getBitWidth() == U.getBitWidth() &&
46 "ConstantRange with unequal bit widths");
47 uint32_t BitWidth = L.getBitWidth();
48 assert((L != U || (L == APInt::getMaxValue(BitWidth) ||
49 L == APInt::getMinValue(BitWidth))) &&
50 "Lower == Upper, but they aren't min or max value!");
51}
52
Chris Lattner645e00d2002-09-01 23:53:36 +000053/// isFullSet - Return true if this set contains all of the elements possible
54/// for this data-type
55bool ConstantRange::isFullSet() const {
Reid Spencerbb626a62007-02-28 22:02:48 +000056 return Lower == Upper && Lower == APInt::getMaxValue(getBitWidth());
Chris Lattner645e00d2002-09-01 23:53:36 +000057}
Misha Brukmanfd939082005-04-21 23:48:37 +000058
Chris Lattner645e00d2002-09-01 23:53:36 +000059/// isEmptySet - Return true if this set contains no members.
60///
61bool ConstantRange::isEmptySet() const {
Reid Spencerbb626a62007-02-28 22:02:48 +000062 return Lower == Upper && Lower == APInt::getMinValue(getBitWidth());
Chris Lattner645e00d2002-09-01 23:53:36 +000063}
64
65/// isWrappedSet - Return true if this set wraps around the top of the range,
66/// for example: [100, 8)
67///
Reid Spencere4d87aa2006-12-23 06:05:41 +000068bool ConstantRange::isWrappedSet(bool isSigned) const {
Reid Spencer663e7112007-02-28 17:36:23 +000069 if (isSigned)
70 return Lower.sgt(Upper);
71 return Lower.ugt(Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +000072}
73
Chris Lattner645e00d2002-09-01 23:53:36 +000074/// getSetSize - Return the number of elements in this set.
75///
Reid Spencer663e7112007-02-28 17:36:23 +000076APInt ConstantRange::getSetSize() const {
77 if (isEmptySet())
Reid Spencerbb626a62007-02-28 22:02:48 +000078 return APInt(getBitWidth(), 0);
79 if (getBitWidth() == 1) {
Chris Lattner645e00d2002-09-01 23:53:36 +000080 if (Lower != Upper) // One of T or F in the set...
Reid Spencerbb626a62007-02-28 22:02:48 +000081 return APInt(2, 1);
82 return APInt(2, 2); // Must be full set...
Chris Lattner645e00d2002-09-01 23:53:36 +000083 }
Misha Brukmanfd939082005-04-21 23:48:37 +000084
Chris Lattner645e00d2002-09-01 23:53:36 +000085 // Simply subtract the bounds...
Reid Spencer663e7112007-02-28 17:36:23 +000086 return Upper - Lower;
Chris Lattner645e00d2002-09-01 23:53:36 +000087}
88
Chris Lattnerfc33d302004-03-30 00:20:08 +000089/// contains - Return true if the specified value is in the set.
90///
Reid Spencer581b0d42007-02-28 19:57:34 +000091bool ConstantRange::contains(const APInt &V, bool isSigned) const {
Chris Lattnerfc33d302004-03-30 00:20:08 +000092 if (Lower == Upper) {
Reid Spencer663e7112007-02-28 17:36:23 +000093 if (isFullSet())
94 return true;
Chris Lattnerfc33d302004-03-30 00:20:08 +000095 return false;
96 }
Chris Lattner645e00d2002-09-01 23:53:36 +000097
Reid Spencere4d87aa2006-12-23 06:05:41 +000098 if (!isWrappedSet(isSigned))
Reid Spencer663e7112007-02-28 17:36:23 +000099 if (isSigned)
100 return Lower.sle(V) && V.slt(Upper);
101 else
102 return Lower.ule(V) && V.ult(Upper);
103 if (isSigned)
104 return Lower.sle(V) || V.slt(Upper);
105 else
106 return Lower.ule(V) || V.ult(Upper);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000107}
108
Chris Lattnerfc33d302004-03-30 00:20:08 +0000109/// subtract - Subtract the specified constant from the endpoints of this
110/// constant range.
Reid Spencer581b0d42007-02-28 19:57:34 +0000111ConstantRange ConstantRange::subtract(const APInt &Val) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000112 assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
Chris Lattnerfc33d302004-03-30 00:20:08 +0000113 // If the set is empty or full, don't modify the endpoints.
Reid Spencer663e7112007-02-28 17:36:23 +0000114 if (Lower == Upper)
115 return *this;
Reid Spencer663e7112007-02-28 17:36:23 +0000116 return ConstantRange(Lower - Val, Upper - Val);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000117}
Chris Lattner645e00d2002-09-01 23:53:36 +0000118
119
120// intersect1Wrapped - This helper function is used to intersect two ranges when
121// it is known that LHS is wrapped and RHS isn't.
122//
Reid Spencer663e7112007-02-28 17:36:23 +0000123ConstantRange
124ConstantRange::intersect1Wrapped(const ConstantRange &LHS,
125 const ConstantRange &RHS, bool isSigned) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000126 assert(LHS.isWrappedSet(isSigned) && !RHS.isWrappedSet(isSigned));
Chris Lattner645e00d2002-09-01 23:53:36 +0000127
Chris Lattner645e00d2002-09-01 23:53:36 +0000128 // Check to see if we overlap on the Left side of RHS...
129 //
Reid Spencer663e7112007-02-28 17:36:23 +0000130 bool LT = (isSigned ? RHS.Lower.slt(LHS.Upper) : RHS.Lower.ult(LHS.Upper));
131 bool GT = (isSigned ? RHS.Upper.sgt(LHS.Lower) : RHS.Upper.ugt(LHS.Lower));
132 if (LT) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000133 // We do overlap on the left side of RHS, see if we overlap on the right of
134 // RHS...
Reid Spencer663e7112007-02-28 17:36:23 +0000135 if (GT) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000136 // Ok, the result overlaps on both the left and right sides. See if the
137 // resultant interval will be smaller if we wrap or not...
138 //
Reid Spencer663e7112007-02-28 17:36:23 +0000139 if (LHS.getSetSize().ult(RHS.getSetSize()))
Chris Lattner645e00d2002-09-01 23:53:36 +0000140 return LHS;
141 else
142 return RHS;
143
144 } else {
145 // No overlap on the right, just on the left.
Reid Spencerdc5c1592007-02-28 18:57:32 +0000146 return ConstantRange(RHS.Lower, LHS.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000147 }
Chris Lattner645e00d2002-09-01 23:53:36 +0000148 } else {
149 // We don't overlap on the left side of RHS, see if we overlap on the right
150 // of RHS...
Reid Spencer663e7112007-02-28 17:36:23 +0000151 if (GT) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000152 // Simple overlap...
Reid Spencerdc5c1592007-02-28 18:57:32 +0000153 return ConstantRange(LHS.Lower, RHS.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000154 } else {
155 // No overlap...
Reid Spencerbb626a62007-02-28 22:02:48 +0000156 return ConstantRange(LHS.getBitWidth(), false);
Chris Lattner645e00d2002-09-01 23:53:36 +0000157 }
158 }
159}
160
Nick Lewycky3e051642007-02-11 00:58:49 +0000161/// intersectWith - Return the range that results from the intersection of this
Chris Lattner645e00d2002-09-01 23:53:36 +0000162/// range with another range.
163///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000164ConstantRange ConstantRange::intersectWith(const ConstantRange &CR,
165 bool isSigned) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000166 assert(getBitWidth() == CR.getBitWidth() &&
167 "ConstantRange types don't agree!");
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000168 // Handle common special cases
Reid Spencer663e7112007-02-28 17:36:23 +0000169 if (isEmptySet() || CR.isFullSet())
170 return *this;
171 if (isFullSet() || CR.isEmptySet())
172 return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000173
Reid Spencere4d87aa2006-12-23 06:05:41 +0000174 if (!isWrappedSet(isSigned)) {
175 if (!CR.isWrappedSet(isSigned)) {
Reid Spencer663e7112007-02-28 17:36:23 +0000176 using namespace APIntOps;
177 APInt L = isSigned ? smax(Lower, CR.Lower) : umax(Lower, CR.Lower);
178 APInt U = isSigned ? smin(Upper, CR.Upper) : umin(Upper, CR.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000179
Reid Spencer663e7112007-02-28 17:36:23 +0000180 if (isSigned ? L.slt(U) : L.ult(U)) // If range isn't empty...
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000181 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000182 else
Reid Spencerbb626a62007-02-28 22:02:48 +0000183 return ConstantRange(getBitWidth(), false);// Otherwise, empty set
Chris Lattner645e00d2002-09-01 23:53:36 +0000184 } else
Reid Spencere4d87aa2006-12-23 06:05:41 +0000185 return intersect1Wrapped(CR, *this, isSigned);
Chris Lattner645e00d2002-09-01 23:53:36 +0000186 } else { // We know "this" is wrapped...
Reid Spencere4d87aa2006-12-23 06:05:41 +0000187 if (!CR.isWrappedSet(isSigned))
188 return intersect1Wrapped(*this, CR, isSigned);
Chris Lattner645e00d2002-09-01 23:53:36 +0000189 else {
190 // Both ranges are wrapped...
Reid Spencer663e7112007-02-28 17:36:23 +0000191 using namespace APIntOps;
192 APInt L = isSigned ? smax(Lower, CR.Lower) : umax(Lower, CR.Lower);
193 APInt U = isSigned ? smin(Upper, CR.Upper) : umin(Upper, CR.Upper);
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000194 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000195 }
196 }
197 return *this;
198}
199
Nick Lewycky3e051642007-02-11 00:58:49 +0000200/// unionWith - Return the range that results from the union of this range with
Chris Lattner645e00d2002-09-01 23:53:36 +0000201/// another range. The resultant range is guaranteed to include the elements of
202/// both sets, but may contain more. For example, [3, 9) union [12,15) is [3,
203/// 15), which includes 9, 10, and 11, which were not included in either set
204/// before.
205///
Reid Spencere4d87aa2006-12-23 06:05:41 +0000206ConstantRange ConstantRange::unionWith(const ConstantRange &CR,
207 bool isSigned) const {
Reid Spencerbb626a62007-02-28 22:02:48 +0000208 assert(getBitWidth() == CR.getBitWidth() &&
209 "ConstantRange types don't agree!");
Chris Lattner645e00d2002-09-01 23:53:36 +0000210
211 assert(0 && "Range union not implemented yet!");
212
213 return *this;
214}
Chris Lattner96f9d722002-09-02 00:18:22 +0000215
Chris Lattnerfc33d302004-03-30 00:20:08 +0000216/// zeroExtend - Return a new range in the specified integer type, which must
217/// be strictly larger than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000218/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000219/// zero extended.
Reid Spencerbb626a62007-02-28 22:02:48 +0000220ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
221 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000222 assert(SrcTySize < DstTySize && "Not a value extension");
Reid Spencerdc5c1592007-02-28 18:57:32 +0000223 if (isFullSet())
Chris Lattnerfc33d302004-03-30 00:20:08 +0000224 // Change a source full set into [0, 1 << 8*numbytes)
Reid Spencerdc5c1592007-02-28 18:57:32 +0000225 return ConstantRange(APInt(DstTySize,0), APInt(DstTySize,1).shl(SrcTySize));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000226
Reid Spencer663e7112007-02-28 17:36:23 +0000227 APInt L = Lower; L.zext(DstTySize);
228 APInt U = Upper; U.zext(DstTySize);
229 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000230}
231
232/// truncate - Return a new range in the specified integer type, which must be
233/// strictly smaller than the current type. The returned range will
Reid Spencere4d87aa2006-12-23 06:05:41 +0000234/// correspond to the possible range of values as if the source range had been
Chris Lattnerfc33d302004-03-30 00:20:08 +0000235/// truncated to the specified type.
Reid Spencerbb626a62007-02-28 22:02:48 +0000236ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
237 unsigned SrcTySize = getBitWidth();
Reid Spencer663e7112007-02-28 17:36:23 +0000238 assert(SrcTySize > DstTySize && "Not a value truncation");
239 APInt Size = APInt::getMaxValue(DstTySize).zext(SrcTySize);
240 if (isFullSet() || getSetSize().ugt(Size))
Reid Spencerbb626a62007-02-28 22:02:48 +0000241 return ConstantRange(DstTySize);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000242
Reid Spencer663e7112007-02-28 17:36:23 +0000243 APInt L = Lower; L.trunc(DstTySize);
244 APInt U = Upper; U.trunc(DstTySize);
245 return ConstantRange(L, U);
Chris Lattnerfc33d302004-03-30 00:20:08 +0000246}
247
Chris Lattner96f9d722002-09-02 00:18:22 +0000248/// print - Print out the bounds to a stream...
249///
250void ConstantRange::print(std::ostream &OS) const {
Reid Spencer663e7112007-02-28 17:36:23 +0000251 OS << "[" << Lower.toStringSigned(10) << ","
252 << Upper.toStringSigned(10) << " )";
Chris Lattner96f9d722002-09-02 00:18:22 +0000253}
254
255/// dump - Allow printing from a debugger easily...
256///
257void ConstantRange::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000258 print(cerr);
Chris Lattner96f9d722002-09-02 00:18:22 +0000259}