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"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Attr.h"
17 #include "clang/AST/CXXInheritance.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/RecordLayout.h"
21 #include "clang/Frontend/CodeGenOptions.h"
22 #include "CodeGenTypes.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/Type.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/DataLayout.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.
82 /// IsMsStruct - Whether ms_struct is in effect or not
88 /// LastLaidOutBaseInfo - Contains the offset and non-virtual size of the
89 /// last base laid out. Used so that we can replace the last laid out base
90 /// type with an i8 array if needed.
91 struct LastLaidOutBaseInfo {
93 CharUnits NonVirtualSize;
95 bool isValid() const { return !NonVirtualSize.isZero(); }
96 void invalidate() { NonVirtualSize = CharUnits::Zero(); }
100 /// Alignment - Contains the alignment of the RecordDecl.
103 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
104 /// this will have the number of bits still available in the field.
105 char BitsAvailableInLastField;
107 /// NextFieldOffset - Holds the next field offset.
108 CharUnits NextFieldOffset;
110 /// LayoutUnionField - Will layout a field in an union and return the type
111 /// that the field will have.
112 llvm::Type *LayoutUnionField(const FieldDecl *Field,
113 const ASTRecordLayout &Layout);
115 /// LayoutUnion - Will layout a union RecordDecl.
116 void LayoutUnion(const RecordDecl *D);
118 /// LayoutField - try to layout all fields in the record decl.
119 /// Returns false if the operation failed because the struct is not packed.
120 bool LayoutFields(const RecordDecl *D);
122 /// Layout a single base, virtual or non-virtual
123 bool LayoutBase(const CXXRecordDecl *base,
124 const CGRecordLayout &baseLayout,
125 CharUnits baseOffset);
127 /// LayoutVirtualBase - layout a single virtual base.
128 bool LayoutVirtualBase(const CXXRecordDecl *base,
129 CharUnits baseOffset);
131 /// LayoutVirtualBases - layout the virtual bases of a record decl.
132 bool LayoutVirtualBases(const CXXRecordDecl *RD,
133 const ASTRecordLayout &Layout);
135 /// MSLayoutVirtualBases - layout the virtual bases of a record decl,
137 bool MSLayoutVirtualBases(const CXXRecordDecl *RD,
138 const ASTRecordLayout &Layout);
140 /// LayoutNonVirtualBase - layout a single non-virtual base.
141 bool LayoutNonVirtualBase(const CXXRecordDecl *base,
142 CharUnits baseOffset);
144 /// LayoutNonVirtualBases - layout the virtual bases of a record decl.
145 bool LayoutNonVirtualBases(const CXXRecordDecl *RD,
146 const ASTRecordLayout &Layout);
148 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
149 bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
151 /// LayoutField - layout a single field. Returns false if the operation failed
152 /// because the current struct is not packed.
153 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
155 /// LayoutBitField - layout a single bit field.
156 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
158 /// AppendField - Appends a field with the given offset and type.
159 void AppendField(CharUnits fieldOffset, llvm::Type *FieldTy);
161 /// AppendPadding - Appends enough padding bytes so that the total
162 /// struct size is a multiple of the field alignment.
163 void AppendPadding(CharUnits fieldOffset, CharUnits fieldAlignment);
165 /// ResizeLastBaseFieldIfNecessary - Fields and bases can be laid out in the
166 /// tail padding of a previous base. If this happens, the type of the previous
167 /// base needs to be changed to an array of i8. Returns true if the last
168 /// laid out base was resized.
169 bool ResizeLastBaseFieldIfNecessary(CharUnits offset);
171 /// getByteArrayType - Returns a byte array type with the given number of
173 llvm::Type *getByteArrayType(CharUnits NumBytes);
175 /// AppendBytes - Append a given number of bytes to the record.
176 void AppendBytes(CharUnits numBytes);
178 /// AppendTailPadding - Append enough tail padding so that the type will have
180 void AppendTailPadding(CharUnits RecordSize);
182 CharUnits getTypeAlignment(llvm::Type *Ty) const;
184 /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
185 /// LLVM element types.
186 CharUnits getAlignmentAsLLVMStruct() const;
188 /// CheckZeroInitializable - Check if the given type contains a pointer
190 void CheckZeroInitializable(QualType T);
193 CGRecordLayoutBuilder(CodeGenTypes &Types)
194 : BaseSubobjectType(0),
195 IsZeroInitializable(true), IsZeroInitializableAsBase(true),
196 Packed(false), IsMsStruct(false),
197 Types(Types), BitsAvailableInLastField(0) { }
199 /// Layout - Will layout a RecordDecl.
200 void Layout(const RecordDecl *D);
205 void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
206 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment();
207 Packed = D->hasAttr<PackedAttr>();
209 IsMsStruct = D->isMsStruct(Types.getContext());
219 // We weren't able to layout the struct. Try again with a packed struct
221 LastLaidOutBase.invalidate();
222 NextFieldOffset = CharUnits::Zero();
226 NonVirtualBases.clear();
227 VirtualBases.clear();
232 CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
234 uint64_t FieldOffset,
236 uint64_t ContainingTypeSizeInBits,
237 unsigned ContainingTypeAlign) {
238 assert(ContainingTypeAlign && "Expected alignment to be specified");
240 llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType());
241 CharUnits TypeSizeInBytes =
242 CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(Ty));
243 uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
245 bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
247 if (FieldSize > TypeSizeInBits) {
248 // We have a wide bit-field. The extra bits are only used for padding, so
249 // if we have a bitfield of type T, with size N:
253 // We can just assume that it's:
257 FieldSize = TypeSizeInBits;
260 // in big-endian machines the first fields are in higher bit positions,
261 // so revert the offset. The byte offsets are reversed(back) later.
262 if (Types.getDataLayout().isBigEndian()) {
263 FieldOffset = ((ContainingTypeSizeInBits)-FieldOffset-FieldSize);
266 // Compute the access components. The policy we use is to start by attempting
267 // to access using the width of the bit-field type itself and to always access
268 // at aligned indices of that type. If such an access would fail because it
269 // extends past the bound of the type, then we reduce size to the next smaller
270 // power of two and retry. The current algorithm assumes pow2 sized types,
271 // although this is easy to fix.
273 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
274 CGBitFieldInfo::AccessInfo Components[3];
275 unsigned NumComponents = 0;
276 unsigned AccessedTargetBits = 0; // The number of target bits accessed.
277 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
279 // If requested, widen the initial bit-field access to be register sized. The
280 // theory is that this is most likely to allow multiple accesses into the same
281 // structure to be coalesced, and that the backend should be smart enough to
282 // narrow the store if no coalescing is ever done.
284 // The subsequent code will handle align these access to common boundaries and
285 // guaranteeing that we do not access past the end of the structure.
286 if (Types.getCodeGenOpts().UseRegisterSizedBitfieldAccess) {
287 if (AccessWidth < Types.getTarget().getRegisterWidth())
288 AccessWidth = Types.getTarget().getRegisterWidth();
291 // Round down from the field offset to find the first access position that is
292 // at an aligned offset of the initial access type.
293 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
295 // Adjust initial access size to fit within record.
296 while (AccessWidth > Types.getTarget().getCharWidth() &&
297 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
299 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
302 while (AccessedTargetBits < FieldSize) {
303 // Check that we can access using a type of this size, without reading off
304 // the end of the structure. This can occur with packed structures and
305 // -fno-bitfield-type-align, for example.
306 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
307 // If so, reduce access size to the next smaller power-of-two and retry.
309 assert(AccessWidth >= Types.getTarget().getCharWidth()
310 && "Cannot access under byte size!");
314 // Otherwise, add an access component.
316 // First, compute the bits inside this access which are part of the
317 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
318 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
319 // in the target that we are reading.
320 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
321 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
322 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
323 uint64_t AccessBitsInFieldSize =
324 std::min(AccessWidth + AccessStart,
325 FieldOffset + FieldSize) - AccessBitsInFieldStart;
327 assert(NumComponents < 3 && "Unexpected number of components!");
328 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
330 // FIXME: We still follow the old access pattern of only using the field
331 // byte offset. We should switch this once we fix the struct layout to be
334 // on big-endian machines we reverted the bit offset because first fields are
335 // in higher bits. But this also reverts the bytes, so fix this here by reverting
336 // the byte offset on big-endian machines.
337 if (Types.getDataLayout().isBigEndian()) {
338 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(
339 ContainingTypeSizeInBits - AccessStart - AccessWidth);
341 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(AccessStart);
343 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
344 AI.AccessWidth = AccessWidth;
345 AI.AccessAlignment = Types.getContext().toCharUnitsFromBits(
346 llvm::MinAlign(ContainingTypeAlign, AccessStart));
347 AI.TargetBitOffset = AccessedTargetBits;
348 AI.TargetBitWidth = AccessBitsInFieldSize;
350 AccessStart += AccessWidth;
351 AccessedTargetBits += AI.TargetBitWidth;
354 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
355 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
358 CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
360 uint64_t FieldOffset,
361 uint64_t FieldSize) {
362 const RecordDecl *RD = FD->getParent();
363 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
364 uint64_t ContainingTypeSizeInBits = Types.getContext().toBits(RL.getSize());
365 unsigned ContainingTypeAlign = Types.getContext().toBits(RL.getAlignment());
367 return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
368 ContainingTypeAlign);
371 void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
372 uint64_t fieldOffset) {
373 uint64_t fieldSize = D->getBitWidthValue(Types.getContext());
378 uint64_t nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
379 CharUnits numBytesToAppend;
380 unsigned charAlign = Types.getContext().getTargetInfo().getCharAlign();
382 if (fieldOffset < nextFieldOffsetInBits && !BitsAvailableInLastField) {
383 assert(fieldOffset % charAlign == 0 &&
384 "Field offset not aligned correctly");
386 CharUnits fieldOffsetInCharUnits =
387 Types.getContext().toCharUnitsFromBits(fieldOffset);
389 // Try to resize the last base field.
390 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInCharUnits))
391 nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
394 if (fieldOffset < nextFieldOffsetInBits) {
395 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
396 assert(!NextFieldOffset.isZero() && "Must have laid out at least one byte");
398 // The bitfield begins in the previous bit-field.
399 numBytesToAppend = Types.getContext().toCharUnitsFromBits(
400 llvm::RoundUpToAlignment(fieldSize - BitsAvailableInLastField,
403 assert(fieldOffset % charAlign == 0 &&
404 "Field offset not aligned correctly");
406 // Append padding if necessary.
407 AppendPadding(Types.getContext().toCharUnitsFromBits(fieldOffset),
410 numBytesToAppend = Types.getContext().toCharUnitsFromBits(
411 llvm::RoundUpToAlignment(fieldSize, charAlign));
413 assert(!numBytesToAppend.isZero() && "No bytes to append!");
416 // Add the bit field info.
417 BitFields.insert(std::make_pair(D,
418 CGBitFieldInfo::MakeInfo(Types, D, fieldOffset, fieldSize)));
420 AppendBytes(numBytesToAppend);
422 BitsAvailableInLastField =
423 Types.getContext().toBits(NextFieldOffset) - (fieldOffset + fieldSize);
426 bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
427 uint64_t fieldOffset) {
428 // If the field is packed, then we need a packed struct.
429 if (!Packed && D->hasAttr<PackedAttr>())
432 if (D->isBitField()) {
433 // We must use packed structs for unnamed bit fields since they
434 // don't affect the struct alignment.
435 if (!Packed && !D->getDeclName())
438 LayoutBitField(D, fieldOffset);
442 CheckZeroInitializable(D->getType());
444 assert(fieldOffset % Types.getTarget().getCharWidth() == 0
445 && "field offset is not on a byte boundary!");
446 CharUnits fieldOffsetInBytes
447 = Types.getContext().toCharUnitsFromBits(fieldOffset);
449 llvm::Type *Ty = Types.ConvertTypeForMem(D->getType());
450 CharUnits typeAlignment = getTypeAlignment(Ty);
452 // If the type alignment is larger then the struct alignment, we must use
454 if (typeAlignment > Alignment) {
455 assert(!Packed && "Alignment is wrong even with packed struct!");
460 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
461 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
462 if (const MaxFieldAlignmentAttr *MFAA =
463 RD->getAttr<MaxFieldAlignmentAttr>()) {
464 if (MFAA->getAlignment() != Types.getContext().toBits(typeAlignment))
470 // Round up the field offset to the alignment of the field type.
471 CharUnits alignedNextFieldOffsetInBytes =
472 NextFieldOffset.RoundUpToAlignment(typeAlignment);
474 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
475 // Try to resize the last base field.
476 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInBytes)) {
477 alignedNextFieldOffsetInBytes =
478 NextFieldOffset.RoundUpToAlignment(typeAlignment);
482 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
483 assert(!Packed && "Could not place field even with packed struct!");
487 AppendPadding(fieldOffsetInBytes, typeAlignment);
489 // Now append the field.
490 Fields[D] = FieldTypes.size();
491 AppendField(fieldOffsetInBytes, Ty);
493 LastLaidOutBase.invalidate();
498 CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
499 const ASTRecordLayout &Layout) {
500 if (Field->isBitField()) {
501 uint64_t FieldSize = Field->getBitWidthValue(Types.getContext());
503 // Ignore zero sized bit fields.
507 llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
508 CharUnits NumBytesToAppend = Types.getContext().toCharUnitsFromBits(
509 llvm::RoundUpToAlignment(FieldSize,
510 Types.getContext().getTargetInfo().getCharAlign()));
512 if (NumBytesToAppend > CharUnits::One())
513 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend.getQuantity());
515 // Add the bit field info.
516 BitFields.insert(std::make_pair(Field,
517 CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize)));
521 // This is a regular union field.
523 return Types.ConvertTypeForMem(Field->getType());
526 void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
527 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
529 const ASTRecordLayout &layout = Types.getContext().getASTRecordLayout(D);
531 llvm::Type *unionType = 0;
532 CharUnits unionSize = CharUnits::Zero();
533 CharUnits unionAlign = CharUnits::Zero();
535 bool hasOnlyZeroSizedBitFields = true;
536 bool checkedFirstFieldZeroInit = false;
538 unsigned fieldNo = 0;
539 for (RecordDecl::field_iterator field = D->field_begin(),
540 fieldEnd = D->field_end(); field != fieldEnd; ++field, ++fieldNo) {
541 assert(layout.getFieldOffset(fieldNo) == 0 &&
542 "Union field offset did not start at the beginning of record!");
543 llvm::Type *fieldType = LayoutUnionField(*field, layout);
548 if (field->getDeclName() && !checkedFirstFieldZeroInit) {
549 CheckZeroInitializable(field->getType());
550 checkedFirstFieldZeroInit = true;
553 hasOnlyZeroSizedBitFields = false;
555 CharUnits fieldAlign = CharUnits::fromQuantity(
556 Types.getDataLayout().getABITypeAlignment(fieldType));
557 CharUnits fieldSize = CharUnits::fromQuantity(
558 Types.getDataLayout().getTypeAllocSize(fieldType));
560 if (fieldAlign < unionAlign)
563 if (fieldAlign > unionAlign || fieldSize > unionSize) {
564 unionType = fieldType;
565 unionAlign = fieldAlign;
566 unionSize = fieldSize;
570 // Now add our field.
572 AppendField(CharUnits::Zero(), unionType);
574 if (getTypeAlignment(unionType) > layout.getAlignment()) {
575 // We need a packed struct.
577 unionAlign = CharUnits::One();
580 if (unionAlign.isZero()) {
581 (void)hasOnlyZeroSizedBitFields;
582 assert(hasOnlyZeroSizedBitFields &&
583 "0-align record did not have all zero-sized bit-fields!");
584 unionAlign = CharUnits::One();
587 // Append tail padding.
588 CharUnits recordSize = layout.getSize();
589 if (recordSize > unionSize)
590 AppendPadding(recordSize, unionAlign);
593 bool CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base,
594 const CGRecordLayout &baseLayout,
595 CharUnits baseOffset) {
596 ResizeLastBaseFieldIfNecessary(baseOffset);
598 AppendPadding(baseOffset, CharUnits::One());
600 const ASTRecordLayout &baseASTLayout
601 = Types.getContext().getASTRecordLayout(base);
603 LastLaidOutBase.Offset = NextFieldOffset;
604 LastLaidOutBase.NonVirtualSize = baseASTLayout.getNonVirtualSize();
606 llvm::StructType *subobjectType = baseLayout.getBaseSubobjectLLVMType();
607 if (getTypeAlignment(subobjectType) > Alignment)
610 AppendField(baseOffset, subobjectType);
614 bool CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base,
615 CharUnits baseOffset) {
616 // Ignore empty bases.
617 if (base->isEmpty()) return true;
619 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
620 if (IsZeroInitializableAsBase) {
621 assert(IsZeroInitializable &&
622 "class zero-initializable as base but not as complete object");
624 IsZeroInitializable = IsZeroInitializableAsBase =
625 baseLayout.isZeroInitializableAsBase();
628 if (!LayoutBase(base, baseLayout, baseOffset))
630 NonVirtualBases[base] = (FieldTypes.size() - 1);
635 CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base,
636 CharUnits baseOffset) {
637 // Ignore empty bases.
638 if (base->isEmpty()) return true;
640 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
641 if (IsZeroInitializable)
642 IsZeroInitializable = baseLayout.isZeroInitializableAsBase();
644 if (!LayoutBase(base, baseLayout, baseOffset))
646 VirtualBases[base] = (FieldTypes.size() - 1);
651 CGRecordLayoutBuilder::MSLayoutVirtualBases(const CXXRecordDecl *RD,
652 const ASTRecordLayout &Layout) {
653 if (!RD->getNumVBases())
656 // The vbases list is uniqued and ordered by a depth-first
657 // traversal, which is what we need here.
658 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
659 E = RD->vbases_end(); I != E; ++I) {
661 const CXXRecordDecl *BaseDecl =
662 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
664 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
665 if (!LayoutVirtualBase(BaseDecl, vbaseOffset))
671 /// LayoutVirtualBases - layout the non-virtual bases of a record decl.
673 CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
674 const ASTRecordLayout &Layout) {
675 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
676 E = RD->bases_end(); I != E; ++I) {
677 const CXXRecordDecl *BaseDecl =
678 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
680 // We only want to lay out virtual bases that aren't indirect primary bases
681 // of some other base.
682 if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
683 // Only lay out the base once.
684 if (!LaidOutVirtualBases.insert(BaseDecl))
687 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
688 if (!LayoutVirtualBase(BaseDecl, vbaseOffset))
692 if (!BaseDecl->getNumVBases()) {
693 // This base isn't interesting since it doesn't have any virtual bases.
697 if (!LayoutVirtualBases(BaseDecl, Layout))
704 CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
705 const ASTRecordLayout &Layout) {
706 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
708 // If we have a primary base, lay it out first.
710 if (!Layout.isPrimaryBaseVirtual()) {
711 if (!LayoutNonVirtualBase(PrimaryBase, CharUnits::Zero()))
714 if (!LayoutVirtualBase(PrimaryBase, CharUnits::Zero()))
718 // Otherwise, add a vtable / vf-table if the layout says to do so.
719 } else if (Layout.hasOwnVFPtr()) {
720 llvm::Type *FunctionType =
721 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
723 llvm::Type *VTableTy = FunctionType->getPointerTo();
725 if (getTypeAlignment(VTableTy) > Alignment) {
726 // FIXME: Should we allow this to happen in Sema?
727 assert(!Packed && "Alignment is wrong even with packed struct!");
731 assert(NextFieldOffset.isZero() &&
732 "VTable pointer must come first!");
733 AppendField(CharUnits::Zero(), VTableTy->getPointerTo());
736 // Layout the non-virtual bases.
737 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
738 E = RD->bases_end(); I != E; ++I) {
742 const CXXRecordDecl *BaseDecl =
743 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
745 // We've already laid out the primary base.
746 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
749 if (!LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl)))
753 // Add a vb-table pointer if the layout insists.
754 if (Layout.getVBPtrOffset() != CharUnits::fromQuantity(-1)) {
755 CharUnits VBPtrOffset = Layout.getVBPtrOffset();
756 llvm::Type *Vbptr = llvm::Type::getInt32PtrTy(Types.getLLVMContext());
757 AppendPadding(VBPtrOffset, getTypeAlignment(Vbptr));
758 AppendField(VBPtrOffset, Vbptr);
765 CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
766 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
768 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
769 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
770 CharUnits AlignedNonVirtualTypeSize =
771 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
773 // First check if we can use the same fields as for the complete class.
774 CharUnits RecordSize = Layout.getSize();
775 if (AlignedNonVirtualTypeSize == RecordSize)
778 // Check if we need padding.
779 CharUnits AlignedNextFieldOffset =
780 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
782 if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) {
783 assert(!Packed && "cannot layout even as packed struct");
784 return false; // Needs packing.
787 bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset);
789 CharUnits NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
790 FieldTypes.push_back(getByteArrayType(NumBytes));
793 BaseSubobjectType = llvm::StructType::create(Types.getLLVMContext(),
794 FieldTypes, "", Packed);
795 Types.addRecordTypeName(RD, BaseSubobjectType, ".base");
797 // Pull the padding back off.
799 FieldTypes.pop_back();
804 bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
805 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
806 assert(!Alignment.isZero() && "Did not set alignment!");
808 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
810 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
812 if (!LayoutNonVirtualBases(RD, Layout))
815 unsigned FieldNo = 0;
816 const FieldDecl *LastFD = 0;
818 for (RecordDecl::field_iterator Field = D->field_begin(),
819 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
821 // Zero-length bitfields following non-bitfield members are
823 const FieldDecl *FD = *Field;
824 if (Types.getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
831 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
833 "Could not layout fields even with a packed LLVM struct!");
839 // We've laid out the non-virtual bases and the fields, now compute the
840 // non-virtual base field types.
841 if (!ComputeNonVirtualBaseType(RD)) {
842 assert(!Packed && "Could not layout even with a packed LLVM struct!");
846 // Lay out the virtual bases. The MS ABI uses a different
847 // algorithm here due to the lack of primary virtual bases.
848 if (Types.getContext().getTargetInfo().getCXXABI() != CXXABI_Microsoft) {
849 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
850 if (Layout.isPrimaryBaseVirtual())
851 IndirectPrimaryBases.insert(Layout.getPrimaryBase());
853 if (!LayoutVirtualBases(RD, Layout))
856 if (!MSLayoutVirtualBases(RD, Layout))
861 // Append tail padding if necessary.
862 AppendTailPadding(Layout.getSize());
867 void CGRecordLayoutBuilder::AppendTailPadding(CharUnits RecordSize) {
868 ResizeLastBaseFieldIfNecessary(RecordSize);
870 assert(NextFieldOffset <= RecordSize && "Size mismatch!");
872 CharUnits AlignedNextFieldOffset =
873 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
875 if (AlignedNextFieldOffset == RecordSize) {
876 // We don't need any padding.
880 CharUnits NumPadBytes = RecordSize - NextFieldOffset;
881 AppendBytes(NumPadBytes);
884 void CGRecordLayoutBuilder::AppendField(CharUnits fieldOffset,
885 llvm::Type *fieldType) {
886 CharUnits fieldSize =
887 CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(fieldType));
889 FieldTypes.push_back(fieldType);
891 NextFieldOffset = fieldOffset + fieldSize;
892 BitsAvailableInLastField = 0;
895 void CGRecordLayoutBuilder::AppendPadding(CharUnits fieldOffset,
896 CharUnits fieldAlignment) {
897 assert(NextFieldOffset <= fieldOffset &&
898 "Incorrect field layout!");
900 // Do nothing if we're already at the right offset.
901 if (fieldOffset == NextFieldOffset) return;
903 // If we're not emitting a packed LLVM type, try to avoid adding
904 // unnecessary padding fields.
906 // Round up the field offset to the alignment of the field type.
907 CharUnits alignedNextFieldOffset =
908 NextFieldOffset.RoundUpToAlignment(fieldAlignment);
909 assert(alignedNextFieldOffset <= fieldOffset);
911 // If that's the right offset, we're done.
912 if (alignedNextFieldOffset == fieldOffset) return;
915 // Otherwise we need explicit padding.
916 CharUnits padding = fieldOffset - NextFieldOffset;
917 AppendBytes(padding);
920 bool CGRecordLayoutBuilder::ResizeLastBaseFieldIfNecessary(CharUnits offset) {
921 // Check if we have a base to resize.
922 if (!LastLaidOutBase.isValid())
925 // This offset does not overlap with the tail padding.
926 if (offset >= NextFieldOffset)
929 // Restore the field offset and append an i8 array instead.
930 FieldTypes.pop_back();
931 NextFieldOffset = LastLaidOutBase.Offset;
932 AppendBytes(LastLaidOutBase.NonVirtualSize);
933 LastLaidOutBase.invalidate();
938 llvm::Type *CGRecordLayoutBuilder::getByteArrayType(CharUnits numBytes) {
939 assert(!numBytes.isZero() && "Empty byte arrays aren't allowed.");
941 llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
942 if (numBytes > CharUnits::One())
943 Ty = llvm::ArrayType::get(Ty, numBytes.getQuantity());
948 void CGRecordLayoutBuilder::AppendBytes(CharUnits numBytes) {
949 if (numBytes.isZero())
952 // Append the padding field
953 AppendField(NextFieldOffset, getByteArrayType(numBytes));
956 CharUnits CGRecordLayoutBuilder::getTypeAlignment(llvm::Type *Ty) const {
958 return CharUnits::One();
960 return CharUnits::fromQuantity(Types.getDataLayout().getABITypeAlignment(Ty));
963 CharUnits CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
965 return CharUnits::One();
967 CharUnits maxAlignment = CharUnits::One();
968 for (size_t i = 0; i != FieldTypes.size(); ++i)
969 maxAlignment = std::max(maxAlignment, getTypeAlignment(FieldTypes[i]));
974 /// Merge in whether a field of the given type is zero-initializable.
975 void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
976 // This record already contains a member pointer.
977 if (!IsZeroInitializableAsBase)
980 // Can only have member pointers if we're compiling C++.
981 if (!Types.getContext().getLangOpts().CPlusPlus)
984 const Type *elementType = T->getBaseElementTypeUnsafe();
986 if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) {
987 if (!Types.getCXXABI().isZeroInitializable(MPT))
988 IsZeroInitializable = IsZeroInitializableAsBase = false;
989 } else if (const RecordType *RT = elementType->getAs<RecordType>()) {
990 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
991 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
992 if (!Layout.isZeroInitializable())
993 IsZeroInitializable = IsZeroInitializableAsBase = false;
997 CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D,
998 llvm::StructType *Ty) {
999 CGRecordLayoutBuilder Builder(*this);
1003 Ty->setBody(Builder.FieldTypes, Builder.Packed);
1005 // If we're in C++, compute the base subobject type.
1006 llvm::StructType *BaseTy = 0;
1007 if (isa<CXXRecordDecl>(D) && !D->isUnion()) {
1008 BaseTy = Builder.BaseSubobjectType;
1009 if (!BaseTy) BaseTy = Ty;
1012 CGRecordLayout *RL =
1013 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
1014 Builder.IsZeroInitializableAsBase);
1016 RL->NonVirtualBases.swap(Builder.NonVirtualBases);
1017 RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
1019 // Add all the field numbers.
1020 RL->FieldInfo.swap(Builder.Fields);
1022 // Add bitfield info.
1023 RL->BitFields.swap(Builder.BitFields);
1025 // Dump the layout, if requested.
1026 if (getContext().getLangOpts().DumpRecordLayouts) {
1027 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
1028 llvm::errs() << "Record: ";
1030 llvm::errs() << "\nLayout: ";
1035 // Verify that the computed LLVM struct size matches the AST layout size.
1036 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
1038 uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
1039 assert(TypeSizeInBits == getDataLayout().getTypeAllocSizeInBits(Ty) &&
1040 "Type size mismatch!");
1043 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
1044 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
1045 CharUnits AlignedNonVirtualTypeSize =
1046 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
1048 uint64_t AlignedNonVirtualTypeSizeInBits =
1049 getContext().toBits(AlignedNonVirtualTypeSize);
1051 assert(AlignedNonVirtualTypeSizeInBits ==
1052 getDataLayout().getTypeAllocSizeInBits(BaseTy) &&
1053 "Type size mismatch!");
1056 // Verify that the LLVM and AST field offsets agree.
1057 llvm::StructType *ST =
1058 dyn_cast<llvm::StructType>(RL->getLLVMType());
1059 const llvm::StructLayout *SL = getDataLayout().getStructLayout(ST);
1061 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
1062 RecordDecl::field_iterator it = D->field_begin();
1063 const FieldDecl *LastFD = 0;
1064 bool IsMsStruct = D->isMsStruct(getContext());
1065 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
1066 const FieldDecl *FD = *it;
1068 // For non-bit-fields, just check that the LLVM struct offset matches the
1070 if (!FD->isBitField()) {
1071 unsigned FieldNo = RL->getLLVMFieldNo(FD);
1072 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
1073 "Invalid field offset!");
1079 // Zero-length bitfields following non-bitfield members are
1081 if (getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
1088 // Ignore unnamed bit-fields.
1089 if (!FD->getDeclName()) {
1094 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
1095 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
1096 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
1098 // Verify that every component access is within the structure.
1099 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
1100 uint64_t AccessBitOffset = FieldOffset +
1101 getContext().toBits(AI.FieldByteOffset);
1102 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
1103 "Invalid bit-field access (out of range)!");
1111 void CGRecordLayout::print(raw_ostream &OS) const {
1112 OS << "<CGRecordLayout\n";
1113 OS << " LLVMType:" << *CompleteObjectType << "\n";
1114 if (BaseSubobjectType)
1115 OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
1116 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
1117 OS << " BitFields:[\n";
1119 // Print bit-field infos in declaration order.
1120 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
1121 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
1122 it = BitFields.begin(), ie = BitFields.end();
1124 const RecordDecl *RD = it->first->getParent();
1126 for (RecordDecl::field_iterator
1127 it2 = RD->field_begin(); *it2 != it->first; ++it2)
1129 BFIs.push_back(std::make_pair(Index, &it->second));
1131 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
1132 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
1134 BFIs[i].second->print(OS);
1141 void CGRecordLayout::dump() const {
1142 print(llvm::errs());
1145 void CGBitFieldInfo::print(raw_ostream &OS) const {
1146 OS << "<CGBitFieldInfo";
1147 OS << " Size:" << Size;
1148 OS << " IsSigned:" << IsSigned << "\n";
1150 OS.indent(4 + strlen("<CGBitFieldInfo"));
1151 OS << " NumComponents:" << getNumComponents();
1152 OS << " Components: [";
1153 if (getNumComponents()) {
1155 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
1156 const AccessInfo &AI = getComponent(i);
1159 << " FieldIndex:" << AI.FieldIndex
1160 << " FieldByteOffset:" << AI.FieldByteOffset.getQuantity()
1161 << " FieldBitStart:" << AI.FieldBitStart
1162 << " AccessWidth:" << AI.AccessWidth << "\n";
1163 OS.indent(8 + strlen("<AccessInfo"));
1164 OS << " AccessAlignment:" << AI.AccessAlignment.getQuantity()
1165 << " TargetBitOffset:" << AI.TargetBitOffset
1166 << " TargetBitWidth:" << AI.TargetBitWidth
1174 void CGBitFieldInfo::dump() const {
1175 print(llvm::errs());