blob: 580ca01ac39bdf35d9fa812a888b2313c18f73f3 [file] [log] [blame]
Chris Lattner645e00d2002-09-01 23:53:36 +00001//===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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"
Chris Lattner67bb7602004-01-12 20:13:04 +000027#include "llvm/Type.h"
Reid Spencer954da372004-07-04 12:19:56 +000028#include <iostream>
29
Chris Lattner2cdd21c2003-12-14 21:35:53 +000030using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000031
Chris Lattnerfc33d302004-03-30 00:20:08 +000032static ConstantIntegral *Next(ConstantIntegral *CI) {
33 if (CI->getType() == Type::BoolTy)
34 return CI == ConstantBool::True ? ConstantBool::False : ConstantBool::True;
35
36 Constant *Result = ConstantExpr::getAdd(CI,
37 ConstantInt::get(CI->getType(), 1));
38 return cast<ConstantIntegral>(Result);
39}
40
Chris Lattner67bb7602004-01-12 20:13:04 +000041static bool LT(ConstantIntegral *A, ConstantIntegral *B) {
Chris Lattnerfc33d302004-03-30 00:20:08 +000042 Constant *C = ConstantExpr::getSetLT(A, B);
Chris Lattner67bb7602004-01-12 20:13:04 +000043 assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
44 return cast<ConstantBool>(C)->getValue();
45}
46
Chris Lattnerfc33d302004-03-30 00:20:08 +000047static bool LTE(ConstantIntegral *A, ConstantIntegral *B) {
48 Constant *C = ConstantExpr::getSetLE(A, B);
Chris Lattner67bb7602004-01-12 20:13:04 +000049 assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
50 return cast<ConstantBool>(C)->getValue();
51}
52
Chris Lattnerfc33d302004-03-30 00:20:08 +000053static bool GT(ConstantIntegral *A, ConstantIntegral *B) { return LT(B, A); }
54
Chris Lattner67bb7602004-01-12 20:13:04 +000055static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
56 return LT(A, B) ? A : B;
57}
58static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
59 return GT(A, B) ? A : B;
60}
61
Chris Lattner645e00d2002-09-01 23:53:36 +000062/// Initialize a full (the default) or empty set for the specified type.
63///
64ConstantRange::ConstantRange(const Type *Ty, bool Full) {
65 assert(Ty->isIntegral() &&
66 "Cannot make constant range of non-integral type!");
67 if (Full)
68 Lower = Upper = ConstantIntegral::getMaxValue(Ty);
69 else
70 Lower = Upper = ConstantIntegral::getMinValue(Ty);
71}
72
Chris Lattnerfc33d302004-03-30 00:20:08 +000073/// Initialize a range to hold the single specified value.
74///
75ConstantRange::ConstantRange(Constant *V)
76 : Lower(cast<ConstantIntegral>(V)), Upper(Next(cast<ConstantIntegral>(V))) {
77}
78
Chris Lattner645e00d2002-09-01 23:53:36 +000079/// Initialize a range of values explicitly... this will assert out if
80/// Lower==Upper and Lower != Min or Max for its type (or if the two constants
81/// have different types)
82///
Chris Lattnerdb813952004-03-29 20:42:49 +000083ConstantRange::ConstantRange(Constant *L, Constant *U)
84 : Lower(cast<ConstantIntegral>(L)), Upper(cast<ConstantIntegral>(U)) {
Chris Lattner645e00d2002-09-01 23:53:36 +000085 assert(Lower->getType() == Upper->getType() &&
86 "Incompatible types for ConstantRange!");
87
88 // Make sure that if L & U are equal that they are either Min or Max...
89 assert((L != U || (L == ConstantIntegral::getMaxValue(L->getType()) ||
90 L == ConstantIntegral::getMinValue(L->getType()))) &&
91 "Lower == Upper, but they aren't min or max for type!");
92}
93
Chris Lattner645e00d2002-09-01 23:53:36 +000094/// Initialize a set of values that all satisfy the condition with C.
95///
96ConstantRange::ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C) {
97 switch (SetCCOpcode) {
98 default: assert(0 && "Invalid SetCC opcode to ConstantRange ctor!");
99 case Instruction::SetEQ: Lower = C; Upper = Next(C); return;
100 case Instruction::SetNE: Upper = C; Lower = Next(C); return;
101 case Instruction::SetLT:
102 Lower = ConstantIntegral::getMinValue(C->getType());
103 Upper = C;
104 return;
105 case Instruction::SetGT:
Chris Lattner645e00d2002-09-01 23:53:36 +0000106 Lower = Next(C);
Chris Lattner20d41292002-09-03 23:12:40 +0000107 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
Chris Lattner645e00d2002-09-01 23:53:36 +0000108 return;
109 case Instruction::SetLE:
110 Lower = ConstantIntegral::getMinValue(C->getType());
111 Upper = Next(C);
112 return;
113 case Instruction::SetGE:
Chris Lattner645e00d2002-09-01 23:53:36 +0000114 Lower = C;
Chris Lattner20d41292002-09-03 23:12:40 +0000115 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
Chris Lattner645e00d2002-09-01 23:53:36 +0000116 return;
117 }
118}
119
120/// getType - Return the LLVM data type of this range.
121///
122const Type *ConstantRange::getType() const { return Lower->getType(); }
123
124/// isFullSet - Return true if this set contains all of the elements possible
125/// for this data-type
126bool ConstantRange::isFullSet() const {
127 return Lower == Upper && Lower == ConstantIntegral::getMaxValue(getType());
128}
129
130/// isEmptySet - Return true if this set contains no members.
131///
132bool ConstantRange::isEmptySet() const {
133 return Lower == Upper && Lower == ConstantIntegral::getMinValue(getType());
134}
135
136/// isWrappedSet - Return true if this set wraps around the top of the range,
137/// for example: [100, 8)
138///
139bool ConstantRange::isWrappedSet() const {
Chris Lattner67bb7602004-01-12 20:13:04 +0000140 return GT(Lower, Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000141}
142
143
144/// getSingleElement - If this set contains a single element, return it,
145/// otherwise return null.
146ConstantIntegral *ConstantRange::getSingleElement() const {
147 if (Upper == Next(Lower)) // Is it a single element range?
148 return Lower;
149 return 0;
150}
151
152/// getSetSize - Return the number of elements in this set.
153///
154uint64_t ConstantRange::getSetSize() const {
155 if (isEmptySet()) return 0;
156 if (getType() == Type::BoolTy) {
157 if (Lower != Upper) // One of T or F in the set...
158 return 1;
159 return 2; // Must be full set...
160 }
161
162 // Simply subtract the bounds...
Chris Lattnerfc33d302004-03-30 00:20:08 +0000163 Constant *Result = ConstantExpr::getSub(Upper, Lower);
Chris Lattnerc07736a2003-07-23 15:22:26 +0000164 return cast<ConstantInt>(Result)->getRawValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000165}
166
Chris Lattnerfc33d302004-03-30 00:20:08 +0000167/// contains - Return true if the specified value is in the set.
168///
169bool ConstantRange::contains(ConstantInt *Val) const {
170 if (Lower == Upper) {
171 if (isFullSet()) return true;
172 return false;
173 }
Chris Lattner645e00d2002-09-01 23:53:36 +0000174
Chris Lattnerfc33d302004-03-30 00:20:08 +0000175 if (!isWrappedSet())
176 return LTE(Lower, Val) && LT(Val, Upper);
177 return LTE(Lower, Val) || LT(Val, Upper);
178}
179
180
181
182/// subtract - Subtract the specified constant from the endpoints of this
183/// constant range.
184ConstantRange ConstantRange::subtract(ConstantInt *CI) const {
185 assert(CI->getType() == getType() && getType()->isInteger() &&
186 "Cannot subtract from different type range or non-integer!");
187 // If the set is empty or full, don't modify the endpoints.
188 if (Lower == Upper) return *this;
189 return ConstantRange(ConstantExpr::getSub(Lower, CI),
190 ConstantExpr::getSub(Upper, CI));
191}
Chris Lattner645e00d2002-09-01 23:53:36 +0000192
193
194// intersect1Wrapped - This helper function is used to intersect two ranges when
195// it is known that LHS is wrapped and RHS isn't.
196//
197static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
198 const ConstantRange &RHS) {
199 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
200
Chris Lattner645e00d2002-09-01 23:53:36 +0000201 // Check to see if we overlap on the Left side of RHS...
202 //
Chris Lattner67bb7602004-01-12 20:13:04 +0000203 if (LT(RHS.getLower(), LHS.getUpper())) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000204 // We do overlap on the left side of RHS, see if we overlap on the right of
205 // RHS...
Chris Lattner67bb7602004-01-12 20:13:04 +0000206 if (GT(RHS.getUpper(), LHS.getLower())) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000207 // Ok, the result overlaps on both the left and right sides. See if the
208 // resultant interval will be smaller if we wrap or not...
209 //
210 if (LHS.getSetSize() < RHS.getSetSize())
211 return LHS;
212 else
213 return RHS;
214
215 } else {
216 // No overlap on the right, just on the left.
217 return ConstantRange(RHS.getLower(), LHS.getUpper());
218 }
219
220 } else {
221 // We don't overlap on the left side of RHS, see if we overlap on the right
222 // of RHS...
Chris Lattner67bb7602004-01-12 20:13:04 +0000223 if (GT(RHS.getUpper(), LHS.getLower())) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000224 // Simple overlap...
225 return ConstantRange(LHS.getLower(), RHS.getUpper());
226 } else {
227 // No overlap...
228 return ConstantRange(LHS.getType(), false);
229 }
230 }
231}
232
Chris Lattner645e00d2002-09-01 23:53:36 +0000233/// intersect - Return the range that results from the intersection of this
234/// range with another range.
235///
236ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
237 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000238 // Handle common special cases
239 if (isEmptySet() || CR.isFullSet()) return *this;
240 if (isFullSet() || CR.isEmptySet()) return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000241
242 if (!isWrappedSet()) {
243 if (!CR.isWrappedSet()) {
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000244 ConstantIntegral *L = Max(Lower, CR.Lower);
245 ConstantIntegral *U = Min(Upper, CR.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000246
Chris Lattner67bb7602004-01-12 20:13:04 +0000247 if (LT(L, U)) // If range isn't empty...
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000248 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000249 else
250 return ConstantRange(getType(), false); // Otherwise, return empty set
251 } else
252 return intersect1Wrapped(CR, *this);
253 } else { // We know "this" is wrapped...
254 if (!CR.isWrappedSet())
255 return intersect1Wrapped(*this, CR);
256 else {
257 // Both ranges are wrapped...
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000258 ConstantIntegral *L = Max(Lower, CR.Lower);
259 ConstantIntegral *U = Min(Upper, CR.Upper);
260 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000261 }
262 }
263 return *this;
264}
265
266/// union - Return the range that results from the union of this range with
267/// another range. The resultant range is guaranteed to include the elements of
268/// both sets, but may contain more. For example, [3, 9) union [12,15) is [3,
269/// 15), which includes 9, 10, and 11, which were not included in either set
270/// before.
271///
272ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
273 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
274
275 assert(0 && "Range union not implemented yet!");
276
277 return *this;
278}
Chris Lattner96f9d722002-09-02 00:18:22 +0000279
Chris Lattnerfc33d302004-03-30 00:20:08 +0000280/// zeroExtend - Return a new range in the specified integer type, which must
281/// be strictly larger than the current type. The returned range will
282/// correspond to the possible range of values if the source range had been
283/// zero extended.
284ConstantRange ConstantRange::zeroExtend(const Type *Ty) const {
285 assert(getLower()->getType()->getPrimitiveSize() < Ty->getPrimitiveSize() &&
286 "Not a value extension");
287 if (isFullSet()) {
288 // Change a source full set into [0, 1 << 8*numbytes)
289 unsigned SrcTySize = getLower()->getType()->getPrimitiveSize();
290 return ConstantRange(Constant::getNullValue(Ty),
291 ConstantUInt::get(Ty, 1ULL << SrcTySize*8));
292 }
293
294 Constant *Lower = getLower();
295 Constant *Upper = getUpper();
296 if (Lower->getType()->isInteger() && !Lower->getType()->isUnsigned()) {
297 // Ensure we are doing a ZERO extension even if the input range is signed.
298 Lower = ConstantExpr::getCast(Lower, Ty->getUnsignedVersion());
299 Upper = ConstantExpr::getCast(Upper, Ty->getUnsignedVersion());
300 }
301
302 return ConstantRange(ConstantExpr::getCast(Lower, Ty),
303 ConstantExpr::getCast(Upper, Ty));
304}
305
306/// truncate - Return a new range in the specified integer type, which must be
307/// strictly smaller than the current type. The returned range will
308/// correspond to the possible range of values if the source range had been
309/// truncated to the specified type.
310ConstantRange ConstantRange::truncate(const Type *Ty) const {
311 assert(getLower()->getType()->getPrimitiveSize() > Ty->getPrimitiveSize() &&
312 "Not a value truncation");
313 uint64_t Size = 1ULL << Ty->getPrimitiveSize()*8;
314 if (isFullSet() || getSetSize() >= Size)
315 return ConstantRange(getType());
316
317 return ConstantRange(ConstantExpr::getCast(getLower(), Ty),
318 ConstantExpr::getCast(getUpper(), Ty));
319}
320
321
Chris Lattner96f9d722002-09-02 00:18:22 +0000322/// print - Print out the bounds to a stream...
323///
324void ConstantRange::print(std::ostream &OS) const {
325 OS << "[" << Lower << "," << Upper << " )";
326}
327
328/// dump - Allow printing from a debugger easily...
329///
330void ConstantRange::dump() const {
331 print(std::cerr);
332}