[ADT] BitVector: give subsetOf(RHS) name to !test(RHS) (NFC) (#170875)

Define `LHS.subsetOf(RHS)` as a more descriptive name for `!LHS.test(RHS)`
and update the existing callers to use that name.

Co-authored-by: Jakub Kuderski <jakub@nod-labs.com>
This commit is contained in:
Anatoly Trosinenko
2025-12-09 21:00:27 +03:00
committed by GitHub
parent 0a39d1ff9e
commit c0eac77f3c
9 changed files with 107 additions and 12 deletions

View File

@@ -547,7 +547,7 @@ protected:
// Being trusted is a strictly stronger property than being
// safe-to-dereference.
assert(!Next.TrustedRegs.test(Next.SafeToDerefRegs) &&
assert(Next.TrustedRegs.subsetOf(Next.SafeToDerefRegs) &&
"SafeToDerefRegs should contain all TrustedRegs");
return Next;

View File

@@ -550,7 +550,7 @@ public:
return *this;
}
/// test - Check if (This - RHS) is zero.
/// test - Check if (This - RHS) is non-zero.
/// This is the same as reset(RHS) and any().
bool test(const BitVector &RHS) const {
unsigned ThisWords = Bits.size();
@@ -567,6 +567,9 @@ public:
return false;
}
/// subsetOf - Check if This is a subset of RHS.
bool subsetOf(const BitVector &RHS) const { return !test(RHS); }
template <class F, class... ArgTys>
static BitVector &apply(F &&f, BitVector &Out, BitVector const &Arg,
ArgTys const &...Args) {

View File

@@ -552,7 +552,8 @@ public:
return *this;
}
/// Check if (This - RHS) is zero. This is the same as reset(RHS) and any().
/// Check if (This - RHS) is non-zero.
/// This is the same as reset(RHS) and any().
bool test(const SmallBitVector &RHS) const {
if (isSmall() && RHS.isSmall())
return (getSmallBits() & ~RHS.getSmallBits()) != 0;
@@ -571,6 +572,9 @@ public:
return false;
}
/// Check if This is a subset of RHS.
bool subsetOf(const SmallBitVector &RHS) const { return !test(RHS); }
SmallBitVector &operator|=(const SmallBitVector &RHS) {
resize(std::max(size(), RHS.size()));
if (isSmall() && RHS.isSmall())

View File

@@ -173,7 +173,7 @@ void StackLifetime::calculateLocalLiveness() {
BitsIn.resize(NumAllocas, true);
// Update block LiveIn set, noting whether it has changed.
if (BitsIn.test(BlockInfo.LiveIn)) {
if (!BitsIn.subsetOf(BlockInfo.LiveIn)) {
BlockInfo.LiveIn |= BitsIn;
}
@@ -198,7 +198,7 @@ void StackLifetime::calculateLocalLiveness() {
}
// Update block LiveOut set, noting whether it has changed.
if (BitsIn.test(BlockInfo.LiveOut)) {
if (!BitsIn.subsetOf(BlockInfo.LiveOut)) {
Changed = true;
BlockInfo.LiveOut |= BitsIn;
}

View File

@@ -164,7 +164,7 @@ bool DwarfExpression::addMachineReg(const TargetRegisterInfo &TRI,
// If this sub-register has a DWARF number and we haven't covered
// its range, and its range covers the value, emit a DWARF piece for it.
if (Offset < MaxSize && CurSubReg.test(Coverage)) {
if (Offset < MaxSize && !CurSubReg.subsetOf(Coverage)) {
// Emit a piece for any gap in the coverage.
if (Offset > CurPos)
DwarfRegs.push_back(Register::createSubRegister(

View File

@@ -815,13 +815,13 @@ void StackColoring::calculateLocalLiveness() {
LocalLiveOut |= BlockInfo.Begin;
// Update block LiveIn set, noting whether it has changed.
if (LocalLiveIn.test(BlockInfo.LiveIn)) {
if (!LocalLiveIn.subsetOf(BlockInfo.LiveIn)) {
changed = true;
BlockInfo.LiveIn |= LocalLiveIn;
}
// Update block LiveOut set, noting whether it has changed.
if (LocalLiveOut.test(BlockInfo.LiveOut)) {
if (!LocalLiveOut.subsetOf(BlockInfo.LiveOut)) {
changed = true;
BlockInfo.LiveOut |= LocalLiveOut;
}

View File

@@ -137,8 +137,7 @@ namespace {
return !Bits.any();
}
bool includes(const RegisterSet &Rs) const {
// A.test(B) <=> A-B != {}
return !Rs.Bits.test(Bits);
return Rs.Bits.subsetOf(Bits);
}
bool intersects(const RegisterSet &Rs) const {
return Bits.anyCommon(Rs.Bits);

View File

@@ -153,8 +153,7 @@ namespace {
return !BitVector::any();
}
bool includes(const RegisterSet &Rs) const {
// A.BitVector::test(B) <=> A-B != {}
return !Rs.BitVector::test(*this);
return Rs.BitVector::subsetOf(*this);
}
bool intersects(const RegisterSet &Rs) const {
return BitVector::anyCommon(Rs);

View File

@@ -11,6 +11,7 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallBitVector.h"
#include "gtest/gtest.h"
#include <initializer_list>
using namespace llvm;
@@ -835,19 +836,27 @@ TYPED_TEST(BitVectorTest, BinOps) {
A.resize(65);
EXPECT_FALSE(A.anyCommon(B));
EXPECT_FALSE(B.anyCommon(B));
EXPECT_TRUE(A.subsetOf(B));
EXPECT_TRUE(B.subsetOf(A));
B.resize(64);
A.set(64);
EXPECT_FALSE(A.anyCommon(B));
EXPECT_FALSE(B.anyCommon(A));
EXPECT_FALSE(A.subsetOf(B));
EXPECT_TRUE(B.subsetOf(A));
B.set(63);
EXPECT_FALSE(A.anyCommon(B));
EXPECT_FALSE(B.anyCommon(A));
EXPECT_FALSE(A.subsetOf(B));
EXPECT_FALSE(B.subsetOf(A));
A.set(63);
EXPECT_TRUE(A.anyCommon(B));
EXPECT_TRUE(B.anyCommon(A));
EXPECT_FALSE(A.subsetOf(B));
EXPECT_TRUE(B.subsetOf(A));
B.resize(70);
B.set(64);
@@ -855,6 +864,87 @@ TYPED_TEST(BitVectorTest, BinOps) {
A.resize(64);
EXPECT_FALSE(A.anyCommon(B));
EXPECT_FALSE(B.anyCommon(A));
EXPECT_FALSE(A.subsetOf(B));
EXPECT_FALSE(B.subsetOf(A));
B.set(63);
B.reset(64);
EXPECT_TRUE(A.anyCommon(B));
EXPECT_TRUE(B.anyCommon(A));
EXPECT_TRUE(A.subsetOf(B));
EXPECT_TRUE(B.subsetOf(A));
}
template <typename VecType>
static inline VecType
createBitVectorFromBits(uint32_t Size, std::initializer_list<int> SetBits) {
VecType V;
V.resize(Size);
for (int BitIndex : SetBits)
V.set(BitIndex);
return V;
}
TYPED_TEST(BitVectorTest, BinOpsLiteral) {
// More tests of binary operations with more focus on the semantics and
// less focus on mutability.
auto AnyCommon = [](uint32_t SizeLHS, std::initializer_list<int> SetBitsLHS,
uint32_t SizeRHS, std::initializer_list<int> SetBitsRHS) {
auto LHS = createBitVectorFromBits<TypeParam>(SizeLHS, SetBitsLHS);
auto RHS = createBitVectorFromBits<TypeParam>(SizeRHS, SetBitsRHS);
return LHS.anyCommon(RHS);
};
auto SubsetOf = [](uint32_t SizeLHS, std::initializer_list<int> SetBitsLHS,
uint32_t SizeRHS, std::initializer_list<int> SetBitsRHS) {
auto LHS = createBitVectorFromBits<TypeParam>(SizeLHS, SetBitsLHS);
auto RHS = createBitVectorFromBits<TypeParam>(SizeRHS, SetBitsRHS);
return LHS.subsetOf(RHS);
};
// clang-format off
// Test small-sized vectors.
EXPECT_TRUE (AnyCommon(10, {1, 2, 3}, 10, {3, 4, 5}));
EXPECT_FALSE(AnyCommon(10, {1, 2, 3}, 10, {4, 5}));
EXPECT_FALSE(SubsetOf(10, {1, 2, 3}, 10, {2, 3, 4}));
EXPECT_TRUE (SubsetOf(10, {2, 3}, 10, {2, 3, 4}));
EXPECT_FALSE(SubsetOf(10, {1, 2, 3}, 10, {2, 3}));
EXPECT_TRUE (SubsetOf(10, {1, 2, 3}, 10, {1, 2, 3}));
// Test representations of empty sets of various sizes.
EXPECT_FALSE(AnyCommon(10, {}, 10, {}));
EXPECT_FALSE(AnyCommon(10, {}, 123, {}));
EXPECT_FALSE(AnyCommon(123, {}, 10, {}));
EXPECT_FALSE(AnyCommon(123, {}, 123, {}));
EXPECT_TRUE(SubsetOf(10, {}, 10, {}));
EXPECT_TRUE(SubsetOf(10, {}, 123, {}));
EXPECT_TRUE(SubsetOf(123, {}, 10, {}));
EXPECT_TRUE(SubsetOf(123, {}, 123, {}));
// Test handling of the remainder words.
EXPECT_FALSE(AnyCommon(10, {1, 2}, 123, {5, 70}));
EXPECT_TRUE (AnyCommon(10, {1, 2}, 123, {1, 70}));
EXPECT_FALSE(AnyCommon(123, {5, 70}, 10, {1, 2}));
EXPECT_TRUE (AnyCommon(123, {1, 70}, 10, {1, 2}));
EXPECT_FALSE(AnyCommon(10, {1, 2}, 123, {5}));
EXPECT_TRUE (AnyCommon(10, {1, 2}, 123, {1}));
EXPECT_FALSE(AnyCommon(123, {5}, 10, {1, 2}));
EXPECT_TRUE (AnyCommon(123, {1}, 10, {1, 2}));
EXPECT_FALSE(SubsetOf(10, {1, 2}, 123, {2, 70}));
EXPECT_TRUE (SubsetOf(10, {1, 2}, 123, {1, 2, 70}));
EXPECT_FALSE(SubsetOf(123, {2, 70}, 10, {1, 2}));
EXPECT_FALSE(SubsetOf(123, {1, 2, 70}, 10, {1, 2}));
EXPECT_FALSE(SubsetOf(10, {1, 2}, 123, {2}));
EXPECT_TRUE (SubsetOf(10, {1, 2}, 123, {1, 2}));
EXPECT_TRUE (SubsetOf(123, {2}, 10, {1, 2}));
EXPECT_TRUE (SubsetOf(123, {1, 2}, 10, {1, 2}));
// clang-format on
}
using RangeList = std::vector<std::pair<int, int>>;