Adjust to new itf
[oota-llvm.git] / lib / Support / ConstantRange.cpp
1 //===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // Represent a range of possible values that may occur when the program is run
11 // for an integral value.  This keeps track of a lower and upper bound for the
12 // constant, which MAY wrap around the end of the numeric range.  To do this, it
13 // keeps track of a [lower, upper) bound, which specifies an interval just like
14 // STL iterators.  When used with boolean values, the following are important
15 // ranges (other integral ranges use min/max values for special range values):
16 //
17 //  [F, F) = {}     = Empty set
18 //  [T, F) = {T}
19 //  [F, T) = {F}
20 //  [T, T) = {F, T} = Full set
21 //
22 //===----------------------------------------------------------------------===//
23
24 #include "llvm/Support/ConstantRange.h"
25 #include "llvm/Constants.h"
26 #include "llvm/Instruction.h"
27 #include "llvm/Type.h"
28 using namespace llvm;
29
30 static bool LT(ConstantIntegral *A, ConstantIntegral *B) {
31   Constant *C = ConstantExpr::get(Instruction::SetLT, A, B);
32   assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
33   return cast<ConstantBool>(C)->getValue();
34 }
35
36 static bool GT(ConstantIntegral *A, ConstantIntegral *B) {
37   Constant *C = ConstantExpr::get(Instruction::SetGT, A, B);
38   assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
39   return cast<ConstantBool>(C)->getValue();
40 }
41
42 static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
43   return LT(A, B) ? A : B;
44 }
45 static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
46   return GT(A, B) ? A : B;
47 }
48
49
50 /// Initialize a full (the default) or empty set for the specified type.
51 ///
52 ConstantRange::ConstantRange(const Type *Ty, bool Full) {
53   assert(Ty->isIntegral() &&
54          "Cannot make constant range of non-integral type!");
55   if (Full)
56     Lower = Upper = ConstantIntegral::getMaxValue(Ty);
57   else
58     Lower = Upper = ConstantIntegral::getMinValue(Ty);
59 }
60
61 /// Initialize a range of values explicitly... this will assert out if
62 /// Lower==Upper and Lower != Min or Max for its type (or if the two constants
63 /// have different types)
64 ///
65 ConstantRange::ConstantRange(Constant *L, Constant *U)
66   : Lower(cast<ConstantIntegral>(L)), Upper(cast<ConstantIntegral>(U)) {
67   assert(Lower->getType() == Upper->getType() &&
68          "Incompatible types for ConstantRange!");
69   
70   // Make sure that if L & U are equal that they are either Min or Max...
71   assert((L != U || (L == ConstantIntegral::getMaxValue(L->getType()) ||
72                      L == ConstantIntegral::getMinValue(L->getType()))) &&
73          "Lower == Upper, but they aren't min or max for type!");
74 }
75
76 static ConstantIntegral *Next(ConstantIntegral *CI) {
77   if (CI->getType() == Type::BoolTy)
78     return CI == ConstantBool::True ? ConstantBool::False : ConstantBool::True;
79       
80   Constant *Result = ConstantExpr::get(Instruction::Add, CI,
81                                        ConstantInt::get(CI->getType(), 1));
82   return cast<ConstantIntegral>(Result);
83 }
84
85 /// Initialize a set of values that all satisfy the condition with C.
86 ///
87 ConstantRange::ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C) {
88   switch (SetCCOpcode) {
89   default: assert(0 && "Invalid SetCC opcode to ConstantRange ctor!");
90   case Instruction::SetEQ: Lower = C; Upper = Next(C); return;
91   case Instruction::SetNE: Upper = C; Lower = Next(C); return;
92   case Instruction::SetLT:
93     Lower = ConstantIntegral::getMinValue(C->getType());
94     Upper = C;
95     return;
96   case Instruction::SetGT:
97     Lower = Next(C);
98     Upper = ConstantIntegral::getMinValue(C->getType());  // Min = Next(Max)
99     return;
100   case Instruction::SetLE:
101     Lower = ConstantIntegral::getMinValue(C->getType());
102     Upper = Next(C);
103     return;
104   case Instruction::SetGE:
105     Lower = C;
106     Upper = ConstantIntegral::getMinValue(C->getType());  // Min = Next(Max)
107     return;
108   }
109 }
110
111 /// getType - Return the LLVM data type of this range.
112 ///
113 const Type *ConstantRange::getType() const { return Lower->getType(); }
114
115 /// isFullSet - Return true if this set contains all of the elements possible
116 /// for this data-type
117 bool ConstantRange::isFullSet() const {
118   return Lower == Upper && Lower == ConstantIntegral::getMaxValue(getType());
119 }
120   
121 /// isEmptySet - Return true if this set contains no members.
122 ///
123 bool ConstantRange::isEmptySet() const {
124   return Lower == Upper && Lower == ConstantIntegral::getMinValue(getType());
125 }
126
127 /// isWrappedSet - Return true if this set wraps around the top of the range,
128 /// for example: [100, 8)
129 ///
130 bool ConstantRange::isWrappedSet() const {
131   return GT(Lower, Upper);
132 }
133
134   
135 /// getSingleElement - If this set contains a single element, return it,
136 /// otherwise return null.
137 ConstantIntegral *ConstantRange::getSingleElement() const {
138   if (Upper == Next(Lower))  // Is it a single element range?
139     return Lower;
140   return 0;
141 }
142
143 /// getSetSize - Return the number of elements in this set.
144 ///
145 uint64_t ConstantRange::getSetSize() const {
146   if (isEmptySet()) return 0;
147   if (getType() == Type::BoolTy) {
148     if (Lower != Upper)  // One of T or F in the set...
149       return 1;
150     return 2;            // Must be full set...
151   }
152   
153   // Simply subtract the bounds...
154   Constant *Result =
155     ConstantExpr::get(Instruction::Sub, (Constant*)Upper, (Constant*)Lower);
156   return cast<ConstantInt>(Result)->getRawValue();
157 }
158
159
160
161
162 // intersect1Wrapped - This helper function is used to intersect two ranges when
163 // it is known that LHS is wrapped and RHS isn't.
164 //
165 static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
166                                        const ConstantRange &RHS) {
167   assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
168
169   // Check to see if we overlap on the Left side of RHS...
170   //
171   if (LT(RHS.getLower(), LHS.getUpper())) {
172     // We do overlap on the left side of RHS, see if we overlap on the right of
173     // RHS...
174     if (GT(RHS.getUpper(), LHS.getLower())) {
175       // Ok, the result overlaps on both the left and right sides.  See if the
176       // resultant interval will be smaller if we wrap or not...
177       //
178       if (LHS.getSetSize() < RHS.getSetSize())
179         return LHS;
180       else
181         return RHS;
182
183     } else {
184       // No overlap on the right, just on the left.
185       return ConstantRange(RHS.getLower(), LHS.getUpper());
186     }
187
188   } else {
189     // We don't overlap on the left side of RHS, see if we overlap on the right
190     // of RHS...
191     if (GT(RHS.getUpper(), LHS.getLower())) {
192       // Simple overlap...
193       return ConstantRange(LHS.getLower(), RHS.getUpper());
194     } else {
195       // No overlap...
196       return ConstantRange(LHS.getType(), false);
197     }
198   }
199 }
200
201 /// intersect - Return the range that results from the intersection of this
202 /// range with another range.
203 ///
204 ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
205   assert(getType() == CR.getType() && "ConstantRange types don't agree!");
206   // Handle common special cases
207   if (isEmptySet() || CR.isFullSet())  return *this;
208   if (isFullSet()  || CR.isEmptySet()) return CR;
209
210   if (!isWrappedSet()) {
211     if (!CR.isWrappedSet()) {
212       ConstantIntegral *L = Max(Lower, CR.Lower);
213       ConstantIntegral *U = Min(Upper, CR.Upper);
214
215       if (LT(L, U))  // If range isn't empty...
216         return ConstantRange(L, U);
217       else
218         return ConstantRange(getType(), false);  // Otherwise, return empty set
219     } else
220       return intersect1Wrapped(CR, *this);
221   } else {   // We know "this" is wrapped...
222     if (!CR.isWrappedSet())
223       return intersect1Wrapped(*this, CR);
224     else {
225       // Both ranges are wrapped...
226       ConstantIntegral *L = Max(Lower, CR.Lower);
227       ConstantIntegral *U = Min(Upper, CR.Upper);
228       return ConstantRange(L, U);
229     }
230   }
231   return *this;
232 }
233
234 /// union - Return the range that results from the union of this range with
235 /// another range.  The resultant range is guaranteed to include the elements of
236 /// both sets, but may contain more.  For example, [3, 9) union [12,15) is [3,
237 /// 15), which includes 9, 10, and 11, which were not included in either set
238 /// before.
239 ///
240 ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
241   assert(getType() == CR.getType() && "ConstantRange types don't agree!");
242
243   assert(0 && "Range union not implemented yet!");
244
245   return *this;
246 }
247
248 /// print - Print out the bounds to a stream...
249 ///
250 void ConstantRange::print(std::ostream &OS) const {
251   OS << "[" << Lower << "," << Upper << " )";
252 }
253
254 /// dump - Allow printing from a debugger easily...
255 ///
256 void ConstantRange::dump() const {
257   print(std::cerr);
258 }