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