diff options
| -rw-r--r-- | llvm/include/llvm/ADT/APInt.h | 5 | ||||
| -rw-r--r-- | llvm/include/llvm/IR/PatternMatch.h | 24 | ||||
| -rw-r--r-- | llvm/unittests/ADT/APIntTest.cpp | 17 | ||||
| -rw-r--r-- | llvm/unittests/IR/PatternMatch.cpp | 24 |
4 files changed, 69 insertions, 1 deletions
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h index 796110f753b..0791a6d686a 100644 --- a/llvm/include/llvm/ADT/APInt.h +++ b/llvm/include/llvm/ADT/APInt.h @@ -389,6 +389,11 @@ public: /// \returns true if this APInt is positive. bool isStrictlyPositive() const { return isNonNegative() && !isNullValue(); } + /// Determine if this APInt Value is non-positive (<= 0). + /// + /// \returns true if this APInt is non-positive. + bool isNonPositive() const { return !isStrictlyPositive(); } + /// Determine if all bits are set /// /// This checks to see if the value has all bits of the APInt are set or not. diff --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h index 64ffb59a0b1..6621fc9f819 100644 --- a/llvm/include/llvm/IR/PatternMatch.h +++ b/llvm/include/llvm/IR/PatternMatch.h @@ -366,7 +366,7 @@ inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { struct is_nonnegative { bool isValue(const APInt &C) { return C.isNonNegative(); } }; -/// Match an integer or vector of nonnegative values. +/// Match an integer or vector of non-negative values. /// For vectors, this includes constants with undefined elements. inline cst_pred_ty<is_nonnegative> m_NonNegative() { return cst_pred_ty<is_nonnegative>(); @@ -375,6 +375,28 @@ inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; } +struct is_strictlypositive { + bool isValue(const APInt &C) { return C.isStrictlyPositive(); } +}; +/// Match an integer or vector of strictly positive values. +/// For vectors, this includes constants with undefined elements. +inline cst_pred_ty<is_strictlypositive> m_StrictlyPositive() { + return cst_pred_ty<is_strictlypositive>(); +} +inline api_pred_ty<is_strictlypositive> m_StrictlyPositive(const APInt *&V) { + return V; +} + +struct is_nonpositive { + bool isValue(const APInt &C) { return C.isNonPositive(); } +}; +/// Match an integer or vector of non-positive values. +/// For vectors, this includes constants with undefined elements. +inline cst_pred_ty<is_nonpositive> m_NonPositive() { + return cst_pred_ty<is_nonpositive>(); +} +inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; } + struct is_one { bool isValue(const APInt &C) { return C.isOneValue(); } }; diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp index 2a3abbfaf16..b6fee5b7ff1 100644 --- a/llvm/unittests/ADT/APIntTest.cpp +++ b/llvm/unittests/ADT/APIntTest.cpp @@ -2845,4 +2845,21 @@ TEST(APIntTest, GetMostSignificantDifferentBitExaustive) { } } +TEST(APIntTest, SignbitZeroChecks) { + EXPECT_TRUE(APInt(8, -1).isNegative()); + EXPECT_FALSE(APInt(8, -1).isNonNegative()); + EXPECT_FALSE(APInt(8, -1).isStrictlyPositive()); + EXPECT_TRUE(APInt(8, -1).isNonPositive()); + + EXPECT_FALSE(APInt(8, 0).isNegative()); + EXPECT_TRUE(APInt(8, 0).isNonNegative()); + EXPECT_FALSE(APInt(8, 0).isStrictlyPositive()); + EXPECT_TRUE(APInt(8, 0).isNonPositive()); + + EXPECT_FALSE(APInt(8, 1).isNegative()); + EXPECT_TRUE(APInt(8, 1).isNonNegative()); + EXPECT_TRUE(APInt(8, 1).isStrictlyPositive()); + EXPECT_FALSE(APInt(8, 1).isNonPositive()); +} + } // end anonymous namespace diff --git a/llvm/unittests/IR/PatternMatch.cpp b/llvm/unittests/IR/PatternMatch.cpp index 0c2ec36f878..80e0e92615a 100644 --- a/llvm/unittests/IR/PatternMatch.cpp +++ b/llvm/unittests/IR/PatternMatch.cpp @@ -182,6 +182,30 @@ TEST_F(PatternMatchTest, SpecificIntUGT) { .match(NegOne)); } +TEST_F(PatternMatchTest, SignbitZeroChecks) { + Type *IntTy = IRB.getInt32Ty(); + unsigned BitWidth = IntTy->getScalarSizeInBits(); + + Value *Zero = ConstantInt::get(IntTy, 0); + Value *One = ConstantInt::get(IntTy, 1); + Value *NegOne = ConstantInt::get(IntTy, -1); + + EXPECT_TRUE(m_Negative().match(NegOne)); + EXPECT_FALSE(m_NonNegative().match(NegOne)); + EXPECT_FALSE(m_StrictlyPositive().match(NegOne)); + EXPECT_TRUE(m_NonPositive().match(NegOne)); + + EXPECT_FALSE(m_Negative().match(Zero)); + EXPECT_TRUE(m_NonNegative().match(Zero)); + EXPECT_FALSE(m_StrictlyPositive().match(Zero)); + EXPECT_TRUE(m_NonPositive().match(Zero)); + + EXPECT_FALSE(m_Negative().match(One)); + EXPECT_TRUE(m_NonNegative().match(One)); + EXPECT_TRUE(m_StrictlyPositive().match(One)); + EXPECT_FALSE(m_NonPositive().match(One)); +} + TEST_F(PatternMatchTest, SpecificIntUGE) { Type *IntTy = IRB.getInt32Ty(); unsigned BitWidth = IntTy->getScalarSizeInBits(); |

