forked from OSchip/llvm-project
Add TinyPtrVector support for general pointer-like things.
In particular, make TinyPtrVector<PtrIntPair<T *, 1>> work. Remove all unnecessary assumptions that the element type has a formal "null" representation. The important property to maintain is that default-constructed element type has the same internal representation as the default-constructed PointerUnion (all zero bits). Remove the incorrect recursive behavior from PointerUnion::isNull. This was never generally correct because it only recursed over the first type parameter. With variadic templates it's completely unnecessary. llvm-svn: 369473
This commit is contained in:
parent
5a7bba09ac
commit
861b371e13
|
@ -160,10 +160,11 @@ class PointerUnion
|
||||||
void *, pointer_union_detail::bitsRequired(sizeof...(PTs)), int,
|
void *, pointer_union_detail::bitsRequired(sizeof...(PTs)), int,
|
||||||
pointer_union_detail::PointerUnionUIntTraits<PTs...>>,
|
pointer_union_detail::PointerUnionUIntTraits<PTs...>>,
|
||||||
0, PTs...> {
|
0, PTs...> {
|
||||||
// The first type is special in some ways, but we don't want PointerUnion to
|
// The first type is special because we want to directly cast a pointer to a
|
||||||
// be a 'template <typename First, typename ...Rest>' because it's much more
|
// default-initialized union to a pointer to the first type. But we don't
|
||||||
// convenient to have a name for the whole pack. So split off the first type
|
// want PointerUnion to be a 'template <typename First, typename ...Rest>'
|
||||||
// here.
|
// because it's much more convenient to have a name for the whole pack. So
|
||||||
|
// split off the first type here.
|
||||||
using First = typename pointer_union_detail::GetFirstType<PTs...>::type;
|
using First = typename pointer_union_detail::GetFirstType<PTs...>::type;
|
||||||
using Base = typename PointerUnion::PointerUnionMembers;
|
using Base = typename PointerUnion::PointerUnionMembers;
|
||||||
|
|
||||||
|
@ -175,12 +176,7 @@ public:
|
||||||
|
|
||||||
/// Test if the pointer held in the union is null, regardless of
|
/// Test if the pointer held in the union is null, regardless of
|
||||||
/// which type it is.
|
/// which type it is.
|
||||||
bool isNull() const {
|
bool isNull() const { return !this->Val.getPointer(); }
|
||||||
// Convert from the void* to one of the pointer types, to make sure that
|
|
||||||
// we recursively strip off low bits if we have a nested PointerUnion.
|
|
||||||
return !PointerLikeTypeTraits<First>::getFromVoidPointer(
|
|
||||||
this->Val.getPointer());
|
|
||||||
}
|
|
||||||
|
|
||||||
explicit operator bool() const { return !isNull(); }
|
explicit operator bool() const { return !isNull(); }
|
||||||
|
|
||||||
|
@ -219,7 +215,8 @@ public:
|
||||||
First *getAddrOfPtr1() {
|
First *getAddrOfPtr1() {
|
||||||
assert(is<First>() && "Val is not the first pointer");
|
assert(is<First>() && "Val is not the first pointer");
|
||||||
assert(
|
assert(
|
||||||
get<First>() == this->Val.getPointer() &&
|
PointerLikeTypeTraits<First>::getAsVoidPointer(get<First>()) ==
|
||||||
|
this->Val.getPointer() &&
|
||||||
"Can't get the address because PointerLikeTypeTraits changes the ptr");
|
"Can't get the address because PointerLikeTypeTraits changes the ptr");
|
||||||
return const_cast<First *>(
|
return const_cast<First *>(
|
||||||
reinterpret_cast<const First *>(this->Val.getAddrOfPointer()));
|
reinterpret_cast<const First *>(this->Val.getAddrOfPointer()));
|
||||||
|
|
|
@ -31,6 +31,10 @@ class TinyPtrVector {
|
||||||
public:
|
public:
|
||||||
using VecTy = SmallVector<EltTy, 4>;
|
using VecTy = SmallVector<EltTy, 4>;
|
||||||
using value_type = typename VecTy::value_type;
|
using value_type = typename VecTy::value_type;
|
||||||
|
// EltTy must be the first pointer type so that is<EltTy> is true for the
|
||||||
|
// default-constructed PtrUnion. This allows an empty TinyPtrVector to
|
||||||
|
// naturally vend a begin/end iterator of type EltTy* without an additional
|
||||||
|
// check for the empty state.
|
||||||
using PtrUnion = PointerUnion<EltTy, VecTy *>;
|
using PtrUnion = PointerUnion<EltTy, VecTy *>;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -96,14 +100,14 @@ public:
|
||||||
if (RHS.Val.template is<EltTy>()) {
|
if (RHS.Val.template is<EltTy>()) {
|
||||||
V->clear();
|
V->clear();
|
||||||
V->push_back(RHS.front());
|
V->push_back(RHS.front());
|
||||||
RHS.Val = (EltTy)nullptr;
|
RHS.Val = EltTy();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
delete V;
|
delete V;
|
||||||
}
|
}
|
||||||
|
|
||||||
Val = RHS.Val;
|
Val = RHS.Val;
|
||||||
RHS.Val = (EltTy)nullptr;
|
RHS.Val = EltTy();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -213,9 +217,9 @@ public:
|
||||||
|
|
||||||
EltTy operator[](unsigned i) const {
|
EltTy operator[](unsigned i) const {
|
||||||
assert(!Val.isNull() && "can't index into an empty vector");
|
assert(!Val.isNull() && "can't index into an empty vector");
|
||||||
if (EltTy V = Val.template dyn_cast<EltTy>()) {
|
if (Val.template is<EltTy>()) {
|
||||||
assert(i == 0 && "tinyvector index out of range");
|
assert(i == 0 && "tinyvector index out of range");
|
||||||
return V;
|
return Val.template get<EltTy>();
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(i < Val.template get<VecTy*>()->size() &&
|
assert(i < Val.template get<VecTy*>()->size() &&
|
||||||
|
@ -225,29 +229,29 @@ public:
|
||||||
|
|
||||||
EltTy front() const {
|
EltTy front() const {
|
||||||
assert(!empty() && "vector empty");
|
assert(!empty() && "vector empty");
|
||||||
if (EltTy V = Val.template dyn_cast<EltTy>())
|
if (Val.template is<EltTy>())
|
||||||
return V;
|
return Val.template get<EltTy>();
|
||||||
return Val.template get<VecTy*>()->front();
|
return Val.template get<VecTy*>()->front();
|
||||||
}
|
}
|
||||||
|
|
||||||
EltTy back() const {
|
EltTy back() const {
|
||||||
assert(!empty() && "vector empty");
|
assert(!empty() && "vector empty");
|
||||||
if (EltTy V = Val.template dyn_cast<EltTy>())
|
if (Val.template is<EltTy>())
|
||||||
return V;
|
return Val.template get<EltTy>();
|
||||||
return Val.template get<VecTy*>()->back();
|
return Val.template get<VecTy*>()->back();
|
||||||
}
|
}
|
||||||
|
|
||||||
void push_back(EltTy NewVal) {
|
void push_back(EltTy NewVal) {
|
||||||
assert(NewVal && "Can't add a null value");
|
|
||||||
|
|
||||||
// If we have nothing, add something.
|
// If we have nothing, add something.
|
||||||
if (Val.isNull()) {
|
if (Val.isNull()) {
|
||||||
Val = NewVal;
|
Val = NewVal;
|
||||||
|
assert(!Val.isNull() && "Can't add a null value");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we have a single value, convert to a vector.
|
// If we have a single value, convert to a vector.
|
||||||
if (EltTy V = Val.template dyn_cast<EltTy>()) {
|
if (Val.template is<EltTy>()) {
|
||||||
|
EltTy V = Val.template get<EltTy>();
|
||||||
Val = new VecTy();
|
Val = new VecTy();
|
||||||
Val.template get<VecTy*>()->push_back(V);
|
Val.template get<VecTy*>()->push_back(V);
|
||||||
}
|
}
|
||||||
|
@ -267,7 +271,7 @@ public:
|
||||||
void clear() {
|
void clear() {
|
||||||
// If we have a single value, convert to empty.
|
// If we have a single value, convert to empty.
|
||||||
if (Val.template is<EltTy>()) {
|
if (Val.template is<EltTy>()) {
|
||||||
Val = (EltTy)nullptr;
|
Val = EltTy();
|
||||||
} else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
|
} else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
|
||||||
// If we have a vector form, just clear it.
|
// If we have a vector form, just clear it.
|
||||||
Vec->clear();
|
Vec->clear();
|
||||||
|
@ -282,7 +286,7 @@ public:
|
||||||
// If we have a single value, convert to empty.
|
// If we have a single value, convert to empty.
|
||||||
if (Val.template is<EltTy>()) {
|
if (Val.template is<EltTy>()) {
|
||||||
if (I == begin())
|
if (I == begin())
|
||||||
Val = (EltTy)nullptr;
|
Val = EltTy();
|
||||||
} else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
|
} else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
|
||||||
// multiple items in a vector; just do the erase, there is no
|
// multiple items in a vector; just do the erase, there is no
|
||||||
// benefit to collapsing back to a pointer
|
// benefit to collapsing back to a pointer
|
||||||
|
@ -298,7 +302,7 @@ public:
|
||||||
|
|
||||||
if (Val.template is<EltTy>()) {
|
if (Val.template is<EltTy>()) {
|
||||||
if (S == begin() && S != E)
|
if (S == begin() && S != E)
|
||||||
Val = (EltTy)nullptr;
|
Val = EltTy();
|
||||||
} else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
|
} else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
|
||||||
return Vec->erase(S, E);
|
return Vec->erase(S, E);
|
||||||
}
|
}
|
||||||
|
@ -313,7 +317,8 @@ public:
|
||||||
return std::prev(end());
|
return std::prev(end());
|
||||||
}
|
}
|
||||||
assert(!Val.isNull() && "Null value with non-end insert iterator.");
|
assert(!Val.isNull() && "Null value with non-end insert iterator.");
|
||||||
if (EltTy V = Val.template dyn_cast<EltTy>()) {
|
if (Val.template is<EltTy>()) {
|
||||||
|
EltTy V = Val.template get<EltTy>();
|
||||||
assert(I == begin());
|
assert(I == begin());
|
||||||
Val = Elt;
|
Val = Elt;
|
||||||
push_back(V);
|
push_back(V);
|
||||||
|
@ -339,7 +344,8 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
Val = new VecTy();
|
Val = new VecTy();
|
||||||
} else if (EltTy V = Val.template dyn_cast<EltTy>()) {
|
} else if (Val.template is<EltTy>()) {
|
||||||
|
EltTy V = Val.template get<EltTy>();
|
||||||
Val = new VecTy();
|
Val = new VecTy();
|
||||||
Val.template get<VecTy*>()->push_back(V);
|
Val.template get<VecTy*>()->push_back(V);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,14 +13,25 @@ using namespace llvm;
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
typedef PointerUnion<int *, float *> PU;
|
typedef PointerUnion<int *, float *> PU;
|
||||||
|
typedef PointerUnion3<int *, float *, long long *> PU3;
|
||||||
|
typedef PointerUnion4<int *, float *, long long *, double *> PU4;
|
||||||
|
|
||||||
struct PointerUnionTest : public testing::Test {
|
struct PointerUnionTest : public testing::Test {
|
||||||
float f;
|
float f;
|
||||||
int i;
|
int i;
|
||||||
|
double d;
|
||||||
|
long long l;
|
||||||
|
|
||||||
PU a, b, c, n;
|
PU a, b, c, n;
|
||||||
|
PU3 i3, f3, l3;
|
||||||
|
PU4 i4, f4, l4, d4;
|
||||||
|
PU4 i4null, f4null, l4null, d4null;
|
||||||
|
|
||||||
PointerUnionTest() : f(3.14f), i(42), a(&f), b(&i), c(&i), n() {}
|
PointerUnionTest()
|
||||||
|
: f(3.14f), i(42), d(3.14), l(42), a(&f), b(&i), c(&i), n(), i3(&i),
|
||||||
|
f3(&f), l3(&l), i4(&i), f4(&f), l4(&l), d4(&d), i4null((int *)nullptr),
|
||||||
|
f4null((float *)nullptr), l4null((long long *)nullptr),
|
||||||
|
d4null((double *)nullptr) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_F(PointerUnionTest, Comparison) {
|
TEST_F(PointerUnionTest, Comparison) {
|
||||||
|
@ -32,6 +43,19 @@ TEST_F(PointerUnionTest, Comparison) {
|
||||||
EXPECT_FALSE(b != c);
|
EXPECT_FALSE(b != c);
|
||||||
EXPECT_TRUE(b != n);
|
EXPECT_TRUE(b != n);
|
||||||
EXPECT_FALSE(b == n);
|
EXPECT_FALSE(b == n);
|
||||||
|
EXPECT_TRUE(i3 == i3);
|
||||||
|
EXPECT_FALSE(i3 != i3);
|
||||||
|
EXPECT_TRUE(i3 != f3);
|
||||||
|
EXPECT_TRUE(f3 != l3);
|
||||||
|
EXPECT_TRUE(i4 == i4);
|
||||||
|
EXPECT_FALSE(i4 != i4);
|
||||||
|
EXPECT_TRUE(i4 != f4);
|
||||||
|
EXPECT_TRUE(i4 != l4);
|
||||||
|
EXPECT_TRUE(f4 != l4);
|
||||||
|
EXPECT_TRUE(l4 != d4);
|
||||||
|
EXPECT_TRUE(i4null != f4null);
|
||||||
|
EXPECT_TRUE(i4null != l4null);
|
||||||
|
EXPECT_TRUE(i4null != d4null);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(PointerUnionTest, Null) {
|
TEST_F(PointerUnionTest, Null) {
|
||||||
|
@ -51,6 +75,17 @@ TEST_F(PointerUnionTest, Null) {
|
||||||
b = nullptr;
|
b = nullptr;
|
||||||
EXPECT_EQ(n, b);
|
EXPECT_EQ(n, b);
|
||||||
EXPECT_NE(b, c);
|
EXPECT_NE(b, c);
|
||||||
|
EXPECT_FALSE(i3.isNull());
|
||||||
|
EXPECT_FALSE(f3.isNull());
|
||||||
|
EXPECT_FALSE(l3.isNull());
|
||||||
|
EXPECT_FALSE(i4.isNull());
|
||||||
|
EXPECT_FALSE(f4.isNull());
|
||||||
|
EXPECT_FALSE(l4.isNull());
|
||||||
|
EXPECT_FALSE(d4.isNull());
|
||||||
|
EXPECT_TRUE(i4null.isNull());
|
||||||
|
EXPECT_TRUE(f4null.isNull());
|
||||||
|
EXPECT_TRUE(l4null.isNull());
|
||||||
|
EXPECT_TRUE(d4null.isNull());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(PointerUnionTest, Is) {
|
TEST_F(PointerUnionTest, Is) {
|
||||||
|
@ -60,6 +95,17 @@ TEST_F(PointerUnionTest, Is) {
|
||||||
EXPECT_FALSE(b.is<float *>());
|
EXPECT_FALSE(b.is<float *>());
|
||||||
EXPECT_TRUE(n.is<int *>());
|
EXPECT_TRUE(n.is<int *>());
|
||||||
EXPECT_FALSE(n.is<float *>());
|
EXPECT_FALSE(n.is<float *>());
|
||||||
|
EXPECT_TRUE(i3.is<int *>());
|
||||||
|
EXPECT_TRUE(f3.is<float *>());
|
||||||
|
EXPECT_TRUE(l3.is<long long *>());
|
||||||
|
EXPECT_TRUE(i4.is<int *>());
|
||||||
|
EXPECT_TRUE(f4.is<float *>());
|
||||||
|
EXPECT_TRUE(l4.is<long long *>());
|
||||||
|
EXPECT_TRUE(d4.is<double *>());
|
||||||
|
EXPECT_TRUE(i4null.is<int *>());
|
||||||
|
EXPECT_TRUE(f4null.is<float *>());
|
||||||
|
EXPECT_TRUE(l4null.is<long long *>());
|
||||||
|
EXPECT_TRUE(d4null.is<double *>());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(PointerUnionTest, Get) {
|
TEST_F(PointerUnionTest, Get) {
|
||||||
|
@ -105,4 +151,9 @@ TEST_F(PointerUnionTest, ManyElements) {
|
||||||
EXPECT_TRUE(a != PU8(&a0));
|
EXPECT_TRUE(a != PU8(&a0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(PointerUnionTest, GetAddrOfPtr1) {
|
||||||
|
EXPECT_TRUE((void *)b.getAddrOfPtr1() == (void *)&b);
|
||||||
|
EXPECT_TRUE((void *)n.getAddrOfPtr1() == (void *)&n);
|
||||||
|
}
|
||||||
|
|
||||||
} // end anonymous namespace
|
} // end anonymous namespace
|
||||||
|
|
|
@ -22,12 +22,21 @@
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
template <typename T> struct RemovePointer : std::remove_pointer<T> {};
|
||||||
|
|
||||||
|
template <typename PointerTy, unsigned IntBits, typename IntType,
|
||||||
|
typename PtrTraits, typename Info>
|
||||||
|
struct RemovePointer<
|
||||||
|
PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>> {
|
||||||
|
typedef typename RemovePointer<PointerTy>::type type;
|
||||||
|
};
|
||||||
|
|
||||||
template <typename VectorT>
|
template <typename VectorT>
|
||||||
class TinyPtrVectorTest : public testing::Test {
|
class TinyPtrVectorTest : public testing::Test {
|
||||||
protected:
|
protected:
|
||||||
typedef typename VectorT::value_type PtrT;
|
typedef typename VectorT::value_type PtrT;
|
||||||
typedef typename std::remove_pointer<PtrT>::type ValueT;
|
typedef typename RemovePointer<PtrT>::type ValueT;
|
||||||
|
using PtrTraits = PointerLikeTypeTraits<PtrT>;
|
||||||
|
|
||||||
VectorT V;
|
VectorT V;
|
||||||
VectorT V2;
|
VectorT V2;
|
||||||
|
@ -37,11 +46,13 @@ protected:
|
||||||
|
|
||||||
TinyPtrVectorTest() {
|
TinyPtrVectorTest() {
|
||||||
for (size_t i = 0, e = array_lengthof(TestValues); i != e; ++i)
|
for (size_t i = 0, e = array_lengthof(TestValues); i != e; ++i)
|
||||||
TestPtrs.push_back(&TestValues[i]);
|
TestPtrs.push_back(PtrT(&TestValues[i]));
|
||||||
|
|
||||||
std::shuffle(TestPtrs.begin(), TestPtrs.end(), std::mt19937{});
|
std::shuffle(TestPtrs.begin(), TestPtrs.end(), std::mt19937{});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PtrT makePtr(ValueT *V) { return PtrT(V); }
|
||||||
|
|
||||||
ArrayRef<PtrT> testArray(size_t N) {
|
ArrayRef<PtrT> testArray(size_t N) {
|
||||||
return makeArrayRef(&TestPtrs[0], N);
|
return makeArrayRef(&TestPtrs[0], N);
|
||||||
}
|
}
|
||||||
|
@ -69,9 +80,9 @@ protected:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef ::testing::Types<TinyPtrVector<int*>,
|
typedef ::testing::Types<TinyPtrVector<int *>, TinyPtrVector<double *>,
|
||||||
TinyPtrVector<double*>
|
TinyPtrVector<PointerIntPair<int *, 1>>>
|
||||||
> TinyPtrVectorTestTypes;
|
TinyPtrVectorTestTypes;
|
||||||
TYPED_TEST_CASE(TinyPtrVectorTest, TinyPtrVectorTestTypes);
|
TYPED_TEST_CASE(TinyPtrVectorTest, TinyPtrVectorTestTypes);
|
||||||
|
|
||||||
TYPED_TEST(TinyPtrVectorTest, EmptyTest) {
|
TYPED_TEST(TinyPtrVectorTest, EmptyTest) {
|
||||||
|
@ -95,8 +106,8 @@ TYPED_TEST(TinyPtrVectorTest, PushPopBack) {
|
||||||
this->expectValues(this->V, this->testArray(4));
|
this->expectValues(this->V, this->testArray(4));
|
||||||
this->V.pop_back();
|
this->V.pop_back();
|
||||||
this->expectValues(this->V, this->testArray(3));
|
this->expectValues(this->V, this->testArray(3));
|
||||||
this->TestPtrs[3] = &this->TestValues[42];
|
this->TestPtrs[3] = this->makePtr(&this->TestValues[42]);
|
||||||
this->TestPtrs[4] = &this->TestValues[43];
|
this->TestPtrs[4] = this->makePtr(&this->TestValues[43]);
|
||||||
this->V.push_back(this->TestPtrs[3]);
|
this->V.push_back(this->TestPtrs[3]);
|
||||||
this->expectValues(this->V, this->testArray(4));
|
this->expectValues(this->V, this->testArray(4));
|
||||||
this->V.push_back(this->TestPtrs[4]);
|
this->V.push_back(this->TestPtrs[4]);
|
||||||
|
|
Loading…
Reference in New Issue