1 //===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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):
17 // [F, F) = {} = Empty set
20 // [T, T) = {F, T} = Full set
22 //===----------------------------------------------------------------------===//
24 #include "llvm/Support/ConstantRange.h"
25 #include "llvm/Constants.h"
26 #include "llvm/Instruction.h"
27 #include "llvm/Type.h"
28 #include "llvm/Support/Streams.h"
32 static ConstantIntegral *getMaxValue(const Type *Ty) {
33 switch (Ty->getTypeID()) {
34 case Type::BoolTyID: return ConstantBool::getTrue();
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);
47 case Type::UShortTyID:
49 case Type::ULongTyID: return ConstantInt::getAllOnesValue(Ty);
55 // Static constructor to create the minimum constant for an integral type...
56 static ConstantIntegral *getMinValue(const Type *Ty) {
57 switch (Ty->getTypeID()) {
58 case Type::BoolTyID: return ConstantBool::getFalse();
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);
71 case Type::UShortTyID:
73 case Type::ULongTyID: return ConstantInt::get(Ty, 0);
78 static ConstantIntegral *Next(ConstantIntegral *CI) {
79 if (ConstantBool *CB = dyn_cast<ConstantBool>(CI))
80 return ConstantBool::get(!CB->getValue());
82 Constant *Result = ConstantExpr::getAdd(CI,
83 ConstantInt::get(CI->getType(), 1));
84 return cast<ConstantIntegral>(Result);
87 static bool LT(ConstantIntegral *A, ConstantIntegral *B) {
88 Constant *C = ConstantExpr::getSetLT(A, B);
89 assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
90 return cast<ConstantBool>(C)->getValue();
93 static bool LTE(ConstantIntegral *A, ConstantIntegral *B) {
94 Constant *C = ConstantExpr::getSetLE(A, B);
95 assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
96 return cast<ConstantBool>(C)->getValue();
99 static bool GT(ConstantIntegral *A, ConstantIntegral *B) { return LT(B, A); }
101 static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
102 return LT(A, B) ? A : B;
104 static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
105 return GT(A, B) ? A : B;
108 /// Initialize a full (the default) or empty set for the specified type.
110 ConstantRange::ConstantRange(const Type *Ty, bool Full) {
111 assert(Ty->isIntegral() &&
112 "Cannot make constant range of non-integral type!");
114 Lower = Upper = getMaxValue(Ty);
116 Lower = Upper = getMinValue(Ty);
119 /// Initialize a range to hold the single specified value.
121 ConstantRange::ConstantRange(Constant *V)
122 : Lower(cast<ConstantIntegral>(V)), Upper(Next(cast<ConstantIntegral>(V))) {
125 /// 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)
129 ConstantRange::ConstantRange(Constant *L, Constant *U)
130 : Lower(cast<ConstantIntegral>(L)), Upper(cast<ConstantIntegral>(U)) {
131 assert(Lower->getType() == Upper->getType() &&
132 "Incompatible types for ConstantRange!");
134 // Make sure that if L & U are equal that they are either Min or Max...
135 assert((L != U || (L == getMaxValue(L->getType()) ||
136 L == getMinValue(L->getType()))) &&
137 "Lower == Upper, but they aren't min or max for type!");
140 /// Initialize a set of values that all satisfy the condition with C.
142 ConstantRange::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:
148 Lower = getMinValue(C->getType());
151 case Instruction::SetGT:
153 Upper = getMinValue(C->getType()); // Min = Next(Max)
155 case Instruction::SetLE:
156 Lower = getMinValue(C->getType());
159 case Instruction::SetGE:
161 Upper = getMinValue(C->getType()); // Min = Next(Max)
166 /// getType - Return the LLVM data type of this range.
168 const Type *ConstantRange::getType() const { return Lower->getType(); }
170 /// isFullSet - Return true if this set contains all of the elements possible
171 /// for this data-type
172 bool ConstantRange::isFullSet() const {
173 return Lower == Upper && Lower == getMaxValue(getType());
176 /// isEmptySet - Return true if this set contains no members.
178 bool ConstantRange::isEmptySet() const {
179 return Lower == Upper && Lower == getMinValue(getType());
182 /// isWrappedSet - Return true if this set wraps around the top of the range,
183 /// for example: [100, 8)
185 bool ConstantRange::isWrappedSet() const {
186 return GT(Lower, Upper);
190 /// getSingleElement - If this set contains a single element, return it,
191 /// otherwise return null.
192 ConstantIntegral *ConstantRange::getSingleElement() const {
193 if (Upper == Next(Lower)) // Is it a single element range?
198 /// getSetSize - Return the number of elements in this set.
200 uint64_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...
205 return 2; // Must be full set...
208 // Simply subtract the bounds...
209 Constant *Result = ConstantExpr::getSub(Upper, Lower);
210 return cast<ConstantInt>(Result)->getZExtValue();
213 /// contains - Return true if the specified value is in the set.
215 bool ConstantRange::contains(ConstantInt *Val) const {
216 if (Lower == Upper) {
217 if (isFullSet()) return true;
222 return LTE(Lower, Val) && LT(Val, Upper);
223 return LTE(Lower, Val) || LT(Val, Upper);
228 /// subtract - Subtract the specified constant from the endpoints of this
230 ConstantRange 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));
240 // intersect1Wrapped - This helper function is used to intersect two ranges when
241 // it is known that LHS is wrapped and RHS isn't.
243 static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
244 const ConstantRange &RHS) {
245 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
247 // Check to see if we overlap on the Left side of RHS...
249 if (LT(RHS.getLower(), LHS.getUpper())) {
250 // We do overlap on the left side of RHS, see if we overlap on the right of
252 if (GT(RHS.getUpper(), LHS.getLower())) {
253 // 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...
256 if (LHS.getSetSize() < RHS.getSetSize())
262 // No overlap on the right, just on the left.
263 return ConstantRange(RHS.getLower(), LHS.getUpper());
267 // We don't overlap on the left side of RHS, see if we overlap on the right
269 if (GT(RHS.getUpper(), LHS.getLower())) {
271 return ConstantRange(LHS.getLower(), RHS.getUpper());
274 return ConstantRange(LHS.getType(), false);
279 /// intersect - Return the range that results from the intersection of this
280 /// range with another range.
282 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
283 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
284 // Handle common special cases
285 if (isEmptySet() || CR.isFullSet()) return *this;
286 if (isFullSet() || CR.isEmptySet()) return CR;
288 if (!isWrappedSet()) {
289 if (!CR.isWrappedSet()) {
290 ConstantIntegral *L = Max(Lower, CR.Lower);
291 ConstantIntegral *U = Min(Upper, CR.Upper);
293 if (LT(L, U)) // If range isn't empty...
294 return ConstantRange(L, U);
296 return ConstantRange(getType(), false); // Otherwise, return empty set
298 return intersect1Wrapped(CR, *this);
299 } else { // We know "this" is wrapped...
300 if (!CR.isWrappedSet())
301 return intersect1Wrapped(*this, CR);
303 // Both ranges are wrapped...
304 ConstantIntegral *L = Max(Lower, CR.Lower);
305 ConstantIntegral *U = Min(Upper, CR.Upper);
306 return ConstantRange(L, U);
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
318 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
319 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
321 assert(0 && "Range union not implemented yet!");
326 /// 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
330 ConstantRange ConstantRange::zeroExtend(const Type *Ty) const {
331 assert(getLower()->getType()->getPrimitiveSize() < Ty->getPrimitiveSize() &&
332 "Not a value extension");
334 // Change a source full set into [0, 1 << 8*numbytes)
335 unsigned SrcTySize = getLower()->getType()->getPrimitiveSize();
336 return ConstantRange(Constant::getNullValue(Ty),
337 ConstantInt::get(Ty, 1ULL << SrcTySize*8));
340 Constant *Lower = getLower();
341 Constant *Upper = getUpper();
343 return ConstantRange(ConstantExpr::getCast(Instruction::ZExt, Lower, Ty),
344 ConstantExpr::getCast(Instruction::ZExt, Upper, Ty));
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.
351 ConstantRange 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());
358 return ConstantRange(
359 ConstantExpr::getCast(Instruction::Trunc, getLower(), Ty),
360 ConstantExpr::getCast(Instruction::Trunc, getUpper(), Ty));
364 /// print - Print out the bounds to a stream...
366 void ConstantRange::print(std::ostream &OS) const {
367 OS << "[" << *Lower << "," << *Upper << " )";
370 /// dump - Allow printing from a debugger easily...
372 void ConstantRange::dump() const {