Initial implementation of ConstantRange support
[oota-llvm.git] / lib / Support / 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     Upper = ConstantIntegral::getMaxValue(C->getType());
71     Lower = Next(C);
72     return;
73   case Instruction::SetLE:
74     Lower = ConstantIntegral::getMinValue(C->getType());
75     Upper = Next(C);
76     return;
77   case Instruction::SetGE:
78     Upper = ConstantIntegral::getMaxValue(C->getType());
79     Lower = C;
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   // Handle common special cases
146   if (RHS.isEmptySet()) return RHS;
147   if (RHS.isFullSet()) return LHS;
148
149   // Check to see if we overlap on the Left side of RHS...
150   //
151   if ((*(Constant*)RHS.getLower() < *(Constant*)LHS.getUpper())->getValue()) {
152     // We do overlap on the left side of RHS, see if we overlap on the right of
153     // RHS...
154     if ((*(Constant*)RHS.getUpper() > *(Constant*)LHS.getLower())->getValue()) {
155       // Ok, the result overlaps on both the left and right sides.  See if the
156       // resultant interval will be smaller if we wrap or not...
157       //
158       if (LHS.getSetSize() < RHS.getSetSize())
159         return LHS;
160       else
161         return RHS;
162
163     } else {
164       // No overlap on the right, just on the left.
165       return ConstantRange(RHS.getLower(), LHS.getUpper());
166     }
167
168   } else {
169     // We don't overlap on the left side of RHS, see if we overlap on the right
170     // of RHS...
171     if ((*(Constant*)RHS.getUpper() > *(Constant*)LHS.getLower())->getValue()) {
172       // Simple overlap...
173       return ConstantRange(LHS.getLower(), RHS.getUpper());
174     } else {
175       // No overlap...
176       return ConstantRange(LHS.getType(), false);
177     }
178   }
179 }
180
181   
182 /// intersect - Return the range that results from the intersection of this
183 /// range with another range.
184 ///
185 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
186   assert(getType() == CR.getType() && "ConstantRange types don't agree!");
187
188   if (!isWrappedSet()) {
189     if (!CR.isWrappedSet()) {
190       const Constant &L = std::max(*(Constant*)Lower, *(Constant*)CR.Lower);
191       const Constant &U = std::min(*(Constant*)Upper, *(Constant*)CR.Upper);
192
193       if ((L < U)->getValue())  // If range isn't empty...
194         return ConstantRange(cast<ConstantIntegral>((Constant*)&L),
195                              cast<ConstantIntegral>((Constant*)&U));
196       else
197         return ConstantRange(getType(), false);  // Otherwise, return empty set
198     } else
199       return intersect1Wrapped(CR, *this);
200   } else {   // We know "this" is wrapped...
201     if (!CR.isWrappedSet())
202       return intersect1Wrapped(*this, CR);
203     else {
204       // Both ranges are wrapped...
205       const Constant &L = std::max(*(Constant*)Lower, *(Constant*)CR.Lower);
206       const Constant &U = std::min(*(Constant*)Upper, *(Constant*)CR.Upper);
207
208       return ConstantRange(cast<ConstantIntegral>((Constant*)&L),
209                            cast<ConstantIntegral>((Constant*)&U));      
210     }
211   }
212   return *this;
213 }
214
215 /// union - Return the range that results from the union of this range with
216 /// another range.  The resultant range is guaranteed to include the elements of
217 /// both sets, but may contain more.  For example, [3, 9) union [12,15) is [3,
218 /// 15), which includes 9, 10, and 11, which were not included in either set
219 /// before.
220 ///
221 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
222   assert(getType() == CR.getType() && "ConstantRange types don't agree!");
223
224   assert(0 && "Range union not implemented yet!");
225
226   return *this;
227 }