2014-03-04 13:24:34 +01:00
|
|
|
//===- ConstantRange.h - Represent a range ----------------------*- C++ -*-===//
|
2005-04-21 22:48:15 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2005-04-21 22:48:15 +02:00
|
|
|
//
|
2003-10-20 22:19:47 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-09-02 01:53:39 +02:00
|
|
|
//
|
|
|
|
// Represent a range of possible values that may occur when the program is run
|
|
|
|
// for an integral value. This keeps track of a lower and upper bound for the
|
|
|
|
// constant, which MAY wrap around the end of the numeric range. To do this, it
|
|
|
|
// keeps track of a [lower, upper) bound, which specifies an interval just like
|
|
|
|
// STL iterators. When used with boolean values, the following are important
|
2006-12-23 07:05:41 +01:00
|
|
|
// ranges: :
|
2002-09-02 01:53:39 +02:00
|
|
|
//
|
|
|
|
// [F, F) = {} = Empty set
|
|
|
|
// [T, F) = {T}
|
|
|
|
// [F, T) = {F}
|
|
|
|
// [T, T) = {F, T} = Full set
|
|
|
|
//
|
2006-12-23 07:05:41 +01:00
|
|
|
// The other integral ranges use min/max values for special range values. For
|
|
|
|
// example, for 8-bit types, it uses:
|
|
|
|
// [0, 0) = {} = Empty set
|
|
|
|
// [255, 255) = {0..255} = Full Set
|
|
|
|
//
|
2009-07-10 01:16:10 +02:00
|
|
|
// Note that ConstantRange can be used to represent either signed or
|
|
|
|
// unsigned ranges.
|
2009-07-10 00:07:27 +02:00
|
|
|
//
|
2002-09-02 01:53:39 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-13 18:26:38 +02:00
|
|
|
#ifndef LLVM_IR_CONSTANTRANGE_H
|
|
|
|
#define LLVM_IR_CONSTANTRANGE_H
|
2002-09-02 01:53:39 +02:00
|
|
|
|
2007-02-28 18:36:23 +01:00
|
|
|
#include "llvm/ADT/APInt.h"
|
2015-03-18 01:41:24 +01:00
|
|
|
#include "llvm/IR/InstrTypes.h"
|
2017-06-20 00:05:08 +02:00
|
|
|
#include "llvm/IR/Instruction.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include <cstdint>
|
2003-11-11 23:41:34 +01:00
|
|
|
|
|
|
|
namespace llvm {
|
2002-09-02 01:53:39 +02:00
|
|
|
|
2016-10-21 21:59:26 +02:00
|
|
|
class MDNode;
|
2017-06-20 00:05:08 +02:00
|
|
|
class raw_ostream;
|
2019-03-17 21:24:02 +01:00
|
|
|
struct KnownBits;
|
2016-10-21 21:59:26 +02:00
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// This class represents a range of values.
|
2017-05-04 23:29:50 +02:00
|
|
|
class LLVM_NODISCARD ConstantRange {
|
2007-02-28 18:36:23 +01:00
|
|
|
APInt Lower, Upper;
|
2009-07-10 01:16:10 +02:00
|
|
|
|
2019-03-24 10:34:40 +01:00
|
|
|
/// Create empty constant range with same bitwidth.
|
|
|
|
ConstantRange getEmpty() const {
|
|
|
|
return ConstantRange(getBitWidth(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create full constant range with same bitwidth.
|
|
|
|
ConstantRange getFull() const {
|
|
|
|
return ConstantRange(getBitWidth(), true);
|
|
|
|
}
|
|
|
|
|
2009-07-10 01:16:10 +02:00
|
|
|
public:
|
2019-03-26 19:05:53 +01:00
|
|
|
/// Initialize a full or empty set for the specified bit width.
|
2019-03-24 10:34:40 +01:00
|
|
|
explicit ConstantRange(uint32_t BitWidth, bool isFullSet);
|
2005-04-21 22:48:15 +02:00
|
|
|
|
2004-03-30 02:20:08 +02:00
|
|
|
/// Initialize a range to hold the single specified value.
|
2017-04-13 02:20:31 +02:00
|
|
|
ConstantRange(APInt Value);
|
2004-03-30 02:20:08 +02:00
|
|
|
|
2018-05-01 18:10:38 +02:00
|
|
|
/// Initialize a range of values explicitly. This will assert out if
|
2007-02-28 19:57:32 +01:00
|
|
|
/// Lower==Upper and Lower != Min or Max value for its type. It will also
|
|
|
|
/// assert out if the two APInt's are not the same bit width.
|
2017-04-13 02:20:31 +02:00
|
|
|
ConstantRange(APInt Lower, APInt Upper);
|
2007-02-28 18:36:23 +01:00
|
|
|
|
2019-03-24 10:34:40 +01:00
|
|
|
/// Create empty constant range with the given bit width.
|
|
|
|
static ConstantRange getEmpty(uint32_t BitWidth) {
|
|
|
|
return ConstantRange(BitWidth, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create full constant range with the given bit width.
|
|
|
|
static ConstantRange getFull(uint32_t BitWidth) {
|
|
|
|
return ConstantRange(BitWidth, true);
|
|
|
|
}
|
|
|
|
|
2019-04-21 17:22:54 +02:00
|
|
|
/// Create non-empty constant range with the given bounds. If Lower and
|
|
|
|
/// Upper are the same, a full range is returned.
|
|
|
|
static ConstantRange getNonEmpty(APInt Lower, APInt Upper) {
|
|
|
|
if (Lower == Upper)
|
|
|
|
return getFull(Lower.getBitWidth());
|
|
|
|
return ConstantRange(std::move(Lower), std::move(Upper));
|
|
|
|
}
|
|
|
|
|
2019-03-17 21:24:02 +01:00
|
|
|
/// Initialize a range based on a known bits constraint. The IsSigned flag
|
|
|
|
/// indicates whether the constant range should not wrap in the signed or
|
|
|
|
/// unsigned domain.
|
|
|
|
static ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned);
|
|
|
|
|
2015-03-18 01:41:24 +01:00
|
|
|
/// Produce the smallest range such that all values that may satisfy the given
|
|
|
|
/// predicate with any value contained within Other is contained in the
|
|
|
|
/// returned range. Formally, this returns a superset of
|
|
|
|
/// 'union over all y in Other . { x : icmp op x y is true }'. If the exact
|
|
|
|
/// answer is not representable as a ConstantRange, the return value will be a
|
|
|
|
/// proper superset of the above.
|
2009-07-12 07:44:08 +02:00
|
|
|
///
|
2015-03-18 01:41:24 +01:00
|
|
|
/// Example: Pred = ult and Other = i8 [2, 5) returns Result = [0, 4)
|
|
|
|
static ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred,
|
|
|
|
const ConstantRange &Other);
|
|
|
|
|
|
|
|
/// Produce the largest range such that all values in the returned range
|
|
|
|
/// satisfy the given predicate with all values contained within Other.
|
|
|
|
/// Formally, this returns a subset of
|
|
|
|
/// 'intersection over all y in Other . { x : icmp op x y is true }'. If the
|
|
|
|
/// exact answer is not representable as a ConstantRange, the return value
|
|
|
|
/// will be a proper subset of the above.
|
|
|
|
///
|
|
|
|
/// Example: Pred = ult and Other = i8 [2, 5) returns [0, 2)
|
|
|
|
static ConstantRange makeSatisfyingICmpRegion(CmpInst::Predicate Pred,
|
|
|
|
const ConstantRange &Other);
|
2009-07-11 08:15:39 +02:00
|
|
|
|
2016-05-04 23:32:14 +02:00
|
|
|
/// Produce the exact range such that all values in the returned range satisfy
|
|
|
|
/// the given predicate with any value contained within Other. Formally, this
|
|
|
|
/// returns the exact answer when the superset of 'union over all y in Other
|
|
|
|
/// is exactly same as the subset of intersection over all y in Other.
|
|
|
|
/// { x : icmp op x y is true}'.
|
|
|
|
///
|
|
|
|
/// Example: Pred = ult and Other = i8 3 returns [0, 3)
|
|
|
|
static ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred,
|
|
|
|
const APInt &Other);
|
|
|
|
|
2019-04-28 17:40:56 +02:00
|
|
|
/// Produce the largest range containing all X such that "X BinOp Y" is
|
|
|
|
/// guaranteed not to wrap (overflow) for *all* Y in Other. However, there may
|
|
|
|
/// be *some* Y in Other for which additional X not contained in the result
|
|
|
|
/// also do not overflow.
|
2016-02-22 17:13:02 +01:00
|
|
|
///
|
2019-04-13 21:43:45 +02:00
|
|
|
/// NoWrapKind must be one of OBO::NoUnsignedWrap or OBO::NoSignedWrap.
|
2015-10-22 05:12:57 +02:00
|
|
|
///
|
2016-03-03 19:31:16 +01:00
|
|
|
/// Examples:
|
2015-10-22 05:12:57 +02:00
|
|
|
/// typedef OverflowingBinaryOperator OBO;
|
2019-04-22 11:01:38 +02:00
|
|
|
/// #define MGNR makeGuaranteedNoWrapRegion
|
|
|
|
/// MGNR(Add, [i8 1, 2), OBO::NoSignedWrap) == [-128, 127)
|
|
|
|
/// MGNR(Add, [i8 1, 2), OBO::NoUnsignedWrap) == [0, -1)
|
|
|
|
/// MGNR(Add, [i8 0, 1), OBO::NoUnsignedWrap) == Full Set
|
|
|
|
/// MGNR(Add, [i8 -1, 6), OBO::NoSignedWrap) == [INT_MIN+1, INT_MAX-4)
|
|
|
|
/// MGNR(Sub, [i8 1, 2), OBO::NoSignedWrap) == [-127, 128)
|
|
|
|
/// MGNR(Sub, [i8 1, 2), OBO::NoUnsignedWrap) == [1, 0)
|
|
|
|
static ConstantRange makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp,
|
|
|
|
const ConstantRange &Other,
|
|
|
|
unsigned NoWrapKind);
|
2015-10-22 05:12:57 +02:00
|
|
|
|
2019-04-28 17:40:56 +02:00
|
|
|
/// Produce the range that contains X if and only if "X BinOp Other" does
|
|
|
|
/// not wrap.
|
|
|
|
static ConstantRange makeExactNoWrapRegion(Instruction::BinaryOps BinOp,
|
|
|
|
const APInt &Other,
|
|
|
|
unsigned NoWrapKind);
|
|
|
|
|
2020-07-25 16:21:55 +02:00
|
|
|
/// Returns true if ConstantRange calculations are supported for intrinsic
|
|
|
|
/// with \p IntrinsicID.
|
|
|
|
static bool isIntrinsicSupported(Intrinsic::ID IntrinsicID);
|
|
|
|
|
|
|
|
/// Compute range of intrinsic result for the given operand ranges.
|
|
|
|
static ConstantRange intrinsic(Intrinsic::ID IntrinsicID,
|
|
|
|
ArrayRef<ConstantRange> Ops);
|
|
|
|
|
2016-05-19 05:53:06 +02:00
|
|
|
/// Set up \p Pred and \p RHS such that
|
|
|
|
/// ConstantRange::makeExactICmpRegion(Pred, RHS) == *this. Return true if
|
|
|
|
/// successful.
|
|
|
|
bool getEquivalentICmp(CmpInst::Predicate &Pred, APInt &RHS) const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return the lower value for this range.
|
2007-02-28 20:57:34 +01:00
|
|
|
const APInt &getLower() const { return Lower; }
|
2002-09-02 01:53:39 +02:00
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return the upper value for this range.
|
2009-02-20 23:51:36 +01:00
|
|
|
const APInt &getUpper() const { return Upper; }
|
2002-09-02 01:53:39 +02:00
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Get the bit width of this ConstantRange.
|
2007-02-28 23:02:48 +01:00
|
|
|
uint32_t getBitWidth() const { return Lower.getBitWidth(); }
|
2005-04-21 22:48:15 +02:00
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return true if this set contains all of the elements possible
|
|
|
|
/// for this data-type.
|
2002-09-02 01:53:39 +02:00
|
|
|
bool isFullSet() const;
|
2005-04-21 22:48:15 +02:00
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return true if this set contains no members.
|
2002-09-02 01:53:39 +02:00
|
|
|
bool isEmptySet() const;
|
|
|
|
|
2019-03-27 20:12:09 +01:00
|
|
|
/// Return true if this set wraps around the unsigned domain. Special cases:
|
|
|
|
/// * Empty set: Not wrapped.
|
|
|
|
/// * Full set: Not wrapped.
|
|
|
|
/// * [X, 0) == [X, Max]: Not wrapped.
|
|
|
|
bool isWrappedSet() const;
|
|
|
|
|
2019-03-27 19:19:33 +01:00
|
|
|
/// Return true if the exclusive upper bound wraps around the unsigned
|
|
|
|
/// domain. Special cases:
|
|
|
|
/// * Empty set: Not wrapped.
|
|
|
|
/// * Full set: Not wrapped.
|
|
|
|
/// * [X, 0): Wrapped.
|
|
|
|
bool isUpperWrapped() const;
|
2004-03-30 02:20:08 +02:00
|
|
|
|
2019-03-26 23:37:26 +01:00
|
|
|
/// Return true if this set wraps around the signed domain. Special cases:
|
|
|
|
/// * Empty set: Not wrapped.
|
|
|
|
/// * Full set: Not wrapped.
|
|
|
|
/// * [X, SignedMin) == [X, SignedMax]: Not wrapped.
|
2010-09-07 01:52:49 +02:00
|
|
|
bool isSignWrappedSet() const;
|
|
|
|
|
2019-03-27 20:12:09 +01:00
|
|
|
/// Return true if the (exclusive) upper bound wraps around the signed
|
|
|
|
/// domain. Special cases:
|
|
|
|
/// * Empty set: Not wrapped.
|
|
|
|
/// * Full set: Not wrapped.
|
|
|
|
/// * [X, SignedMin): Wrapped.
|
|
|
|
bool isUpperSignWrapped() const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return true if the specified value is in the set.
|
2007-03-01 08:54:15 +01:00
|
|
|
bool contains(const APInt &Val) const;
|
2005-04-21 22:48:15 +02:00
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return true if the other range is a subset of this one.
|
2009-07-11 08:15:39 +02:00
|
|
|
bool contains(const ConstantRange &CR) const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// If this set contains a single element, return it, otherwise return null.
|
2009-07-10 01:16:10 +02:00
|
|
|
const APInt *getSingleElement() const {
|
|
|
|
if (Upper == Lower + 1)
|
|
|
|
return &Lower;
|
2014-04-09 08:08:46 +02:00
|
|
|
return nullptr;
|
2009-07-10 01:16:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 22:59:05 +02:00
|
|
|
/// If this set contains all but a single element, return it, otherwise return
|
|
|
|
/// null.
|
|
|
|
const APInt *getSingleMissingElement() const {
|
|
|
|
if (Lower == Upper + 1)
|
|
|
|
return &Upper;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return true if this set contains exactly one member.
|
2014-04-09 08:08:46 +02:00
|
|
|
bool isSingleElement() const { return getSingleElement() != nullptr; }
|
2009-07-10 01:16:10 +02:00
|
|
|
|
2017-03-20 07:33:07 +01:00
|
|
|
/// Compare set size of this range with the range CR.
|
2017-05-07 23:48:08 +02:00
|
|
|
bool isSizeStrictlySmallerThan(const ConstantRange &CR) const;
|
2017-03-20 07:33:07 +01:00
|
|
|
|
2019-04-07 19:52:40 +02:00
|
|
|
/// Compare set size of this range with Value.
|
2017-05-08 00:22:11 +02:00
|
|
|
bool isSizeLargerThan(uint64_t MaxSize) const;
|
|
|
|
|
2019-04-07 19:52:40 +02:00
|
|
|
/// Return true if all values in this range are negative.
|
|
|
|
bool isAllNegative() const;
|
|
|
|
|
|
|
|
/// Return true if all values in this range are non-negative.
|
|
|
|
bool isAllNonNegative() const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return the largest unsigned value contained in the ConstantRange.
|
2007-03-10 16:54:12 +01:00
|
|
|
APInt getUnsignedMax() const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return the smallest unsigned value contained in the ConstantRange.
|
2007-03-10 16:54:12 +01:00
|
|
|
APInt getUnsignedMin() const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return the largest signed value contained in the ConstantRange.
|
2007-03-10 16:54:12 +01:00
|
|
|
APInt getSignedMax() const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return the smallest signed value contained in the ConstantRange.
|
2007-03-10 16:54:12 +01:00
|
|
|
APInt getSignedMin() const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return true if this range is equal to another range.
|
2009-07-10 01:16:10 +02:00
|
|
|
bool operator==(const ConstantRange &CR) const {
|
|
|
|
return Lower == CR.Lower && Upper == CR.Upper;
|
|
|
|
}
|
|
|
|
bool operator!=(const ConstantRange &CR) const {
|
|
|
|
return !operator==(CR);
|
|
|
|
}
|
|
|
|
|
2020-09-22 14:17:24 +02:00
|
|
|
/// Compute the maximal number of active bits needed to represent every value
|
|
|
|
/// in this range.
|
|
|
|
unsigned getActiveBits() const;
|
|
|
|
|
2020-09-22 14:51:25 +02:00
|
|
|
/// Compute the maximal number of bits needed to represent every value
|
|
|
|
/// in this signed range.
|
|
|
|
unsigned getMinSignedBits() const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Subtract the specified constant from the endpoints of this constant range.
|
2007-02-28 20:57:34 +01:00
|
|
|
ConstantRange subtract(const APInt &CI) const;
|
2004-03-30 02:20:08 +02:00
|
|
|
|
2017-04-18 16:27:24 +02:00
|
|
|
/// Subtract the specified range from this range (aka relative complement of
|
|
|
|
/// the sets).
|
2012-06-28 18:10:13 +02:00
|
|
|
ConstantRange difference(const ConstantRange &CR) const;
|
|
|
|
|
2019-04-07 20:44:36 +02:00
|
|
|
/// If represented precisely, the result of some range operations may consist
|
|
|
|
/// of multiple disjoint ranges. As only a single range may be returned, any
|
|
|
|
/// range covering these disjoint ranges constitutes a valid result, but some
|
|
|
|
/// may be more useful than others depending on context. The preferred range
|
|
|
|
/// type specifies whether a range that is non-wrapping in the unsigned or
|
|
|
|
/// signed domain, or has the smallest size, is preferred. If a signedness is
|
|
|
|
/// preferred but all ranges are non-wrapping or all wrapping, then the
|
|
|
|
/// smallest set size is preferred. If there are multiple smallest sets, any
|
|
|
|
/// one of them may be returned.
|
|
|
|
enum PreferredRangeType { Smallest, Unsigned, Signed };
|
|
|
|
|
|
|
|
/// Return the range that results from the intersection of this range with
|
|
|
|
/// another range. If the intersection is disjoint, such that two results
|
|
|
|
/// are possible, the preferred range is determined by the PreferredRangeType.
|
|
|
|
ConstantRange intersectWith(const ConstantRange &CR,
|
|
|
|
PreferredRangeType Type = Smallest) const;
|
2007-07-14 04:51:34 +02:00
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return the range that results from the union of this range
|
2007-02-11 01:58:49 +01:00
|
|
|
/// with another range. The resultant range is guaranteed to include the
|
|
|
|
/// elements of both sets, but may contain more. For example, [3, 9) union
|
|
|
|
/// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
|
|
|
|
/// in either set before.
|
2019-04-07 22:20:24 +02:00
|
|
|
ConstantRange unionWith(const ConstantRange &CR,
|
|
|
|
PreferredRangeType Type = Smallest) const;
|
2002-09-02 02:18:26 +02:00
|
|
|
|
2016-12-01 21:08:47 +01:00
|
|
|
/// Return a new range representing the possible values resulting
|
|
|
|
/// from an application of the specified cast operator to this range. \p
|
|
|
|
/// BitWidth is the target bitwidth of the cast. For casts which don't
|
|
|
|
/// change bitwidth, it must be the same as the source bitwidth. For casts
|
|
|
|
/// which do change bitwidth, the bitwidth must be consistent with the
|
|
|
|
/// requested cast and source bitwidth.
|
|
|
|
ConstantRange castOp(Instruction::CastOps CastOp,
|
|
|
|
uint32_t BitWidth) const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return a new range in the specified integer type, which must
|
2004-03-30 02:20:08 +02:00
|
|
|
/// be strictly larger than the current type. The returned range will
|
|
|
|
/// correspond to the possible range of values if the source range had been
|
2007-02-28 23:02:48 +01:00
|
|
|
/// zero extended to BitWidth.
|
|
|
|
ConstantRange zeroExtend(uint32_t BitWidth) const;
|
2004-03-30 02:20:08 +02:00
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return a new range in the specified integer type, which must
|
2007-04-07 17:41:33 +02:00
|
|
|
/// be strictly larger than the current type. The returned range will
|
|
|
|
/// correspond to the possible range of values if the source range had been
|
|
|
|
/// sign extended to BitWidth.
|
|
|
|
ConstantRange signExtend(uint32_t BitWidth) const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return a new range in the specified integer type, which must be
|
2004-03-30 02:20:08 +02:00
|
|
|
/// strictly smaller than the current type. The returned range will
|
|
|
|
/// correspond to the possible range of values if the source range had been
|
|
|
|
/// truncated to the specified type.
|
2007-02-28 23:02:48 +01:00
|
|
|
ConstantRange truncate(uint32_t BitWidth) const;
|
2004-03-30 02:20:08 +02:00
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Make this range have the bit width given by \p BitWidth. The
|
2009-11-09 16:36:28 +01:00
|
|
|
/// value is zero extended, truncated, or left alone to make it that width.
|
|
|
|
ConstantRange zextOrTrunc(uint32_t BitWidth) const;
|
2015-09-22 13:14:39 +02:00
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Make this range have the bit width given by \p BitWidth. The
|
2009-11-09 16:36:28 +01:00
|
|
|
/// value is sign extended, truncated, or left alone to make it that width.
|
|
|
|
ConstantRange sextOrTrunc(uint32_t BitWidth) const;
|
|
|
|
|
2016-12-01 21:08:47 +01:00
|
|
|
/// Return a new range representing the possible values resulting
|
|
|
|
/// from an application of the specified binary operator to an left hand side
|
|
|
|
/// of this range and a right hand side of \p Other.
|
|
|
|
ConstantRange binaryOp(Instruction::BinaryOps BinOp,
|
|
|
|
const ConstantRange &Other) const;
|
|
|
|
|
2019-10-23 15:56:04 +02:00
|
|
|
/// Return a new range representing the possible values resulting
|
|
|
|
/// from an application of the specified overflowing binary operator to a
|
|
|
|
/// left hand side of this range and a right hand side of \p Other given
|
|
|
|
/// the provided knowledge about lack of wrapping \p NoWrapKind.
|
|
|
|
ConstantRange overflowingBinaryOp(Instruction::BinaryOps BinOp,
|
|
|
|
const ConstantRange &Other,
|
|
|
|
unsigned NoWrapKind) const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return a new range representing the possible values resulting
|
2010-08-12 00:04:36 +02:00
|
|
|
/// from an addition of a value in this range and a value in \p Other.
|
2009-07-10 00:07:27 +02:00
|
|
|
ConstantRange add(const ConstantRange &Other) const;
|
|
|
|
|
2019-09-30 14:57:53 +02:00
|
|
|
/// Return a new range representing the possible values resulting
|
|
|
|
/// from an addition with wrap type \p NoWrapKind of a value in this
|
|
|
|
/// range and a value in \p Other.
|
|
|
|
/// If the result range is disjoint, the preferred range is determined by the
|
|
|
|
/// \p PreferredRangeType.
|
|
|
|
ConstantRange addWithNoWrap(const ConstantRange &Other, unsigned NoWrapKind,
|
|
|
|
PreferredRangeType RangeType = Smallest) const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return a new range representing the possible values resulting
|
2010-08-12 00:04:36 +02:00
|
|
|
/// from a subtraction of a value in this range and a value in \p Other.
|
|
|
|
ConstantRange sub(const ConstantRange &Other) const;
|
|
|
|
|
2019-11-06 23:21:29 +01:00
|
|
|
/// Return a new range representing the possible values resulting
|
|
|
|
/// from an subtraction with wrap type \p NoWrapKind of a value in this
|
|
|
|
/// range and a value in \p Other.
|
|
|
|
/// If the result range is disjoint, the preferred range is determined by the
|
|
|
|
/// \p PreferredRangeType.
|
|
|
|
ConstantRange subWithNoWrap(const ConstantRange &Other, unsigned NoWrapKind,
|
|
|
|
PreferredRangeType RangeType = Smallest) const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return a new range representing the possible values resulting
|
2015-03-06 16:50:47 +01:00
|
|
|
/// from a multiplication of a value in this range and a value in \p Other,
|
|
|
|
/// treating both this and \p Other as unsigned ranges.
|
2009-07-10 00:07:27 +02:00
|
|
|
ConstantRange multiply(const ConstantRange &Other) const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return a new range representing the possible values resulting
|
2010-08-12 00:04:36 +02:00
|
|
|
/// from a signed maximum of a value in this range and a value in \p Other.
|
2009-07-10 00:07:27 +02:00
|
|
|
ConstantRange smax(const ConstantRange &Other) const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return a new range representing the possible values resulting
|
2010-08-12 00:04:36 +02:00
|
|
|
/// from an unsigned maximum of a value in this range and a value in \p Other.
|
2009-07-10 00:07:27 +02:00
|
|
|
ConstantRange umax(const ConstantRange &Other) const;
|
|
|
|
|
2016-02-26 23:08:18 +01:00
|
|
|
/// Return a new range representing the possible values resulting
|
|
|
|
/// from a signed minimum of a value in this range and a value in \p Other.
|
|
|
|
ConstantRange smin(const ConstantRange &Other) const;
|
|
|
|
|
|
|
|
/// Return a new range representing the possible values resulting
|
|
|
|
/// from an unsigned minimum of a value in this range and a value in \p Other.
|
|
|
|
ConstantRange umin(const ConstantRange &Other) const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return a new range representing the possible values resulting
|
2010-08-12 00:04:36 +02:00
|
|
|
/// from an unsigned division of a value in this range and a value in
|
|
|
|
/// \p Other.
|
2009-07-10 00:07:27 +02:00
|
|
|
ConstantRange udiv(const ConstantRange &Other) const;
|
|
|
|
|
2019-06-03 20:19:54 +02:00
|
|
|
/// Return a new range representing the possible values resulting
|
|
|
|
/// from a signed division of a value in this range and a value in
|
|
|
|
/// \p Other. Division by zero and division of SignedMin by -1 are considered
|
|
|
|
/// undefined behavior, in line with IR, and do not contribute towards the
|
|
|
|
/// result.
|
|
|
|
ConstantRange sdiv(const ConstantRange &Other) const;
|
|
|
|
|
2019-04-23 20:00:17 +02:00
|
|
|
/// Return a new range representing the possible values resulting
|
|
|
|
/// from an unsigned remainder operation of a value in this range and a
|
|
|
|
/// value in \p Other.
|
|
|
|
ConstantRange urem(const ConstantRange &Other) const;
|
|
|
|
|
2019-05-06 18:59:37 +02:00
|
|
|
/// Return a new range representing the possible values resulting
|
|
|
|
/// from a signed remainder operation of a value in this range and a
|
|
|
|
/// value in \p Other.
|
|
|
|
ConstantRange srem(const ConstantRange &Other) const;
|
|
|
|
|
2020-09-22 10:50:25 +02:00
|
|
|
/// Return a new range representing the possible values resulting from
|
|
|
|
/// a binary-xor of a value in this range by an all-one value,
|
|
|
|
/// aka bitwise complement operation.
|
|
|
|
ConstantRange binaryNot() const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return a new range representing the possible values resulting
|
2010-09-07 07:39:02 +02:00
|
|
|
/// from a binary-and of a value in this range by a value in \p Other.
|
|
|
|
ConstantRange binaryAnd(const ConstantRange &Other) const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return a new range representing the possible values resulting
|
2010-09-07 07:39:02 +02:00
|
|
|
/// from a binary-or of a value in this range by a value in \p Other.
|
|
|
|
ConstantRange binaryOr(const ConstantRange &Other) const;
|
|
|
|
|
2020-03-24 13:47:24 +01:00
|
|
|
/// Return a new range representing the possible values resulting
|
|
|
|
/// from a binary-xor of a value in this range by a value in \p Other.
|
|
|
|
ConstantRange binaryXor(const ConstantRange &Other) const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return a new range representing the possible values resulting
|
2010-08-12 00:04:36 +02:00
|
|
|
/// from a left shift of a value in this range by a value in \p Other.
|
|
|
|
/// TODO: This isn't fully implemented yet.
|
|
|
|
ConstantRange shl(const ConstantRange &Other) const;
|
2009-11-12 15:53:53 +01:00
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return a new range representing the possible values resulting from a
|
|
|
|
/// logical right shift of a value in this range and a value in \p Other.
|
2010-08-12 00:04:36 +02:00
|
|
|
ConstantRange lshr(const ConstantRange &Other) const;
|
2009-11-12 15:53:53 +01:00
|
|
|
|
2017-12-18 14:01:32 +01:00
|
|
|
/// Return a new range representing the possible values resulting from a
|
|
|
|
/// arithmetic right shift of a value in this range and a value in \p Other.
|
|
|
|
ConstantRange ashr(const ConstantRange &Other) const;
|
|
|
|
|
2019-04-21 17:23:05 +02:00
|
|
|
/// Perform an unsigned saturating addition of two constant ranges.
|
|
|
|
ConstantRange uadd_sat(const ConstantRange &Other) const;
|
|
|
|
|
|
|
|
/// Perform a signed saturating addition of two constant ranges.
|
|
|
|
ConstantRange sadd_sat(const ConstantRange &Other) const;
|
|
|
|
|
|
|
|
/// Perform an unsigned saturating subtraction of two constant ranges.
|
|
|
|
ConstantRange usub_sat(const ConstantRange &Other) const;
|
|
|
|
|
|
|
|
/// Perform a signed saturating subtraction of two constant ranges.
|
|
|
|
ConstantRange ssub_sat(const ConstantRange &Other) const;
|
|
|
|
|
2019-11-08 15:19:56 +01:00
|
|
|
/// Perform an unsigned saturating multiplication of two constant ranges.
|
|
|
|
ConstantRange umul_sat(const ConstantRange &Other) const;
|
|
|
|
|
|
|
|
/// Perform a signed saturating multiplication of two constant ranges.
|
|
|
|
ConstantRange smul_sat(const ConstantRange &Other) const;
|
|
|
|
|
2019-11-08 08:24:46 +01:00
|
|
|
/// Perform an unsigned saturating left shift of this constant range by a
|
|
|
|
/// value in \p Other.
|
|
|
|
ConstantRange ushl_sat(const ConstantRange &Other) const;
|
|
|
|
|
|
|
|
/// Perform a signed saturating left shift of this constant range by a
|
|
|
|
/// value in \p Other.
|
|
|
|
ConstantRange sshl_sat(const ConstantRange &Other) const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Return a new range that is the logical not of the current set.
|
2010-08-07 07:47:46 +02:00
|
|
|
ConstantRange inverse() const;
|
2015-09-22 13:14:39 +02:00
|
|
|
|
2019-04-26 18:50:31 +02:00
|
|
|
/// Calculate absolute value range. If the original range contains signed
|
2020-07-30 22:47:33 +02:00
|
|
|
/// min, then the resulting range will contain signed min if and only if
|
|
|
|
/// \p IntMinIsPoison is false.
|
|
|
|
ConstantRange abs(bool IntMinIsPoison = false) const;
|
2019-04-26 18:50:31 +02:00
|
|
|
|
2019-03-15 18:29:05 +01:00
|
|
|
/// Represents whether an operation on the given constant range is known to
|
|
|
|
/// always or never overflow.
|
2019-05-28 20:08:31 +02:00
|
|
|
enum class OverflowResult {
|
|
|
|
/// Always overflows in the direction of signed/unsigned min value.
|
|
|
|
AlwaysOverflowsLow,
|
|
|
|
/// Always overflows in the direction of signed/unsigned max value.
|
|
|
|
AlwaysOverflowsHigh,
|
|
|
|
/// May or may not overflow.
|
|
|
|
MayOverflow,
|
|
|
|
/// Never overflows.
|
|
|
|
NeverOverflows,
|
|
|
|
};
|
2019-03-15 18:29:05 +01:00
|
|
|
|
|
|
|
/// Return whether unsigned add of the two ranges always/never overflows.
|
|
|
|
OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const;
|
|
|
|
|
|
|
|
/// Return whether signed add of the two ranges always/never overflows.
|
|
|
|
OverflowResult signedAddMayOverflow(const ConstantRange &Other) const;
|
|
|
|
|
|
|
|
/// Return whether unsigned sub of the two ranges always/never overflows.
|
|
|
|
OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const;
|
|
|
|
|
|
|
|
/// Return whether signed sub of the two ranges always/never overflows.
|
|
|
|
OverflowResult signedSubMayOverflow(const ConstantRange &Other) const;
|
|
|
|
|
2019-04-11 23:10:33 +02:00
|
|
|
/// Return whether unsigned mul of the two ranges always/never overflows.
|
|
|
|
OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const;
|
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Print out the bounds to a stream.
|
2009-07-10 01:16:10 +02:00
|
|
|
void print(raw_ostream &OS) const;
|
2009-07-10 00:07:27 +02:00
|
|
|
|
2015-01-09 18:11:51 +01:00
|
|
|
/// Allow printing from a debugger easily.
|
2009-07-10 01:16:10 +02:00
|
|
|
void dump() const;
|
2002-09-02 01:53:39 +02:00
|
|
|
};
|
|
|
|
|
2009-07-10 01:16:10 +02:00
|
|
|
inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) {
|
2002-09-02 02:18:26 +02:00
|
|
|
CR.print(OS);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2016-10-21 21:59:26 +02:00
|
|
|
/// Parse out a conservative ConstantRange from !range metadata.
|
|
|
|
///
|
|
|
|
/// E.g. if RangeMD is !{i32 0, i32 10, i32 15, i32 20} then return [0, 20).
|
|
|
|
ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD);
|
|
|
|
|
2017-06-20 00:05:08 +02:00
|
|
|
} // end namespace llvm
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2017-06-20 00:05:08 +02:00
|
|
|
#endif // LLVM_IR_CONSTANTRANGE_H
|