1 //===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Builder implementation for CGRecordLayout objects.
12 //===----------------------------------------------------------------------===//
14 #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/Frontend/CodeGenOptions.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/DerivedTypes.h"
26 #include "llvm/IR/Type.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/raw_ostream.h"
29 using namespace clang;
30 using namespace CodeGen;
34 class CGRecordLayoutBuilder {
36 /// FieldTypes - Holds the LLVM types that the struct is created from.
38 SmallVector<llvm::Type *, 16> FieldTypes;
40 /// BaseSubobjectType - Holds the LLVM type for the non-virtual part
41 /// of the struct. For example, consider:
43 /// struct A { int i; };
44 /// struct B { void *v; };
45 /// struct C : virtual A, B { };
47 /// The LLVM type of C will be
48 /// %struct.C = type { i32 (...)**, %struct.A, i32, %struct.B }
50 /// And the LLVM type of the non-virtual base struct will be
51 /// %struct.C.base = type { i32 (...)**, %struct.A, i32 }
53 /// This only gets initialized if the base subobject type is
54 /// different from the complete-object type.
55 llvm::StructType *BaseSubobjectType;
57 /// FieldInfo - Holds a field and its corresponding LLVM field number.
58 llvm::DenseMap<const FieldDecl *, unsigned> Fields;
60 /// BitFieldInfo - Holds location and size information about a bit field.
61 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
63 llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
64 llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases;
66 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
67 /// primary base classes for some other direct or indirect base class.
68 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
70 /// LaidOutVirtualBases - A set of all laid out virtual bases, used to avoid
71 /// avoid laying out virtual bases more than once.
72 llvm::SmallPtrSet<const CXXRecordDecl *, 4> LaidOutVirtualBases;
74 /// IsZeroInitializable - Whether this struct can be C++
75 /// zero-initialized with an LLVM zeroinitializer.
76 bool IsZeroInitializable;
77 bool IsZeroInitializableAsBase;
79 /// Packed - Whether the resulting LLVM struct will be packed or not.
85 /// LastLaidOutBaseInfo - Contains the offset and non-virtual size of the
86 /// last base laid out. Used so that we can replace the last laid out base
87 /// type with an i8 array if needed.
88 struct LastLaidOutBaseInfo {
90 CharUnits NonVirtualSize;
92 bool isValid() const { return !NonVirtualSize.isZero(); }
93 void invalidate() { NonVirtualSize = CharUnits::Zero(); }
97 /// Alignment - Contains the alignment of the RecordDecl.
100 /// NextFieldOffset - Holds the next field offset.
101 CharUnits NextFieldOffset;
103 /// LayoutUnionField - Will layout a field in an union and return the type
104 /// that the field will have.
105 llvm::Type *LayoutUnionField(const FieldDecl *Field,
106 const ASTRecordLayout &Layout);
108 /// LayoutUnion - Will layout a union RecordDecl.
109 void LayoutUnion(const RecordDecl *D);
111 /// Lay out a sequence of contiguous bitfields.
112 bool LayoutBitfields(const ASTRecordLayout &Layout,
113 unsigned &FirstFieldNo,
114 RecordDecl::field_iterator &FI,
115 RecordDecl::field_iterator FE);
117 /// LayoutFields - try to layout all fields in the record decl.
118 /// Returns false if the operation failed because the struct is not packed.
119 bool LayoutFields(const RecordDecl *D);
121 /// Layout a single base, virtual or non-virtual
122 bool LayoutBase(const CXXRecordDecl *base,
123 const CGRecordLayout &baseLayout,
124 CharUnits baseOffset);
126 /// LayoutVirtualBase - layout a single virtual base.
127 bool LayoutVirtualBase(const CXXRecordDecl *base,
128 CharUnits baseOffset);
130 /// LayoutVirtualBases - layout the virtual bases of a record decl.
131 bool LayoutVirtualBases(const CXXRecordDecl *RD,
132 const ASTRecordLayout &Layout);
134 /// MSLayoutVirtualBases - layout the virtual bases of a record decl,
136 bool MSLayoutVirtualBases(const CXXRecordDecl *RD,
137 const ASTRecordLayout &Layout);
139 /// LayoutNonVirtualBase - layout a single non-virtual base.
140 bool LayoutNonVirtualBase(const CXXRecordDecl *base,
141 CharUnits baseOffset);
143 /// LayoutNonVirtualBases - layout the virtual bases of a record decl.
144 bool LayoutNonVirtualBases(const CXXRecordDecl *RD,
145 const ASTRecordLayout &Layout);
147 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
148 bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
150 /// LayoutField - layout a single field. Returns false if the operation failed
151 /// because the current struct is not packed.
152 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
154 /// LayoutBitField - layout a single bit field.
155 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
157 /// AppendField - Appends a field with the given offset and type.
158 void AppendField(CharUnits fieldOffset, llvm::Type *FieldTy);
160 /// AppendPadding - Appends enough padding bytes so that the total
161 /// struct size is a multiple of the field alignment.
162 void AppendPadding(CharUnits fieldOffset, CharUnits fieldAlignment);
164 /// ResizeLastBaseFieldIfNecessary - Fields and bases can be laid out in the
165 /// tail padding of a previous base. If this happens, the type of the previous
166 /// base needs to be changed to an array of i8. Returns true if the last
167 /// laid out base was resized.
168 bool ResizeLastBaseFieldIfNecessary(CharUnits offset);
170 /// getByteArrayType - Returns a byte array type with the given number of
172 llvm::Type *getByteArrayType(CharUnits NumBytes);
174 /// AppendBytes - Append a given number of bytes to the record.
175 void AppendBytes(CharUnits numBytes);
177 /// AppendTailPadding - Append enough tail padding so that the type will have
179 void AppendTailPadding(CharUnits RecordSize);
181 CharUnits getTypeAlignment(llvm::Type *Ty) const;
183 /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
184 /// LLVM element types.
185 CharUnits getAlignmentAsLLVMStruct() const;
187 /// CheckZeroInitializable - Check if the given type contains a pointer
189 void CheckZeroInitializable(QualType T);
192 CGRecordLayoutBuilder(CodeGenTypes &Types)
193 : BaseSubobjectType(0),
194 IsZeroInitializable(true), IsZeroInitializableAsBase(true),
195 Packed(false), Types(Types) { }
197 /// Layout - Will layout a RecordDecl.
198 void Layout(const RecordDecl *D);
203 void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
204 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
205 Alignment = Layout.getAlignment();
206 Packed = D->hasAttr<PackedAttr>() || Layout.getSize() % Alignment != 0;
216 // We weren't able to layout the struct. Try again with a packed struct
218 LastLaidOutBase.invalidate();
219 NextFieldOffset = CharUnits::Zero();
223 NonVirtualBases.clear();
224 VirtualBases.clear();
229 CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
231 uint64_t Offset, uint64_t Size,
232 uint64_t StorageSize,
233 uint64_t StorageAlignment) {
234 llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType());
235 CharUnits TypeSizeInBytes =
236 CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(Ty));
237 uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
239 bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
241 if (Size > TypeSizeInBits) {
242 // We have a wide bit-field. The extra bits are only used for padding, so
243 // if we have a bitfield of type T, with size N:
247 // We can just assume that it's:
251 Size = TypeSizeInBits;
254 // Reverse the bit offsets for big endian machines. Because we represent
255 // a bitfield as a single large integer load, we can imagine the bits
256 // counting from the most-significant-bit instead of the
257 // least-significant-bit.
258 if (Types.getDataLayout().isBigEndian()) {
259 Offset = StorageSize - (Offset + Size);
262 return CGBitFieldInfo(Offset, Size, IsSigned, StorageSize, StorageAlignment);
265 /// \brief Layout the range of bitfields from BFI to BFE as contiguous storage.
266 bool CGRecordLayoutBuilder::LayoutBitfields(const ASTRecordLayout &Layout,
267 unsigned &FirstFieldNo,
268 RecordDecl::field_iterator &FI,
269 RecordDecl::field_iterator FE) {
271 uint64_t FirstFieldOffset = Layout.getFieldOffset(FirstFieldNo);
272 uint64_t NextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
274 unsigned CharAlign = Types.getTarget().getCharAlign();
275 assert(FirstFieldOffset % CharAlign == 0 &&
276 "First field offset is misaligned");
277 CharUnits FirstFieldOffsetInBytes
278 = Types.getContext().toCharUnitsFromBits(FirstFieldOffset);
280 unsigned StorageAlignment
281 = llvm::MinAlign(Alignment.getQuantity(),
282 FirstFieldOffsetInBytes.getQuantity());
284 if (FirstFieldOffset < NextFieldOffsetInBits) {
285 CharUnits FieldOffsetInCharUnits =
286 Types.getContext().toCharUnitsFromBits(FirstFieldOffset);
288 // Try to resize the last base field.
289 if (!ResizeLastBaseFieldIfNecessary(FieldOffsetInCharUnits))
290 llvm_unreachable("We must be able to resize the last base if we need to "
291 "pack bits into it.");
293 NextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
294 assert(FirstFieldOffset >= NextFieldOffsetInBits);
297 // Append padding if necessary.
298 AppendPadding(Types.getContext().toCharUnitsFromBits(FirstFieldOffset),
301 // Find the last bitfield in a contiguous run of bitfields.
302 RecordDecl::field_iterator BFI = FI;
303 unsigned LastFieldNo = FirstFieldNo;
304 uint64_t NextContiguousFieldOffset = FirstFieldOffset;
305 for (RecordDecl::field_iterator FJ = FI;
306 (FJ != FE && (*FJ)->isBitField() &&
307 NextContiguousFieldOffset == Layout.getFieldOffset(LastFieldNo) &&
308 (*FJ)->getBitWidthValue(Types.getContext()) != 0); FI = FJ++) {
309 NextContiguousFieldOffset += (*FJ)->getBitWidthValue(Types.getContext());
312 // We must use packed structs for packed fields, and also unnamed bit
313 // fields since they don't affect the struct alignment.
314 if (!Packed && ((*FJ)->hasAttr<PackedAttr>() || !(*FJ)->getDeclName()))
317 RecordDecl::field_iterator BFE = llvm::next(FI);
319 assert(LastFieldNo >= FirstFieldNo && "Empty run of contiguous bitfields");
320 FieldDecl *LastFD = *FI;
322 // Find the last bitfield's offset, add its size, and round it up to the
323 // character alignment to compute the storage required.
324 uint64_t LastFieldOffset = Layout.getFieldOffset(LastFieldNo);
325 uint64_t LastFieldSize = LastFD->getBitWidthValue(Types.getContext());
326 uint64_t TotalBits = (LastFieldOffset + LastFieldSize) - FirstFieldOffset;
327 CharUnits StorageBytes = Types.getContext().toCharUnitsFromBits(
328 llvm::RoundUpToAlignment(TotalBits, CharAlign));
329 uint64_t StorageBits = Types.getContext().toBits(StorageBytes);
331 // Grow the storage to encompass any known padding in the layout when doing
332 // so will make the storage a power-of-two. There are two cases when we can
333 // do this. The first is when we have a subsequent field and can widen up to
334 // its offset. The second is when the data size of the AST record layout is
335 // past the end of the current storage. The latter is true when there is tail
336 // padding on a struct and no members of a super class can be packed into it.
338 // Note that we widen the storage as much as possible here to express the
339 // maximum latitude the language provides, and rely on the backend to lower
340 // these in conjunction with shifts and masks to narrower operations where
342 uint64_t EndOffset = Types.getContext().toBits(Layout.getDataSize());
344 // If there are more fields to be laid out, the offset at the end of the
345 // bitfield is the offset of the next field in the record.
346 EndOffset = Layout.getFieldOffset(LastFieldNo + 1);
347 assert(EndOffset >= (FirstFieldOffset + TotalBits) &&
348 "End offset is not past the end of the known storage bits.");
349 uint64_t SpaceBits = EndOffset - FirstFieldOffset;
350 uint64_t LongBits = Types.getTarget().getLongWidth();
351 uint64_t WidenedBits = (StorageBits / LongBits) * LongBits +
352 llvm::NextPowerOf2(StorageBits % LongBits - 1);
353 assert(WidenedBits >= StorageBits && "Widening shrunk the bits!");
354 if (WidenedBits <= SpaceBits) {
355 StorageBits = WidenedBits;
356 StorageBytes = Types.getContext().toCharUnitsFromBits(StorageBits);
357 assert(StorageBits == (uint64_t)Types.getContext().toBits(StorageBytes));
360 unsigned FieldIndex = FieldTypes.size();
361 AppendBytes(StorageBytes);
363 // Now walk the bitfields associating them with this field of storage and
364 // building up the bitfield specific info.
365 unsigned FieldNo = FirstFieldNo;
366 for (; BFI != BFE; ++BFI, ++FieldNo) {
367 FieldDecl *FD = *BFI;
368 uint64_t FieldOffset = Layout.getFieldOffset(FieldNo) - FirstFieldOffset;
369 uint64_t FieldSize = FD->getBitWidthValue(Types.getContext());
370 Fields[FD] = FieldIndex;
371 BitFields[FD] = CGBitFieldInfo::MakeInfo(Types, FD, FieldOffset, FieldSize,
372 StorageBits, StorageAlignment);
374 FirstFieldNo = LastFieldNo;
378 bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
379 uint64_t fieldOffset) {
380 // If the field is packed, then we need a packed struct.
381 if (!Packed && D->hasAttr<PackedAttr>())
384 assert(!D->isBitField() && "Bitfields should be laid out seperately.");
386 CheckZeroInitializable(D->getType());
388 assert(fieldOffset % Types.getTarget().getCharWidth() == 0
389 && "field offset is not on a byte boundary!");
390 CharUnits fieldOffsetInBytes
391 = Types.getContext().toCharUnitsFromBits(fieldOffset);
393 llvm::Type *Ty = Types.ConvertTypeForMem(D->getType());
394 CharUnits typeAlignment = getTypeAlignment(Ty);
396 // If the type alignment is larger then the struct alignment, we must use
398 if (typeAlignment > Alignment) {
399 assert(!Packed && "Alignment is wrong even with packed struct!");
404 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
405 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
406 if (const MaxFieldAlignmentAttr *MFAA =
407 RD->getAttr<MaxFieldAlignmentAttr>()) {
408 if (MFAA->getAlignment() != Types.getContext().toBits(typeAlignment))
414 // Round up the field offset to the alignment of the field type.
415 CharUnits alignedNextFieldOffsetInBytes =
416 NextFieldOffset.RoundUpToAlignment(typeAlignment);
418 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
419 // Try to resize the last base field.
420 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInBytes)) {
421 alignedNextFieldOffsetInBytes =
422 NextFieldOffset.RoundUpToAlignment(typeAlignment);
426 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
427 assert(!Packed && "Could not place field even with packed struct!");
431 AppendPadding(fieldOffsetInBytes, typeAlignment);
433 // Now append the field.
434 Fields[D] = FieldTypes.size();
435 AppendField(fieldOffsetInBytes, Ty);
437 LastLaidOutBase.invalidate();
442 CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
443 const ASTRecordLayout &Layout) {
445 if (Field->isBitField()) {
446 uint64_t FieldSize = Field->getBitWidthValue(Types.getContext());
448 // Ignore zero sized bit fields.
452 unsigned StorageBits = llvm::RoundUpToAlignment(
453 FieldSize, Types.getTarget().getCharAlign());
454 CharUnits NumBytesToAppend
455 = Types.getContext().toCharUnitsFromBits(StorageBits);
457 llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
458 if (NumBytesToAppend > CharUnits::One())
459 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend.getQuantity());
461 // Add the bit field info.
462 BitFields[Field] = CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize,
464 Alignment.getQuantity());
468 // This is a regular union field.
469 return Types.ConvertTypeForMem(Field->getType());
472 void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
473 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
475 const ASTRecordLayout &layout = Types.getContext().getASTRecordLayout(D);
477 llvm::Type *unionType = 0;
478 CharUnits unionSize = CharUnits::Zero();
479 CharUnits unionAlign = CharUnits::Zero();
481 bool hasOnlyZeroSizedBitFields = true;
482 bool checkedFirstFieldZeroInit = false;
484 unsigned fieldNo = 0;
485 for (RecordDecl::field_iterator field = D->field_begin(),
486 fieldEnd = D->field_end(); field != fieldEnd; ++field, ++fieldNo) {
487 assert(layout.getFieldOffset(fieldNo) == 0 &&
488 "Union field offset did not start at the beginning of record!");
489 llvm::Type *fieldType = LayoutUnionField(*field, layout);
494 if (field->getDeclName() && !checkedFirstFieldZeroInit) {
495 CheckZeroInitializable(field->getType());
496 checkedFirstFieldZeroInit = true;
499 hasOnlyZeroSizedBitFields = false;
501 CharUnits fieldAlign = CharUnits::fromQuantity(
502 Types.getDataLayout().getABITypeAlignment(fieldType));
503 CharUnits fieldSize = CharUnits::fromQuantity(
504 Types.getDataLayout().getTypeAllocSize(fieldType));
506 if (fieldAlign < unionAlign)
509 if (fieldAlign > unionAlign || fieldSize > unionSize) {
510 unionType = fieldType;
511 unionAlign = fieldAlign;
512 unionSize = fieldSize;
516 // Now add our field.
518 AppendField(CharUnits::Zero(), unionType);
520 if (getTypeAlignment(unionType) > layout.getAlignment()) {
521 // We need a packed struct.
523 unionAlign = CharUnits::One();
526 if (unionAlign.isZero()) {
527 (void)hasOnlyZeroSizedBitFields;
528 assert(hasOnlyZeroSizedBitFields &&
529 "0-align record did not have all zero-sized bit-fields!");
530 unionAlign = CharUnits::One();
533 // Append tail padding.
534 CharUnits recordSize = layout.getSize();
535 if (recordSize > unionSize)
536 AppendPadding(recordSize, unionAlign);
539 bool CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base,
540 const CGRecordLayout &baseLayout,
541 CharUnits baseOffset) {
542 ResizeLastBaseFieldIfNecessary(baseOffset);
544 AppendPadding(baseOffset, CharUnits::One());
546 const ASTRecordLayout &baseASTLayout
547 = Types.getContext().getASTRecordLayout(base);
549 LastLaidOutBase.Offset = NextFieldOffset;
550 LastLaidOutBase.NonVirtualSize = baseASTLayout.getNonVirtualSize();
552 llvm::StructType *subobjectType = baseLayout.getBaseSubobjectLLVMType();
553 if (getTypeAlignment(subobjectType) > Alignment)
556 AppendField(baseOffset, subobjectType);
560 bool CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base,
561 CharUnits baseOffset) {
562 // Ignore empty bases.
563 if (base->isEmpty()) return true;
565 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
566 if (IsZeroInitializableAsBase) {
567 assert(IsZeroInitializable &&
568 "class zero-initializable as base but not as complete object");
570 IsZeroInitializable = IsZeroInitializableAsBase =
571 baseLayout.isZeroInitializableAsBase();
574 if (!LayoutBase(base, baseLayout, baseOffset))
576 NonVirtualBases[base] = (FieldTypes.size() - 1);
581 CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base,
582 CharUnits baseOffset) {
583 // Ignore empty bases.
584 if (base->isEmpty()) return true;
586 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
587 if (IsZeroInitializable)
588 IsZeroInitializable = baseLayout.isZeroInitializableAsBase();
590 if (!LayoutBase(base, baseLayout, baseOffset))
592 VirtualBases[base] = (FieldTypes.size() - 1);
597 CGRecordLayoutBuilder::MSLayoutVirtualBases(const CXXRecordDecl *RD,
598 const ASTRecordLayout &Layout) {
599 if (!RD->getNumVBases())
602 // The vbases list is uniqued and ordered by a depth-first
603 // traversal, which is what we need here.
604 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
605 E = RD->vbases_end(); I != E; ++I) {
607 const CXXRecordDecl *BaseDecl =
608 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
610 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
611 if (!LayoutVirtualBase(BaseDecl, vbaseOffset))
617 /// LayoutVirtualBases - layout the non-virtual bases of a record decl.
619 CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
620 const ASTRecordLayout &Layout) {
621 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
622 E = RD->bases_end(); I != E; ++I) {
623 const CXXRecordDecl *BaseDecl =
624 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
626 // We only want to lay out virtual bases that aren't indirect primary bases
627 // of some other base.
628 if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
629 // Only lay out the base once.
630 if (!LaidOutVirtualBases.insert(BaseDecl))
633 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
634 if (!LayoutVirtualBase(BaseDecl, vbaseOffset))
638 if (!BaseDecl->getNumVBases()) {
639 // This base isn't interesting since it doesn't have any virtual bases.
643 if (!LayoutVirtualBases(BaseDecl, Layout))
650 CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
651 const ASTRecordLayout &Layout) {
652 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
654 // If we have a primary base, lay it out first.
656 if (!Layout.isPrimaryBaseVirtual()) {
657 if (!LayoutNonVirtualBase(PrimaryBase, CharUnits::Zero()))
660 if (!LayoutVirtualBase(PrimaryBase, CharUnits::Zero()))
664 // Otherwise, add a vtable / vf-table if the layout says to do so.
665 } else if (Layout.hasOwnVFPtr()) {
666 llvm::Type *FunctionType =
667 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
669 llvm::Type *VTableTy = FunctionType->getPointerTo();
671 if (getTypeAlignment(VTableTy) > Alignment) {
672 // FIXME: Should we allow this to happen in Sema?
673 assert(!Packed && "Alignment is wrong even with packed struct!");
677 assert(NextFieldOffset.isZero() &&
678 "VTable pointer must come first!");
679 AppendField(CharUnits::Zero(), VTableTy->getPointerTo());
682 // Layout the non-virtual bases.
683 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
684 E = RD->bases_end(); I != E; ++I) {
688 const CXXRecordDecl *BaseDecl =
689 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
691 // We've already laid out the primary base.
692 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
695 if (!LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl)))
699 // Add a vb-table pointer if the layout insists.
700 if (Layout.hasOwnVBPtr()) {
701 CharUnits VBPtrOffset = Layout.getVBPtrOffset();
702 llvm::Type *Vbptr = llvm::Type::getInt32PtrTy(Types.getLLVMContext());
703 AppendPadding(VBPtrOffset, getTypeAlignment(Vbptr));
704 AppendField(VBPtrOffset, Vbptr);
711 CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
712 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
714 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
715 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
716 CharUnits AlignedNonVirtualTypeSize =
717 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
719 // First check if we can use the same fields as for the complete class.
720 CharUnits RecordSize = Layout.getSize();
721 if (AlignedNonVirtualTypeSize == RecordSize)
724 // Check if we need padding.
725 CharUnits AlignedNextFieldOffset =
726 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
728 if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) {
729 assert(!Packed && "cannot layout even as packed struct");
730 return false; // Needs packing.
733 bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset);
735 CharUnits NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
736 FieldTypes.push_back(getByteArrayType(NumBytes));
739 BaseSubobjectType = llvm::StructType::create(Types.getLLVMContext(),
740 FieldTypes, "", Packed);
741 Types.addRecordTypeName(RD, BaseSubobjectType, ".base");
743 // Pull the padding back off.
745 FieldTypes.pop_back();
750 bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
751 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
752 assert(!Alignment.isZero() && "Did not set alignment!");
754 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
756 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
758 if (!LayoutNonVirtualBases(RD, Layout))
761 unsigned FieldNo = 0;
763 for (RecordDecl::field_iterator FI = D->field_begin(), FE = D->field_end();
764 FI != FE; ++FI, ++FieldNo) {
767 // If this field is a bitfield, layout all of the consecutive
768 // non-zero-length bitfields and the last zero-length bitfield; these will
769 // all share storage.
770 if (FD->isBitField()) {
771 // If all we have is a zero-width bitfield, skip it.
772 if (FD->getBitWidthValue(Types.getContext()) == 0)
775 // Layout this range of bitfields.
776 if (!LayoutBitfields(Layout, FieldNo, FI, FE)) {
778 "Could not layout bitfields even with a packed LLVM struct!");
781 assert(FI != FE && "Advanced past the last bitfield");
785 if (!LayoutField(FD, Layout.getFieldOffset(FieldNo))) {
787 "Could not layout fields even with a packed LLVM struct!");
793 // We've laid out the non-virtual bases and the fields, now compute the
794 // non-virtual base field types.
795 if (!ComputeNonVirtualBaseType(RD)) {
796 assert(!Packed && "Could not layout even with a packed LLVM struct!");
800 // Lay out the virtual bases. The MS ABI uses a different
801 // algorithm here due to the lack of primary virtual bases.
802 if (Types.getTarget().getCXXABI().hasPrimaryVBases()) {
803 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
804 if (Layout.isPrimaryBaseVirtual())
805 IndirectPrimaryBases.insert(Layout.getPrimaryBase());
807 if (!LayoutVirtualBases(RD, Layout))
810 if (!MSLayoutVirtualBases(RD, Layout))
815 // Append tail padding if necessary.
816 AppendTailPadding(Layout.getSize());
821 void CGRecordLayoutBuilder::AppendTailPadding(CharUnits RecordSize) {
822 ResizeLastBaseFieldIfNecessary(RecordSize);
824 assert(NextFieldOffset <= RecordSize && "Size mismatch!");
826 CharUnits AlignedNextFieldOffset =
827 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
829 if (AlignedNextFieldOffset == RecordSize) {
830 // We don't need any padding.
834 CharUnits NumPadBytes = RecordSize - NextFieldOffset;
835 AppendBytes(NumPadBytes);
838 void CGRecordLayoutBuilder::AppendField(CharUnits fieldOffset,
839 llvm::Type *fieldType) {
840 CharUnits fieldSize =
841 CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(fieldType));
843 FieldTypes.push_back(fieldType);
845 NextFieldOffset = fieldOffset + fieldSize;
848 void CGRecordLayoutBuilder::AppendPadding(CharUnits fieldOffset,
849 CharUnits fieldAlignment) {
850 assert(NextFieldOffset <= fieldOffset &&
851 "Incorrect field layout!");
853 // Do nothing if we're already at the right offset.
854 if (fieldOffset == NextFieldOffset) return;
856 // If we're not emitting a packed LLVM type, try to avoid adding
857 // unnecessary padding fields.
859 // Round up the field offset to the alignment of the field type.
860 CharUnits alignedNextFieldOffset =
861 NextFieldOffset.RoundUpToAlignment(fieldAlignment);
862 assert(alignedNextFieldOffset <= fieldOffset);
864 // If that's the right offset, we're done.
865 if (alignedNextFieldOffset == fieldOffset) return;
868 // Otherwise we need explicit padding.
869 CharUnits padding = fieldOffset - NextFieldOffset;
870 AppendBytes(padding);
873 bool CGRecordLayoutBuilder::ResizeLastBaseFieldIfNecessary(CharUnits offset) {
874 // Check if we have a base to resize.
875 if (!LastLaidOutBase.isValid())
878 // This offset does not overlap with the tail padding.
879 if (offset >= NextFieldOffset)
882 // Restore the field offset and append an i8 array instead.
883 FieldTypes.pop_back();
884 NextFieldOffset = LastLaidOutBase.Offset;
885 AppendBytes(LastLaidOutBase.NonVirtualSize);
886 LastLaidOutBase.invalidate();
891 llvm::Type *CGRecordLayoutBuilder::getByteArrayType(CharUnits numBytes) {
892 assert(!numBytes.isZero() && "Empty byte arrays aren't allowed.");
894 llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
895 if (numBytes > CharUnits::One())
896 Ty = llvm::ArrayType::get(Ty, numBytes.getQuantity());
901 void CGRecordLayoutBuilder::AppendBytes(CharUnits numBytes) {
902 if (numBytes.isZero())
905 // Append the padding field
906 AppendField(NextFieldOffset, getByteArrayType(numBytes));
909 CharUnits CGRecordLayoutBuilder::getTypeAlignment(llvm::Type *Ty) const {
911 return CharUnits::One();
913 return CharUnits::fromQuantity(Types.getDataLayout().getABITypeAlignment(Ty));
916 CharUnits CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
918 return CharUnits::One();
920 CharUnits maxAlignment = CharUnits::One();
921 for (size_t i = 0; i != FieldTypes.size(); ++i)
922 maxAlignment = std::max(maxAlignment, getTypeAlignment(FieldTypes[i]));
927 /// Merge in whether a field of the given type is zero-initializable.
928 void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
929 // This record already contains a member pointer.
930 if (!IsZeroInitializableAsBase)
933 // Can only have member pointers if we're compiling C++.
934 if (!Types.getContext().getLangOpts().CPlusPlus)
937 const Type *elementType = T->getBaseElementTypeUnsafe();
939 if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) {
940 if (!Types.getCXXABI().isZeroInitializable(MPT))
941 IsZeroInitializable = IsZeroInitializableAsBase = false;
942 } else if (const RecordType *RT = elementType->getAs<RecordType>()) {
943 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
944 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
945 if (!Layout.isZeroInitializable())
946 IsZeroInitializable = IsZeroInitializableAsBase = false;
950 CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D,
951 llvm::StructType *Ty) {
952 CGRecordLayoutBuilder Builder(*this);
956 Ty->setBody(Builder.FieldTypes, Builder.Packed);
958 // If we're in C++, compute the base subobject type.
959 llvm::StructType *BaseTy = 0;
960 if (isa<CXXRecordDecl>(D) && !D->isUnion()) {
961 BaseTy = Builder.BaseSubobjectType;
962 if (!BaseTy) BaseTy = Ty;
966 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
967 Builder.IsZeroInitializableAsBase);
969 RL->NonVirtualBases.swap(Builder.NonVirtualBases);
970 RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
972 // Add all the field numbers.
973 RL->FieldInfo.swap(Builder.Fields);
975 // Add bitfield info.
976 RL->BitFields.swap(Builder.BitFields);
978 // Dump the layout, if requested.
979 if (getContext().getLangOpts().DumpRecordLayouts) {
980 llvm::outs() << "\n*** Dumping IRgen Record Layout\n";
981 llvm::outs() << "Record: ";
982 D->dump(llvm::outs());
983 llvm::outs() << "\nLayout: ";
984 RL->print(llvm::outs());
988 // Verify that the computed LLVM struct size matches the AST layout size.
989 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
991 uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
992 assert(TypeSizeInBits == getDataLayout().getTypeAllocSizeInBits(Ty) &&
993 "Type size mismatch!");
996 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
997 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
998 CharUnits AlignedNonVirtualTypeSize =
999 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
1001 uint64_t AlignedNonVirtualTypeSizeInBits =
1002 getContext().toBits(AlignedNonVirtualTypeSize);
1004 assert(AlignedNonVirtualTypeSizeInBits ==
1005 getDataLayout().getTypeAllocSizeInBits(BaseTy) &&
1006 "Type size mismatch!");
1009 // Verify that the LLVM and AST field offsets agree.
1010 llvm::StructType *ST =
1011 dyn_cast<llvm::StructType>(RL->getLLVMType());
1012 const llvm::StructLayout *SL = getDataLayout().getStructLayout(ST);
1014 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
1015 RecordDecl::field_iterator it = D->field_begin();
1016 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
1017 const FieldDecl *FD = *it;
1019 // For non-bit-fields, just check that the LLVM struct offset matches the
1021 if (!FD->isBitField()) {
1022 unsigned FieldNo = RL->getLLVMFieldNo(FD);
1023 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
1024 "Invalid field offset!");
1028 // Ignore unnamed bit-fields.
1029 if (!FD->getDeclName())
1032 // Don't inspect zero-length bitfields.
1033 if (FD->getBitWidthValue(getContext()) == 0)
1036 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
1037 llvm::Type *ElementTy = ST->getTypeAtIndex(RL->getLLVMFieldNo(FD));
1039 // Unions have overlapping elements dictating their layout, but for
1040 // non-unions we can verify that this section of the layout is the exact
1043 // For unions we verify that the start is zero and the size
1044 // is in-bounds. However, on BE systems, the offset may be non-zero, but
1045 // the size + offset should match the storage size in that case as it
1046 // "starts" at the back.
1047 if (getDataLayout().isBigEndian())
1048 assert(static_cast<unsigned>(Info.Offset + Info.Size) ==
1050 "Big endian union bitfield does not end at the back");
1052 assert(Info.Offset == 0 &&
1053 "Little endian union bitfield with a non-zero offset");
1054 assert(Info.StorageSize <= SL->getSizeInBits() &&
1055 "Union not large enough for bitfield storage");
1057 assert(Info.StorageSize ==
1058 getDataLayout().getTypeAllocSizeInBits(ElementTy) &&
1059 "Storage size does not match the element type size");
1061 assert(Info.Size > 0 && "Empty bitfield!");
1062 assert(static_cast<unsigned>(Info.Offset) + Info.Size <= Info.StorageSize &&
1063 "Bitfield outside of its allocated storage");
1070 void CGRecordLayout::print(raw_ostream &OS) const {
1071 OS << "<CGRecordLayout\n";
1072 OS << " LLVMType:" << *CompleteObjectType << "\n";
1073 if (BaseSubobjectType)
1074 OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
1075 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
1076 OS << " BitFields:[\n";
1078 // Print bit-field infos in declaration order.
1079 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
1080 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
1081 it = BitFields.begin(), ie = BitFields.end();
1083 const RecordDecl *RD = it->first->getParent();
1085 for (RecordDecl::field_iterator
1086 it2 = RD->field_begin(); *it2 != it->first; ++it2)
1088 BFIs.push_back(std::make_pair(Index, &it->second));
1090 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
1091 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
1093 BFIs[i].second->print(OS);
1100 void CGRecordLayout::dump() const {
1101 print(llvm::errs());
1104 void CGBitFieldInfo::print(raw_ostream &OS) const {
1105 OS << "<CGBitFieldInfo"
1106 << " Offset:" << Offset
1108 << " IsSigned:" << IsSigned
1109 << " StorageSize:" << StorageSize
1110 << " StorageAlignment:" << StorageAlignment << ">";
1113 void CGBitFieldInfo::dump() const {
1114 print(llvm::errs());