[PATCH] D142469: [KnownBits] Add blsi and blsmsk
Jay Foad via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 24 06:58:19 PST 2023
foad updated this revision to Diff 491765.
foad added a comment.
Upload the right patch
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D142469/new/
https://reviews.llvm.org/D142469
Files:
llvm/include/llvm/Support/KnownBits.h
llvm/lib/Support/KnownBits.cpp
llvm/unittests/Support/KnownBitsTest.cpp
Index: llvm/unittests/Support/KnownBitsTest.cpp
===================================================================
--- llvm/unittests/Support/KnownBitsTest.cpp
+++ llvm/unittests/Support/KnownBitsTest.cpp
@@ -284,6 +284,12 @@
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 @@
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 @@
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);
});
}
Index: llvm/lib/Support/KnownBits.cpp
===================================================================
--- llvm/lib/Support/KnownBits.cpp
+++ llvm/lib/Support/KnownBits.cpp
@@ -623,6 +623,27 @@
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 << "}";
}
Index: llvm/include/llvm/Support/KnownBits.h
===================================================================
--- llvm/include/llvm/Support/KnownBits.h
+++ llvm/include/llvm/Support/KnownBits.h
@@ -422,6 +422,14 @@
return KnownBits(Zero.reverseBits(), One.reverseBits());
}
+ /// Compute known bits for X & -X. The name comes from the X86 BMI instruction
+ /// BLSI.
+ KnownBits blsi() const;
+
+ /// Compute known bits for X ^ (X - 1). The name comes from the X86 BMI
+ /// instruction BLSMSK.
+ KnownBits blsmsk() const;
+
bool operator==(const KnownBits &Other) const {
return Zero == Other.Zero && One == Other.One;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142469.491765.patch
Type: text/x-patch
Size: 3051 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230124/3a82d2c6/attachment.bin>
More information about the llvm-commits
mailing list