1//===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===//
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//
9// Builder implementation for CGRecordLayout objects.
10//
11//===----------------------------------------------------------------------===//
12
13#include "ABIInfoImpl.h"
14#include "CGCXXABI.h"
15#include "CGRecordLayout.h"
16#include "CodeGenTypes.h"
17#include "clang/AST/ASTContext.h"
18#include "clang/AST/Attr.h"
19#include "clang/AST/CXXInheritance.h"
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/RecordLayout.h"
23#include "clang/Basic/CodeGenOptions.h"
24#include "clang/CodeGenUtils/RecordLayoutUtils.h"
25#include "clang/CodeGenUtils/TargetUtils.h"
26#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/DerivedTypes.h"
28#include "llvm/IR/Type.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
31#include "llvm/Support/raw_ostream.h"
32using namespace clang;
33using namespace CodeGen;
34
35namespace {
36/// The CGRecordLowering is responsible for lowering an ASTRecordLayout to an
37/// llvm::Type. Some of the lowering is straightforward, some is not. Here we
38/// detail some of the complexities and weirdnesses here.
39/// * LLVM does not have unions - Unions can, in theory be represented by any
40/// llvm::Type with correct size. We choose a field via a specific heuristic
41/// and add padding if necessary.
42/// * LLVM does not have bitfields - Bitfields are collected into contiguous
43/// runs and allocated as a single storage type for the run. ASTRecordLayout
44/// contains enough information to determine where the runs break. Microsoft
45/// and Itanium follow different rules and use different codepaths.
46/// * It is desired that, when possible, bitfields use the appropriate iN type
47/// when lowered to llvm types. For example unsigned x : 24 gets lowered to
48/// i24. This isn't always possible because i24 has storage size of 32 bit
49/// and if it is possible to use that extra byte of padding we must use [i8 x
50/// 3] instead of i24. This is computed when accumulating bitfields in
51/// accumulateBitfields.
52/// C++ examples that require clipping:
53/// struct { int a : 24; char b; }; // a must be clipped, b goes at offset 3
54/// struct A { int a : 24; ~A(); }; // a must be clipped because:
55/// struct B : A { char b; }; // b goes at offset 3
56/// * The allocation of bitfield access units is described in more detail in
57/// CGRecordLowering::accumulateBitFields.
58/// * Clang ignores 0 sized bitfields and 0 sized bases but *not* zero sized
59/// fields. The existing asserts suggest that LLVM assumes that *every* field
60/// has an underlying storage type. Therefore empty structures containing
61/// zero sized subobjects such as empty records or zero sized arrays still get
62/// a zero sized (empty struct) storage type.
63/// * Clang reads the complete type rather than the base type when generating
64/// code to access fields. Bitfields in tail position with tail padding may
65/// be clipped in the base class but not the complete class (we may discover
66/// that the tail padding is not used in the complete class.) However,
67/// because LLVM reads from the complete type it can generate incorrect code
68/// if we do not clip the tail padding off of the bitfield in the complete
69/// layout.
70/// * Itanium allows nearly empty primary virtual bases. These bases don't get
71/// get their own storage because they're laid out as part of another base
72/// or at the beginning of the structure. Determining if a VBase actually
73/// gets storage awkwardly involves a walk of all bases.
74/// * VFPtrs and VBPtrs do *not* make a record NotZeroInitializable.
75struct CGRecordLowering {
76 // MemberInfo is a helper structure that contains information about a record
77 // member. In additional to the standard member types, there exists a
78 // sentinel member type that ensures correct rounding.
79 struct MemberInfo {
80 CharUnits Offset;
81 enum InfoKind { VFPtr, VBPtr, Field, Base, VBase } Kind;
82 llvm::Type *Data;
83 union {
84 const FieldDecl *FD;
85 const CXXRecordDecl *RD;
86 };
87 MemberInfo(CharUnits Offset, InfoKind Kind, llvm::Type *Data,
88 const FieldDecl *FD = nullptr)
89 : Offset(Offset), Kind(Kind), Data(Data), FD(FD) {}
90 MemberInfo(CharUnits Offset, InfoKind Kind, llvm::Type *Data,
91 const CXXRecordDecl *RD)
92 : Offset(Offset), Kind(Kind), Data(Data), RD(RD) {}
93 // MemberInfos are sorted so we define a < operator.
94 bool operator <(const MemberInfo& a) const { return Offset < a.Offset; }
95 };
96 // The constructor.
97 CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D, bool Packed);
98 // Short helper routines.
99 /// Constructs a MemberInfo instance from an offset and llvm::Type *.
100 static MemberInfo StorageInfo(CharUnits Offset, llvm::Type *Data) {
101 return MemberInfo(Offset, MemberInfo::Field, Data);
102 }
103
104 /// Helper function to check if the target machine is BigEndian.
105 bool isBE() const { return Context.getTargetInfo().isBigEndian(); }
106
107 /// Wraps llvm::Type::getIntNTy with some implicit arguments.
108 llvm::Type *getIntNType(uint64_t NumBits) const {
109 unsigned AlignedBits = llvm::alignTo(Value: NumBits, Align: Context.getCharWidth());
110 return llvm::Type::getIntNTy(C&: Types.getLLVMContext(), N: AlignedBits);
111 }
112 /// Get the LLVM type sized as one character unit.
113 llvm::Type *getCharType() const {
114 return llvm::Type::getIntNTy(C&: Types.getLLVMContext(),
115 N: Context.getCharWidth());
116 }
117 /// Gets an llvm type of size NumChars and alignment 1.
118 llvm::Type *getByteArrayType(CharUnits NumChars) const {
119 assert(!NumChars.isZero() && "Empty byte arrays aren't allowed.");
120 llvm::Type *Type = getCharType();
121 return NumChars == CharUnits::One() ? Type :
122 (llvm::Type *)llvm::ArrayType::get(ElementType: Type, NumElements: NumChars.getQuantity());
123 }
124 /// Gets the storage type for a field decl and handles storage
125 /// for itanium bitfields that are smaller than their declared type.
126 llvm::Type *getStorageType(const FieldDecl *FD) const {
127 llvm::Type *Type = Types.ConvertTypeForMem(T: FD->getType());
128 if (!FD->isBitField()) return Type;
129 if (CodeGenUtils::isDiscreteBitFieldABI(Ctx: Context, RD: D))
130 return Type;
131 return getIntNType(NumBits: std::min(a: FD->getBitWidthValue(),
132 b: (unsigned)Context.toBits(CharSize: getSize(Type))));
133 }
134 /// Gets the llvm Basesubobject type from a CXXRecordDecl.
135 llvm::Type *getStorageType(const CXXRecordDecl *RD) const {
136 return Types.getCGRecordLayout(RD).getBaseSubobjectLLVMType();
137 }
138 CharUnits bitsToCharUnits(uint64_t BitOffset) const {
139 return Context.toCharUnitsFromBits(BitSize: BitOffset);
140 }
141 CharUnits getSize(llvm::Type *Type) const {
142 return CharUnits::fromQuantity(Quantity: DataLayout.getTypeAllocSize(Ty: Type));
143 }
144 CharUnits getAlignment(llvm::Type *Type) const {
145 return CharUnits::fromQuantity(Quantity: DataLayout.getABITypeAlign(Ty: Type));
146 }
147 bool isZeroInitializable(const FieldDecl *FD) const {
148 return Types.isZeroInitializable(T: FD->getType());
149 }
150 bool isZeroInitializable(const RecordDecl *RD) const {
151 return Types.isZeroInitializable(RD);
152 }
153 void appendPaddingBytes(CharUnits Size) {
154 if (!Size.isZero())
155 FieldTypes.push_back(Elt: getByteArrayType(NumChars: Size));
156 }
157 uint64_t getFieldBitOffset(const FieldDecl *FD) const {
158 return Layout.getFieldOffset(FieldNo: FD->getFieldIndex());
159 }
160 // Layout routines.
161 void setBitFieldInfo(const FieldDecl *FD, CharUnits StartOffset,
162 llvm::Type *StorageType);
163 /// Lowers an ASTRecordLayout to a llvm type.
164 void lower(bool NonVirtualBaseType);
165 void lowerUnion(bool isNonVirtualBaseType);
166 void accumulateFields(bool isNonVirtualBaseType);
167 RecordDecl::field_iterator
168 accumulateBitFields(bool isNonVirtualBaseType,
169 RecordDecl::field_iterator Field,
170 RecordDecl::field_iterator FieldEnd);
171 void computeVolatileBitfields();
172 void accumulateBases();
173 void accumulateVPtrs();
174 void accumulateVBases();
175 void calculateZeroInit();
176 CharUnits calculateTailClippingOffset(bool isNonVirtualBaseType) const;
177 void checkBitfieldClipping(bool isNonVirtualBaseType) const;
178 /// Determines if we need a packed llvm struct.
179 void determinePacked(bool NVBaseType);
180 /// Inserts padding everywhere it's needed.
181 void insertPadding();
182 /// Fills out the structures that are ultimately consumed.
183 void fillOutputFields();
184 // Input memoization fields.
185 CodeGenTypes &Types;
186 const ASTContext &Context;
187 const RecordDecl *D;
188 const CXXRecordDecl *RD;
189 const ASTRecordLayout &Layout;
190 const llvm::DataLayout &DataLayout;
191 // Helpful intermediate data-structures.
192 std::vector<MemberInfo> Members;
193 // Output fields, consumed by CodeGenTypes::ComputeRecordLayout.
194 SmallVector<llvm::Type *, 16> FieldTypes;
195 llvm::DenseMap<const FieldDecl *, unsigned> Fields;
196 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
197 llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
198 llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases;
199 bool IsZeroInitializable : 1;
200 bool IsZeroInitializableAsBase : 1;
201 bool Packed : 1;
202private:
203 CGRecordLowering(const CGRecordLowering &) = delete;
204 void operator =(const CGRecordLowering &) = delete;
205};
206} // namespace {
207
208CGRecordLowering::CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D,
209 bool Packed)
210 : Types(Types), Context(Types.getContext()), D(D),
211 RD(dyn_cast<CXXRecordDecl>(Val: D)),
212 Layout(Types.getContext().getASTRecordLayout(D)),
213 DataLayout(Types.getDataLayout()), IsZeroInitializable(true),
214 IsZeroInitializableAsBase(true), Packed(Packed) {}
215
216void CGRecordLowering::setBitFieldInfo(
217 const FieldDecl *FD, CharUnits StartOffset, llvm::Type *StorageType) {
218 CGBitFieldInfo &Info = BitFields[FD->getCanonicalDecl()];
219 Info.IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
220 Info.Offset = (unsigned)(getFieldBitOffset(FD) - Context.toBits(CharSize: StartOffset));
221 Info.Size = FD->getBitWidthValue();
222 Info.StorageSize = (unsigned)DataLayout.getTypeAllocSizeInBits(Ty: StorageType);
223 Info.StorageOffset = StartOffset;
224 if (Info.Size > Info.StorageSize)
225 Info.Size = Info.StorageSize;
226 // CGBitFieldInfo::MakeInfo does the same clamp
227 llvm::Type *FieldType = Types.ConvertTypeForMem(T: FD->getType());
228 unsigned FieldTypeSize = DataLayout.getTypeAllocSizeInBits(Ty: FieldType);
229 if (Info.Size > FieldTypeSize)
230 Info.Size = FieldTypeSize;
231 // Reverse the bit offsets for big endian machines. Because we represent
232 // a bitfield as a single large integer load, we can imagine the bits
233 // counting from the most-significant-bit instead of the
234 // least-significant-bit.
235 if (DataLayout.isBigEndian())
236 Info.Offset = Info.StorageSize - (Info.Offset + Info.Size);
237
238 Info.VolatileStorageSize = 0;
239 Info.VolatileOffset = 0;
240 Info.VolatileStorageOffset = CharUnits::Zero();
241}
242
243void CGRecordLowering::lower(bool NVBaseType) {
244 // The lowering process implemented in this function takes a variety of
245 // carefully ordered phases.
246 // 1) Store all members (fields and bases) in a list and sort them by offset.
247 // 2) Add a 1-byte capstone member at the Size of the structure.
248 // 3) Clip bitfield storages members if their tail padding is or might be
249 // used by another field or base. The clipping process uses the capstone
250 // by treating it as another object that occurs after the record.
251 // 4) Determine if the llvm-struct requires packing. It's important that this
252 // phase occur after clipping, because clipping changes the llvm type.
253 // This phase reads the offset of the capstone when determining packedness
254 // and updates the alignment of the capstone to be equal of the alignment
255 // of the record after doing so.
256 // 5) Insert padding everywhere it is needed. This phase requires 'Packed' to
257 // have been computed and needs to know the alignment of the record in
258 // order to understand if explicit tail padding is needed.
259 // 6) Remove the capstone, we don't need it anymore.
260 // 7) Determine if this record can be zero-initialized. This phase could have
261 // been placed anywhere after phase 1.
262 // 8) Format the complete list of members in a way that can be consumed by
263 // CodeGenTypes::ComputeRecordLayout.
264 CharUnits Size = NVBaseType ? Layout.getNonVirtualSize() : Layout.getSize();
265 if (D->isUnion()) {
266 lowerUnion(isNonVirtualBaseType: NVBaseType);
267 computeVolatileBitfields();
268 return;
269 }
270 accumulateFields(isNonVirtualBaseType: NVBaseType);
271 // RD implies C++.
272 if (RD) {
273 accumulateVPtrs();
274 accumulateBases();
275 if (Members.empty()) {
276 appendPaddingBytes(Size);
277 computeVolatileBitfields();
278 return;
279 }
280 if (!NVBaseType)
281 accumulateVBases();
282 }
283 llvm::stable_sort(Range&: Members);
284 checkBitfieldClipping(isNonVirtualBaseType: NVBaseType);
285 Members.push_back(x: StorageInfo(Offset: Size, Data: getIntNType(NumBits: 8)));
286 determinePacked(NVBaseType);
287 insertPadding();
288 Members.pop_back();
289 calculateZeroInit();
290 fillOutputFields();
291 computeVolatileBitfields();
292}
293
294void CGRecordLowering::lowerUnion(bool isNonVirtualBaseType) {
295 CharUnits LayoutSize =
296 isNonVirtualBaseType ? Layout.getDataSize() : Layout.getSize();
297 llvm::Type *StorageType = nullptr;
298 bool SeenNamedMember = false;
299 // Iterate through the fields setting bitFieldInfo and the Fields array. Also
300 // locate the "most appropriate" storage type. The heuristic for finding the
301 // storage type isn't necessary, the first (non-0-length-bitfield) field's
302 // type would work fine and be simpler but would be different than what we've
303 // been doing and cause lit tests to change.
304 for (const auto *Field : D->fields()) {
305 if (Field->isBitField()) {
306 if (Field->isZeroLengthBitField())
307 continue;
308 llvm::Type *FieldType = getStorageType(FD: Field);
309 if (LayoutSize < getSize(Type: FieldType))
310 FieldType = getByteArrayType(NumChars: LayoutSize);
311 setBitFieldInfo(FD: Field, StartOffset: CharUnits::Zero(), StorageType: FieldType);
312 }
313 Fields[Field->getCanonicalDecl()] = 0;
314 llvm::Type *FieldType = getStorageType(FD: Field);
315 // Compute zero-initializable status.
316 // This union might not be zero initialized: it may contain a pointer to
317 // data member which might have some exotic initialization sequence.
318 // If this is the case, then we aught not to try and come up with a "better"
319 // type, it might not be very easy to come up with a Constant which
320 // correctly initializes it.
321 if (!SeenNamedMember) {
322 SeenNamedMember = Field->getIdentifier();
323 if (!SeenNamedMember)
324 if (const auto *FieldRD = Field->getType()->getAsRecordDecl())
325 SeenNamedMember = FieldRD->findFirstNamedDataMember();
326 if (SeenNamedMember && !isZeroInitializable(FD: Field)) {
327 IsZeroInitializable = IsZeroInitializableAsBase = false;
328 StorageType = FieldType;
329 }
330 }
331 // Because our union isn't zero initializable, we won't be getting a better
332 // storage type.
333 if (!IsZeroInitializable)
334 continue;
335 // Conditionally update our storage type if we've got a new "better" one.
336 if (!StorageType ||
337 getAlignment(Type: FieldType) > getAlignment(Type: StorageType) ||
338 (getAlignment(Type: FieldType) == getAlignment(Type: StorageType) &&
339 getSize(Type: FieldType) > getSize(Type: StorageType)))
340 StorageType = FieldType;
341 }
342 // If we have no storage type just pad to the appropriate size and return.
343 if (!StorageType)
344 return appendPaddingBytes(Size: LayoutSize);
345 // If our storage size was bigger than our required size (can happen in the
346 // case of packed bitfields on Itanium) then just use an I8 array.
347 if (LayoutSize < getSize(Type: StorageType))
348 StorageType = getByteArrayType(NumChars: LayoutSize);
349 FieldTypes.push_back(Elt: StorageType);
350 appendPaddingBytes(Size: LayoutSize - getSize(Type: StorageType));
351 // Set packed if we need it.
352 const auto StorageAlignment = getAlignment(Type: StorageType);
353 assert((Layout.getSize().isMultipleOf(StorageAlignment) ||
354 !Layout.getDataSize().isMultipleOf(StorageAlignment)) &&
355 "Union's standard layout and no_unique_address layout must agree on "
356 "packedness");
357 if (!Layout.getDataSize().isMultipleOf(N: StorageAlignment))
358 Packed = true;
359}
360
361void CGRecordLowering::accumulateFields(bool isNonVirtualBaseType) {
362 for (RecordDecl::field_iterator Field = D->field_begin(),
363 FieldEnd = D->field_end();
364 Field != FieldEnd;) {
365 if (Field->isBitField()) {
366 Field = accumulateBitFields(isNonVirtualBaseType, Field, FieldEnd);
367 assert((Field == FieldEnd || !Field->isBitField()) &&
368 "Failed to accumulate all the bitfields");
369 } else if (CodeGenUtils::isEmptyFieldForLayout(Ctx: Context, FD: *Field)) {
370 // Empty fields have no storage.
371 ++Field;
372 } else {
373 // Use base subobject layout for the potentially-overlapping field,
374 // as it is done in RecordLayoutBuilder
375 Members.push_back(x: MemberInfo(
376 bitsToCharUnits(BitOffset: getFieldBitOffset(FD: *Field)), MemberInfo::Field,
377 Field->isPotentiallyOverlapping()
378 ? getStorageType(RD: Field->getType()->getAsCXXRecordDecl())
379 : getStorageType(FD: *Field),
380 *Field));
381 ++Field;
382 }
383 }
384}
385
386// Create members for bitfields. Field is a bitfield, and FieldEnd is the end
387// iterator of the record. Return the first non-bitfield encountered. We need
388// to know whether this is the base or complete layout, as virtual bases could
389// affect the upper bound of bitfield access unit allocation.
390RecordDecl::field_iterator
391CGRecordLowering::accumulateBitFields(bool isNonVirtualBaseType,
392 RecordDecl::field_iterator Field,
393 RecordDecl::field_iterator FieldEnd) {
394 if (CodeGenUtils::isDiscreteBitFieldABI(Ctx: Context, RD: D)) {
395 // Run stores the first element of the current run of bitfields. FieldEnd is
396 // used as a special value to note that we don't have a current run. A
397 // bitfield run is a contiguous collection of bitfields that can be stored
398 // in the same storage block. Zero-sized bitfields and bitfields that would
399 // cross an alignment boundary break a run and start a new one.
400 RecordDecl::field_iterator Run = FieldEnd;
401 // Tail is the offset of the first bit off the end of the current run. It's
402 // used to determine if the ASTRecordLayout is treating these two bitfields
403 // as contiguous. StartBitOffset is offset of the beginning of the Run.
404 uint64_t StartBitOffset, Tail = 0;
405 for (; Field != FieldEnd && Field->isBitField(); ++Field) {
406 // Zero-width bitfields end runs.
407 if (Field->isZeroLengthBitField()) {
408 Run = FieldEnd;
409 continue;
410 }
411 uint64_t BitOffset = getFieldBitOffset(FD: *Field);
412 llvm::Type *Type = Types.ConvertTypeForMem(T: Field->getType());
413 // If we don't have a run yet, or don't live within the previous run's
414 // allocated storage then we allocate some storage and start a new run.
415 if (Run == FieldEnd || BitOffset >= Tail) {
416 Run = Field;
417 StartBitOffset = BitOffset;
418 Tail = StartBitOffset + DataLayout.getTypeAllocSizeInBits(Ty: Type);
419 // Add the storage member to the record. This must be added to the
420 // record before the bitfield members so that it gets laid out before
421 // the bitfields it contains get laid out.
422 Members.push_back(x: StorageInfo(Offset: bitsToCharUnits(BitOffset: StartBitOffset), Data: Type));
423 }
424 // Bitfields get the offset of their storage but come afterward and remain
425 // there after a stable sort.
426 Members.push_back(x: MemberInfo(bitsToCharUnits(BitOffset: StartBitOffset),
427 MemberInfo::Field, nullptr, *Field));
428 }
429 return Field;
430 }
431
432 // The SysV ABI can overlap bitfield storage units with both other bitfield
433 // storage units /and/ other non-bitfield data members. Accessing a sequence
434 // of bitfields mustn't interfere with adjacent non-bitfields -- they're
435 // permitted to be accessed in separate threads for instance.
436
437 // We split runs of bit-fields into a sequence of "access units". When we emit
438 // a load or store of a bit-field, we'll load/store the entire containing
439 // access unit. As mentioned, the standard requires that these loads and
440 // stores must not interfere with accesses to other memory locations, and it
441 // defines the bit-field's memory location as the current run of
442 // non-zero-width bit-fields. So an access unit must never overlap with
443 // non-bit-field storage or cross a zero-width bit-field. Otherwise, we're
444 // free to draw the lines as we see fit.
445
446 // Drawing these lines well can be complicated. LLVM generally can't modify a
447 // program to access memory that it didn't before, so using very narrow access
448 // units can prevent the compiler from using optimal access patterns. For
449 // example, suppose a run of bit-fields occupies four bytes in a struct. If we
450 // split that into four 1-byte access units, then a sequence of assignments
451 // that doesn't touch all four bytes may have to be emitted with multiple
452 // 8-bit stores instead of a single 32-bit store. On the other hand, if we use
453 // very wide access units, we may find ourselves emitting accesses to
454 // bit-fields we didn't really need to touch, just because LLVM was unable to
455 // clean up after us.
456
457 // It is desirable to have access units be aligned powers of 2 no larger than
458 // a register. (On non-strict alignment ISAs, the alignment requirement can be
459 // dropped.) A three byte access unit will be accessed using 2-byte and 1-byte
460 // accesses and bit manipulation. If no bitfield straddles across the two
461 // separate accesses, it is better to have separate 2-byte and 1-byte access
462 // units, as then LLVM will not generate unnecessary memory accesses, or bit
463 // manipulation. Similarly, on a strict-alignment architecture, it is better
464 // to keep access-units naturally aligned, to avoid similar bit
465 // manipulation synthesizing larger unaligned accesses.
466
467 // Bitfields that share parts of a single byte are, of necessity, placed in
468 // the same access unit. That unit will encompass a consecutive run where
469 // adjacent bitfields share parts of a byte. (The first bitfield of such an
470 // access unit will start at the beginning of a byte.)
471
472 // We then try and accumulate adjacent access units when the combined unit is
473 // naturally sized, no larger than a register, and (on a strict alignment
474 // ISA), naturally aligned. Note that this requires lookahead to one or more
475 // subsequent access units. For instance, consider a 2-byte access-unit
476 // followed by 2 1-byte units. We can merge that into a 4-byte access-unit,
477 // but we would not want to merge a 2-byte followed by a single 1-byte (and no
478 // available tail padding). We keep track of the best access unit seen so far,
479 // and use that when we determine we cannot accumulate any more. Then we start
480 // again at the bitfield following that best one.
481
482 // The accumulation is also prevented when:
483 // *) it would cross a character-aigned zero-width bitfield, or
484 // *) fine-grained bitfield access option is in effect.
485
486 CharUnits RegSize =
487 bitsToCharUnits(BitOffset: Context.getTargetInfo().getRegisterWidth());
488 unsigned CharBits = Context.getCharWidth();
489
490 // Limit of useable tail padding at end of the record. Computed lazily and
491 // cached here.
492 CharUnits ScissorOffset = CharUnits::Zero();
493
494 // Data about the start of the span we're accumulating to create an access
495 // unit from. Begin is the first bitfield of the span. If Begin is FieldEnd,
496 // we've not got a current span. The span starts at the BeginOffset character
497 // boundary. BitSizeSinceBegin is the size (in bits) of the span -- this might
498 // include padding when we've advanced to a subsequent bitfield run.
499 RecordDecl::field_iterator Begin = FieldEnd;
500 CharUnits BeginOffset;
501 uint64_t BitSizeSinceBegin;
502
503 // The (non-inclusive) end of the largest acceptable access unit we've found
504 // since Begin. If this is Begin, we're gathering the initial set of bitfields
505 // of a new span. BestEndOffset is the end of that acceptable access unit --
506 // it might extend beyond the last character of the bitfield run, using
507 // available padding characters.
508 RecordDecl::field_iterator BestEnd = Begin;
509 CharUnits BestEndOffset;
510 bool BestClipped; // Whether the representation must be in a byte array.
511
512 for (;;) {
513 // AtAlignedBoundary is true iff Field is the (potential) start of a new
514 // span (or the end of the bitfields). When true, LimitOffset is the
515 // character offset of that span and Barrier indicates whether the new
516 // span cannot be merged into the current one.
517 bool AtAlignedBoundary = false;
518 bool Barrier = false;
519
520 if (Field != FieldEnd && Field->isBitField()) {
521 uint64_t BitOffset = getFieldBitOffset(FD: *Field);
522 if (Begin == FieldEnd) {
523 // Beginning a new span.
524 Begin = Field;
525 BestEnd = Begin;
526
527 assert((BitOffset % CharBits) == 0 && "Not at start of char");
528 BeginOffset = bitsToCharUnits(BitOffset);
529 BitSizeSinceBegin = 0;
530 } else if ((BitOffset % CharBits) != 0) {
531 // Bitfield occupies the same character as previous bitfield, it must be
532 // part of the same span. This can include zero-length bitfields, should
533 // the target not align them to character boundaries. Such non-alignment
534 // is at variance with the standards, which require zero-length
535 // bitfields be a barrier between access units. But of course we can't
536 // achieve that in the middle of a character.
537 assert(BitOffset == Context.toBits(BeginOffset) + BitSizeSinceBegin &&
538 "Concatenating non-contiguous bitfields");
539 } else {
540 // Bitfield potentially begins a new span. This includes zero-length
541 // bitfields on non-aligning targets that lie at character boundaries
542 // (those are barriers to merging).
543 if (Field->isZeroLengthBitField())
544 Barrier = true;
545 AtAlignedBoundary = true;
546 }
547 } else {
548 // We've reached the end of the bitfield run. Either we're done, or this
549 // is a barrier for the current span.
550 if (Begin == FieldEnd)
551 break;
552
553 Barrier = true;
554 AtAlignedBoundary = true;
555 }
556
557 // InstallBest indicates whether we should create an access unit for the
558 // current best span: fields [Begin, BestEnd) occupying characters
559 // [BeginOffset, BestEndOffset).
560 bool InstallBest = false;
561 if (AtAlignedBoundary) {
562 // Field is the start of a new span or the end of the bitfields. The
563 // just-seen span now extends to BitSizeSinceBegin.
564
565 // Determine if we can accumulate that just-seen span into the current
566 // accumulation.
567 CharUnits AccessSize = bitsToCharUnits(BitOffset: BitSizeSinceBegin + CharBits - 1);
568 if (BestEnd == Begin) {
569 // This is the initial run at the start of a new span. By definition,
570 // this is the best seen so far.
571 BestEnd = Field;
572 BestEndOffset = BeginOffset + AccessSize;
573 // Assume clipped until proven not below.
574 BestClipped = true;
575 if (!BitSizeSinceBegin)
576 // A zero-sized initial span -- this will install nothing and reset
577 // for another.
578 InstallBest = true;
579 } else if (AccessSize > RegSize)
580 // Accumulating the just-seen span would create a multi-register access
581 // unit, which would increase register pressure.
582 InstallBest = true;
583
584 if (!InstallBest) {
585 // Determine if accumulating the just-seen span will create an expensive
586 // access unit or not.
587 llvm::Type *Type = getIntNType(NumBits: Context.toBits(CharSize: AccessSize));
588 if (!Context.getTargetInfo().hasCheapUnalignedBitFieldAccess()) {
589 // Unaligned accesses are expensive. Only accumulate if the new unit
590 // is naturally aligned. Otherwise install the best we have, which is
591 // either the initial access unit (can't do better), or a naturally
592 // aligned accumulation (since we would have already installed it if
593 // it wasn't naturally aligned).
594 CharUnits Align = getAlignment(Type);
595 if (Align > Layout.getAlignment())
596 // The alignment required is greater than the containing structure
597 // itself.
598 InstallBest = true;
599 else if (!BeginOffset.isMultipleOf(N: Align))
600 // The access unit is not at a naturally aligned offset within the
601 // structure.
602 InstallBest = true;
603
604 if (InstallBest && BestEnd == Field)
605 // We're installing the first span, whose clipping was presumed
606 // above. Compute it correctly.
607 if (getSize(Type) == AccessSize)
608 BestClipped = false;
609 }
610
611 if (!InstallBest) {
612 // Find the next used storage offset to determine what the limit of
613 // the current span is. That's either the offset of the next field
614 // with storage (which might be Field itself) or the end of the
615 // non-reusable tail padding.
616 CharUnits LimitOffset;
617 for (auto Probe = Field; Probe != FieldEnd; ++Probe)
618 if (!CodeGenUtils::isEmptyFieldForLayout(Ctx: Context, FD: *Probe)) {
619 // A member with storage sets the limit.
620 assert((getFieldBitOffset(*Probe) % CharBits) == 0 &&
621 "Next storage is not byte-aligned");
622 LimitOffset = bitsToCharUnits(BitOffset: getFieldBitOffset(FD: *Probe));
623 goto FoundLimit;
624 }
625 // We reached the end of the fields, determine the bounds of useable
626 // tail padding. As this can be complex for C++, we cache the result.
627 if (ScissorOffset.isZero()) {
628 ScissorOffset = calculateTailClippingOffset(isNonVirtualBaseType);
629 assert(!ScissorOffset.isZero() && "Tail clipping at zero");
630 }
631
632 LimitOffset = ScissorOffset;
633 FoundLimit:;
634
635 CharUnits TypeSize = getSize(Type);
636 if (BeginOffset + TypeSize <= LimitOffset) {
637 // There is space before LimitOffset to create a naturally-sized
638 // access unit.
639 BestEndOffset = BeginOffset + TypeSize;
640 BestEnd = Field;
641 BestClipped = false;
642 }
643
644 if (Barrier)
645 // The next field is a barrier that we cannot merge across.
646 InstallBest = true;
647 else if (Types.getCodeGenOpts().FineGrainedBitfieldAccesses)
648 // Fine-grained access, so no merging of spans.
649 InstallBest = true;
650 else
651 // Otherwise, we're not installing. Update the bit size
652 // of the current span to go all the way to LimitOffset, which is
653 // the (aligned) offset of next bitfield to consider.
654 BitSizeSinceBegin = Context.toBits(CharSize: LimitOffset - BeginOffset);
655 }
656 }
657 }
658
659 if (InstallBest) {
660 assert((Field == FieldEnd || !Field->isBitField() ||
661 (getFieldBitOffset(*Field) % CharBits) == 0) &&
662 "Installing but not at an aligned bitfield or limit");
663 CharUnits AccessSize = BestEndOffset - BeginOffset;
664 if (!AccessSize.isZero()) {
665 // Add the storage member for the access unit to the record. The
666 // bitfields get the offset of their storage but come afterward and
667 // remain there after a stable sort.
668 llvm::Type *Type;
669 if (BestClipped) {
670 assert(getSize(getIntNType(Context.toBits(AccessSize))) >
671 AccessSize &&
672 "Clipped access need not be clipped");
673 Type = getByteArrayType(NumChars: AccessSize);
674 } else {
675 Type = getIntNType(NumBits: Context.toBits(CharSize: AccessSize));
676 assert(getSize(Type) == AccessSize &&
677 "Unclipped access must be clipped");
678 }
679 Members.push_back(x: StorageInfo(Offset: BeginOffset, Data: Type));
680 for (; Begin != BestEnd; ++Begin)
681 if (!Begin->isZeroLengthBitField())
682 Members.push_back(
683 x: MemberInfo(BeginOffset, MemberInfo::Field, nullptr, *Begin));
684 }
685 // Reset to start a new span.
686 Field = BestEnd;
687 Begin = FieldEnd;
688 } else {
689 assert(Field != FieldEnd && Field->isBitField() &&
690 "Accumulating past end of bitfields");
691 assert(!Barrier && "Accumulating across barrier");
692 // Accumulate this bitfield into the current (potential) span.
693 BitSizeSinceBegin += Field->getBitWidthValue();
694 ++Field;
695 }
696 }
697
698 return Field;
699}
700
701void CGRecordLowering::accumulateBases() {
702 // If we've got a primary virtual base, we need to add it with the bases.
703 if (Layout.isPrimaryBaseVirtual()) {
704 const CXXRecordDecl *BaseDecl = Layout.getPrimaryBase();
705 Members.push_back(x: MemberInfo(CharUnits::Zero(), MemberInfo::Base,
706 getStorageType(RD: BaseDecl), BaseDecl));
707 }
708 // Accumulate the non-virtual bases.
709 for (const auto &Base : RD->bases()) {
710 if (Base.isVirtual())
711 continue;
712
713 // Bases can be zero-sized even if not technically empty if they
714 // contain only a trailing array member.
715 const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
716 if (!CodeGenUtils::isEmptyRecordForLayout(Ctx: Context, T: Base.getType()) &&
717 !Context.getASTRecordLayout(D: BaseDecl).getNonVirtualSize().isZero())
718 Members.push_back(x: MemberInfo(Layout.getBaseClassOffset(Base: BaseDecl),
719 MemberInfo::Base, getStorageType(RD: BaseDecl), BaseDecl));
720 }
721}
722
723/// The AAPCS that defines that, when possible, bit-fields should
724/// be accessed using containers of the declared type width:
725/// When a volatile bit-field is read, and its container does not overlap with
726/// any non-bit-field member or any zero length bit-field member, its container
727/// must be read exactly once using the access width appropriate to the type of
728/// the container. When a volatile bit-field is written, and its container does
729/// not overlap with any non-bit-field member or any zero-length bit-field
730/// member, its container must be read exactly once and written exactly once
731/// using the access width appropriate to the type of the container. The two
732/// accesses are not atomic.
733///
734/// Enforcing the width restriction can be disabled using
735/// -fno-aapcs-bitfield-width.
736void CGRecordLowering::computeVolatileBitfields() {
737 if (!CodeGenUtils::isAAPCS(TargetInfo: Context.getTargetInfo()) ||
738 !Types.getCodeGenOpts().AAPCSBitfieldWidth)
739 return;
740
741 for (auto &I : BitFields) {
742 const FieldDecl *Field = I.first;
743 CGBitFieldInfo &Info = I.second;
744 llvm::Type *ResLTy = Types.ConvertTypeForMem(T: Field->getType());
745 // If the record alignment is less than the type width, we can't enforce a
746 // aligned load, bail out.
747 if ((uint64_t)(Context.toBits(CharSize: Layout.getAlignment())) <
748 ResLTy->getPrimitiveSizeInBits())
749 continue;
750 // CGRecordLowering::setBitFieldInfo() pre-adjusts the bit-field offsets
751 // for big-endian targets, but it assumes a container of width
752 // Info.StorageSize. Since AAPCS uses a different container size (width
753 // of the type), we first undo that calculation here and redo it once
754 // the bit-field offset within the new container is calculated.
755 const unsigned OldOffset =
756 isBE() ? Info.StorageSize - (Info.Offset + Info.Size) : Info.Offset;
757 // Offset to the bit-field from the beginning of the struct.
758 const unsigned AbsoluteOffset =
759 Context.toBits(CharSize: Info.StorageOffset) + OldOffset;
760
761 // Container size is the width of the bit-field type.
762 const unsigned StorageSize = ResLTy->getPrimitiveSizeInBits();
763 // Nothing to do if the access uses the desired
764 // container width and is naturally aligned.
765 if (Info.StorageSize == StorageSize && (OldOffset % StorageSize == 0))
766 continue;
767
768 // Offset within the container.
769 unsigned Offset = AbsoluteOffset & (StorageSize - 1);
770 // Bail out if an aligned load of the container cannot cover the entire
771 // bit-field. This can happen for example, if the bit-field is part of a
772 // packed struct. AAPCS does not define access rules for such cases, we let
773 // clang to follow its own rules.
774 if (Offset + Info.Size > StorageSize)
775 continue;
776
777 // Re-adjust offsets for big-endian targets.
778 if (isBE())
779 Offset = StorageSize - (Offset + Info.Size);
780
781 const CharUnits StorageOffset =
782 Context.toCharUnitsFromBits(BitSize: AbsoluteOffset & ~(StorageSize - 1));
783 const CharUnits End = StorageOffset +
784 Context.toCharUnitsFromBits(BitSize: StorageSize) -
785 CharUnits::One();
786
787 const ASTRecordLayout &Layout =
788 Context.getASTRecordLayout(D: Field->getParent());
789 // If we access outside memory outside the record, than bail out.
790 const CharUnits RecordSize = Layout.getSize();
791 if (End >= RecordSize)
792 continue;
793
794 // Bail out if performing this load would access non-bit-fields members.
795 bool Conflict = false;
796 for (const auto *F : D->fields()) {
797 // Allow sized bit-fields overlaps.
798 if (F->isBitField() && !F->isZeroLengthBitField())
799 continue;
800
801 const CharUnits FOffset = Context.toCharUnitsFromBits(
802 BitSize: Layout.getFieldOffset(FieldNo: F->getFieldIndex()));
803
804 // As C11 defines, a zero sized bit-field defines a barrier, so
805 // fields after and before it should be race condition free.
806 // The AAPCS acknowledges it and imposes no restritions when the
807 // natural container overlaps a zero-length bit-field.
808 if (F->isZeroLengthBitField()) {
809 if (End > FOffset && StorageOffset < FOffset) {
810 Conflict = true;
811 break;
812 }
813 }
814
815 const CharUnits FEnd =
816 FOffset +
817 Context.toCharUnitsFromBits(
818 BitSize: Types.ConvertTypeForMem(T: F->getType())->getPrimitiveSizeInBits()) -
819 CharUnits::One();
820 // If no overlap, continue.
821 if (End < FOffset || FEnd < StorageOffset)
822 continue;
823
824 // The desired load overlaps a non-bit-field member, bail out.
825 Conflict = true;
826 break;
827 }
828
829 if (Conflict)
830 continue;
831 // Write the new bit-field access parameters.
832 // As the storage offset now is defined as the number of elements from the
833 // start of the structure, we should divide the Offset by the element size.
834 Info.VolatileStorageOffset =
835 StorageOffset / Context.toCharUnitsFromBits(BitSize: StorageSize).getQuantity();
836 Info.VolatileStorageSize = StorageSize;
837 Info.VolatileOffset = Offset;
838 }
839}
840
841void CGRecordLowering::accumulateVPtrs() {
842 if (Layout.hasOwnVFPtr())
843 Members.push_back(
844 x: MemberInfo(CharUnits::Zero(), MemberInfo::VFPtr,
845 llvm::PointerType::getUnqual(C&: Types.getLLVMContext())));
846 if (Layout.hasOwnVBPtr())
847 Members.push_back(
848 x: MemberInfo(Layout.getVBPtrOffset(), MemberInfo::VBPtr,
849 llvm::PointerType::getUnqual(C&: Types.getLLVMContext())));
850}
851
852CharUnits
853CGRecordLowering::calculateTailClippingOffset(bool isNonVirtualBaseType) const {
854 if (!RD)
855 return Layout.getDataSize();
856
857 CharUnits ScissorOffset = Layout.getNonVirtualSize();
858 // In the itanium ABI, it's possible to place a vbase at a dsize that is
859 // smaller than the nvsize. Here we check to see if such a base is placed
860 // before the nvsize and set the scissor offset to that, instead of the
861 // nvsize.
862 if (!isNonVirtualBaseType && CodeGenUtils::isOverlappingVBaseABI(Ctx: Context))
863 for (const auto &Base : RD->vbases()) {
864 const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
865 if (CodeGenUtils::isEmptyRecordForLayout(Ctx: Context, T: Base.getType()))
866 continue;
867 // If the vbase is a primary virtual base of some base, then it doesn't
868 // get its own storage location but instead lives inside of that base.
869 if (Context.isNearlyEmpty(RD: BaseDecl) &&
870 !CodeGenUtils::hasOwnStorage(Ctx: Context, Decl: RD, Query: BaseDecl))
871 continue;
872 ScissorOffset = std::min(a: ScissorOffset,
873 b: Layout.getVBaseClassOffset(VBase: BaseDecl));
874 }
875
876 return ScissorOffset;
877}
878
879void CGRecordLowering::accumulateVBases() {
880 for (const auto &Base : RD->vbases()) {
881 const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
882 if (CodeGenUtils::isEmptyRecordForLayout(Ctx: Context, T: Base.getType()))
883 continue;
884 CharUnits Offset = Layout.getVBaseClassOffset(VBase: BaseDecl);
885 // If the vbase is a primary virtual base of some base, then it doesn't
886 // get its own storage location but instead lives inside of that base.
887 if (CodeGenUtils::isOverlappingVBaseABI(Ctx: Context) &&
888 Context.isNearlyEmpty(RD: BaseDecl) &&
889 !CodeGenUtils::hasOwnStorage(Ctx: Context, Decl: RD, Query: BaseDecl)) {
890 Members.push_back(x: MemberInfo(Offset, MemberInfo::VBase, nullptr,
891 BaseDecl));
892 continue;
893 }
894 // If we've got a vtordisp, add it as a storage type.
895 if (Layout.getVBaseOffsetsMap().find(Val: BaseDecl)->second.hasVtorDisp())
896 Members.push_back(x: StorageInfo(Offset: Offset - CharUnits::fromQuantity(Quantity: 4),
897 Data: getIntNType(NumBits: 32)));
898 Members.push_back(x: MemberInfo(Offset, MemberInfo::VBase,
899 getStorageType(RD: BaseDecl), BaseDecl));
900 }
901}
902
903void CGRecordLowering::calculateZeroInit() {
904 for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
905 MemberEnd = Members.end();
906 IsZeroInitializableAsBase && Member != MemberEnd; ++Member) {
907 if (Member->Kind == MemberInfo::Field) {
908 if (!Member->FD || isZeroInitializable(FD: Member->FD))
909 continue;
910 IsZeroInitializable = IsZeroInitializableAsBase = false;
911 } else if (Member->Kind == MemberInfo::Base ||
912 Member->Kind == MemberInfo::VBase) {
913 if (isZeroInitializable(RD: Member->RD))
914 continue;
915 IsZeroInitializable = false;
916 if (Member->Kind == MemberInfo::Base)
917 IsZeroInitializableAsBase = false;
918 }
919 }
920}
921
922// Verify accumulateBitfields computed the correct storage representations.
923void CGRecordLowering::checkBitfieldClipping(bool IsNonVirtualBaseType) const {
924#ifndef NDEBUG
925 auto ScissorOffset = calculateTailClippingOffset(IsNonVirtualBaseType);
926 auto Tail = CharUnits::Zero();
927 for (const auto &M : Members) {
928 // Only members with data could possibly overlap.
929 if (!M.Data)
930 continue;
931
932 assert(M.Offset >= Tail && "Bitfield access unit is not clipped");
933 Tail = M.Offset + getSize(M.Data);
934 assert((Tail <= ScissorOffset || M.Offset >= ScissorOffset) &&
935 "Bitfield straddles scissor offset");
936 }
937#endif
938}
939
940void CGRecordLowering::determinePacked(bool NVBaseType) {
941 if (Packed)
942 return;
943 CharUnits Alignment = CharUnits::One();
944 CharUnits NVAlignment = CharUnits::One();
945 CharUnits NVSize =
946 !NVBaseType && RD ? Layout.getNonVirtualSize() : CharUnits::Zero();
947 for (const MemberInfo &Member : Members) {
948 if (!Member.Data)
949 continue;
950 // If any member falls at an offset that it not a multiple of its alignment,
951 // then the entire record must be packed.
952 if (!Member.Offset.isMultipleOf(N: getAlignment(Type: Member.Data)))
953 Packed = true;
954 if (Member.Offset < NVSize)
955 NVAlignment = std::max(a: NVAlignment, b: getAlignment(Type: Member.Data));
956 Alignment = std::max(a: Alignment, b: getAlignment(Type: Member.Data));
957 }
958 // If the size of the record (the capstone's offset) is not a multiple of the
959 // record's alignment, it must be packed.
960 if (!Members.back().Offset.isMultipleOf(N: Alignment))
961 Packed = true;
962 // If the non-virtual sub-object is not a multiple of the non-virtual
963 // sub-object's alignment, it must be packed. We cannot have a packed
964 // non-virtual sub-object and an unpacked complete object or vise versa.
965 if (!NVSize.isMultipleOf(N: NVAlignment))
966 Packed = true;
967 // Update the alignment of the sentinel.
968 if (!Packed)
969 Members.back().Data = getIntNType(NumBits: Context.toBits(CharSize: Alignment));
970}
971
972void CGRecordLowering::insertPadding() {
973 std::vector<std::pair<CharUnits, CharUnits> > Padding;
974 CharUnits Size = CharUnits::Zero();
975 for (const MemberInfo &Member : Members) {
976 if (!Member.Data)
977 continue;
978 CharUnits Offset = Member.Offset;
979 assert(Offset >= Size);
980 // Insert padding if we need to.
981 if (Offset !=
982 Size.alignTo(Align: Packed ? CharUnits::One() : getAlignment(Type: Member.Data)))
983 Padding.push_back(x: std::make_pair(x&: Size, y: Offset - Size));
984 Size = Offset + getSize(Type: Member.Data);
985 }
986 if (Padding.empty())
987 return;
988 // Add the padding to the Members list and sort it.
989 for (const auto &Pad : Padding)
990 Members.push_back(x: StorageInfo(Offset: Pad.first, Data: getByteArrayType(NumChars: Pad.second)));
991 llvm::stable_sort(Range&: Members);
992}
993
994void CGRecordLowering::fillOutputFields() {
995 for (const MemberInfo &Member : Members) {
996 if (Member.Data)
997 FieldTypes.push_back(Elt: Member.Data);
998 if (Member.Kind == MemberInfo::Field) {
999 if (Member.FD)
1000 Fields[Member.FD->getCanonicalDecl()] = FieldTypes.size() - 1;
1001 // A field without storage must be a bitfield.
1002 if (!Member.Data) {
1003 assert(Member.FD &&
1004 "Member.Data is a nullptr so Member.FD should not be");
1005 setBitFieldInfo(FD: Member.FD, StartOffset: Member.Offset, StorageType: FieldTypes.back());
1006 }
1007 } else if (Member.Kind == MemberInfo::Base)
1008 NonVirtualBases[Member.RD] = FieldTypes.size() - 1;
1009 else if (Member.Kind == MemberInfo::VBase)
1010 VirtualBases[Member.RD] = FieldTypes.size() - 1;
1011 }
1012}
1013
1014CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
1015 const FieldDecl *FD,
1016 uint64_t Offset, uint64_t Size,
1017 uint64_t StorageSize,
1018 CharUnits StorageOffset) {
1019 // This function is vestigial from CGRecordLayoutBuilder days but is still
1020 // used in GCObjCRuntime.cpp. That usage has a "fixme" attached to it that
1021 // when addressed will allow for the removal of this function.
1022 llvm::Type *Ty = Types.ConvertTypeForMem(T: FD->getType());
1023 CharUnits TypeSizeInBytes =
1024 CharUnits::fromQuantity(Quantity: Types.getDataLayout().getTypeAllocSize(Ty));
1025 uint64_t TypeSizeInBits = Types.getContext().toBits(CharSize: TypeSizeInBytes);
1026
1027 bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
1028
1029 if (Size > TypeSizeInBits) {
1030 // We have a wide bit-field. The extra bits are only used for padding, so
1031 // if we have a bitfield of type T, with size N:
1032 //
1033 // T t : N;
1034 //
1035 // We can just assume that it's:
1036 //
1037 // T t : sizeof(T);
1038 //
1039 Size = TypeSizeInBits;
1040 }
1041
1042 // Reverse the bit offsets for big endian machines. Because we represent
1043 // a bitfield as a single large integer load, we can imagine the bits
1044 // counting from the most-significant-bit instead of the
1045 // least-significant-bit.
1046 if (Types.getDataLayout().isBigEndian()) {
1047 Offset = StorageSize - (Offset + Size);
1048 }
1049
1050 return CGBitFieldInfo(Offset, Size, IsSigned, StorageSize, StorageOffset);
1051}
1052
1053std::unique_ptr<CGRecordLayout>
1054CodeGenTypes::ComputeRecordLayout(const RecordDecl *D, llvm::StructType *Ty) {
1055 CGRecordLowering Builder(*this, D, /*Packed=*/false);
1056
1057 Builder.lower(/*NonVirtualBaseType=*/NVBaseType: false);
1058
1059 // If we're in C++, compute the base subobject type.
1060 llvm::StructType *BaseTy = nullptr;
1061 if (isa<CXXRecordDecl>(Val: D)) {
1062 BaseTy = Ty;
1063 if (Builder.Layout.getNonVirtualSize() != Builder.Layout.getSize()) {
1064 CGRecordLowering BaseBuilder(*this, D, /*Packed=*/Builder.Packed);
1065 BaseBuilder.lower(/*NonVirtualBaseType=*/NVBaseType: true);
1066 BaseTy = llvm::StructType::create(
1067 Context&: getLLVMContext(), Elements: BaseBuilder.FieldTypes, Name: "", isPacked: BaseBuilder.Packed);
1068 addRecordTypeName(RD: D, Ty: BaseTy, suffix: ".base");
1069 // BaseTy and Ty must agree on their packedness for getLLVMFieldNo to work
1070 // on both of them with the same index.
1071 assert(Builder.Packed == BaseBuilder.Packed &&
1072 "Non-virtual and complete types must agree on packedness");
1073 }
1074 }
1075
1076 // Fill in the struct *after* computing the base type. Filling in the body
1077 // signifies that the type is no longer opaque and record layout is complete,
1078 // but we may need to recursively layout D while laying D out as a base type.
1079 Ty->setBody(Elements: Builder.FieldTypes, isPacked: Builder.Packed);
1080
1081 auto RL = std::make_unique<CGRecordLayout>(
1082 args&: Ty, args&: BaseTy, args: (bool)Builder.IsZeroInitializable,
1083 args: (bool)Builder.IsZeroInitializableAsBase);
1084
1085 RL->NonVirtualBases.swap(RHS&: Builder.NonVirtualBases);
1086 RL->CompleteObjectVirtualBases.swap(RHS&: Builder.VirtualBases);
1087
1088 // Add all the field numbers.
1089 RL->FieldInfo.swap(RHS&: Builder.Fields);
1090
1091 // Add bitfield info.
1092 RL->BitFields.swap(RHS&: Builder.BitFields);
1093
1094 // Dump the layout, if requested.
1095 if (getContext().getLangOpts().DumpRecordLayouts) {
1096 llvm::outs() << "\n*** Dumping IRgen Record Layout\n";
1097 llvm::outs() << "Record: ";
1098 D->dump(Out&: llvm::outs());
1099 llvm::outs() << "\nLayout: ";
1100 RL->print(OS&: llvm::outs());
1101 }
1102
1103#ifndef NDEBUG
1104 // Verify that the computed LLVM struct size matches the AST layout size.
1105 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
1106
1107 uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
1108 assert(TypeSizeInBits == getDataLayout().getTypeAllocSizeInBits(Ty) &&
1109 "Type size mismatch!");
1110
1111 if (BaseTy) {
1112 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
1113
1114 uint64_t AlignedNonVirtualTypeSizeInBits =
1115 getContext().toBits(NonVirtualSize);
1116
1117 assert(AlignedNonVirtualTypeSizeInBits ==
1118 getDataLayout().getTypeAllocSizeInBits(BaseTy) &&
1119 "Type size mismatch!");
1120 }
1121
1122 // Verify that the LLVM and AST field offsets agree.
1123 llvm::StructType *ST = RL->getLLVMType();
1124 const llvm::StructLayout *SL = getDataLayout().getStructLayout(ST);
1125
1126 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
1127 RecordDecl::field_iterator it = D->field_begin();
1128 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
1129 const FieldDecl *FD = *it;
1130
1131 // Ignore zero-sized fields.
1132 if (CodeGenUtils::isEmptyFieldForLayout(getContext(), FD))
1133 continue;
1134
1135 // For non-bit-fields, just check that the LLVM struct offset matches the
1136 // AST offset.
1137 if (!FD->isBitField()) {
1138 unsigned FieldNo = RL->getLLVMFieldNo(FD);
1139 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
1140 "Invalid field offset!");
1141 continue;
1142 }
1143
1144 // Ignore unnamed bit-fields.
1145 if (!FD->getDeclName())
1146 continue;
1147
1148 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
1149 llvm::Type *ElementTy = ST->getTypeAtIndex(RL->getLLVMFieldNo(FD));
1150
1151 // Unions have overlapping elements dictating their layout, but for
1152 // non-unions we can verify that this section of the layout is the exact
1153 // expected size.
1154 if (D->isUnion()) {
1155 // For unions we verify that the start is zero and the size
1156 // is in-bounds. However, on BE systems, the offset may be non-zero, but
1157 // the size + offset should match the storage size in that case as it
1158 // "starts" at the back.
1159 if (getDataLayout().isBigEndian())
1160 assert(static_cast<unsigned>(Info.Offset + Info.Size) ==
1161 Info.StorageSize &&
1162 "Big endian union bitfield does not end at the back");
1163 else
1164 assert(Info.Offset == 0 &&
1165 "Little endian union bitfield with a non-zero offset");
1166 assert(Info.StorageSize <= SL->getSizeInBits() &&
1167 "Union not large enough for bitfield storage");
1168 } else {
1169 assert((Info.StorageSize ==
1170 getDataLayout().getTypeAllocSizeInBits(ElementTy) ||
1171 Info.VolatileStorageSize ==
1172 getDataLayout().getTypeAllocSizeInBits(ElementTy)) &&
1173 "Storage size does not match the element type size");
1174 }
1175 assert(Info.Size > 0 && "Empty bitfield!");
1176 assert(static_cast<unsigned>(Info.Offset) + Info.Size <= Info.StorageSize &&
1177 "Bitfield outside of its allocated storage");
1178 }
1179#endif
1180
1181 return RL;
1182}
1183
1184void CGRecordLayout::print(raw_ostream &OS) const {
1185 OS << "<CGRecordLayout\n";
1186 OS << " LLVMType:" << *CompleteObjectType << "\n";
1187 if (BaseSubobjectType)
1188 OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
1189 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
1190 OS << " BitFields:[\n";
1191
1192 // Print bit-field infos in declaration order.
1193 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
1194 for (const auto &BitField : BitFields) {
1195 const RecordDecl *RD = BitField.first->getParent();
1196 unsigned Index = 0;
1197 for (RecordDecl::field_iterator it2 = RD->field_begin();
1198 *it2 != BitField.first; ++it2)
1199 ++Index;
1200 BFIs.push_back(x: std::make_pair(x&: Index, y: &BitField.second));
1201 }
1202 llvm::array_pod_sort(Start: BFIs.begin(), End: BFIs.end());
1203 for (auto &BFI : BFIs) {
1204 OS.indent(NumSpaces: 4);
1205 BFI.second->print(OS);
1206 OS << "\n";
1207 }
1208
1209 OS << "]>\n";
1210}
1211
1212LLVM_DUMP_METHOD void CGRecordLayout::dump() const {
1213 print(OS&: llvm::errs());
1214}
1215
1216void CGBitFieldInfo::print(raw_ostream &OS) const {
1217 OS << "<CGBitFieldInfo"
1218 << " Offset:" << Offset << " Size:" << Size << " IsSigned:" << IsSigned
1219 << " StorageSize:" << StorageSize
1220 << " StorageOffset:" << StorageOffset.getQuantity()
1221 << " VolatileOffset:" << VolatileOffset
1222 << " VolatileStorageSize:" << VolatileStorageSize
1223 << " VolatileStorageOffset:" << VolatileStorageOffset.getQuantity() << ">";
1224}
1225
1226LLVM_DUMP_METHOD void CGBitFieldInfo::dump() const {
1227 print(OS&: llvm::errs());
1228}
1229