Clean up ConstantRange a bit:
[llvm.git] / include / llvm / Support / ConstantRange.h
blob29086b2ac4f2d302b71ef2cbb4bbf56cf13effcf
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: :
17 // [F, F) = {} = Empty set
18 // [T, F) = {T}
19 // [F, T) = {F}
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 can be used to represent either signed or
28 // unsigned ranges.
30 //===----------------------------------------------------------------------===//
32 #ifndef LLVM_SUPPORT_CONSTANT_RANGE_H
33 #define LLVM_SUPPORT_CONSTANT_RANGE_H
35 #include "llvm/ADT/APInt.h"
36 #include "llvm/System/DataTypes.h"
38 namespace llvm {
40 /// ConstantRange - This class represents an range of values.
41 ///
42 class ConstantRange {
43 APInt Lower, Upper;
45 public:
46 /// Initialize a full (the default) or empty set for the specified bit width.
47 ///
48 explicit ConstantRange(uint32_t BitWidth, bool isFullSet = true);
50 /// Initialize a range to hold the single specified value.
51 ///
52 ConstantRange(const APInt &Value);
54 /// @brief Initialize a range of values explicitly. This will assert out if
55 /// Lower==Upper and Lower != Min or Max value for its type. It will also
56 /// assert out if the two APInt's are not the same bit width.
57 ConstantRange(const APInt& Lower, const APInt& Upper);
59 /// makeICmpRegion - Produce the smallest range that contains all values that
60 /// might satisfy the comparison specified by Pred when compared to any value
61 /// contained within Other.
62 ///
63 /// Solves for range X in 'for all x in X, there exists a y in Y such that
64 /// icmp op x, y is true'. Every value that might make the comparison true
65 /// is included in the resulting range.
66 static ConstantRange makeICmpRegion(unsigned Pred,
67 const ConstantRange &Other);
69 /// getLower - Return the lower value for this range...
70 ///
71 const APInt &getLower() const { return Lower; }
73 /// getUpper - Return the upper value for this range...
74 ///
75 const APInt &getUpper() const { return Upper; }
77 /// getBitWidth - get the bit width of this ConstantRange
78 ///
79 uint32_t getBitWidth() const { return Lower.getBitWidth(); }
81 /// isFullSet - Return true if this set contains all of the elements possible
82 /// for this data-type
83 ///
84 bool isFullSet() const;
86 /// isEmptySet - Return true if this set contains no members.
87 ///
88 bool isEmptySet() const;
90 /// isWrappedSet - Return true if this set wraps around the top of the range,
91 /// for example: [100, 8)
92 ///
93 bool isWrappedSet() const;
95 /// contains - Return true if the specified value is in the set.
96 ///
97 bool contains(const APInt &Val) const;
99 /// contains - Return true if the other range is a subset of this one.
101 bool contains(const ConstantRange &CR) const;
103 /// getSingleElement - If this set contains a single element, return it,
104 /// otherwise return null.
106 const APInt *getSingleElement() const {
107 if (Upper == Lower + 1)
108 return &Lower;
109 return 0;
112 /// isSingleElement - Return true if this set contains exactly one member.
114 bool isSingleElement() const { return getSingleElement() != 0; }
116 /// getSetSize - Return the number of elements in this set.
118 APInt getSetSize() const;
120 /// getUnsignedMax - Return the largest unsigned value contained in the
121 /// ConstantRange.
123 APInt getUnsignedMax() const;
125 /// getUnsignedMin - Return the smallest unsigned value contained in the
126 /// ConstantRange.
128 APInt getUnsignedMin() const;
130 /// getSignedMax - Return the largest signed value contained in the
131 /// ConstantRange.
133 APInt getSignedMax() const;
135 /// getSignedMin - Return the smallest signed value contained in the
136 /// ConstantRange.
138 APInt getSignedMin() const;
140 /// operator== - Return true if this range is equal to another range.
142 bool operator==(const ConstantRange &CR) const {
143 return Lower == CR.Lower && Upper == CR.Upper;
145 bool operator!=(const ConstantRange &CR) const {
146 return !operator==(CR);
149 /// subtract - Subtract the specified constant from the endpoints of this
150 /// constant range.
151 ConstantRange subtract(const APInt &CI) const;
153 /// intersectWith - Return the range that results from the intersection of
154 /// this range with another range. The resultant range is guaranteed to
155 /// include all elements contained in both input ranges, and to have the
156 /// smallest possible set size that does so. Because there may be two
157 /// intersections with the same set size, A.intersectWith(B) might not
158 /// be equal to B.intersectWith(A).
160 ConstantRange intersectWith(const ConstantRange &CR) const;
162 /// unionWith - Return the range that results from the union of this range
163 /// with another range. The resultant range is guaranteed to include the
164 /// elements of both sets, but may contain more. For example, [3, 9) union
165 /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
166 /// in either set before.
168 ConstantRange unionWith(const ConstantRange &CR) const;
170 /// zeroExtend - Return a new range in the specified integer type, which must
171 /// be strictly larger than the current type. The returned range will
172 /// correspond to the possible range of values if the source range had been
173 /// zero extended to BitWidth.
174 ConstantRange zeroExtend(uint32_t BitWidth) const;
176 /// signExtend - Return a new range in the specified integer type, which must
177 /// be strictly larger than the current type. The returned range will
178 /// correspond to the possible range of values if the source range had been
179 /// sign extended to BitWidth.
180 ConstantRange signExtend(uint32_t BitWidth) const;
182 /// truncate - Return a new range in the specified integer type, which must be
183 /// strictly smaller than the current type. The returned range will
184 /// correspond to the possible range of values if the source range had been
185 /// truncated to the specified type.
186 ConstantRange truncate(uint32_t BitWidth) const;
188 /// zextOrTrunc - make this range have the bit width given by \p BitWidth. The
189 /// value is zero extended, truncated, or left alone to make it that width.
190 ConstantRange zextOrTrunc(uint32_t BitWidth) const;
192 /// sextOrTrunc - make this range have the bit width given by \p BitWidth. The
193 /// value is sign extended, truncated, or left alone to make it that width.
194 ConstantRange sextOrTrunc(uint32_t BitWidth) const;
196 /// add - Return a new range representing the possible values resulting
197 /// from an addition of a value in this range and a value in \p Other.
198 ConstantRange add(const ConstantRange &Other) const;
200 /// sub - Return a new range representing the possible values resulting
201 /// from a subtraction of a value in this range and a value in \p Other.
202 ConstantRange sub(const ConstantRange &Other) const;
204 /// multiply - Return a new range representing the possible values resulting
205 /// from a multiplication of a value in this range and a value in \p Other.
206 /// TODO: This isn't fully implemented yet.
207 ConstantRange multiply(const ConstantRange &Other) const;
209 /// smax - Return a new range representing the possible values resulting
210 /// from a signed maximum of a value in this range and a value in \p Other.
211 ConstantRange smax(const ConstantRange &Other) const;
213 /// umax - Return a new range representing the possible values resulting
214 /// from an unsigned maximum of a value in this range and a value in \p Other.
215 ConstantRange umax(const ConstantRange &Other) const;
217 /// udiv - Return a new range representing the possible values resulting
218 /// from an unsigned division of a value in this range and a value in
219 /// \p Other.
220 ConstantRange udiv(const ConstantRange &Other) const;
222 /// shl - Return a new range representing the possible values resulting
223 /// from a left shift of a value in this range by a value in \p Other.
224 /// TODO: This isn't fully implemented yet.
225 ConstantRange shl(const ConstantRange &Other) const;
227 /// lshr - Return a new range representing the possible values resulting
228 /// from a logical right shift of a value in this range and a value in
229 /// \p Other.
230 ConstantRange lshr(const ConstantRange &Other) const;
232 /// inverse - Return a new range that is the logical not of the current set.
234 ConstantRange inverse() const;
236 /// print - Print out the bounds to a stream...
238 void print(raw_ostream &OS) const;
240 /// dump - Allow printing from a debugger easily...
242 void dump() const;
245 inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) {
246 CR.print(OS);
247 return OS;
250 } // End llvm namespace
252 #endif