[llvm] 6749d18 - [KnownBits] Add blsi and blsmsk
Noah Goldstein via llvm-commits
llvm-commits at lists.llvm.org
Sat Feb 18 11:31:30 PST 2023
Author: Jay Foad
Date: 2023-02-18T13:31:07-06:00
New Revision: 6749d187c6dfd1b126698e44276bbf1f9050beb8
URL: https://github.com/llvm/llvm-project/commit/6749d187c6dfd1b126698e44276bbf1f9050beb8
DIFF: https://github.com/llvm/llvm-project/commit/6749d187c6dfd1b126698e44276bbf1f9050beb8.diff
LOG: [KnownBits] Add blsi and blsmsk
Reviewed By: nikic
Differential Revision: https://reviews.llvm.org/D142519
Added:
Modified:
llvm/include/llvm/Support/KnownBits.h
llvm/lib/Support/KnownBits.cpp
llvm/unittests/Support/KnownBitsTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index 0fb056b254172..83b962a11ea34 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -422,6 +422,14 @@ struct KnownBits {
return KnownBits(Zero.reverseBits(), One.reverseBits());
}
+ /// Compute known bits for X & -X, which has only the lowest bit set of X set.
+ /// The name comes from the X86 BMI instruction
+ KnownBits blsi() const;
+
+ /// Compute known bits for X ^ (X - 1), which has all bits up to and including
+ /// the lowest set bit of X set. The name comes from the X86 BMI instruction.
+ KnownBits blsmsk() const;
+
bool operator==(const KnownBits &Other) const {
return Zero == Other.Zero && One == Other.One;
}
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 745c46fb6ffb6..cb6f5082d6aff 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -623,6 +623,27 @@ KnownBits &KnownBits::operator^=(const KnownBits &RHS) {
return *this;
}
+KnownBits KnownBits::blsi() const {
+ unsigned BitWidth = getBitWidth();
+ KnownBits Known(Zero, APInt(BitWidth, 0));
+ unsigned Max = countMaxTrailingZeros();
+ Known.Zero.setBitsFrom(std::min(Max + 1, BitWidth));
+ unsigned Min = countMinTrailingZeros();
+ if (Max == Min && Max < BitWidth)
+ Known.One.setBit(Max);
+ return Known;
+}
+
+KnownBits KnownBits::blsmsk() const {
+ unsigned BitWidth = getBitWidth();
+ KnownBits Known(BitWidth);
+ unsigned Max = countMaxTrailingZeros();
+ Known.Zero.setBitsFrom(std::min(Max + 1, BitWidth));
+ unsigned Min = countMinTrailingZeros();
+ Known.One.setLowBits(std::min(Min + 1, BitWidth));
+ return Known;
+}
+
void KnownBits::print(raw_ostream &OS) const {
OS << "{Zero=" << Zero << ", One=" << One << "}";
}
diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp
index 2f5d2e81bac6c..da1f6b50bdc1b 100644
--- a/llvm/unittests/Support/KnownBitsTest.cpp
+++ b/llvm/unittests/Support/KnownBitsTest.cpp
@@ -284,6 +284,12 @@ TEST(KnownBitsTest, UnaryExhaustive) {
KnownAbs.Zero.setAllBits();
KnownAbs.One.setAllBits();
KnownBits KnownAbsPoison(KnownAbs);
+ KnownBits KnownBlsi(Bits);
+ KnownBlsi.Zero.setAllBits();
+ KnownBlsi.One.setAllBits();
+ KnownBits KnownBlsmsk(Bits);
+ KnownBlsmsk.Zero.setAllBits();
+ KnownBlsmsk.One.setAllBits();
ForeachNumInKnownBits(Known, [&](const APInt &N) {
APInt Res = N.abs();
@@ -294,6 +300,14 @@ TEST(KnownBitsTest, UnaryExhaustive) {
KnownAbsPoison.One &= Res;
KnownAbsPoison.Zero &= ~Res;
}
+
+ Res = N & -N;
+ KnownBlsi.One &= Res;
+ KnownBlsi.Zero &= ~Res;
+
+ Res = N ^ (N - 1);
+ KnownBlsmsk.One &= Res;
+ KnownBlsmsk.Zero &= ~Res;
});
// abs() is conservatively correct, but not guaranteed to be precise.
@@ -304,6 +318,12 @@ TEST(KnownBitsTest, UnaryExhaustive) {
KnownBits ComputedAbsPoison = Known.abs(true);
EXPECT_TRUE(ComputedAbsPoison.Zero.isSubsetOf(KnownAbsPoison.Zero));
EXPECT_TRUE(ComputedAbsPoison.One.isSubsetOf(KnownAbsPoison.One));
+
+ KnownBits ComputedBlsi = Known.blsi();
+ EXPECT_EQ(KnownBlsi, ComputedBlsi);
+
+ KnownBits ComputedBlsmsk = Known.blsmsk();
+ EXPECT_EQ(KnownBlsmsk, ComputedBlsmsk);
});
}
More information about the llvm-commits
mailing list