1 //===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
3 // Represent a range of possible values that may occur when the program is run
4 // for an integral value. This keeps track of a lower and upper bound for the
5 // constant, which MAY wrap around the end of the numeric range. To do this, it
6 // keeps track of a [lower, upper) bound, which specifies an interval just like
7 // STL iterators. When used with boolean values, the following are important
8 // ranges (other integral ranges use min/max values for special range values):
10 // [F, F) = {} = Empty set
13 // [T, T) = {F, T} = Full set
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Support/ConstantRange.h"
18 #include "llvm/Type.h"
19 #include "llvm/Instruction.h"
20 #include "llvm/ConstantHandling.h"
22 /// Initialize a full (the default) or empty set for the specified type.
24 ConstantRange::ConstantRange(const Type *Ty, bool Full) {
25 assert(Ty->isIntegral() &&
26 "Cannot make constant range of non-integral type!");
28 Lower = Upper = ConstantIntegral::getMaxValue(Ty);
30 Lower = Upper = ConstantIntegral::getMinValue(Ty);
33 /// Initialize a range of values explicitly... this will assert out if
34 /// Lower==Upper and Lower != Min or Max for its type (or if the two constants
35 /// have different types)
37 ConstantRange::ConstantRange(ConstantIntegral *L,
38 ConstantIntegral *U) : Lower(L), Upper(U) {
39 assert(Lower->getType() == Upper->getType() &&
40 "Incompatible types for ConstantRange!");
42 // Make sure that if L & U are equal that they are either Min or Max...
43 assert((L != U || (L == ConstantIntegral::getMaxValue(L->getType()) ||
44 L == ConstantIntegral::getMinValue(L->getType()))) &&
45 "Lower == Upper, but they aren't min or max for type!");
48 static ConstantIntegral *Next(ConstantIntegral *CI) {
49 if (CI->getType() == Type::BoolTy)
50 return CI == ConstantBool::True ? ConstantBool::False : ConstantBool::True;
52 // Otherwise use operator+ in the ConstantHandling Library.
53 Constant *Result = *ConstantInt::get(CI->getType(), 1) + *CI;
54 assert(Result && "ConstantHandling not implemented for integral plus!?");
55 return cast<ConstantIntegral>(Result);
58 /// Initialize a set of values that all satisfy the condition with C.
60 ConstantRange::ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C) {
61 switch (SetCCOpcode) {
62 default: assert(0 && "Invalid SetCC opcode to ConstantRange ctor!");
63 case Instruction::SetEQ: Lower = C; Upper = Next(C); return;
64 case Instruction::SetNE: Upper = C; Lower = Next(C); return;
65 case Instruction::SetLT:
66 Lower = ConstantIntegral::getMinValue(C->getType());
69 case Instruction::SetGT:
71 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
73 case Instruction::SetLE:
74 Lower = ConstantIntegral::getMinValue(C->getType());
77 case Instruction::SetGE:
79 Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
84 /// getType - Return the LLVM data type of this range.
86 const Type *ConstantRange::getType() const { return Lower->getType(); }
88 /// isFullSet - Return true if this set contains all of the elements possible
89 /// for this data-type
90 bool ConstantRange::isFullSet() const {
91 return Lower == Upper && Lower == ConstantIntegral::getMaxValue(getType());
94 /// isEmptySet - Return true if this set contains no members.
96 bool ConstantRange::isEmptySet() const {
97 return Lower == Upper && Lower == ConstantIntegral::getMinValue(getType());
100 /// isWrappedSet - Return true if this set wraps around the top of the range,
101 /// for example: [100, 8)
103 bool ConstantRange::isWrappedSet() const {
104 return (*(Constant*)Lower > *(Constant*)Upper)->getValue();
108 /// getSingleElement - If this set contains a single element, return it,
109 /// otherwise return null.
110 ConstantIntegral *ConstantRange::getSingleElement() const {
111 if (Upper == Next(Lower)) // Is it a single element range?
116 /// getSetSize - Return the number of elements in this set.
118 uint64_t ConstantRange::getSetSize() const {
119 if (isEmptySet()) return 0;
120 if (getType() == Type::BoolTy) {
121 if (Lower != Upper) // One of T or F in the set...
123 return 2; // Must be full set...
126 // Simply subtract the bounds...
127 Constant *Result = *(Constant*)Upper - *(Constant*)Lower;
128 assert(Result && "Subtraction of constant integers not implemented?");
129 if (getType()->isSigned())
130 return (uint64_t)cast<ConstantSInt>(Result)->getValue();
132 return cast<ConstantUInt>(Result)->getValue();
138 // intersect1Wrapped - This helper function is used to intersect two ranges when
139 // it is known that LHS is wrapped and RHS isn't.
141 static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
142 const ConstantRange &RHS) {
143 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
145 // Check to see if we overlap on the Left side of RHS...
147 if ((*(Constant*)RHS.getLower() < *(Constant*)LHS.getUpper())->getValue()) {
148 // We do overlap on the left side of RHS, see if we overlap on the right of
150 if ((*(Constant*)RHS.getUpper() > *(Constant*)LHS.getLower())->getValue()) {
151 // Ok, the result overlaps on both the left and right sides. See if the
152 // resultant interval will be smaller if we wrap or not...
154 if (LHS.getSetSize() < RHS.getSetSize())
160 // No overlap on the right, just on the left.
161 return ConstantRange(RHS.getLower(), LHS.getUpper());
165 // We don't overlap on the left side of RHS, see if we overlap on the right
167 if ((*(Constant*)RHS.getUpper() > *(Constant*)LHS.getLower())->getValue()) {
169 return ConstantRange(LHS.getLower(), RHS.getUpper());
172 return ConstantRange(LHS.getType(), false);
177 static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
178 if ((*(Constant*)A < *(Constant*)B)->getValue())
182 static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
183 if ((*(Constant*)A > *(Constant*)B)->getValue())
189 /// intersect - Return the range that results from the intersection of this
190 /// range with another range.
192 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
193 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
194 // Handle common special cases
195 if (isEmptySet() || CR.isFullSet()) return *this;
196 if (isFullSet() || CR.isEmptySet()) return CR;
198 if (!isWrappedSet()) {
199 if (!CR.isWrappedSet()) {
200 ConstantIntegral *L = Max(Lower, CR.Lower);
201 ConstantIntegral *U = Min(Upper, CR.Upper);
203 if ((*L < *U)->getValue()) // If range isn't empty...
204 return ConstantRange(L, U);
206 return ConstantRange(getType(), false); // Otherwise, return empty set
208 return intersect1Wrapped(CR, *this);
209 } else { // We know "this" is wrapped...
210 if (!CR.isWrappedSet())
211 return intersect1Wrapped(*this, CR);
213 // Both ranges are wrapped...
214 ConstantIntegral *L = Max(Lower, CR.Lower);
215 ConstantIntegral *U = Min(Upper, CR.Upper);
216 return ConstantRange(L, U);
222 /// union - Return the range that results from the union of this range with
223 /// another range. The resultant range is guaranteed to include the elements of
224 /// both sets, but may contain more. For example, [3, 9) union [12,15) is [3,
225 /// 15), which includes 9, 10, and 11, which were not included in either set
228 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
229 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
231 assert(0 && "Range union not implemented yet!");
236 /// print - Print out the bounds to a stream...
238 void ConstantRange::print(std::ostream &OS) const {
239 OS << "[" << Lower << "," << Upper << " )";
242 /// dump - Allow printing from a debugger easily...
244 void ConstantRange::dump() const {