From bad648a23e15829a7210cd194e3d3f3ea13a91f8 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sun, 7 Apr 2019 17:52:40 +0000 Subject: [ConstantRange] Add isAllNegative() and isAllNonNegative() methods Add isAllNegative() and isAllNonNegative() methods to ConstantRange, which determine whether all values in the constant range are negative/non-negative. This is useful for replacing KnownBits isNegative() and isNonNegative() calls when changing code to use constant ranges. Differential Revision: https://reviews.llvm.org/D60264 llvm-svn: 357871 --- llvm/lib/IR/ConstantRange.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'llvm/lib') diff --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp index f07ec9d5a3f..d048b8c2ed5 100644 --- a/llvm/lib/IR/ConstantRange.cpp +++ b/llvm/lib/IR/ConstantRange.cpp @@ -389,6 +389,21 @@ ConstantRange::isSizeLargerThan(uint64_t MaxSize) const { return (Upper - Lower).ugt(MaxSize); } +bool ConstantRange::isAllNegative() const { + // Empty set is all negative, full set is not. + if (isEmptySet()) + return true; + if (isFullSet()) + return false; + + return !isUpperSignWrapped() && !Upper.isStrictlyPositive(); +} + +bool ConstantRange::isAllNonNegative() const { + // Empty and full set are automatically treated correctly. + return !isSignWrappedSet() && Lower.isNonNegative(); +} + APInt ConstantRange::getUnsignedMax() const { if (isFullSet() || isUpperWrapped()) return APInt::getMaxValue(getBitWidth()); -- cgit v1.2.3