xref: /freebsd/contrib/llvm-project/llvm/include/llvm/IR/ConstantRange.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- ConstantRange.h - Represent a range ----------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Represent a range of possible values that may occur when the program is run
10 // for an integral value.  This keeps track of a lower and upper bound for the
11 // constant, which MAY wrap around the end of the numeric range.  To do this, it
12 // keeps track of a [lower, upper) bound, which specifies an interval just like
13 // STL iterators.  When used with boolean values, the following are important
14 // ranges: :
15 //
16 //  [F, F) = {}     = Empty set
17 //  [T, F) = {T}
18 //  [F, T) = {F}
19 //  [T, T) = {F, T} = Full set
20 //
21 // The other integral ranges use min/max values for special range values. For
22 // example, for 8-bit types, it uses:
23 // [0, 0)     = {}       = Empty set
24 // [255, 255) = {0..255} = Full Set
25 //
26 // Note that ConstantRange can be used to represent either signed or
27 // unsigned ranges.
28 //
29 //===----------------------------------------------------------------------===//
30 
31 #ifndef LLVM_IR_CONSTANTRANGE_H
32 #define LLVM_IR_CONSTANTRANGE_H
33 
34 #include "llvm/ADT/APInt.h"
35 #include "llvm/IR/InstrTypes.h"
36 #include "llvm/IR/Instruction.h"
37 #include "llvm/Support/Compiler.h"
38 #include <cstdint>
39 
40 namespace llvm {
41 
42 class MDNode;
43 class raw_ostream;
44 struct KnownBits;
45 
46 /// This class represents a range of values.
47 class [[nodiscard]] ConstantRange {
48   APInt Lower, Upper;
49 
50   /// Create empty constant range with same bitwidth.
getEmpty()51   ConstantRange getEmpty() const {
52     return ConstantRange(getBitWidth(), false);
53   }
54 
55   /// Create full constant range with same bitwidth.
getFull()56   ConstantRange getFull() const {
57     return ConstantRange(getBitWidth(), true);
58   }
59 
60 public:
61   /// Initialize a full or empty set for the specified bit width.
62   LLVM_ABI explicit ConstantRange(uint32_t BitWidth, bool isFullSet);
63 
64   /// Initialize a range to hold the single specified value.
65   LLVM_ABI ConstantRange(APInt Value);
66 
67   /// Initialize a range of values explicitly. This will assert out if
68   /// Lower==Upper and Lower != Min or Max value for its type. It will also
69   /// assert out if the two APInt's are not the same bit width.
70   LLVM_ABI ConstantRange(APInt Lower, APInt Upper);
71 
72   /// Create empty constant range with the given bit width.
getEmpty(uint32_t BitWidth)73   static ConstantRange getEmpty(uint32_t BitWidth) {
74     return ConstantRange(BitWidth, false);
75   }
76 
77   /// Create full constant range with the given bit width.
getFull(uint32_t BitWidth)78   static ConstantRange getFull(uint32_t BitWidth) {
79     return ConstantRange(BitWidth, true);
80   }
81 
82   /// Create non-empty constant range with the given bounds. If Lower and
83   /// Upper are the same, a full range is returned.
getNonEmpty(APInt Lower,APInt Upper)84   static ConstantRange getNonEmpty(APInt Lower, APInt Upper) {
85     if (Lower == Upper)
86       return getFull(Lower.getBitWidth());
87     return ConstantRange(std::move(Lower), std::move(Upper));
88   }
89 
90   /// Initialize a range based on a known bits constraint. The IsSigned flag
91   /// indicates whether the constant range should not wrap in the signed or
92   /// unsigned domain.
93   LLVM_ABI static ConstantRange fromKnownBits(const KnownBits &Known,
94                                               bool IsSigned);
95 
96   /// Split the ConstantRange into positive and negative components, ignoring
97   /// zero values.
98   LLVM_ABI std::pair<ConstantRange, ConstantRange> splitPosNeg() const;
99 
100   /// Produce the smallest range such that all values that may satisfy the given
101   /// predicate with any value contained within Other is contained in the
102   /// returned range.  Formally, this returns a superset of
103   /// 'union over all y in Other . { x : icmp op x y is true }'.  If the exact
104   /// answer is not representable as a ConstantRange, the return value will be a
105   /// proper superset of the above.
106   ///
107   /// Example: Pred = ult and Other = i8 [2, 5) returns Result = [0, 4)
108   LLVM_ABI static ConstantRange
109   makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other);
110 
111   /// Produce the largest range such that all values in the returned range
112   /// satisfy the given predicate with all values contained within Other.
113   /// Formally, this returns a subset of
114   /// 'intersection over all y in Other . { x : icmp op x y is true }'.  If the
115   /// exact answer is not representable as a ConstantRange, the return value
116   /// will be a proper subset of the above.
117   ///
118   /// Example: Pred = ult and Other = i8 [2, 5) returns [0, 2)
119   LLVM_ABI static ConstantRange
120   makeSatisfyingICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other);
121 
122   /// Produce the exact range such that all values in the returned range satisfy
123   /// the given predicate with any value contained within Other. Formally, this
124   /// returns the exact answer when the superset of 'union over all y in Other
125   /// is exactly same as the subset of intersection over all y in Other.
126   /// { x : icmp op x y is true}'.
127   ///
128   /// Example: Pred = ult and Other = i8 3 returns [0, 3)
129   LLVM_ABI static ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred,
130                                                     const APInt &Other);
131 
132   /// Does the predicate \p Pred hold between ranges this and \p Other?
133   /// NOTE: false does not mean that inverse predicate holds!
134   LLVM_ABI bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const;
135 
136   /// Return true iff CR1 ult CR2 is equivalent to CR1 slt CR2.
137   /// Does not depend on strictness/direction of the predicate.
138   LLVM_ABI static bool
139   areInsensitiveToSignednessOfICmpPredicate(const ConstantRange &CR1,
140                                             const ConstantRange &CR2);
141 
142   /// Return true iff CR1 ult CR2 is equivalent to CR1 sge CR2.
143   /// Does not depend on strictness/direction of the predicate.
144   LLVM_ABI static bool
145   areInsensitiveToSignednessOfInvertedICmpPredicate(const ConstantRange &CR1,
146                                                     const ConstantRange &CR2);
147 
148   /// If the comparison between constant ranges this and Other
149   /// is insensitive to the signedness of the comparison predicate,
150   /// return a predicate equivalent to \p Pred, with flipped signedness
151   /// (i.e. unsigned instead of signed or vice versa), and maybe inverted,
152   /// otherwise returns CmpInst::Predicate::BAD_ICMP_PREDICATE.
153   LLVM_ABI static CmpInst::Predicate
154   getEquivalentPredWithFlippedSignedness(CmpInst::Predicate Pred,
155                                          const ConstantRange &CR1,
156                                          const ConstantRange &CR2);
157 
158   /// Produce the largest range containing all X such that "X BinOp Y" is
159   /// guaranteed not to wrap (overflow) for *all* Y in Other. However, there may
160   /// be *some* Y in Other for which additional X not contained in the result
161   /// also do not overflow.
162   ///
163   /// NoWrapKind must be one of OBO::NoUnsignedWrap or OBO::NoSignedWrap.
164   ///
165   /// Examples:
166   ///  typedef OverflowingBinaryOperator OBO;
167   ///  #define MGNR makeGuaranteedNoWrapRegion
168   ///  MGNR(Add, [i8 1, 2), OBO::NoSignedWrap) == [-128, 127)
169   ///  MGNR(Add, [i8 1, 2), OBO::NoUnsignedWrap) == [0, -1)
170   ///  MGNR(Add, [i8 0, 1), OBO::NoUnsignedWrap) == Full Set
171   ///  MGNR(Add, [i8 -1, 6), OBO::NoSignedWrap) == [INT_MIN+1, INT_MAX-4)
172   ///  MGNR(Sub, [i8 1, 2), OBO::NoSignedWrap) == [-127, 128)
173   ///  MGNR(Sub, [i8 1, 2), OBO::NoUnsignedWrap) == [1, 0)
174   LLVM_ABI static ConstantRange
175   makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp,
176                              const ConstantRange &Other, unsigned NoWrapKind);
177 
178   /// Produce the range that contains X if and only if "X BinOp Other" does
179   /// not wrap.
180   LLVM_ABI static ConstantRange
181   makeExactNoWrapRegion(Instruction::BinaryOps BinOp, const APInt &Other,
182                         unsigned NoWrapKind);
183 
184   /// Initialize a range containing all values X that satisfy `(X & Mask)
185   /// != C`. Note that the range returned may contain values where `(X & Mask)
186   /// == C` holds, making it less precise, but still conservative.
187   LLVM_ABI static ConstantRange makeMaskNotEqualRange(const APInt &Mask,
188                                                       const APInt &C);
189 
190   /// Returns true if ConstantRange calculations are supported for intrinsic
191   /// with \p IntrinsicID.
192   LLVM_ABI static bool isIntrinsicSupported(Intrinsic::ID IntrinsicID);
193 
194   /// Compute range of intrinsic result for the given operand ranges.
195   LLVM_ABI static ConstantRange intrinsic(Intrinsic::ID IntrinsicID,
196                                           ArrayRef<ConstantRange> Ops);
197 
198   /// Set up \p Pred and \p RHS such that
199   /// ConstantRange::makeExactICmpRegion(Pred, RHS) == *this.  Return true if
200   /// successful.
201   LLVM_ABI bool getEquivalentICmp(CmpInst::Predicate &Pred, APInt &RHS) const;
202 
203   /// Set up \p Pred, \p RHS and \p Offset such that (V + Offset) Pred RHS
204   /// is true iff V is in the range. Prefers using Offset == 0 if possible.
205   LLVM_ABI void getEquivalentICmp(CmpInst::Predicate &Pred, APInt &RHS,
206                                   APInt &Offset) const;
207 
208   /// Return the lower value for this range.
getLower()209   const APInt &getLower() const { return Lower; }
210 
211   /// Return the upper value for this range.
getUpper()212   const APInt &getUpper() const { return Upper; }
213 
214   /// Get the bit width of this ConstantRange.
getBitWidth()215   uint32_t getBitWidth() const { return Lower.getBitWidth(); }
216 
217   /// Return true if this set contains all of the elements possible
218   /// for this data-type.
219   LLVM_ABI bool isFullSet() const;
220 
221   /// Return true if this set contains no members.
222   LLVM_ABI bool isEmptySet() const;
223 
224   /// Return true if this set wraps around the unsigned domain. Special cases:
225   ///  * Empty set: Not wrapped.
226   ///  * Full set: Not wrapped.
227   ///  * [X, 0) == [X, Max]: Not wrapped.
228   LLVM_ABI bool isWrappedSet() const;
229 
230   /// Return true if the exclusive upper bound wraps around the unsigned
231   /// domain. Special cases:
232   ///  * Empty set: Not wrapped.
233   ///  * Full set: Not wrapped.
234   ///  * [X, 0): Wrapped.
235   LLVM_ABI bool isUpperWrapped() const;
236 
237   /// Return true if this set wraps around the signed domain. Special cases:
238   ///  * Empty set: Not wrapped.
239   ///  * Full set: Not wrapped.
240   ///  * [X, SignedMin) == [X, SignedMax]: Not wrapped.
241   LLVM_ABI bool isSignWrappedSet() const;
242 
243   /// Return true if the (exclusive) upper bound wraps around the signed
244   /// domain. Special cases:
245   ///  * Empty set: Not wrapped.
246   ///  * Full set: Not wrapped.
247   ///  * [X, SignedMin): Wrapped.
248   LLVM_ABI bool isUpperSignWrapped() const;
249 
250   /// Return true if the specified value is in the set.
251   LLVM_ABI bool contains(const APInt &Val) const;
252 
253   /// Return true if the other range is a subset of this one.
254   LLVM_ABI bool contains(const ConstantRange &CR) const;
255 
256   /// If this set contains a single element, return it, otherwise return null.
getSingleElement()257   const APInt *getSingleElement() const {
258     if (Upper == Lower + 1)
259       return &Lower;
260     return nullptr;
261   }
262 
263   /// If this set contains all but a single element, return it, otherwise return
264   /// null.
getSingleMissingElement()265   const APInt *getSingleMissingElement() const {
266     if (Lower == Upper + 1)
267       return &Upper;
268     return nullptr;
269   }
270 
271   /// Return true if this set contains exactly one member.
isSingleElement()272   bool isSingleElement() const { return getSingleElement() != nullptr; }
273 
274   /// Compare set size of this range with the range CR.
275   LLVM_ABI bool isSizeStrictlySmallerThan(const ConstantRange &CR) const;
276 
277   /// Compare set size of this range with Value.
278   LLVM_ABI bool isSizeLargerThan(uint64_t MaxSize) const;
279 
280   /// Return true if all values in this range are negative.
281   LLVM_ABI bool isAllNegative() const;
282 
283   /// Return true if all values in this range are non-negative.
284   LLVM_ABI bool isAllNonNegative() const;
285 
286   /// Return true if all values in this range are positive.
287   LLVM_ABI bool isAllPositive() const;
288 
289   /// Return the largest unsigned value contained in the ConstantRange.
290   LLVM_ABI APInt getUnsignedMax() const;
291 
292   /// Return the smallest unsigned value contained in the ConstantRange.
293   LLVM_ABI APInt getUnsignedMin() const;
294 
295   /// Return the largest signed value contained in the ConstantRange.
296   LLVM_ABI APInt getSignedMax() const;
297 
298   /// Return the smallest signed value contained in the ConstantRange.
299   LLVM_ABI APInt getSignedMin() const;
300 
301   /// Return true if this range is equal to another range.
302   bool operator==(const ConstantRange &CR) const {
303     return Lower == CR.Lower && Upper == CR.Upper;
304   }
305   bool operator!=(const ConstantRange &CR) const {
306     return !operator==(CR);
307   }
308 
309   /// Compute the maximal number of active bits needed to represent every value
310   /// in this range.
311   LLVM_ABI unsigned getActiveBits() const;
312 
313   /// Compute the maximal number of bits needed to represent every value
314   /// in this signed range.
315   LLVM_ABI unsigned getMinSignedBits() const;
316 
317   /// Subtract the specified constant from the endpoints of this constant range.
318   LLVM_ABI ConstantRange subtract(const APInt &CI) const;
319 
320   /// Subtract the specified range from this range (aka relative complement of
321   /// the sets).
322   LLVM_ABI ConstantRange difference(const ConstantRange &CR) const;
323 
324   /// If represented precisely, the result of some range operations may consist
325   /// of multiple disjoint ranges. As only a single range may be returned, any
326   /// range covering these disjoint ranges constitutes a valid result, but some
327   /// may be more useful than others depending on context. The preferred range
328   /// type specifies whether a range that is non-wrapping in the unsigned or
329   /// signed domain, or has the smallest size, is preferred. If a signedness is
330   /// preferred but all ranges are non-wrapping or all wrapping, then the
331   /// smallest set size is preferred. If there are multiple smallest sets, any
332   /// one of them may be returned.
333   enum PreferredRangeType { Smallest, Unsigned, Signed };
334 
335   /// Return the range that results from the intersection of this range with
336   /// another range. If the intersection is disjoint, such that two results
337   /// are possible, the preferred range is determined by the PreferredRangeType.
338   LLVM_ABI ConstantRange intersectWith(
339       const ConstantRange &CR, PreferredRangeType Type = Smallest) const;
340 
341   /// Return the range that results from the union of this range
342   /// with another range.  The resultant range is guaranteed to include the
343   /// elements of both sets, but may contain more.  For example, [3, 9) union
344   /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
345   /// in either set before.
346   LLVM_ABI ConstantRange unionWith(const ConstantRange &CR,
347                                    PreferredRangeType Type = Smallest) const;
348 
349   /// Intersect the two ranges and return the result if it can be represented
350   /// exactly, otherwise return std::nullopt.
351   LLVM_ABI std::optional<ConstantRange>
352   exactIntersectWith(const ConstantRange &CR) const;
353 
354   /// Union the two ranges and return the result if it can be represented
355   /// exactly, otherwise return std::nullopt.
356   LLVM_ABI std::optional<ConstantRange>
357   exactUnionWith(const ConstantRange &CR) const;
358 
359   /// Return a new range representing the possible values resulting
360   /// from an application of the specified cast operator to this range. \p
361   /// BitWidth is the target bitwidth of the cast.  For casts which don't
362   /// change bitwidth, it must be the same as the source bitwidth.  For casts
363   /// which do change bitwidth, the bitwidth must be consistent with the
364   /// requested cast and source bitwidth.
365   LLVM_ABI ConstantRange castOp(Instruction::CastOps CastOp,
366                                 uint32_t BitWidth) const;
367 
368   /// Return a new range in the specified integer type, which must
369   /// be strictly larger than the current type.  The returned range will
370   /// correspond to the possible range of values if the source range had been
371   /// zero extended to BitWidth.
372   LLVM_ABI ConstantRange zeroExtend(uint32_t BitWidth) const;
373 
374   /// Return a new range in the specified integer type, which must
375   /// be strictly larger than the current type.  The returned range will
376   /// correspond to the possible range of values if the source range had been
377   /// sign extended to BitWidth.
378   LLVM_ABI ConstantRange signExtend(uint32_t BitWidth) const;
379 
380   /// Return a new range in the specified integer type, which must be
381   /// strictly smaller than the current type.  The returned range will
382   /// correspond to the possible range of values if the source range had been
383   /// truncated to the specified type.
384   LLVM_ABI ConstantRange truncate(uint32_t BitWidth) const;
385 
386   /// Make this range have the bit width given by \p BitWidth. The
387   /// value is zero extended, truncated, or left alone to make it that width.
388   LLVM_ABI ConstantRange zextOrTrunc(uint32_t BitWidth) const;
389 
390   /// Make this range have the bit width given by \p BitWidth. The
391   /// value is sign extended, truncated, or left alone to make it that width.
392   LLVM_ABI ConstantRange sextOrTrunc(uint32_t BitWidth) const;
393 
394   /// Return a new range representing the possible values resulting
395   /// from an application of the specified binary operator to an left hand side
396   /// of this range and a right hand side of \p Other.
397   LLVM_ABI ConstantRange binaryOp(Instruction::BinaryOps BinOp,
398                                   const ConstantRange &Other) const;
399 
400   /// Return a new range representing the possible values resulting
401   /// from an application of the specified overflowing binary operator to a
402   /// left hand side of this range and a right hand side of \p Other given
403   /// the provided knowledge about lack of wrapping \p NoWrapKind.
404   LLVM_ABI ConstantRange overflowingBinaryOp(Instruction::BinaryOps BinOp,
405                                              const ConstantRange &Other,
406                                              unsigned NoWrapKind) const;
407 
408   /// Return a new range representing the possible values resulting
409   /// from an addition of a value in this range and a value in \p Other.
410   LLVM_ABI ConstantRange add(const ConstantRange &Other) const;
411 
412   /// Return a new range representing the possible values resulting
413   /// from an addition with wrap type \p NoWrapKind of a value in this
414   /// range and a value in \p Other.
415   /// If the result range is disjoint, the preferred range is determined by the
416   /// \p PreferredRangeType.
417   LLVM_ABI ConstantRange
418   addWithNoWrap(const ConstantRange &Other, unsigned NoWrapKind,
419                 PreferredRangeType RangeType = Smallest) const;
420 
421   /// Return a new range representing the possible values resulting
422   /// from a subtraction of a value in this range and a value in \p Other.
423   LLVM_ABI ConstantRange sub(const ConstantRange &Other) const;
424 
425   /// Return a new range representing the possible values resulting
426   /// from an subtraction with wrap type \p NoWrapKind of a value in this
427   /// range and a value in \p Other.
428   /// If the result range is disjoint, the preferred range is determined by the
429   /// \p PreferredRangeType.
430   LLVM_ABI ConstantRange
431   subWithNoWrap(const ConstantRange &Other, unsigned NoWrapKind,
432                 PreferredRangeType RangeType = Smallest) const;
433 
434   /// Return a new range representing the possible values resulting
435   /// from a multiplication of a value in this range and a value in \p Other,
436   /// treating both this and \p Other as unsigned ranges.
437   LLVM_ABI ConstantRange multiply(const ConstantRange &Other) const;
438 
439   /// Return a new range representing the possible values resulting
440   /// from a multiplication with wrap type \p NoWrapKind of a value in this
441   /// range and a value in \p Other.
442   /// If the result range is disjoint, the preferred range is determined by the
443   /// \p PreferredRangeType.
444   LLVM_ABI ConstantRange
445   multiplyWithNoWrap(const ConstantRange &Other, unsigned NoWrapKind,
446                      PreferredRangeType RangeType = Smallest) const;
447 
448   /// Return range of possible values for a signed multiplication of this and
449   /// \p Other. However, if overflow is possible always return a full range
450   /// rather than trying to determine a more precise result.
451   LLVM_ABI ConstantRange smul_fast(const ConstantRange &Other) const;
452 
453   /// Return a new range representing the possible values resulting
454   /// from a signed maximum of a value in this range and a value in \p Other.
455   LLVM_ABI ConstantRange smax(const ConstantRange &Other) const;
456 
457   /// Return a new range representing the possible values resulting
458   /// from an unsigned maximum of a value in this range and a value in \p Other.
459   LLVM_ABI ConstantRange umax(const ConstantRange &Other) const;
460 
461   /// Return a new range representing the possible values resulting
462   /// from a signed minimum of a value in this range and a value in \p Other.
463   LLVM_ABI ConstantRange smin(const ConstantRange &Other) const;
464 
465   /// Return a new range representing the possible values resulting
466   /// from an unsigned minimum of a value in this range and a value in \p Other.
467   LLVM_ABI ConstantRange umin(const ConstantRange &Other) const;
468 
469   /// Return a new range representing the possible values resulting
470   /// from an unsigned division of a value in this range and a value in
471   /// \p Other.
472   LLVM_ABI ConstantRange udiv(const ConstantRange &Other) const;
473 
474   /// Return a new range representing the possible values resulting
475   /// from a signed division of a value in this range and a value in
476   /// \p Other. Division by zero and division of SignedMin by -1 are considered
477   /// undefined behavior, in line with IR, and do not contribute towards the
478   /// result.
479   LLVM_ABI ConstantRange sdiv(const ConstantRange &Other) const;
480 
481   /// Return a new range representing the possible values resulting
482   /// from an unsigned remainder operation of a value in this range and a
483   /// value in \p Other.
484   LLVM_ABI ConstantRange urem(const ConstantRange &Other) const;
485 
486   /// Return a new range representing the possible values resulting
487   /// from a signed remainder operation of a value in this range and a
488   /// value in \p Other.
489   LLVM_ABI ConstantRange srem(const ConstantRange &Other) const;
490 
491   /// Return a new range representing the possible values resulting from
492   /// a binary-xor of a value in this range by an all-one value,
493   /// aka bitwise complement operation.
494   LLVM_ABI ConstantRange binaryNot() const;
495 
496   /// Return a new range representing the possible values resulting
497   /// from a binary-and of a value in this range by a value in \p Other.
498   LLVM_ABI ConstantRange binaryAnd(const ConstantRange &Other) const;
499 
500   /// Return a new range representing the possible values resulting
501   /// from a binary-or of a value in this range by a value in \p Other.
502   LLVM_ABI ConstantRange binaryOr(const ConstantRange &Other) const;
503 
504   /// Return a new range representing the possible values resulting
505   /// from a binary-xor of a value in this range by a value in \p Other.
506   LLVM_ABI ConstantRange binaryXor(const ConstantRange &Other) const;
507 
508   /// Return a new range representing the possible values resulting
509   /// from a left shift of a value in this range by a value in \p Other.
510   /// TODO: This isn't fully implemented yet.
511   LLVM_ABI ConstantRange shl(const ConstantRange &Other) const;
512 
513   /// Return a new range representing the possible values resulting
514   /// from a left shift with wrap type \p NoWrapKind of a value in this
515   /// range and a value in \p Other.
516   /// If the result range is disjoint, the preferred range is determined by the
517   /// \p PreferredRangeType.
518   LLVM_ABI ConstantRange
519   shlWithNoWrap(const ConstantRange &Other, unsigned NoWrapKind,
520                 PreferredRangeType RangeType = Smallest) const;
521 
522   /// Return a new range representing the possible values resulting from a
523   /// logical right shift of a value in this range and a value in \p Other.
524   LLVM_ABI ConstantRange lshr(const ConstantRange &Other) const;
525 
526   /// Return a new range representing the possible values resulting from a
527   /// arithmetic right shift of a value in this range and a value in \p Other.
528   LLVM_ABI ConstantRange ashr(const ConstantRange &Other) const;
529 
530   /// Perform an unsigned saturating addition of two constant ranges.
531   LLVM_ABI ConstantRange uadd_sat(const ConstantRange &Other) const;
532 
533   /// Perform a signed saturating addition of two constant ranges.
534   LLVM_ABI ConstantRange sadd_sat(const ConstantRange &Other) const;
535 
536   /// Perform an unsigned saturating subtraction of two constant ranges.
537   LLVM_ABI ConstantRange usub_sat(const ConstantRange &Other) const;
538 
539   /// Perform a signed saturating subtraction of two constant ranges.
540   LLVM_ABI ConstantRange ssub_sat(const ConstantRange &Other) const;
541 
542   /// Perform an unsigned saturating multiplication of two constant ranges.
543   LLVM_ABI ConstantRange umul_sat(const ConstantRange &Other) const;
544 
545   /// Perform a signed saturating multiplication of two constant ranges.
546   LLVM_ABI ConstantRange smul_sat(const ConstantRange &Other) const;
547 
548   /// Perform an unsigned saturating left shift of this constant range by a
549   /// value in \p Other.
550   LLVM_ABI ConstantRange ushl_sat(const ConstantRange &Other) const;
551 
552   /// Perform a signed saturating left shift of this constant range by a
553   /// value in \p Other.
554   LLVM_ABI ConstantRange sshl_sat(const ConstantRange &Other) const;
555 
556   /// Return a new range that is the logical not of the current set.
557   LLVM_ABI ConstantRange inverse() const;
558 
559   /// Calculate absolute value range. If the original range contains signed
560   /// min, then the resulting range will contain signed min if and only if
561   /// \p IntMinIsPoison is false.
562   LLVM_ABI ConstantRange abs(bool IntMinIsPoison = false) const;
563 
564   /// Calculate ctlz range. If \p ZeroIsPoison is set, the range is computed
565   /// ignoring a possible zero value contained in the input range.
566   LLVM_ABI ConstantRange ctlz(bool ZeroIsPoison = false) const;
567 
568   /// Calculate cttz range. If \p ZeroIsPoison is set, the range is computed
569   /// ignoring a possible zero value contained in the input range.
570   LLVM_ABI ConstantRange cttz(bool ZeroIsPoison = false) const;
571 
572   /// Calculate ctpop range.
573   LLVM_ABI ConstantRange ctpop() const;
574 
575   /// Represents whether an operation on the given constant range is known to
576   /// always or never overflow.
577   enum class OverflowResult {
578     /// Always overflows in the direction of signed/unsigned min value.
579     AlwaysOverflowsLow,
580     /// Always overflows in the direction of signed/unsigned max value.
581     AlwaysOverflowsHigh,
582     /// May or may not overflow.
583     MayOverflow,
584     /// Never overflows.
585     NeverOverflows,
586   };
587 
588   /// Return whether unsigned add of the two ranges always/never overflows.
589   LLVM_ABI OverflowResult
590   unsignedAddMayOverflow(const ConstantRange &Other) const;
591 
592   /// Return whether signed add of the two ranges always/never overflows.
593   LLVM_ABI OverflowResult
594   signedAddMayOverflow(const ConstantRange &Other) const;
595 
596   /// Return whether unsigned sub of the two ranges always/never overflows.
597   LLVM_ABI OverflowResult
598   unsignedSubMayOverflow(const ConstantRange &Other) const;
599 
600   /// Return whether signed sub of the two ranges always/never overflows.
601   LLVM_ABI OverflowResult
602   signedSubMayOverflow(const ConstantRange &Other) const;
603 
604   /// Return whether unsigned mul of the two ranges always/never overflows.
605   LLVM_ABI OverflowResult
606   unsignedMulMayOverflow(const ConstantRange &Other) const;
607 
608   /// Return known bits for values in this range.
609   LLVM_ABI KnownBits toKnownBits() const;
610 
611   /// Print out the bounds to a stream.
612   LLVM_ABI void print(raw_ostream &OS) const;
613 
614   /// Allow printing from a debugger easily.
615   LLVM_ABI void dump() const;
616 };
617 
618 inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) {
619   CR.print(OS);
620   return OS;
621 }
622 
623 /// Parse out a conservative ConstantRange from !range metadata.
624 ///
625 /// E.g. if RangeMD is !{i32 0, i32 10, i32 15, i32 20} then return [0, 20).
626 LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD);
627 
628 } // end namespace llvm
629 
630 #endif // LLVM_IR_CONSTANTRANGE_H
631