[llvm] 444ac34 - [APInt][PatternMatch] Add 'is non-positive' predicate
Roman Lebedev via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 3 10:34:34 PST 2019
Author: Roman Lebedev
Date: 2019-12-03T21:34:12+03:00
New Revision: 444ac34182d562db2cb3b7d93d0d63acca1a6c9a
URL: https://github.com/llvm/llvm-project/commit/444ac34182d562db2cb3b7d93d0d63acca1a6c9a
DIFF: https://github.com/llvm/llvm-project/commit/444ac34182d562db2cb3b7d93d0d63acca1a6c9a.diff
LOG: [APInt][PatternMatch] Add 'is non-positive' predicate
It will be useful for implementing the fold mentioned in
https://bugs.llvm.org/show_bug.cgi?id=44100#c4
Added:
Modified:
llvm/include/llvm/ADT/APInt.h
llvm/include/llvm/IR/PatternMatch.h
llvm/unittests/ADT/APIntTest.cpp
llvm/unittests/IR/PatternMatch.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 796110f753bc..0791a6d686a3 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -389,6 +389,11 @@ class LLVM_NODISCARD APInt {
/// \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 64ffb59a0b13..6621fc9f819c 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 2a3abbfaf164..b6fee5b7ff1a 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 0c2ec36f8789..80e0e92615ae 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();
More information about the llvm-commits
mailing list