#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/FoldingSet.h"
+#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/Pass.h"
#include "llvm/Support/Allocator.h"
-#include "llvm/Support/ConstantRange.h"
#include "llvm/Support/DataTypes.h"
#include <map>
--- /dev/null
+//===- ConstantRange.h - Represent a range ----------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// 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
+// ranges: :
+//
+// [F, F) = {} = Empty set
+// [T, F) = {T}
+// [F, T) = {F}
+// [T, T) = {F, T} = Full set
+//
+// 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
+//
+// Note that ConstantRange can be used to represent either signed or
+// unsigned ranges.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_SUPPORT_CONSTANTRANGE_H
+#define LLVM_SUPPORT_CONSTANTRANGE_H
+
+#include "llvm/ADT/APInt.h"
+#include "llvm/Support/DataTypes.h"
+
+namespace llvm {
+
+/// ConstantRange - This class represents an range of values.
+///
+class ConstantRange {
+ APInt Lower, Upper;
+
+ // If we have move semantics, pass APInts by value and move them into place.
+ typedef APInt APIntMoveTy;
+
+public:
+ /// Initialize a full (the default) or empty set for the specified bit width.
+ ///
+ explicit ConstantRange(uint32_t BitWidth, bool isFullSet = true);
+
+ /// Initialize a range to hold the single specified value.
+ ///
+ ConstantRange(APIntMoveTy Value);
+
+ /// @brief Initialize a range of values explicitly. This will assert out if
+ /// 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.
+ ConstantRange(APIntMoveTy Lower, APIntMoveTy Upper);
+
+ /// makeICmpRegion - Produce the smallest range that contains all values that
+ /// might satisfy the comparison specified by Pred when compared to any value
+ /// contained within Other.
+ ///
+ /// Solves for range X in 'for all x in X, there exists a y in Y such that
+ /// icmp op x, y is true'. Every value that might make the comparison true
+ /// is included in the resulting range.
+ static ConstantRange makeICmpRegion(unsigned Pred,
+ const ConstantRange &Other);
+
+ /// getLower - Return the lower value for this range...
+ ///
+ const APInt &getLower() const { return Lower; }
+
+ /// getUpper - Return the upper value for this range...
+ ///
+ const APInt &getUpper() const { return Upper; }
+
+ /// getBitWidth - get the bit width of this ConstantRange
+ ///
+ uint32_t getBitWidth() const { return Lower.getBitWidth(); }
+
+ /// isFullSet - Return true if this set contains all of the elements possible
+ /// for this data-type
+ ///
+ bool isFullSet() const;
+
+ /// isEmptySet - Return true if this set contains no members.
+ ///
+ bool isEmptySet() const;
+
+ /// isWrappedSet - Return true if this set wraps around the top of the range,
+ /// for example: [100, 8)
+ ///
+ bool isWrappedSet() const;
+
+ /// isSignWrappedSet - Return true if this set wraps around the INT_MIN of
+ /// its bitwidth, for example: i8 [120, 140).
+ ///
+ bool isSignWrappedSet() const;
+
+ /// contains - Return true if the specified value is in the set.
+ ///
+ bool contains(const APInt &Val) const;
+
+ /// contains - Return true if the other range is a subset of this one.
+ ///
+ bool contains(const ConstantRange &CR) const;
+
+ /// getSingleElement - If this set contains a single element, return it,
+ /// otherwise return null.
+ ///
+ const APInt *getSingleElement() const {
+ if (Upper == Lower + 1)
+ return &Lower;
+ return 0;
+ }
+
+ /// isSingleElement - Return true if this set contains exactly one member.
+ ///
+ bool isSingleElement() const { return getSingleElement() != 0; }
+
+ /// getSetSize - Return the number of elements in this set.
+ ///
+ APInt getSetSize() const;
+
+ /// getUnsignedMax - Return the largest unsigned value contained in the
+ /// ConstantRange.
+ ///
+ APInt getUnsignedMax() const;
+
+ /// getUnsignedMin - Return the smallest unsigned value contained in the
+ /// ConstantRange.
+ ///
+ APInt getUnsignedMin() const;
+
+ /// getSignedMax - Return the largest signed value contained in the
+ /// ConstantRange.
+ ///
+ APInt getSignedMax() const;
+
+ /// getSignedMin - Return the smallest signed value contained in the
+ /// ConstantRange.
+ ///
+ APInt getSignedMin() const;
+
+ /// operator== - Return true if this range is equal to another range.
+ ///
+ bool operator==(const ConstantRange &CR) const {
+ return Lower == CR.Lower && Upper == CR.Upper;
+ }
+ bool operator!=(const ConstantRange &CR) const {
+ return !operator==(CR);
+ }
+
+ /// subtract - Subtract the specified constant from the endpoints of this
+ /// constant range.
+ ConstantRange subtract(const APInt &CI) const;
+
+ /// \brief Subtract the specified range from this range (aka relative
+ /// complement of the sets).
+ ConstantRange difference(const ConstantRange &CR) const;
+
+ /// intersectWith - Return the range that results from the intersection of
+ /// this range with another range. The resultant range is guaranteed to
+ /// include all elements contained in both input ranges, and to have the
+ /// smallest possible set size that does so. Because there may be two
+ /// intersections with the same set size, A.intersectWith(B) might not
+ /// be equal to B.intersectWith(A).
+ ///
+ ConstantRange intersectWith(const ConstantRange &CR) const;
+
+ /// unionWith - Return the range that results from the union of this range
+ /// 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.
+ ///
+ ConstantRange unionWith(const ConstantRange &CR) const;
+
+ /// zeroExtend - Return a new range in the specified integer type, which must
+ /// be strictly larger than the current type. The returned range will
+ /// correspond to the possible range of values if the source range had been
+ /// zero extended to BitWidth.
+ ConstantRange zeroExtend(uint32_t BitWidth) const;
+
+ /// signExtend - Return a new range in the specified integer type, which must
+ /// 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;
+
+ /// truncate - Return a new range in the specified integer type, which must be
+ /// 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.
+ ConstantRange truncate(uint32_t BitWidth) const;
+
+ /// zextOrTrunc - make this range have the bit width given by \p BitWidth. The
+ /// value is zero extended, truncated, or left alone to make it that width.
+ ConstantRange zextOrTrunc(uint32_t BitWidth) const;
+
+ /// sextOrTrunc - make this range have the bit width given by \p BitWidth. The
+ /// value is sign extended, truncated, or left alone to make it that width.
+ ConstantRange sextOrTrunc(uint32_t BitWidth) const;
+
+ /// add - Return a new range representing the possible values resulting
+ /// from an addition of a value in this range and a value in \p Other.
+ ConstantRange add(const ConstantRange &Other) const;
+
+ /// sub - Return a new range representing the possible values resulting
+ /// from a subtraction of a value in this range and a value in \p Other.
+ ConstantRange sub(const ConstantRange &Other) const;
+
+ /// multiply - Return a new range representing the possible values resulting
+ /// from a multiplication of a value in this range and a value in \p Other.
+ /// TODO: This isn't fully implemented yet.
+ ConstantRange multiply(const ConstantRange &Other) const;
+
+ /// smax - Return a new range representing the possible values resulting
+ /// from a signed maximum of a value in this range and a value in \p Other.
+ ConstantRange smax(const ConstantRange &Other) const;
+
+ /// umax - Return a new range representing the possible values resulting
+ /// from an unsigned maximum of a value in this range and a value in \p Other.
+ ConstantRange umax(const ConstantRange &Other) const;
+
+ /// udiv - Return a new range representing the possible values resulting
+ /// from an unsigned division of a value in this range and a value in
+ /// \p Other.
+ ConstantRange udiv(const ConstantRange &Other) const;
+
+ /// binaryAnd - return a new range representing the possible values resulting
+ /// from a binary-and of a value in this range by a value in \p Other.
+ ConstantRange binaryAnd(const ConstantRange &Other) const;
+
+ /// binaryOr - return a new range representing the possible values resulting
+ /// from a binary-or of a value in this range by a value in \p Other.
+ ConstantRange binaryOr(const ConstantRange &Other) const;
+
+ /// shl - Return a new range representing the possible values resulting
+ /// 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;
+
+ /// lshr - 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.
+ ConstantRange lshr(const ConstantRange &Other) const;
+
+ /// inverse - Return a new range that is the logical not of the current set.
+ ///
+ ConstantRange inverse() const;
+
+ /// print - Print out the bounds to a stream...
+ ///
+ void print(raw_ostream &OS) const;
+
+ /// dump - Allow printing from a debugger easily...
+ ///
+ void dump() const;
+};
+
+inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) {
+ CR.print(OS);
+ return OS;
+}
+
+} // End llvm namespace
+
+#endif
+++ /dev/null
-//===-- llvm/Support/ConstantRange.h - Represent a range --------*- C++ -*-===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// 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
-// ranges: :
-//
-// [F, F) = {} = Empty set
-// [T, F) = {T}
-// [F, T) = {F}
-// [T, T) = {F, T} = Full set
-//
-// 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
-//
-// Note that ConstantRange can be used to represent either signed or
-// unsigned ranges.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_SUPPORT_CONSTANTRANGE_H
-#define LLVM_SUPPORT_CONSTANTRANGE_H
-
-#include "llvm/ADT/APInt.h"
-#include "llvm/Support/DataTypes.h"
-
-namespace llvm {
-
-/// ConstantRange - This class represents an range of values.
-///
-class ConstantRange {
- APInt Lower, Upper;
-
- // If we have move semantics, pass APInts by value and move them into place.
- typedef APInt APIntMoveTy;
-
-public:
- /// Initialize a full (the default) or empty set for the specified bit width.
- ///
- explicit ConstantRange(uint32_t BitWidth, bool isFullSet = true);
-
- /// Initialize a range to hold the single specified value.
- ///
- ConstantRange(APIntMoveTy Value);
-
- /// @brief Initialize a range of values explicitly. This will assert out if
- /// 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.
- ConstantRange(APIntMoveTy Lower, APIntMoveTy Upper);
-
- /// makeICmpRegion - Produce the smallest range that contains all values that
- /// might satisfy the comparison specified by Pred when compared to any value
- /// contained within Other.
- ///
- /// Solves for range X in 'for all x in X, there exists a y in Y such that
- /// icmp op x, y is true'. Every value that might make the comparison true
- /// is included in the resulting range.
- static ConstantRange makeICmpRegion(unsigned Pred,
- const ConstantRange &Other);
-
- /// getLower - Return the lower value for this range...
- ///
- const APInt &getLower() const { return Lower; }
-
- /// getUpper - Return the upper value for this range...
- ///
- const APInt &getUpper() const { return Upper; }
-
- /// getBitWidth - get the bit width of this ConstantRange
- ///
- uint32_t getBitWidth() const { return Lower.getBitWidth(); }
-
- /// isFullSet - Return true if this set contains all of the elements possible
- /// for this data-type
- ///
- bool isFullSet() const;
-
- /// isEmptySet - Return true if this set contains no members.
- ///
- bool isEmptySet() const;
-
- /// isWrappedSet - Return true if this set wraps around the top of the range,
- /// for example: [100, 8)
- ///
- bool isWrappedSet() const;
-
- /// isSignWrappedSet - Return true if this set wraps around the INT_MIN of
- /// its bitwidth, for example: i8 [120, 140).
- ///
- bool isSignWrappedSet() const;
-
- /// contains - Return true if the specified value is in the set.
- ///
- bool contains(const APInt &Val) const;
-
- /// contains - Return true if the other range is a subset of this one.
- ///
- bool contains(const ConstantRange &CR) const;
-
- /// getSingleElement - If this set contains a single element, return it,
- /// otherwise return null.
- ///
- const APInt *getSingleElement() const {
- if (Upper == Lower + 1)
- return &Lower;
- return 0;
- }
-
- /// isSingleElement - Return true if this set contains exactly one member.
- ///
- bool isSingleElement() const { return getSingleElement() != 0; }
-
- /// getSetSize - Return the number of elements in this set.
- ///
- APInt getSetSize() const;
-
- /// getUnsignedMax - Return the largest unsigned value contained in the
- /// ConstantRange.
- ///
- APInt getUnsignedMax() const;
-
- /// getUnsignedMin - Return the smallest unsigned value contained in the
- /// ConstantRange.
- ///
- APInt getUnsignedMin() const;
-
- /// getSignedMax - Return the largest signed value contained in the
- /// ConstantRange.
- ///
- APInt getSignedMax() const;
-
- /// getSignedMin - Return the smallest signed value contained in the
- /// ConstantRange.
- ///
- APInt getSignedMin() const;
-
- /// operator== - Return true if this range is equal to another range.
- ///
- bool operator==(const ConstantRange &CR) const {
- return Lower == CR.Lower && Upper == CR.Upper;
- }
- bool operator!=(const ConstantRange &CR) const {
- return !operator==(CR);
- }
-
- /// subtract - Subtract the specified constant from the endpoints of this
- /// constant range.
- ConstantRange subtract(const APInt &CI) const;
-
- /// \brief Subtract the specified range from this range (aka relative
- /// complement of the sets).
- ConstantRange difference(const ConstantRange &CR) const;
-
- /// intersectWith - Return the range that results from the intersection of
- /// this range with another range. The resultant range is guaranteed to
- /// include all elements contained in both input ranges, and to have the
- /// smallest possible set size that does so. Because there may be two
- /// intersections with the same set size, A.intersectWith(B) might not
- /// be equal to B.intersectWith(A).
- ///
- ConstantRange intersectWith(const ConstantRange &CR) const;
-
- /// unionWith - Return the range that results from the union of this range
- /// 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.
- ///
- ConstantRange unionWith(const ConstantRange &CR) const;
-
- /// zeroExtend - Return a new range in the specified integer type, which must
- /// be strictly larger than the current type. The returned range will
- /// correspond to the possible range of values if the source range had been
- /// zero extended to BitWidth.
- ConstantRange zeroExtend(uint32_t BitWidth) const;
-
- /// signExtend - Return a new range in the specified integer type, which must
- /// 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;
-
- /// truncate - Return a new range in the specified integer type, which must be
- /// 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.
- ConstantRange truncate(uint32_t BitWidth) const;
-
- /// zextOrTrunc - make this range have the bit width given by \p BitWidth. The
- /// value is zero extended, truncated, or left alone to make it that width.
- ConstantRange zextOrTrunc(uint32_t BitWidth) const;
-
- /// sextOrTrunc - make this range have the bit width given by \p BitWidth. The
- /// value is sign extended, truncated, or left alone to make it that width.
- ConstantRange sextOrTrunc(uint32_t BitWidth) const;
-
- /// add - Return a new range representing the possible values resulting
- /// from an addition of a value in this range and a value in \p Other.
- ConstantRange add(const ConstantRange &Other) const;
-
- /// sub - Return a new range representing the possible values resulting
- /// from a subtraction of a value in this range and a value in \p Other.
- ConstantRange sub(const ConstantRange &Other) const;
-
- /// multiply - Return a new range representing the possible values resulting
- /// from a multiplication of a value in this range and a value in \p Other.
- /// TODO: This isn't fully implemented yet.
- ConstantRange multiply(const ConstantRange &Other) const;
-
- /// smax - Return a new range representing the possible values resulting
- /// from a signed maximum of a value in this range and a value in \p Other.
- ConstantRange smax(const ConstantRange &Other) const;
-
- /// umax - Return a new range representing the possible values resulting
- /// from an unsigned maximum of a value in this range and a value in \p Other.
- ConstantRange umax(const ConstantRange &Other) const;
-
- /// udiv - Return a new range representing the possible values resulting
- /// from an unsigned division of a value in this range and a value in
- /// \p Other.
- ConstantRange udiv(const ConstantRange &Other) const;
-
- /// binaryAnd - return a new range representing the possible values resulting
- /// from a binary-and of a value in this range by a value in \p Other.
- ConstantRange binaryAnd(const ConstantRange &Other) const;
-
- /// binaryOr - return a new range representing the possible values resulting
- /// from a binary-or of a value in this range by a value in \p Other.
- ConstantRange binaryOr(const ConstantRange &Other) const;
-
- /// shl - Return a new range representing the possible values resulting
- /// 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;
-
- /// lshr - 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.
- ConstantRange lshr(const ConstantRange &Other) const;
-
- /// inverse - Return a new range that is the logical not of the current set.
- ///
- ConstantRange inverse() const;
-
- /// print - Print out the bounds to a stream...
- ///
- void print(raw_ostream &OS) const;
-
- /// dump - Allow printing from a debugger easily...
- ///
- void dump() const;
-};
-
-inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) {
- CR.print(OS);
- return OS;
-}
-
-} // End llvm namespace
-
-#endif
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/MemoryBuiltins.h"
#include "llvm/Analysis/ValueTracking.h"
+#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/GetElementPtrTypeIterator.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/ValueHandle.h"
-#include "llvm/Support/ConstantRange.h"
using namespace llvm;
using namespace llvm::PatternMatch;
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/CFG.h"
+#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/ValueHandle.h"
-#include "llvm/Support/ConstantRange.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetLibraryInfo.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
#include "llvm/Analysis/ValueTracking.h"
+#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Operator.h"
#include "llvm/Support/CommandLine.h"
-#include "llvm/Support/ConstantRange.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/MemoryBuiltins.h"
+#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/GetElementPtrTypeIterator.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/PatternMatch.h"
-#include "llvm/Support/ConstantRange.h"
#include "llvm/Support/MathExtras.h"
#include <cstring>
using namespace llvm;
AutoUpgrade.cpp
BasicBlock.cpp
ConstantFold.cpp
+ ConstantRange.cpp
Constants.cpp
Core.cpp
DiagnosticInfo.cpp
--- /dev/null
+//===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// 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
+// ranges (other integral ranges use min/max values for special range values):
+//
+// [F, F) = {} = Empty set
+// [T, F) = {T}
+// [F, T) = {F}
+// [T, T) = {F, T} = Full set
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/InstrTypes.h"
+#include "llvm/IR/ConstantRange.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/raw_ostream.h"
+using namespace llvm;
+
+/// Initialize a full (the default) or empty set for the specified type.
+///
+ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) {
+ if (Full)
+ Lower = Upper = APInt::getMaxValue(BitWidth);
+ else
+ Lower = Upper = APInt::getMinValue(BitWidth);
+}
+
+/// Initialize a range to hold the single specified value.
+///
+ConstantRange::ConstantRange(APIntMoveTy V)
+ : Lower(std::move(V)), Upper(Lower + 1) {}
+
+ConstantRange::ConstantRange(APIntMoveTy L, APIntMoveTy U)
+ : Lower(std::move(L)), Upper(std::move(U)) {
+ assert(Lower.getBitWidth() == Upper.getBitWidth() &&
+ "ConstantRange with unequal bit widths");
+ assert((Lower != Upper || (Lower.isMaxValue() || Lower.isMinValue())) &&
+ "Lower == Upper, but they aren't min or max value!");
+}
+
+ConstantRange ConstantRange::makeICmpRegion(unsigned Pred,
+ const ConstantRange &CR) {
+ if (CR.isEmptySet())
+ return CR;
+
+ uint32_t W = CR.getBitWidth();
+ switch (Pred) {
+ default: llvm_unreachable("Invalid ICmp predicate to makeICmpRegion()");
+ case CmpInst::ICMP_EQ:
+ return CR;
+ case CmpInst::ICMP_NE:
+ if (CR.isSingleElement())
+ return ConstantRange(CR.getUpper(), CR.getLower());
+ return ConstantRange(W);
+ case CmpInst::ICMP_ULT: {
+ APInt UMax(CR.getUnsignedMax());
+ if (UMax.isMinValue())
+ return ConstantRange(W, /* empty */ false);
+ return ConstantRange(APInt::getMinValue(W), UMax);
+ }
+ case CmpInst::ICMP_SLT: {
+ APInt SMax(CR.getSignedMax());
+ if (SMax.isMinSignedValue())
+ return ConstantRange(W, /* empty */ false);
+ return ConstantRange(APInt::getSignedMinValue(W), SMax);
+ }
+ case CmpInst::ICMP_ULE: {
+ APInt UMax(CR.getUnsignedMax());
+ if (UMax.isMaxValue())
+ return ConstantRange(W);
+ return ConstantRange(APInt::getMinValue(W), UMax + 1);
+ }
+ case CmpInst::ICMP_SLE: {
+ APInt SMax(CR.getSignedMax());
+ if (SMax.isMaxSignedValue())
+ return ConstantRange(W);
+ return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
+ }
+ case CmpInst::ICMP_UGT: {
+ APInt UMin(CR.getUnsignedMin());
+ if (UMin.isMaxValue())
+ return ConstantRange(W, /* empty */ false);
+ return ConstantRange(UMin + 1, APInt::getNullValue(W));
+ }
+ case CmpInst::ICMP_SGT: {
+ APInt SMin(CR.getSignedMin());
+ if (SMin.isMaxSignedValue())
+ return ConstantRange(W, /* empty */ false);
+ return ConstantRange(SMin + 1, APInt::getSignedMinValue(W));
+ }
+ case CmpInst::ICMP_UGE: {
+ APInt UMin(CR.getUnsignedMin());
+ if (UMin.isMinValue())
+ return ConstantRange(W);
+ return ConstantRange(UMin, APInt::getNullValue(W));
+ }
+ case CmpInst::ICMP_SGE: {
+ APInt SMin(CR.getSignedMin());
+ if (SMin.isMinSignedValue())
+ return ConstantRange(W);
+ return ConstantRange(SMin, APInt::getSignedMinValue(W));
+ }
+ }
+}
+
+/// isFullSet - Return true if this set contains all of the elements possible
+/// for this data-type
+bool ConstantRange::isFullSet() const {
+ return Lower == Upper && Lower.isMaxValue();
+}
+
+/// isEmptySet - Return true if this set contains no members.
+///
+bool ConstantRange::isEmptySet() const {
+ return Lower == Upper && Lower.isMinValue();
+}
+
+/// isWrappedSet - Return true if this set wraps around the top of the range,
+/// for example: [100, 8)
+///
+bool ConstantRange::isWrappedSet() const {
+ return Lower.ugt(Upper);
+}
+
+/// isSignWrappedSet - Return true if this set wraps around the INT_MIN of
+/// its bitwidth, for example: i8 [120, 140).
+///
+bool ConstantRange::isSignWrappedSet() const {
+ return contains(APInt::getSignedMaxValue(getBitWidth())) &&
+ contains(APInt::getSignedMinValue(getBitWidth()));
+}
+
+/// getSetSize - Return the number of elements in this set.
+///
+APInt ConstantRange::getSetSize() const {
+ if (isFullSet()) {
+ APInt Size(getBitWidth()+1, 0);
+ Size.setBit(getBitWidth());
+ return Size;
+ }
+
+ // This is also correct for wrapped sets.
+ return (Upper - Lower).zext(getBitWidth()+1);
+}
+
+/// getUnsignedMax - Return the largest unsigned value contained in the
+/// ConstantRange.
+///
+APInt ConstantRange::getUnsignedMax() const {
+ if (isFullSet() || isWrappedSet())
+ return APInt::getMaxValue(getBitWidth());
+ return getUpper() - 1;
+}
+
+/// getUnsignedMin - Return the smallest unsigned value contained in the
+/// ConstantRange.
+///
+APInt ConstantRange::getUnsignedMin() const {
+ if (isFullSet() || (isWrappedSet() && getUpper() != 0))
+ return APInt::getMinValue(getBitWidth());
+ return getLower();
+}
+
+/// getSignedMax - Return the largest signed value contained in the
+/// ConstantRange.
+///
+APInt ConstantRange::getSignedMax() const {
+ APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
+ if (!isWrappedSet()) {
+ if (getLower().sle(getUpper() - 1))
+ return getUpper() - 1;
+ return SignedMax;
+ }
+ if (getLower().isNegative() == getUpper().isNegative())
+ return SignedMax;
+ return getUpper() - 1;
+}
+
+/// getSignedMin - Return the smallest signed value contained in the
+/// ConstantRange.
+///
+APInt ConstantRange::getSignedMin() const {
+ APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
+ if (!isWrappedSet()) {
+ if (getLower().sle(getUpper() - 1))
+ return getLower();
+ return SignedMin;
+ }
+ if ((getUpper() - 1).slt(getLower())) {
+ if (getUpper() != SignedMin)
+ return SignedMin;
+ }
+ return getLower();
+}
+
+/// contains - Return true if the specified value is in the set.
+///
+bool ConstantRange::contains(const APInt &V) const {
+ if (Lower == Upper)
+ return isFullSet();
+
+ if (!isWrappedSet())
+ return Lower.ule(V) && V.ult(Upper);
+ return Lower.ule(V) || V.ult(Upper);
+}
+
+/// contains - Return true if the argument is a subset of this range.
+/// Two equal sets contain each other. The empty set contained by all other
+/// sets.
+///
+bool ConstantRange::contains(const ConstantRange &Other) const {
+ if (isFullSet() || Other.isEmptySet()) return true;
+ if (isEmptySet() || Other.isFullSet()) return false;
+
+ if (!isWrappedSet()) {
+ if (Other.isWrappedSet())
+ return false;
+
+ return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
+ }
+
+ if (!Other.isWrappedSet())
+ return Other.getUpper().ule(Upper) ||
+ Lower.ule(Other.getLower());
+
+ return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
+}
+
+/// subtract - Subtract the specified constant from the endpoints of this
+/// constant range.
+ConstantRange ConstantRange::subtract(const APInt &Val) const {
+ assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
+ // If the set is empty or full, don't modify the endpoints.
+ if (Lower == Upper)
+ return *this;
+ return ConstantRange(Lower - Val, Upper - Val);
+}
+
+/// \brief Subtract the specified range from this range (aka relative complement
+/// of the sets).
+ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
+ return intersectWith(CR.inverse());
+}
+
+/// intersectWith - Return the range that results from the intersection of this
+/// range with another range. The resultant range is guaranteed to include all
+/// elements contained in both input ranges, and to have the smallest possible
+/// set size that does so. Because there may be two intersections with the
+/// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
+ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
+ assert(getBitWidth() == CR.getBitWidth() &&
+ "ConstantRange types don't agree!");
+
+ // Handle common cases.
+ if ( isEmptySet() || CR.isFullSet()) return *this;
+ if (CR.isEmptySet() || isFullSet()) return CR;
+
+ if (!isWrappedSet() && CR.isWrappedSet())
+ return CR.intersectWith(*this);
+
+ if (!isWrappedSet() && !CR.isWrappedSet()) {
+ if (Lower.ult(CR.Lower)) {
+ if (Upper.ule(CR.Lower))
+ return ConstantRange(getBitWidth(), false);
+
+ if (Upper.ult(CR.Upper))
+ return ConstantRange(CR.Lower, Upper);
+
+ return CR;
+ }
+ if (Upper.ult(CR.Upper))
+ return *this;
+
+ if (Lower.ult(CR.Upper))
+ return ConstantRange(Lower, CR.Upper);
+
+ return ConstantRange(getBitWidth(), false);
+ }
+
+ if (isWrappedSet() && !CR.isWrappedSet()) {
+ if (CR.Lower.ult(Upper)) {
+ if (CR.Upper.ult(Upper))
+ return CR;
+
+ if (CR.Upper.ule(Lower))
+ return ConstantRange(CR.Lower, Upper);
+
+ if (getSetSize().ult(CR.getSetSize()))
+ return *this;
+ return CR;
+ }
+ if (CR.Lower.ult(Lower)) {
+ if (CR.Upper.ule(Lower))
+ return ConstantRange(getBitWidth(), false);
+
+ return ConstantRange(Lower, CR.Upper);
+ }
+ return CR;
+ }
+
+ if (CR.Upper.ult(Upper)) {
+ if (CR.Lower.ult(Upper)) {
+ if (getSetSize().ult(CR.getSetSize()))
+ return *this;
+ return CR;
+ }
+
+ if (CR.Lower.ult(Lower))
+ return ConstantRange(Lower, CR.Upper);
+
+ return CR;
+ }
+ if (CR.Upper.ule(Lower)) {
+ if (CR.Lower.ult(Lower))
+ return *this;
+
+ return ConstantRange(CR.Lower, Upper);
+ }
+ if (getSetSize().ult(CR.getSetSize()))
+ return *this;
+ return CR;
+}
+
+
+/// unionWith - Return the range that results from the union of this range 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.
+///
+ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
+ assert(getBitWidth() == CR.getBitWidth() &&
+ "ConstantRange types don't agree!");
+
+ if ( isFullSet() || CR.isEmptySet()) return *this;
+ if (CR.isFullSet() || isEmptySet()) return CR;
+
+ if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
+
+ if (!isWrappedSet() && !CR.isWrappedSet()) {
+ if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
+ // If the two ranges are disjoint, find the smaller gap and bridge it.
+ APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
+ if (d1.ult(d2))
+ return ConstantRange(Lower, CR.Upper);
+ return ConstantRange(CR.Lower, Upper);
+ }
+
+ APInt L = Lower, U = Upper;
+ if (CR.Lower.ult(L))
+ L = CR.Lower;
+ if ((CR.Upper - 1).ugt(U - 1))
+ U = CR.Upper;
+
+ if (L == 0 && U == 0)
+ return ConstantRange(getBitWidth());
+
+ return ConstantRange(L, U);
+ }
+
+ if (!CR.isWrappedSet()) {
+ // ------U L----- and ------U L----- : this
+ // L--U L--U : CR
+ if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
+ return *this;
+
+ // ------U L----- : this
+ // L---------U : CR
+ if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
+ return ConstantRange(getBitWidth());
+
+ // ----U L---- : this
+ // L---U : CR
+ // <d1> <d2>
+ if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
+ APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
+ if (d1.ult(d2))
+ return ConstantRange(Lower, CR.Upper);
+ return ConstantRange(CR.Lower, Upper);
+ }
+
+ // ----U L----- : this
+ // L----U : CR
+ if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
+ return ConstantRange(CR.Lower, Upper);
+
+ // ------U L---- : this
+ // L-----U : CR
+ assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) &&
+ "ConstantRange::unionWith missed a case with one range wrapped");
+ return ConstantRange(Lower, CR.Upper);
+ }
+
+ // ------U L---- and ------U L---- : this
+ // -U L----------- and ------------U L : CR
+ if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
+ return ConstantRange(getBitWidth());
+
+ APInt L = Lower, U = Upper;
+ if (CR.Upper.ugt(U))
+ U = CR.Upper;
+ if (CR.Lower.ult(L))
+ L = CR.Lower;
+
+ return ConstantRange(L, U);
+}
+
+/// zeroExtend - Return a new range in the specified integer type, which must
+/// be strictly larger than the current type. The returned range will
+/// correspond to the possible range of values as if the source range had been
+/// zero extended.
+ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
+ if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
+
+ unsigned SrcTySize = getBitWidth();
+ assert(SrcTySize < DstTySize && "Not a value extension");
+ if (isFullSet() || isWrappedSet()) {
+ // Change into [0, 1 << src bit width)
+ APInt LowerExt(DstTySize, 0);
+ if (!Upper) // special case: [X, 0) -- not really wrapping around
+ LowerExt = Lower.zext(DstTySize);
+ return ConstantRange(LowerExt, APInt::getOneBitSet(DstTySize, SrcTySize));
+ }
+
+ return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
+}
+
+/// signExtend - Return a new range in the specified integer type, which must
+/// be strictly larger than the current type. The returned range will
+/// correspond to the possible range of values as if the source range had been
+/// sign extended.
+ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
+ if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
+
+ unsigned SrcTySize = getBitWidth();
+ assert(SrcTySize < DstTySize && "Not a value extension");
+
+ // special case: [X, INT_MIN) -- not really wrapping around
+ if (Upper.isMinSignedValue())
+ return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize));
+
+ if (isFullSet() || isSignWrappedSet()) {
+ return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
+ APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
+ }
+
+ return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
+}
+
+/// truncate - Return a new range in the specified integer type, which must be
+/// strictly smaller than the current type. The returned range will
+/// correspond to the possible range of values as if the source range had been
+/// truncated to the specified type.
+ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
+ assert(getBitWidth() > DstTySize && "Not a value truncation");
+ if (isEmptySet())
+ return ConstantRange(DstTySize, /*isFullSet=*/false);
+ if (isFullSet())
+ return ConstantRange(DstTySize, /*isFullSet=*/true);
+
+ APInt MaxValue = APInt::getMaxValue(DstTySize).zext(getBitWidth());
+ APInt MaxBitValue(getBitWidth(), 0);
+ MaxBitValue.setBit(DstTySize);
+
+ APInt LowerDiv(Lower), UpperDiv(Upper);
+ ConstantRange Union(DstTySize, /*isFullSet=*/false);
+
+ // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue]
+ // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and
+ // then we do the union with [MaxValue, Upper)
+ if (isWrappedSet()) {
+ // if Upper is greater than Max Value, it covers the whole truncated range.
+ if (Upper.uge(MaxValue))
+ return ConstantRange(DstTySize, /*isFullSet=*/true);
+
+ Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
+ UpperDiv = APInt::getMaxValue(getBitWidth());
+
+ // Union covers the MaxValue case, so return if the remaining range is just
+ // MaxValue.
+ if (LowerDiv == UpperDiv)
+ return Union;
+ }
+
+ // Chop off the most significant bits that are past the destination bitwidth.
+ if (LowerDiv.uge(MaxValue)) {
+ APInt Div(getBitWidth(), 0);
+ APInt::udivrem(LowerDiv, MaxBitValue, Div, LowerDiv);
+ UpperDiv = UpperDiv - MaxBitValue * Div;
+ }
+
+ if (UpperDiv.ule(MaxValue))
+ return ConstantRange(LowerDiv.trunc(DstTySize),
+ UpperDiv.trunc(DstTySize)).unionWith(Union);
+
+ // The truncated value wrapps around. Check if we can do better than fullset.
+ APInt UpperModulo = UpperDiv - MaxBitValue;
+ if (UpperModulo.ult(LowerDiv))
+ return ConstantRange(LowerDiv.trunc(DstTySize),
+ UpperModulo.trunc(DstTySize)).unionWith(Union);
+
+ return ConstantRange(DstTySize, /*isFullSet=*/true);
+}
+
+/// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
+/// value is zero extended, truncated, or left alone to make it that width.
+ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
+ unsigned SrcTySize = getBitWidth();
+ if (SrcTySize > DstTySize)
+ return truncate(DstTySize);
+ if (SrcTySize < DstTySize)
+ return zeroExtend(DstTySize);
+ return *this;
+}
+
+/// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
+/// value is sign extended, truncated, or left alone to make it that width.
+ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
+ unsigned SrcTySize = getBitWidth();
+ if (SrcTySize > DstTySize)
+ return truncate(DstTySize);
+ if (SrcTySize < DstTySize)
+ return signExtend(DstTySize);
+ return *this;
+}
+
+ConstantRange
+ConstantRange::add(const ConstantRange &Other) const {
+ if (isEmptySet() || Other.isEmptySet())
+ return ConstantRange(getBitWidth(), /*isFullSet=*/false);
+ if (isFullSet() || Other.isFullSet())
+ return ConstantRange(getBitWidth(), /*isFullSet=*/true);
+
+ APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
+ APInt NewLower = getLower() + Other.getLower();
+ APInt NewUpper = getUpper() + Other.getUpper() - 1;
+ if (NewLower == NewUpper)
+ return ConstantRange(getBitWidth(), /*isFullSet=*/true);
+
+ ConstantRange X = ConstantRange(NewLower, NewUpper);
+ if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
+ // We've wrapped, therefore, full set.
+ return ConstantRange(getBitWidth(), /*isFullSet=*/true);
+
+ return X;
+}
+
+ConstantRange
+ConstantRange::sub(const ConstantRange &Other) const {
+ if (isEmptySet() || Other.isEmptySet())
+ return ConstantRange(getBitWidth(), /*isFullSet=*/false);
+ if (isFullSet() || Other.isFullSet())
+ return ConstantRange(getBitWidth(), /*isFullSet=*/true);
+
+ APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
+ APInt NewLower = getLower() - Other.getUpper() + 1;
+ APInt NewUpper = getUpper() - Other.getLower();
+ if (NewLower == NewUpper)
+ return ConstantRange(getBitWidth(), /*isFullSet=*/true);
+
+ ConstantRange X = ConstantRange(NewLower, NewUpper);
+ if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
+ // We've wrapped, therefore, full set.
+ return ConstantRange(getBitWidth(), /*isFullSet=*/true);
+
+ return X;
+}
+
+ConstantRange
+ConstantRange::multiply(const ConstantRange &Other) const {
+ // TODO: If either operand is a single element and the multiply is known to
+ // be non-wrapping, round the result min and max value to the appropriate
+ // multiple of that element. If wrapping is possible, at least adjust the
+ // range according to the greatest power-of-two factor of the single element.
+
+ if (isEmptySet() || Other.isEmptySet())
+ return ConstantRange(getBitWidth(), /*isFullSet=*/false);
+
+ APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
+ APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
+ APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
+ APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
+
+ ConstantRange Result_zext = ConstantRange(this_min * Other_min,
+ this_max * Other_max + 1);
+ return Result_zext.truncate(getBitWidth());
+}
+
+ConstantRange
+ConstantRange::smax(const ConstantRange &Other) const {
+ // X smax Y is: range(smax(X_smin, Y_smin),
+ // smax(X_smax, Y_smax))
+ if (isEmptySet() || Other.isEmptySet())
+ return ConstantRange(getBitWidth(), /*isFullSet=*/false);
+ APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
+ APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
+ if (NewU == NewL)
+ return ConstantRange(getBitWidth(), /*isFullSet=*/true);
+ return ConstantRange(NewL, NewU);
+}
+
+ConstantRange
+ConstantRange::umax(const ConstantRange &Other) const {
+ // X umax Y is: range(umax(X_umin, Y_umin),
+ // umax(X_umax, Y_umax))
+ if (isEmptySet() || Other.isEmptySet())
+ return ConstantRange(getBitWidth(), /*isFullSet=*/false);
+ APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
+ APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
+ if (NewU == NewL)
+ return ConstantRange(getBitWidth(), /*isFullSet=*/true);
+ return ConstantRange(NewL, NewU);
+}
+
+ConstantRange
+ConstantRange::udiv(const ConstantRange &RHS) const {
+ if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
+ return ConstantRange(getBitWidth(), /*isFullSet=*/false);
+ if (RHS.isFullSet())
+ return ConstantRange(getBitWidth(), /*isFullSet=*/true);
+
+ APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
+
+ APInt RHS_umin = RHS.getUnsignedMin();
+ if (RHS_umin == 0) {
+ // We want the lowest value in RHS excluding zero. Usually that would be 1
+ // except for a range in the form of [X, 1) in which case it would be X.
+ if (RHS.getUpper() == 1)
+ RHS_umin = RHS.getLower();
+ else
+ RHS_umin = APInt(getBitWidth(), 1);
+ }
+
+ APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
+
+ // If the LHS is Full and the RHS is a wrapped interval containing 1 then
+ // this could occur.
+ if (Lower == Upper)
+ return ConstantRange(getBitWidth(), /*isFullSet=*/true);
+
+ return ConstantRange(Lower, Upper);
+}
+
+ConstantRange
+ConstantRange::binaryAnd(const ConstantRange &Other) const {
+ if (isEmptySet() || Other.isEmptySet())
+ return ConstantRange(getBitWidth(), /*isFullSet=*/false);
+
+ // TODO: replace this with something less conservative
+
+ APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax());
+ if (umin.isAllOnesValue())
+ return ConstantRange(getBitWidth(), /*isFullSet=*/true);
+ return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1);
+}
+
+ConstantRange
+ConstantRange::binaryOr(const ConstantRange &Other) const {
+ if (isEmptySet() || Other.isEmptySet())
+ return ConstantRange(getBitWidth(), /*isFullSet=*/false);
+
+ // TODO: replace this with something less conservative
+
+ APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
+ if (umax.isMinValue())
+ return ConstantRange(getBitWidth(), /*isFullSet=*/true);
+ return ConstantRange(umax, APInt::getNullValue(getBitWidth()));
+}
+
+ConstantRange
+ConstantRange::shl(const ConstantRange &Other) const {
+ if (isEmptySet() || Other.isEmptySet())
+ return ConstantRange(getBitWidth(), /*isFullSet=*/false);
+
+ APInt min = getUnsignedMin().shl(Other.getUnsignedMin());
+ APInt max = getUnsignedMax().shl(Other.getUnsignedMax());
+
+ // there's no overflow!
+ APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
+ if (Zeros.ugt(Other.getUnsignedMax()))
+ return ConstantRange(min, max + 1);
+
+ // FIXME: implement the other tricky cases
+ return ConstantRange(getBitWidth(), /*isFullSet=*/true);
+}
+
+ConstantRange
+ConstantRange::lshr(const ConstantRange &Other) const {
+ if (isEmptySet() || Other.isEmptySet())
+ return ConstantRange(getBitWidth(), /*isFullSet=*/false);
+
+ APInt max = getUnsignedMax().lshr(Other.getUnsignedMin());
+ APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
+ if (min == max + 1)
+ return ConstantRange(getBitWidth(), /*isFullSet=*/true);
+
+ return ConstantRange(min, max + 1);
+}
+
+ConstantRange ConstantRange::inverse() const {
+ if (isFullSet())
+ return ConstantRange(getBitWidth(), /*isFullSet=*/false);
+ if (isEmptySet())
+ return ConstantRange(getBitWidth(), /*isFullSet=*/true);
+ return ConstantRange(Upper, Lower);
+}
+
+/// print - Print out the bounds to a stream...
+///
+void ConstantRange::print(raw_ostream &OS) const {
+ if (isFullSet())
+ OS << "full-set";
+ else if (isEmptySet())
+ OS << "empty-set";
+ else
+ OS << "[" << Lower << "," << Upper << ")";
+}
+
+/// dump - Allow printing from a debugger easily...
+///
+void ConstantRange::dump() const {
+ print(dbgs());
+}
#include "llvm/IR/Instructions.h"
#include "LLVMContextImpl.h"
#include "llvm/IR/CallSite.h"
+#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
-#include "llvm/Support/ConstantRange.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
using namespace llvm;
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringMap.h"
+#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/ValueHandle.h"
-#include "llvm/Support/ConstantRange.h"
#include "llvm/Support/LeakDetector.h"
using namespace llvm;
#include "llvm/IR/CFG.h"
#include "llvm/IR/CallSite.h"
#include "llvm/IR/CallingConv.h"
+#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/InstVisitor.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
-#include "llvm/Support/ConstantRange.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
circular_raw_ostream.cpp
CommandLine.cpp
Compression.cpp
- ConstantRange.cpp
ConvertUTF.c
ConvertUTFWrapper.cpp
CrashRecoveryContext.cpp
+++ /dev/null
-//===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// 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
-// ranges (other integral ranges use min/max values for special range values):
-//
-// [F, F) = {} = Empty set
-// [T, F) = {T}
-// [F, T) = {F}
-// [T, T) = {F, T} = Full set
-//
-//===----------------------------------------------------------------------===//
-
-#include "llvm/IR/InstrTypes.h"
-#include "llvm/Support/ConstantRange.h"
-#include "llvm/Support/Debug.h"
-#include "llvm/Support/raw_ostream.h"
-using namespace llvm;
-
-/// Initialize a full (the default) or empty set for the specified type.
-///
-ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) {
- if (Full)
- Lower = Upper = APInt::getMaxValue(BitWidth);
- else
- Lower = Upper = APInt::getMinValue(BitWidth);
-}
-
-/// Initialize a range to hold the single specified value.
-///
-ConstantRange::ConstantRange(APIntMoveTy V)
- : Lower(std::move(V)), Upper(Lower + 1) {}
-
-ConstantRange::ConstantRange(APIntMoveTy L, APIntMoveTy U)
- : Lower(std::move(L)), Upper(std::move(U)) {
- assert(Lower.getBitWidth() == Upper.getBitWidth() &&
- "ConstantRange with unequal bit widths");
- assert((Lower != Upper || (Lower.isMaxValue() || Lower.isMinValue())) &&
- "Lower == Upper, but they aren't min or max value!");
-}
-
-ConstantRange ConstantRange::makeICmpRegion(unsigned Pred,
- const ConstantRange &CR) {
- if (CR.isEmptySet())
- return CR;
-
- uint32_t W = CR.getBitWidth();
- switch (Pred) {
- default: llvm_unreachable("Invalid ICmp predicate to makeICmpRegion()");
- case CmpInst::ICMP_EQ:
- return CR;
- case CmpInst::ICMP_NE:
- if (CR.isSingleElement())
- return ConstantRange(CR.getUpper(), CR.getLower());
- return ConstantRange(W);
- case CmpInst::ICMP_ULT: {
- APInt UMax(CR.getUnsignedMax());
- if (UMax.isMinValue())
- return ConstantRange(W, /* empty */ false);
- return ConstantRange(APInt::getMinValue(W), UMax);
- }
- case CmpInst::ICMP_SLT: {
- APInt SMax(CR.getSignedMax());
- if (SMax.isMinSignedValue())
- return ConstantRange(W, /* empty */ false);
- return ConstantRange(APInt::getSignedMinValue(W), SMax);
- }
- case CmpInst::ICMP_ULE: {
- APInt UMax(CR.getUnsignedMax());
- if (UMax.isMaxValue())
- return ConstantRange(W);
- return ConstantRange(APInt::getMinValue(W), UMax + 1);
- }
- case CmpInst::ICMP_SLE: {
- APInt SMax(CR.getSignedMax());
- if (SMax.isMaxSignedValue())
- return ConstantRange(W);
- return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
- }
- case CmpInst::ICMP_UGT: {
- APInt UMin(CR.getUnsignedMin());
- if (UMin.isMaxValue())
- return ConstantRange(W, /* empty */ false);
- return ConstantRange(UMin + 1, APInt::getNullValue(W));
- }
- case CmpInst::ICMP_SGT: {
- APInt SMin(CR.getSignedMin());
- if (SMin.isMaxSignedValue())
- return ConstantRange(W, /* empty */ false);
- return ConstantRange(SMin + 1, APInt::getSignedMinValue(W));
- }
- case CmpInst::ICMP_UGE: {
- APInt UMin(CR.getUnsignedMin());
- if (UMin.isMinValue())
- return ConstantRange(W);
- return ConstantRange(UMin, APInt::getNullValue(W));
- }
- case CmpInst::ICMP_SGE: {
- APInt SMin(CR.getSignedMin());
- if (SMin.isMinSignedValue())
- return ConstantRange(W);
- return ConstantRange(SMin, APInt::getSignedMinValue(W));
- }
- }
-}
-
-/// isFullSet - Return true if this set contains all of the elements possible
-/// for this data-type
-bool ConstantRange::isFullSet() const {
- return Lower == Upper && Lower.isMaxValue();
-}
-
-/// isEmptySet - Return true if this set contains no members.
-///
-bool ConstantRange::isEmptySet() const {
- return Lower == Upper && Lower.isMinValue();
-}
-
-/// isWrappedSet - Return true if this set wraps around the top of the range,
-/// for example: [100, 8)
-///
-bool ConstantRange::isWrappedSet() const {
- return Lower.ugt(Upper);
-}
-
-/// isSignWrappedSet - Return true if this set wraps around the INT_MIN of
-/// its bitwidth, for example: i8 [120, 140).
-///
-bool ConstantRange::isSignWrappedSet() const {
- return contains(APInt::getSignedMaxValue(getBitWidth())) &&
- contains(APInt::getSignedMinValue(getBitWidth()));
-}
-
-/// getSetSize - Return the number of elements in this set.
-///
-APInt ConstantRange::getSetSize() const {
- if (isFullSet()) {
- APInt Size(getBitWidth()+1, 0);
- Size.setBit(getBitWidth());
- return Size;
- }
-
- // This is also correct for wrapped sets.
- return (Upper - Lower).zext(getBitWidth()+1);
-}
-
-/// getUnsignedMax - Return the largest unsigned value contained in the
-/// ConstantRange.
-///
-APInt ConstantRange::getUnsignedMax() const {
- if (isFullSet() || isWrappedSet())
- return APInt::getMaxValue(getBitWidth());
- return getUpper() - 1;
-}
-
-/// getUnsignedMin - Return the smallest unsigned value contained in the
-/// ConstantRange.
-///
-APInt ConstantRange::getUnsignedMin() const {
- if (isFullSet() || (isWrappedSet() && getUpper() != 0))
- return APInt::getMinValue(getBitWidth());
- return getLower();
-}
-
-/// getSignedMax - Return the largest signed value contained in the
-/// ConstantRange.
-///
-APInt ConstantRange::getSignedMax() const {
- APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
- if (!isWrappedSet()) {
- if (getLower().sle(getUpper() - 1))
- return getUpper() - 1;
- return SignedMax;
- }
- if (getLower().isNegative() == getUpper().isNegative())
- return SignedMax;
- return getUpper() - 1;
-}
-
-/// getSignedMin - Return the smallest signed value contained in the
-/// ConstantRange.
-///
-APInt ConstantRange::getSignedMin() const {
- APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
- if (!isWrappedSet()) {
- if (getLower().sle(getUpper() - 1))
- return getLower();
- return SignedMin;
- }
- if ((getUpper() - 1).slt(getLower())) {
- if (getUpper() != SignedMin)
- return SignedMin;
- }
- return getLower();
-}
-
-/// contains - Return true if the specified value is in the set.
-///
-bool ConstantRange::contains(const APInt &V) const {
- if (Lower == Upper)
- return isFullSet();
-
- if (!isWrappedSet())
- return Lower.ule(V) && V.ult(Upper);
- return Lower.ule(V) || V.ult(Upper);
-}
-
-/// contains - Return true if the argument is a subset of this range.
-/// Two equal sets contain each other. The empty set contained by all other
-/// sets.
-///
-bool ConstantRange::contains(const ConstantRange &Other) const {
- if (isFullSet() || Other.isEmptySet()) return true;
- if (isEmptySet() || Other.isFullSet()) return false;
-
- if (!isWrappedSet()) {
- if (Other.isWrappedSet())
- return false;
-
- return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
- }
-
- if (!Other.isWrappedSet())
- return Other.getUpper().ule(Upper) ||
- Lower.ule(Other.getLower());
-
- return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
-}
-
-/// subtract - Subtract the specified constant from the endpoints of this
-/// constant range.
-ConstantRange ConstantRange::subtract(const APInt &Val) const {
- assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
- // If the set is empty or full, don't modify the endpoints.
- if (Lower == Upper)
- return *this;
- return ConstantRange(Lower - Val, Upper - Val);
-}
-
-/// \brief Subtract the specified range from this range (aka relative complement
-/// of the sets).
-ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
- return intersectWith(CR.inverse());
-}
-
-/// intersectWith - Return the range that results from the intersection of this
-/// range with another range. The resultant range is guaranteed to include all
-/// elements contained in both input ranges, and to have the smallest possible
-/// set size that does so. Because there may be two intersections with the
-/// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
-ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
- assert(getBitWidth() == CR.getBitWidth() &&
- "ConstantRange types don't agree!");
-
- // Handle common cases.
- if ( isEmptySet() || CR.isFullSet()) return *this;
- if (CR.isEmptySet() || isFullSet()) return CR;
-
- if (!isWrappedSet() && CR.isWrappedSet())
- return CR.intersectWith(*this);
-
- if (!isWrappedSet() && !CR.isWrappedSet()) {
- if (Lower.ult(CR.Lower)) {
- if (Upper.ule(CR.Lower))
- return ConstantRange(getBitWidth(), false);
-
- if (Upper.ult(CR.Upper))
- return ConstantRange(CR.Lower, Upper);
-
- return CR;
- }
- if (Upper.ult(CR.Upper))
- return *this;
-
- if (Lower.ult(CR.Upper))
- return ConstantRange(Lower, CR.Upper);
-
- return ConstantRange(getBitWidth(), false);
- }
-
- if (isWrappedSet() && !CR.isWrappedSet()) {
- if (CR.Lower.ult(Upper)) {
- if (CR.Upper.ult(Upper))
- return CR;
-
- if (CR.Upper.ule(Lower))
- return ConstantRange(CR.Lower, Upper);
-
- if (getSetSize().ult(CR.getSetSize()))
- return *this;
- return CR;
- }
- if (CR.Lower.ult(Lower)) {
- if (CR.Upper.ule(Lower))
- return ConstantRange(getBitWidth(), false);
-
- return ConstantRange(Lower, CR.Upper);
- }
- return CR;
- }
-
- if (CR.Upper.ult(Upper)) {
- if (CR.Lower.ult(Upper)) {
- if (getSetSize().ult(CR.getSetSize()))
- return *this;
- return CR;
- }
-
- if (CR.Lower.ult(Lower))
- return ConstantRange(Lower, CR.Upper);
-
- return CR;
- }
- if (CR.Upper.ule(Lower)) {
- if (CR.Lower.ult(Lower))
- return *this;
-
- return ConstantRange(CR.Lower, Upper);
- }
- if (getSetSize().ult(CR.getSetSize()))
- return *this;
- return CR;
-}
-
-
-/// unionWith - Return the range that results from the union of this range 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.
-///
-ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
- assert(getBitWidth() == CR.getBitWidth() &&
- "ConstantRange types don't agree!");
-
- if ( isFullSet() || CR.isEmptySet()) return *this;
- if (CR.isFullSet() || isEmptySet()) return CR;
-
- if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
-
- if (!isWrappedSet() && !CR.isWrappedSet()) {
- if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
- // If the two ranges are disjoint, find the smaller gap and bridge it.
- APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
- if (d1.ult(d2))
- return ConstantRange(Lower, CR.Upper);
- return ConstantRange(CR.Lower, Upper);
- }
-
- APInt L = Lower, U = Upper;
- if (CR.Lower.ult(L))
- L = CR.Lower;
- if ((CR.Upper - 1).ugt(U - 1))
- U = CR.Upper;
-
- if (L == 0 && U == 0)
- return ConstantRange(getBitWidth());
-
- return ConstantRange(L, U);
- }
-
- if (!CR.isWrappedSet()) {
- // ------U L----- and ------U L----- : this
- // L--U L--U : CR
- if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
- return *this;
-
- // ------U L----- : this
- // L---------U : CR
- if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
- return ConstantRange(getBitWidth());
-
- // ----U L---- : this
- // L---U : CR
- // <d1> <d2>
- if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
- APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
- if (d1.ult(d2))
- return ConstantRange(Lower, CR.Upper);
- return ConstantRange(CR.Lower, Upper);
- }
-
- // ----U L----- : this
- // L----U : CR
- if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
- return ConstantRange(CR.Lower, Upper);
-
- // ------U L---- : this
- // L-----U : CR
- assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) &&
- "ConstantRange::unionWith missed a case with one range wrapped");
- return ConstantRange(Lower, CR.Upper);
- }
-
- // ------U L---- and ------U L---- : this
- // -U L----------- and ------------U L : CR
- if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
- return ConstantRange(getBitWidth());
-
- APInt L = Lower, U = Upper;
- if (CR.Upper.ugt(U))
- U = CR.Upper;
- if (CR.Lower.ult(L))
- L = CR.Lower;
-
- return ConstantRange(L, U);
-}
-
-/// zeroExtend - Return a new range in the specified integer type, which must
-/// be strictly larger than the current type. The returned range will
-/// correspond to the possible range of values as if the source range had been
-/// zero extended.
-ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
- if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
-
- unsigned SrcTySize = getBitWidth();
- assert(SrcTySize < DstTySize && "Not a value extension");
- if (isFullSet() || isWrappedSet()) {
- // Change into [0, 1 << src bit width)
- APInt LowerExt(DstTySize, 0);
- if (!Upper) // special case: [X, 0) -- not really wrapping around
- LowerExt = Lower.zext(DstTySize);
- return ConstantRange(LowerExt, APInt::getOneBitSet(DstTySize, SrcTySize));
- }
-
- return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
-}
-
-/// signExtend - Return a new range in the specified integer type, which must
-/// be strictly larger than the current type. The returned range will
-/// correspond to the possible range of values as if the source range had been
-/// sign extended.
-ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
- if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
-
- unsigned SrcTySize = getBitWidth();
- assert(SrcTySize < DstTySize && "Not a value extension");
-
- // special case: [X, INT_MIN) -- not really wrapping around
- if (Upper.isMinSignedValue())
- return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize));
-
- if (isFullSet() || isSignWrappedSet()) {
- return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
- APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
- }
-
- return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
-}
-
-/// truncate - Return a new range in the specified integer type, which must be
-/// strictly smaller than the current type. The returned range will
-/// correspond to the possible range of values as if the source range had been
-/// truncated to the specified type.
-ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
- assert(getBitWidth() > DstTySize && "Not a value truncation");
- if (isEmptySet())
- return ConstantRange(DstTySize, /*isFullSet=*/false);
- if (isFullSet())
- return ConstantRange(DstTySize, /*isFullSet=*/true);
-
- APInt MaxValue = APInt::getMaxValue(DstTySize).zext(getBitWidth());
- APInt MaxBitValue(getBitWidth(), 0);
- MaxBitValue.setBit(DstTySize);
-
- APInt LowerDiv(Lower), UpperDiv(Upper);
- ConstantRange Union(DstTySize, /*isFullSet=*/false);
-
- // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue]
- // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and
- // then we do the union with [MaxValue, Upper)
- if (isWrappedSet()) {
- // if Upper is greater than Max Value, it covers the whole truncated range.
- if (Upper.uge(MaxValue))
- return ConstantRange(DstTySize, /*isFullSet=*/true);
-
- Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
- UpperDiv = APInt::getMaxValue(getBitWidth());
-
- // Union covers the MaxValue case, so return if the remaining range is just
- // MaxValue.
- if (LowerDiv == UpperDiv)
- return Union;
- }
-
- // Chop off the most significant bits that are past the destination bitwidth.
- if (LowerDiv.uge(MaxValue)) {
- APInt Div(getBitWidth(), 0);
- APInt::udivrem(LowerDiv, MaxBitValue, Div, LowerDiv);
- UpperDiv = UpperDiv - MaxBitValue * Div;
- }
-
- if (UpperDiv.ule(MaxValue))
- return ConstantRange(LowerDiv.trunc(DstTySize),
- UpperDiv.trunc(DstTySize)).unionWith(Union);
-
- // The truncated value wrapps around. Check if we can do better than fullset.
- APInt UpperModulo = UpperDiv - MaxBitValue;
- if (UpperModulo.ult(LowerDiv))
- return ConstantRange(LowerDiv.trunc(DstTySize),
- UpperModulo.trunc(DstTySize)).unionWith(Union);
-
- return ConstantRange(DstTySize, /*isFullSet=*/true);
-}
-
-/// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
-/// value is zero extended, truncated, or left alone to make it that width.
-ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
- unsigned SrcTySize = getBitWidth();
- if (SrcTySize > DstTySize)
- return truncate(DstTySize);
- if (SrcTySize < DstTySize)
- return zeroExtend(DstTySize);
- return *this;
-}
-
-/// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
-/// value is sign extended, truncated, or left alone to make it that width.
-ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
- unsigned SrcTySize = getBitWidth();
- if (SrcTySize > DstTySize)
- return truncate(DstTySize);
- if (SrcTySize < DstTySize)
- return signExtend(DstTySize);
- return *this;
-}
-
-ConstantRange
-ConstantRange::add(const ConstantRange &Other) const {
- if (isEmptySet() || Other.isEmptySet())
- return ConstantRange(getBitWidth(), /*isFullSet=*/false);
- if (isFullSet() || Other.isFullSet())
- return ConstantRange(getBitWidth(), /*isFullSet=*/true);
-
- APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
- APInt NewLower = getLower() + Other.getLower();
- APInt NewUpper = getUpper() + Other.getUpper() - 1;
- if (NewLower == NewUpper)
- return ConstantRange(getBitWidth(), /*isFullSet=*/true);
-
- ConstantRange X = ConstantRange(NewLower, NewUpper);
- if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
- // We've wrapped, therefore, full set.
- return ConstantRange(getBitWidth(), /*isFullSet=*/true);
-
- return X;
-}
-
-ConstantRange
-ConstantRange::sub(const ConstantRange &Other) const {
- if (isEmptySet() || Other.isEmptySet())
- return ConstantRange(getBitWidth(), /*isFullSet=*/false);
- if (isFullSet() || Other.isFullSet())
- return ConstantRange(getBitWidth(), /*isFullSet=*/true);
-
- APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
- APInt NewLower = getLower() - Other.getUpper() + 1;
- APInt NewUpper = getUpper() - Other.getLower();
- if (NewLower == NewUpper)
- return ConstantRange(getBitWidth(), /*isFullSet=*/true);
-
- ConstantRange X = ConstantRange(NewLower, NewUpper);
- if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
- // We've wrapped, therefore, full set.
- return ConstantRange(getBitWidth(), /*isFullSet=*/true);
-
- return X;
-}
-
-ConstantRange
-ConstantRange::multiply(const ConstantRange &Other) const {
- // TODO: If either operand is a single element and the multiply is known to
- // be non-wrapping, round the result min and max value to the appropriate
- // multiple of that element. If wrapping is possible, at least adjust the
- // range according to the greatest power-of-two factor of the single element.
-
- if (isEmptySet() || Other.isEmptySet())
- return ConstantRange(getBitWidth(), /*isFullSet=*/false);
-
- APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
- APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
- APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
- APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
-
- ConstantRange Result_zext = ConstantRange(this_min * Other_min,
- this_max * Other_max + 1);
- return Result_zext.truncate(getBitWidth());
-}
-
-ConstantRange
-ConstantRange::smax(const ConstantRange &Other) const {
- // X smax Y is: range(smax(X_smin, Y_smin),
- // smax(X_smax, Y_smax))
- if (isEmptySet() || Other.isEmptySet())
- return ConstantRange(getBitWidth(), /*isFullSet=*/false);
- APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
- APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
- if (NewU == NewL)
- return ConstantRange(getBitWidth(), /*isFullSet=*/true);
- return ConstantRange(NewL, NewU);
-}
-
-ConstantRange
-ConstantRange::umax(const ConstantRange &Other) const {
- // X umax Y is: range(umax(X_umin, Y_umin),
- // umax(X_umax, Y_umax))
- if (isEmptySet() || Other.isEmptySet())
- return ConstantRange(getBitWidth(), /*isFullSet=*/false);
- APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
- APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
- if (NewU == NewL)
- return ConstantRange(getBitWidth(), /*isFullSet=*/true);
- return ConstantRange(NewL, NewU);
-}
-
-ConstantRange
-ConstantRange::udiv(const ConstantRange &RHS) const {
- if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
- return ConstantRange(getBitWidth(), /*isFullSet=*/false);
- if (RHS.isFullSet())
- return ConstantRange(getBitWidth(), /*isFullSet=*/true);
-
- APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
-
- APInt RHS_umin = RHS.getUnsignedMin();
- if (RHS_umin == 0) {
- // We want the lowest value in RHS excluding zero. Usually that would be 1
- // except for a range in the form of [X, 1) in which case it would be X.
- if (RHS.getUpper() == 1)
- RHS_umin = RHS.getLower();
- else
- RHS_umin = APInt(getBitWidth(), 1);
- }
-
- APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
-
- // If the LHS is Full and the RHS is a wrapped interval containing 1 then
- // this could occur.
- if (Lower == Upper)
- return ConstantRange(getBitWidth(), /*isFullSet=*/true);
-
- return ConstantRange(Lower, Upper);
-}
-
-ConstantRange
-ConstantRange::binaryAnd(const ConstantRange &Other) const {
- if (isEmptySet() || Other.isEmptySet())
- return ConstantRange(getBitWidth(), /*isFullSet=*/false);
-
- // TODO: replace this with something less conservative
-
- APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax());
- if (umin.isAllOnesValue())
- return ConstantRange(getBitWidth(), /*isFullSet=*/true);
- return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1);
-}
-
-ConstantRange
-ConstantRange::binaryOr(const ConstantRange &Other) const {
- if (isEmptySet() || Other.isEmptySet())
- return ConstantRange(getBitWidth(), /*isFullSet=*/false);
-
- // TODO: replace this with something less conservative
-
- APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
- if (umax.isMinValue())
- return ConstantRange(getBitWidth(), /*isFullSet=*/true);
- return ConstantRange(umax, APInt::getNullValue(getBitWidth()));
-}
-
-ConstantRange
-ConstantRange::shl(const ConstantRange &Other) const {
- if (isEmptySet() || Other.isEmptySet())
- return ConstantRange(getBitWidth(), /*isFullSet=*/false);
-
- APInt min = getUnsignedMin().shl(Other.getUnsignedMin());
- APInt max = getUnsignedMax().shl(Other.getUnsignedMax());
-
- // there's no overflow!
- APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
- if (Zeros.ugt(Other.getUnsignedMax()))
- return ConstantRange(min, max + 1);
-
- // FIXME: implement the other tricky cases
- return ConstantRange(getBitWidth(), /*isFullSet=*/true);
-}
-
-ConstantRange
-ConstantRange::lshr(const ConstantRange &Other) const {
- if (isEmptySet() || Other.isEmptySet())
- return ConstantRange(getBitWidth(), /*isFullSet=*/false);
-
- APInt max = getUnsignedMax().lshr(Other.getUnsignedMin());
- APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
- if (min == max + 1)
- return ConstantRange(getBitWidth(), /*isFullSet=*/true);
-
- return ConstantRange(min, max + 1);
-}
-
-ConstantRange ConstantRange::inverse() const {
- if (isFullSet())
- return ConstantRange(getBitWidth(), /*isFullSet=*/false);
- if (isEmptySet())
- return ConstantRange(getBitWidth(), /*isFullSet=*/true);
- return ConstantRange(Upper, Lower);
-}
-
-/// print - Print out the bounds to a stream...
-///
-void ConstantRange::print(raw_ostream &OS) const {
- if (isFullSet())
- OS << "full-set";
- else if (isEmptySet())
- OS << "empty-set";
- else
- OS << "[" << Lower << "," << Upper << ")";
-}
-
-/// dump - Allow printing from a debugger easily...
-///
-void ConstantRange::dump() const {
- print(dbgs());
-}
#include "InstCombine.h"
#include "llvm/Analysis/InstructionSimplify.h"
+#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/PatternMatch.h"
-#include "llvm/Support/ConstantRange.h"
#include "llvm/Transforms/Utils/CmpInstAnalysis.h"
using namespace llvm;
using namespace PatternMatch;
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/MemoryBuiltins.h"
+#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/GetElementPtrTypeIterator.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/PatternMatch.h"
-#include "llvm/Support/ConstantRange.h"
#include "llvm/Target/TargetLibraryInfo.h"
using namespace llvm;
using namespace PatternMatch;
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/CFG.h"
+#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/CommandLine.h"
-#include "llvm/Support/ConstantRange.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
set(IRSources
AttributesTest.cpp
+ ConstantRangeTest.cpp
ConstantsTest.cpp
DominatorTreeTest.cpp
IRBuilderTest.cpp
--- /dev/null
+//===- ConstantRangeTest.cpp - ConstantRange tests ------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/ConstantRange.h"
+#include "llvm/IR/Instructions.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+
+class ConstantRangeTest : public ::testing::Test {
+protected:
+ static ConstantRange Full;
+ static ConstantRange Empty;
+ static ConstantRange One;
+ static ConstantRange Some;
+ static ConstantRange Wrap;
+};
+
+ConstantRange ConstantRangeTest::Full(16);
+ConstantRange ConstantRangeTest::Empty(16, false);
+ConstantRange ConstantRangeTest::One(APInt(16, 0xa));
+ConstantRange ConstantRangeTest::Some(APInt(16, 0xa), APInt(16, 0xaaa));
+ConstantRange ConstantRangeTest::Wrap(APInt(16, 0xaaa), APInt(16, 0xa));
+
+TEST_F(ConstantRangeTest, Basics) {
+ EXPECT_TRUE(Full.isFullSet());
+ EXPECT_FALSE(Full.isEmptySet());
+ EXPECT_TRUE(Full.inverse().isEmptySet());
+ EXPECT_FALSE(Full.isWrappedSet());
+ EXPECT_TRUE(Full.contains(APInt(16, 0x0)));
+ EXPECT_TRUE(Full.contains(APInt(16, 0x9)));
+ EXPECT_TRUE(Full.contains(APInt(16, 0xa)));
+ EXPECT_TRUE(Full.contains(APInt(16, 0xaa9)));
+ EXPECT_TRUE(Full.contains(APInt(16, 0xaaa)));
+
+ EXPECT_FALSE(Empty.isFullSet());
+ EXPECT_TRUE(Empty.isEmptySet());
+ EXPECT_TRUE(Empty.inverse().isFullSet());
+ EXPECT_FALSE(Empty.isWrappedSet());
+ EXPECT_FALSE(Empty.contains(APInt(16, 0x0)));
+ EXPECT_FALSE(Empty.contains(APInt(16, 0x9)));
+ EXPECT_FALSE(Empty.contains(APInt(16, 0xa)));
+ EXPECT_FALSE(Empty.contains(APInt(16, 0xaa9)));
+ EXPECT_FALSE(Empty.contains(APInt(16, 0xaaa)));
+
+ EXPECT_FALSE(One.isFullSet());
+ EXPECT_FALSE(One.isEmptySet());
+ EXPECT_FALSE(One.isWrappedSet());
+ EXPECT_FALSE(One.contains(APInt(16, 0x0)));
+ EXPECT_FALSE(One.contains(APInt(16, 0x9)));
+ EXPECT_TRUE(One.contains(APInt(16, 0xa)));
+ EXPECT_FALSE(One.contains(APInt(16, 0xaa9)));
+ EXPECT_FALSE(One.contains(APInt(16, 0xaaa)));
+ EXPECT_FALSE(One.inverse().contains(APInt(16, 0xa)));
+
+ EXPECT_FALSE(Some.isFullSet());
+ EXPECT_FALSE(Some.isEmptySet());
+ EXPECT_FALSE(Some.isWrappedSet());
+ EXPECT_FALSE(Some.contains(APInt(16, 0x0)));
+ EXPECT_FALSE(Some.contains(APInt(16, 0x9)));
+ EXPECT_TRUE(Some.contains(APInt(16, 0xa)));
+ EXPECT_TRUE(Some.contains(APInt(16, 0xaa9)));
+ EXPECT_FALSE(Some.contains(APInt(16, 0xaaa)));
+
+ EXPECT_FALSE(Wrap.isFullSet());
+ EXPECT_FALSE(Wrap.isEmptySet());
+ EXPECT_TRUE(Wrap.isWrappedSet());
+ EXPECT_TRUE(Wrap.contains(APInt(16, 0x0)));
+ EXPECT_TRUE(Wrap.contains(APInt(16, 0x9)));
+ EXPECT_FALSE(Wrap.contains(APInt(16, 0xa)));
+ EXPECT_FALSE(Wrap.contains(APInt(16, 0xaa9)));
+ EXPECT_TRUE(Wrap.contains(APInt(16, 0xaaa)));
+}
+
+TEST_F(ConstantRangeTest, Equality) {
+ EXPECT_EQ(Full, Full);
+ EXPECT_EQ(Empty, Empty);
+ EXPECT_EQ(One, One);
+ EXPECT_EQ(Some, Some);
+ EXPECT_EQ(Wrap, Wrap);
+ EXPECT_NE(Full, Empty);
+ EXPECT_NE(Full, One);
+ EXPECT_NE(Full, Some);
+ EXPECT_NE(Full, Wrap);
+ EXPECT_NE(Empty, One);
+ EXPECT_NE(Empty, Some);
+ EXPECT_NE(Empty, Wrap);
+ EXPECT_NE(One, Some);
+ EXPECT_NE(One, Wrap);
+ EXPECT_NE(Some, Wrap);
+}
+
+TEST_F(ConstantRangeTest, SingleElement) {
+ EXPECT_EQ(Full.getSingleElement(), static_cast<APInt *>(NULL));
+ EXPECT_EQ(Empty.getSingleElement(), static_cast<APInt *>(NULL));
+ EXPECT_EQ(*One.getSingleElement(), APInt(16, 0xa));
+ EXPECT_EQ(Some.getSingleElement(), static_cast<APInt *>(NULL));
+ EXPECT_EQ(Wrap.getSingleElement(), static_cast<APInt *>(NULL));
+
+ EXPECT_FALSE(Full.isSingleElement());
+ EXPECT_FALSE(Empty.isSingleElement());
+ EXPECT_TRUE(One.isSingleElement());
+ EXPECT_FALSE(Some.isSingleElement());
+ EXPECT_FALSE(Wrap.isSingleElement());
+}
+
+TEST_F(ConstantRangeTest, GetSetSize) {
+ EXPECT_EQ(Full.getSetSize(), APInt(17, 65536));
+ EXPECT_EQ(Empty.getSetSize(), APInt(17, 0));
+ EXPECT_EQ(One.getSetSize(), APInt(17, 1));
+ EXPECT_EQ(Some.getSetSize(), APInt(17, 0xaa0));
+
+ ConstantRange Wrap(APInt(4, 7), APInt(4, 3));
+ ConstantRange Wrap2(APInt(4, 8), APInt(4, 7));
+ EXPECT_EQ(Wrap.getSetSize(), APInt(5, 12));
+ EXPECT_EQ(Wrap2.getSetSize(), APInt(5, 15));
+}
+
+TEST_F(ConstantRangeTest, GetMinsAndMaxes) {
+ EXPECT_EQ(Full.getUnsignedMax(), APInt(16, UINT16_MAX));
+ EXPECT_EQ(One.getUnsignedMax(), APInt(16, 0xa));
+ EXPECT_EQ(Some.getUnsignedMax(), APInt(16, 0xaa9));
+ EXPECT_EQ(Wrap.getUnsignedMax(), APInt(16, UINT16_MAX));
+
+ EXPECT_EQ(Full.getUnsignedMin(), APInt(16, 0));
+ EXPECT_EQ(One.getUnsignedMin(), APInt(16, 0xa));
+ EXPECT_EQ(Some.getUnsignedMin(), APInt(16, 0xa));
+ EXPECT_EQ(Wrap.getUnsignedMin(), APInt(16, 0));
+
+ EXPECT_EQ(Full.getSignedMax(), APInt(16, INT16_MAX));
+ EXPECT_EQ(One.getSignedMax(), APInt(16, 0xa));
+ EXPECT_EQ(Some.getSignedMax(), APInt(16, 0xaa9));
+ EXPECT_EQ(Wrap.getSignedMax(), APInt(16, INT16_MAX));
+
+ EXPECT_EQ(Full.getSignedMin(), APInt(16, (uint64_t)INT16_MIN));
+ EXPECT_EQ(One.getSignedMin(), APInt(16, 0xa));
+ EXPECT_EQ(Some.getSignedMin(), APInt(16, 0xa));
+ EXPECT_EQ(Wrap.getSignedMin(), APInt(16, (uint64_t)INT16_MIN));
+
+ // Found by Klee
+ EXPECT_EQ(ConstantRange(APInt(4, 7), APInt(4, 0)).getSignedMax(),
+ APInt(4, 7));
+}
+
+TEST_F(ConstantRangeTest, SignWrapped) {
+ EXPECT_TRUE(Full.isSignWrappedSet());
+ EXPECT_FALSE(Empty.isSignWrappedSet());
+ EXPECT_FALSE(One.isSignWrappedSet());
+ EXPECT_FALSE(Some.isSignWrappedSet());
+ EXPECT_TRUE(Wrap.isSignWrappedSet());
+
+ EXPECT_FALSE(ConstantRange(APInt(8, 127), APInt(8, 128)).isSignWrappedSet());
+ EXPECT_TRUE(ConstantRange(APInt(8, 127), APInt(8, 129)).isSignWrappedSet());
+ EXPECT_FALSE(ConstantRange(APInt(8, 128), APInt(8, 129)).isSignWrappedSet());
+ EXPECT_TRUE(ConstantRange(APInt(8, 10), APInt(8, 9)).isSignWrappedSet());
+ EXPECT_TRUE(ConstantRange(APInt(8, 10), APInt(8, 250)).isSignWrappedSet());
+ EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 10)).isSignWrappedSet());
+ EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 251)).isSignWrappedSet());
+}
+
+TEST_F(ConstantRangeTest, Trunc) {
+ ConstantRange TFull = Full.truncate(10);
+ ConstantRange TEmpty = Empty.truncate(10);
+ ConstantRange TOne = One.truncate(10);
+ ConstantRange TSome = Some.truncate(10);
+ ConstantRange TWrap = Wrap.truncate(10);
+ EXPECT_TRUE(TFull.isFullSet());
+ EXPECT_TRUE(TEmpty.isEmptySet());
+ EXPECT_EQ(TOne, ConstantRange(One.getLower().trunc(10),
+ One.getUpper().trunc(10)));
+ EXPECT_TRUE(TSome.isFullSet());
+}
+
+TEST_F(ConstantRangeTest, ZExt) {
+ ConstantRange ZFull = Full.zeroExtend(20);
+ ConstantRange ZEmpty = Empty.zeroExtend(20);
+ ConstantRange ZOne = One.zeroExtend(20);
+ ConstantRange ZSome = Some.zeroExtend(20);
+ ConstantRange ZWrap = Wrap.zeroExtend(20);
+ EXPECT_EQ(ZFull, ConstantRange(APInt(20, 0), APInt(20, 0x10000)));
+ EXPECT_TRUE(ZEmpty.isEmptySet());
+ EXPECT_EQ(ZOne, ConstantRange(One.getLower().zext(20),
+ One.getUpper().zext(20)));
+ EXPECT_EQ(ZSome, ConstantRange(Some.getLower().zext(20),
+ Some.getUpper().zext(20)));
+ EXPECT_EQ(ZWrap, ConstantRange(APInt(20, 0), APInt(20, 0x10000)));
+
+ // zext([5, 0), 3->7) = [5, 8)
+ ConstantRange FiveZero(APInt(3, 5), APInt(3, 0));
+ EXPECT_EQ(FiveZero.zeroExtend(7), ConstantRange(APInt(7, 5), APInt(7, 8)));
+}
+
+TEST_F(ConstantRangeTest, SExt) {
+ ConstantRange SFull = Full.signExtend(20);
+ ConstantRange SEmpty = Empty.signExtend(20);
+ ConstantRange SOne = One.signExtend(20);
+ ConstantRange SSome = Some.signExtend(20);
+ ConstantRange SWrap = Wrap.signExtend(20);
+ EXPECT_EQ(SFull, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true),
+ APInt(20, INT16_MAX + 1, true)));
+ EXPECT_TRUE(SEmpty.isEmptySet());
+ EXPECT_EQ(SOne, ConstantRange(One.getLower().sext(20),
+ One.getUpper().sext(20)));
+ EXPECT_EQ(SSome, ConstantRange(Some.getLower().sext(20),
+ Some.getUpper().sext(20)));
+ EXPECT_EQ(SWrap, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true),
+ APInt(20, INT16_MAX + 1, true)));
+
+ EXPECT_EQ(ConstantRange(APInt(8, 120), APInt(8, 140)).signExtend(16),
+ ConstantRange(APInt(16, -128), APInt(16, 128)));
+
+ EXPECT_EQ(ConstantRange(APInt(16, 0x0200), APInt(16, 0x8000)).signExtend(19),
+ ConstantRange(APInt(19, 0x0200), APInt(19, 0x8000)));
+}
+
+TEST_F(ConstantRangeTest, IntersectWith) {
+ EXPECT_EQ(Empty.intersectWith(Full), Empty);
+ EXPECT_EQ(Empty.intersectWith(Empty), Empty);
+ EXPECT_EQ(Empty.intersectWith(One), Empty);
+ EXPECT_EQ(Empty.intersectWith(Some), Empty);
+ EXPECT_EQ(Empty.intersectWith(Wrap), Empty);
+ EXPECT_EQ(Full.intersectWith(Full), Full);
+ EXPECT_EQ(Some.intersectWith(Some), Some);
+ EXPECT_EQ(Some.intersectWith(One), One);
+ EXPECT_EQ(Full.intersectWith(One), One);
+ EXPECT_EQ(Full.intersectWith(Some), Some);
+ EXPECT_EQ(Some.intersectWith(Wrap), Empty);
+ EXPECT_EQ(One.intersectWith(Wrap), Empty);
+ EXPECT_EQ(One.intersectWith(Wrap), Wrap.intersectWith(One));
+
+ // Klee generated testcase from PR4545.
+ // The intersection of i16 [4, 2) and [6, 5) is disjoint, looking like
+ // 01..4.6789ABCDEF where the dots represent values not in the intersection.
+ ConstantRange LHS(APInt(16, 4), APInt(16, 2));
+ ConstantRange RHS(APInt(16, 6), APInt(16, 5));
+ EXPECT_TRUE(LHS.intersectWith(RHS) == LHS);
+
+ // previous bug: intersection of [min, 3) and [2, max) should be 2
+ LHS = ConstantRange(APInt(32, -2147483646), APInt(32, 3));
+ RHS = ConstantRange(APInt(32, 2), APInt(32, 2147483646));
+ EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 2)));
+
+ // [2, 0) /\ [4, 3) = [2, 0)
+ LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
+ RHS = ConstantRange(APInt(32, 4), APInt(32, 3));
+ EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 2), APInt(32, 0)));
+
+ // [2, 0) /\ [4, 2) = [4, 0)
+ LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
+ RHS = ConstantRange(APInt(32, 4), APInt(32, 2));
+ EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 4), APInt(32, 0)));
+
+ // [4, 2) /\ [5, 1) = [5, 1)
+ LHS = ConstantRange(APInt(32, 4), APInt(32, 2));
+ RHS = ConstantRange(APInt(32, 5), APInt(32, 1));
+ EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 5), APInt(32, 1)));
+
+ // [2, 0) /\ [7, 4) = [7, 4)
+ LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
+ RHS = ConstantRange(APInt(32, 7), APInt(32, 4));
+ EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 7), APInt(32, 4)));
+
+ // [4, 2) /\ [1, 0) = [1, 0)
+ LHS = ConstantRange(APInt(32, 4), APInt(32, 2));
+ RHS = ConstantRange(APInt(32, 1), APInt(32, 0));
+ EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 4), APInt(32, 2)));
+
+ // [15, 0) /\ [7, 6) = [15, 0)
+ LHS = ConstantRange(APInt(32, 15), APInt(32, 0));
+ RHS = ConstantRange(APInt(32, 7), APInt(32, 6));
+ EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 15), APInt(32, 0)));
+}
+
+TEST_F(ConstantRangeTest, UnionWith) {
+ EXPECT_EQ(Wrap.unionWith(One),
+ ConstantRange(APInt(16, 0xaaa), APInt(16, 0xb)));
+ EXPECT_EQ(One.unionWith(Wrap), Wrap.unionWith(One));
+ EXPECT_EQ(Empty.unionWith(Empty), Empty);
+ EXPECT_EQ(Full.unionWith(Full), Full);
+ EXPECT_EQ(Some.unionWith(Wrap), Full);
+
+ // PR4545
+ EXPECT_EQ(ConstantRange(APInt(16, 14), APInt(16, 1)).unionWith(
+ ConstantRange(APInt(16, 0), APInt(16, 8))),
+ ConstantRange(APInt(16, 14), APInt(16, 8)));
+ EXPECT_EQ(ConstantRange(APInt(16, 6), APInt(16, 4)).unionWith(
+ ConstantRange(APInt(16, 4), APInt(16, 0))),
+ ConstantRange(16));
+ EXPECT_EQ(ConstantRange(APInt(16, 1), APInt(16, 0)).unionWith(
+ ConstantRange(APInt(16, 2), APInt(16, 1))),
+ ConstantRange(16));
+}
+
+TEST_F(ConstantRangeTest, SetDifference) {
+ EXPECT_EQ(Full.difference(Empty), Full);
+ EXPECT_EQ(Full.difference(Full), Empty);
+ EXPECT_EQ(Empty.difference(Empty), Empty);
+ EXPECT_EQ(Empty.difference(Full), Empty);
+
+ ConstantRange A(APInt(16, 3), APInt(16, 7));
+ ConstantRange B(APInt(16, 5), APInt(16, 9));
+ ConstantRange C(APInt(16, 3), APInt(16, 5));
+ ConstantRange D(APInt(16, 7), APInt(16, 9));
+ ConstantRange E(APInt(16, 5), APInt(16, 4));
+ ConstantRange F(APInt(16, 7), APInt(16, 3));
+ EXPECT_EQ(A.difference(B), C);
+ EXPECT_EQ(B.difference(A), D);
+ EXPECT_EQ(E.difference(A), F);
+}
+
+TEST_F(ConstantRangeTest, SubtractAPInt) {
+ EXPECT_EQ(Full.subtract(APInt(16, 4)), Full);
+ EXPECT_EQ(Empty.subtract(APInt(16, 4)), Empty);
+ EXPECT_EQ(Some.subtract(APInt(16, 4)),
+ ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6)));
+ EXPECT_EQ(Wrap.subtract(APInt(16, 4)),
+ ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6)));
+ EXPECT_EQ(One.subtract(APInt(16, 4)),
+ ConstantRange(APInt(16, 0x6)));
+}
+
+TEST_F(ConstantRangeTest, Add) {
+ EXPECT_EQ(Full.add(APInt(16, 4)), Full);
+ EXPECT_EQ(Full.add(Full), Full);
+ EXPECT_EQ(Full.add(Empty), Empty);
+ EXPECT_EQ(Full.add(One), Full);
+ EXPECT_EQ(Full.add(Some), Full);
+ EXPECT_EQ(Full.add(Wrap), Full);
+ EXPECT_EQ(Empty.add(Empty), Empty);
+ EXPECT_EQ(Empty.add(One), Empty);
+ EXPECT_EQ(Empty.add(Some), Empty);
+ EXPECT_EQ(Empty.add(Wrap), Empty);
+ EXPECT_EQ(Empty.add(APInt(16, 4)), Empty);
+ EXPECT_EQ(Some.add(APInt(16, 4)),
+ ConstantRange(APInt(16, 0xe), APInt(16, 0xaae)));
+ EXPECT_EQ(Wrap.add(APInt(16, 4)),
+ ConstantRange(APInt(16, 0xaae), APInt(16, 0xe)));
+ EXPECT_EQ(One.add(APInt(16, 4)),
+ ConstantRange(APInt(16, 0xe)));
+}
+
+TEST_F(ConstantRangeTest, Sub) {
+ EXPECT_EQ(Full.sub(APInt(16, 4)), Full);
+ EXPECT_EQ(Full.sub(Full), Full);
+ EXPECT_EQ(Full.sub(Empty), Empty);
+ EXPECT_EQ(Full.sub(One), Full);
+ EXPECT_EQ(Full.sub(Some), Full);
+ EXPECT_EQ(Full.sub(Wrap), Full);
+ EXPECT_EQ(Empty.sub(Empty), Empty);
+ EXPECT_EQ(Empty.sub(One), Empty);
+ EXPECT_EQ(Empty.sub(Some), Empty);
+ EXPECT_EQ(Empty.sub(Wrap), Empty);
+ EXPECT_EQ(Empty.sub(APInt(16, 4)), Empty);
+ EXPECT_EQ(Some.sub(APInt(16, 4)),
+ ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6)));
+ EXPECT_EQ(Some.sub(Some),
+ ConstantRange(APInt(16, 0xf561), APInt(16, 0xaa0)));
+ EXPECT_EQ(Wrap.sub(APInt(16, 4)),
+ ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6)));
+ EXPECT_EQ(One.sub(APInt(16, 4)),
+ ConstantRange(APInt(16, 0x6)));
+}
+
+TEST_F(ConstantRangeTest, Multiply) {
+ EXPECT_EQ(Full.multiply(Full), Full);
+ EXPECT_EQ(Full.multiply(Empty), Empty);
+ EXPECT_EQ(Full.multiply(One), Full);
+ EXPECT_EQ(Full.multiply(Some), Full);
+ EXPECT_EQ(Full.multiply(Wrap), Full);
+ EXPECT_EQ(Empty.multiply(Empty), Empty);
+ EXPECT_EQ(Empty.multiply(One), Empty);
+ EXPECT_EQ(Empty.multiply(Some), Empty);
+ EXPECT_EQ(Empty.multiply(Wrap), Empty);
+ EXPECT_EQ(One.multiply(One), ConstantRange(APInt(16, 0xa*0xa),
+ APInt(16, 0xa*0xa + 1)));
+ EXPECT_EQ(One.multiply(Some), ConstantRange(APInt(16, 0xa*0xa),
+ APInt(16, 0xa*0xaa9 + 1)));
+ EXPECT_EQ(One.multiply(Wrap), Full);
+ EXPECT_EQ(Some.multiply(Some), Full);
+ EXPECT_EQ(Some.multiply(Wrap), Full);
+ EXPECT_EQ(Wrap.multiply(Wrap), Full);
+
+ ConstantRange Zero(APInt(16, 0));
+ EXPECT_EQ(Zero.multiply(Full), Zero);
+ EXPECT_EQ(Zero.multiply(Some), Zero);
+ EXPECT_EQ(Zero.multiply(Wrap), Zero);
+ EXPECT_EQ(Full.multiply(Zero), Zero);
+ EXPECT_EQ(Some.multiply(Zero), Zero);
+ EXPECT_EQ(Wrap.multiply(Zero), Zero);
+
+ // http://llvm.org/PR4545
+ EXPECT_EQ(ConstantRange(APInt(4, 1), APInt(4, 6)).multiply(
+ ConstantRange(APInt(4, 6), APInt(4, 2))),
+ ConstantRange(4, /*isFullSet=*/true));
+}
+
+TEST_F(ConstantRangeTest, UMax) {
+ EXPECT_EQ(Full.umax(Full), Full);
+ EXPECT_EQ(Full.umax(Empty), Empty);
+ EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
+ EXPECT_EQ(Full.umax(Wrap), Full);
+ EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
+ EXPECT_EQ(Empty.umax(Empty), Empty);
+ EXPECT_EQ(Empty.umax(Some), Empty);
+ EXPECT_EQ(Empty.umax(Wrap), Empty);
+ EXPECT_EQ(Empty.umax(One), Empty);
+ EXPECT_EQ(Some.umax(Some), Some);
+ EXPECT_EQ(Some.umax(Wrap), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
+ EXPECT_EQ(Some.umax(One), Some);
+ // TODO: ConstantRange is currently over-conservative here.
+ EXPECT_EQ(Wrap.umax(Wrap), Full);
+ EXPECT_EQ(Wrap.umax(One), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
+ EXPECT_EQ(One.umax(One), One);
+}
+
+TEST_F(ConstantRangeTest, SMax) {
+ EXPECT_EQ(Full.smax(Full), Full);
+ EXPECT_EQ(Full.smax(Empty), Empty);
+ EXPECT_EQ(Full.smax(Some), ConstantRange(APInt(16, 0xa),
+ APInt::getSignedMinValue(16)));
+ EXPECT_EQ(Full.smax(Wrap), Full);
+ EXPECT_EQ(Full.smax(One), ConstantRange(APInt(16, 0xa),
+ APInt::getSignedMinValue(16)));
+ EXPECT_EQ(Empty.smax(Empty), Empty);
+ EXPECT_EQ(Empty.smax(Some), Empty);
+ EXPECT_EQ(Empty.smax(Wrap), Empty);
+ EXPECT_EQ(Empty.smax(One), Empty);
+ EXPECT_EQ(Some.smax(Some), Some);
+ EXPECT_EQ(Some.smax(Wrap), ConstantRange(APInt(16, 0xa),
+ APInt(16, (uint64_t)INT16_MIN)));
+ EXPECT_EQ(Some.smax(One), Some);
+ EXPECT_EQ(Wrap.smax(One), ConstantRange(APInt(16, 0xa),
+ APInt(16, (uint64_t)INT16_MIN)));
+ EXPECT_EQ(One.smax(One), One);
+}
+
+TEST_F(ConstantRangeTest, UDiv) {
+ EXPECT_EQ(Full.udiv(Full), Full);
+ EXPECT_EQ(Full.udiv(Empty), Empty);
+ EXPECT_EQ(Full.udiv(One), ConstantRange(APInt(16, 0),
+ APInt(16, 0xffff / 0xa + 1)));
+ EXPECT_EQ(Full.udiv(Some), ConstantRange(APInt(16, 0),
+ APInt(16, 0xffff / 0xa + 1)));
+ EXPECT_EQ(Full.udiv(Wrap), Full);
+ EXPECT_EQ(Empty.udiv(Empty), Empty);
+ EXPECT_EQ(Empty.udiv(One), Empty);
+ EXPECT_EQ(Empty.udiv(Some), Empty);
+ EXPECT_EQ(Empty.udiv(Wrap), Empty);
+ EXPECT_EQ(One.udiv(One), ConstantRange(APInt(16, 1)));
+ EXPECT_EQ(One.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 2)));
+ EXPECT_EQ(One.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb)));
+ EXPECT_EQ(Some.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 0x111)));
+ EXPECT_EQ(Some.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
+ EXPECT_EQ(Wrap.udiv(Wrap), Full);
+}
+
+TEST_F(ConstantRangeTest, Shl) {
+ EXPECT_EQ(Full.shl(Full), Full);
+ EXPECT_EQ(Full.shl(Empty), Empty);
+ EXPECT_EQ(Full.shl(One), Full); // TODO: [0, (-1 << 0xa) + 1)
+ EXPECT_EQ(Full.shl(Some), Full); // TODO: [0, (-1 << 0xa) + 1)
+ EXPECT_EQ(Full.shl(Wrap), Full);
+ EXPECT_EQ(Empty.shl(Empty), Empty);
+ EXPECT_EQ(Empty.shl(One), Empty);
+ EXPECT_EQ(Empty.shl(Some), Empty);
+ EXPECT_EQ(Empty.shl(Wrap), Empty);
+ EXPECT_EQ(One.shl(One), ConstantRange(APInt(16, 0xa << 0xa),
+ APInt(16, (0xa << 0xa) + 1)));
+ EXPECT_EQ(One.shl(Some), Full); // TODO: [0xa << 0xa, 0)
+ EXPECT_EQ(One.shl(Wrap), Full); // TODO: [0xa, 0xa << 14 + 1)
+ EXPECT_EQ(Some.shl(Some), Full); // TODO: [0xa << 0xa, 0xfc01)
+ EXPECT_EQ(Some.shl(Wrap), Full); // TODO: [0xa, 0x7ff << 0x5 + 1)
+ EXPECT_EQ(Wrap.shl(Wrap), Full);
+}
+
+TEST_F(ConstantRangeTest, Lshr) {
+ EXPECT_EQ(Full.lshr(Full), Full);
+ EXPECT_EQ(Full.lshr(Empty), Empty);
+ EXPECT_EQ(Full.lshr(One), ConstantRange(APInt(16, 0),
+ APInt(16, (0xffff >> 0xa) + 1)));
+ EXPECT_EQ(Full.lshr(Some), ConstantRange(APInt(16, 0),
+ APInt(16, (0xffff >> 0xa) + 1)));
+ EXPECT_EQ(Full.lshr(Wrap), Full);
+ EXPECT_EQ(Empty.lshr(Empty), Empty);
+ EXPECT_EQ(Empty.lshr(One), Empty);
+ EXPECT_EQ(Empty.lshr(Some), Empty);
+ EXPECT_EQ(Empty.lshr(Wrap), Empty);
+ EXPECT_EQ(One.lshr(One), ConstantRange(APInt(16, 0)));
+ EXPECT_EQ(One.lshr(Some), ConstantRange(APInt(16, 0)));
+ EXPECT_EQ(One.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb)));
+ EXPECT_EQ(Some.lshr(Some), ConstantRange(APInt(16, 0),
+ APInt(16, (0xaaa >> 0xa) + 1)));
+ EXPECT_EQ(Some.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
+ EXPECT_EQ(Wrap.lshr(Wrap), Full);
+}
+
+TEST(ConstantRange, MakeICmpRegion) {
+ // PR8250
+ ConstantRange SMax = ConstantRange(APInt::getSignedMaxValue(32));
+ EXPECT_TRUE(ConstantRange::makeICmpRegion(ICmpInst::ICMP_SGT,
+ SMax).isEmptySet());
+}
+
+} // anonymous namespace
Casting.cpp
CommandLineTest.cpp
CompressionTest.cpp
- ConstantRangeTest.cpp
ConvertUTFTest.cpp
DataExtractorTest.cpp
EndianTest.cpp
+++ /dev/null
-//===- llvm/unittest/Support/ConstantRangeTest.cpp - ConstantRange tests --===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#include "llvm/Support/ConstantRange.h"
-#include "llvm/IR/Instructions.h"
-#include "gtest/gtest.h"
-
-using namespace llvm;
-
-namespace {
-
-class ConstantRangeTest : public ::testing::Test {
-protected:
- static ConstantRange Full;
- static ConstantRange Empty;
- static ConstantRange One;
- static ConstantRange Some;
- static ConstantRange Wrap;
-};
-
-ConstantRange ConstantRangeTest::Full(16);
-ConstantRange ConstantRangeTest::Empty(16, false);
-ConstantRange ConstantRangeTest::One(APInt(16, 0xa));
-ConstantRange ConstantRangeTest::Some(APInt(16, 0xa), APInt(16, 0xaaa));
-ConstantRange ConstantRangeTest::Wrap(APInt(16, 0xaaa), APInt(16, 0xa));
-
-TEST_F(ConstantRangeTest, Basics) {
- EXPECT_TRUE(Full.isFullSet());
- EXPECT_FALSE(Full.isEmptySet());
- EXPECT_TRUE(Full.inverse().isEmptySet());
- EXPECT_FALSE(Full.isWrappedSet());
- EXPECT_TRUE(Full.contains(APInt(16, 0x0)));
- EXPECT_TRUE(Full.contains(APInt(16, 0x9)));
- EXPECT_TRUE(Full.contains(APInt(16, 0xa)));
- EXPECT_TRUE(Full.contains(APInt(16, 0xaa9)));
- EXPECT_TRUE(Full.contains(APInt(16, 0xaaa)));
-
- EXPECT_FALSE(Empty.isFullSet());
- EXPECT_TRUE(Empty.isEmptySet());
- EXPECT_TRUE(Empty.inverse().isFullSet());
- EXPECT_FALSE(Empty.isWrappedSet());
- EXPECT_FALSE(Empty.contains(APInt(16, 0x0)));
- EXPECT_FALSE(Empty.contains(APInt(16, 0x9)));
- EXPECT_FALSE(Empty.contains(APInt(16, 0xa)));
- EXPECT_FALSE(Empty.contains(APInt(16, 0xaa9)));
- EXPECT_FALSE(Empty.contains(APInt(16, 0xaaa)));
-
- EXPECT_FALSE(One.isFullSet());
- EXPECT_FALSE(One.isEmptySet());
- EXPECT_FALSE(One.isWrappedSet());
- EXPECT_FALSE(One.contains(APInt(16, 0x0)));
- EXPECT_FALSE(One.contains(APInt(16, 0x9)));
- EXPECT_TRUE(One.contains(APInt(16, 0xa)));
- EXPECT_FALSE(One.contains(APInt(16, 0xaa9)));
- EXPECT_FALSE(One.contains(APInt(16, 0xaaa)));
- EXPECT_FALSE(One.inverse().contains(APInt(16, 0xa)));
-
- EXPECT_FALSE(Some.isFullSet());
- EXPECT_FALSE(Some.isEmptySet());
- EXPECT_FALSE(Some.isWrappedSet());
- EXPECT_FALSE(Some.contains(APInt(16, 0x0)));
- EXPECT_FALSE(Some.contains(APInt(16, 0x9)));
- EXPECT_TRUE(Some.contains(APInt(16, 0xa)));
- EXPECT_TRUE(Some.contains(APInt(16, 0xaa9)));
- EXPECT_FALSE(Some.contains(APInt(16, 0xaaa)));
-
- EXPECT_FALSE(Wrap.isFullSet());
- EXPECT_FALSE(Wrap.isEmptySet());
- EXPECT_TRUE(Wrap.isWrappedSet());
- EXPECT_TRUE(Wrap.contains(APInt(16, 0x0)));
- EXPECT_TRUE(Wrap.contains(APInt(16, 0x9)));
- EXPECT_FALSE(Wrap.contains(APInt(16, 0xa)));
- EXPECT_FALSE(Wrap.contains(APInt(16, 0xaa9)));
- EXPECT_TRUE(Wrap.contains(APInt(16, 0xaaa)));
-}
-
-TEST_F(ConstantRangeTest, Equality) {
- EXPECT_EQ(Full, Full);
- EXPECT_EQ(Empty, Empty);
- EXPECT_EQ(One, One);
- EXPECT_EQ(Some, Some);
- EXPECT_EQ(Wrap, Wrap);
- EXPECT_NE(Full, Empty);
- EXPECT_NE(Full, One);
- EXPECT_NE(Full, Some);
- EXPECT_NE(Full, Wrap);
- EXPECT_NE(Empty, One);
- EXPECT_NE(Empty, Some);
- EXPECT_NE(Empty, Wrap);
- EXPECT_NE(One, Some);
- EXPECT_NE(One, Wrap);
- EXPECT_NE(Some, Wrap);
-}
-
-TEST_F(ConstantRangeTest, SingleElement) {
- EXPECT_EQ(Full.getSingleElement(), static_cast<APInt *>(NULL));
- EXPECT_EQ(Empty.getSingleElement(), static_cast<APInt *>(NULL));
- EXPECT_EQ(*One.getSingleElement(), APInt(16, 0xa));
- EXPECT_EQ(Some.getSingleElement(), static_cast<APInt *>(NULL));
- EXPECT_EQ(Wrap.getSingleElement(), static_cast<APInt *>(NULL));
-
- EXPECT_FALSE(Full.isSingleElement());
- EXPECT_FALSE(Empty.isSingleElement());
- EXPECT_TRUE(One.isSingleElement());
- EXPECT_FALSE(Some.isSingleElement());
- EXPECT_FALSE(Wrap.isSingleElement());
-}
-
-TEST_F(ConstantRangeTest, GetSetSize) {
- EXPECT_EQ(Full.getSetSize(), APInt(17, 65536));
- EXPECT_EQ(Empty.getSetSize(), APInt(17, 0));
- EXPECT_EQ(One.getSetSize(), APInt(17, 1));
- EXPECT_EQ(Some.getSetSize(), APInt(17, 0xaa0));
-
- ConstantRange Wrap(APInt(4, 7), APInt(4, 3));
- ConstantRange Wrap2(APInt(4, 8), APInt(4, 7));
- EXPECT_EQ(Wrap.getSetSize(), APInt(5, 12));
- EXPECT_EQ(Wrap2.getSetSize(), APInt(5, 15));
-}
-
-TEST_F(ConstantRangeTest, GetMinsAndMaxes) {
- EXPECT_EQ(Full.getUnsignedMax(), APInt(16, UINT16_MAX));
- EXPECT_EQ(One.getUnsignedMax(), APInt(16, 0xa));
- EXPECT_EQ(Some.getUnsignedMax(), APInt(16, 0xaa9));
- EXPECT_EQ(Wrap.getUnsignedMax(), APInt(16, UINT16_MAX));
-
- EXPECT_EQ(Full.getUnsignedMin(), APInt(16, 0));
- EXPECT_EQ(One.getUnsignedMin(), APInt(16, 0xa));
- EXPECT_EQ(Some.getUnsignedMin(), APInt(16, 0xa));
- EXPECT_EQ(Wrap.getUnsignedMin(), APInt(16, 0));
-
- EXPECT_EQ(Full.getSignedMax(), APInt(16, INT16_MAX));
- EXPECT_EQ(One.getSignedMax(), APInt(16, 0xa));
- EXPECT_EQ(Some.getSignedMax(), APInt(16, 0xaa9));
- EXPECT_EQ(Wrap.getSignedMax(), APInt(16, INT16_MAX));
-
- EXPECT_EQ(Full.getSignedMin(), APInt(16, (uint64_t)INT16_MIN));
- EXPECT_EQ(One.getSignedMin(), APInt(16, 0xa));
- EXPECT_EQ(Some.getSignedMin(), APInt(16, 0xa));
- EXPECT_EQ(Wrap.getSignedMin(), APInt(16, (uint64_t)INT16_MIN));
-
- // Found by Klee
- EXPECT_EQ(ConstantRange(APInt(4, 7), APInt(4, 0)).getSignedMax(),
- APInt(4, 7));
-}
-
-TEST_F(ConstantRangeTest, SignWrapped) {
- EXPECT_TRUE(Full.isSignWrappedSet());
- EXPECT_FALSE(Empty.isSignWrappedSet());
- EXPECT_FALSE(One.isSignWrappedSet());
- EXPECT_FALSE(Some.isSignWrappedSet());
- EXPECT_TRUE(Wrap.isSignWrappedSet());
-
- EXPECT_FALSE(ConstantRange(APInt(8, 127), APInt(8, 128)).isSignWrappedSet());
- EXPECT_TRUE(ConstantRange(APInt(8, 127), APInt(8, 129)).isSignWrappedSet());
- EXPECT_FALSE(ConstantRange(APInt(8, 128), APInt(8, 129)).isSignWrappedSet());
- EXPECT_TRUE(ConstantRange(APInt(8, 10), APInt(8, 9)).isSignWrappedSet());
- EXPECT_TRUE(ConstantRange(APInt(8, 10), APInt(8, 250)).isSignWrappedSet());
- EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 10)).isSignWrappedSet());
- EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 251)).isSignWrappedSet());
-}
-
-TEST_F(ConstantRangeTest, Trunc) {
- ConstantRange TFull = Full.truncate(10);
- ConstantRange TEmpty = Empty.truncate(10);
- ConstantRange TOne = One.truncate(10);
- ConstantRange TSome = Some.truncate(10);
- ConstantRange TWrap = Wrap.truncate(10);
- EXPECT_TRUE(TFull.isFullSet());
- EXPECT_TRUE(TEmpty.isEmptySet());
- EXPECT_EQ(TOne, ConstantRange(One.getLower().trunc(10),
- One.getUpper().trunc(10)));
- EXPECT_TRUE(TSome.isFullSet());
-}
-
-TEST_F(ConstantRangeTest, ZExt) {
- ConstantRange ZFull = Full.zeroExtend(20);
- ConstantRange ZEmpty = Empty.zeroExtend(20);
- ConstantRange ZOne = One.zeroExtend(20);
- ConstantRange ZSome = Some.zeroExtend(20);
- ConstantRange ZWrap = Wrap.zeroExtend(20);
- EXPECT_EQ(ZFull, ConstantRange(APInt(20, 0), APInt(20, 0x10000)));
- EXPECT_TRUE(ZEmpty.isEmptySet());
- EXPECT_EQ(ZOne, ConstantRange(One.getLower().zext(20),
- One.getUpper().zext(20)));
- EXPECT_EQ(ZSome, ConstantRange(Some.getLower().zext(20),
- Some.getUpper().zext(20)));
- EXPECT_EQ(ZWrap, ConstantRange(APInt(20, 0), APInt(20, 0x10000)));
-
- // zext([5, 0), 3->7) = [5, 8)
- ConstantRange FiveZero(APInt(3, 5), APInt(3, 0));
- EXPECT_EQ(FiveZero.zeroExtend(7), ConstantRange(APInt(7, 5), APInt(7, 8)));
-}
-
-TEST_F(ConstantRangeTest, SExt) {
- ConstantRange SFull = Full.signExtend(20);
- ConstantRange SEmpty = Empty.signExtend(20);
- ConstantRange SOne = One.signExtend(20);
- ConstantRange SSome = Some.signExtend(20);
- ConstantRange SWrap = Wrap.signExtend(20);
- EXPECT_EQ(SFull, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true),
- APInt(20, INT16_MAX + 1, true)));
- EXPECT_TRUE(SEmpty.isEmptySet());
- EXPECT_EQ(SOne, ConstantRange(One.getLower().sext(20),
- One.getUpper().sext(20)));
- EXPECT_EQ(SSome, ConstantRange(Some.getLower().sext(20),
- Some.getUpper().sext(20)));
- EXPECT_EQ(SWrap, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true),
- APInt(20, INT16_MAX + 1, true)));
-
- EXPECT_EQ(ConstantRange(APInt(8, 120), APInt(8, 140)).signExtend(16),
- ConstantRange(APInt(16, -128), APInt(16, 128)));
-
- EXPECT_EQ(ConstantRange(APInt(16, 0x0200), APInt(16, 0x8000)).signExtend(19),
- ConstantRange(APInt(19, 0x0200), APInt(19, 0x8000)));
-}
-
-TEST_F(ConstantRangeTest, IntersectWith) {
- EXPECT_EQ(Empty.intersectWith(Full), Empty);
- EXPECT_EQ(Empty.intersectWith(Empty), Empty);
- EXPECT_EQ(Empty.intersectWith(One), Empty);
- EXPECT_EQ(Empty.intersectWith(Some), Empty);
- EXPECT_EQ(Empty.intersectWith(Wrap), Empty);
- EXPECT_EQ(Full.intersectWith(Full), Full);
- EXPECT_EQ(Some.intersectWith(Some), Some);
- EXPECT_EQ(Some.intersectWith(One), One);
- EXPECT_EQ(Full.intersectWith(One), One);
- EXPECT_EQ(Full.intersectWith(Some), Some);
- EXPECT_EQ(Some.intersectWith(Wrap), Empty);
- EXPECT_EQ(One.intersectWith(Wrap), Empty);
- EXPECT_EQ(One.intersectWith(Wrap), Wrap.intersectWith(One));
-
- // Klee generated testcase from PR4545.
- // The intersection of i16 [4, 2) and [6, 5) is disjoint, looking like
- // 01..4.6789ABCDEF where the dots represent values not in the intersection.
- ConstantRange LHS(APInt(16, 4), APInt(16, 2));
- ConstantRange RHS(APInt(16, 6), APInt(16, 5));
- EXPECT_TRUE(LHS.intersectWith(RHS) == LHS);
-
- // previous bug: intersection of [min, 3) and [2, max) should be 2
- LHS = ConstantRange(APInt(32, -2147483646), APInt(32, 3));
- RHS = ConstantRange(APInt(32, 2), APInt(32, 2147483646));
- EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 2)));
-
- // [2, 0) /\ [4, 3) = [2, 0)
- LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
- RHS = ConstantRange(APInt(32, 4), APInt(32, 3));
- EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 2), APInt(32, 0)));
-
- // [2, 0) /\ [4, 2) = [4, 0)
- LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
- RHS = ConstantRange(APInt(32, 4), APInt(32, 2));
- EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 4), APInt(32, 0)));
-
- // [4, 2) /\ [5, 1) = [5, 1)
- LHS = ConstantRange(APInt(32, 4), APInt(32, 2));
- RHS = ConstantRange(APInt(32, 5), APInt(32, 1));
- EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 5), APInt(32, 1)));
-
- // [2, 0) /\ [7, 4) = [7, 4)
- LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
- RHS = ConstantRange(APInt(32, 7), APInt(32, 4));
- EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 7), APInt(32, 4)));
-
- // [4, 2) /\ [1, 0) = [1, 0)
- LHS = ConstantRange(APInt(32, 4), APInt(32, 2));
- RHS = ConstantRange(APInt(32, 1), APInt(32, 0));
- EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 4), APInt(32, 2)));
-
- // [15, 0) /\ [7, 6) = [15, 0)
- LHS = ConstantRange(APInt(32, 15), APInt(32, 0));
- RHS = ConstantRange(APInt(32, 7), APInt(32, 6));
- EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 15), APInt(32, 0)));
-}
-
-TEST_F(ConstantRangeTest, UnionWith) {
- EXPECT_EQ(Wrap.unionWith(One),
- ConstantRange(APInt(16, 0xaaa), APInt(16, 0xb)));
- EXPECT_EQ(One.unionWith(Wrap), Wrap.unionWith(One));
- EXPECT_EQ(Empty.unionWith(Empty), Empty);
- EXPECT_EQ(Full.unionWith(Full), Full);
- EXPECT_EQ(Some.unionWith(Wrap), Full);
-
- // PR4545
- EXPECT_EQ(ConstantRange(APInt(16, 14), APInt(16, 1)).unionWith(
- ConstantRange(APInt(16, 0), APInt(16, 8))),
- ConstantRange(APInt(16, 14), APInt(16, 8)));
- EXPECT_EQ(ConstantRange(APInt(16, 6), APInt(16, 4)).unionWith(
- ConstantRange(APInt(16, 4), APInt(16, 0))),
- ConstantRange(16));
- EXPECT_EQ(ConstantRange(APInt(16, 1), APInt(16, 0)).unionWith(
- ConstantRange(APInt(16, 2), APInt(16, 1))),
- ConstantRange(16));
-}
-
-TEST_F(ConstantRangeTest, SetDifference) {
- EXPECT_EQ(Full.difference(Empty), Full);
- EXPECT_EQ(Full.difference(Full), Empty);
- EXPECT_EQ(Empty.difference(Empty), Empty);
- EXPECT_EQ(Empty.difference(Full), Empty);
-
- ConstantRange A(APInt(16, 3), APInt(16, 7));
- ConstantRange B(APInt(16, 5), APInt(16, 9));
- ConstantRange C(APInt(16, 3), APInt(16, 5));
- ConstantRange D(APInt(16, 7), APInt(16, 9));
- ConstantRange E(APInt(16, 5), APInt(16, 4));
- ConstantRange F(APInt(16, 7), APInt(16, 3));
- EXPECT_EQ(A.difference(B), C);
- EXPECT_EQ(B.difference(A), D);
- EXPECT_EQ(E.difference(A), F);
-}
-
-TEST_F(ConstantRangeTest, SubtractAPInt) {
- EXPECT_EQ(Full.subtract(APInt(16, 4)), Full);
- EXPECT_EQ(Empty.subtract(APInt(16, 4)), Empty);
- EXPECT_EQ(Some.subtract(APInt(16, 4)),
- ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6)));
- EXPECT_EQ(Wrap.subtract(APInt(16, 4)),
- ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6)));
- EXPECT_EQ(One.subtract(APInt(16, 4)),
- ConstantRange(APInt(16, 0x6)));
-}
-
-TEST_F(ConstantRangeTest, Add) {
- EXPECT_EQ(Full.add(APInt(16, 4)), Full);
- EXPECT_EQ(Full.add(Full), Full);
- EXPECT_EQ(Full.add(Empty), Empty);
- EXPECT_EQ(Full.add(One), Full);
- EXPECT_EQ(Full.add(Some), Full);
- EXPECT_EQ(Full.add(Wrap), Full);
- EXPECT_EQ(Empty.add(Empty), Empty);
- EXPECT_EQ(Empty.add(One), Empty);
- EXPECT_EQ(Empty.add(Some), Empty);
- EXPECT_EQ(Empty.add(Wrap), Empty);
- EXPECT_EQ(Empty.add(APInt(16, 4)), Empty);
- EXPECT_EQ(Some.add(APInt(16, 4)),
- ConstantRange(APInt(16, 0xe), APInt(16, 0xaae)));
- EXPECT_EQ(Wrap.add(APInt(16, 4)),
- ConstantRange(APInt(16, 0xaae), APInt(16, 0xe)));
- EXPECT_EQ(One.add(APInt(16, 4)),
- ConstantRange(APInt(16, 0xe)));
-}
-
-TEST_F(ConstantRangeTest, Sub) {
- EXPECT_EQ(Full.sub(APInt(16, 4)), Full);
- EXPECT_EQ(Full.sub(Full), Full);
- EXPECT_EQ(Full.sub(Empty), Empty);
- EXPECT_EQ(Full.sub(One), Full);
- EXPECT_EQ(Full.sub(Some), Full);
- EXPECT_EQ(Full.sub(Wrap), Full);
- EXPECT_EQ(Empty.sub(Empty), Empty);
- EXPECT_EQ(Empty.sub(One), Empty);
- EXPECT_EQ(Empty.sub(Some), Empty);
- EXPECT_EQ(Empty.sub(Wrap), Empty);
- EXPECT_EQ(Empty.sub(APInt(16, 4)), Empty);
- EXPECT_EQ(Some.sub(APInt(16, 4)),
- ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6)));
- EXPECT_EQ(Some.sub(Some),
- ConstantRange(APInt(16, 0xf561), APInt(16, 0xaa0)));
- EXPECT_EQ(Wrap.sub(APInt(16, 4)),
- ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6)));
- EXPECT_EQ(One.sub(APInt(16, 4)),
- ConstantRange(APInt(16, 0x6)));
-}
-
-TEST_F(ConstantRangeTest, Multiply) {
- EXPECT_EQ(Full.multiply(Full), Full);
- EXPECT_EQ(Full.multiply(Empty), Empty);
- EXPECT_EQ(Full.multiply(One), Full);
- EXPECT_EQ(Full.multiply(Some), Full);
- EXPECT_EQ(Full.multiply(Wrap), Full);
- EXPECT_EQ(Empty.multiply(Empty), Empty);
- EXPECT_EQ(Empty.multiply(One), Empty);
- EXPECT_EQ(Empty.multiply(Some), Empty);
- EXPECT_EQ(Empty.multiply(Wrap), Empty);
- EXPECT_EQ(One.multiply(One), ConstantRange(APInt(16, 0xa*0xa),
- APInt(16, 0xa*0xa + 1)));
- EXPECT_EQ(One.multiply(Some), ConstantRange(APInt(16, 0xa*0xa),
- APInt(16, 0xa*0xaa9 + 1)));
- EXPECT_EQ(One.multiply(Wrap), Full);
- EXPECT_EQ(Some.multiply(Some), Full);
- EXPECT_EQ(Some.multiply(Wrap), Full);
- EXPECT_EQ(Wrap.multiply(Wrap), Full);
-
- ConstantRange Zero(APInt(16, 0));
- EXPECT_EQ(Zero.multiply(Full), Zero);
- EXPECT_EQ(Zero.multiply(Some), Zero);
- EXPECT_EQ(Zero.multiply(Wrap), Zero);
- EXPECT_EQ(Full.multiply(Zero), Zero);
- EXPECT_EQ(Some.multiply(Zero), Zero);
- EXPECT_EQ(Wrap.multiply(Zero), Zero);
-
- // http://llvm.org/PR4545
- EXPECT_EQ(ConstantRange(APInt(4, 1), APInt(4, 6)).multiply(
- ConstantRange(APInt(4, 6), APInt(4, 2))),
- ConstantRange(4, /*isFullSet=*/true));
-}
-
-TEST_F(ConstantRangeTest, UMax) {
- EXPECT_EQ(Full.umax(Full), Full);
- EXPECT_EQ(Full.umax(Empty), Empty);
- EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
- EXPECT_EQ(Full.umax(Wrap), Full);
- EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
- EXPECT_EQ(Empty.umax(Empty), Empty);
- EXPECT_EQ(Empty.umax(Some), Empty);
- EXPECT_EQ(Empty.umax(Wrap), Empty);
- EXPECT_EQ(Empty.umax(One), Empty);
- EXPECT_EQ(Some.umax(Some), Some);
- EXPECT_EQ(Some.umax(Wrap), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
- EXPECT_EQ(Some.umax(One), Some);
- // TODO: ConstantRange is currently over-conservative here.
- EXPECT_EQ(Wrap.umax(Wrap), Full);
- EXPECT_EQ(Wrap.umax(One), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
- EXPECT_EQ(One.umax(One), One);
-}
-
-TEST_F(ConstantRangeTest, SMax) {
- EXPECT_EQ(Full.smax(Full), Full);
- EXPECT_EQ(Full.smax(Empty), Empty);
- EXPECT_EQ(Full.smax(Some), ConstantRange(APInt(16, 0xa),
- APInt::getSignedMinValue(16)));
- EXPECT_EQ(Full.smax(Wrap), Full);
- EXPECT_EQ(Full.smax(One), ConstantRange(APInt(16, 0xa),
- APInt::getSignedMinValue(16)));
- EXPECT_EQ(Empty.smax(Empty), Empty);
- EXPECT_EQ(Empty.smax(Some), Empty);
- EXPECT_EQ(Empty.smax(Wrap), Empty);
- EXPECT_EQ(Empty.smax(One), Empty);
- EXPECT_EQ(Some.smax(Some), Some);
- EXPECT_EQ(Some.smax(Wrap), ConstantRange(APInt(16, 0xa),
- APInt(16, (uint64_t)INT16_MIN)));
- EXPECT_EQ(Some.smax(One), Some);
- EXPECT_EQ(Wrap.smax(One), ConstantRange(APInt(16, 0xa),
- APInt(16, (uint64_t)INT16_MIN)));
- EXPECT_EQ(One.smax(One), One);
-}
-
-TEST_F(ConstantRangeTest, UDiv) {
- EXPECT_EQ(Full.udiv(Full), Full);
- EXPECT_EQ(Full.udiv(Empty), Empty);
- EXPECT_EQ(Full.udiv(One), ConstantRange(APInt(16, 0),
- APInt(16, 0xffff / 0xa + 1)));
- EXPECT_EQ(Full.udiv(Some), ConstantRange(APInt(16, 0),
- APInt(16, 0xffff / 0xa + 1)));
- EXPECT_EQ(Full.udiv(Wrap), Full);
- EXPECT_EQ(Empty.udiv(Empty), Empty);
- EXPECT_EQ(Empty.udiv(One), Empty);
- EXPECT_EQ(Empty.udiv(Some), Empty);
- EXPECT_EQ(Empty.udiv(Wrap), Empty);
- EXPECT_EQ(One.udiv(One), ConstantRange(APInt(16, 1)));
- EXPECT_EQ(One.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 2)));
- EXPECT_EQ(One.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb)));
- EXPECT_EQ(Some.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 0x111)));
- EXPECT_EQ(Some.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
- EXPECT_EQ(Wrap.udiv(Wrap), Full);
-}
-
-TEST_F(ConstantRangeTest, Shl) {
- EXPECT_EQ(Full.shl(Full), Full);
- EXPECT_EQ(Full.shl(Empty), Empty);
- EXPECT_EQ(Full.shl(One), Full); // TODO: [0, (-1 << 0xa) + 1)
- EXPECT_EQ(Full.shl(Some), Full); // TODO: [0, (-1 << 0xa) + 1)
- EXPECT_EQ(Full.shl(Wrap), Full);
- EXPECT_EQ(Empty.shl(Empty), Empty);
- EXPECT_EQ(Empty.shl(One), Empty);
- EXPECT_EQ(Empty.shl(Some), Empty);
- EXPECT_EQ(Empty.shl(Wrap), Empty);
- EXPECT_EQ(One.shl(One), ConstantRange(APInt(16, 0xa << 0xa),
- APInt(16, (0xa << 0xa) + 1)));
- EXPECT_EQ(One.shl(Some), Full); // TODO: [0xa << 0xa, 0)
- EXPECT_EQ(One.shl(Wrap), Full); // TODO: [0xa, 0xa << 14 + 1)
- EXPECT_EQ(Some.shl(Some), Full); // TODO: [0xa << 0xa, 0xfc01)
- EXPECT_EQ(Some.shl(Wrap), Full); // TODO: [0xa, 0x7ff << 0x5 + 1)
- EXPECT_EQ(Wrap.shl(Wrap), Full);
-}
-
-TEST_F(ConstantRangeTest, Lshr) {
- EXPECT_EQ(Full.lshr(Full), Full);
- EXPECT_EQ(Full.lshr(Empty), Empty);
- EXPECT_EQ(Full.lshr(One), ConstantRange(APInt(16, 0),
- APInt(16, (0xffff >> 0xa) + 1)));
- EXPECT_EQ(Full.lshr(Some), ConstantRange(APInt(16, 0),
- APInt(16, (0xffff >> 0xa) + 1)));
- EXPECT_EQ(Full.lshr(Wrap), Full);
- EXPECT_EQ(Empty.lshr(Empty), Empty);
- EXPECT_EQ(Empty.lshr(One), Empty);
- EXPECT_EQ(Empty.lshr(Some), Empty);
- EXPECT_EQ(Empty.lshr(Wrap), Empty);
- EXPECT_EQ(One.lshr(One), ConstantRange(APInt(16, 0)));
- EXPECT_EQ(One.lshr(Some), ConstantRange(APInt(16, 0)));
- EXPECT_EQ(One.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb)));
- EXPECT_EQ(Some.lshr(Some), ConstantRange(APInt(16, 0),
- APInt(16, (0xaaa >> 0xa) + 1)));
- EXPECT_EQ(Some.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
- EXPECT_EQ(Wrap.lshr(Wrap), Full);
-}
-
-TEST(ConstantRange, MakeICmpRegion) {
- // PR8250
- ConstantRange SMax = ConstantRange(APInt::getSignedMaxValue(32));
- EXPECT_TRUE(ConstantRange::makeICmpRegion(ICmpInst::ICMP_SGT,
- SMax).isEmptySet());
-}
-
-} // anonymous namespace