2010-03-31 08:11:27 +08:00
|
|
|
//===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===//
|
2009-07-23 11:17:50 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-03-31 08:11:27 +08:00
|
|
|
// Builder implementation for CGRecordLayout objects.
|
2009-07-23 11:17:50 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-03-31 06:26:10 +08:00
|
|
|
#include "CGRecordLayout.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "CGCXXABI.h"
|
|
|
|
#include "CodeGenTypes.h"
|
2009-07-23 11:17:50 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Attr.h"
|
2010-11-25 06:50:27 +08:00
|
|
|
#include "clang/AST/CXXInheritance.h"
|
2009-07-23 11:17:50 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/RecordLayout.h"
|
IRgen: Add a -fuse-register-sized-bitfield-access option, for testing.
- Changes bit-field access policy to try to use (aligned) register sized accesses.
The idea here is that by using larger accesses we expose more coalescing
potential to the backend when we have situations like adjacent bit-fields in the
same structure (which is common), and that the backend should be smart enough to
narrow the accesses down when no coalescing is done or when it is shown not to
be profitable.
--
$ clang -m32 -O3 -S -o - t.c
_f0: ## @f0
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
movb (%eax), %cl
andb $-128, %cl
orb $1, %cl
movb %cl, (%eax)
movb 1(%eax), %cl
andb $-128, %cl
orb $1, %cl
movb %cl, 1(%eax)
movb 2(%eax), %cl
andb $-128, %cl
orb $1, %cl
movb %cl, 2(%eax)
movb 3(%eax), %cl
andb $-128, %cl
orb $1, %cl
movb %cl, 3(%eax)
popl %ebp
ret
$ clang -m32 -O3 -S -o - t.c -Xclang -fuse-register-sized-bitfield-access
_f0: ## @f0
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
movl $-2139062144, %ecx ## imm = 0xFFFFFFFF80808080
andl (%eax), %ecx
orl $16843009, %ecx ## imm = 0x1010101
movl %ecx, (%eax)
popl %ebp
ret
--
llvm-svn: 133532
2011-06-22 02:54:46 +08:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2013-01-02 19:45:17 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
2010-04-22 03:10:49 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2010-04-13 02:14:18 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-07-23 11:17:50 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2010-12-01 07:17:27 +08:00
|
|
|
namespace {
|
2010-03-31 08:11:27 +08:00
|
|
|
|
|
|
|
class CGRecordLayoutBuilder {
|
|
|
|
public:
|
|
|
|
/// FieldTypes - Holds the LLVM types that the struct is created from.
|
2011-02-15 14:40:56 +08:00
|
|
|
///
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<llvm::Type *, 16> FieldTypes;
|
2010-03-31 08:11:27 +08:00
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
/// BaseSubobjectType - Holds the LLVM type for the non-virtual part
|
2010-11-09 13:25:47 +08:00
|
|
|
/// of the struct. For example, consider:
|
|
|
|
///
|
|
|
|
/// struct A { int i; };
|
|
|
|
/// struct B { void *v; };
|
|
|
|
/// struct C : virtual A, B { };
|
|
|
|
///
|
|
|
|
/// The LLVM type of C will be
|
|
|
|
/// %struct.C = type { i32 (...)**, %struct.A, i32, %struct.B }
|
|
|
|
///
|
|
|
|
/// And the LLVM type of the non-virtual base struct will be
|
|
|
|
/// %struct.C.base = type { i32 (...)**, %struct.A, i32 }
|
2011-02-15 14:40:56 +08:00
|
|
|
///
|
|
|
|
/// This only gets initialized if the base subobject type is
|
|
|
|
/// different from the complete-object type.
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::StructType *BaseSubobjectType;
|
2010-11-09 13:25:47 +08:00
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
/// FieldInfo - Holds a field and its corresponding LLVM field number.
|
|
|
|
llvm::DenseMap<const FieldDecl *, unsigned> Fields;
|
2010-03-31 08:11:27 +08:00
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
/// BitFieldInfo - Holds location and size information about a bit field.
|
|
|
|
llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
|
2010-03-31 08:11:27 +08:00
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
|
|
|
|
llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases;
|
2010-11-25 06:50:27 +08:00
|
|
|
|
|
|
|
/// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
|
|
|
|
/// primary base classes for some other direct or indirect base class.
|
|
|
|
CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
|
|
|
|
|
2010-11-29 03:18:44 +08:00
|
|
|
/// LaidOutVirtualBases - A set of all laid out virtual bases, used to avoid
|
|
|
|
/// avoid laying out virtual bases more than once.
|
|
|
|
llvm::SmallPtrSet<const CXXRecordDecl *, 4> LaidOutVirtualBases;
|
|
|
|
|
2010-08-23 05:01:12 +08:00
|
|
|
/// IsZeroInitializable - Whether this struct can be C++
|
|
|
|
/// zero-initialized with an LLVM zeroinitializer.
|
|
|
|
bool IsZeroInitializable;
|
2011-02-15 14:40:56 +08:00
|
|
|
bool IsZeroInitializableAsBase;
|
2010-03-31 08:11:27 +08:00
|
|
|
|
|
|
|
/// Packed - Whether the resulting LLVM struct will be packed or not.
|
|
|
|
bool Packed;
|
2011-04-27 07:52:16 +08:00
|
|
|
|
|
|
|
/// IsMsStruct - Whether ms_struct is in effect or not
|
|
|
|
bool IsMsStruct;
|
2010-03-31 08:11:27 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
CodeGenTypes &Types;
|
|
|
|
|
2011-04-18 05:56:13 +08:00
|
|
|
/// LastLaidOutBaseInfo - Contains the offset and non-virtual size of the
|
|
|
|
/// last base laid out. Used so that we can replace the last laid out base
|
|
|
|
/// type with an i8 array if needed.
|
|
|
|
struct LastLaidOutBaseInfo {
|
|
|
|
CharUnits Offset;
|
|
|
|
CharUnits NonVirtualSize;
|
|
|
|
|
|
|
|
bool isValid() const { return !NonVirtualSize.isZero(); }
|
|
|
|
void invalidate() { NonVirtualSize = CharUnits::Zero(); }
|
|
|
|
|
|
|
|
} LastLaidOutBase;
|
|
|
|
|
2010-03-31 08:11:27 +08:00
|
|
|
/// Alignment - Contains the alignment of the RecordDecl.
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits Alignment;
|
2010-03-31 08:11:27 +08:00
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
/// NextFieldOffset - Holds the next field offset.
|
|
|
|
CharUnits NextFieldOffset;
|
2010-03-31 08:11:27 +08:00
|
|
|
|
2010-04-18 04:49:27 +08:00
|
|
|
/// LayoutUnionField - Will layout a field in an union and return the type
|
|
|
|
/// that the field will have.
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *LayoutUnionField(const FieldDecl *Field,
|
|
|
|
const ASTRecordLayout &Layout);
|
2010-04-18 04:49:27 +08:00
|
|
|
|
2010-03-31 08:11:27 +08:00
|
|
|
/// LayoutUnion - Will layout a union RecordDecl.
|
|
|
|
void LayoutUnion(const RecordDecl *D);
|
|
|
|
|
2012-12-06 19:14:44 +08:00
|
|
|
/// Lay out a sequence of contiguous bitfields.
|
|
|
|
bool LayoutBitfields(const ASTRecordLayout &Layout,
|
|
|
|
unsigned &FirstFieldNo,
|
|
|
|
RecordDecl::field_iterator &FI,
|
|
|
|
RecordDecl::field_iterator FE);
|
|
|
|
|
2010-03-31 08:11:27 +08:00
|
|
|
/// LayoutField - try to layout all fields in the record decl.
|
|
|
|
/// Returns false if the operation failed because the struct is not packed.
|
|
|
|
bool LayoutFields(const RecordDecl *D);
|
|
|
|
|
2010-12-05 07:59:48 +08:00
|
|
|
/// Layout a single base, virtual or non-virtual
|
2011-12-13 07:13:20 +08:00
|
|
|
bool LayoutBase(const CXXRecordDecl *base,
|
2011-02-16 06:21:29 +08:00
|
|
|
const CGRecordLayout &baseLayout,
|
|
|
|
CharUnits baseOffset);
|
2010-12-05 07:59:48 +08:00
|
|
|
|
2010-11-25 09:59:35 +08:00
|
|
|
/// LayoutVirtualBase - layout a single virtual base.
|
2011-12-13 07:13:20 +08:00
|
|
|
bool LayoutVirtualBase(const CXXRecordDecl *base,
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits baseOffset);
|
2010-11-25 09:59:35 +08:00
|
|
|
|
2010-11-29 03:18:44 +08:00
|
|
|
/// LayoutVirtualBases - layout the virtual bases of a record decl.
|
2011-12-13 07:13:20 +08:00
|
|
|
bool LayoutVirtualBases(const CXXRecordDecl *RD,
|
2010-11-29 03:18:44 +08:00
|
|
|
const ASTRecordLayout &Layout);
|
2011-11-08 12:01:03 +08:00
|
|
|
|
|
|
|
/// MSLayoutVirtualBases - layout the virtual bases of a record decl,
|
|
|
|
/// like MSVC.
|
2011-12-13 07:13:20 +08:00
|
|
|
bool MSLayoutVirtualBases(const CXXRecordDecl *RD,
|
2011-11-08 12:01:03 +08:00
|
|
|
const ASTRecordLayout &Layout);
|
2010-11-29 03:18:44 +08:00
|
|
|
|
2010-05-18 13:12:20 +08:00
|
|
|
/// LayoutNonVirtualBase - layout a single non-virtual base.
|
2011-12-13 07:13:20 +08:00
|
|
|
bool LayoutNonVirtualBase(const CXXRecordDecl *base,
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits baseOffset);
|
2010-05-18 13:12:20 +08:00
|
|
|
|
2010-11-29 03:18:44 +08:00
|
|
|
/// LayoutNonVirtualBases - layout the virtual bases of a record decl.
|
2011-12-13 07:13:20 +08:00
|
|
|
bool LayoutNonVirtualBases(const CXXRecordDecl *RD,
|
2010-05-18 13:12:20 +08:00
|
|
|
const ASTRecordLayout &Layout);
|
2010-03-31 08:11:27 +08:00
|
|
|
|
2010-11-09 13:25:47 +08:00
|
|
|
/// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
|
2010-12-10 08:11:00 +08:00
|
|
|
bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
|
2010-11-09 13:25:47 +08:00
|
|
|
|
2010-03-31 08:11:27 +08:00
|
|
|
/// LayoutField - layout a single field. Returns false if the operation failed
|
|
|
|
/// because the current struct is not packed.
|
|
|
|
bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
|
|
|
|
|
|
|
|
/// LayoutBitField - layout a single bit field.
|
|
|
|
void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
|
|
|
|
|
|
|
|
/// AppendField - Appends a field with the given offset and type.
|
2011-07-10 01:41:47 +08:00
|
|
|
void AppendField(CharUnits fieldOffset, llvm::Type *FieldTy);
|
2010-03-31 08:11:27 +08:00
|
|
|
|
|
|
|
/// AppendPadding - Appends enough padding bytes so that the total
|
|
|
|
/// struct size is a multiple of the field alignment.
|
2011-02-16 06:21:29 +08:00
|
|
|
void AppendPadding(CharUnits fieldOffset, CharUnits fieldAlignment);
|
2010-03-31 08:11:27 +08:00
|
|
|
|
2011-04-18 05:56:13 +08:00
|
|
|
/// ResizeLastBaseFieldIfNecessary - Fields and bases can be laid out in the
|
|
|
|
/// tail padding of a previous base. If this happens, the type of the previous
|
|
|
|
/// base needs to be changed to an array of i8. Returns true if the last
|
|
|
|
/// laid out base was resized.
|
|
|
|
bool ResizeLastBaseFieldIfNecessary(CharUnits offset);
|
|
|
|
|
2010-11-09 13:25:47 +08:00
|
|
|
/// getByteArrayType - Returns a byte array type with the given number of
|
|
|
|
/// elements.
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *getByteArrayType(CharUnits NumBytes);
|
2010-11-09 13:25:47 +08:00
|
|
|
|
2010-03-31 08:11:27 +08:00
|
|
|
/// AppendBytes - Append a given number of bytes to the record.
|
2011-02-16 06:21:29 +08:00
|
|
|
void AppendBytes(CharUnits numBytes);
|
2010-03-31 08:11:27 +08:00
|
|
|
|
|
|
|
/// AppendTailPadding - Append enough tail padding so that the type will have
|
|
|
|
/// the passed size.
|
2011-04-25 00:53:44 +08:00
|
|
|
void AppendTailPadding(CharUnits RecordSize);
|
2010-03-31 08:11:27 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
CharUnits getTypeAlignment(llvm::Type *Ty) const;
|
2010-03-31 08:11:27 +08:00
|
|
|
|
2010-11-29 07:06:23 +08:00
|
|
|
/// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
|
|
|
|
/// LLVM element types.
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits getAlignmentAsLLVMStruct() const;
|
2010-11-29 07:06:23 +08:00
|
|
|
|
2010-08-23 05:01:12 +08:00
|
|
|
/// CheckZeroInitializable - Check if the given type contains a pointer
|
2010-03-31 08:11:27 +08:00
|
|
|
/// to data member.
|
2010-08-23 05:01:12 +08:00
|
|
|
void CheckZeroInitializable(QualType T);
|
2010-03-31 08:11:27 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
CGRecordLayoutBuilder(CodeGenTypes &Types)
|
2011-02-15 14:40:56 +08:00
|
|
|
: BaseSubobjectType(0),
|
|
|
|
IsZeroInitializable(true), IsZeroInitializableAsBase(true),
|
2011-04-27 07:52:16 +08:00
|
|
|
Packed(false), IsMsStruct(false),
|
2012-12-06 19:14:44 +08:00
|
|
|
Types(Types) { }
|
2010-03-31 08:11:27 +08:00
|
|
|
|
|
|
|
/// Layout - Will layout a RecordDecl.
|
|
|
|
void Layout(const RecordDecl *D);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-07-23 11:17:50 +08:00
|
|
|
void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
|
2011-02-16 06:21:29 +08:00
|
|
|
Alignment = Types.getContext().getASTRecordLayout(D).getAlignment();
|
2009-09-03 01:51:33 +08:00
|
|
|
Packed = D->hasAttr<PackedAttr>();
|
2011-04-27 07:52:16 +08:00
|
|
|
|
2012-10-13 07:29:20 +08:00
|
|
|
IsMsStruct = D->isMsStruct(Types.getContext());
|
2009-08-09 03:38:24 +08:00
|
|
|
|
2009-07-23 11:43:54 +08:00
|
|
|
if (D->isUnion()) {
|
|
|
|
LayoutUnion(D);
|
|
|
|
return;
|
|
|
|
}
|
2009-08-09 02:23:56 +08:00
|
|
|
|
2009-07-23 11:17:50 +08:00
|
|
|
if (LayoutFields(D))
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 11:17:50 +08:00
|
|
|
// We weren't able to layout the struct. Try again with a packed struct
|
2009-07-24 01:24:40 +08:00
|
|
|
Packed = true;
|
2011-04-18 05:56:13 +08:00
|
|
|
LastLaidOutBase.invalidate();
|
2011-02-16 06:21:29 +08:00
|
|
|
NextFieldOffset = CharUnits::Zero();
|
2009-07-23 11:17:50 +08:00
|
|
|
FieldTypes.clear();
|
2011-02-15 14:40:56 +08:00
|
|
|
Fields.clear();
|
|
|
|
BitFields.clear();
|
|
|
|
NonVirtualBases.clear();
|
|
|
|
VirtualBases.clear();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 11:17:50 +08:00
|
|
|
LayoutFields(D);
|
|
|
|
}
|
|
|
|
|
2010-09-03 07:53:28 +08:00
|
|
|
CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
|
2012-12-06 19:14:44 +08:00
|
|
|
const FieldDecl *FD,
|
|
|
|
uint64_t Offset, uint64_t Size,
|
|
|
|
uint64_t StorageSize,
|
|
|
|
uint64_t StorageAlignment) {
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType());
|
2011-02-26 16:41:59 +08:00
|
|
|
CharUnits TypeSizeInBytes =
|
2012-10-09 00:25:52 +08:00
|
|
|
CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(Ty));
|
2011-02-26 16:41:59 +08:00
|
|
|
uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
|
2010-04-13 05:01:28 +08:00
|
|
|
|
2011-05-21 00:38:50 +08:00
|
|
|
bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
|
2010-04-13 05:01:28 +08:00
|
|
|
|
2012-12-06 19:14:44 +08:00
|
|
|
if (Size > TypeSizeInBits) {
|
2010-04-18 06:54:57 +08:00
|
|
|
// We have a wide bit-field. The extra bits are only used for padding, so
|
|
|
|
// if we have a bitfield of type T, with size N:
|
|
|
|
//
|
|
|
|
// T t : N;
|
|
|
|
//
|
|
|
|
// We can just assume that it's:
|
|
|
|
//
|
|
|
|
// T t : sizeof(T);
|
|
|
|
//
|
2012-12-06 19:14:44 +08:00
|
|
|
Size = TypeSizeInBits;
|
2010-04-14 04:58:55 +08:00
|
|
|
}
|
|
|
|
|
2012-12-09 15:26:04 +08:00
|
|
|
// Reverse the bit offsets for big endian machines. Because we represent
|
|
|
|
// a bitfield as a single large integer load, we can imagine the bits
|
|
|
|
// counting from the most-significant-bit instead of the
|
|
|
|
// least-significant-bit.
|
|
|
|
if (Types.getDataLayout().isBigEndian()) {
|
|
|
|
Offset = StorageSize - (Offset + Size);
|
|
|
|
}
|
2010-04-13 05:01:28 +08:00
|
|
|
|
2012-12-06 19:14:44 +08:00
|
|
|
return CGBitFieldInfo(Offset, Size, IsSigned, StorageSize, StorageAlignment);
|
2010-09-03 07:53:28 +08:00
|
|
|
}
|
|
|
|
|
2012-12-06 19:14:44 +08:00
|
|
|
/// \brief Layout the range of bitfields from BFI to BFE as contiguous storage.
|
|
|
|
bool CGRecordLayoutBuilder::LayoutBitfields(const ASTRecordLayout &Layout,
|
|
|
|
unsigned &FirstFieldNo,
|
|
|
|
RecordDecl::field_iterator &FI,
|
|
|
|
RecordDecl::field_iterator FE) {
|
|
|
|
assert(FI != FE);
|
|
|
|
uint64_t FirstFieldOffset = Layout.getFieldOffset(FirstFieldNo);
|
|
|
|
uint64_t NextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
|
|
|
|
|
|
|
|
unsigned CharAlign = Types.getContext().getTargetInfo().getCharAlign();
|
|
|
|
assert(FirstFieldOffset % CharAlign == 0 &&
|
|
|
|
"First field offset is misaligned");
|
|
|
|
CharUnits FirstFieldOffsetInBytes
|
|
|
|
= Types.getContext().toCharUnitsFromBits(FirstFieldOffset);
|
|
|
|
|
|
|
|
unsigned StorageAlignment
|
|
|
|
= llvm::MinAlign(Alignment.getQuantity(),
|
|
|
|
FirstFieldOffsetInBytes.getQuantity());
|
|
|
|
|
|
|
|
if (FirstFieldOffset < NextFieldOffsetInBits) {
|
|
|
|
CharUnits FieldOffsetInCharUnits =
|
|
|
|
Types.getContext().toCharUnitsFromBits(FirstFieldOffset);
|
2011-04-18 05:56:13 +08:00
|
|
|
|
|
|
|
// Try to resize the last base field.
|
2012-12-06 19:14:44 +08:00
|
|
|
if (!ResizeLastBaseFieldIfNecessary(FieldOffsetInCharUnits))
|
|
|
|
llvm_unreachable("We must be able to resize the last base if we need to "
|
|
|
|
"pack bits into it.");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-12-06 19:14:44 +08:00
|
|
|
NextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
|
|
|
|
assert(FirstFieldOffset >= NextFieldOffsetInBits);
|
2009-07-23 11:17:50 +08:00
|
|
|
}
|
|
|
|
|
2012-12-06 19:14:44 +08:00
|
|
|
// Append padding if necessary.
|
|
|
|
AppendPadding(Types.getContext().toCharUnitsFromBits(FirstFieldOffset),
|
|
|
|
CharUnits::One());
|
|
|
|
|
|
|
|
// Find the last bitfield in a contiguous run of bitfields.
|
|
|
|
RecordDecl::field_iterator BFI = FI;
|
|
|
|
unsigned LastFieldNo = FirstFieldNo;
|
|
|
|
uint64_t NextContiguousFieldOffset = FirstFieldOffset;
|
|
|
|
for (RecordDecl::field_iterator FJ = FI;
|
|
|
|
(FJ != FE && (*FJ)->isBitField() &&
|
|
|
|
NextContiguousFieldOffset == Layout.getFieldOffset(LastFieldNo) &&
|
|
|
|
(*FJ)->getBitWidthValue(Types.getContext()) != 0); FI = FJ++) {
|
|
|
|
NextContiguousFieldOffset += (*FJ)->getBitWidthValue(Types.getContext());
|
|
|
|
++LastFieldNo;
|
|
|
|
|
|
|
|
// We must use packed structs for packed fields, and also unnamed bit
|
|
|
|
// fields since they don't affect the struct alignment.
|
|
|
|
if (!Packed && ((*FJ)->hasAttr<PackedAttr>() || !(*FJ)->getDeclName()))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
RecordDecl::field_iterator BFE = llvm::next(FI);
|
|
|
|
--LastFieldNo;
|
|
|
|
assert(LastFieldNo >= FirstFieldNo && "Empty run of contiguous bitfields");
|
|
|
|
FieldDecl *LastFD = *FI;
|
|
|
|
|
|
|
|
// Find the last bitfield's offset, add its size, and round it up to the
|
|
|
|
// character alignment to compute the storage required.
|
|
|
|
uint64_t LastFieldOffset = Layout.getFieldOffset(LastFieldNo);
|
|
|
|
uint64_t LastFieldSize = LastFD->getBitWidthValue(Types.getContext());
|
|
|
|
uint64_t TotalBits = (LastFieldOffset + LastFieldSize) - FirstFieldOffset;
|
|
|
|
CharUnits StorageBytes = Types.getContext().toCharUnitsFromBits(
|
|
|
|
llvm::RoundUpToAlignment(TotalBits, CharAlign));
|
|
|
|
uint64_t StorageBits = Types.getContext().toBits(StorageBytes);
|
|
|
|
|
|
|
|
// Grow the storage to encompass any known padding in the layout when doing
|
|
|
|
// so will make the storage a power-of-two. There are two cases when we can
|
|
|
|
// do this. The first is when we have a subsequent field and can widen up to
|
|
|
|
// its offset. The second is when the data size of the AST record layout is
|
|
|
|
// past the end of the current storage. The latter is true when there is tail
|
|
|
|
// padding on a struct and no members of a super class can be packed into it.
|
|
|
|
//
|
|
|
|
// Note that we widen the storage as much as possible here to express the
|
|
|
|
// maximum latitude the language provides, and rely on the backend to lower
|
|
|
|
// these in conjunction with shifts and masks to narrower operations where
|
|
|
|
// beneficial.
|
|
|
|
uint64_t EndOffset = Types.getContext().toBits(Layout.getDataSize());
|
|
|
|
if (BFE != FE)
|
|
|
|
// If there are more fields to be laid out, the offset at the end of the
|
|
|
|
// bitfield is the offset of the next field in the record.
|
|
|
|
EndOffset = Layout.getFieldOffset(LastFieldNo + 1);
|
|
|
|
assert(EndOffset >= (FirstFieldOffset + TotalBits) &&
|
|
|
|
"End offset is not past the end of the known storage bits.");
|
|
|
|
uint64_t SpaceBits = EndOffset - FirstFieldOffset;
|
|
|
|
uint64_t LongBits = Types.getContext().getTargetInfo().getLongWidth();
|
|
|
|
uint64_t WidenedBits = (StorageBits / LongBits) * LongBits +
|
|
|
|
llvm::NextPowerOf2(StorageBits % LongBits - 1);
|
|
|
|
assert(WidenedBits >= StorageBits && "Widening shrunk the bits!");
|
|
|
|
if (WidenedBits <= SpaceBits) {
|
|
|
|
StorageBits = WidenedBits;
|
|
|
|
StorageBytes = Types.getContext().toCharUnitsFromBits(StorageBits);
|
|
|
|
assert(StorageBits == (uint64_t)Types.getContext().toBits(StorageBytes));
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-12-06 19:14:44 +08:00
|
|
|
unsigned FieldIndex = FieldTypes.size();
|
|
|
|
AppendBytes(StorageBytes);
|
|
|
|
|
|
|
|
// Now walk the bitfields associating them with this field of storage and
|
|
|
|
// building up the bitfield specific info.
|
|
|
|
unsigned FieldNo = FirstFieldNo;
|
|
|
|
for (; BFI != BFE; ++BFI, ++FieldNo) {
|
|
|
|
FieldDecl *FD = *BFI;
|
|
|
|
uint64_t FieldOffset = Layout.getFieldOffset(FieldNo) - FirstFieldOffset;
|
|
|
|
uint64_t FieldSize = FD->getBitWidthValue(Types.getContext());
|
|
|
|
Fields[FD] = FieldIndex;
|
|
|
|
BitFields[FD] = CGBitFieldInfo::MakeInfo(Types, FD, FieldOffset, FieldSize,
|
|
|
|
StorageBits, StorageAlignment);
|
|
|
|
}
|
|
|
|
FirstFieldNo = LastFieldNo;
|
|
|
|
return true;
|
2009-07-23 11:17:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
|
2011-02-16 06:21:29 +08:00
|
|
|
uint64_t fieldOffset) {
|
2009-07-23 11:17:50 +08:00
|
|
|
// If the field is packed, then we need a packed struct.
|
2009-08-09 02:23:56 +08:00
|
|
|
if (!Packed && D->hasAttr<PackedAttr>())
|
2009-07-23 11:17:50 +08:00
|
|
|
return false;
|
|
|
|
|
2012-12-06 19:14:44 +08:00
|
|
|
assert(!D->isBitField() && "Bitfields should be laid out seperately.");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-23 05:01:12 +08:00
|
|
|
CheckZeroInitializable(D->getType());
|
2010-03-31 08:11:27 +08:00
|
|
|
|
2011-02-26 16:41:59 +08:00
|
|
|
assert(fieldOffset % Types.getTarget().getCharWidth() == 0
|
|
|
|
&& "field offset is not on a byte boundary!");
|
|
|
|
CharUnits fieldOffsetInBytes
|
|
|
|
= Types.getContext().toCharUnitsFromBits(fieldOffset);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *Ty = Types.ConvertTypeForMem(D->getType());
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits typeAlignment = getTypeAlignment(Ty);
|
2009-07-23 11:17:50 +08:00
|
|
|
|
2009-08-09 03:38:24 +08:00
|
|
|
// If the type alignment is larger then the struct alignment, we must use
|
|
|
|
// a packed struct.
|
2011-02-16 06:21:29 +08:00
|
|
|
if (typeAlignment > Alignment) {
|
2009-08-09 03:38:24 +08:00
|
|
|
assert(!Packed && "Alignment is wrong even with packed struct!");
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
if (!Packed) {
|
|
|
|
if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
|
|
|
|
const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
|
|
|
|
if (const MaxFieldAlignmentAttr *MFAA =
|
|
|
|
RD->getAttr<MaxFieldAlignmentAttr>()) {
|
2011-02-26 16:41:59 +08:00
|
|
|
if (MFAA->getAlignment() != Types.getContext().toBits(typeAlignment))
|
2011-02-16 06:21:29 +08:00
|
|
|
return false;
|
|
|
|
}
|
2009-08-09 03:38:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-05 00:29:15 +08:00
|
|
|
// Round up the field offset to the alignment of the field type.
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits alignedNextFieldOffsetInBytes =
|
|
|
|
NextFieldOffset.RoundUpToAlignment(typeAlignment);
|
2009-08-05 00:29:15 +08:00
|
|
|
|
2011-04-18 05:56:13 +08:00
|
|
|
if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
|
|
|
|
// Try to resize the last base field.
|
|
|
|
if (ResizeLastBaseFieldIfNecessary(fieldOffsetInBytes)) {
|
|
|
|
alignedNextFieldOffsetInBytes =
|
|
|
|
NextFieldOffset.RoundUpToAlignment(typeAlignment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
|
2009-08-05 00:29:15 +08:00
|
|
|
assert(!Packed && "Could not place field even with packed struct!");
|
|
|
|
return false;
|
2009-07-23 11:17:50 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
AppendPadding(fieldOffsetInBytes, typeAlignment);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 11:17:50 +08:00
|
|
|
// Now append the field.
|
2011-02-15 14:40:56 +08:00
|
|
|
Fields[D] = FieldTypes.size();
|
2011-02-16 06:21:29 +08:00
|
|
|
AppendField(fieldOffsetInBytes, Ty);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-04-18 05:56:13 +08:00
|
|
|
LastLaidOutBase.invalidate();
|
2009-07-23 11:17:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *
|
2010-04-18 04:49:27 +08:00
|
|
|
CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
|
|
|
|
const ASTRecordLayout &Layout) {
|
2012-12-06 19:14:44 +08:00
|
|
|
Fields[Field] = 0;
|
2010-04-18 04:49:27 +08:00
|
|
|
if (Field->isBitField()) {
|
2011-10-11 02:28:20 +08:00
|
|
|
uint64_t FieldSize = Field->getBitWidthValue(Types.getContext());
|
2010-04-18 04:49:27 +08:00
|
|
|
|
|
|
|
// Ignore zero sized bit fields.
|
|
|
|
if (FieldSize == 0)
|
|
|
|
return 0;
|
|
|
|
|
2012-12-06 19:14:44 +08:00
|
|
|
unsigned StorageBits = llvm::RoundUpToAlignment(
|
|
|
|
FieldSize, Types.getContext().getTargetInfo().getCharAlign());
|
|
|
|
CharUnits NumBytesToAppend
|
|
|
|
= Types.getContext().toCharUnitsFromBits(StorageBits);
|
2010-04-18 05:04:52 +08:00
|
|
|
|
2012-12-06 19:14:44 +08:00
|
|
|
llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
|
2011-04-25 00:47:33 +08:00
|
|
|
if (NumBytesToAppend > CharUnits::One())
|
|
|
|
FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend.getQuantity());
|
2010-04-18 05:04:52 +08:00
|
|
|
|
2010-04-18 04:49:27 +08:00
|
|
|
// Add the bit field info.
|
2012-12-06 19:14:44 +08:00
|
|
|
BitFields[Field] = CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize,
|
|
|
|
StorageBits,
|
|
|
|
Alignment.getQuantity());
|
2010-04-18 05:04:52 +08:00
|
|
|
return FieldTy;
|
2010-04-18 04:49:27 +08:00
|
|
|
}
|
2010-04-21 01:52:30 +08:00
|
|
|
|
2010-04-18 04:49:27 +08:00
|
|
|
// This is a regular union field.
|
2011-07-10 01:41:47 +08:00
|
|
|
return Types.ConvertTypeForMem(Field->getType());
|
2010-04-18 04:49:27 +08:00
|
|
|
}
|
|
|
|
|
2009-07-23 11:43:54 +08:00
|
|
|
void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
|
|
|
|
assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
const ASTRecordLayout &layout = Types.getContext().getASTRecordLayout(D);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *unionType = 0;
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits unionSize = CharUnits::Zero();
|
|
|
|
CharUnits unionAlign = CharUnits::Zero();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
bool hasOnlyZeroSizedBitFields = true;
|
2011-12-07 09:30:11 +08:00
|
|
|
bool checkedFirstFieldZeroInit = false;
|
2010-03-31 08:11:27 +08:00
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
unsigned fieldNo = 0;
|
|
|
|
for (RecordDecl::field_iterator field = D->field_begin(),
|
|
|
|
fieldEnd = D->field_end(); field != fieldEnd; ++field, ++fieldNo) {
|
|
|
|
assert(layout.getFieldOffset(fieldNo) == 0 &&
|
2009-07-23 11:43:54 +08:00
|
|
|
"Union field offset did not start at the beginning of record!");
|
2012-06-07 04:45:41 +08:00
|
|
|
llvm::Type *fieldType = LayoutUnionField(*field, layout);
|
2010-04-18 04:49:27 +08:00
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
if (!fieldType)
|
2010-04-18 04:49:27 +08:00
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-12-07 09:30:11 +08:00
|
|
|
if (field->getDeclName() && !checkedFirstFieldZeroInit) {
|
|
|
|
CheckZeroInitializable(field->getType());
|
|
|
|
checkedFirstFieldZeroInit = true;
|
|
|
|
}
|
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
hasOnlyZeroSizedBitFields = false;
|
2010-03-31 08:11:27 +08:00
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits fieldAlign = CharUnits::fromQuantity(
|
2012-10-09 00:25:52 +08:00
|
|
|
Types.getDataLayout().getABITypeAlignment(fieldType));
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits fieldSize = CharUnits::fromQuantity(
|
2012-10-09 00:25:52 +08:00
|
|
|
Types.getDataLayout().getTypeAllocSize(fieldType));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
if (fieldAlign < unionAlign)
|
2009-07-23 11:43:54 +08:00
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
if (fieldAlign > unionAlign || fieldSize > unionSize) {
|
|
|
|
unionType = fieldType;
|
|
|
|
unionAlign = fieldAlign;
|
|
|
|
unionSize = fieldSize;
|
2009-07-23 11:43:54 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 11:43:54 +08:00
|
|
|
// Now add our field.
|
2011-02-16 06:21:29 +08:00
|
|
|
if (unionType) {
|
|
|
|
AppendField(CharUnits::Zero(), unionType);
|
2009-09-04 06:56:02 +08:00
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
if (getTypeAlignment(unionType) > layout.getAlignment()) {
|
2009-09-04 06:56:02 +08:00
|
|
|
// We need a packed struct.
|
|
|
|
Packed = true;
|
2011-02-16 06:21:29 +08:00
|
|
|
unionAlign = CharUnits::One();
|
2009-09-04 06:56:02 +08:00
|
|
|
}
|
|
|
|
}
|
2011-02-16 06:21:29 +08:00
|
|
|
if (unionAlign.isZero()) {
|
2012-03-04 20:16:40 +08:00
|
|
|
(void)hasOnlyZeroSizedBitFields;
|
2011-02-16 06:21:29 +08:00
|
|
|
assert(hasOnlyZeroSizedBitFields &&
|
2010-01-29 02:22:03 +08:00
|
|
|
"0-align record did not have all zero-sized bit-fields!");
|
2011-02-16 06:21:29 +08:00
|
|
|
unionAlign = CharUnits::One();
|
2009-11-07 04:47:40 +08:00
|
|
|
}
|
2010-03-31 08:11:27 +08:00
|
|
|
|
2009-07-23 11:43:54 +08:00
|
|
|
// Append tail padding.
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits recordSize = layout.getSize();
|
|
|
|
if (recordSize > unionSize)
|
|
|
|
AppendPadding(recordSize, unionAlign);
|
2009-07-23 11:43:54 +08:00
|
|
|
}
|
|
|
|
|
2011-12-13 07:13:20 +08:00
|
|
|
bool CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base,
|
2011-02-16 06:21:29 +08:00
|
|
|
const CGRecordLayout &baseLayout,
|
|
|
|
CharUnits baseOffset) {
|
2011-04-18 05:56:13 +08:00
|
|
|
ResizeLastBaseFieldIfNecessary(baseOffset);
|
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
AppendPadding(baseOffset, CharUnits::One());
|
2010-12-05 07:59:48 +08:00
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
const ASTRecordLayout &baseASTLayout
|
|
|
|
= Types.getContext().getASTRecordLayout(base);
|
|
|
|
|
2011-04-18 05:56:13 +08:00
|
|
|
LastLaidOutBase.Offset = NextFieldOffset;
|
|
|
|
LastLaidOutBase.NonVirtualSize = baseASTLayout.getNonVirtualSize();
|
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::StructType *subobjectType = baseLayout.getBaseSubobjectLLVMType();
|
2011-12-13 07:13:20 +08:00
|
|
|
if (getTypeAlignment(subobjectType) > Alignment)
|
|
|
|
return false;
|
|
|
|
|
2011-04-18 05:56:13 +08:00
|
|
|
AppendField(baseOffset, subobjectType);
|
2011-12-13 07:13:20 +08:00
|
|
|
return true;
|
2010-12-05 07:59:48 +08:00
|
|
|
}
|
|
|
|
|
2011-12-13 07:13:20 +08:00
|
|
|
bool CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base,
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits baseOffset) {
|
2010-11-25 09:59:35 +08:00
|
|
|
// Ignore empty bases.
|
2011-12-13 07:13:20 +08:00
|
|
|
if (base->isEmpty()) return true;
|
2010-11-25 09:59:35 +08:00
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
|
|
|
|
if (IsZeroInitializableAsBase) {
|
|
|
|
assert(IsZeroInitializable &&
|
|
|
|
"class zero-initializable as base but not as complete object");
|
2010-11-25 09:59:35 +08:00
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
IsZeroInitializable = IsZeroInitializableAsBase =
|
|
|
|
baseLayout.isZeroInitializableAsBase();
|
|
|
|
}
|
2010-11-25 09:59:35 +08:00
|
|
|
|
2011-12-13 07:13:20 +08:00
|
|
|
if (!LayoutBase(base, baseLayout, baseOffset))
|
|
|
|
return false;
|
2011-02-15 14:40:56 +08:00
|
|
|
NonVirtualBases[base] = (FieldTypes.size() - 1);
|
2011-12-13 07:13:20 +08:00
|
|
|
return true;
|
2011-02-15 14:40:56 +08:00
|
|
|
}
|
2010-11-25 09:59:35 +08:00
|
|
|
|
2011-12-13 07:13:20 +08:00
|
|
|
bool
|
2011-02-15 14:40:56 +08:00
|
|
|
CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base,
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits baseOffset) {
|
2011-02-15 14:40:56 +08:00
|
|
|
// Ignore empty bases.
|
2011-12-13 07:13:20 +08:00
|
|
|
if (base->isEmpty()) return true;
|
2010-11-25 09:59:35 +08:00
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
|
|
|
|
if (IsZeroInitializable)
|
|
|
|
IsZeroInitializable = baseLayout.isZeroInitializableAsBase();
|
|
|
|
|
2011-12-13 07:13:20 +08:00
|
|
|
if (!LayoutBase(base, baseLayout, baseOffset))
|
|
|
|
return false;
|
2011-02-15 14:40:56 +08:00
|
|
|
VirtualBases[base] = (FieldTypes.size() - 1);
|
2011-12-13 07:13:20 +08:00
|
|
|
return true;
|
2010-11-25 09:59:35 +08:00
|
|
|
}
|
|
|
|
|
2011-12-13 07:13:20 +08:00
|
|
|
bool
|
2011-11-08 12:01:03 +08:00
|
|
|
CGRecordLayoutBuilder::MSLayoutVirtualBases(const CXXRecordDecl *RD,
|
|
|
|
const ASTRecordLayout &Layout) {
|
|
|
|
if (!RD->getNumVBases())
|
2011-12-13 07:13:20 +08:00
|
|
|
return true;
|
2011-11-08 12:01:03 +08:00
|
|
|
|
|
|
|
// The vbases list is uniqued and ordered by a depth-first
|
|
|
|
// traversal, which is what we need here.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
|
|
|
|
E = RD->vbases_end(); I != E; ++I) {
|
|
|
|
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
|
2011-12-13 07:13:20 +08:00
|
|
|
if (!LayoutVirtualBase(BaseDecl, vbaseOffset))
|
|
|
|
return false;
|
2011-11-08 12:01:03 +08:00
|
|
|
}
|
2011-12-13 07:13:20 +08:00
|
|
|
return true;
|
2011-11-08 12:01:03 +08:00
|
|
|
}
|
|
|
|
|
2010-11-29 03:18:44 +08:00
|
|
|
/// LayoutVirtualBases - layout the non-virtual bases of a record decl.
|
2011-12-13 07:13:20 +08:00
|
|
|
bool
|
2010-11-29 03:18:44 +08:00
|
|
|
CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
|
|
|
|
const ASTRecordLayout &Layout) {
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
// We only want to lay out virtual bases that aren't indirect primary bases
|
|
|
|
// of some other base.
|
|
|
|
if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
|
|
|
|
// Only lay out the base once.
|
|
|
|
if (!LaidOutVirtualBases.insert(BaseDecl))
|
|
|
|
continue;
|
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
|
2011-12-13 07:13:20 +08:00
|
|
|
if (!LayoutVirtualBase(BaseDecl, vbaseOffset))
|
|
|
|
return false;
|
2010-11-29 03:18:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!BaseDecl->getNumVBases()) {
|
|
|
|
// This base isn't interesting since it doesn't have any virtual bases.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-12-13 07:13:20 +08:00
|
|
|
if (!LayoutVirtualBases(BaseDecl, Layout))
|
|
|
|
return false;
|
2010-11-29 03:18:44 +08:00
|
|
|
}
|
2011-12-13 07:13:20 +08:00
|
|
|
return true;
|
2010-11-29 03:18:44 +08:00
|
|
|
}
|
|
|
|
|
2011-12-13 07:13:20 +08:00
|
|
|
bool
|
2010-05-18 13:12:20 +08:00
|
|
|
CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
|
|
|
|
const ASTRecordLayout &Layout) {
|
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
|
|
|
|
2011-11-08 12:01:03 +08:00
|
|
|
// If we have a primary base, lay it out first.
|
|
|
|
if (PrimaryBase) {
|
2011-12-13 07:13:20 +08:00
|
|
|
if (!Layout.isPrimaryBaseVirtual()) {
|
|
|
|
if (!LayoutNonVirtualBase(PrimaryBase, CharUnits::Zero()))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (!LayoutVirtualBase(PrimaryBase, CharUnits::Zero()))
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-08 12:01:03 +08:00
|
|
|
|
|
|
|
// Otherwise, add a vtable / vf-table if the layout says to do so.
|
2012-05-01 16:55:32 +08:00
|
|
|
} else if (Layout.hasOwnVFPtr()) {
|
2011-11-08 12:01:03 +08:00
|
|
|
llvm::Type *FunctionType =
|
|
|
|
llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
|
|
|
|
/*isVarArg=*/true);
|
|
|
|
llvm::Type *VTableTy = FunctionType->getPointerTo();
|
2012-04-27 10:34:46 +08:00
|
|
|
|
|
|
|
if (getTypeAlignment(VTableTy) > Alignment) {
|
|
|
|
// FIXME: Should we allow this to happen in Sema?
|
|
|
|
assert(!Packed && "Alignment is wrong even with packed struct!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-08 12:01:03 +08:00
|
|
|
assert(NextFieldOffset.isZero() &&
|
|
|
|
"VTable pointer must come first!");
|
|
|
|
AppendField(CharUnits::Zero(), VTableTy->getPointerTo());
|
2010-05-18 13:12:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Layout the non-virtual bases.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
|
|
|
if (I->isVirtual())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
// We've already laid out the primary base.
|
2010-11-25 07:12:57 +08:00
|
|
|
if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
|
2010-05-18 13:12:20 +08:00
|
|
|
continue;
|
|
|
|
|
2011-12-13 07:13:20 +08:00
|
|
|
if (!LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl)))
|
|
|
|
return false;
|
2009-12-17 01:27:20 +08:00
|
|
|
}
|
2011-11-08 12:01:03 +08:00
|
|
|
|
|
|
|
// Add a vb-table pointer if the layout insists.
|
|
|
|
if (Layout.getVBPtrOffset() != CharUnits::fromQuantity(-1)) {
|
|
|
|
CharUnits VBPtrOffset = Layout.getVBPtrOffset();
|
|
|
|
llvm::Type *Vbptr = llvm::Type::getInt32PtrTy(Types.getLLVMContext());
|
|
|
|
AppendPadding(VBPtrOffset, getTypeAlignment(Vbptr));
|
|
|
|
AppendField(VBPtrOffset, Vbptr);
|
|
|
|
}
|
2011-12-13 07:13:20 +08:00
|
|
|
|
|
|
|
return true;
|
2009-12-17 01:27:20 +08:00
|
|
|
}
|
|
|
|
|
2010-12-10 08:11:00 +08:00
|
|
|
bool
|
2010-11-09 13:25:47 +08:00
|
|
|
CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
|
|
|
|
const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
|
|
|
|
|
2011-02-08 10:02:47 +08:00
|
|
|
CharUnits NonVirtualSize = Layout.getNonVirtualSize();
|
|
|
|
CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits AlignedNonVirtualTypeSize =
|
|
|
|
NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
|
2010-11-09 13:25:47 +08:00
|
|
|
|
|
|
|
// First check if we can use the same fields as for the complete class.
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits RecordSize = Layout.getSize();
|
2011-02-15 14:40:56 +08:00
|
|
|
if (AlignedNonVirtualTypeSize == RecordSize)
|
2010-12-10 08:11:00 +08:00
|
|
|
return true;
|
2010-11-09 13:25:47 +08:00
|
|
|
|
|
|
|
// Check if we need padding.
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits AlignedNextFieldOffset =
|
|
|
|
NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
|
2010-11-09 13:25:47 +08:00
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) {
|
|
|
|
assert(!Packed && "cannot layout even as packed struct");
|
2010-12-10 08:11:00 +08:00
|
|
|
return false; // Needs packing.
|
2011-02-15 14:40:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset);
|
|
|
|
if (needsPadding) {
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
|
2011-02-15 14:40:56 +08:00
|
|
|
FieldTypes.push_back(getByteArrayType(NumBytes));
|
|
|
|
}
|
2011-07-10 01:41:47 +08:00
|
|
|
|
2011-08-13 01:43:31 +08:00
|
|
|
BaseSubobjectType = llvm::StructType::create(Types.getLLVMContext(),
|
|
|
|
FieldTypes, "", Packed);
|
2011-07-10 01:41:47 +08:00
|
|
|
Types.addRecordTypeName(RD, BaseSubobjectType, ".base");
|
2010-11-09 13:25:47 +08:00
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
// Pull the padding back off.
|
|
|
|
if (needsPadding)
|
2011-02-15 14:40:56 +08:00
|
|
|
FieldTypes.pop_back();
|
2010-11-09 13:25:47 +08:00
|
|
|
|
2010-12-10 08:11:00 +08:00
|
|
|
return true;
|
2010-11-09 13:25:47 +08:00
|
|
|
}
|
|
|
|
|
2009-07-23 11:17:50 +08:00
|
|
|
bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
|
|
|
|
assert(!D->isUnion() && "Can't call LayoutFields on a union!");
|
2011-02-16 06:21:29 +08:00
|
|
|
assert(!Alignment.isZero() && "Did not set alignment!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 11:43:54 +08:00
|
|
|
const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-11-09 13:25:47 +08:00
|
|
|
const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
|
|
|
|
if (RD)
|
2011-12-13 07:13:20 +08:00
|
|
|
if (!LayoutNonVirtualBases(RD, Layout))
|
|
|
|
return false;
|
2010-03-31 08:11:27 +08:00
|
|
|
|
2009-07-23 11:17:50 +08:00
|
|
|
unsigned FieldNo = 0;
|
2011-04-27 07:52:16 +08:00
|
|
|
const FieldDecl *LastFD = 0;
|
|
|
|
|
2012-12-06 19:14:44 +08:00
|
|
|
for (RecordDecl::field_iterator FI = D->field_begin(), FE = D->field_end();
|
|
|
|
FI != FE; ++FI, ++FieldNo) {
|
|
|
|
FieldDecl *FD = *FI;
|
2011-04-27 07:52:16 +08:00
|
|
|
if (IsMsStruct) {
|
|
|
|
// Zero-length bitfields following non-bitfield members are
|
|
|
|
// ignored:
|
2011-05-04 04:21:04 +08:00
|
|
|
if (Types.getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
|
2011-04-27 07:52:16 +08:00
|
|
|
--FieldNo;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
LastFD = FD;
|
|
|
|
}
|
2012-12-06 19:14:44 +08:00
|
|
|
|
|
|
|
// If this field is a bitfield, layout all of the consecutive
|
|
|
|
// non-zero-length bitfields and the last zero-length bitfield; these will
|
|
|
|
// all share storage.
|
|
|
|
if (FD->isBitField()) {
|
|
|
|
// If all we have is a zero-width bitfield, skip it.
|
|
|
|
if (FD->getBitWidthValue(Types.getContext()) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Layout this range of bitfields.
|
|
|
|
if (!LayoutBitfields(Layout, FieldNo, FI, FE)) {
|
|
|
|
assert(!Packed &&
|
|
|
|
"Could not layout bitfields even with a packed LLVM struct!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
assert(FI != FE && "Advanced past the last bitfield");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!LayoutField(FD, Layout.getFieldOffset(FieldNo))) {
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(!Packed &&
|
2009-07-23 11:17:50 +08:00
|
|
|
"Could not layout fields even with a packed LLVM struct!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-25 09:59:35 +08:00
|
|
|
if (RD) {
|
2010-11-29 03:18:44 +08:00
|
|
|
// We've laid out the non-virtual bases and the fields, now compute the
|
|
|
|
// non-virtual base field types.
|
2010-12-10 08:11:00 +08:00
|
|
|
if (!ComputeNonVirtualBaseType(RD)) {
|
|
|
|
assert(!Packed && "Could not layout even with a packed LLVM struct!");
|
|
|
|
return false;
|
|
|
|
}
|
2010-11-29 03:18:44 +08:00
|
|
|
|
2011-11-08 12:01:03 +08:00
|
|
|
// Lay out the virtual bases. The MS ABI uses a different
|
|
|
|
// algorithm here due to the lack of primary virtual bases.
|
|
|
|
if (Types.getContext().getTargetInfo().getCXXABI() != CXXABI_Microsoft) {
|
|
|
|
RD->getIndirectPrimaryBases(IndirectPrimaryBases);
|
|
|
|
if (Layout.isPrimaryBaseVirtual())
|
|
|
|
IndirectPrimaryBases.insert(Layout.getPrimaryBase());
|
|
|
|
|
2011-12-13 07:13:20 +08:00
|
|
|
if (!LayoutVirtualBases(RD, Layout))
|
|
|
|
return false;
|
2011-11-08 12:01:03 +08:00
|
|
|
} else {
|
2011-12-13 07:13:20 +08:00
|
|
|
if (!MSLayoutVirtualBases(RD, Layout))
|
|
|
|
return false;
|
2011-11-08 12:01:03 +08:00
|
|
|
}
|
2010-11-25 09:59:35 +08:00
|
|
|
}
|
2010-11-09 13:25:47 +08:00
|
|
|
|
2009-07-23 11:17:50 +08:00
|
|
|
// Append tail padding if necessary.
|
2011-04-25 00:53:44 +08:00
|
|
|
AppendTailPadding(Layout.getSize());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 11:17:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-25 00:53:44 +08:00
|
|
|
void CGRecordLayoutBuilder::AppendTailPadding(CharUnits RecordSize) {
|
|
|
|
ResizeLastBaseFieldIfNecessary(RecordSize);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-04-25 00:53:44 +08:00
|
|
|
assert(NextFieldOffset <= RecordSize && "Size mismatch!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits AlignedNextFieldOffset =
|
|
|
|
NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
|
2009-12-08 09:24:23 +08:00
|
|
|
|
2011-04-25 00:53:44 +08:00
|
|
|
if (AlignedNextFieldOffset == RecordSize) {
|
2009-12-08 09:24:23 +08:00
|
|
|
// We don't need any padding.
|
|
|
|
return;
|
|
|
|
}
|
2010-03-31 08:11:27 +08:00
|
|
|
|
2011-04-25 00:53:44 +08:00
|
|
|
CharUnits NumPadBytes = RecordSize - NextFieldOffset;
|
2009-07-27 22:55:54 +08:00
|
|
|
AppendBytes(NumPadBytes);
|
|
|
|
}
|
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
void CGRecordLayoutBuilder::AppendField(CharUnits fieldOffset,
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *fieldType) {
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits fieldSize =
|
2012-10-09 00:25:52 +08:00
|
|
|
CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(fieldType));
|
2009-07-24 10:45:50 +08:00
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
FieldTypes.push_back(fieldType);
|
2009-07-23 11:17:50 +08:00
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
NextFieldOffset = fieldOffset + fieldSize;
|
2009-07-23 11:17:50 +08:00
|
|
|
}
|
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
void CGRecordLayoutBuilder::AppendPadding(CharUnits fieldOffset,
|
|
|
|
CharUnits fieldAlignment) {
|
|
|
|
assert(NextFieldOffset <= fieldOffset &&
|
2009-07-23 11:17:50 +08:00
|
|
|
"Incorrect field layout!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-11-08 12:01:03 +08:00
|
|
|
// Do nothing if we're already at the right offset.
|
|
|
|
if (fieldOffset == NextFieldOffset) return;
|
2009-07-23 11:17:50 +08:00
|
|
|
|
2011-11-08 12:01:03 +08:00
|
|
|
// If we're not emitting a packed LLVM type, try to avoid adding
|
|
|
|
// unnecessary padding fields.
|
|
|
|
if (!Packed) {
|
|
|
|
// Round up the field offset to the alignment of the field type.
|
|
|
|
CharUnits alignedNextFieldOffset =
|
|
|
|
NextFieldOffset.RoundUpToAlignment(fieldAlignment);
|
|
|
|
assert(alignedNextFieldOffset <= fieldOffset);
|
2009-07-23 11:17:50 +08:00
|
|
|
|
2011-11-08 12:01:03 +08:00
|
|
|
// If that's the right offset, we're done.
|
|
|
|
if (alignedNextFieldOffset == fieldOffset) return;
|
2009-07-23 11:17:50 +08:00
|
|
|
}
|
2011-11-08 12:01:03 +08:00
|
|
|
|
|
|
|
// Otherwise we need explicit padding.
|
|
|
|
CharUnits padding = fieldOffset - NextFieldOffset;
|
|
|
|
AppendBytes(padding);
|
2009-07-23 11:17:50 +08:00
|
|
|
}
|
|
|
|
|
2011-04-18 05:56:13 +08:00
|
|
|
bool CGRecordLayoutBuilder::ResizeLastBaseFieldIfNecessary(CharUnits offset) {
|
|
|
|
// Check if we have a base to resize.
|
|
|
|
if (!LastLaidOutBase.isValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// This offset does not overlap with the tail padding.
|
|
|
|
if (offset >= NextFieldOffset)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Restore the field offset and append an i8 array instead.
|
|
|
|
FieldTypes.pop_back();
|
|
|
|
NextFieldOffset = LastLaidOutBase.Offset;
|
|
|
|
AppendBytes(LastLaidOutBase.NonVirtualSize);
|
|
|
|
LastLaidOutBase.invalidate();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *CGRecordLayoutBuilder::getByteArrayType(CharUnits numBytes) {
|
2011-02-16 06:21:29 +08:00
|
|
|
assert(!numBytes.isZero() && "Empty byte arrays aren't allowed.");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
|
2011-02-16 06:21:29 +08:00
|
|
|
if (numBytes > CharUnits::One())
|
|
|
|
Ty = llvm::ArrayType::get(Ty, numBytes.getQuantity());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-11-09 13:25:47 +08:00
|
|
|
return Ty;
|
|
|
|
}
|
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
void CGRecordLayoutBuilder::AppendBytes(CharUnits numBytes) {
|
|
|
|
if (numBytes.isZero())
|
2010-11-09 13:25:47 +08:00
|
|
|
return;
|
|
|
|
|
2009-07-23 11:17:50 +08:00
|
|
|
// Append the padding field
|
2011-02-16 06:21:29 +08:00
|
|
|
AppendField(NextFieldOffset, getByteArrayType(numBytes));
|
2009-07-23 11:17:50 +08:00
|
|
|
}
|
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
CharUnits CGRecordLayoutBuilder::getTypeAlignment(llvm::Type *Ty) const {
|
2009-07-24 01:24:40 +08:00
|
|
|
if (Packed)
|
2011-02-16 06:21:29 +08:00
|
|
|
return CharUnits::One();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-10-09 00:25:52 +08:00
|
|
|
return CharUnits::fromQuantity(Types.getDataLayout().getABITypeAlignment(Ty));
|
2009-07-23 11:17:50 +08:00
|
|
|
}
|
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
|
2010-11-29 07:06:23 +08:00
|
|
|
if (Packed)
|
2011-02-16 06:21:29 +08:00
|
|
|
return CharUnits::One();
|
2010-11-29 07:06:23 +08:00
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
CharUnits maxAlignment = CharUnits::One();
|
2010-11-29 07:06:23 +08:00
|
|
|
for (size_t i = 0; i != FieldTypes.size(); ++i)
|
2011-02-16 06:21:29 +08:00
|
|
|
maxAlignment = std::max(maxAlignment, getTypeAlignment(FieldTypes[i]));
|
2010-11-29 07:06:23 +08:00
|
|
|
|
2011-02-16 06:21:29 +08:00
|
|
|
return maxAlignment;
|
2010-11-29 07:06:23 +08:00
|
|
|
}
|
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
/// Merge in whether a field of the given type is zero-initializable.
|
2010-08-23 05:01:12 +08:00
|
|
|
void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
|
2009-08-23 09:25:01 +08:00
|
|
|
// This record already contains a member pointer.
|
2011-02-15 14:40:56 +08:00
|
|
|
if (!IsZeroInitializableAsBase)
|
2009-08-23 09:25:01 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-23 09:25:01 +08:00
|
|
|
// Can only have member pointers if we're compiling C++.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!Types.getContext().getLangOpts().CPlusPlus)
|
2009-08-23 09:25:01 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
const Type *elementType = T->getBaseElementTypeUnsafe();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) {
|
2010-08-23 05:01:12 +08:00
|
|
|
if (!Types.getCXXABI().isZeroInitializable(MPT))
|
2011-02-15 14:40:56 +08:00
|
|
|
IsZeroInitializable = IsZeroInitializableAsBase = false;
|
|
|
|
} else if (const RecordType *RT = elementType->getAs<RecordType>()) {
|
2010-02-02 13:17:25 +08:00
|
|
|
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
|
2011-02-15 14:40:56 +08:00
|
|
|
const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
|
|
|
|
if (!Layout.isZeroInitializable())
|
|
|
|
IsZeroInitializable = IsZeroInitializableAsBase = false;
|
2010-05-19 00:51:41 +08:00
|
|
|
}
|
|
|
|
}
|
2010-03-31 08:11:27 +08:00
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D,
|
|
|
|
llvm::StructType *Ty) {
|
2010-03-31 08:11:27 +08:00
|
|
|
CGRecordLayoutBuilder Builder(*this);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 11:17:50 +08:00
|
|
|
Builder.Layout(D);
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
Ty->setBody(Builder.FieldTypes, Builder.Packed);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
// If we're in C++, compute the base subobject type.
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::StructType *BaseTy = 0;
|
2012-01-13 11:58:31 +08:00
|
|
|
if (isa<CXXRecordDecl>(D) && !D->isUnion()) {
|
2011-02-15 14:40:56 +08:00
|
|
|
BaseTy = Builder.BaseSubobjectType;
|
|
|
|
if (!BaseTy) BaseTy = Ty;
|
2010-11-09 13:25:47 +08:00
|
|
|
}
|
|
|
|
|
2010-03-31 09:09:11 +08:00
|
|
|
CGRecordLayout *RL =
|
2011-02-15 14:40:56 +08:00
|
|
|
new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
|
|
|
|
Builder.IsZeroInitializableAsBase);
|
2010-03-31 09:09:11 +08:00
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
RL->NonVirtualBases.swap(Builder.NonVirtualBases);
|
|
|
|
RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
|
2010-05-18 13:22:06 +08:00
|
|
|
|
2009-07-23 11:17:50 +08:00
|
|
|
// Add all the field numbers.
|
2011-02-15 14:40:56 +08:00
|
|
|
RL->FieldInfo.swap(Builder.Fields);
|
2009-07-23 11:17:50 +08:00
|
|
|
|
|
|
|
// Add bitfield info.
|
2011-02-15 14:40:56 +08:00
|
|
|
RL->BitFields.swap(Builder.BitFields);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-20 04:44:47 +08:00
|
|
|
// Dump the layout, if requested.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getContext().getLangOpts().DumpRecordLayouts) {
|
2010-04-20 04:44:53 +08:00
|
|
|
llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
|
2010-04-14 04:58:55 +08:00
|
|
|
llvm::errs() << "Record: ";
|
|
|
|
D->dump();
|
|
|
|
llvm::errs() << "\nLayout: ";
|
2010-04-13 02:14:18 +08:00
|
|
|
RL->dump();
|
2010-04-14 04:58:55 +08:00
|
|
|
}
|
2010-04-13 02:14:18 +08:00
|
|
|
|
2010-04-22 10:35:46 +08:00
|
|
|
#ifndef NDEBUG
|
2010-04-20 04:44:47 +08:00
|
|
|
// Verify that the computed LLVM struct size matches the AST layout size.
|
2010-11-09 13:25:47 +08:00
|
|
|
const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
|
|
|
|
|
2011-02-11 09:54:29 +08:00
|
|
|
uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
|
2012-10-09 00:25:52 +08:00
|
|
|
assert(TypeSizeInBits == getDataLayout().getTypeAllocSizeInBits(Ty) &&
|
2010-04-20 04:44:47 +08:00
|
|
|
"Type size mismatch!");
|
|
|
|
|
2010-11-09 13:25:47 +08:00
|
|
|
if (BaseTy) {
|
2011-02-08 10:02:47 +08:00
|
|
|
CharUnits NonVirtualSize = Layout.getNonVirtualSize();
|
|
|
|
CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
|
|
|
|
CharUnits AlignedNonVirtualTypeSize =
|
|
|
|
NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
|
|
|
|
|
|
|
|
uint64_t AlignedNonVirtualTypeSizeInBits =
|
2011-02-11 09:54:29 +08:00
|
|
|
getContext().toBits(AlignedNonVirtualTypeSize);
|
2010-11-09 13:25:47 +08:00
|
|
|
|
|
|
|
assert(AlignedNonVirtualTypeSizeInBits ==
|
2012-10-09 00:25:52 +08:00
|
|
|
getDataLayout().getTypeAllocSizeInBits(BaseTy) &&
|
2010-11-09 13:25:47 +08:00
|
|
|
"Type size mismatch!");
|
|
|
|
}
|
|
|
|
|
2010-04-22 03:10:49 +08:00
|
|
|
// Verify that the LLVM and AST field offsets agree.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::StructType *ST =
|
2010-04-22 03:10:49 +08:00
|
|
|
dyn_cast<llvm::StructType>(RL->getLLVMType());
|
2012-10-09 00:25:52 +08:00
|
|
|
const llvm::StructLayout *SL = getDataLayout().getStructLayout(ST);
|
2010-04-22 03:10:49 +08:00
|
|
|
|
|
|
|
const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
|
|
|
|
RecordDecl::field_iterator it = D->field_begin();
|
2011-04-27 07:52:16 +08:00
|
|
|
const FieldDecl *LastFD = 0;
|
2012-10-13 07:29:20 +08:00
|
|
|
bool IsMsStruct = D->isMsStruct(getContext());
|
2010-04-22 03:10:49 +08:00
|
|
|
for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
|
2012-06-07 04:45:41 +08:00
|
|
|
const FieldDecl *FD = *it;
|
2010-04-22 10:35:46 +08:00
|
|
|
|
|
|
|
// For non-bit-fields, just check that the LLVM struct offset matches the
|
|
|
|
// AST offset.
|
|
|
|
if (!FD->isBitField()) {
|
2010-04-22 03:10:49 +08:00
|
|
|
unsigned FieldNo = RL->getLLVMFieldNo(FD);
|
|
|
|
assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
|
|
|
|
"Invalid field offset!");
|
2011-04-27 07:52:16 +08:00
|
|
|
LastFD = FD;
|
2010-04-22 10:35:46 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-04-27 07:52:16 +08:00
|
|
|
if (IsMsStruct) {
|
|
|
|
// Zero-length bitfields following non-bitfield members are
|
|
|
|
// ignored:
|
2011-05-04 04:21:04 +08:00
|
|
|
if (getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
|
2011-04-27 07:52:16 +08:00
|
|
|
--i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
LastFD = FD;
|
|
|
|
}
|
|
|
|
|
2010-04-22 10:35:46 +08:00
|
|
|
// Ignore unnamed bit-fields.
|
2011-04-27 07:52:16 +08:00
|
|
|
if (!FD->getDeclName()) {
|
|
|
|
LastFD = FD;
|
2010-04-22 10:35:46 +08:00
|
|
|
continue;
|
2011-04-27 07:52:16 +08:00
|
|
|
}
|
2012-12-06 19:14:44 +08:00
|
|
|
|
|
|
|
// Don't inspect zero-length bitfields.
|
|
|
|
if (FD->getBitWidthValue(getContext()) == 0)
|
|
|
|
continue;
|
|
|
|
|
2010-04-22 10:35:46 +08:00
|
|
|
const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
|
2012-12-09 18:33:27 +08:00
|
|
|
llvm::Type *ElementTy = ST->getTypeAtIndex(RL->getLLVMFieldNo(FD));
|
|
|
|
|
2012-12-06 19:14:44 +08:00
|
|
|
// Unions have overlapping elements dictating their layout, but for
|
|
|
|
// non-unions we can verify that this section of the layout is the exact
|
2012-12-09 18:33:27 +08:00
|
|
|
// expected size.
|
2012-12-06 19:14:44 +08:00
|
|
|
if (D->isUnion()) {
|
2012-12-09 18:33:27 +08:00
|
|
|
// For unions we verify that the start is zero and the size
|
|
|
|
// is in-bounds. However, on BE systems, the offset may be non-zero, but
|
|
|
|
// the size + offset should match the storage size in that case as it
|
|
|
|
// "starts" at the back.
|
|
|
|
if (getDataLayout().isBigEndian())
|
2013-01-16 07:13:49 +08:00
|
|
|
assert(static_cast<unsigned>(Info.Offset + Info.Size) ==
|
|
|
|
Info.StorageSize &&
|
2012-12-09 18:33:27 +08:00
|
|
|
"Big endian union bitfield does not end at the back");
|
|
|
|
else
|
|
|
|
assert(Info.Offset == 0 &&
|
|
|
|
"Little endian union bitfield with a non-zero offset");
|
2012-12-06 19:14:44 +08:00
|
|
|
assert(Info.StorageSize <= SL->getSizeInBits() &&
|
|
|
|
"Union not large enough for bitfield storage");
|
|
|
|
} else {
|
|
|
|
assert(Info.StorageSize ==
|
|
|
|
getDataLayout().getTypeAllocSizeInBits(ElementTy) &&
|
|
|
|
"Storage size does not match the element type size");
|
2010-04-22 03:10:49 +08:00
|
|
|
}
|
2012-12-06 19:14:44 +08:00
|
|
|
assert(Info.Size > 0 && "Empty bitfield!");
|
2012-12-19 02:53:14 +08:00
|
|
|
assert(static_cast<unsigned>(Info.Offset) + Info.Size <= Info.StorageSize &&
|
2012-12-06 19:14:44 +08:00
|
|
|
"Bitfield outside of its allocated storage");
|
2010-04-22 03:10:49 +08:00
|
|
|
}
|
|
|
|
#endif
|
2010-04-20 04:44:47 +08:00
|
|
|
|
2010-03-31 09:09:11 +08:00
|
|
|
return RL;
|
2009-07-23 11:17:50 +08:00
|
|
|
}
|
2010-04-13 02:14:18 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void CGRecordLayout::print(raw_ostream &OS) const {
|
2010-04-13 02:14:18 +08:00
|
|
|
OS << "<CGRecordLayout\n";
|
2011-02-15 14:40:56 +08:00
|
|
|
OS << " LLVMType:" << *CompleteObjectType << "\n";
|
|
|
|
if (BaseSubobjectType)
|
|
|
|
OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
|
2010-08-23 05:01:12 +08:00
|
|
|
OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
|
2010-04-13 02:14:18 +08:00
|
|
|
OS << " BitFields:[\n";
|
2010-04-22 10:35:36 +08:00
|
|
|
|
|
|
|
// Print bit-field infos in declaration order.
|
|
|
|
std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
|
2010-04-13 02:14:18 +08:00
|
|
|
for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
|
|
|
|
it = BitFields.begin(), ie = BitFields.end();
|
|
|
|
it != ie; ++it) {
|
2010-04-22 10:35:36 +08:00
|
|
|
const RecordDecl *RD = it->first->getParent();
|
|
|
|
unsigned Index = 0;
|
|
|
|
for (RecordDecl::field_iterator
|
2012-06-07 04:45:41 +08:00
|
|
|
it2 = RD->field_begin(); *it2 != it->first; ++it2)
|
2010-04-22 10:35:36 +08:00
|
|
|
++Index;
|
|
|
|
BFIs.push_back(std::make_pair(Index, &it->second));
|
|
|
|
}
|
|
|
|
llvm::array_pod_sort(BFIs.begin(), BFIs.end());
|
|
|
|
for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
|
2010-04-14 04:58:55 +08:00
|
|
|
OS.indent(4);
|
2010-04-22 10:35:36 +08:00
|
|
|
BFIs[i].second->print(OS);
|
2010-04-13 02:14:18 +08:00
|
|
|
OS << "\n";
|
|
|
|
}
|
2010-04-22 10:35:36 +08:00
|
|
|
|
2010-04-13 02:14:18 +08:00
|
|
|
OS << "]>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGRecordLayout::dump() const {
|
|
|
|
print(llvm::errs());
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void CGBitFieldInfo::print(raw_ostream &OS) const {
|
2012-12-06 19:14:44 +08:00
|
|
|
OS << "<CGBitFieldInfo"
|
|
|
|
<< " Offset:" << Offset
|
|
|
|
<< " Size:" << Size
|
|
|
|
<< " IsSigned:" << IsSigned
|
|
|
|
<< " StorageSize:" << StorageSize
|
|
|
|
<< " StorageAlignment:" << StorageAlignment << ">";
|
2010-04-13 02:14:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGBitFieldInfo::dump() const {
|
|
|
|
print(llvm::errs());
|
|
|
|
}
|