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