Index: llvm/trunk/include/llvm/ADT/PointerUnion.h =================================================================== --- llvm/trunk/include/llvm/ADT/PointerUnion.h +++ llvm/trunk/include/llvm/ADT/PointerUnion.h @@ -53,22 +53,98 @@ typename PointerUnionTypeSelector::Return; }; -/// Provide PointerLikeTypeTraits for void* that is used by PointerUnion -/// for the two template arguments. -template class PointerUnionUIntTraits { -public: - static inline void *getAsVoidPointer(void *P) { return P; } - static inline void *getFromVoidPointer(void *P) { return P; } +namespace pointer_union_detail { + constexpr int constexprMin(int a, int b) { return a < b ? a : b; } + /// Determine the number of bits required to store integers with values < n. + /// This is ceil(log2(n)). + constexpr int bitsRequired(unsigned n) { + return n > 1 ? 1 + bitsRequired((n + 1) / 2) : 0; + } + + // FIXME: In C++14, replace this with + // std::min({PointerLikeTypeTraits::NumLowBitsAvailable...}) + template constexpr int lowBitsAvailable() { + return PointerLikeTypeTraits::NumLowBitsAvailable; + } + template + constexpr int lowBitsAvailable() { + return constexprMin(lowBitsAvailable(), lowBitsAvailable()); + } + + /// Find the index of a type in a list of types. TypeIndex::Index + /// is the index of T in Us, or sizeof...(Us) if T does not appear in the + /// list. + template struct TypeIndex; + template struct TypeIndex { + static constexpr int Index = 0; + }; + template + struct TypeIndex { + static constexpr int Index = 1 + TypeIndex::Index; + }; + template struct TypeIndex { + static constexpr int Index = 0; + }; - enum { - PT1BitsAv = (int)(PointerLikeTypeTraits::NumLowBitsAvailable), - PT2BitsAv = (int)(PointerLikeTypeTraits::NumLowBitsAvailable), - NumLowBitsAvailable = PT1BitsAv < PT2BitsAv ? PT1BitsAv : PT2BitsAv + /// Find the first type in a list of types. + template struct GetFirstType { + using type = T; + }; + + /// Provide PointerLikeTypeTraits for void* that is used by PointerUnion + /// for the template arguments. + template class PointerUnionUIntTraits { + public: + static inline void *getAsVoidPointer(void *P) { return P; } + static inline void *getFromVoidPointer(void *P) { return P; } + static constexpr int NumLowBitsAvailable = lowBitsAvailable(); + }; + + /// Implement assigment in terms of construction. + template struct AssignableFrom { + Derived &operator=(T t) { + return static_cast(*this) = Derived(t); + } }; -}; -/// A discriminated union of two pointer types, with the discriminator in the -/// low bit of the pointer. + template + class PointerUnionMembers; + + template + class PointerUnionMembers { + protected: + ValTy Val; + PointerUnionMembers() = default; + PointerUnionMembers(ValTy Val) : Val(Val) {} + + friend struct PointerLikeTypeTraits; + }; + + template + class PointerUnionMembers + : public PointerUnionMembers { + using Base = PointerUnionMembers; + public: + using Base::Base; + PointerUnionMembers() = default; + PointerUnionMembers(Type V) + : Base(ValTy(const_cast( + PointerLikeTypeTraits::getAsVoidPointer(V)), + I)) {} + + using Base::operator=; + Derived &operator=(Type V) { + this->Val = ValTy( + const_cast(PointerLikeTypeTraits::getAsVoidPointer(V)), + I); + return static_cast(*this); + }; + }; +} + +/// A discriminated union of two or more pointer types, with the discriminator +/// in the low bit of the pointer. /// /// This implementation is extremely efficient in space due to leveraging the /// low bits of the pointer, while exposing a natural and type-safe API. @@ -83,49 +159,44 @@ /// P = (float*)0; /// Y = P.get(); // ok. /// X = P.get(); // runtime assertion failure. -template class PointerUnion { -public: - using ValTy = - PointerIntPair>; - -private: - ValTy Val; - - struct IsPT1 { - static const int Num = 0; - }; - struct IsPT2 { - static const int Num = 1; - }; - template struct UNION_DOESNT_CONTAIN_TYPE {}; +template +class PointerUnion + : public pointer_union_detail::PointerUnionMembers< + PointerUnion, + PointerIntPair< + void *, pointer_union_detail::bitsRequired(sizeof...(PTs)), int, + pointer_union_detail::PointerUnionUIntTraits>, + 0, PTs...> { + // The first type is special in some ways, but we don't want PointerUnion to + // be a 'template ' 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::type; + using Base = typename PointerUnion::PointerUnionMembers; public: PointerUnion() = default; - PointerUnion(PT1 V) - : Val(const_cast( - PointerLikeTypeTraits::getAsVoidPointer(V))) {} - PointerUnion(PT2 V) - : Val(const_cast(PointerLikeTypeTraits::getAsVoidPointer(V)), - 1) {} + + PointerUnion(std::nullptr_t) : PointerUnion() {} + using Base::Base; /// Test if the pointer held in the union is null, regardless of /// which type it is. bool isNull() const { // 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::getFromVoidPointer(Val.getPointer()); + return !PointerLikeTypeTraits::getFromVoidPointer( + this->Val.getPointer()); } explicit operator bool() const { return !isNull(); } /// Test if the Union currently holds the type matching T. template int is() const { - using Ty = typename ::llvm::PointerUnionTypeSelector< - PT1, T, IsPT1, - ::llvm::PointerUnionTypeSelector>>::Return; - int TyNo = Ty::Num; - return static_cast(Val.getInt()) == TyNo; + constexpr int Index = pointer_union_detail::TypeIndex::Index; + static_assert(Index < sizeof...(PTs), + "PointerUnion::is given type not in the union"); + return this->Val.getInt() == Index; } /// Returns the value of the specified pointer type. @@ -133,7 +204,7 @@ /// If the specified pointer type is incorrect, assert. template T get() const { assert(is() && "Invalid accessor called"); - return PointerLikeTypeTraits::getFromVoidPointer(Val.getPointer()); + return PointerLikeTypeTraits::getFromVoidPointer(this->Val.getPointer()); } /// Returns the current pointer if it is of the specified pointer type, @@ -146,342 +217,100 @@ /// If the union is set to the first pointer type get an address pointing to /// it. - PT1 const *getAddrOfPtr1() const { + First const *getAddrOfPtr1() const { return const_cast(this)->getAddrOfPtr1(); } /// If the union is set to the first pointer type get an address pointing to /// it. - PT1 *getAddrOfPtr1() { - assert(is() && "Val is not the first pointer"); + First *getAddrOfPtr1() { + assert(is() && "Val is not the first pointer"); assert( - get() == Val.getPointer() && + get() == this->Val.getPointer() && "Can't get the address because PointerLikeTypeTraits changes the ptr"); - return const_cast( - reinterpret_cast(Val.getAddrOfPointer())); + return const_cast( + reinterpret_cast(this->Val.getAddrOfPointer())); } /// Assignment from nullptr which just clears the union. const PointerUnion &operator=(std::nullptr_t) { - Val.initWithPointer(nullptr); + this->Val.initWithPointer(nullptr); return *this; } - /// Assignment operators - Allow assigning into this union from either - /// pointer type, setting the discriminator to remember what it came from. - const PointerUnion &operator=(const PT1 &RHS) { - Val.initWithPointer( - const_cast(PointerLikeTypeTraits::getAsVoidPointer(RHS))); - return *this; - } - const PointerUnion &operator=(const PT2 &RHS) { - Val.setPointerAndInt( - const_cast(PointerLikeTypeTraits::getAsVoidPointer(RHS)), - 1); - return *this; - } + /// Assignment from elements of the union. + using Base::operator=; - void *getOpaqueValue() const { return Val.getOpaqueValue(); } + void *getOpaqueValue() const { return this->Val.getOpaqueValue(); } static inline PointerUnion getFromOpaqueValue(void *VP) { PointerUnion V; - V.Val = ValTy::getFromOpaqueValue(VP); + V.Val = decltype(V.Val)::getFromOpaqueValue(VP); return V; } }; -template -bool operator==(PointerUnion lhs, PointerUnion rhs) { +template +bool operator==(PointerUnion lhs, PointerUnion rhs) { return lhs.getOpaqueValue() == rhs.getOpaqueValue(); } -template -bool operator!=(PointerUnion lhs, PointerUnion rhs) { +template +bool operator!=(PointerUnion lhs, PointerUnion rhs) { return lhs.getOpaqueValue() != rhs.getOpaqueValue(); } -template -bool operator<(PointerUnion lhs, PointerUnion rhs) { +template +bool operator<(PointerUnion lhs, PointerUnion rhs) { return lhs.getOpaqueValue() < rhs.getOpaqueValue(); } // Teach SmallPtrSet that PointerUnion is "basically a pointer", that has // # low bits available = min(PT1bits,PT2bits)-1. -template -struct PointerLikeTypeTraits> { - static inline void *getAsVoidPointer(const PointerUnion &P) { +template +struct PointerLikeTypeTraits> { + static inline void *getAsVoidPointer(const PointerUnion &P) { return P.getOpaqueValue(); } - static inline PointerUnion getFromVoidPointer(void *P) { - return PointerUnion::getFromOpaqueValue(P); + static inline PointerUnion getFromVoidPointer(void *P) { + return PointerUnion::getFromOpaqueValue(P); } - // The number of bits available are the min of the two pointer types. - enum { - NumLowBitsAvailable = PointerLikeTypeTraits< - typename PointerUnion::ValTy>::NumLowBitsAvailable - }; + // The number of bits available are the min of the pointer types minus the + // bits needed for the discriminator. + static constexpr int NumLowBitsAvailable = PointerLikeTypeTraits::Val)>::NumLowBitsAvailable; }; /// A pointer union of three pointer types. See documentation for PointerUnion /// for usage. -template class PointerUnion3 { -public: - using InnerUnion = PointerUnion; - using ValTy = PointerUnion; - -private: - ValTy Val; - - struct IsInnerUnion { - ValTy Val; - - IsInnerUnion(ValTy val) : Val(val) {} - - template int is() const { - return Val.template is() && - Val.template get().template is(); - } - - template T get() const { - return Val.template get().template get(); - } - }; - - struct IsPT3 { - ValTy Val; - - IsPT3(ValTy val) : Val(val) {} - - template int is() const { return Val.template is(); } - template T get() const { return Val.template get(); } - }; - -public: - PointerUnion3() = default; - PointerUnion3(PT1 V) { Val = InnerUnion(V); } - PointerUnion3(PT2 V) { Val = InnerUnion(V); } - PointerUnion3(PT3 V) { Val = V; } - - /// Test if the pointer held in the union is null, regardless of - /// which type it is. - bool isNull() const { return Val.isNull(); } - explicit operator bool() const { return !isNull(); } - - /// Test if the Union currently holds the type matching T. - template int is() const { - // If T is PT1/PT2 choose IsInnerUnion otherwise choose IsPT3. - using Ty = typename ::llvm::PointerUnionTypeSelector< - PT1, T, IsInnerUnion, - ::llvm::PointerUnionTypeSelector>::Return; - return Ty(Val).template is(); - } - - /// Returns the value of the specified pointer type. - /// - /// If the specified pointer type is incorrect, assert. - template T get() const { - assert(is() && "Invalid accessor called"); - // If T is PT1/PT2 choose IsInnerUnion otherwise choose IsPT3. - using Ty = typename ::llvm::PointerUnionTypeSelector< - PT1, T, IsInnerUnion, - ::llvm::PointerUnionTypeSelector>::Return; - return Ty(Val).template get(); - } - - /// Returns the current pointer if it is of the specified pointer type, - /// otherwises returns null. - template T dyn_cast() const { - if (is()) - return get(); - return T(); - } - - /// Assignment from nullptr which just clears the union. - const PointerUnion3 &operator=(std::nullptr_t) { - Val = nullptr; - return *this; - } - - /// Assignment operators - Allow assigning into this union from either - /// pointer type, setting the discriminator to remember what it came from. - const PointerUnion3 &operator=(const PT1 &RHS) { - Val = InnerUnion(RHS); - return *this; - } - const PointerUnion3 &operator=(const PT2 &RHS) { - Val = InnerUnion(RHS); - return *this; - } - const PointerUnion3 &operator=(const PT3 &RHS) { - Val = RHS; - return *this; - } - - void *getOpaqueValue() const { return Val.getOpaqueValue(); } - static inline PointerUnion3 getFromOpaqueValue(void *VP) { - PointerUnion3 V; - V.Val = ValTy::getFromOpaqueValue(VP); - return V; - } -}; - -// Teach SmallPtrSet that PointerUnion3 is "basically a pointer", that has -// # low bits available = min(PT1bits,PT2bits,PT2bits)-2. template -struct PointerLikeTypeTraits> { - static inline void *getAsVoidPointer(const PointerUnion3 &P) { - return P.getOpaqueValue(); - } - - static inline PointerUnion3 getFromVoidPointer(void *P) { - return PointerUnion3::getFromOpaqueValue(P); - } - - // The number of bits available are the min of the two pointer types. - enum { - NumLowBitsAvailable = PointerLikeTypeTraits< - typename PointerUnion3::ValTy>::NumLowBitsAvailable - }; -}; - -template -bool operator<(PointerUnion3 lhs, - PointerUnion3 rhs) { - return lhs.getOpaqueValue() < rhs.getOpaqueValue(); -} +using PointerUnion3 = PointerUnion; /// A pointer union of four pointer types. See documentation for PointerUnion /// for usage. template -class PointerUnion4 { -public: - using InnerUnion1 = PointerUnion; - using InnerUnion2 = PointerUnion; - using ValTy = PointerUnion; - -private: - ValTy Val; - -public: - PointerUnion4() = default; - PointerUnion4(PT1 V) { Val = InnerUnion1(V); } - PointerUnion4(PT2 V) { Val = InnerUnion1(V); } - PointerUnion4(PT3 V) { Val = InnerUnion2(V); } - PointerUnion4(PT4 V) { Val = InnerUnion2(V); } - - /// Test if the pointer held in the union is null, regardless of - /// which type it is. - bool isNull() const { return Val.isNull(); } - explicit operator bool() const { return !isNull(); } - - /// Test if the Union currently holds the type matching T. - template int is() const { - // If T is PT1/PT2 choose InnerUnion1 otherwise choose InnerUnion2. - using Ty = typename ::llvm::PointerUnionTypeSelector< - PT1, T, InnerUnion1, - ::llvm::PointerUnionTypeSelector>::Return; - return Val.template is() && Val.template get().template is(); - } - - /// Returns the value of the specified pointer type. - /// - /// If the specified pointer type is incorrect, assert. - template T get() const { - assert(is() && "Invalid accessor called"); - // If T is PT1/PT2 choose InnerUnion1 otherwise choose InnerUnion2. - using Ty = typename ::llvm::PointerUnionTypeSelector< - PT1, T, InnerUnion1, - ::llvm::PointerUnionTypeSelector>::Return; - return Val.template get().template get(); - } - - /// Returns the current pointer if it is of the specified pointer type, - /// otherwises returns null. - template T dyn_cast() const { - if (is()) - return get(); - return T(); - } - - /// Assignment from nullptr which just clears the union. - const PointerUnion4 &operator=(std::nullptr_t) { - Val = nullptr; - return *this; - } - - /// Assignment operators - Allow assigning into this union from either - /// pointer type, setting the discriminator to remember what it came from. - const PointerUnion4 &operator=(const PT1 &RHS) { - Val = InnerUnion1(RHS); - return *this; - } - const PointerUnion4 &operator=(const PT2 &RHS) { - Val = InnerUnion1(RHS); - return *this; - } - const PointerUnion4 &operator=(const PT3 &RHS) { - Val = InnerUnion2(RHS); - return *this; - } - const PointerUnion4 &operator=(const PT4 &RHS) { - Val = InnerUnion2(RHS); - return *this; - } - - void *getOpaqueValue() const { return Val.getOpaqueValue(); } - static inline PointerUnion4 getFromOpaqueValue(void *VP) { - PointerUnion4 V; - V.Val = ValTy::getFromOpaqueValue(VP); - return V; - } -}; - -// Teach SmallPtrSet that PointerUnion4 is "basically a pointer", that has -// # low bits available = min(PT1bits,PT2bits,PT2bits)-2. -template -struct PointerLikeTypeTraits> { - static inline void * - getAsVoidPointer(const PointerUnion4 &P) { - return P.getOpaqueValue(); - } - - static inline PointerUnion4 getFromVoidPointer(void *P) { - return PointerUnion4::getFromOpaqueValue(P); - } - - // The number of bits available are the min of the two pointer types. - enum { - NumLowBitsAvailable = PointerLikeTypeTraits< - typename PointerUnion4::ValTy>::NumLowBitsAvailable - }; -}; +using PointerUnion4 = PointerUnion; // Teach DenseMap how to use PointerUnions as keys. -template struct DenseMapInfo> { - using Pair = PointerUnion; - using FirstInfo = DenseMapInfo; - using SecondInfo = DenseMapInfo; +template struct DenseMapInfo> { + using Union = PointerUnion; + using FirstInfo = + DenseMapInfo::type>; - static inline Pair getEmptyKey() { return Pair(FirstInfo::getEmptyKey()); } + static inline Union getEmptyKey() { return Union(FirstInfo::getEmptyKey()); } - static inline Pair getTombstoneKey() { - return Pair(FirstInfo::getTombstoneKey()); + static inline Union getTombstoneKey() { + return Union(FirstInfo::getTombstoneKey()); } - static unsigned getHashValue(const Pair &PairVal) { - intptr_t key = (intptr_t)PairVal.getOpaqueValue(); + static unsigned getHashValue(const Union &UnionVal) { + intptr_t key = (intptr_t)UnionVal.getOpaqueValue(); return DenseMapInfo::getHashValue(key); } - static bool isEqual(const Pair &LHS, const Pair &RHS) { - return LHS.template is() == RHS.template is() && - (LHS.template is() ? FirstInfo::isEqual(LHS.template get(), - RHS.template get()) - : SecondInfo::isEqual(LHS.template get(), - RHS.template get())); + static bool isEqual(const Union &LHS, const Union &RHS) { + return LHS == RHS; } }; Index: llvm/trunk/unittests/ADT/PointerUnionTest.cpp =================================================================== --- llvm/trunk/unittests/ADT/PointerUnionTest.cpp +++ llvm/trunk/unittests/ADT/PointerUnionTest.cpp @@ -68,4 +68,41 @@ EXPECT_EQ(n.get(), (int *)nullptr); } +template struct alignas(8) Aligned {}; + +typedef PointerUnion *, Aligned<1> *, Aligned<2> *, Aligned<3> *, + Aligned<4> *, Aligned<5> *, Aligned<6> *, Aligned<7> *> + PU8; + +TEST_F(PointerUnionTest, ManyElements) { + Aligned<0> a0; + Aligned<7> a7; + + PU8 a = &a0; + EXPECT_TRUE(a.is*>()); + EXPECT_FALSE(a.is*>()); + EXPECT_FALSE(a.is*>()); + EXPECT_FALSE(a.is*>()); + EXPECT_FALSE(a.is*>()); + EXPECT_FALSE(a.is*>()); + EXPECT_FALSE(a.is*>()); + EXPECT_FALSE(a.is*>()); + EXPECT_EQ(a.dyn_cast*>() == &a0); + EXPECT_EQ(*a.getAddrOfPtr1() == &a0); + + a = &a7; + EXPECT_FALSE(a.is*>()); + EXPECT_FALSE(a.is*>()); + EXPECT_FALSE(a.is*>()); + EXPECT_FALSE(a.is*>()); + EXPECT_FALSE(a.is*>()); + EXPECT_FALSE(a.is*>()); + EXPECT_FALSE(a.is*>()); + EXPECT_TRUE(a.is*>()); + EXPECT_EQ(a.dyn_cast*>() == &a7); + + EXPECT_TRUE(a == PU8(&a7)); + EXPECT_TRUE(a != PU8(&a0)); +} + } // end anonymous namespace