Skip to content

Commit c0eac77

Browse files
atrosinenkokuhar
andauthored
[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 <[email protected]>
1 parent 0a39d1f commit c0eac77

File tree

9 files changed

+107
-12
lines changed

9 files changed

+107
-12
lines changed

bolt/lib/Passes/PAuthGadgetScanner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ class SrcSafetyAnalysis {
547547

548548
// Being trusted is a strictly stronger property than being
549549
// safe-to-dereference.
550-
assert(!Next.TrustedRegs.test(Next.SafeToDerefRegs) &&
550+
assert(Next.TrustedRegs.subsetOf(Next.SafeToDerefRegs) &&
551551
"SafeToDerefRegs should contain all TrustedRegs");
552552

553553
return Next;

llvm/include/llvm/ADT/BitVector.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ class BitVector {
550550
return *this;
551551
}
552552

553-
/// test - Check if (This - RHS) is zero.
553+
/// test - Check if (This - RHS) is non-zero.
554554
/// This is the same as reset(RHS) and any().
555555
bool test(const BitVector &RHS) const {
556556
unsigned ThisWords = Bits.size();
@@ -567,6 +567,9 @@ class BitVector {
567567
return false;
568568
}
569569

570+
/// subsetOf - Check if This is a subset of RHS.
571+
bool subsetOf(const BitVector &RHS) const { return !test(RHS); }
572+
570573
template <class F, class... ArgTys>
571574
static BitVector &apply(F &&f, BitVector &Out, BitVector const &Arg,
572575
ArgTys const &...Args) {

llvm/include/llvm/ADT/SmallBitVector.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,8 @@ class SmallBitVector {
552552
return *this;
553553
}
554554

555-
/// Check if (This - RHS) is zero. This is the same as reset(RHS) and any().
555+
/// Check if (This - RHS) is non-zero.
556+
/// This is the same as reset(RHS) and any().
556557
bool test(const SmallBitVector &RHS) const {
557558
if (isSmall() && RHS.isSmall())
558559
return (getSmallBits() & ~RHS.getSmallBits()) != 0;
@@ -571,6 +572,9 @@ class SmallBitVector {
571572
return false;
572573
}
573574

575+
/// Check if This is a subset of RHS.
576+
bool subsetOf(const SmallBitVector &RHS) const { return !test(RHS); }
577+
574578
SmallBitVector &operator|=(const SmallBitVector &RHS) {
575579
resize(std::max(size(), RHS.size()));
576580
if (isSmall() && RHS.isSmall())

llvm/lib/Analysis/StackLifetime.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ void StackLifetime::calculateLocalLiveness() {
173173
BitsIn.resize(NumAllocas, true);
174174

175175
// Update block LiveIn set, noting whether it has changed.
176-
if (BitsIn.test(BlockInfo.LiveIn)) {
176+
if (!BitsIn.subsetOf(BlockInfo.LiveIn)) {
177177
BlockInfo.LiveIn |= BitsIn;
178178
}
179179

@@ -198,7 +198,7 @@ void StackLifetime::calculateLocalLiveness() {
198198
}
199199

200200
// Update block LiveOut set, noting whether it has changed.
201-
if (BitsIn.test(BlockInfo.LiveOut)) {
201+
if (!BitsIn.subsetOf(BlockInfo.LiveOut)) {
202202
Changed = true;
203203
BlockInfo.LiveOut |= BitsIn;
204204
}

llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ bool DwarfExpression::addMachineReg(const TargetRegisterInfo &TRI,
164164

165165
// If this sub-register has a DWARF number and we haven't covered
166166
// its range, and its range covers the value, emit a DWARF piece for it.
167-
if (Offset < MaxSize && CurSubReg.test(Coverage)) {
167+
if (Offset < MaxSize && !CurSubReg.subsetOf(Coverage)) {
168168
// Emit a piece for any gap in the coverage.
169169
if (Offset > CurPos)
170170
DwarfRegs.push_back(Register::createSubRegister(

llvm/lib/CodeGen/StackColoring.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,13 +815,13 @@ void StackColoring::calculateLocalLiveness() {
815815
LocalLiveOut |= BlockInfo.Begin;
816816

817817
// Update block LiveIn set, noting whether it has changed.
818-
if (LocalLiveIn.test(BlockInfo.LiveIn)) {
818+
if (!LocalLiveIn.subsetOf(BlockInfo.LiveIn)) {
819819
changed = true;
820820
BlockInfo.LiveIn |= LocalLiveIn;
821821
}
822822

823823
// Update block LiveOut set, noting whether it has changed.
824-
if (LocalLiveOut.test(BlockInfo.LiveOut)) {
824+
if (!LocalLiveOut.subsetOf(BlockInfo.LiveOut)) {
825825
changed = true;
826826
BlockInfo.LiveOut |= LocalLiveOut;
827827
}

llvm/lib/Target/Hexagon/HexagonBitSimplify.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ namespace {
137137
return !Bits.any();
138138
}
139139
bool includes(const RegisterSet &Rs) const {
140-
// A.test(B) <=> A-B != {}
141-
return !Rs.Bits.test(Bits);
140+
return Rs.Bits.subsetOf(Bits);
142141
}
143142
bool intersects(const RegisterSet &Rs) const {
144143
return Bits.anyCommon(Rs.Bits);

llvm/lib/Target/Hexagon/HexagonGenInsert.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ namespace {
153153
return !BitVector::any();
154154
}
155155
bool includes(const RegisterSet &Rs) const {
156-
// A.BitVector::test(B) <=> A-B != {}
157-
return !Rs.BitVector::test(*this);
156+
return Rs.BitVector::subsetOf(*this);
158157
}
159158
bool intersects(const RegisterSet &Rs) const {
160159
return BitVector::anyCommon(Rs);

llvm/unittests/ADT/BitVectorTest.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "llvm/ADT/STLExtras.h"
1212
#include "llvm/ADT/SmallBitVector.h"
1313
#include "gtest/gtest.h"
14+
#include <initializer_list>
1415

1516
using namespace llvm;
1617

@@ -835,26 +836,115 @@ TYPED_TEST(BitVectorTest, BinOps) {
835836
A.resize(65);
836837
EXPECT_FALSE(A.anyCommon(B));
837838
EXPECT_FALSE(B.anyCommon(B));
839+
EXPECT_TRUE(A.subsetOf(B));
840+
EXPECT_TRUE(B.subsetOf(A));
838841

839842
B.resize(64);
840843
A.set(64);
841844
EXPECT_FALSE(A.anyCommon(B));
842845
EXPECT_FALSE(B.anyCommon(A));
846+
EXPECT_FALSE(A.subsetOf(B));
847+
EXPECT_TRUE(B.subsetOf(A));
843848

844849
B.set(63);
845850
EXPECT_FALSE(A.anyCommon(B));
846851
EXPECT_FALSE(B.anyCommon(A));
852+
EXPECT_FALSE(A.subsetOf(B));
853+
EXPECT_FALSE(B.subsetOf(A));
847854

848855
A.set(63);
849856
EXPECT_TRUE(A.anyCommon(B));
850857
EXPECT_TRUE(B.anyCommon(A));
858+
EXPECT_FALSE(A.subsetOf(B));
859+
EXPECT_TRUE(B.subsetOf(A));
851860

852861
B.resize(70);
853862
B.set(64);
854863
B.reset(63);
855864
A.resize(64);
856865
EXPECT_FALSE(A.anyCommon(B));
857866
EXPECT_FALSE(B.anyCommon(A));
867+
EXPECT_FALSE(A.subsetOf(B));
868+
EXPECT_FALSE(B.subsetOf(A));
869+
870+
B.set(63);
871+
B.reset(64);
872+
EXPECT_TRUE(A.anyCommon(B));
873+
EXPECT_TRUE(B.anyCommon(A));
874+
EXPECT_TRUE(A.subsetOf(B));
875+
EXPECT_TRUE(B.subsetOf(A));
876+
}
877+
878+
template <typename VecType>
879+
static inline VecType
880+
createBitVectorFromBits(uint32_t Size, std::initializer_list<int> SetBits) {
881+
VecType V;
882+
V.resize(Size);
883+
for (int BitIndex : SetBits)
884+
V.set(BitIndex);
885+
return V;
886+
}
887+
888+
TYPED_TEST(BitVectorTest, BinOpsLiteral) {
889+
// More tests of binary operations with more focus on the semantics and
890+
// less focus on mutability.
891+
892+
auto AnyCommon = [](uint32_t SizeLHS, std::initializer_list<int> SetBitsLHS,
893+
uint32_t SizeRHS, std::initializer_list<int> SetBitsRHS) {
894+
auto LHS = createBitVectorFromBits<TypeParam>(SizeLHS, SetBitsLHS);
895+
auto RHS = createBitVectorFromBits<TypeParam>(SizeRHS, SetBitsRHS);
896+
return LHS.anyCommon(RHS);
897+
};
898+
auto SubsetOf = [](uint32_t SizeLHS, std::initializer_list<int> SetBitsLHS,
899+
uint32_t SizeRHS, std::initializer_list<int> SetBitsRHS) {
900+
auto LHS = createBitVectorFromBits<TypeParam>(SizeLHS, SetBitsLHS);
901+
auto RHS = createBitVectorFromBits<TypeParam>(SizeRHS, SetBitsRHS);
902+
return LHS.subsetOf(RHS);
903+
};
904+
905+
// clang-format off
906+
907+
// Test small-sized vectors.
908+
EXPECT_TRUE (AnyCommon(10, {1, 2, 3}, 10, {3, 4, 5}));
909+
EXPECT_FALSE(AnyCommon(10, {1, 2, 3}, 10, {4, 5}));
910+
911+
EXPECT_FALSE(SubsetOf(10, {1, 2, 3}, 10, {2, 3, 4}));
912+
EXPECT_TRUE (SubsetOf(10, {2, 3}, 10, {2, 3, 4}));
913+
EXPECT_FALSE(SubsetOf(10, {1, 2, 3}, 10, {2, 3}));
914+
EXPECT_TRUE (SubsetOf(10, {1, 2, 3}, 10, {1, 2, 3}));
915+
916+
// Test representations of empty sets of various sizes.
917+
EXPECT_FALSE(AnyCommon(10, {}, 10, {}));
918+
EXPECT_FALSE(AnyCommon(10, {}, 123, {}));
919+
EXPECT_FALSE(AnyCommon(123, {}, 10, {}));
920+
EXPECT_FALSE(AnyCommon(123, {}, 123, {}));
921+
EXPECT_TRUE(SubsetOf(10, {}, 10, {}));
922+
EXPECT_TRUE(SubsetOf(10, {}, 123, {}));
923+
EXPECT_TRUE(SubsetOf(123, {}, 10, {}));
924+
EXPECT_TRUE(SubsetOf(123, {}, 123, {}));
925+
926+
// Test handling of the remainder words.
927+
EXPECT_FALSE(AnyCommon(10, {1, 2}, 123, {5, 70}));
928+
EXPECT_TRUE (AnyCommon(10, {1, 2}, 123, {1, 70}));
929+
EXPECT_FALSE(AnyCommon(123, {5, 70}, 10, {1, 2}));
930+
EXPECT_TRUE (AnyCommon(123, {1, 70}, 10, {1, 2}));
931+
932+
EXPECT_FALSE(AnyCommon(10, {1, 2}, 123, {5}));
933+
EXPECT_TRUE (AnyCommon(10, {1, 2}, 123, {1}));
934+
EXPECT_FALSE(AnyCommon(123, {5}, 10, {1, 2}));
935+
EXPECT_TRUE (AnyCommon(123, {1}, 10, {1, 2}));
936+
937+
EXPECT_FALSE(SubsetOf(10, {1, 2}, 123, {2, 70}));
938+
EXPECT_TRUE (SubsetOf(10, {1, 2}, 123, {1, 2, 70}));
939+
EXPECT_FALSE(SubsetOf(123, {2, 70}, 10, {1, 2}));
940+
EXPECT_FALSE(SubsetOf(123, {1, 2, 70}, 10, {1, 2}));
941+
942+
EXPECT_FALSE(SubsetOf(10, {1, 2}, 123, {2}));
943+
EXPECT_TRUE (SubsetOf(10, {1, 2}, 123, {1, 2}));
944+
EXPECT_TRUE (SubsetOf(123, {2}, 10, {1, 2}));
945+
EXPECT_TRUE (SubsetOf(123, {1, 2}, 10, {1, 2}));
946+
947+
// clang-format on
858948
}
859949

860950
using RangeList = std::vector<std::pair<int, int>>;

0 commit comments

Comments
 (0)