clang 24.0.0git
CGRecordLayoutBuilder.cpp
Go to the documentation of this file.
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"
18#include "clang/AST/Attr.h"
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/Expr.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(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(NumBits, Context.getCharWidth());
127 return llvm::Type::getIntNTy(Types.getLLVMContext(), AlignedBits);
128 }
129 /// Get the LLVM type sized as one character unit.
130 llvm::Type *getCharType() const {
131 return llvm::Type::getIntNTy(Types.getLLVMContext(),
132 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(Type, 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(FD->getType());
145 if (!FD->isBitField()) return Type;
146 if (isDiscreteBitFieldABI()) return Type;
147 return getIntNType(std::min(FD->getBitWidthValue(),
148 (unsigned)Context.toBits(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(BitOffset);
156 }
157 CharUnits getSize(llvm::Type *Type) const {
158 return CharUnits::fromQuantity(DataLayout.getTypeAllocSize(Type));
159 }
160 CharUnits getAlignment(llvm::Type *Type) const {
161 return CharUnits::fromQuantity(DataLayout.getABITypeAlign(Type));
162 }
163 bool isZeroInitializable(const FieldDecl *FD) const {
164 return Types.isZeroInitializable(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(getByteArrayType(Size));
172 }
173 uint64_t getFieldBitOffset(const FieldDecl *FD) const {
174 return Layout.getFieldOffset(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);
184 accumulateBitFields(bool isNonVirtualBaseType,
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>(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()];
240 Info.Offset = (unsigned)(getFieldBitOffset(FD) - Context.toBits(StartOffset));
241 Info.Size = FD->getBitWidthValue();
242 Info.StorageSize = (unsigned)DataLayout.getTypeAllocSizeInBits(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;
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(NVBaseType);
282 computeVolatileBitfields();
283 return;
284 }
285 accumulateFields(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(Members);
299 checkBitfieldClipping(NVBaseType);
300 Members.push_back(StorageInfo(Size, getIntNType(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(Field);
324 if (LayoutSize < getSize(FieldType))
325 FieldType = getByteArrayType(LayoutSize);
326 setBitFieldInfo(Field, CharUnits::Zero(), FieldType);
327 }
328 Fields[Field->getCanonicalDecl()] = 0;
329 llvm::Type *FieldType = getStorageType(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(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(FieldType) > getAlignment(StorageType) ||
353 (getAlignment(FieldType) == getAlignment(StorageType) &&
354 getSize(FieldType) > getSize(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(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(StorageType))
363 StorageType = getByteArrayType(LayoutSize);
364 FieldTypes.push_back(StorageType);
365 appendPaddingBytes(LayoutSize - getSize(StorageType));
366 // Set packed if we need it.
367 const auto StorageAlignment = getAlignment(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(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, *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(MemberInfo(
391 bitsToCharUnits(getFieldBitOffset(*Field)), MemberInfo::Field,
392 Field->isPotentiallyOverlapping()
393 ? getStorageType(Field->getType()->getAsCXXRecordDecl())
394 : getStorageType(*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.
406CGRecordLowering::accumulateBitFields(bool isNonVirtualBaseType,
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(*Field);
427 llvm::Type *Type = Types.ConvertTypeForMem(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(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(StorageInfo(bitsToCharUnits(StartBitOffset), 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(MemberInfo(bitsToCharUnits(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(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(*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(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(Context.toBits(AccessSize));
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(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, *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(getFieldBitOffset(*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(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(AccessSize);
689 } else {
690 Type = getIntNType(Context.toBits(AccessSize));
691 assert(getSize(Type) == AccessSize &&
692 "Unclipped access must be clipped");
693 }
694 Members.push_back(StorageInfo(BeginOffset, Type));
695 for (; Begin != BestEnd; ++Begin)
696 if (!Begin->isZeroLengthBitField())
697 Members.push_back(
698 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(MemberInfo(CharUnits::Zero(), MemberInfo::Base,
721 getStorageType(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, Base.getType()) &&
732 !Context.getASTRecordLayout(BaseDecl).getNonVirtualSize().isZero())
733 Members.push_back(MemberInfo(Layout.getBaseClassOffset(BaseDecl),
734 MemberInfo::Base, getStorageType(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(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(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(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(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(AbsoluteOffset & ~(StorageSize - 1));
798 const CharUnits End = StorageOffset +
799 Context.toCharUnitsFromBits(StorageSize) -
801
802 const ASTRecordLayout &Layout =
803 Context.getASTRecordLayout(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 Layout.getFieldOffset(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 Types.ConvertTypeForMem(F->getType())->getPrimitiveSizeInBits()) -
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.
850 StorageOffset / Context.toCharUnitsFromBits(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 MemberInfo(CharUnits::Zero(), MemberInfo::VFPtr,
860 llvm::PointerType::getUnqual(Types.getLLVMContext())));
861 if (Layout.hasOwnVBPtr())
862 Members.push_back(
863 MemberInfo(Layout.getVBPtrOffset(), MemberInfo::VBPtr,
864 llvm::PointerType::getUnqual(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, 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(BaseDecl) && !hasOwnStorage(RD, BaseDecl))
885 continue;
886 ScissorOffset = std::min(ScissorOffset,
887 Layout.getVBaseClassOffset(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, Base.getType()))
897 continue;
898 CharUnits Offset = Layout.getVBaseClassOffset(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(BaseDecl) &&
903 !hasOwnStorage(RD, BaseDecl)) {
904 Members.push_back(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(BaseDecl)->second.hasVtorDisp())
910 Members.push_back(StorageInfo(Offset - CharUnits::fromQuantity(4),
911 getIntNType(32)));
912 Members.push_back(MemberInfo(Offset, MemberInfo::VBase,
913 getStorageType(BaseDecl), BaseDecl));
914 }
915}
916
917bool CGRecordLowering::hasOwnStorage(const CXXRecordDecl *Decl,
918 const CXXRecordDecl *Query) const {
919 const ASTRecordLayout &DeclLayout = Context.getASTRecordLayout(Decl);
920 if (DeclLayout.isPrimaryBaseVirtual() && DeclLayout.getPrimaryBase() == Query)
921 return false;
922 for (const auto &Base : Decl->bases())
923 if (!hasOwnStorage(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(Member->FD))
934 continue;
935 IsZeroInitializable = IsZeroInitializableAsBase = false;
936 } else if (Member->Kind == MemberInfo::Base ||
937 Member->Kind == MemberInfo::VBase) {
938 if (isZeroInitializable(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(getAlignment(Member.Data)))
978 Packed = true;
979 if (Member.Offset < NVSize)
980 NVAlignment = std::max(NVAlignment, getAlignment(Member.Data));
981 Alignment = std::max(Alignment, getAlignment(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(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(NVAlignment))
991 Packed = true;
992 // Update the alignment of the sentinel.
993 if (!Packed)
994 Members.back().Data = getIntNType(Context.toBits(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(Packed ? CharUnits::One() : getAlignment(Member.Data)))
1008 Padding.push_back(std::make_pair(Size, Offset - Size));
1009 Size = Offset + getSize(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(StorageInfo(Pad.first, getByteArrayType(Pad.second)));
1016 llvm::stable_sort(Members);
1017}
1018
1019void CGRecordLowering::fillOutputFields() {
1020 for (const MemberInfo &Member : Members) {
1021 if (Member.Data)
1022 FieldTypes.push_back(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(Member.FD, Member.Offset, 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
1040 const FieldDecl *FD,
1041 uint64_t Offset, uint64_t Size,
1042 uint64_t StorageSize,
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(FD->getType());
1048 CharUnits TypeSizeInBytes =
1049 CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(Ty));
1050 uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
1051
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
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=*/false);
1083
1084 // If we're in C++, compute the base subobject type.
1085 llvm::StructType *BaseTy = nullptr;
1086 if (isa<CXXRecordDecl>(D)) {
1087 BaseTy = Ty;
1088 if (Builder.Layout.getNonVirtualSize() != Builder.Layout.getSize()) {
1089 CGRecordLowering BaseBuilder(*this, D, /*Packed=*/Builder.Packed);
1090 BaseBuilder.lower(/*NonVirtualBaseType=*/true);
1091 BaseTy = llvm::StructType::create(
1092 getLLVMContext(), BaseBuilder.FieldTypes, "", BaseBuilder.Packed);
1093 addRecordTypeName(D, BaseTy, ".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(Builder.FieldTypes, Builder.Packed);
1105
1106 auto RL = std::make_unique<CGRecordLayout>(
1107 Ty, BaseTy, (bool)Builder.IsZeroInitializable,
1108 (bool)Builder.IsZeroInitializableAsBase);
1109
1110 RL->NonVirtualBases.swap(Builder.NonVirtualBases);
1111 RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
1112
1113 // Add all the field numbers.
1114 RL->FieldInfo.swap(Builder.Fields);
1115
1116 // Add bitfield info.
1117 RL->BitFields.swap(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(llvm::outs());
1124 llvm::outs() << "\nLayout: ";
1125 RL->print(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);
1153 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
1154 const FieldDecl *FD = *it;
1155
1156 // Ignore zero-sized fields.
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(std::make_pair(Index, &BitField.second));
1226 }
1227 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
1228 for (auto &BFI : BFIs) {
1229 OS.indent(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(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(llvm::errs());
1253}
Defines the clang::ASTContext interface.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
static void print(llvm::raw_ostream &OS, const T &V, const Context &Ctx, QualType Ty)
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D,...
bool isNearlyEmpty(const CXXRecordDecl *RD) const
int64_t toBits(CharUnits CharSize) const
Convert a size in characters to a size in bits.
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:965
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
uint64_t getCharWidth() const
Return the size of the character type, in bits.
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
bool hasOwnVFPtr() const
hasOwnVFPtr - Does this class provide its own virtual-function table pointer, rather than inheriting ...
CharUnits getAlignment() const
getAlignment - Get the record alignment in characters.
bool hasOwnVBPtr() const
hasOwnVBPtr - Does this class provide its own virtual-base table pointer, rather than inheriting one ...
CharUnits getSize() const
getSize - Get the record size in characters.
unsigned getFieldCount() const
getFieldCount - Get the number of fields in the layout.
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
CharUnits getVBPtrOffset() const
getVBPtrOffset - Get the offset for virtual base table pointer.
CharUnits getDataSize() const
getDataSize() - Get the record data size, which is the record size without tail padding,...
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const
getVBaseClassOffset - Get the offset, in chars, for the given base class.
const VBaseOffsetsMapTy & getVBaseOffsetsMap() const
const CXXRecordDecl * getPrimaryBase() const
getPrimaryBase - Get the primary base for this record.
bool isPrimaryBaseVirtual() const
isPrimaryBaseVirtual - Get whether the primary base for this record is virtual or not.
CharUnits getNonVirtualSize() const
getNonVirtualSize - Get the non-virtual size (in chars) of an object, which is the size of the object...
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition CharUnits.h:122
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition CharUnits.h:185
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition CharUnits.h:58
bool isMultipleOf(CharUnits N) const
Test whether this is a multiple of the other value.
Definition CharUnits.h:143
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:63
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition CharUnits.h:53
This class organizes the cross-module state that is used while lowering AST types to LLVM types.
const CodeGenOptions & getCodeGenOpts() const
ASTContext & getContext() const
std::unique_ptr< CGRecordLayout > ComputeRecordLayout(const RecordDecl *D, llvm::StructType *Ty)
Compute a new LLVM record layout object for the given record.
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
llvm::LLVMContext & getLLVMContext()
const llvm::DataLayout & getDataLayout() const
void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty, StringRef suffix)
addRecordTypeName - Compute a name from the given record decl with an optional suffix and name the gi...
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition DeclBase.h:2126
void dump() const
Represents a member of a struct/union/class.
Definition Decl.h:3295
bool isBitField() const
Determines whether this field is a bitfield.
Definition Decl.h:3398
unsigned getBitWidthValue() const
Computes the bit width of this field, if this is a bit field.
Definition Decl.cpp:4816
unsigned getFieldIndex() const
Returns the index of this field within its record, as appropriate for passing to ASTRecordLayout::get...
Definition Decl.h:3380
FieldDecl * getCanonicalDecl() override
Retrieves the canonical declaration of this field.
Definition Decl.h:3542
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition Decl.h:341
Represents a struct/union/class.
Definition Decl.h:4460
field_iterator field_end() const
Definition Decl.h:4666
field_range fields() const
Definition Decl.h:4663
specific_decl_iterator< FieldDecl > field_iterator
Definition Decl.h:4660
field_iterator field_begin() const
Definition Decl.cpp:5339
bool isUnion() const
Definition Decl.h:4063
virtual unsigned getRegisterWidth() const
Return the "preferred" register width on this target.
Definition TargetInfo.h:900
bool hasCheapUnalignedBitFieldAccess() const
Return true iff unaligned accesses are cheap.
Definition TargetInfo.h:914
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
Definition Type.cpp:2319
QualType getType() const
Definition Decl.h:724
bool isAAPCS(const TargetInfo &TargetInfo)
Helper method to check if the underlying ABI is AAPCS.
bool isEmptyRecordForLayout(const ASTContext &Context, QualType T)
isEmptyRecordForLayout - Return true iff a structure contains only empty base classes (per isEmptyRec...
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
Definition CGValue.h:146
bool isEmptyFieldForLayout(const ASTContext &Context, const FieldDecl *FD)
isEmptyFieldForLayout - Return true iff the field is "empty", that is, either a zero-width bit-field ...
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
@ Type
The name was classified as a type.
Definition Sema.h:558
unsigned long uint64_t
#define true
Definition stdbool.h:25
Structure with information about how a bitfield should be accessed.
CharUnits StorageOffset
The offset of the bitfield storage from the start of the struct.
CharUnits VolatileStorageOffset
The offset of the bitfield storage from the start of the struct.
unsigned VolatileOffset
The offset within a contiguous run of bitfields that are represented as a single "field" within the L...
unsigned Offset
The offset within a contiguous run of bitfields that are represented as a single "field" within the L...
unsigned VolatileStorageSize
The storage size in bits which should be used when accessing this bitfield.
unsigned Size
The total size of the bit-field, in bits.
unsigned StorageSize
The storage size in bits which should be used when accessing this bitfield.
unsigned IsSigned
Whether the bit-field is signed.
static CGBitFieldInfo MakeInfo(class CodeGenTypes &Types, const FieldDecl *FD, uint64_t Offset, uint64_t Size, uint64_t StorageSize, CharUnits StorageOffset)
Given a bit-field decl, build an appropriate helper object for accessing that field (which is expecte...