LLVM 22.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;
44struct KnownBits;
45
46/// This class represents a range of values.
47class [[nodiscard]] ConstantRange {
48 APInt Lower, Upper;
49
50 /// Create empty constant range with same bitwidth.
51 ConstantRange getEmpty() const {
52 return ConstantRange(getBitWidth(), false);
53 }
54
55 /// Create full constant range with same bitwidth.
56 ConstantRange getFull() const {
57 return ConstantRange(getBitWidth(), true);
58 }
59
60public:
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.
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.
71
72 /// Create empty constant range with the given bit width.
74 return ConstantRange(BitWidth, false);
75 }
76
77 /// Create full constant range with the given bit width.
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.
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)
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)
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.
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)
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.
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,
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.
209 const APInt &getLower() const { return Lower; }
210
211 /// Return the upper value for this range.
212 const APInt &getUpper() const { return Upper; }
213
214 /// Get the bit width of this ConstantRange.
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.
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.
266 if (Lower == Upper + 1)
267 return &Upper;
268 return nullptr;
269 }
270
271 /// Return true if this set contains exactly one member.
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.
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 with wrap type \p NoWrapKind.
384 /// Note that the result of trunc nuw is exact.
386 unsigned NoWrapKind = 0) const;
387
388 /// Make this range have the bit width given by \p BitWidth. The
389 /// value is zero extended, truncated, or left alone to make it that width.
390 LLVM_ABI ConstantRange zextOrTrunc(uint32_t BitWidth) const;
391
392 /// Make this range have the bit width given by \p BitWidth. The
393 /// value is sign extended, truncated, or left alone to make it that width.
394 LLVM_ABI ConstantRange sextOrTrunc(uint32_t BitWidth) const;
395
396 /// Return a new range representing the possible values resulting
397 /// from an application of the specified binary operator to an left hand side
398 /// of this range and a right hand side of \p Other.
400 const ConstantRange &Other) const;
401
402 /// Return a new range representing the possible values resulting
403 /// from an application of the specified overflowing binary operator to a
404 /// left hand side of this range and a right hand side of \p Other given
405 /// the provided knowledge about lack of wrapping \p NoWrapKind.
406 LLVM_ABI ConstantRange overflowingBinaryOp(Instruction::BinaryOps BinOp,
407 const ConstantRange &Other,
408 unsigned NoWrapKind) const;
409
410 /// Return a new range representing the possible values resulting
411 /// from an addition of a value in this range and a value in \p Other.
412 LLVM_ABI ConstantRange add(const ConstantRange &Other) const;
413
414 /// Return a new range representing the possible values resulting
415 /// from an addition with wrap type \p NoWrapKind of a value in this
416 /// range and a value in \p Other.
417 /// If the result range is disjoint, the preferred range is determined by the
418 /// \p PreferredRangeType.
420 addWithNoWrap(const ConstantRange &Other, unsigned NoWrapKind,
421 PreferredRangeType RangeType = Smallest) const;
422
423 /// Return a new range representing the possible values resulting
424 /// from a subtraction of a value in this range and a value in \p Other.
425 LLVM_ABI ConstantRange sub(const ConstantRange &Other) const;
426
427 /// Return a new range representing the possible values resulting
428 /// from an subtraction with wrap type \p NoWrapKind of a value in this
429 /// range and a value in \p Other.
430 /// If the result range is disjoint, the preferred range is determined by the
431 /// \p PreferredRangeType.
433 subWithNoWrap(const ConstantRange &Other, unsigned NoWrapKind,
434 PreferredRangeType RangeType = Smallest) const;
435
436 /// Return a new range representing the possible values resulting
437 /// from a multiplication of a value in this range and a value in \p Other,
438 /// treating both this and \p Other as unsigned ranges.
439 LLVM_ABI ConstantRange multiply(const ConstantRange &Other) const;
440
441 /// Return a new range representing the possible values resulting
442 /// from a multiplication with wrap type \p NoWrapKind of a value in this
443 /// range and a value in \p Other.
444 /// If the result range is disjoint, the preferred range is determined by the
445 /// \p PreferredRangeType.
447 multiplyWithNoWrap(const ConstantRange &Other, unsigned NoWrapKind,
448 PreferredRangeType RangeType = Smallest) const;
449
450 /// Return range of possible values for a signed multiplication of this and
451 /// \p Other. However, if overflow is possible always return a full range
452 /// rather than trying to determine a more precise result.
453 LLVM_ABI ConstantRange smul_fast(const ConstantRange &Other) const;
454
455 /// Return a new range representing the possible values resulting
456 /// from a signed maximum of a value in this range and a value in \p Other.
457 LLVM_ABI ConstantRange smax(const ConstantRange &Other) const;
458
459 /// Return a new range representing the possible values resulting
460 /// from an unsigned maximum of a value in this range and a value in \p Other.
461 LLVM_ABI ConstantRange umax(const ConstantRange &Other) const;
462
463 /// Return a new range representing the possible values resulting
464 /// from a signed minimum of a value in this range and a value in \p Other.
465 LLVM_ABI ConstantRange smin(const ConstantRange &Other) const;
466
467 /// Return a new range representing the possible values resulting
468 /// from an unsigned minimum of a value in this range and a value in \p Other.
469 LLVM_ABI ConstantRange umin(const ConstantRange &Other) const;
470
471 /// Return a new range representing the possible values resulting
472 /// from an unsigned division of a value in this range and a value in
473 /// \p Other.
474 LLVM_ABI ConstantRange udiv(const ConstantRange &Other) const;
475
476 /// Return a new range representing the possible values resulting
477 /// from a signed division of a value in this range and a value in
478 /// \p Other. Division by zero and division of SignedMin by -1 are considered
479 /// undefined behavior, in line with IR, and do not contribute towards the
480 /// result.
481 LLVM_ABI ConstantRange sdiv(const ConstantRange &Other) const;
482
483 /// Return a new range representing the possible values resulting
484 /// from an unsigned remainder operation of a value in this range and a
485 /// value in \p Other.
486 LLVM_ABI ConstantRange urem(const ConstantRange &Other) const;
487
488 /// Return a new range representing the possible values resulting
489 /// from a signed remainder operation of a value in this range and a
490 /// value in \p Other.
491 LLVM_ABI ConstantRange srem(const ConstantRange &Other) const;
492
493 /// Return a new range representing the possible values resulting from
494 /// a binary-xor of a value in this range by an all-one value,
495 /// aka bitwise complement operation.
496 LLVM_ABI ConstantRange binaryNot() const;
497
498 /// Return a new range representing the possible values resulting
499 /// from a binary-and of a value in this range by a value in \p Other.
500 LLVM_ABI ConstantRange binaryAnd(const ConstantRange &Other) const;
501
502 /// Return a new range representing the possible values resulting
503 /// from a binary-or of a value in this range by a value in \p Other.
504 LLVM_ABI ConstantRange binaryOr(const ConstantRange &Other) const;
505
506 /// Return a new range representing the possible values resulting
507 /// from a binary-xor of a value in this range by a value in \p Other.
508 LLVM_ABI ConstantRange binaryXor(const ConstantRange &Other) const;
509
510 /// Return a new range representing the possible values resulting
511 /// from a left shift of a value in this range by a value in \p Other.
512 /// TODO: This isn't fully implemented yet.
513 LLVM_ABI ConstantRange shl(const ConstantRange &Other) const;
514
515 /// Return a new range representing the possible values resulting
516 /// from a left shift with wrap type \p NoWrapKind of a value in this
517 /// range and a value in \p Other.
518 /// If the result range is disjoint, the preferred range is determined by the
519 /// \p PreferredRangeType.
521 shlWithNoWrap(const ConstantRange &Other, unsigned NoWrapKind,
522 PreferredRangeType RangeType = Smallest) const;
523
524 /// Return a new range representing the possible values resulting from a
525 /// logical right shift of a value in this range and a value in \p Other.
526 LLVM_ABI ConstantRange lshr(const ConstantRange &Other) const;
527
528 /// Return a new range representing the possible values resulting from a
529 /// arithmetic right shift of a value in this range and a value in \p Other.
530 LLVM_ABI ConstantRange ashr(const ConstantRange &Other) const;
531
532 /// Perform an unsigned saturating addition of two constant ranges.
533 LLVM_ABI ConstantRange uadd_sat(const ConstantRange &Other) const;
534
535 /// Perform a signed saturating addition of two constant ranges.
536 LLVM_ABI ConstantRange sadd_sat(const ConstantRange &Other) const;
537
538 /// Perform an unsigned saturating subtraction of two constant ranges.
539 LLVM_ABI ConstantRange usub_sat(const ConstantRange &Other) const;
540
541 /// Perform a signed saturating subtraction of two constant ranges.
542 LLVM_ABI ConstantRange ssub_sat(const ConstantRange &Other) const;
543
544 /// Perform an unsigned saturating multiplication of two constant ranges.
545 LLVM_ABI ConstantRange umul_sat(const ConstantRange &Other) const;
546
547 /// Perform a signed saturating multiplication of two constant ranges.
548 LLVM_ABI ConstantRange smul_sat(const ConstantRange &Other) const;
549
550 /// Perform an unsigned saturating left shift of this constant range by a
551 /// value in \p Other.
552 LLVM_ABI ConstantRange ushl_sat(const ConstantRange &Other) const;
553
554 /// Perform a signed saturating left shift of this constant range by a
555 /// value in \p Other.
556 LLVM_ABI ConstantRange sshl_sat(const ConstantRange &Other) const;
557
558 /// Return a new range that is the logical not of the current set.
559 LLVM_ABI ConstantRange inverse() const;
560
561 /// Calculate absolute value range. If the original range contains signed
562 /// min, then the resulting range will contain signed min if and only if
563 /// \p IntMinIsPoison is false.
564 LLVM_ABI ConstantRange abs(bool IntMinIsPoison = false) const;
565
566 /// Calculate ctlz range. If \p ZeroIsPoison is set, the range is computed
567 /// ignoring a possible zero value contained in the input range.
568 LLVM_ABI ConstantRange ctlz(bool ZeroIsPoison = false) const;
569
570 /// Calculate cttz range. If \p ZeroIsPoison is set, the range is computed
571 /// ignoring a possible zero value contained in the input range.
572 LLVM_ABI ConstantRange cttz(bool ZeroIsPoison = false) const;
573
574 /// Calculate ctpop range.
575 LLVM_ABI ConstantRange ctpop() const;
576
577 /// Represents whether an operation on the given constant range is known to
578 /// always or never overflow.
579 enum class OverflowResult {
580 /// Always overflows in the direction of signed/unsigned min value.
582 /// Always overflows in the direction of signed/unsigned max value.
584 /// May or may not overflow.
586 /// Never overflows.
588 };
589
590 /// Return whether unsigned add of the two ranges always/never overflows.
592 unsignedAddMayOverflow(const ConstantRange &Other) const;
593
594 /// Return whether signed add of the two ranges always/never overflows.
596 signedAddMayOverflow(const ConstantRange &Other) const;
597
598 /// Return whether unsigned sub of the two ranges always/never overflows.
600 unsignedSubMayOverflow(const ConstantRange &Other) const;
601
602 /// Return whether signed sub of the two ranges always/never overflows.
604 signedSubMayOverflow(const ConstantRange &Other) const;
605
606 /// Return whether unsigned mul of the two ranges always/never overflows.
608 unsignedMulMayOverflow(const ConstantRange &Other) const;
609
610 /// Return known bits for values in this range.
611 LLVM_ABI KnownBits toKnownBits() const;
612
613 /// Print out the bounds to a stream.
614 LLVM_ABI void print(raw_ostream &OS) const;
615
616 /// Allow printing from a debugger easily.
617 LLVM_ABI void dump() const;
618};
619
621 CR.print(OS);
622 return OS;
623}
624
625/// Parse out a conservative ConstantRange from !range metadata.
626///
627/// E.g. if RangeMD is !{i32 0, i32 10, i32 15, i32 20} then return [0, 20).
628LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD);
629
630} // end namespace llvm
631
632#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)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_ABI
Definition: Compiler.h:213
std::optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1328
uint64_t Offset
Definition: ELF_riscv.cpp:478
const uint64_t BitWidth
raw_pwrite_stream & OS
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition: Value.cpp:480
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:41
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:678
This class represents a range of values.
Definition: ConstantRange.h:47
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.
Definition: ConstantRange.h:78
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.
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.
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.
bool operator!=(const ConstantRange &CR) const
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
Definition: ConstantRange.h:84
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.
Definition: ConstantRange.h:73
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
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
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
OverflowResult
@ NeverOverflows
Never overflows.
@ 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.
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Definition: APFloat.h:1563
decltype(auto) getSingleElement(ContainerTy &&C)
Asserts that the given container has a single element and returns that element.
Definition: STLExtras.h:331
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:312