Minor bug fix.
[oota-llvm.git] / lib / Analysis / ConstantRange.cpp
1 //===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
2 //
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):
9 //
10 //  [F, F) = {}     = Empty set
11 //  [T, F) = {T}
12 //  [F, T) = {F}
13 //  [T, T) = {F, T} = Full set
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Support/ConstantRange.h"
18 #include "llvm/Type.h"
19 #include "llvm/Instruction.h"
20 #include "llvm/ConstantHandling.h"
21
22 /// Initialize a full (the default) or empty set for the specified type.
23 ///
24 ConstantRange::ConstantRange(const Type *Ty, bool Full) {
25   assert(Ty->isIntegral() &&
26          "Cannot make constant range of non-integral type!");
27   if (Full)
28     Lower = Upper = ConstantIntegral::getMaxValue(Ty);
29   else
30     Lower = Upper = ConstantIntegral::getMinValue(Ty);
31 }
32
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)
36 ///
37 ConstantRange::ConstantRange(ConstantIntegral *L,
38                              ConstantIntegral *U) : Lower(L), Upper(U) {
39   assert(Lower->getType() == Upper->getType() &&
40          "Incompatible types for ConstantRange!");
41   
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!");
46 }
47
48 static ConstantIntegral *Next(ConstantIntegral *CI) {
49   if (CI->getType() == Type::BoolTy)
50     return CI == ConstantBool::True ? ConstantBool::False : ConstantBool::True;
51       
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);
56 }
57
58 /// Initialize a set of values that all satisfy the condition with C.
59 ///
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());
67     Upper = C;
68     return;
69   case Instruction::SetGT:
70     Lower = Next(C);
71     Upper = ConstantIntegral::getMinValue(C->getType());  // Min = Next(Max)
72     return;
73   case Instruction::SetLE:
74     Lower = ConstantIntegral::getMinValue(C->getType());
75     Upper = Next(C);
76     return;
77   case Instruction::SetGE:
78     Lower = C;
79     Upper = ConstantIntegral::getMinValue(C->getType());  // Min = Next(Max)
80     return;
81   }
82 }
83
84 /// getType - Return the LLVM data type of this range.
85 ///
86 const Type *ConstantRange::getType() const { return Lower->getType(); }
87
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());
92 }
93   
94 /// isEmptySet - Return true if this set contains no members.
95 ///
96 bool ConstantRange::isEmptySet() const {
97   return Lower == Upper && Lower == ConstantIntegral::getMinValue(getType());
98 }
99
100 /// isWrappedSet - Return true if this set wraps around the top of the range,
101 /// for example: [100, 8)
102 ///
103 bool ConstantRange::isWrappedSet() const {
104   return (*(Constant*)Lower > *(Constant*)Upper)->getValue();
105 }
106
107   
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?
112     return Lower;
113   return 0;
114 }
115
116 /// getSetSize - Return the number of elements in this set.
117 ///
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...
122       return 1;
123     return 2;            // Must be full set...
124   }
125   
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();
131   else
132     return cast<ConstantUInt>(Result)->getValue();
133 }
134
135
136
137
138 // intersect1Wrapped - This helper function is used to intersect two ranges when
139 // it is known that LHS is wrapped and RHS isn't.
140 //
141 static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
142                                        const ConstantRange &RHS) {
143   assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
144
145   // Check to see if we overlap on the Left side of RHS...
146   //
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
149     // RHS...
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...
153       //
154       if (LHS.getSetSize() < RHS.getSetSize())
155         return LHS;
156       else
157         return RHS;
158
159     } else {
160       // No overlap on the right, just on the left.
161       return ConstantRange(RHS.getLower(), LHS.getUpper());
162     }
163
164   } else {
165     // We don't overlap on the left side of RHS, see if we overlap on the right
166     // of RHS...
167     if ((*(Constant*)RHS.getUpper() > *(Constant*)LHS.getLower())->getValue()) {
168       // Simple overlap...
169       return ConstantRange(LHS.getLower(), RHS.getUpper());
170     } else {
171       // No overlap...
172       return ConstantRange(LHS.getType(), false);
173     }
174   }
175 }
176
177 static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
178   if ((*(Constant*)A < *(Constant*)B)->getValue())
179     return A;
180   return B;
181 }
182 static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
183   if ((*(Constant*)A > *(Constant*)B)->getValue())
184     return A;
185   return B;
186 }
187
188   
189 /// intersect - Return the range that results from the intersection of this
190 /// range with another range.
191 ///
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;
197
198   if (!isWrappedSet()) {
199     if (!CR.isWrappedSet()) {
200       ConstantIntegral *L = Max(Lower, CR.Lower);
201       ConstantIntegral *U = Min(Upper, CR.Upper);
202
203       if ((*L < *U)->getValue())  // If range isn't empty...
204         return ConstantRange(L, U);
205       else
206         return ConstantRange(getType(), false);  // Otherwise, return empty set
207     } else
208       return intersect1Wrapped(CR, *this);
209   } else {   // We know "this" is wrapped...
210     if (!CR.isWrappedSet())
211       return intersect1Wrapped(*this, CR);
212     else {
213       // Both ranges are wrapped...
214       ConstantIntegral *L = Max(Lower, CR.Lower);
215       ConstantIntegral *U = Min(Upper, CR.Upper);
216       return ConstantRange(L, U);
217     }
218   }
219   return *this;
220 }
221
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
226 /// before.
227 ///
228 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
229   assert(getType() == CR.getType() && "ConstantRange types don't agree!");
230
231   assert(0 && "Range union not implemented yet!");
232
233   return *this;
234 }
235
236 /// print - Print out the bounds to a stream...
237 ///
238 void ConstantRange::print(std::ostream &OS) const {
239   OS << "[" << Lower << "," << Upper << " )";
240 }
241
242 /// dump - Allow printing from a debugger easily...
243 ///
244 void ConstantRange::dump() const {
245   print(std::cerr);
246 }