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 return cast<ConstantInt>(Result)->getRawValue();
135 // intersect1Wrapped - This helper function is used to intersect two ranges when
136 // it is known that LHS is wrapped and RHS isn't.
138 static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
139 const ConstantRange &RHS) {
140 assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
142 // Check to see if we overlap on the Left side of RHS...
144 if ((*(Constant*)RHS.getLower() < *(Constant*)LHS.getUpper())->getValue()) {
145 // We do overlap on the left side of RHS, see if we overlap on the right of
147 if ((*(Constant*)RHS.getUpper() > *(Constant*)LHS.getLower())->getValue()) {
148 // Ok, the result overlaps on both the left and right sides. See if the
149 // resultant interval will be smaller if we wrap or not...
151 if (LHS.getSetSize() < RHS.getSetSize())
157 // No overlap on the right, just on the left.
158 return ConstantRange(RHS.getLower(), LHS.getUpper());
162 // We don't overlap on the left side of RHS, see if we overlap on the right
164 if ((*(Constant*)RHS.getUpper() > *(Constant*)LHS.getLower())->getValue()) {
166 return ConstantRange(LHS.getLower(), RHS.getUpper());
169 return ConstantRange(LHS.getType(), false);
174 static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
175 if ((*(Constant*)A < *(Constant*)B)->getValue())
179 static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
180 if ((*(Constant*)A > *(Constant*)B)->getValue())
186 /// intersect - Return the range that results from the intersection of this
187 /// range with another range.
189 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
190 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
191 // Handle common special cases
192 if (isEmptySet() || CR.isFullSet()) return *this;
193 if (isFullSet() || CR.isEmptySet()) return CR;
195 if (!isWrappedSet()) {
196 if (!CR.isWrappedSet()) {
197 ConstantIntegral *L = Max(Lower, CR.Lower);
198 ConstantIntegral *U = Min(Upper, CR.Upper);
200 if ((*L < *U)->getValue()) // If range isn't empty...
201 return ConstantRange(L, U);
203 return ConstantRange(getType(), false); // Otherwise, return empty set
205 return intersect1Wrapped(CR, *this);
206 } else { // We know "this" is wrapped...
207 if (!CR.isWrappedSet())
208 return intersect1Wrapped(*this, CR);
210 // Both ranges are wrapped...
211 ConstantIntegral *L = Max(Lower, CR.Lower);
212 ConstantIntegral *U = Min(Upper, CR.Upper);
213 return ConstantRange(L, U);
219 /// union - Return the range that results from the union of this range with
220 /// another range. The resultant range is guaranteed to include the elements of
221 /// both sets, but may contain more. For example, [3, 9) union [12,15) is [3,
222 /// 15), which includes 9, 10, and 11, which were not included in either set
225 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
226 assert(getType() == CR.getType() && "ConstantRange types don't agree!");
228 assert(0 && "Range union not implemented yet!");
233 /// print - Print out the bounds to a stream...
235 void ConstantRange::print(std::ostream &OS) const {
236 OS << "[" << Lower << "," << Upper << " )";
239 /// dump - Allow printing from a debugger easily...
241 void ConstantRange::dump() const {