blob: 21eabe2aca6a94ee8fcec404f29a5acc35d7d922 [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"
Chris Lattner67bb7602004-01-12 20:13:04 +000027#include "llvm/Type.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000028#include "llvm/Support/Streams.h"
29#include <ostream>
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) {
Chris Lattner193c2d82006-09-28 23:14:29 +000033 if (ConstantBool *CB = dyn_cast<ConstantBool>(CI))
34 return ConstantBool::get(!CB->getValue());
Misha Brukmanfd939082005-04-21 23:48:37 +000035
Chris Lattnerfc33d302004-03-30 00:20:08 +000036 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
Bill Wendling6f81b512006-11-28 22:46:12 +000053
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
Chris Lattnerfc33d302004-03-30 00:20:08 +000079static bool GT(ConstantIntegral *A, ConstantIntegral *B) { return LT(B, A); }
80
Chris Lattner67bb7602004-01-12 20:13:04 +000081static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
82 return LT(A, B) ? A : B;
83}
84static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
85 return GT(A, B) ? A : B;
86}
87
Chris Lattner645e00d2002-09-01 23:53:36 +000088/// Initialize a full (the default) or empty set for the specified type.
89///
90ConstantRange::ConstantRange(const Type *Ty, bool Full) {
91 assert(Ty->isIntegral() &&
92 "Cannot make constant range of non-integral type!");
93 if (Full)
94 Lower = Upper = ConstantIntegral::getMaxValue(Ty);
95 else
96 Lower = Upper = ConstantIntegral::getMinValue(Ty);
97}
98
Chris Lattnerfc33d302004-03-30 00:20:08 +000099/// Initialize a range to hold the single specified value.
100///
101ConstantRange::ConstantRange(Constant *V)
102 : Lower(cast<ConstantIntegral>(V)), Upper(Next(cast<ConstantIntegral>(V))) {
103}
104
Chris Lattner645e00d2002-09-01 23:53:36 +0000105/// Initialize a range of values explicitly... this will assert out if
106/// Lower==Upper and Lower != Min or Max for its type (or if the two constants
107/// have different types)
108///
Chris Lattnerdb813952004-03-29 20:42:49 +0000109ConstantRange::ConstantRange(Constant *L, Constant *U)
110 : Lower(cast<ConstantIntegral>(L)), Upper(cast<ConstantIntegral>(U)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000111 assert(Lower->getType() == Upper->getType() &&
112 "Incompatible types for ConstantRange!");
Misha Brukmanfd939082005-04-21 23:48:37 +0000113
Chris Lattner645e00d2002-09-01 23:53:36 +0000114 // Make sure that if L & U are equal that they are either Min or Max...
115 assert((L != U || (L == ConstantIntegral::getMaxValue(L->getType()) ||
116 L == ConstantIntegral::getMinValue(L->getType()))) &&
117 "Lower == Upper, but they aren't min or max for type!");
118}
119
Chris Lattner645e00d2002-09-01 23:53:36 +0000120/// Initialize a set of values that all satisfy the condition with C.
121///
122ConstantRange::ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C) {
123 switch (SetCCOpcode) {
124 default: assert(0 && "Invalid SetCC opcode to ConstantRange ctor!");
125 case Instruction::SetEQ: Lower = C; Upper = Next(C); return;
126 case Instruction::SetNE: Upper = C; Lower = Next(C); return;
127 case Instruction::SetLT:
128 Lower = ConstantIntegral::getMinValue(C->getType());
129 Upper = C;
130 return;
131 case Instruction::SetGT:
Chris Lattner645e00d2002-09-01 23:53:36 +0000132 Lower = Next(C);
Chris Lattner20d41292002-09-03 23:12:40 +0000133 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
Chris Lattner645e00d2002-09-01 23:53:36 +0000134 return;
135 case Instruction::SetLE:
136 Lower = ConstantIntegral::getMinValue(C->getType());
137 Upper = Next(C);
138 return;
139 case Instruction::SetGE:
Chris Lattner645e00d2002-09-01 23:53:36 +0000140 Lower = C;
Chris Lattner20d41292002-09-03 23:12:40 +0000141 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
Chris Lattner645e00d2002-09-01 23:53:36 +0000142 return;
143 }
144}
145
146/// getType - Return the LLVM data type of this range.
147///
148const Type *ConstantRange::getType() const { return Lower->getType(); }
149
150/// isFullSet - Return true if this set contains all of the elements possible
151/// for this data-type
152bool ConstantRange::isFullSet() const {
153 return Lower == Upper && Lower == ConstantIntegral::getMaxValue(getType());
154}
Misha Brukmanfd939082005-04-21 23:48:37 +0000155
Chris Lattner645e00d2002-09-01 23:53:36 +0000156/// isEmptySet - Return true if this set contains no members.
157///
158bool ConstantRange::isEmptySet() const {
159 return Lower == Upper && Lower == ConstantIntegral::getMinValue(getType());
160}
161
162/// isWrappedSet - Return true if this set wraps around the top of the range,
163/// for example: [100, 8)
164///
165bool ConstantRange::isWrappedSet() const {
Chris Lattner67bb7602004-01-12 20:13:04 +0000166 return GT(Lower, Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000167}
168
Misha Brukmanfd939082005-04-21 23:48:37 +0000169
Chris Lattner645e00d2002-09-01 23:53:36 +0000170/// getSingleElement - If this set contains a single element, return it,
171/// otherwise return null.
172ConstantIntegral *ConstantRange::getSingleElement() const {
173 if (Upper == Next(Lower)) // Is it a single element range?
174 return Lower;
175 return 0;
176}
177
178/// getSetSize - Return the number of elements in this set.
179///
180uint64_t ConstantRange::getSetSize() const {
181 if (isEmptySet()) return 0;
182 if (getType() == Type::BoolTy) {
183 if (Lower != Upper) // One of T or F in the set...
184 return 1;
185 return 2; // Must be full set...
186 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000187
Chris Lattner645e00d2002-09-01 23:53:36 +0000188 // Simply subtract the bounds...
Chris Lattnerfc33d302004-03-30 00:20:08 +0000189 Constant *Result = ConstantExpr::getSub(Upper, Lower);
Reid Spencerb83eb642006-10-20 07:07:24 +0000190 return cast<ConstantInt>(Result)->getZExtValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000191}
192
Chris Lattnerfc33d302004-03-30 00:20:08 +0000193/// contains - Return true if the specified value is in the set.
194///
195bool ConstantRange::contains(ConstantInt *Val) const {
196 if (Lower == Upper) {
197 if (isFullSet()) return true;
198 return false;
199 }
Chris Lattner645e00d2002-09-01 23:53:36 +0000200
Chris Lattnerfc33d302004-03-30 00:20:08 +0000201 if (!isWrappedSet())
202 return LTE(Lower, Val) && LT(Val, Upper);
203 return LTE(Lower, Val) || LT(Val, Upper);
204}
205
206
207
208/// subtract - Subtract the specified constant from the endpoints of this
209/// constant range.
210ConstantRange ConstantRange::subtract(ConstantInt *CI) const {
211 assert(CI->getType() == getType() && getType()->isInteger() &&
212 "Cannot subtract from different type range or non-integer!");
213 // If the set is empty or full, don't modify the endpoints.
214 if (Lower == Upper) return *this;
215 return ConstantRange(ConstantExpr::getSub(Lower, CI),
216 ConstantExpr::getSub(Upper, CI));
217}
Chris Lattner645e00d2002-09-01 23:53:36 +0000218
219
220// intersect1Wrapped - This helper function is used to intersect two ranges when
221// it is known that LHS is wrapped and RHS isn't.
222//
223static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
224 const ConstantRange &RHS) {
225 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
226
Chris Lattner645e00d2002-09-01 23:53:36 +0000227 // Check to see if we overlap on the Left side of RHS...
228 //
Chris Lattner67bb7602004-01-12 20:13:04 +0000229 if (LT(RHS.getLower(), LHS.getUpper())) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000230 // We do overlap on the left side of RHS, see if we overlap on the right of
231 // RHS...
Chris Lattner67bb7602004-01-12 20:13:04 +0000232 if (GT(RHS.getUpper(), LHS.getLower())) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000233 // Ok, the result overlaps on both the left and right sides. See if the
234 // resultant interval will be smaller if we wrap or not...
235 //
236 if (LHS.getSetSize() < RHS.getSetSize())
237 return LHS;
238 else
239 return RHS;
240
241 } else {
242 // No overlap on the right, just on the left.
243 return ConstantRange(RHS.getLower(), LHS.getUpper());
244 }
245
246 } else {
247 // We don't overlap on the left side of RHS, see if we overlap on the right
248 // of RHS...
Chris Lattner67bb7602004-01-12 20:13:04 +0000249 if (GT(RHS.getUpper(), LHS.getLower())) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000250 // Simple overlap...
251 return ConstantRange(LHS.getLower(), RHS.getUpper());
252 } else {
253 // No overlap...
254 return ConstantRange(LHS.getType(), false);
255 }
256 }
257}
258
Chris Lattner645e00d2002-09-01 23:53:36 +0000259/// intersect - Return the range that results from the intersection of this
260/// range with another range.
261///
262ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
263 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000264 // Handle common special cases
265 if (isEmptySet() || CR.isFullSet()) return *this;
266 if (isFullSet() || CR.isEmptySet()) return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000267
268 if (!isWrappedSet()) {
269 if (!CR.isWrappedSet()) {
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000270 ConstantIntegral *L = Max(Lower, CR.Lower);
271 ConstantIntegral *U = Min(Upper, CR.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000272
Chris Lattner67bb7602004-01-12 20:13:04 +0000273 if (LT(L, U)) // If range isn't empty...
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000274 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000275 else
276 return ConstantRange(getType(), false); // Otherwise, return empty set
277 } else
278 return intersect1Wrapped(CR, *this);
279 } else { // We know "this" is wrapped...
280 if (!CR.isWrappedSet())
281 return intersect1Wrapped(*this, CR);
282 else {
283 // Both ranges are wrapped...
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000284 ConstantIntegral *L = Max(Lower, CR.Lower);
285 ConstantIntegral *U = Min(Upper, CR.Upper);
286 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000287 }
288 }
289 return *this;
290}
291
292/// union - Return the range that results from the union of this range with
293/// another range. The resultant range is guaranteed to include the elements of
294/// both sets, but may contain more. For example, [3, 9) union [12,15) is [3,
295/// 15), which includes 9, 10, and 11, which were not included in either set
296/// before.
297///
298ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
299 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
300
301 assert(0 && "Range union not implemented yet!");
302
303 return *this;
304}
Chris Lattner96f9d722002-09-02 00:18:22 +0000305
Chris Lattnerfc33d302004-03-30 00:20:08 +0000306/// zeroExtend - Return a new range in the specified integer type, which must
307/// be strictly larger than the current type. The returned range will
308/// correspond to the possible range of values if the source range had been
309/// zero extended.
310ConstantRange ConstantRange::zeroExtend(const Type *Ty) const {
311 assert(getLower()->getType()->getPrimitiveSize() < Ty->getPrimitiveSize() &&
312 "Not a value extension");
313 if (isFullSet()) {
314 // Change a source full set into [0, 1 << 8*numbytes)
315 unsigned SrcTySize = getLower()->getType()->getPrimitiveSize();
316 return ConstantRange(Constant::getNullValue(Ty),
Reid Spencerb83eb642006-10-20 07:07:24 +0000317 ConstantInt::get(Ty, 1ULL << SrcTySize*8));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000318 }
319
320 Constant *Lower = getLower();
321 Constant *Upper = getUpper();
322 if (Lower->getType()->isInteger() && !Lower->getType()->isUnsigned()) {
323 // Ensure we are doing a ZERO extension even if the input range is signed.
324 Lower = ConstantExpr::getCast(Lower, Ty->getUnsignedVersion());
325 Upper = ConstantExpr::getCast(Upper, Ty->getUnsignedVersion());
326 }
327
328 return ConstantRange(ConstantExpr::getCast(Lower, Ty),
329 ConstantExpr::getCast(Upper, Ty));
330}
331
332/// truncate - Return a new range in the specified integer type, which must be
333/// strictly smaller than the current type. The returned range will
334/// correspond to the possible range of values if the source range had been
335/// truncated to the specified type.
336ConstantRange ConstantRange::truncate(const Type *Ty) const {
337 assert(getLower()->getType()->getPrimitiveSize() > Ty->getPrimitiveSize() &&
338 "Not a value truncation");
339 uint64_t Size = 1ULL << Ty->getPrimitiveSize()*8;
340 if (isFullSet() || getSetSize() >= Size)
341 return ConstantRange(getType());
342
343 return ConstantRange(ConstantExpr::getCast(getLower(), Ty),
344 ConstantExpr::getCast(getUpper(), Ty));
345}
346
347
Chris Lattner96f9d722002-09-02 00:18:22 +0000348/// print - Print out the bounds to a stream...
349///
350void ConstantRange::print(std::ostream &OS) const {
Chris Lattnerce36d552004-07-15 01:29:12 +0000351 OS << "[" << *Lower << "," << *Upper << " )";
Chris Lattner96f9d722002-09-02 00:18:22 +0000352}
353
354/// dump - Allow printing from a debugger easily...
355///
356void ConstantRange::dump() const {
Bill Wendling6f81b512006-11-28 22:46:12 +0000357 print(llvm_cerr);
Chris Lattner96f9d722002-09-02 00:18:22 +0000358}