Skip to content

Commit a9de444

Browse files
authored
[clang][bytecode][NFC] Use an anonymous union in Pointer (llvm#154405)
So we can save ourselves writing PointeeStorage all the time.
1 parent 27f3283 commit a9de444

File tree

5 files changed

+106
-100
lines changed

5 files changed

+106
-100
lines changed

clang/lib/AST/ByteCode/DynamicAllocator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void DynamicAllocator::cleanup() {
3131
if (B->hasPointers()) {
3232
while (B->Pointers) {
3333
Pointer *Next = B->Pointers->asBlockPointer().Next;
34-
B->Pointers->PointeeStorage.BS.Pointee = nullptr;
34+
B->Pointers->BS.Pointee = nullptr;
3535
B->Pointers = Next;
3636
}
3737
B->Pointers = nullptr;

clang/lib/AST/ByteCode/InterpBlock.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ void Block::addPointer(Pointer *P) {
2323
assert(!hasPointer(P));
2424
#endif
2525
if (Pointers)
26-
Pointers->PointeeStorage.BS.Prev = P;
27-
P->PointeeStorage.BS.Next = Pointers;
28-
P->PointeeStorage.BS.Prev = nullptr;
26+
Pointers->BS.Prev = P;
27+
P->BS.Next = Pointers;
28+
P->BS.Prev = nullptr;
2929
Pointers = P;
3030
#ifndef NDEBUG
3131
assert(hasPointer(P));
@@ -40,16 +40,16 @@ void Block::removePointer(Pointer *P) {
4040
assert(hasPointer(P));
4141
#endif
4242

43-
BlockPointer &BP = P->PointeeStorage.BS;
43+
BlockPointer &BP = P->BS;
4444

4545
if (Pointers == P)
4646
Pointers = BP.Next;
4747

4848
if (BP.Prev)
49-
BP.Prev->PointeeStorage.BS.Next = BP.Next;
49+
BP.Prev->BS.Next = BP.Next;
5050
if (BP.Next)
51-
BP.Next->PointeeStorage.BS.Prev = BP.Prev;
52-
P->PointeeStorage.BS.Pointee = nullptr;
51+
BP.Next->BS.Prev = BP.Prev;
52+
P->BS.Pointee = nullptr;
5353
#ifndef NDEBUG
5454
assert(!hasPointer(P));
5555
#endif
@@ -70,13 +70,13 @@ void Block::replacePointer(Pointer *Old, Pointer *New) {
7070
assert(hasPointer(Old));
7171
#endif
7272

73-
BlockPointer &OldBP = Old->PointeeStorage.BS;
74-
BlockPointer &NewBP = New->PointeeStorage.BS;
73+
BlockPointer &OldBP = Old->BS;
74+
BlockPointer &NewBP = New->BS;
7575

7676
if (OldBP.Prev)
77-
OldBP.Prev->PointeeStorage.BS.Next = New;
77+
OldBP.Prev->BS.Next = New;
7878
if (OldBP.Next)
79-
OldBP.Next->PointeeStorage.BS.Prev = New;
79+
OldBP.Next->BS.Prev = New;
8080
NewBP.Prev = OldBP.Prev;
8181
NewBP.Next = OldBP.Next;
8282
if (Pointers == Old)
@@ -116,7 +116,7 @@ DeadBlock::DeadBlock(DeadBlock *&Root, Block *Blk)
116116
// Transfer pointers.
117117
B.Pointers = Blk->Pointers;
118118
for (Pointer *P = Blk->Pointers; P; P = P->asBlockPointer().Next)
119-
P->PointeeStorage.BS.Pointee = &B;
119+
P->BS.Pointee = &B;
120120
Blk->Pointers = nullptr;
121121
}
122122

clang/lib/AST/ByteCode/Pointer.cpp

Lines changed: 76 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,62 @@ Pointer::Pointer(Block *Pointee)
3030
Pointer::Pointer(Block *Pointee, uint64_t BaseAndOffset)
3131
: Pointer(Pointee, BaseAndOffset, BaseAndOffset) {}
3232

33-
Pointer::Pointer(const Pointer &P)
34-
: Offset(P.Offset), StorageKind(P.StorageKind),
35-
PointeeStorage(P.PointeeStorage) {
36-
37-
if (isBlockPointer() && PointeeStorage.BS.Pointee)
38-
PointeeStorage.BS.Pointee->addPointer(this);
39-
}
40-
4133
Pointer::Pointer(Block *Pointee, unsigned Base, uint64_t Offset)
4234
: Offset(Offset), StorageKind(Storage::Block) {
4335
assert((Base == RootPtrMark || Base % alignof(void *) == 0) && "wrong base");
4436

45-
PointeeStorage.BS = {Pointee, Base, nullptr, nullptr};
37+
BS = {Pointee, Base, nullptr, nullptr};
4638

4739
if (Pointee)
4840
Pointee->addPointer(this);
4941
}
5042

51-
Pointer::Pointer(Pointer &&P)
52-
: Offset(P.Offset), StorageKind(P.StorageKind),
53-
PointeeStorage(P.PointeeStorage) {
43+
Pointer::Pointer(const Pointer &P)
44+
: Offset(P.Offset), StorageKind(P.StorageKind) {
45+
switch (StorageKind) {
46+
case Storage::Int:
47+
Int = P.Int;
48+
break;
49+
case Storage::Block:
50+
BS = P.BS;
51+
if (BS.Pointee)
52+
BS.Pointee->addPointer(this);
53+
break;
54+
case Storage::Fn:
55+
Fn = P.Fn;
56+
break;
57+
case Storage::Typeid:
58+
Typeid = P.Typeid;
59+
break;
60+
}
61+
}
5462

55-
if (StorageKind == Storage::Block && PointeeStorage.BS.Pointee)
56-
PointeeStorage.BS.Pointee->replacePointer(&P, this);
63+
Pointer::Pointer(Pointer &&P) : Offset(P.Offset), StorageKind(P.StorageKind) {
64+
switch (StorageKind) {
65+
case Storage::Int:
66+
Int = P.Int;
67+
break;
68+
case Storage::Block:
69+
BS = P.BS;
70+
if (BS.Pointee)
71+
BS.Pointee->replacePointer(&P, this);
72+
break;
73+
case Storage::Fn:
74+
Fn = P.Fn;
75+
break;
76+
case Storage::Typeid:
77+
Typeid = P.Typeid;
78+
break;
79+
}
5780
}
5881

5982
Pointer::~Pointer() {
6083
if (!isBlockPointer())
6184
return;
6285

63-
if (Block *Pointee = PointeeStorage.BS.Pointee) {
86+
if (Block *Pointee = BS.Pointee) {
6487
Pointee->removePointer(this);
65-
PointeeStorage.BS.Pointee = nullptr;
88+
BS.Pointee = nullptr;
6689
Pointee->cleanup();
6790
}
6891
}
@@ -73,13 +96,13 @@ Pointer &Pointer::operator=(const Pointer &P) {
7396
if (isBlockPointer()) {
7497
if (P.isBlockPointer() && this->block() == P.block()) {
7598
Offset = P.Offset;
76-
PointeeStorage.BS.Base = P.PointeeStorage.BS.Base;
99+
BS.Base = P.BS.Base;
77100
return *this;
78101
}
79102

80-
if (Block *Pointee = PointeeStorage.BS.Pointee) {
103+
if (Block *Pointee = BS.Pointee) {
81104
Pointee->removePointer(this);
82-
PointeeStorage.BS.Pointee = nullptr;
105+
BS.Pointee = nullptr;
83106
Pointee->cleanup();
84107
}
85108
}
@@ -88,16 +111,16 @@ Pointer &Pointer::operator=(const Pointer &P) {
88111
Offset = P.Offset;
89112

90113
if (P.isBlockPointer()) {
91-
PointeeStorage.BS = P.PointeeStorage.BS;
114+
BS = P.BS;
92115

93-
if (PointeeStorage.BS.Pointee)
94-
PointeeStorage.BS.Pointee->addPointer(this);
116+
if (BS.Pointee)
117+
BS.Pointee->addPointer(this);
95118
} else if (P.isIntegralPointer()) {
96-
PointeeStorage.Int = P.PointeeStorage.Int;
119+
Int = P.Int;
97120
} else if (P.isFunctionPointer()) {
98-
PointeeStorage.Fn = P.PointeeStorage.Fn;
121+
Fn = P.Fn;
99122
} else if (P.isTypeidPointer()) {
100-
PointeeStorage.Typeid = P.PointeeStorage.Typeid;
123+
Typeid = P.Typeid;
101124
} else {
102125
assert(false && "Unhandled storage kind");
103126
}
@@ -110,13 +133,13 @@ Pointer &Pointer::operator=(Pointer &&P) {
110133
if (isBlockPointer()) {
111134
if (P.isBlockPointer() && this->block() == P.block()) {
112135
Offset = P.Offset;
113-
PointeeStorage.BS.Base = P.PointeeStorage.BS.Base;
136+
BS.Base = P.BS.Base;
114137
return *this;
115138
}
116139

117-
if (Block *Pointee = PointeeStorage.BS.Pointee) {
140+
if (Block *Pointee = BS.Pointee) {
118141
Pointee->removePointer(this);
119-
PointeeStorage.BS.Pointee = nullptr;
142+
BS.Pointee = nullptr;
120143
Pointee->cleanup();
121144
}
122145
}
@@ -125,16 +148,16 @@ Pointer &Pointer::operator=(Pointer &&P) {
125148
Offset = P.Offset;
126149

127150
if (P.isBlockPointer()) {
128-
PointeeStorage.BS = P.PointeeStorage.BS;
151+
BS = P.BS;
129152

130-
if (PointeeStorage.BS.Pointee)
131-
PointeeStorage.BS.Pointee->addPointer(this);
153+
if (BS.Pointee)
154+
BS.Pointee->addPointer(this);
132155
} else if (P.isIntegralPointer()) {
133-
PointeeStorage.Int = P.PointeeStorage.Int;
156+
Int = P.Int;
134157
} else if (P.isFunctionPointer()) {
135-
PointeeStorage.Fn = P.PointeeStorage.Fn;
158+
Fn = P.Fn;
136159
} else if (P.isTypeidPointer()) {
137-
PointeeStorage.Typeid = P.PointeeStorage.Typeid;
160+
Typeid = P.Typeid;
138161
} else {
139162
assert(false && "Unhandled storage kind");
140163
}
@@ -163,12 +186,11 @@ APValue Pointer::toAPValue(const ASTContext &ASTCtx) const {
163186
}
164187

165188
if (isTypeidPointer()) {
166-
TypeInfoLValue TypeInfo(PointeeStorage.Typeid.TypePtr);
167-
return APValue(
168-
APValue::LValueBase::getTypeInfo(
169-
TypeInfo, QualType(PointeeStorage.Typeid.TypeInfoType, 0)),
170-
CharUnits::Zero(), {},
171-
/*OnePastTheEnd=*/false, /*IsNull=*/false);
189+
TypeInfoLValue TypeInfo(Typeid.TypePtr);
190+
return APValue(APValue::LValueBase::getTypeInfo(
191+
TypeInfo, QualType(Typeid.TypeInfoType, 0)),
192+
CharUnits::Zero(), {},
193+
/*OnePastTheEnd=*/false, /*IsNull=*/false);
172194
}
173195

174196
// Build the lvalue base from the block.
@@ -300,13 +322,13 @@ APValue Pointer::toAPValue(const ASTContext &ASTCtx) const {
300322
void Pointer::print(llvm::raw_ostream &OS) const {
301323
switch (StorageKind) {
302324
case Storage::Block: {
303-
const Block *B = PointeeStorage.BS.Pointee;
325+
const Block *B = BS.Pointee;
304326
OS << "(Block) " << B << " {";
305327

306328
if (isRoot())
307-
OS << "rootptr(" << PointeeStorage.BS.Base << "), ";
329+
OS << "rootptr(" << BS.Base << "), ";
308330
else
309-
OS << PointeeStorage.BS.Base << ", ";
331+
OS << BS.Base << ", ";
310332

311333
if (isElementPastEnd())
312334
OS << "pastend, ";
@@ -321,8 +343,7 @@ void Pointer::print(llvm::raw_ostream &OS) const {
321343
} break;
322344
case Storage::Int:
323345
OS << "(Int) {";
324-
OS << PointeeStorage.Int.Value << " + " << Offset << ", "
325-
<< PointeeStorage.Int.Desc;
346+
OS << Int.Value << " + " << Offset << ", " << Int.Desc;
326347
OS << "}";
327348
break;
328349
case Storage::Fn:
@@ -412,18 +433,17 @@ bool Pointer::isInitialized() const {
412433
if (!isBlockPointer())
413434
return true;
414435

415-
if (isRoot() && PointeeStorage.BS.Base == sizeof(GlobalInlineDescriptor)) {
436+
if (isRoot() && BS.Base == sizeof(GlobalInlineDescriptor)) {
416437
const GlobalInlineDescriptor &GD =
417438
*reinterpret_cast<const GlobalInlineDescriptor *>(block()->rawData());
418439
return GD.InitState == GlobalInitState::Initialized;
419440
}
420441

421-
assert(PointeeStorage.BS.Pointee &&
422-
"Cannot check if null pointer was initialized");
442+
assert(BS.Pointee && "Cannot check if null pointer was initialized");
423443
const Descriptor *Desc = getFieldDesc();
424444
assert(Desc);
425445
if (Desc->isPrimitiveArray()) {
426-
if (isStatic() && PointeeStorage.BS.Base == 0)
446+
if (isStatic() && BS.Base == 0)
427447
return true;
428448

429449
InitMapPtr &IM = getInitMap();
@@ -448,9 +468,9 @@ void Pointer::initialize() const {
448468
if (!isBlockPointer())
449469
return;
450470

451-
assert(PointeeStorage.BS.Pointee && "Cannot initialize null pointer");
471+
assert(BS.Pointee && "Cannot initialize null pointer");
452472

453-
if (isRoot() && PointeeStorage.BS.Base == sizeof(GlobalInlineDescriptor)) {
473+
if (isRoot() && BS.Base == sizeof(GlobalInlineDescriptor)) {
454474
GlobalInlineDescriptor &GD = *reinterpret_cast<GlobalInlineDescriptor *>(
455475
asBlockPointer().Pointee->rawData());
456476
GD.InitState = GlobalInitState::Initialized;
@@ -461,7 +481,7 @@ void Pointer::initialize() const {
461481
assert(Desc);
462482
if (Desc->isPrimitiveArray()) {
463483
// Primitive global arrays don't have an initmap.
464-
if (isStatic() && PointeeStorage.BS.Base == 0)
484+
if (isStatic() && BS.Base == 0)
465485
return;
466486

467487
// Nothing to do for these.
@@ -487,8 +507,7 @@ void Pointer::initialize() const {
487507
}
488508

489509
// Field has its bit in an inline descriptor.
490-
assert(PointeeStorage.BS.Base != 0 &&
491-
"Only composite fields can be initialised");
510+
assert(BS.Base != 0 && "Only composite fields can be initialised");
492511
getInlineDesc()->IsInitialized = true;
493512
}
494513

@@ -507,10 +526,9 @@ void Pointer::initializeAllElements() const {
507526

508527
void Pointer::activate() const {
509528
// Field has its bit in an inline descriptor.
510-
assert(PointeeStorage.BS.Base != 0 &&
511-
"Only composite fields can be activated");
529+
assert(BS.Base != 0 && "Only composite fields can be activated");
512530

513-
if (isRoot() && PointeeStorage.BS.Base == sizeof(GlobalInlineDescriptor))
531+
if (isRoot() && BS.Base == sizeof(GlobalInlineDescriptor))
514532
return;
515533
if (!getInlineDesc()->InUnion)
516534
return;
@@ -592,8 +610,7 @@ bool Pointer::pointToSameBlock(const Pointer &A, const Pointer &B) {
592610
}
593611

594612
bool Pointer::hasSameArray(const Pointer &A, const Pointer &B) {
595-
return hasSameBase(A, B) &&
596-
A.PointeeStorage.BS.Base == B.PointeeStorage.BS.Base &&
613+
return hasSameBase(A, B) && A.BS.Base == B.BS.Base &&
597614
A.getFieldDesc()->IsArray;
598615
}
599616

0 commit comments

Comments
 (0)