blob: 20e8e67436a436242b24e8d242c46bd42e1b8052 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- ConstantRange.h - Represent a range ----------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// Represent a range of possible values that may occur when the program is run
10// for an integral value. This keeps track of a lower and upper bound for the
11// constant, which MAY wrap around the end of the numeric range. To do this, it
12// keeps track of a [lower, upper) bound, which specifies an interval just like
13// STL iterators. When used with boolean values, the following are important
14// ranges: :
15//
16// [F, F) = {} = Empty set
17// [T, F) = {T}
18// [F, T) = {F}
19// [T, T) = {F, T} = Full set
20//
21// The other integral ranges use min/max values for special range values. For
22// example, for 8-bit types, it uses:
23// [0, 0) = {} = Empty set
24// [255, 255) = {0..255} = Full Set
25//
26// Note that ConstantRange can be used to represent either signed or
27// unsigned ranges.
28//
29//===----------------------------------------------------------------------===//
30
31#ifndef LLVM_IR_CONSTANTRANGE_H
32#define LLVM_IR_CONSTANTRANGE_H
33
34#include "llvm/ADT/APInt.h"
35#include "llvm/IR/InstrTypes.h"
36#include "llvm/IR/Instruction.h"
37#include "llvm/Support/Compiler.h"
38#include <cstdint>
39
40namespace llvm {
41
42class MDNode;
43class raw_ostream;
Andrew Walbran3d2c1972020-04-07 12:24:26 +010044struct KnownBits;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010045
46/// This class represents a range of values.
47class LLVM_NODISCARD ConstantRange {
48 APInt Lower, Upper;
49
Andrew Walbran3d2c1972020-04-07 12:24:26 +010050 /// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010060public:
Andrew Walbran3d2c1972020-04-07 12:24:26 +010061 /// Initialize a full or empty set for the specified bit width.
62 explicit ConstantRange(uint32_t BitWidth, bool isFullSet);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010063
64 /// Initialize a range to hold the single specified value.
65 ConstantRange(APInt Value);
66
Andrew Scullcdfcccc2018-10-05 20:58:37 +010067 /// Initialize a range of values explicitly. This will assert out if
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010068 /// Lower==Upper and Lower != Min or Max value for its type. It will also
69 /// assert out if the two APInt's are not the same bit width.
70 ConstantRange(APInt Lower, APInt Upper);
71
Andrew Walbran3d2c1972020-04-07 12:24:26 +010072 /// Create empty constant range with the given bit width.
73 static ConstantRange getEmpty(uint32_t BitWidth) {
74 return ConstantRange(BitWidth, false);
75 }
76
77 /// Create full constant range with the given bit width.
78 static ConstantRange getFull(uint32_t BitWidth) {
79 return ConstantRange(BitWidth, true);
80 }
81
82 /// Create non-empty constant range with the given bounds. If Lower and
83 /// Upper are the same, a full range is returned.
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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010095 /// 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
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100127 /// Produce the largest range containing all X such that "X BinOp Y" is
128 /// guaranteed not to wrap (overflow) for *all* Y in Other. However, there may
129 /// be *some* Y in Other for which additional X not contained in the result
130 /// also do not overflow.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100131 ///
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100132 /// NoWrapKind must be one of OBO::NoUnsignedWrap or OBO::NoSignedWrap.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100133 ///
134 /// Examples:
135 /// typedef OverflowingBinaryOperator OBO;
136 /// #define MGNR makeGuaranteedNoWrapRegion
137 /// MGNR(Add, [i8 1, 2), OBO::NoSignedWrap) == [-128, 127)
138 /// MGNR(Add, [i8 1, 2), OBO::NoUnsignedWrap) == [0, -1)
139 /// MGNR(Add, [i8 0, 1), OBO::NoUnsignedWrap) == Full Set
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100140 /// MGNR(Add, [i8 -1, 6), OBO::NoSignedWrap) == [INT_MIN+1, INT_MAX-4)
141 /// MGNR(Sub, [i8 1, 2), OBO::NoSignedWrap) == [-127, 128)
142 /// MGNR(Sub, [i8 1, 2), OBO::NoUnsignedWrap) == [1, 0)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100143 static ConstantRange makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp,
144 const ConstantRange &Other,
145 unsigned NoWrapKind);
146
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100147 /// Produce the range that contains X if and only if "X BinOp Other" does
148 /// not wrap.
149 static ConstantRange makeExactNoWrapRegion(Instruction::BinaryOps BinOp,
150 const APInt &Other,
151 unsigned NoWrapKind);
152
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200153 /// Returns true if ConstantRange calculations are supported for intrinsic
154 /// with \p IntrinsicID.
155 static bool isIntrinsicSupported(Intrinsic::ID IntrinsicID);
156
157 /// Compute range of intrinsic result for the given operand ranges.
158 static ConstantRange intrinsic(Intrinsic::ID IntrinsicID,
159 ArrayRef<ConstantRange> Ops);
160
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100161 /// Set up \p Pred and \p RHS such that
162 /// ConstantRange::makeExactICmpRegion(Pred, RHS) == *this. Return true if
163 /// successful.
164 bool getEquivalentICmp(CmpInst::Predicate &Pred, APInt &RHS) const;
165
166 /// Return the lower value for this range.
167 const APInt &getLower() const { return Lower; }
168
169 /// Return the upper value for this range.
170 const APInt &getUpper() const { return Upper; }
171
172 /// Get the bit width of this ConstantRange.
173 uint32_t getBitWidth() const { return Lower.getBitWidth(); }
174
175 /// Return true if this set contains all of the elements possible
176 /// for this data-type.
177 bool isFullSet() const;
178
179 /// Return true if this set contains no members.
180 bool isEmptySet() const;
181
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100182 /// Return true if this set wraps around the unsigned domain. Special cases:
183 /// * Empty set: Not wrapped.
184 /// * Full set: Not wrapped.
185 /// * [X, 0) == [X, Max]: Not wrapped.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100186 bool isWrappedSet() const;
187
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100188 /// Return true if the exclusive upper bound wraps around the unsigned
189 /// domain. Special cases:
190 /// * Empty set: Not wrapped.
191 /// * Full set: Not wrapped.
192 /// * [X, 0): Wrapped.
193 bool isUpperWrapped() const;
194
195 /// Return true if this set wraps around the signed domain. Special cases:
196 /// * Empty set: Not wrapped.
197 /// * Full set: Not wrapped.
198 /// * [X, SignedMin) == [X, SignedMax]: Not wrapped.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100199 bool isSignWrappedSet() const;
200
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100201 /// Return true if the (exclusive) upper bound wraps around the signed
202 /// domain. Special cases:
203 /// * Empty set: Not wrapped.
204 /// * Full set: Not wrapped.
205 /// * [X, SignedMin): Wrapped.
206 bool isUpperSignWrapped() const;
207
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100208 /// Return true if the specified value is in the set.
209 bool contains(const APInt &Val) const;
210
211 /// Return true if the other range is a subset of this one.
212 bool contains(const ConstantRange &CR) const;
213
214 /// If this set contains a single element, return it, otherwise return null.
215 const APInt *getSingleElement() const {
216 if (Upper == Lower + 1)
217 return &Lower;
218 return nullptr;
219 }
220
221 /// If this set contains all but a single element, return it, otherwise return
222 /// null.
223 const APInt *getSingleMissingElement() const {
224 if (Lower == Upper + 1)
225 return &Upper;
226 return nullptr;
227 }
228
229 /// Return true if this set contains exactly one member.
230 bool isSingleElement() const { return getSingleElement() != nullptr; }
231
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100232 /// Compare set size of this range with the range CR.
233 bool isSizeStrictlySmallerThan(const ConstantRange &CR) const;
234
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100235 /// Compare set size of this range with Value.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100236 bool isSizeLargerThan(uint64_t MaxSize) const;
237
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100238 /// Return true if all values in this range are negative.
239 bool isAllNegative() const;
240
241 /// Return true if all values in this range are non-negative.
242 bool isAllNonNegative() const;
243
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100244 /// Return the largest unsigned value contained in the ConstantRange.
245 APInt getUnsignedMax() const;
246
247 /// Return the smallest unsigned value contained in the ConstantRange.
248 APInt getUnsignedMin() const;
249
250 /// Return the largest signed value contained in the ConstantRange.
251 APInt getSignedMax() const;
252
253 /// Return the smallest signed value contained in the ConstantRange.
254 APInt getSignedMin() const;
255
256 /// Return true if this range is equal to another range.
257 bool operator==(const ConstantRange &CR) const {
258 return Lower == CR.Lower && Upper == CR.Upper;
259 }
260 bool operator!=(const ConstantRange &CR) const {
261 return !operator==(CR);
262 }
263
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200264 /// Compute the maximal number of active bits needed to represent every value
265 /// in this range.
266 unsigned getActiveBits() const;
267
268 /// Compute the maximal number of bits needed to represent every value
269 /// in this signed range.
270 unsigned getMinSignedBits() const;
271
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100272 /// Subtract the specified constant from the endpoints of this constant range.
273 ConstantRange subtract(const APInt &CI) const;
274
275 /// Subtract the specified range from this range (aka relative complement of
276 /// the sets).
277 ConstantRange difference(const ConstantRange &CR) const;
278
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100279 /// If represented precisely, the result of some range operations may consist
280 /// of multiple disjoint ranges. As only a single range may be returned, any
281 /// range covering these disjoint ranges constitutes a valid result, but some
282 /// may be more useful than others depending on context. The preferred range
283 /// type specifies whether a range that is non-wrapping in the unsigned or
284 /// signed domain, or has the smallest size, is preferred. If a signedness is
285 /// preferred but all ranges are non-wrapping or all wrapping, then the
286 /// smallest set size is preferred. If there are multiple smallest sets, any
287 /// one of them may be returned.
288 enum PreferredRangeType { Smallest, Unsigned, Signed };
289
290 /// Return the range that results from the intersection of this range with
291 /// another range. If the intersection is disjoint, such that two results
292 /// are possible, the preferred range is determined by the PreferredRangeType.
293 ConstantRange intersectWith(const ConstantRange &CR,
294 PreferredRangeType Type = Smallest) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100295
296 /// Return the range that results from the union of this range
297 /// with another range. The resultant range is guaranteed to include the
298 /// elements of both sets, but may contain more. For example, [3, 9) union
299 /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
300 /// in either set before.
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100301 ConstantRange unionWith(const ConstantRange &CR,
302 PreferredRangeType Type = Smallest) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100303
304 /// Return a new range representing the possible values resulting
305 /// from an application of the specified cast operator to this range. \p
306 /// BitWidth is the target bitwidth of the cast. For casts which don't
307 /// change bitwidth, it must be the same as the source bitwidth. For casts
308 /// which do change bitwidth, the bitwidth must be consistent with the
309 /// requested cast and source bitwidth.
310 ConstantRange castOp(Instruction::CastOps CastOp,
311 uint32_t BitWidth) const;
312
313 /// Return a new range in the specified integer type, which must
314 /// be strictly larger than the current type. The returned range will
315 /// correspond to the possible range of values if the source range had been
316 /// zero extended to BitWidth.
317 ConstantRange zeroExtend(uint32_t BitWidth) const;
318
319 /// Return a new range in the specified integer type, which must
320 /// be strictly larger than the current type. The returned range will
321 /// correspond to the possible range of values if the source range had been
322 /// sign extended to BitWidth.
323 ConstantRange signExtend(uint32_t BitWidth) const;
324
325 /// Return a new range in the specified integer type, which must be
326 /// strictly smaller than the current type. The returned range will
327 /// correspond to the possible range of values if the source range had been
328 /// truncated to the specified type.
329 ConstantRange truncate(uint32_t BitWidth) const;
330
331 /// Make this range have the bit width given by \p BitWidth. The
332 /// value is zero extended, truncated, or left alone to make it that width.
333 ConstantRange zextOrTrunc(uint32_t BitWidth) const;
334
335 /// Make this range have the bit width given by \p BitWidth. The
336 /// value is sign extended, truncated, or left alone to make it that width.
337 ConstantRange sextOrTrunc(uint32_t BitWidth) const;
338
339 /// Return a new range representing the possible values resulting
340 /// from an application of the specified binary operator to an left hand side
341 /// of this range and a right hand side of \p Other.
342 ConstantRange binaryOp(Instruction::BinaryOps BinOp,
343 const ConstantRange &Other) const;
344
345 /// Return a new range representing the possible values resulting
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200346 /// from an application of the specified overflowing binary operator to a
347 /// left hand side of this range and a right hand side of \p Other given
348 /// the provided knowledge about lack of wrapping \p NoWrapKind.
349 ConstantRange overflowingBinaryOp(Instruction::BinaryOps BinOp,
350 const ConstantRange &Other,
351 unsigned NoWrapKind) const;
352
353 /// Return a new range representing the possible values resulting
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100354 /// from an addition of a value in this range and a value in \p Other.
355 ConstantRange add(const ConstantRange &Other) const;
356
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200357 /// Return a new range representing the possible values resulting
358 /// from an addition with wrap type \p NoWrapKind of a value in this
359 /// range and a value in \p Other.
360 /// If the result range is disjoint, the preferred range is determined by the
361 /// \p PreferredRangeType.
362 ConstantRange addWithNoWrap(const ConstantRange &Other, unsigned NoWrapKind,
363 PreferredRangeType RangeType = Smallest) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100364
365 /// Return a new range representing the possible values resulting
366 /// from a subtraction of a value in this range and a value in \p Other.
367 ConstantRange sub(const ConstantRange &Other) const;
368
369 /// Return a new range representing the possible values resulting
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200370 /// from an subtraction with wrap type \p NoWrapKind of a value in this
371 /// range and a value in \p Other.
372 /// If the result range is disjoint, the preferred range is determined by the
373 /// \p PreferredRangeType.
374 ConstantRange subWithNoWrap(const ConstantRange &Other, unsigned NoWrapKind,
375 PreferredRangeType RangeType = Smallest) const;
376
377 /// Return a new range representing the possible values resulting
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100378 /// from a multiplication of a value in this range and a value in \p Other,
379 /// treating both this and \p Other as unsigned ranges.
380 ConstantRange multiply(const ConstantRange &Other) const;
381
382 /// Return a new range representing the possible values resulting
383 /// from a signed maximum of a value in this range and a value in \p Other.
384 ConstantRange smax(const ConstantRange &Other) const;
385
386 /// Return a new range representing the possible values resulting
387 /// from an unsigned maximum of a value in this range and a value in \p Other.
388 ConstantRange umax(const ConstantRange &Other) const;
389
390 /// Return a new range representing the possible values resulting
391 /// from a signed minimum of a value in this range and a value in \p Other.
392 ConstantRange smin(const ConstantRange &Other) const;
393
394 /// Return a new range representing the possible values resulting
395 /// from an unsigned minimum of a value in this range and a value in \p Other.
396 ConstantRange umin(const ConstantRange &Other) const;
397
398 /// Return a new range representing the possible values resulting
399 /// from an unsigned division of a value in this range and a value in
400 /// \p Other.
401 ConstantRange udiv(const ConstantRange &Other) const;
402
403 /// Return a new range representing the possible values resulting
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100404 /// from a signed division of a value in this range and a value in
405 /// \p Other. Division by zero and division of SignedMin by -1 are considered
406 /// undefined behavior, in line with IR, and do not contribute towards the
407 /// result.
408 ConstantRange sdiv(const ConstantRange &Other) const;
409
410 /// Return a new range representing the possible values resulting
411 /// from an unsigned remainder operation of a value in this range and a
412 /// value in \p Other.
413 ConstantRange urem(const ConstantRange &Other) const;
414
415 /// Return a new range representing the possible values resulting
416 /// from a signed remainder operation of a value in this range and a
417 /// value in \p Other.
418 ConstantRange srem(const ConstantRange &Other) const;
419
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200420 /// Return a new range representing the possible values resulting from
421 /// a binary-xor of a value in this range by an all-one value,
422 /// aka bitwise complement operation.
423 ConstantRange binaryNot() const;
424
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100425 /// Return a new range representing the possible values resulting
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100426 /// from a binary-and of a value in this range by a value in \p Other.
427 ConstantRange binaryAnd(const ConstantRange &Other) const;
428
429 /// Return a new range representing the possible values resulting
430 /// from a binary-or of a value in this range by a value in \p Other.
431 ConstantRange binaryOr(const ConstantRange &Other) const;
432
433 /// Return a new range representing the possible values resulting
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200434 /// from a binary-xor of a value in this range by a value in \p Other.
435 ConstantRange binaryXor(const ConstantRange &Other) const;
436
437 /// Return a new range representing the possible values resulting
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100438 /// from a left shift of a value in this range by a value in \p Other.
439 /// TODO: This isn't fully implemented yet.
440 ConstantRange shl(const ConstantRange &Other) const;
441
442 /// Return a new range representing the possible values resulting from a
443 /// logical right shift of a value in this range and a value in \p Other.
444 ConstantRange lshr(const ConstantRange &Other) const;
445
446 /// Return a new range representing the possible values resulting from a
447 /// arithmetic right shift of a value in this range and a value in \p Other.
448 ConstantRange ashr(const ConstantRange &Other) const;
449
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100450 /// Perform an unsigned saturating addition of two constant ranges.
451 ConstantRange uadd_sat(const ConstantRange &Other) const;
452
453 /// Perform a signed saturating addition of two constant ranges.
454 ConstantRange sadd_sat(const ConstantRange &Other) const;
455
456 /// Perform an unsigned saturating subtraction of two constant ranges.
457 ConstantRange usub_sat(const ConstantRange &Other) const;
458
459 /// Perform a signed saturating subtraction of two constant ranges.
460 ConstantRange ssub_sat(const ConstantRange &Other) const;
461
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200462 /// Perform an unsigned saturating multiplication of two constant ranges.
463 ConstantRange umul_sat(const ConstantRange &Other) const;
464
465 /// Perform a signed saturating multiplication of two constant ranges.
466 ConstantRange smul_sat(const ConstantRange &Other) const;
467
468 /// Perform an unsigned saturating left shift of this constant range by a
469 /// value in \p Other.
470 ConstantRange ushl_sat(const ConstantRange &Other) const;
471
472 /// Perform a signed saturating left shift of this constant range by a
473 /// value in \p Other.
474 ConstantRange sshl_sat(const ConstantRange &Other) const;
475
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100476 /// Return a new range that is the logical not of the current set.
477 ConstantRange inverse() const;
478
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100479 /// Calculate absolute value range. If the original range contains signed
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200480 /// min, then the resulting range will contain signed min if and only if
481 /// \p IntMinIsPoison is false.
482 ConstantRange abs(bool IntMinIsPoison = false) const;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100483
484 /// Represents whether an operation on the given constant range is known to
485 /// always or never overflow.
486 enum class OverflowResult {
487 /// Always overflows in the direction of signed/unsigned min value.
488 AlwaysOverflowsLow,
489 /// Always overflows in the direction of signed/unsigned max value.
490 AlwaysOverflowsHigh,
491 /// May or may not overflow.
492 MayOverflow,
493 /// Never overflows.
494 NeverOverflows,
495 };
496
497 /// Return whether unsigned add of the two ranges always/never overflows.
498 OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const;
499
500 /// Return whether signed add of the two ranges always/never overflows.
501 OverflowResult signedAddMayOverflow(const ConstantRange &Other) const;
502
503 /// Return whether unsigned sub of the two ranges always/never overflows.
504 OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const;
505
506 /// Return whether signed sub of the two ranges always/never overflows.
507 OverflowResult signedSubMayOverflow(const ConstantRange &Other) const;
508
509 /// Return whether unsigned mul of the two ranges always/never overflows.
510 OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const;
511
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100512 /// Print out the bounds to a stream.
513 void print(raw_ostream &OS) const;
514
515 /// Allow printing from a debugger easily.
516 void dump() const;
517};
518
519inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) {
520 CR.print(OS);
521 return OS;
522}
523
524/// Parse out a conservative ConstantRange from !range metadata.
525///
526/// E.g. if RangeMD is !{i32 0, i32 10, i32 15, i32 20} then return [0, 20).
527ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD);
528
529} // end namespace llvm
530
531#endif // LLVM_IR_CONSTANTRANGE_H