[KnownBits] Add blsi and blsmsk
authorJay Foad <jay.foad@amd.com>
Thu, 26 Jan 2023 17:34:50 +0000 (11:34 -0600)
committerNoah Goldstein <goldstein.w.n@gmail.com>
Sat, 18 Feb 2023 19:31:07 +0000 (13:31 -0600)
Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D142519

llvm/include/llvm/Support/KnownBits.h
llvm/lib/Support/KnownBits.cpp
llvm/unittests/Support/KnownBitsTest.cpp

index 0fb056b..83b962a 100644 (file)
@@ -422,6 +422,14 @@ public:
     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;
   }
index 745c46f..cb6f508 100644 (file)
@@ -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 << "}";
 }
index 2f5d2e8..da1f6b5 100644 (file)
@@ -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);
   });
 }