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 *Next(ConstantIntegral *CI) {
33 if (ConstantBool *CB = dyn_cast<ConstantBool>(CI))
34 return ConstantBool::get(!CB->getValue());
36 Constant *Result = ConstantExpr::getAdd(CI,
37 ConstantInt::get(CI->getType(), 1));
38 return cast<ConstantIntegral>(Result);
41 static bool LT(ConstantIntegral *A, ConstantIntegral *B) {
42 Constant *C = ConstantExpr::getSetLT(A, B);
43 assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
44 return cast<ConstantBool>(C)->getValue();
47 static bool LTE(ConstantIntegral *A, ConstantIntegral *B) {
48 Constant *C = ConstantExpr::getSetLE(A, B);
49 assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
50 return cast<ConstantBool>(C)->getValue();
79 static bool GT(ConstantIntegral *A, ConstantIntegral *B) { return LT(B, A); }
81 static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
82 return LT(A, B) ? A : B;
84 static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
85 return GT(A, B) ? A : B;
88 /// Initialize a full (the default) or empty set for the specified type.
90 ConstantRange::ConstantRange(const Type *Ty, bool Full) {
91 assert(Ty->isIntegral() &&
92 "Cannot make constant range of non-integral type!");
94 Lower = Upper = ConstantIntegral::getMaxValue(Ty);
96 Lower = Upper = ConstantIntegral::getMinValue(Ty);
99 /// Initialize a range to hold the single specified value.
101 ConstantRange::ConstantRange(Constant *V)
102 : Lower(cast<ConstantIntegral>(V)), Upper(Next(cast<ConstantIntegral>(V))) {
105 /// 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)
109 ConstantRange::ConstantRange(Constant *L, Constant *U)
110 : Lower(cast<ConstantIntegral>(L)), Upper(cast<ConstantIntegral>(U)) {
111 assert(Lower->getType() == Upper->getType() &&
112 "Incompatible types for ConstantRange!");
114 // 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!");
120 /// Initialize a set of values that all satisfy the condition with C.
122 ConstantRange::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());
131 case Instruction::SetGT:
133 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
135 case Instruction::SetLE:
136 Lower = ConstantIntegral::getMinValue(C->getType());
139 case Instruction::SetGE:
141 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
146 /// getType - Return the LLVM data type of this range.
148 const Type *ConstantRange::getType() const { return Lower->getType(); }
150 /// isFullSet - Return true if this set contains all of the elements possible
151 /// for this data-type
152 bool ConstantRange::isFullSet() const {
153 return Lower == Upper && Lower == ConstantIntegral::getMaxValue(getType());
156 /// isEmptySet - Return true if this set contains no members.
158 bool ConstantRange::isEmptySet() const {
159 return Lower == Upper && Lower == ConstantIntegral::getMinValue(getType());
162 /// isWrappedSet - Return true if this set wraps around the top of the range,
163 /// for example: [100, 8)
165 bool ConstantRange::isWrappedSet() const {
166 return GT(Lower, Upper);
170 /// getSingleElement - If this set contains a single element, return it,
171 /// otherwise return null.
172 ConstantIntegral *ConstantRange::getSingleElement() const {
173 if (Upper == Next(Lower)) // Is it a single element range?
178 /// getSetSize - Return the number of elements in this set.
180 uint64_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...
185 return 2; // Must be full set...
188 // Simply subtract the bounds...
189 Constant *Result = ConstantExpr::getSub(Upper, Lower);
190 return cast<ConstantInt>(Result)->getZExtValue();
193 /// contains - Return true if the specified value is in the set.
195 bool ConstantRange::contains(ConstantInt *Val) const {
196 if (Lower == Upper) {
197 if (isFullSet()) return true;
202 return LTE(Lower, Val) && LT(Val, Upper);
203 return LTE(Lower, Val) || LT(Val, Upper);
208 /// subtract - Subtract the specified constant from the endpoints of this
210 ConstantRange 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));
220 // intersect1Wrapped - This helper function is used to intersect two ranges when
221 // it is known that LHS is wrapped and RHS isn't.
223 static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
224 const ConstantRange &RHS) {
225 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
227 // Check to see if we overlap on the Left side of RHS...
229 if (LT(RHS.getLower(), LHS.getUpper())) {
230 // We do overlap on the left side of RHS, see if we overlap on the right of
232 if (GT(RHS.getUpper(), LHS.getLower())) {
233 // 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...
236 if (LHS.getSetSize() < RHS.getSetSize())
242 // No overlap on the right, just on the left.
243 return ConstantRange(RHS.getLower(), LHS.getUpper());
247 // We don't overlap on the left side of RHS, see if we overlap on the right
249 if (GT(RHS.getUpper(), LHS.getLower())) {
251 return ConstantRange(LHS.getLower(), RHS.getUpper());
254 return ConstantRange(LHS.getType(), false);
259 /// intersect - Return the range that results from the intersection of this
260 /// range with another range.
262 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
263 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
264 // Handle common special cases
265 if (isEmptySet() || CR.isFullSet()) return *this;
266 if (isFullSet() || CR.isEmptySet()) return CR;
268 if (!isWrappedSet()) {
269 if (!CR.isWrappedSet()) {
270 ConstantIntegral *L = Max(Lower, CR.Lower);
271 ConstantIntegral *U = Min(Upper, CR.Upper);
273 if (LT(L, U)) // If range isn't empty...
274 return ConstantRange(L, U);
276 return ConstantRange(getType(), false); // Otherwise, return empty set
278 return intersect1Wrapped(CR, *this);
279 } else { // We know "this" is wrapped...
280 if (!CR.isWrappedSet())
281 return intersect1Wrapped(*this, CR);
283 // Both ranges are wrapped...
284 ConstantIntegral *L = Max(Lower, CR.Lower);
285 ConstantIntegral *U = Min(Upper, CR.Upper);
286 return ConstantRange(L, U);
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
298 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
299 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
301 assert(0 && "Range union not implemented yet!");
306 /// 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
310 ConstantRange ConstantRange::zeroExtend(const Type *Ty) const {
311 assert(getLower()->getType()->getPrimitiveSize() < Ty->getPrimitiveSize() &&
312 "Not a value extension");
314 // Change a source full set into [0, 1 << 8*numbytes)
315 unsigned SrcTySize = getLower()->getType()->getPrimitiveSize();
316 return ConstantRange(Constant::getNullValue(Ty),
317 ConstantInt::get(Ty, 1ULL << SrcTySize*8));
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());
328 return ConstantRange(ConstantExpr::getCast(Lower, Ty),
329 ConstantExpr::getCast(Upper, Ty));
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.
336 ConstantRange 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());
343 return ConstantRange(ConstantExpr::getCast(getLower(), Ty),
344 ConstantExpr::getCast(getUpper(), Ty));
348 /// print - Print out the bounds to a stream...
350 void ConstantRange::print(std::ostream &OS) const {
351 OS << "[" << *Lower << "," << *Upper << " )";
354 /// dump - Allow printing from a debugger easily...
356 void ConstantRange::dump() const {