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.
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(APInt 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(APInt Lower, APInt Upper);
62 /// Produce the smallest range such that all values that may satisfy the given
63 /// predicate with any value contained within Other is contained in the
64 /// returned range. Formally, this returns a superset of
65 /// 'union over all y in Other . { x : icmp op x y is true }'. If the exact
66 /// answer is not representable as a ConstantRange, the return value will be a
67 /// proper superset of the above.
69 /// Example: Pred = ult and Other = i8 [2, 5) returns Result = [0, 4)
70 static ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred,
71 const ConstantRange &Other);
73 /// Produce the largest range such that all values in the returned range
74 /// satisfy the given predicate with all values contained within Other.
75 /// Formally, this returns a subset of
76 /// 'intersection over all y in Other . { x : icmp op x y is true }'. If the
77 /// exact answer is not representable as a ConstantRange, the return value
78 /// will be a proper subset of the above.
80 /// Example: Pred = ult and Other = i8 [2, 5) returns [0, 2)
81 static ConstantRange makeSatisfyingICmpRegion(CmpInst::Predicate Pred,
82 const ConstantRange &Other);
84 /// Produce the exact range such that all values in the returned range satisfy
85 /// the given predicate with any value contained within Other. Formally, this
86 /// returns the exact answer when the superset of 'union over all y in Other
87 /// is exactly same as the subset of intersection over all y in Other.
88 /// { x : icmp op x y is true}'.
90 /// Example: Pred = ult and Other = i8 3 returns [0, 3)
91 static ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred,
94 /// Return the largest range containing all X such that "X BinOpC Y" is
95 /// guaranteed not to wrap (overflow) for all Y in Other.
97 /// NB! The returned set does *not* contain **all** possible values of X for
98 /// which "X BinOpC Y" does not wrap -- some viable values of X may be
99 /// missing, so you cannot use this to contrain X's range. E.g. in the last
100 /// example, "(-2) + 1" is both nsw and nuw (so the "X" could be -2), but (-2)
101 /// is not in the set returned.
104 /// typedef OverflowingBinaryOperator OBO;
105 /// #define MGNR makeGuaranteedNoWrapRegion
106 /// MGNR(Add, [i8 1, 2), OBO::NoSignedWrap) == [-128, 127)
107 /// MGNR(Add, [i8 1, 2), OBO::NoUnsignedWrap) == [0, -1)
108 /// MGNR(Add, [i8 0, 1), OBO::NoUnsignedWrap) == Full Set
109 /// MGNR(Add, [i8 1, 2), OBO::NoUnsignedWrap | OBO::NoSignedWrap)
111 /// MGNR(Add, [i8 -1, 6), OBO::NoSignedWrap) == [INT_MIN+1, INT_MAX-4)
112 static ConstantRange makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp,
113 const ConstantRange &Other,
114 unsigned NoWrapKind);
116 /// Set up \p Pred and \p RHS such that
117 /// ConstantRange::makeExactICmpRegion(Pred, RHS) == *this. Return true if
119 bool getEquivalentICmp(CmpInst::Predicate &Pred, APInt &RHS) const;
121 /// Return the lower value for this range.
123 const APInt &getLower() const { return Lower; }
125 /// Return the upper value for this range.
127 const APInt &getUpper() const { return Upper; }
129 /// Get the bit width of this ConstantRange.
131 uint32_t getBitWidth() const { return Lower.getBitWidth(); }
133 /// Return true if this set contains all of the elements possible
134 /// for this data-type.
136 bool isFullSet() const;
138 /// Return true if this set contains no members.
140 bool isEmptySet() const;
142 /// Return true if this set wraps around the top of the range.
143 /// For example: [100, 8).
145 bool isWrappedSet() const;
147 /// Return true if this set wraps around the INT_MIN of
148 /// its bitwidth. For example: i8 [120, 140).
150 bool isSignWrappedSet() const;
152 /// Return true if the specified value is in the set.
154 bool contains(const APInt &Val) const;
156 /// Return true if the other range is a subset of this one.
158 bool contains(const ConstantRange &CR) const;
160 /// If this set contains a single element, return it, otherwise return null.
162 const APInt *getSingleElement() const {
163 if (Upper == Lower + 1)
168 /// If this set contains all but a single element, return it, otherwise return
170 const APInt *getSingleMissingElement() const {
171 if (Lower == Upper + 1)
176 /// Return true if this set contains exactly one member.
178 bool isSingleElement() const { return getSingleElement() != nullptr; }
180 /// Return the number of elements in this set.
182 APInt getSetSize() const;
184 /// Compare set size of this range with the range CR.
186 bool isSizeStrictlySmallerThanOf(const ConstantRange &CR) const;
188 /// Return the largest unsigned value contained in the ConstantRange.
190 APInt getUnsignedMax() const;
192 /// Return the smallest unsigned value contained in the ConstantRange.
194 APInt getUnsignedMin() const;
196 /// Return the largest signed value contained in the ConstantRange.
198 APInt getSignedMax() const;
200 /// Return the smallest signed value contained in the ConstantRange.
202 APInt getSignedMin() const;
204 /// Return true if this range is equal to another range.
206 bool operator==(const ConstantRange &CR) const {
207 return Lower == CR.Lower && Upper == CR.Upper;
209 bool operator!=(const ConstantRange &CR) const {
210 return !operator==(CR);
213 /// Subtract the specified constant from the endpoints of this constant range.
214 ConstantRange subtract(const APInt &CI) const;
216 /// \brief Subtract the specified range from this range (aka relative
217 /// complement of the sets).
218 ConstantRange difference(const ConstantRange &CR) const;
220 /// Return the range that results from the intersection of
221 /// this range with another range. The resultant range is guaranteed to
222 /// include all elements contained in both input ranges, and to have the
223 /// smallest possible set size that does so. Because there may be two
224 /// intersections with the same set size, A.intersectWith(B) might not
225 /// be equal to B.intersectWith(A).
227 ConstantRange intersectWith(const ConstantRange &CR) const;
229 /// Return the range that results from the union of this range
230 /// with another range. The resultant range is guaranteed to include the
231 /// elements of both sets, but may contain more. For example, [3, 9) union
232 /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
233 /// in either set before.
235 ConstantRange unionWith(const ConstantRange &CR) const;
237 /// Return a new range representing the possible values resulting
238 /// from an application of the specified cast operator to this range. \p
239 /// BitWidth is the target bitwidth of the cast. For casts which don't
240 /// change bitwidth, it must be the same as the source bitwidth. For casts
241 /// which do change bitwidth, the bitwidth must be consistent with the
242 /// requested cast and source bitwidth.
243 ConstantRange castOp(Instruction::CastOps CastOp,
244 uint32_t BitWidth) const;
246 /// Return a new range in the specified integer type, which must
247 /// be strictly larger than the current type. The returned range will
248 /// correspond to the possible range of values if the source range had been
249 /// zero extended to BitWidth.
250 ConstantRange zeroExtend(uint32_t BitWidth) const;
252 /// Return a new range in the specified integer type, which must
253 /// be strictly larger than the current type. The returned range will
254 /// correspond to the possible range of values if the source range had been
255 /// sign extended to BitWidth.
256 ConstantRange signExtend(uint32_t BitWidth) const;
258 /// Return a new range in the specified integer type, which must be
259 /// strictly smaller than the current type. The returned range will
260 /// correspond to the possible range of values if the source range had been
261 /// truncated to the specified type.
262 ConstantRange truncate(uint32_t BitWidth) const;
264 /// Make this range have the bit width given by \p BitWidth. The
265 /// value is zero extended, truncated, or left alone to make it that width.
266 ConstantRange zextOrTrunc(uint32_t BitWidth) const;
268 /// Make this range have the bit width given by \p BitWidth. The
269 /// value is sign extended, truncated, or left alone to make it that width.
270 ConstantRange sextOrTrunc(uint32_t BitWidth) const;
272 /// Return a new range representing the possible values resulting
273 /// from an application of the specified binary operator to an left hand side
274 /// of this range and a right hand side of \p Other.
275 ConstantRange binaryOp(Instruction::BinaryOps BinOp,
276 const ConstantRange &Other) const;
278 /// Return a new range representing the possible values resulting
279 /// from an addition of a value in this range and a value in \p Other.
280 ConstantRange add(const ConstantRange &Other) const;
282 /// Return a new range representing the possible values resulting from a
283 /// known NSW addition of a value in this range and \p Other constant.
284 ConstantRange addWithNoSignedWrap(const APInt &Other) const;
286 /// Return a new range representing the possible values resulting
287 /// from a subtraction of a value in this range and a value in \p Other.
288 ConstantRange sub(const ConstantRange &Other) const;
290 /// Return a new range representing the possible values resulting
291 /// from a multiplication of a value in this range and a value in \p Other,
292 /// treating both this and \p Other as unsigned ranges.
293 ConstantRange multiply(const ConstantRange &Other) const;
295 /// Return a new range representing the possible values resulting
296 /// from a signed maximum of a value in this range and a value in \p Other.
297 ConstantRange smax(const ConstantRange &Other) const;
299 /// Return a new range representing the possible values resulting
300 /// from an unsigned maximum of a value in this range and a value in \p Other.
301 ConstantRange umax(const ConstantRange &Other) const;
303 /// Return a new range representing the possible values resulting
304 /// from a signed minimum of a value in this range and a value in \p Other.
305 ConstantRange smin(const ConstantRange &Other) const;
307 /// Return a new range representing the possible values resulting
308 /// from an unsigned minimum of a value in this range and a value in \p Other.
309 ConstantRange umin(const ConstantRange &Other) const;
311 /// Return a new range representing the possible values resulting
312 /// from an unsigned division of a value in this range and a value in
314 ConstantRange udiv(const ConstantRange &Other) const;
316 /// Return a new range representing the possible values resulting
317 /// from a binary-and of a value in this range by a value in \p Other.
318 ConstantRange binaryAnd(const ConstantRange &Other) const;
320 /// Return a new range representing the possible values resulting
321 /// from a binary-or of a value in this range by a value in \p Other.
322 ConstantRange binaryOr(const ConstantRange &Other) const;
324 /// Return a new range representing the possible values resulting
325 /// from a left shift of a value in this range by a value in \p Other.
326 /// TODO: This isn't fully implemented yet.
327 ConstantRange shl(const ConstantRange &Other) const;
329 /// Return a new range representing the possible values resulting from a
330 /// logical right shift of a value in this range and a value in \p Other.
331 ConstantRange lshr(const ConstantRange &Other) const;
333 /// Return a new range that is the logical not of the current set.
335 ConstantRange inverse() const;
337 /// Print out the bounds to a stream.
339 void print(raw_ostream &OS) const;
341 /// Allow printing from a debugger easily.
346 inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) {
351 /// Parse out a conservative ConstantRange from !range metadata.
353 /// E.g. if RangeMD is !{i32 0, i32 10, i32 15, i32 20} then return [0, 20).
354 ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD);
356 } // End llvm namespace