1 //===-- llvm/Support/ConstantRange.h - Represent a range --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
17 // [F, F) = {} = Empty set
20 // [T, T) = {F, T} = Full set
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
27 // Note that ConstantRange always keeps unsigned values, and
28 // ConstantSignedRange always keeps signed values.
30 //===----------------------------------------------------------------------===//
32 #ifndef LLVM_SUPPORT_CONSTANT_RANGE_H
33 #define LLVM_SUPPORT_CONSTANT_RANGE_H
35 #include "llvm/ADT/APInt.h"
36 #include "llvm/Support/DataTypes.h"
40 /// ConstantRangeBase - A base class for ConstantRange and ConstantSignedRange.
41 /// This class implements functionality common to both.
42 class ConstantRangeBase {
46 /// Initialize a range to hold the single specified value.
48 ConstantRangeBase(const APInt &Value);
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);
56 /// getLower - Return the lower value for this range...
58 const APInt &getLower() const { return Lower; }
60 /// getUpper - Return the upper value for this range...
62 const APInt &getUpper() const { return Upper; }
64 /// getBitWidth - get the bit width of this ConstantRange
66 uint32_t getBitWidth() const { return Lower.getBitWidth(); }
68 /// getSingleElement - If this set contains a single element, return it,
69 /// otherwise return null.
71 const APInt *getSingleElement() const {
72 if (Upper == Lower + 1)
77 /// isSingleElement - Return true if this set contains exactly one member.
79 bool isSingleElement() const { return getSingleElement() != 0; }
81 /// operator== - Return true if this range is equal to another range.
83 bool operator==(const ConstantRangeBase &CR) const {
84 return Lower == CR.Lower && Upper == CR.Upper;
86 bool operator!=(const ConstantRangeBase &CR) const {
87 return !operator==(CR);
90 /// print - Print out the bounds to a stream...
92 void print(raw_ostream &OS) const;
94 /// dump - Allow printing from a debugger easily...
99 /// ConstantRange - This class represents an range of unsigned values.
101 class ConstantRange : public ConstantRangeBase {
102 static ConstantRange intersect1Wrapped(const ConstantRange &LHS,
103 const ConstantRange &RHS);
105 /// Initialize a full (the default) or empty set for the specified bit width.
107 explicit ConstantRange(uint32_t BitWidth, bool isFullSet = true);
109 /// Initialize a range to hold the single specified value.
111 ConstantRange(const APInt &Value);
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);
118 /// isFullSet - Return true if this set contains all of the elements possible
119 /// for this data-type
121 bool isFullSet() const;
123 /// isEmptySet - Return true if this set contains no members.
125 bool isEmptySet() const;
127 /// isWrappedSet - Return true if this set wraps around the top of the range,
128 /// for example: [100, 8)
130 bool isWrappedSet() const;
132 /// contains - Return true if the specified value is in the set.
134 bool contains(const APInt &Val) const;
136 /// getSetSize - Return the number of elements in this set.
138 APInt getSetSize() const;
140 /// getUnsignedMax - Return the largest unsigned value contained in the
143 APInt getUnsignedMax() const;
145 /// getUnsignedMin - Return the smallest unsigned value contained in the
148 APInt getUnsignedMin() const;
150 /// getSignedMax - Return the largest signed value contained in the
153 APInt getSignedMax() const;
155 /// getSignedMin - Return the smallest signed value contained in the
158 APInt getSignedMin() const;
160 /// subtract - Subtract the specified constant from the endpoints of this
162 ConstantRange subtract(const APInt &CI) const;
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)
170 ConstantRange intersectWith(const ConstantRange &CR) const;
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).
179 ConstantRange maximalIntersectWith(const ConstantRange &CR) const;
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.
187 ConstantRange unionWith(const ConstantRange &CR) const;
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;
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;
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;
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;
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;
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;
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;
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;
231 /// ConstantRange - This class represents an range of signed values.
233 class ConstantSignedRange : public ConstantRangeBase {
234 static ConstantSignedRange intersect1Wrapped(const ConstantSignedRange &LHS,
235 const ConstantSignedRange &RHS);
237 /// Initialize a full (the default) or empty set for the specified bit width.
239 explicit ConstantSignedRange(uint32_t BitWidth, bool isFullSet = true);
241 /// Initialize a range to hold the single specified value.
243 ConstantSignedRange(const APInt &Value);
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);
250 /// isFullSet - Return true if this set contains all of the elements possible
251 /// for this data-type
253 bool isFullSet() const;
255 /// isEmptySet - Return true if this set contains no members.
257 bool isEmptySet() const;
259 /// isWrappedSet - Return true if this set wraps around the top of the range,
260 /// for example: [100, 8)
262 bool isWrappedSet() const;
264 /// contains - Return true if the specified value is in the set.
266 bool contains(const APInt &Val) const;
268 /// getSetSize - Return the number of elements in this set.
270 APInt getSetSize() const;
272 /// getUnsignedMax - Return the largest unsigned value contained in the
273 /// ConstantSignedRange.
275 APInt getUnsignedMax() const;
277 /// getUnsignedMin - Return the smallest unsigned value contained in the
278 /// ConstantSignedRange.
280 APInt getUnsignedMin() const;
282 /// getSignedMax - Return the largest signed value contained in the
283 /// ConstantSignedRange.
285 APInt getSignedMax() const;
287 /// getSignedMin - Return the smallest signed value contained in the
288 /// ConstantSignedRange.
290 APInt getSignedMin() const;
292 /// subtract - Subtract the specified constant from the endpoints of this
294 ConstantSignedRange subtract(const APInt &CI) const;
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)
302 ConstantSignedRange intersectWith(const ConstantSignedRange &CR) const;
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).
311 ConstantSignedRange maximalIntersectWith(const ConstantSignedRange &CR) const;
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.
319 ConstantSignedRange unionWith(const ConstantSignedRange &CR) const;
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;
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;
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;
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;
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;
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;
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;
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;
364 inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRangeBase &CR) {
369 std::ostream &operator<<(std::ostream &OS, const ConstantRangeBase &CR);
371 } // End llvm namespace