c9d8ae6fbbc930f5108c2b77bf17c12ed9eb992e
[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   return cast<ConstantInt>(Result)->getRawValue();
130 }
131
132
133
134
135 // intersect1Wrapped - This helper function is used to intersect two ranges when
136 // it is known that LHS is wrapped and RHS isn't.
137 //
138 static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
139                                        const ConstantRange &RHS) {
140   assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
141
142   // Check to see if we overlap on the Left side of RHS...
143   //
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
146     // RHS...
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...
150       //
151       if (LHS.getSetSize() < RHS.getSetSize())
152         return LHS;
153       else
154         return RHS;
155
156     } else {
157       // No overlap on the right, just on the left.
158       return ConstantRange(RHS.getLower(), LHS.getUpper());
159     }
160
161   } else {
162     // We don't overlap on the left side of RHS, see if we overlap on the right
163     // of RHS...
164     if ((*(Constant*)RHS.getUpper() > *(Constant*)LHS.getLower())->getValue()) {
165       // Simple overlap...
166       return ConstantRange(LHS.getLower(), RHS.getUpper());
167     } else {
168       // No overlap...
169       return ConstantRange(LHS.getType(), false);
170     }
171   }
172 }
173
174 static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
175   if ((*(Constant*)A < *(Constant*)B)->getValue())
176     return A;
177   return B;
178 }
179 static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
180   if ((*(Constant*)A > *(Constant*)B)->getValue())
181     return A;
182   return B;
183 }
184
185   
186 /// intersect - Return the range that results from the intersection of this
187 /// range with another range.
188 ///
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;
194
195   if (!isWrappedSet()) {
196     if (!CR.isWrappedSet()) {
197       ConstantIntegral *L = Max(Lower, CR.Lower);
198       ConstantIntegral *U = Min(Upper, CR.Upper);
199
200       if ((*L < *U)->getValue())  // If range isn't empty...
201         return ConstantRange(L, U);
202       else
203         return ConstantRange(getType(), false);  // Otherwise, return empty set
204     } else
205       return intersect1Wrapped(CR, *this);
206   } else {   // We know "this" is wrapped...
207     if (!CR.isWrappedSet())
208       return intersect1Wrapped(*this, CR);
209     else {
210       // Both ranges are wrapped...
211       ConstantIntegral *L = Max(Lower, CR.Lower);
212       ConstantIntegral *U = Min(Upper, CR.Upper);
213       return ConstantRange(L, U);
214     }
215   }
216   return *this;
217 }
218
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
223 /// before.
224 ///
225 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
226   assert(getType() == CR.getType() && "ConstantRange types don't agree!");
227
228   assert(0 && "Range union not implemented yet!");
229
230   return *this;
231 }
232
233 /// print - Print out the bounds to a stream...
234 ///
235 void ConstantRange::print(std::ostream &OS) const {
236   OS << "[" << Lower << "," << Upper << " )";
237 }
238
239 /// dump - Allow printing from a debugger easily...
240 ///
241 void ConstantRange::dump() const {
242   print(std::cerr);
243 }