llvm.org GIT mirror llvm / 560cdb1
[ADT] Add a sum type abstraction for pointer-like types. This is a much more general and powerful form of PointerUnion. It provides a reasonably complete sum type (from type theory) for pointer-like types. It has several significant advantages over the existing PointerUnion infrastructure: 1) It allows more than two pointer types to participate without awkward nesting structures. 2) It directly exposes the tag so that it is convenient to write switches over the possible members. 3) It can re-use the same type for multiple tag values, something that has been worked around by either abusing PointerIntPair or defining nonce types and doing unsafe pointer casting. 4) It supports customization of the PointerLikeTypeTraits used for specific member types. This means it could (in theory) be used even with types that are over-aligned on allocation to expose larger numbers of bits to the tag. All in all, I think it is at least complimentary to the existing infrastructure, and a strict improvement for some use cases. Differential Revision: http://reviews.llvm.org/D15843 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@257282 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 3 years ago
3 changed file(s) with 319 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===- llvm/ADT/PointerSumType.h --------------------------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_ADT_POINTERSUMTYPE_H
10 #define LLVM_ADT_POINTERSUMTYPE_H
11
12 #include "llvm/ADT/DenseMapInfo.h"
13 #include "llvm/Support/Compiler.h"
14 #include "llvm/Support/PointerLikeTypeTraits.h"
15
16 namespace llvm {
17
18 /// A compile time pair of an integer tag and the pointer-like type which it
19 /// indexes within a sum type. Also allows the user to specify a particular
20 /// traits class for pointer types with custom behavior such as over-aligned
21 /// allocation.
22 template
23 typename TraitsArgT = PointerLikeTypeTraits>
24 struct PointerSumTypeMember {
25 enum { Tag = N };
26 typedef PointerArgT PointerT;
27 typedef TraitsArgT TraitsT;
28 };
29
30 namespace detail {
31
32 template
33 struct PointerSumTypeHelper;
34
35 }
36
37 /// A sum type over pointer-like types.
38 ///
39 /// This is a normal tagged union across pointer-like types that uses the low
40 /// bits of the pointers to store the tag.
41 ///
42 /// Each member of the sum type is specified by passing a \c
43 /// PointerSumTypeMember specialization in the variadic member argument list.
44 /// This allows the user to control the particular tag value associated with
45 /// a particular type, use the same type for multiple different tags, and
46 /// customize the pointer-like traits used for a particular member. Note that
47 /// these *must* be specializations of \c PointerSumTypeMember, no other type
48 /// will suffice, even if it provides a compatible interface.
49 ///
50 /// This type implements all of the comparison operators and even hash table
51 /// support by comparing the underlying storage of the pointer values. It
52 /// doesn't support delegating to particular members for comparisons.
53 ///
54 /// It also default constructs to a zero tag with a null pointer, whatever that
55 /// would be. This means that the zero value for the tag type is significant
56 /// and may be desireable to set to a state that is particularly desirable to
57 /// default construct.
58 ///
59 /// There is no support for constructing or accessing with a dynamic tag as
60 /// that would fundamentally violate the type safety provided by the sum type.
61 template class PointerSumType {
62 uintptr_t Value;
63
64 typedef detail::PointerSumTypeHelper HelperT;
65
66 public:
67 PointerSumType() : Value(0) {}
68
69 /// A typed constructor for a specific tagged member of the sum type.
70 template
71 static PointerSumType
72 create(typename HelperT::template Lookup::PointerT Pointer) {
73 PointerSumType Result;
74 void *V = HelperT::template Lookup::TraitsT::getAsVoidPointer(Pointer);
75 assert((reinterpret_cast(V) & HelperT::TagMask) == 0 &&
76 "Pointer is insufficiently aligned to store the discriminant!");
77 Result.Value = reinterpret_cast(V) | N;
78 return Result;
79 }
80
81 TagT getTag() const { return static_cast(Value & HelperT::TagMask); }
82
83 template bool is() const { return N == getTag(); }
84
85 template typename HelperT::template Lookup::PointerT get() const {
86 void *P = is() ? getImpl() : nullptr;
87 return HelperT::template Lookup::TraitsT::getFromVoidPointer(P);
88 }
89
90 template
91 typename HelperT::template Lookup::PointerT cast() const {
92 assert(is() && "This instance has a different active member.");
93 return HelperT::template Lookup::TraitsT::getFromVoidPointer(getImpl());
94 }
95
96 operator bool() const { return Value & HelperT::PointerMask; }
97 bool operator==(const PointerSumType &R) const { return Value == R.Value; }
98 bool operator!=(const PointerSumType &R) const { return Value != R.Value; }
99 bool operator<(const PointerSumType &R) const { return Value < R.Value; }
100 bool operator>(const PointerSumType &R) const { return Value > R.Value; }
101 bool operator<=(const PointerSumType &R) const { return Value <= R.Value; }
102 bool operator>=(const PointerSumType &R) const { return Value >= R.Value; }
103
104 uintptr_t getOpaqueValue() const { return Value; }
105
106 protected:
107 void *getImpl() const {
108 return reinterpret_cast(Value & HelperT::PointerMask);
109 }
110 };
111
112 namespace detail {
113
114 /// A helper template for implementing \c PointerSumType. It provides fast
115 /// compile-time lookup of the member from a particular tag value, along with
116 /// useful constants and compile time checking infrastructure..
117 template
118 struct PointerSumTypeHelper : MemberTs... {
119 // First we use a trick to allow quickly looking up information about
120 // a particular member of the sum type. This works because we arranged to
121 // have this type derive from all of the member type templates. We can select
122 // the matching member for a tag using type deduction during overload
123 // resolution.
124 template
125 static PointerSumTypeMember
126 LookupOverload(PointerSumTypeMember *);
127 template static void LookupOverload(...);
128 template struct Lookup {
129 // Compute a particular member type by resolving the lookup helper ovorload.
130 typedef decltype(LookupOverload(
131 static_cast(nullptr))) MemberT;
132
133 /// The Nth member's pointer type.
134 typedef typename MemberT::PointerT PointerT;
135
136 /// The Nth member's traits type.
137 typedef typename MemberT::TraitsT TraitsT;
138 };
139
140 // Next we need to compute the number of bits available for the discriminant
141 // by taking the min of the bits available for each member. Much of this
142 // would be amazingly easier with good constexpr support.
143 template
144 struct Min : std::integral_constant<
145 uintptr_t, (V < Min::value ? V : Min::value)> {
146 };
147 template
148 struct Min : std::integral_constant {};
149 enum { NumTagBits = Min::value };
150
151 // Also compute the smallest discriminant and various masks for convenience.
152 enum : uint64_t {
153 MinTag = Min::value,
154 PointerMask = static_cast(-1) << NumTagBits,
155 TagMask = ~PointerMask
156 };
157
158 // Finally we need a recursive template to do static checks of each
159 // member.
160 template
161 struct Checker : Checker {
162 static_assert(MemberT::Tag < (1 << NumTagBits),
163 "This discriminant value requires too many bits!");
164 };
165 template struct Checker : std::true_type {
166 static_assert(MemberT::Tag < (1 << NumTagBits),
167 "This discriminant value requires too many bits!");
168 };
169 static_assert(Checker::value,
170 "Each member must pass the checker.");
171 };
172
173 }
174
175 // Teach DenseMap how to use PointerSumTypes as keys.
176 template
177 struct DenseMapInfo> {
178 typedef PointerSumType SumType;
179
180 typedef detail::PointerSumTypeHelper HelperT;
181 enum { SomeTag = HelperT::MinTag };
182 typedef typename HelperT::template Lookup::PointerT
183 SomePointerT;
184 typedef DenseMapInfo SomePointerInfo;
185
186 static inline SumType getEmptyKey() {
187 return SumType::create(SomePointerInfo::getEmptyKey());
188 }
189 static inline SumType getTombstoneKey() {
190 return SumType::create(
191 SomePointerInfo::getTombstoneKey());
192 }
193 static unsigned getHashValue(const SumType &Arg) {
194 uintptr_t OpaqueValue = Arg.getOpaqueValue();
195 return DenseMapInfo::getHashValue(OpaqueValue);
196 }
197 static bool isEqual(const SumType &LHS, const SumType &RHS) {
198 return LHS == RHS;
199 }
200 };
201
202 }
203
204 #endif
2525 OptionalTest.cpp
2626 PackedVectorTest.cpp
2727 PointerIntPairTest.cpp
28 PointerSumTypeTest.cpp
2829 PointerUnionTest.cpp
2930 PostOrderIteratorTest.cpp
3031 RangeAdapterTest.cpp
0 //===- llvm/unittest/ADT/PointerSumTypeTest.cpp ---------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "gtest/gtest.h"
10 #include "llvm/ADT/PointerSumType.h"
11 using namespace llvm;
12
13 namespace {
14
15 struct PointerSumTypeTest : public testing::Test {
16 enum Kinds { Float, Int1, Int2 };
17 float f;
18 int i1, i2;
19
20 typedef PointerSumType,
21 PointerSumTypeMember,
22 PointerSumTypeMember>
23 SumType;
24 SumType a, b, c, n;
25
26 PointerSumTypeTest()
27 : f(3.14f), i1(42), i2(-1), a(SumType::create(&f)),
28 b(SumType::create(&i1)), c(SumType::create(&i2)), n() {}
29 };
30
31 TEST_F(PointerSumTypeTest, NullTest) {
32 EXPECT_TRUE(a);
33 EXPECT_TRUE(b);
34 EXPECT_TRUE(c);
35 EXPECT_FALSE(n);
36 }
37
38 TEST_F(PointerSumTypeTest, GetTag) {
39 EXPECT_EQ(Float, a.getTag());
40 EXPECT_EQ(Int1, b.getTag());
41 EXPECT_EQ(Int2, c.getTag());
42 EXPECT_EQ((Kinds)0, n.getTag());
43 }
44
45 TEST_F(PointerSumTypeTest, Is) {
46 EXPECT_TRUE(a.is());
47 EXPECT_FALSE(a.is());
48 EXPECT_FALSE(a.is());
49 EXPECT_FALSE(b.is());
50 EXPECT_TRUE(b.is());
51 EXPECT_FALSE(b.is());
52 EXPECT_FALSE(c.is());
53 EXPECT_FALSE(c.is());
54 EXPECT_TRUE(c.is());
55 }
56
57 TEST_F(PointerSumTypeTest, Get) {
58 EXPECT_EQ(&f, a.get());
59 EXPECT_EQ(nullptr, a.get());
60 EXPECT_EQ(nullptr, a.get());
61 EXPECT_EQ(nullptr, b.get());
62 EXPECT_EQ(&i1, b.get());
63 EXPECT_EQ(nullptr, b.get());
64 EXPECT_EQ(nullptr, c.get());
65 EXPECT_EQ(nullptr, c.get());
66 EXPECT_EQ(&i2, c.get());
67
68 // Note that we can use .get even on a null sum type. It just always produces
69 // a null pointer, even if one of the discriminants is null.
70 EXPECT_EQ(nullptr, n.get());
71 EXPECT_EQ(nullptr, n.get());
72 EXPECT_EQ(nullptr, n.get());
73 }
74
75 TEST_F(PointerSumTypeTest, Cast) {
76 EXPECT_EQ(&f, a.cast());
77 EXPECT_EQ(&i1, b.cast());
78 EXPECT_EQ(&i2, c.cast());
79 }
80
81 TEST_F(PointerSumTypeTest, Assignment) {
82 b = SumType::create(&i2);
83 EXPECT_EQ(nullptr, b.get());
84 EXPECT_EQ(nullptr, b.get());
85 EXPECT_EQ(&i2, b.get());
86
87 b = SumType::create(&i1);
88 EXPECT_EQ(nullptr, b.get());
89 EXPECT_EQ(nullptr, b.get());
90 EXPECT_EQ(&i1, b.get());
91
92 float Local = 1.616f;
93 b = SumType::create(&Local);
94 EXPECT_EQ(&Local, b.get());
95 EXPECT_EQ(nullptr, b.get());
96 EXPECT_EQ(nullptr, b.get());
97
98 n = SumType::create(&i2);
99 EXPECT_TRUE(n);
100 EXPECT_EQ(nullptr, n.get());
101 EXPECT_EQ(&i2, n.get());
102 EXPECT_EQ(nullptr, n.get());
103
104 n = SumType::create(nullptr);
105 EXPECT_FALSE(n);
106 EXPECT_EQ(nullptr, n.get());
107 EXPECT_EQ(nullptr, n.get());
108 EXPECT_EQ(nullptr, n.get());
109 }
110
111
112 } // end anonymous namespace