LLVM 24.0.0git
UDTLayout.cpp
Go to the documentation of this file.
1//===- UDTLayout.cpp ------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "llvm/ADT/ArrayRef.h"
11#include "llvm/ADT/BitVector.h"
12#include "llvm/ADT/STLExtras.h"
26#include <algorithm>
27#include <cassert>
28#include <cstdint>
29#include <memory>
30
31using namespace llvm;
32using namespace llvm::pdb;
33
34static std::unique_ptr<PDBSymbol> getSymbolType(const PDBSymbol &Symbol) {
35 const IPDBSession &Session = Symbol.getSession();
36 const IPDBRawSymbol &RawSymbol = Symbol.getRawSymbol();
37 uint32_t TypeId = RawSymbol.getTypeId();
38 return Session.getSymbolById(TypeId);
39}
40
41static uint32_t getTypeLength(const PDBSymbol &Symbol) {
42 auto SymbolType = getSymbolType(Symbol);
43 const IPDBRawSymbol &RawType = SymbolType->getRawSymbol();
44
45 return RawType.getLength();
46}
47
57
59 return UsedBytes.size() - UsedBytes.count();
60}
61
63 int Last = UsedBytes.find_last();
64
65 return UsedBytes.size() - (Last + 1);
66}
67
69 const UDTLayoutBase &Parent, std::unique_ptr<PDBSymbolData> Member)
72 DataMember(std::move(Member)) {
73 auto Type = DataMember->getType();
75 UdtLayout = std::make_unique<ClassLayout>(std::move(UDT));
76 UsedBytes = UdtLayout->usedBytes();
77 }
78}
79
81 std::unique_ptr<PDBSymbolTypeBuiltin> Sym,
83 : LayoutItemBase(&Parent, Sym.get(), "<vbptr>", Offset, Size, false),
84 Type(std::move(Sym)) {
85}
86
90
91bool DataMemberLayoutItem::hasUDTLayout() const { return UdtLayout != nullptr; }
92
94 return *UdtLayout;
95}
96
98 std::unique_ptr<PDBSymbolTypeVTable> VT)
99 : LayoutItemBase(&Parent, VT.get(), "<vtbl>", 0, getTypeLength(*VT), false),
100 VTable(std::move(VT)) {
101 auto VTableType = cast<PDBSymbolTypePointer>(VTable->getType());
102 ElementSize = VTableType->getLength();
103}
104
106 const std::string &Name, uint32_t OffsetInParent,
107 uint32_t Size, bool IsElided)
109 // UDT storage comes from a union of all the children's storage, so start out
110 // uninitialized.
111 UsedBytes.reset(0, Size);
112
114 if (LayoutSize < Size)
115 UsedBytes.resize(LayoutSize);
116}
117
120 if (!LayoutItems.empty()) {
121 const LayoutItemBase *Back = LayoutItems.back();
122 uint32_t ChildPadding = Back->LayoutItemBase::tailPadding();
123 if (Abs < ChildPadding)
124 Abs = 0;
125 else
126 Abs -= ChildPadding;
127 }
128 return Abs;
129}
130
132 : UDTLayoutBase(nullptr, UDT, UDT.getName(), 0, UDT.getLength(), false),
133 UDT(UDT) {
134 ImmediateUsedBytes.resize(SizeOf, false);
135 for (auto &LI : LayoutItems) {
136 uint32_t Begin = LI->getOffsetInParent();
137 uint32_t End = Begin + LI->getLayoutSize();
138 End = std::min(SizeOf, End);
139 ImmediateUsedBytes.set(Begin, End);
140 }
141}
142
143ClassLayout::ClassLayout(std::unique_ptr<PDBSymbolTypeUDT> UDT)
144 : ClassLayout(*UDT) {
145 OwnedStorage = std::move(UDT);
146}
147
149 return SizeOf - ImmediateUsedBytes.count();
150}
151
153 uint32_t OffsetInParent, bool Elide,
154 std::unique_ptr<PDBSymbolTypeBaseClass> B)
155 : UDTLayoutBase(&Parent, *B, B->getName(), OffsetInParent, B->getLength(),
156 Elide),
157 Base(std::move(B)) {
158 if (isEmptyBase()) {
159 // Special case an empty base so that it doesn't get treated as padding.
160 UsedBytes.resize(1);
161 UsedBytes.set(0);
162 }
163 IsVirtualBase = Base->isVirtualBaseClass();
164}
165
167 // Handled bases first, followed by VTables, followed by data members,
168 // followed by functions, followed by other. This ordering is necessary
169 // so that bases and vtables get initialized before any functions which
170 // may override them.
171 UniquePtrVector<PDBSymbolTypeBaseClass> Bases;
172 UniquePtrVector<PDBSymbolTypeVTable> VTables;
173 UniquePtrVector<PDBSymbolData> Members;
174 UniquePtrVector<PDBSymbolTypeBaseClass> VirtualBaseSyms;
175
176 auto Children = Sym.findAllChildren();
177 while (auto Child = Children->getNext()) {
179 if (Base->isVirtualBaseClass())
180 VirtualBaseSyms.push_back(std::move(Base));
181 else
182 Bases.push_back(std::move(Base));
183 }
184 else if (auto Data = unique_dyn_cast<PDBSymbolData>(Child)) {
185 if (Data->getDataKind() == PDB_DataKind::Member)
186 Members.push_back(std::move(Data));
187 else
188 Other.push_back(std::move(Data));
189 } else if (auto VT = unique_dyn_cast<PDBSymbolTypeVTable>(Child))
190 VTables.push_back(std::move(VT));
191 else if (auto Func = unique_dyn_cast<PDBSymbolFunc>(Child))
192 Funcs.push_back(std::move(Func));
193 else {
194 Other.push_back(std::move(Child));
195 }
196 }
197
198 // We don't want to have any re-allocations in the list of bases, so make
199 // sure to reserve enough space so that our ArrayRefs don't get invalidated.
200 AllBases.reserve(Bases.size() + VirtualBaseSyms.size());
201
202 // Only add non-virtual bases to the class first. Only at the end of the
203 // class, after all non-virtual bases and data members have been added do we
204 // add virtual bases. This way the offsets are correctly aligned when we go
205 // to lay out virtual bases.
206 for (auto &Base : Bases) {
207 uint32_t Offset = Base->getOffset();
208 // Non-virtual bases never get elided.
209 auto BL = std::make_unique<BaseClassLayout>(*this, Offset, false,
210 std::move(Base));
211
212 AllBases.push_back(BL.get());
213 addChildToLayout(std::move(BL));
214 }
216
217 assert(VTables.size() <= 1);
218 if (!VTables.empty()) {
219 auto VTLayout =
220 std::make_unique<VTableLayoutItem>(*this, std::move(VTables[0]));
221
222 VTable = VTLayout.get();
223
224 addChildToLayout(std::move(VTLayout));
225 }
226
227 for (auto &Data : Members) {
228 auto DM = std::make_unique<DataMemberLayoutItem>(*this, std::move(Data));
229
230 addChildToLayout(std::move(DM));
231 }
232
233 // Make sure add virtual bases before adding functions, since functions may be
234 // overrides of virtual functions declared in a virtual base, so the VTables
235 // and virtual intros need to be correctly initialized.
236 for (auto &VB : VirtualBaseSyms) {
237 int VBPO = VB->getVirtualBasePointerOffset();
238 if (!hasVBPtrAtOffset(VBPO)) {
239 if (auto VBP = VB->getRawSymbol().getVirtualBaseTableType()) {
240 auto VBPL = std::make_unique<VBPtrLayoutItem>(*this, std::move(VBP),
241 VBPO, VBP->getLength());
242 VBPtr = VBPL.get();
243 addChildToLayout(std::move(VBPL));
244 }
245 }
246
247 // Virtual bases always go at the end. So just look for the last place we
248 // ended when writing something, and put our virtual base there.
249 // Note that virtual bases get elided unless this is a top-most derived
250 // class.
251 uint32_t Offset = UsedBytes.find_last() + 1;
252 bool Elide = (Parent != nullptr);
253 auto BL =
254 std::make_unique<BaseClassLayout>(*this, Offset, Elide, std::move(VB));
255 AllBases.push_back(BL.get());
256
257 // Only lay this virtual base out directly inside of *this* class if this
258 // is a top-most derived class. Keep track of it regardless, but only
259 // physically lay it out if it's a topmost derived class.
260 addChildToLayout(std::move(BL));
261 }
262 VirtualBases = ArrayRef(AllBases).drop_front(NonVirtualBases.size());
263
264 if (Parent != nullptr)
265 LayoutSize = UsedBytes.find_last() + 1;
266}
267
269 if (VBPtr && VBPtr->getOffsetInParent() == Off)
270 return true;
271 for (BaseClassLayout *BL : AllBases) {
272 if (BL->hasVBPtrAtOffset(Off - BL->getOffsetInParent()))
273 return true;
274 }
275 return false;
276}
277
278void UDTLayoutBase::addChildToLayout(std::unique_ptr<LayoutItemBase> Child) {
279 uint32_t Begin = Child->getOffsetInParent();
280
281 if (!Child->isElided()) {
282 BitVector ChildBytes = Child->usedBytes();
283
284 // Suppose the child occupies 4 bytes starting at offset 12 in a 32 byte
285 // class. When we call ChildBytes.resize(32), the Child's storage will
286 // still begin at offset 0, so we need to shift it left by offset bytes
287 // to get it into the right position.
288 ChildBytes.resize(UsedBytes.size());
289 ChildBytes <<= Child->getOffsetInParent();
290 UsedBytes |= ChildBytes;
291
292 if (ChildBytes.count() > 0) {
293 auto Loc = llvm::upper_bound(
294 LayoutItems, Begin, [](uint32_t Off, const LayoutItemBase *Item) {
295 return (Off < Item->getOffsetInParent());
296 });
297
298 LayoutItems.insert(Loc, Child.get());
299 }
300 }
301
302 ChildStorage.push_back(std::move(Child));
303}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements the BitVector class.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static RegisterPass< DebugifyModulePass > DM("debugify", "Attach debug info to everything")
This file contains some templates that are useful if you are working with the STL at all.
static uint32_t getTypeLength(const PDBSymbol &Symbol)
Definition UDTLayout.cpp:41
static std::unique_ptr< PDBSymbol > getSymbolType(const PDBSymbol &Symbol)
Definition UDTLayout.cpp:34
void resize(unsigned N, bool t=false)
Grow or shrink the bitvector.
Definition BitVector.h:355
size_type count() const
Returns the number of bits which are set.
Definition BitVector.h:181
LLVM_ABI BaseClassLayout(const UDTLayoutBase &Parent, uint32_t OffsetInParent, bool Elide, std::unique_ptr< PDBSymbolTypeBaseClass > Base)
uint32_t immediatePadding() const override
ClassLayout(const PDBSymbolTypeUDT &UDT)
LLVM_ABI const PDBSymbolData & getDataMember()
Definition UDTLayout.cpp:87
LLVM_ABI DataMemberLayoutItem(const UDTLayoutBase &Parent, std::unique_ptr< PDBSymbolData > DataMember)
Definition UDTLayout.cpp:68
LLVM_ABI bool hasUDTLayout() const
Definition UDTLayout.cpp:91
LLVM_ABI const ClassLayout & getUDTLayout() const
Definition UDTLayout.cpp:93
IPDBRawSymbol defines an interface used to represent an arbitrary symbol.
virtual uint64_t getLength() const =0
virtual SymIndexId getTypeId() const =0
IPDBSession defines an interface used to provide a context for querying debug information from a debu...
Definition IPDBSession.h:26
virtual std::unique_ptr< PDBSymbol > getSymbolById(SymIndexId SymbolId) const =0
StringRef getName() const
Definition UDTLayout.h:48
virtual uint32_t tailPadding() const
Definition UDTLayout.cpp:62
uint32_t deepPaddingSize() const
Definition UDTLayout.cpp:58
uint32_t getOffsetInParent() const
Definition UDTLayout.h:49
LayoutItemBase(const UDTLayoutBase *Parent, const PDBSymbol *Symbol, const std::string &Name, uint32_t OffsetInParent, uint32_t Size, bool IsElided)
Definition UDTLayout.cpp:48
const UDTLayoutBase * Parent
Definition UDTLayout.h:65
const PDBSymbol * Symbol
Definition UDTLayout.h:64
PDBSymbol defines the base of the inheritance hierarchy for concrete symbol types (e....
Definition PDBSymbol.h:72
std::unique_ptr< ConcreteSymbolEnumerator< T > > findAllChildren() const
Definition PDBSymbol.h:129
ArrayRef< BaseClassLayout * > VirtualBases
Definition UDTLayout.h:148
bool hasVBPtrAtOffset(uint32_t Off) const
std::vector< LayoutItemBase * > LayoutItems
Definition UDTLayout.h:144
UniquePtrVector< PDBSymbol > Other
Definition UDTLayout.h:141
uint32_t tailPadding() const override
void addChildToLayout(std::unique_ptr< LayoutItemBase > Child)
std::vector< BaseClassLayout * > AllBases
Definition UDTLayout.h:146
UniquePtrVector< PDBSymbolFunc > Funcs
Definition UDTLayout.h:142
VTableLayoutItem * VTable
Definition UDTLayout.h:150
void initializeChildren(const PDBSymbol &Sym)
UniquePtrVector< LayoutItemBase > ChildStorage
Definition UDTLayout.h:143
ArrayRef< BaseClassLayout * > NonVirtualBases
Definition UDTLayout.h:147
UDTLayoutBase(const UDTLayoutBase *Parent, const PDBSymbol &Sym, const std::string &Name, uint32_t OffsetInParent, uint32_t Size, bool IsElided)
VBPtrLayoutItem * VBPtr
Definition UDTLayout.h:151
LLVM_ABI VBPtrLayoutItem(const UDTLayoutBase &Parent, std::unique_ptr< PDBSymbolTypeBuiltin > Sym, uint32_t Offset, uint32_t Size)
Definition UDTLayout.cpp:80
LLVM_ABI VTableLayoutItem(const UDTLayoutBase &Parent, std::unique_ptr< PDBSymbolTypeVTable > VTable)
Definition UDTLayout.cpp:97
This is an optimization pass for GlobalISel generic memory operations.
CastInfo< X, std::unique_ptr< Y > >::CastResultType unique_dyn_cast(std::unique_ptr< Y > &Val)
unique_dyn_cast<X> - Given a unique_ptr<Y>, try to return a unique_ptr<X>, taking ownership of the in...
Definition Casting.h:772
auto upper_bound(R &&Range, T &&Value)
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...
Definition STLExtras.h:2065
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878