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