1 //===- 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 can be used to represent either signed or
30 //===----------------------------------------------------------------------===//
32 #ifndef LLVM_SUPPORT_CONSTANTRANGE_H
33 #define LLVM_SUPPORT_CONSTANTRANGE_H
35 #include "llvm/ADT/APInt.h"
36 #include "llvm/Support/DataTypes.h"
40 /// ConstantRange - This class represents an range of values.
45 // If we have move semantics, pass APInts by value and move them into place.
46 typedef APInt APIntMoveTy;
49 /// Initialize a full (the default) or empty set for the specified bit width.
51 explicit ConstantRange(uint32_t BitWidth, bool isFullSet = true);
53 /// Initialize a range to hold the single specified value.
55 ConstantRange(APIntMoveTy Value);
57 /// @brief Initialize a range of values explicitly. This will assert out if
58 /// Lower==Upper and Lower != Min or Max value for its type. It will also
59 /// assert out if the two APInt's are not the same bit width.
60 ConstantRange(APIntMoveTy Lower, APIntMoveTy Upper);
62 /// makeICmpRegion - Produce the smallest range that contains all values that
63 /// might satisfy the comparison specified by Pred when compared to any value
64 /// contained within Other.
66 /// Solves for range X in 'for all x in X, there exists a y in Y such that
67 /// icmp op x, y is true'. Every value that might make the comparison true
68 /// is included in the resulting range.
69 static ConstantRange makeICmpRegion(unsigned Pred,
70 const ConstantRange &Other);
72 /// getLower - Return the lower value for this range...
74 const APInt &getLower() const { return Lower; }
76 /// getUpper - Return the upper value for this range...
78 const APInt &getUpper() const { return Upper; }
80 /// getBitWidth - get the bit width of this ConstantRange
82 uint32_t getBitWidth() const { return Lower.getBitWidth(); }
84 /// isFullSet - Return true if this set contains all of the elements possible
85 /// for this data-type
87 bool isFullSet() const;
89 /// isEmptySet - Return true if this set contains no members.
91 bool isEmptySet() const;
93 /// isWrappedSet - Return true if this set wraps around the top of the range,
94 /// for example: [100, 8)
96 bool isWrappedSet() const;
98 /// isSignWrappedSet - Return true if this set wraps around the INT_MIN of
99 /// its bitwidth, for example: i8 [120, 140).
101 bool isSignWrappedSet() const;
103 /// contains - Return true if the specified value is in the set.
105 bool contains(const APInt &Val) const;
107 /// contains - Return true if the other range is a subset of this one.
109 bool contains(const ConstantRange &CR) const;
111 /// getSingleElement - If this set contains a single element, return it,
112 /// otherwise return null.
114 const APInt *getSingleElement() const {
115 if (Upper == Lower + 1)
120 /// isSingleElement - Return true if this set contains exactly one member.
122 bool isSingleElement() const { return getSingleElement() != nullptr; }
124 /// getSetSize - Return the number of elements in this set.
126 APInt getSetSize() const;
128 /// getUnsignedMax - Return the largest unsigned value contained in the
131 APInt getUnsignedMax() const;
133 /// getUnsignedMin - Return the smallest unsigned value contained in the
136 APInt getUnsignedMin() const;
138 /// getSignedMax - Return the largest signed value contained in the
141 APInt getSignedMax() const;
143 /// getSignedMin - Return the smallest signed value contained in the
146 APInt getSignedMin() const;
148 /// operator== - Return true if this range is equal to another range.
150 bool operator==(const ConstantRange &CR) const {
151 return Lower == CR.Lower && Upper == CR.Upper;
153 bool operator!=(const ConstantRange &CR) const {
154 return !operator==(CR);
157 /// subtract - Subtract the specified constant from the endpoints of this
159 ConstantRange subtract(const APInt &CI) const;
161 /// \brief Subtract the specified range from this range (aka relative
162 /// complement of the sets).
163 ConstantRange difference(const ConstantRange &CR) const;
165 /// intersectWith - Return the range that results from the intersection of
166 /// this range with another range. The resultant range is guaranteed to
167 /// include all elements contained in both input ranges, and to have the
168 /// smallest possible set size that does so. Because there may be two
169 /// intersections with the same set size, A.intersectWith(B) might not
170 /// be equal to B.intersectWith(A).
172 ConstantRange intersectWith(const ConstantRange &CR) const;
174 /// unionWith - Return the range that results from the union of this range
175 /// with another range. The resultant range is guaranteed to include the
176 /// elements of both sets, but may contain more. For example, [3, 9) union
177 /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
178 /// in either set before.
180 ConstantRange unionWith(const ConstantRange &CR) const;
182 /// zeroExtend - Return a new range in the specified integer type, which must
183 /// be strictly larger than the current type. The returned range will
184 /// correspond to the possible range of values if the source range had been
185 /// zero extended to BitWidth.
186 ConstantRange zeroExtend(uint32_t BitWidth) const;
188 /// signExtend - Return a new range in the specified integer type, which must
189 /// be strictly larger than the current type. The returned range will
190 /// correspond to the possible range of values if the source range had been
191 /// sign extended to BitWidth.
192 ConstantRange signExtend(uint32_t BitWidth) const;
194 /// truncate - Return a new range in the specified integer type, which must be
195 /// strictly smaller than the current type. The returned range will
196 /// correspond to the possible range of values if the source range had been
197 /// truncated to the specified type.
198 ConstantRange truncate(uint32_t BitWidth) const;
200 /// zextOrTrunc - make this range have the bit width given by \p BitWidth. The
201 /// value is zero extended, truncated, or left alone to make it that width.
202 ConstantRange zextOrTrunc(uint32_t BitWidth) const;
204 /// sextOrTrunc - make this range have the bit width given by \p BitWidth. The
205 /// value is sign extended, truncated, or left alone to make it that width.
206 ConstantRange sextOrTrunc(uint32_t BitWidth) const;
208 /// add - Return a new range representing the possible values resulting
209 /// from an addition of a value in this range and a value in \p Other.
210 ConstantRange add(const ConstantRange &Other) const;
212 /// sub - Return a new range representing the possible values resulting
213 /// from a subtraction of a value in this range and a value in \p Other.
214 ConstantRange sub(const ConstantRange &Other) const;
216 /// multiply - Return a new range representing the possible values resulting
217 /// from a multiplication of a value in this range and a value in \p Other.
218 /// TODO: This isn't fully implemented yet.
219 ConstantRange multiply(const ConstantRange &Other) const;
221 /// smax - Return a new range representing the possible values resulting
222 /// from a signed maximum of a value in this range and a value in \p Other.
223 ConstantRange smax(const ConstantRange &Other) const;
225 /// umax - Return a new range representing the possible values resulting
226 /// from an unsigned maximum of a value in this range and a value in \p Other.
227 ConstantRange umax(const ConstantRange &Other) const;
229 /// udiv - Return a new range representing the possible values resulting
230 /// from an unsigned division of a value in this range and a value in
232 ConstantRange udiv(const ConstantRange &Other) const;
234 /// binaryAnd - return a new range representing the possible values resulting
235 /// from a binary-and of a value in this range by a value in \p Other.
236 ConstantRange binaryAnd(const ConstantRange &Other) const;
238 /// binaryOr - return a new range representing the possible values resulting
239 /// from a binary-or of a value in this range by a value in \p Other.
240 ConstantRange binaryOr(const ConstantRange &Other) const;
242 /// shl - Return a new range representing the possible values resulting
243 /// from a left shift of a value in this range by a value in \p Other.
244 /// TODO: This isn't fully implemented yet.
245 ConstantRange shl(const ConstantRange &Other) const;
247 /// lshr - Return a new range representing the possible values resulting
248 /// from a logical right shift of a value in this range and a value in
250 ConstantRange lshr(const ConstantRange &Other) const;
252 /// inverse - Return a new range that is the logical not of the current set.
254 ConstantRange inverse() const;
256 /// print - Print out the bounds to a stream...
258 void print(raw_ostream &OS) const;
260 /// dump - Allow printing from a debugger easily...
265 inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) {
270 } // End llvm namespace