Add a convenience constructor.
[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 can be used to represent either signed or
28 // unsigned ranges.
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/System/DataTypes.h"
37
38 namespace llvm {
39
40 class ConstantInt;
41
42 /// ConstantRange - This class represents an range of values.
43 ///
44 class ConstantRange {
45   APInt Lower, Upper;
46   static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
47                                          const ConstantRange &RHS);
48
49 public:
50   /// Initialize a full (the default) or empty set for the specified bit width.
51   ///
52   explicit ConstantRange(uint32_t BitWidth, bool isFullSet = true);
53
54   /// Initialize a range to hold the single specified value.
55   ///
56   ConstantRange(const APInt &Value);
57   ConstantRange(const ConstantInt *Value);
58
59   /// @brief Initialize a range of values explicitly. This will assert out if
60   /// Lower==Upper and Lower != Min or Max value for its type. It will also
61   /// assert out if the two APInt's are not the same bit width.
62   ConstantRange(const APInt& Lower, const APInt& Upper);
63
64   /// makeICmpRegion - Produce the smallest range that contains all values that
65   /// might satisfy the comparison specified by Pred when compared to any value
66   /// contained within Other.
67   ///
68   /// Solves for range X in 'for all x in X, there exists a y in Y such that
69   /// icmp op x, y is true'. Every value that might make the comparison true
70   /// is included in the resulting range.
71   static ConstantRange makeICmpRegion(unsigned Pred,
72                                       const ConstantRange &Other);
73
74   /// getLower - Return the lower value for this range...
75   ///
76   const APInt &getLower() const { return Lower; }
77
78   /// getUpper - Return the upper value for this range...
79   ///
80   const APInt &getUpper() const { return Upper; }
81
82   /// getBitWidth - get the bit width of this ConstantRange
83   ///
84   uint32_t getBitWidth() const { return Lower.getBitWidth(); }
85
86   /// isFullSet - Return true if this set contains all of the elements possible
87   /// for this data-type
88   ///
89   bool isFullSet() const;
90
91   /// isEmptySet - Return true if this set contains no members.
92   ///
93   bool isEmptySet() const;
94
95   /// isWrappedSet - Return true if this set wraps around the top of the range,
96   /// for example: [100, 8)
97   ///
98   bool isWrappedSet() const;
99
100   /// contains - Return true if the specified value is in the set.
101   ///
102   bool contains(const APInt &Val) const;
103
104   /// contains - Return true if the other range is a subset of this one.
105   ///
106   bool contains(const ConstantRange &CR) const;
107
108   /// getSingleElement - If this set contains a single element, return it,
109   /// otherwise return null.
110   ///
111   const APInt *getSingleElement() const {
112     if (Upper == Lower + 1)
113       return &Lower;
114     return 0;
115   }
116
117   /// isSingleElement - Return true if this set contains exactly one member.
118   ///
119   bool isSingleElement() const { return getSingleElement() != 0; }
120
121   /// getSetSize - Return the number of elements in this set.
122   ///
123   APInt getSetSize() const;
124
125   /// getUnsignedMax - Return the largest unsigned value contained in the
126   /// ConstantRange.
127   ///
128   APInt getUnsignedMax() const;
129
130   /// getUnsignedMin - Return the smallest unsigned value contained in the
131   /// ConstantRange.
132   ///
133   APInt getUnsignedMin() const;
134
135   /// getSignedMax - Return the largest signed value contained in the
136   /// ConstantRange.
137   ///
138   APInt getSignedMax() const;
139
140   /// getSignedMin - Return the smallest signed value contained in the
141   /// ConstantRange.
142   ///
143   APInt getSignedMin() const;
144
145   /// operator= - Copy one ConstantRange over another.
146   void operator=(const ConstantRange &CR) {
147     Lower = CR.Lower;
148     Upper = CR.Upper;
149   }
150
151   /// operator== - Return true if this range is equal to another range.
152   ///
153   bool operator==(const ConstantRange &CR) const {
154     return Lower == CR.Lower && Upper == CR.Upper;
155   }
156   bool operator!=(const ConstantRange &CR) const {
157     return !operator==(CR);
158   }
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 guaranteed to
166   /// include all elements contained in both input ranges, and to have the
167   /// smallest possible set size that does so.  Because there may be two
168   /// intersections with the same set size, A.intersectWith(B) might not
169   /// be equal to B.intersectWith(A).
170   ///
171   ConstantRange intersectWith(const ConstantRange &CR) const;
172
173   /// unionWith - Return the range that results from the union of this range
174   /// with another range.  The resultant range is guaranteed to include the
175   /// elements of both sets, but may contain more.  For example, [3, 9) union
176   /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
177   /// in either set before.
178   ///
179   ConstantRange unionWith(const ConstantRange &CR) const;
180
181   /// zeroExtend - Return a new range in the specified integer type, which must
182   /// be strictly larger than the current type.  The returned range will
183   /// correspond to the possible range of values if the source range had been
184   /// zero extended to BitWidth.
185   ConstantRange zeroExtend(uint32_t BitWidth) const;
186
187   /// signExtend - Return a new range in the specified integer type, which must
188   /// be strictly larger than the current type.  The returned range will
189   /// correspond to the possible range of values if the source range had been
190   /// sign extended to BitWidth.
191   ConstantRange signExtend(uint32_t BitWidth) const;
192
193   /// truncate - Return a new range in the specified integer type, which must be
194   /// strictly smaller than the current type.  The returned range will
195   /// correspond to the possible range of values if the source range had been
196   /// truncated to the specified type.
197   ConstantRange truncate(uint32_t BitWidth) const;
198
199   /// zextOrTrunc - make this range have the bit width given by \p BitWidth. The
200   /// value is zero extended, truncated, or left alone to make it that width.
201   ConstantRange zextOrTrunc(uint32_t BitWidth) const;
202   
203   /// sextOrTrunc - make this range have the bit width given by \p BitWidth. The
204   /// value is sign extended, truncated, or left alone to make it that width.
205   ConstantRange sextOrTrunc(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   ConstantRange smax(const ConstantRange &Other) const;
219
220   /// umax - Return a new range representing the possible values resulting
221   /// from an unsigned maximum of a value in this range and a value in Other.
222   ConstantRange umax(const ConstantRange &Other) const;
223
224   /// udiv - Return a new range representing the possible values resulting
225   /// from an unsigned division of a value in this range and a value in Other.
226   /// TODO: This isn't fully implemented yet.
227   ConstantRange udiv(const ConstantRange &Other) const;
228
229   /// shl - Return a new range representing the possible values resulting
230   /// from a left shift of a value in this range by the Amount value.
231   ConstantRange shl(const ConstantRange &Amount) const;
232
233   /// ashr - Return a new range representing the possible values resulting from
234   /// an arithmetic right shift of a value in this range by the Amount value.
235   ConstantRange ashr(const ConstantRange &Amount) const;
236
237   /// shr - Return a new range representing the possible values resulting
238   /// from a logical right shift of a value in this range by the Amount value.
239   ConstantRange lshr(const ConstantRange &Amount) const;
240
241   /// print - Print out the bounds to a stream...
242   ///
243   void print(raw_ostream &OS) const;
244
245   /// dump - Allow printing from a debugger easily...
246   ///
247   void dump() const;
248 };
249
250 inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) {
251   CR.print(OS);
252   return OS;
253 }
254
255 } // End llvm namespace
256
257 #endif