8af3fb00c6ab00b72183377ee7f4fa49e8db760f
[oota-llvm.git] / include / llvm / Support / ConstantRange.h
1 //===-- llvm/Support/ConstantRange.h - Represent a range --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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: :
16 //
17 //  [F, F) = {}     = Empty set
18 //  [T, F) = {T}
19 //  [F, T) = {F}
20 //  [T, T) = {F, T} = Full set
21 //
22 // The other integral ranges use min/max values for special range values. For
23 // example, for 8-bit types, it uses:
24 // [0, 0)     = {}       = Empty set
25 // [255, 255) = {0..255} = Full Set
26 //
27 // Note that ConstantRange always keeps unsigned values, and
28 // ConstantSignedRange always keeps signed values.
29 //
30 //===----------------------------------------------------------------------===//
31
32 #ifndef LLVM_SUPPORT_CONSTANT_RANGE_H
33 #define LLVM_SUPPORT_CONSTANT_RANGE_H
34
35 #include "llvm/ADT/APInt.h"
36 #include "llvm/Support/DataTypes.h"
37
38 namespace llvm {
39
40 /// ConstantRangeBase - A base class for ConstantRange and ConstantSignedRange.
41 /// This class implements functionality common to both.
42 class ConstantRangeBase {
43 protected:
44   APInt Lower, Upper;
45
46   /// Initialize a range to hold the single specified value.
47   ///
48   ConstantRangeBase(const APInt &Value);
49
50   /// @brief Initialize a range of values explicitly. This will assert out if
51   /// Lower==Upper and Lower != Min or Max value for its type. It will also
52   /// assert out if the two APInt's are not the same bit width.
53   ConstantRangeBase(const APInt& Lower, const APInt& Upper);
54
55 public:
56   /// getLower - Return the lower value for this range...
57   ///
58   const APInt &getLower() const { return Lower; }
59
60   /// getUpper - Return the upper value for this range...
61   ///
62   const APInt &getUpper() const { return Upper; }
63
64   /// getBitWidth - get the bit width of this ConstantRange
65   ///
66   uint32_t getBitWidth() const { return Lower.getBitWidth(); }
67
68   /// getSingleElement - If this set contains a single element, return it,
69   /// otherwise return null.
70   ///
71   const APInt *getSingleElement() const {
72     if (Upper == Lower + 1)
73       return &Lower;
74     return 0;
75   }
76
77   /// isSingleElement - Return true if this set contains exactly one member.
78   ///
79   bool isSingleElement() const { return getSingleElement() != 0; }
80
81   /// operator== - Return true if this range is equal to another range.
82   ///
83   bool operator==(const ConstantRangeBase &CR) const {
84     return Lower == CR.Lower && Upper == CR.Upper;
85   }
86   bool operator!=(const ConstantRangeBase &CR) const {
87     return !operator==(CR);
88   }
89
90   /// print - Print out the bounds to a stream...
91   ///
92   void print(raw_ostream &OS) const;
93
94   /// dump - Allow printing from a debugger easily...
95   ///
96   void dump() const;
97 };
98
99 /// ConstantRange - This class represents an range of unsigned values.
100 ///
101 class ConstantRange : public ConstantRangeBase {
102   static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
103                                          const ConstantRange &RHS);
104 public:
105   /// Initialize a full (the default) or empty set for the specified bit width.
106   ///
107   explicit ConstantRange(uint32_t BitWidth, bool isFullSet = true);
108
109   /// Initialize a range to hold the single specified value.
110   ///
111   ConstantRange(const APInt &Value);
112
113   /// @brief Initialize a range of values explicitly. This will assert out if
114   /// Lower==Upper and Lower != Min or Max value for its type. It will also
115   /// assert out if the two APInt's are not the same bit width.
116   ConstantRange(const APInt& Lower, const APInt& Upper);
117
118   /// isFullSet - Return true if this set contains all of the elements possible
119   /// for this data-type
120   ///
121   bool isFullSet() const;
122
123   /// isEmptySet - Return true if this set contains no members.
124   ///
125   bool isEmptySet() const;
126
127   /// isWrappedSet - Return true if this set wraps around the top of the range,
128   /// for example: [100, 8)
129   ///
130   bool isWrappedSet() const;
131
132   /// contains - Return true if the specified value is in the set.
133   ///
134   bool contains(const APInt &Val) const;
135
136   /// getSetSize - Return the number of elements in this set.
137   ///
138   APInt getSetSize() const;
139
140   /// getUnsignedMax - Return the largest unsigned value contained in the
141   /// ConstantRange.
142   ///
143   APInt getUnsignedMax() const;
144
145   /// getUnsignedMin - Return the smallest unsigned value contained in the
146   /// ConstantRange.
147   ///
148   APInt getUnsignedMin() const;
149
150   /// getSignedMax - Return the largest signed value contained in the
151   /// ConstantRange.
152   ///
153   APInt getSignedMax() const;
154
155   /// getSignedMin - Return the smallest signed value contained in the
156   /// ConstantRange.
157   ///
158   APInt getSignedMin() const;
159
160   /// subtract - Subtract the specified constant from the endpoints of this
161   /// constant range.
162   ConstantRange subtract(const APInt &CI) const;
163
164   /// intersectWith - Return the range that results from the intersection of
165   /// this range with another range.  The resultant range is pruned as much as
166   /// possible, but there may be cases where elements are included that are in
167   /// one of the sets but not the other.  For example: [100, 8) intersect [3,
168   /// 120) yields [3, 120)
169   ///
170   ConstantRange intersectWith(const ConstantRange &CR) const;
171
172   /// maximalIntersectWith - Return the range that results from the intersection
173   /// of this range with another range.  The resultant range is guaranteed to
174   /// include all elements contained in both input ranges, and to have the
175   /// smallest possible set size that does so.  Because there may be two
176   /// intersections with the same set size, A.maximalIntersectWith(B) might not
177   /// be equal to B.maximalIntersectWith(A).
178   ///
179   ConstantRange maximalIntersectWith(const ConstantRange &CR) const;
180
181   /// unionWith - Return the range that results from the union of this range
182   /// with another range.  The resultant range is guaranteed to include the
183   /// elements of both sets, but may contain more.  For example, [3, 9) union
184   /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
185   /// in either set before.
186   ///
187   ConstantRange unionWith(const ConstantRange &CR) const;
188
189   /// zeroExtend - Return a new range in the specified integer type, which must
190   /// be strictly larger than the current type.  The returned range will
191   /// correspond to the possible range of values if the source range had been
192   /// zero extended to BitWidth.
193   ConstantRange zeroExtend(uint32_t BitWidth) const;
194
195   /// signExtend - Return a new range in the specified integer type, which must
196   /// be strictly larger than the current type.  The returned range will
197   /// correspond to the possible range of values if the source range had been
198   /// sign extended to BitWidth.
199   ConstantRange signExtend(uint32_t BitWidth) const;
200
201   /// truncate - Return a new range in the specified integer type, which must be
202   /// strictly smaller than the current type.  The returned range will
203   /// correspond to the possible range of values if the source range had been
204   /// truncated to the specified type.
205   ConstantRange truncate(uint32_t BitWidth) const;
206
207   /// add - Return a new range representing the possible values resulting
208   /// from an addition of a value in this range and a value in Other.
209   ConstantRange add(const ConstantRange &Other) const;
210
211   /// multiply - Return a new range representing the possible values resulting
212   /// from a multiplication of a value in this range and a value in Other.
213   /// TODO: This isn't fully implemented yet.
214   ConstantRange multiply(const ConstantRange &Other) const;
215
216   /// smax - Return a new range representing the possible values resulting
217   /// from a signed maximum of a value in this range and a value in Other.
218   /// TODO: This isn't fully implemented yet.
219   ConstantRange smax(const ConstantRange &Other) const;
220
221   /// umax - Return a new range representing the possible values resulting
222   /// from an unsigned maximum of a value in this range and a value in Other.
223   ConstantRange umax(const ConstantRange &Other) const;
224
225   /// udiv - Return a new range representing the possible values resulting
226   /// from an unsigned division of a value in this range and a value in Other.
227   /// TODO: This isn't fully implemented yet.
228   ConstantRange udiv(const ConstantRange &Other) const;
229 };
230
231 /// ConstantRange - This class represents an range of signed values.
232 ///
233 class ConstantSignedRange : public ConstantRangeBase {
234   static ConstantSignedRange intersect1Wrapped(const ConstantSignedRange &LHS,
235                                                const ConstantSignedRange &RHS);
236 public:
237   /// Initialize a full (the default) or empty set for the specified bit width.
238   ///
239   explicit ConstantSignedRange(uint32_t BitWidth, bool isFullSet = true);
240
241   /// Initialize a range to hold the single specified value.
242   ///
243   ConstantSignedRange(const APInt &Value);
244
245   /// @brief Initialize a range of values explicitly. This will assert out if
246   /// Lower==Upper and Lower != Min or Max value for its type. It will also
247   /// assert out if the two APInt's are not the same bit width.
248   ConstantSignedRange(const APInt& Lower, const APInt& Upper);
249
250   /// isFullSet - Return true if this set contains all of the elements possible
251   /// for this data-type
252   ///
253   bool isFullSet() const;
254
255   /// isEmptySet - Return true if this set contains no members.
256   ///
257   bool isEmptySet() const;
258
259   /// isWrappedSet - Return true if this set wraps around the top of the range,
260   /// for example: [100, 8)
261   ///
262   bool isWrappedSet() const;
263
264   /// contains - Return true if the specified value is in the set.
265   ///
266   bool contains(const APInt &Val) const;
267
268   /// getSetSize - Return the number of elements in this set.
269   ///
270   APInt getSetSize() const;
271
272   /// getUnsignedMax - Return the largest unsigned value contained in the
273   /// ConstantSignedRange.
274   ///
275   APInt getUnsignedMax() const;
276
277   /// getUnsignedMin - Return the smallest unsigned value contained in the
278   /// ConstantSignedRange.
279   ///
280   APInt getUnsignedMin() const;
281
282   /// getSignedMax - Return the largest signed value contained in the
283   /// ConstantSignedRange.
284   ///
285   APInt getSignedMax() const;
286
287   /// getSignedMin - Return the smallest signed value contained in the
288   /// ConstantSignedRange.
289   ///
290   APInt getSignedMin() const;
291
292   /// subtract - Subtract the specified constant from the endpoints of this
293   /// constant range.
294   ConstantSignedRange subtract(const APInt &CI) const;
295
296   /// intersectWith - Return the range that results from the intersection of
297   /// this range with another range.  The resultant range is pruned as much as
298   /// possible, but there may be cases where elements are included that are in
299   /// one of the sets but not the other.  For example: [100, 8) intersect [3,
300   /// 120) yields [3, 120)
301   ///
302   ConstantSignedRange intersectWith(const ConstantSignedRange &CR) const;
303
304   /// maximalIntersectWith - Return the range that results from the intersection
305   /// of this range with another range.  The resultant range is guaranteed to
306   /// include all elements contained in both input ranges, and to have the
307   /// smallest possible set size that does so.  Because there may be two
308   /// intersections with the same set size, A.maximalIntersectWith(B) might not
309   /// be equal to B.maximalIntersectWith(A).
310   ///
311   ConstantSignedRange maximalIntersectWith(const ConstantSignedRange &CR) const;
312
313   /// unionWith - Return the range that results from the union of this range
314   /// with another range.  The resultant range is guaranteed to include the
315   /// elements of both sets, but may contain more.  For example, [3, 9) union
316   /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
317   /// in either set before.
318   ///
319   ConstantSignedRange unionWith(const ConstantSignedRange &CR) const;
320
321   /// zeroExtend - Return a new range in the specified integer type, which must
322   /// be strictly larger than the current type.  The returned range will
323   /// correspond to the possible range of values if the source range had been
324   /// zero extended to BitWidth.
325   ConstantSignedRange zeroExtend(uint32_t BitWidth) const;
326
327   /// signExtend - Return a new range in the specified integer type, which must
328   /// be strictly larger than the current type.  The returned range will
329   /// correspond to the possible range of values if the source range had been
330   /// sign extended to BitWidth.
331   ConstantSignedRange signExtend(uint32_t BitWidth) const;
332
333   /// truncate - Return a new range in the specified integer type, which must be
334   /// strictly smaller than the current type.  The returned range will
335   /// correspond to the possible range of values if the source range had been
336   /// truncated to the specified type.
337   ConstantSignedRange truncate(uint32_t BitWidth) const;
338
339   /// add - Return a new range representing the possible values resulting
340   /// from an addition of a value in this range and a value in Other.
341   /// TODO: This isn't fully implemented yet.
342   ConstantSignedRange add(const ConstantSignedRange &Other) const;
343
344   /// multiply - Return a new range representing the possible values resulting
345   /// from a multiplication of a value in this range and a value in Other.
346   /// TODO: This isn't fully implemented yet.
347   ConstantSignedRange multiply(const ConstantSignedRange &Other) const;
348
349   /// smax - Return a new range representing the possible values resulting
350   /// from a signed maximum of a value in this range and a value in Other.
351   ConstantSignedRange smax(const ConstantSignedRange &Other) const;
352
353   /// umax - Return a new range representing the possible values resulting
354   /// from an unsigned maximum of a value in this range and a value in Other.
355   /// TODO: This isn't fully implemented yet.
356   ConstantSignedRange umax(const ConstantSignedRange &Other) const;
357
358   /// udiv - Return a new range representing the possible values resulting
359   /// from an unsigned division of a value in this range and a value in Other.
360   /// TODO: This isn't fully implemented yet.
361   ConstantSignedRange udiv(const ConstantSignedRange &Other) const;
362 };
363
364 inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRangeBase &CR) {
365   CR.print(OS);
366   return OS;
367 }
368
369 std::ostream &operator<<(std::ostream &OS, const ConstantRangeBase &CR);
370
371 } // End llvm namespace
372
373 #endif