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_IR_CONSTANTRANGE_H
33 #define LLVM_IR_CONSTANTRANGE_H
35 #include "llvm/ADT/APInt.h"
36 #include "llvm/IR/InstrTypes.h"
37 #include "llvm/Support/DataTypes.h"
43 /// This class represents a range of values.
48 /// Initialize a full (the default) or empty set for the specified bit width.
49 explicit ConstantRange(uint32_t BitWidth, bool isFullSet = true);
51 /// Initialize a range to hold the single specified value.
52 ConstantRange(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(APInt Lower, APInt Upper);
59 /// Produce the smallest range such that all values that may satisfy the given
60 /// predicate with any value contained within Other is contained in the
61 /// returned range. Formally, this returns a superset of
62 /// 'union over all y in Other . { x : icmp op x y is true }'. If the exact
63 /// answer is not representable as a ConstantRange, the return value will be a
64 /// proper superset of the above.
66 /// Example: Pred = ult and Other = i8 [2, 5) returns Result = [0, 4)
67 static ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred,
68 const ConstantRange &Other);
70 /// Produce the largest range such that all values in the returned range
71 /// satisfy the given predicate with all values contained within Other.
72 /// Formally, this returns a subset of
73 /// 'intersection over all y in Other . { x : icmp op x y is true }'. If the
74 /// exact answer is not representable as a ConstantRange, the return value
75 /// will be a proper subset of the above.
77 /// Example: Pred = ult and Other = i8 [2, 5) returns [0, 2)
78 static ConstantRange makeSatisfyingICmpRegion(CmpInst::Predicate Pred,
79 const ConstantRange &Other);
81 /// Produce the exact range such that all values in the returned range satisfy
82 /// the given predicate with any value contained within Other. Formally, this
83 /// returns the exact answer when the superset of 'union over all y in Other
84 /// is exactly same as the subset of intersection over all y in Other.
85 /// { x : icmp op x y is true}'.
87 /// Example: Pred = ult and Other = i8 3 returns [0, 3)
88 static ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred,
91 /// Return the largest range containing all X such that "X BinOpC Y" is
92 /// guaranteed not to wrap (overflow) for all Y in Other.
94 /// NB! The returned set does *not* contain **all** possible values of X for
95 /// which "X BinOpC Y" does not wrap -- some viable values of X may be
96 /// missing, so you cannot use this to contrain X's range. E.g. in the last
97 /// example, "(-2) + 1" is both nsw and nuw (so the "X" could be -2), but (-2)
98 /// is not in the set returned.
101 /// typedef OverflowingBinaryOperator OBO;
102 /// #define MGNR makeGuaranteedNoWrapRegion
103 /// MGNR(Add, [i8 1, 2), OBO::NoSignedWrap) == [-128, 127)
104 /// MGNR(Add, [i8 1, 2), OBO::NoUnsignedWrap) == [0, -1)
105 /// MGNR(Add, [i8 0, 1), OBO::NoUnsignedWrap) == Full Set
106 /// MGNR(Add, [i8 1, 2), OBO::NoUnsignedWrap | OBO::NoSignedWrap)
108 /// MGNR(Add, [i8 -1, 6), OBO::NoSignedWrap) == [INT_MIN+1, INT_MAX-4)
109 static ConstantRange makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp,
110 const ConstantRange &Other,
111 unsigned NoWrapKind);
113 /// Set up \p Pred and \p RHS such that
114 /// ConstantRange::makeExactICmpRegion(Pred, RHS) == *this. Return true if
116 bool getEquivalentICmp(CmpInst::Predicate &Pred, APInt &RHS) const;
118 /// Return the lower value for this range.
119 const APInt &getLower() const { return Lower; }
121 /// Return the upper value for this range.
122 const APInt &getUpper() const { return Upper; }
124 /// Get the bit width of this ConstantRange.
125 uint32_t getBitWidth() const { return Lower.getBitWidth(); }
127 /// Return true if this set contains all of the elements possible
128 /// for this data-type.
129 bool isFullSet() const;
131 /// Return true if this set contains no members.
132 bool isEmptySet() const;
134 /// Return true if this set wraps around the top of the range.
135 /// For example: [100, 8).
136 bool isWrappedSet() const;
138 /// Return true if this set wraps around the INT_MIN of
139 /// its bitwidth. For example: i8 [120, 140).
140 bool isSignWrappedSet() const;
142 /// Return true if the specified value is in the set.
143 bool contains(const APInt &Val) const;
145 /// Return true if the other range is a subset of this one.
146 bool contains(const ConstantRange &CR) const;
148 /// If this set contains a single element, return it, otherwise return null.
149 const APInt *getSingleElement() const {
150 if (Upper == Lower + 1)
155 /// If this set contains all but a single element, return it, otherwise return
157 const APInt *getSingleMissingElement() const {
158 if (Lower == Upper + 1)
163 /// Return true if this set contains exactly one member.
164 bool isSingleElement() const { return getSingleElement() != nullptr; }
166 /// Return the number of elements in this set.
167 APInt getSetSize() const;
169 /// Compare set size of this range with the range CR.
170 bool isSizeStrictlySmallerThanOf(const ConstantRange &CR) const;
172 /// Return the largest unsigned value contained in the ConstantRange.
173 APInt getUnsignedMax() const;
175 /// Return the smallest unsigned value contained in the ConstantRange.
176 APInt getUnsignedMin() const;
178 /// Return the largest signed value contained in the ConstantRange.
179 APInt getSignedMax() const;
181 /// Return the smallest signed value contained in the ConstantRange.
182 APInt getSignedMin() const;
184 /// Return true if this range is equal to another range.
185 bool operator==(const ConstantRange &CR) const {
186 return Lower == CR.Lower && Upper == CR.Upper;
188 bool operator!=(const ConstantRange &CR) const {
189 return !operator==(CR);
192 /// Subtract the specified constant from the endpoints of this constant range.
193 ConstantRange subtract(const APInt &CI) const;
195 /// Subtract the specified range from this range (aka relative complement of
197 ConstantRange difference(const ConstantRange &CR) const;
199 /// Return the range that results from the intersection of
200 /// this range with another range. The resultant range is guaranteed to
201 /// include all elements contained in both input ranges, and to have the
202 /// smallest possible set size that does so. Because there may be two
203 /// intersections with the same set size, A.intersectWith(B) might not
204 /// be equal to B.intersectWith(A).
205 ConstantRange intersectWith(const ConstantRange &CR) const;
207 /// Return the range that results from the union of this range
208 /// with another range. The resultant range is guaranteed to include the
209 /// elements of both sets, but may contain more. For example, [3, 9) union
210 /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
211 /// in either set before.
212 ConstantRange unionWith(const ConstantRange &CR) const;
214 /// Return a new range representing the possible values resulting
215 /// from an application of the specified cast operator to this range. \p
216 /// BitWidth is the target bitwidth of the cast. For casts which don't
217 /// change bitwidth, it must be the same as the source bitwidth. For casts
218 /// which do change bitwidth, the bitwidth must be consistent with the
219 /// requested cast and source bitwidth.
220 ConstantRange castOp(Instruction::CastOps CastOp,
221 uint32_t BitWidth) const;
223 /// Return a new range in the specified integer type, which must
224 /// be strictly larger than the current type. The returned range will
225 /// correspond to the possible range of values if the source range had been
226 /// zero extended to BitWidth.
227 ConstantRange zeroExtend(uint32_t BitWidth) const;
229 /// Return a new range in the specified integer type, which must
230 /// be strictly larger than the current type. The returned range will
231 /// correspond to the possible range of values if the source range had been
232 /// sign extended to BitWidth.
233 ConstantRange signExtend(uint32_t BitWidth) const;
235 /// Return a new range in the specified integer type, which must be
236 /// strictly smaller than the current type. The returned range will
237 /// correspond to the possible range of values if the source range had been
238 /// truncated to the specified type.
239 ConstantRange truncate(uint32_t BitWidth) const;
241 /// Make this range have the bit width given by \p BitWidth. The
242 /// value is zero extended, truncated, or left alone to make it that width.
243 ConstantRange zextOrTrunc(uint32_t BitWidth) const;
245 /// Make this range have the bit width given by \p BitWidth. The
246 /// value is sign extended, truncated, or left alone to make it that width.
247 ConstantRange sextOrTrunc(uint32_t BitWidth) const;
249 /// Return a new range representing the possible values resulting
250 /// from an application of the specified binary operator to an left hand side
251 /// of this range and a right hand side of \p Other.
252 ConstantRange binaryOp(Instruction::BinaryOps BinOp,
253 const ConstantRange &Other) const;
255 /// Return a new range representing the possible values resulting
256 /// from an addition of a value in this range and a value in \p Other.
257 ConstantRange add(const ConstantRange &Other) const;
259 /// Return a new range representing the possible values resulting from a
260 /// known NSW addition of a value in this range and \p Other constant.
261 ConstantRange addWithNoSignedWrap(const APInt &Other) const;
263 /// Return a new range representing the possible values resulting
264 /// from a subtraction of a value in this range and a value in \p Other.
265 ConstantRange sub(const ConstantRange &Other) const;
267 /// Return a new range representing the possible values resulting
268 /// from a multiplication of a value in this range and a value in \p Other,
269 /// treating both this and \p Other as unsigned ranges.
270 ConstantRange multiply(const ConstantRange &Other) const;
272 /// Return a new range representing the possible values resulting
273 /// from a signed maximum of a value in this range and a value in \p Other.
274 ConstantRange smax(const ConstantRange &Other) const;
276 /// Return a new range representing the possible values resulting
277 /// from an unsigned maximum of a value in this range and a value in \p Other.
278 ConstantRange umax(const ConstantRange &Other) const;
280 /// Return a new range representing the possible values resulting
281 /// from a signed minimum of a value in this range and a value in \p Other.
282 ConstantRange smin(const ConstantRange &Other) const;
284 /// Return a new range representing the possible values resulting
285 /// from an unsigned minimum of a value in this range and a value in \p Other.
286 ConstantRange umin(const ConstantRange &Other) const;
288 /// Return a new range representing the possible values resulting
289 /// from an unsigned division of a value in this range and a value in
291 ConstantRange udiv(const ConstantRange &Other) const;
293 /// Return a new range representing the possible values resulting
294 /// from a binary-and of a value in this range by a value in \p Other.
295 ConstantRange binaryAnd(const ConstantRange &Other) const;
297 /// Return a new range representing the possible values resulting
298 /// from a binary-or of a value in this range by a value in \p Other.
299 ConstantRange binaryOr(const ConstantRange &Other) const;
301 /// Return a new range representing the possible values resulting
302 /// from a left shift of a value in this range by a value in \p Other.
303 /// TODO: This isn't fully implemented yet.
304 ConstantRange shl(const ConstantRange &Other) const;
306 /// Return a new range representing the possible values resulting from a
307 /// logical right shift of a value in this range and a value in \p Other.
308 ConstantRange lshr(const ConstantRange &Other) const;
310 /// Return a new range that is the logical not of the current set.
311 ConstantRange inverse() const;
313 /// Print out the bounds to a stream.
314 void print(raw_ostream &OS) const;
316 /// Allow printing from a debugger easily.
320 inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) {
325 /// Parse out a conservative ConstantRange from !range metadata.
327 /// E.g. if RangeMD is !{i32 0, i32 10, i32 15, i32 20} then return [0, 20).
328 ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD);
330 } // End llvm namespace