blob: 762d5c358adaa35538b30575e70451c215a75e08 [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
Reid Spencerc6bf4bf2006-12-06 20:45:15 +000032static ConstantIntegral *getMaxValue(const Type *Ty) {
33 switch (Ty->getTypeID()) {
34 case Type::BoolTyID: return ConstantBool::getTrue();
35 case Type::SByteTyID:
36 case Type::ShortTyID:
37 case Type::IntTyID:
38 case Type::LongTyID: {
39 // Calculate 011111111111111...
40 unsigned TypeBits = Ty->getPrimitiveSize()*8;
41 int64_t Val = INT64_MAX; // All ones
42 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
43 return ConstantInt::get(Ty, Val);
44 }
45
46 case Type::UByteTyID:
47 case Type::UShortTyID:
48 case Type::UIntTyID:
49 case Type::ULongTyID: return ConstantInt::getAllOnesValue(Ty);
50
51 default: return 0;
52 }
53}
54
55// Static constructor to create the minimum constant for an integral type...
56static ConstantIntegral *getMinValue(const Type *Ty) {
57 switch (Ty->getTypeID()) {
58 case Type::BoolTyID: return ConstantBool::getFalse();
59 case Type::SByteTyID:
60 case Type::ShortTyID:
61 case Type::IntTyID:
62 case Type::LongTyID: {
63 // Calculate 1111111111000000000000
64 unsigned TypeBits = Ty->getPrimitiveSize()*8;
65 int64_t Val = -1; // All ones
66 Val <<= TypeBits-1; // Shift over to the right spot
67 return ConstantInt::get(Ty, Val);
68 }
69
70 case Type::UByteTyID:
71 case Type::UShortTyID:
72 case Type::UIntTyID:
73 case Type::ULongTyID: return ConstantInt::get(Ty, 0);
74
75 default: return 0;
76 }
77}
Chris Lattnerfc33d302004-03-30 00:20:08 +000078static ConstantIntegral *Next(ConstantIntegral *CI) {
Chris Lattner193c2d82006-09-28 23:14:29 +000079 if (ConstantBool *CB = dyn_cast<ConstantBool>(CI))
80 return ConstantBool::get(!CB->getValue());
Misha Brukmanfd939082005-04-21 23:48:37 +000081
Chris Lattnerfc33d302004-03-30 00:20:08 +000082 Constant *Result = ConstantExpr::getAdd(CI,
83 ConstantInt::get(CI->getType(), 1));
84 return cast<ConstantIntegral>(Result);
85}
86
Chris Lattner67bb7602004-01-12 20:13:04 +000087static bool LT(ConstantIntegral *A, ConstantIntegral *B) {
Chris Lattnerfc33d302004-03-30 00:20:08 +000088 Constant *C = ConstantExpr::getSetLT(A, B);
Chris Lattner67bb7602004-01-12 20:13:04 +000089 assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
90 return cast<ConstantBool>(C)->getValue();
91}
92
Chris Lattnerfc33d302004-03-30 00:20:08 +000093static bool LTE(ConstantIntegral *A, ConstantIntegral *B) {
94 Constant *C = ConstantExpr::getSetLE(A, B);
Chris Lattner67bb7602004-01-12 20:13:04 +000095 assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
96 return cast<ConstantBool>(C)->getValue();
97}
98
Chris Lattnerfc33d302004-03-30 00:20:08 +000099static bool GT(ConstantIntegral *A, ConstantIntegral *B) { return LT(B, A); }
100
Chris Lattner67bb7602004-01-12 20:13:04 +0000101static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
102 return LT(A, B) ? A : B;
103}
104static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
105 return GT(A, B) ? A : B;
106}
107
Chris Lattner645e00d2002-09-01 23:53:36 +0000108/// Initialize a full (the default) or empty set for the specified type.
109///
110ConstantRange::ConstantRange(const Type *Ty, bool Full) {
111 assert(Ty->isIntegral() &&
112 "Cannot make constant range of non-integral type!");
113 if (Full)
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000114 Lower = Upper = getMaxValue(Ty);
Chris Lattner645e00d2002-09-01 23:53:36 +0000115 else
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000116 Lower = Upper = getMinValue(Ty);
Chris Lattner645e00d2002-09-01 23:53:36 +0000117}
118
Chris Lattnerfc33d302004-03-30 00:20:08 +0000119/// Initialize a range to hold the single specified value.
120///
121ConstantRange::ConstantRange(Constant *V)
122 : Lower(cast<ConstantIntegral>(V)), Upper(Next(cast<ConstantIntegral>(V))) {
123}
124
Chris Lattner645e00d2002-09-01 23:53:36 +0000125/// Initialize a range of values explicitly... this will assert out if
126/// Lower==Upper and Lower != Min or Max for its type (or if the two constants
127/// have different types)
128///
Chris Lattnerdb813952004-03-29 20:42:49 +0000129ConstantRange::ConstantRange(Constant *L, Constant *U)
130 : Lower(cast<ConstantIntegral>(L)), Upper(cast<ConstantIntegral>(U)) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000131 assert(Lower->getType() == Upper->getType() &&
132 "Incompatible types for ConstantRange!");
Misha Brukmanfd939082005-04-21 23:48:37 +0000133
Chris Lattner645e00d2002-09-01 23:53:36 +0000134 // Make sure that if L & U are equal that they are either Min or Max...
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000135 assert((L != U || (L == getMaxValue(L->getType()) ||
136 L == getMinValue(L->getType()))) &&
Chris Lattner645e00d2002-09-01 23:53:36 +0000137 "Lower == Upper, but they aren't min or max for type!");
138}
139
Chris Lattner645e00d2002-09-01 23:53:36 +0000140/// Initialize a set of values that all satisfy the condition with C.
141///
142ConstantRange::ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C) {
143 switch (SetCCOpcode) {
144 default: assert(0 && "Invalid SetCC opcode to ConstantRange ctor!");
145 case Instruction::SetEQ: Lower = C; Upper = Next(C); return;
146 case Instruction::SetNE: Upper = C; Lower = Next(C); return;
147 case Instruction::SetLT:
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000148 Lower = getMinValue(C->getType());
Chris Lattner645e00d2002-09-01 23:53:36 +0000149 Upper = C;
150 return;
151 case Instruction::SetGT:
Chris Lattner645e00d2002-09-01 23:53:36 +0000152 Lower = Next(C);
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000153 Upper = getMinValue(C->getType()); // Min = Next(Max)
Chris Lattner645e00d2002-09-01 23:53:36 +0000154 return;
155 case Instruction::SetLE:
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000156 Lower = getMinValue(C->getType());
Chris Lattner645e00d2002-09-01 23:53:36 +0000157 Upper = Next(C);
158 return;
159 case Instruction::SetGE:
Chris Lattner645e00d2002-09-01 23:53:36 +0000160 Lower = C;
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000161 Upper = getMinValue(C->getType()); // Min = Next(Max)
Chris Lattner645e00d2002-09-01 23:53:36 +0000162 return;
163 }
164}
165
166/// getType - Return the LLVM data type of this range.
167///
168const Type *ConstantRange::getType() const { return Lower->getType(); }
169
170/// isFullSet - Return true if this set contains all of the elements possible
171/// for this data-type
172bool ConstantRange::isFullSet() const {
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000173 return Lower == Upper && Lower == getMaxValue(getType());
Chris Lattner645e00d2002-09-01 23:53:36 +0000174}
Misha Brukmanfd939082005-04-21 23:48:37 +0000175
Chris Lattner645e00d2002-09-01 23:53:36 +0000176/// isEmptySet - Return true if this set contains no members.
177///
178bool ConstantRange::isEmptySet() const {
Reid Spencerc6bf4bf2006-12-06 20:45:15 +0000179 return Lower == Upper && Lower == getMinValue(getType());
Chris Lattner645e00d2002-09-01 23:53:36 +0000180}
181
182/// isWrappedSet - Return true if this set wraps around the top of the range,
183/// for example: [100, 8)
184///
185bool ConstantRange::isWrappedSet() const {
Chris Lattner67bb7602004-01-12 20:13:04 +0000186 return GT(Lower, Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000187}
188
Misha Brukmanfd939082005-04-21 23:48:37 +0000189
Chris Lattner645e00d2002-09-01 23:53:36 +0000190/// getSingleElement - If this set contains a single element, return it,
191/// otherwise return null.
192ConstantIntegral *ConstantRange::getSingleElement() const {
193 if (Upper == Next(Lower)) // Is it a single element range?
194 return Lower;
195 return 0;
196}
197
198/// getSetSize - Return the number of elements in this set.
199///
200uint64_t ConstantRange::getSetSize() const {
201 if (isEmptySet()) return 0;
202 if (getType() == Type::BoolTy) {
203 if (Lower != Upper) // One of T or F in the set...
204 return 1;
205 return 2; // Must be full set...
206 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000207
Chris Lattner645e00d2002-09-01 23:53:36 +0000208 // Simply subtract the bounds...
Chris Lattnerfc33d302004-03-30 00:20:08 +0000209 Constant *Result = ConstantExpr::getSub(Upper, Lower);
Reid Spencerb83eb642006-10-20 07:07:24 +0000210 return cast<ConstantInt>(Result)->getZExtValue();
Chris Lattner645e00d2002-09-01 23:53:36 +0000211}
212
Chris Lattnerfc33d302004-03-30 00:20:08 +0000213/// contains - Return true if the specified value is in the set.
214///
215bool ConstantRange::contains(ConstantInt *Val) const {
216 if (Lower == Upper) {
217 if (isFullSet()) return true;
218 return false;
219 }
Chris Lattner645e00d2002-09-01 23:53:36 +0000220
Chris Lattnerfc33d302004-03-30 00:20:08 +0000221 if (!isWrappedSet())
222 return LTE(Lower, Val) && LT(Val, Upper);
223 return LTE(Lower, Val) || LT(Val, Upper);
224}
225
226
227
228/// subtract - Subtract the specified constant from the endpoints of this
229/// constant range.
230ConstantRange ConstantRange::subtract(ConstantInt *CI) const {
231 assert(CI->getType() == getType() && getType()->isInteger() &&
232 "Cannot subtract from different type range or non-integer!");
233 // If the set is empty or full, don't modify the endpoints.
234 if (Lower == Upper) return *this;
235 return ConstantRange(ConstantExpr::getSub(Lower, CI),
236 ConstantExpr::getSub(Upper, CI));
237}
Chris Lattner645e00d2002-09-01 23:53:36 +0000238
239
240// intersect1Wrapped - This helper function is used to intersect two ranges when
241// it is known that LHS is wrapped and RHS isn't.
242//
243static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
244 const ConstantRange &RHS) {
245 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
246
Chris Lattner645e00d2002-09-01 23:53:36 +0000247 // Check to see if we overlap on the Left side of RHS...
248 //
Chris Lattner67bb7602004-01-12 20:13:04 +0000249 if (LT(RHS.getLower(), LHS.getUpper())) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000250 // We do overlap on the left side of RHS, see if we overlap on the right of
251 // RHS...
Chris Lattner67bb7602004-01-12 20:13:04 +0000252 if (GT(RHS.getUpper(), LHS.getLower())) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000253 // Ok, the result overlaps on both the left and right sides. See if the
254 // resultant interval will be smaller if we wrap or not...
255 //
256 if (LHS.getSetSize() < RHS.getSetSize())
257 return LHS;
258 else
259 return RHS;
260
261 } else {
262 // No overlap on the right, just on the left.
263 return ConstantRange(RHS.getLower(), LHS.getUpper());
264 }
265
266 } else {
267 // We don't overlap on the left side of RHS, see if we overlap on the right
268 // of RHS...
Chris Lattner67bb7602004-01-12 20:13:04 +0000269 if (GT(RHS.getUpper(), LHS.getLower())) {
Chris Lattner645e00d2002-09-01 23:53:36 +0000270 // Simple overlap...
271 return ConstantRange(LHS.getLower(), RHS.getUpper());
272 } else {
273 // No overlap...
274 return ConstantRange(LHS.getType(), false);
275 }
276 }
277}
278
Chris Lattner645e00d2002-09-01 23:53:36 +0000279/// intersect - Return the range that results from the intersection of this
280/// range with another range.
281///
282ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
283 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000284 // Handle common special cases
285 if (isEmptySet() || CR.isFullSet()) return *this;
286 if (isFullSet() || CR.isEmptySet()) return CR;
Chris Lattner645e00d2002-09-01 23:53:36 +0000287
288 if (!isWrappedSet()) {
289 if (!CR.isWrappedSet()) {
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000290 ConstantIntegral *L = Max(Lower, CR.Lower);
291 ConstantIntegral *U = Min(Upper, CR.Upper);
Chris Lattner645e00d2002-09-01 23:53:36 +0000292
Chris Lattner67bb7602004-01-12 20:13:04 +0000293 if (LT(L, U)) // If range isn't empty...
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000294 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000295 else
296 return ConstantRange(getType(), false); // Otherwise, return empty set
297 } else
298 return intersect1Wrapped(CR, *this);
299 } else { // We know "this" is wrapped...
300 if (!CR.isWrappedSet())
301 return intersect1Wrapped(*this, CR);
302 else {
303 // Both ranges are wrapped...
Chris Lattnerd122f4b2002-09-02 20:49:27 +0000304 ConstantIntegral *L = Max(Lower, CR.Lower);
305 ConstantIntegral *U = Min(Upper, CR.Upper);
306 return ConstantRange(L, U);
Chris Lattner645e00d2002-09-01 23:53:36 +0000307 }
308 }
309 return *this;
310}
311
312/// union - Return the range that results from the union of this range with
313/// another range. The resultant range is guaranteed to include the elements of
314/// both sets, but may contain more. For example, [3, 9) union [12,15) is [3,
315/// 15), which includes 9, 10, and 11, which were not included in either set
316/// before.
317///
318ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
319 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
320
321 assert(0 && "Range union not implemented yet!");
322
323 return *this;
324}
Chris Lattner96f9d722002-09-02 00:18:22 +0000325
Chris Lattnerfc33d302004-03-30 00:20:08 +0000326/// zeroExtend - Return a new range in the specified integer type, which must
327/// be strictly larger than the current type. The returned range will
328/// correspond to the possible range of values if the source range had been
329/// zero extended.
330ConstantRange ConstantRange::zeroExtend(const Type *Ty) const {
331 assert(getLower()->getType()->getPrimitiveSize() < Ty->getPrimitiveSize() &&
332 "Not a value extension");
333 if (isFullSet()) {
334 // Change a source full set into [0, 1 << 8*numbytes)
335 unsigned SrcTySize = getLower()->getType()->getPrimitiveSize();
336 return ConstantRange(Constant::getNullValue(Ty),
Reid Spencerb83eb642006-10-20 07:07:24 +0000337 ConstantInt::get(Ty, 1ULL << SrcTySize*8));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000338 }
339
340 Constant *Lower = getLower();
341 Constant *Upper = getUpper();
Chris Lattnerfc33d302004-03-30 00:20:08 +0000342
Reid Spencerd977d862006-12-12 23:36:14 +0000343 return ConstantRange(ConstantExpr::getZExt(Lower, Ty),
344 ConstantExpr::getZExt(Upper, Ty));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000345}
346
347/// truncate - Return a new range in the specified integer type, which must be
348/// strictly smaller than the current type. The returned range will
349/// correspond to the possible range of values if the source range had been
350/// truncated to the specified type.
351ConstantRange ConstantRange::truncate(const Type *Ty) const {
352 assert(getLower()->getType()->getPrimitiveSize() > Ty->getPrimitiveSize() &&
353 "Not a value truncation");
354 uint64_t Size = 1ULL << Ty->getPrimitiveSize()*8;
355 if (isFullSet() || getSetSize() >= Size)
356 return ConstantRange(getType());
357
Reid Spencer575d95c2006-12-04 02:46:44 +0000358 return ConstantRange(
Reid Spencerd977d862006-12-12 23:36:14 +0000359 ConstantExpr::getTrunc(getLower(), Ty),
360 ConstantExpr::getTrunc(getUpper(), Ty));
Chris Lattnerfc33d302004-03-30 00:20:08 +0000361}
362
363
Chris Lattner96f9d722002-09-02 00:18:22 +0000364/// print - Print out the bounds to a stream...
365///
366void ConstantRange::print(std::ostream &OS) const {
Chris Lattnerce36d552004-07-15 01:29:12 +0000367 OS << "[" << *Lower << "," << *Upper << " )";
Chris Lattner96f9d722002-09-02 00:18:22 +0000368}
369
370/// dump - Allow printing from a debugger easily...
371///
372void ConstantRange::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000373 print(cerr);
Chris Lattner96f9d722002-09-02 00:18:22 +0000374}