1//===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder  ----*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Builder implementation for CGRecordLayout objects.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGRecordLayout.h"
15#include "CGCXXABI.h"
16#include "CodeGenTypes.h"
17#include "clang/AST/ASTContext.h"
18#include "clang/AST/Attr.h"
19#include "clang/AST/CXXInheritance.h"
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/RecordLayout.h"
23#include "clang/Frontend/CodeGenOptions.h"
24#include "llvm/IR/DataLayout.h"
25#include "llvm/IR/DerivedTypes.h"
26#include "llvm/IR/Type.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/MathExtras.h"
29#include "llvm/Support/raw_ostream.h"
30using namespace clang;
31using namespace CodeGen;
32
33namespace {
34/// The CGRecordLowering is responsible for lowering an ASTRecordLayout to an
35/// llvm::Type.  Some of the lowering is straightforward, some is not.  Here we
36/// detail some of the complexities and weirdnesses here.
37/// * LLVM does not have unions - Unions can, in theory be represented by any
38///   llvm::Type with correct size.  We choose a field via a specific heuristic
39///   and add padding if necessary.
40/// * LLVM does not have bitfields - Bitfields are collected into contiguous
41///   runs and allocated as a single storage type for the run.  ASTRecordLayout
42///   contains enough information to determine where the runs break.  Microsoft
43///   and Itanium follow different rules and use different codepaths.
44/// * It is desired that, when possible, bitfields use the appropriate iN type
45///   when lowered to llvm types.  For example unsigned x : 24 gets lowered to
46///   i24.  This isn't always possible because i24 has storage size of 32 bit
47///   and if it is possible to use that extra byte of padding we must use
48///   [i8 x 3] instead of i24.  The function clipTailPadding does this.
49///   C++ examples that require clipping:
50///   struct { int a : 24; char b; }; // a must be clipped, b goes at offset 3
51///   struct A { int a : 24; }; // a must be clipped because a struct like B
52//    could exist: struct B : A { char b; }; // b goes at offset 3
53/// * Clang ignores 0 sized bitfields and 0 sized bases but *not* zero sized
54///   fields.  The existing asserts suggest that LLVM assumes that *every* field
55///   has an underlying storage type.  Therefore empty structures containing
56///   zero sized subobjects such as empty records or zero sized arrays still get
57///   a zero sized (empty struct) storage type.
58/// * Clang reads the complete type rather than the base type when generating
59///   code to access fields.  Bitfields in tail position with tail padding may
60///   be clipped in the base class but not the complete class (we may discover
61///   that the tail padding is not used in the complete class.) However,
62///   because LLVM reads from the complete type it can generate incorrect code
63///   if we do not clip the tail padding off of the bitfield in the complete
64///   layout.  This introduces a somewhat awkward extra unnecessary clip stage.
65///   The location of the clip is stored internally as a sentinal of type
66///   SCISSOR.  If LLVM were updated to read base types (which it probably
67///   should because locations of things such as VBases are bogus in the llvm
68///   type anyway) then we could eliminate the SCISSOR.
69/// * Itanium allows nearly empty primary virtual bases.  These bases don't get
70///   get their own storage because they're laid out as part of another base
71///   or at the beginning of the structure.  Determining if a VBase actually
72///   gets storage awkwardly involves a walk of all bases.
73/// * VFPtrs and VBPtrs do *not* make a record NotZeroInitializable.
74struct CGRecordLowering {
75  // MemberInfo is a helper structure that contains information about a record
76  // member.  In additional to the standard member types, there exists a
77  // sentinal member type that ensures correct rounding.
78  struct MemberInfo {
79    CharUnits Offset;
80    enum InfoKind { VFPtr, VBPtr, Field, Base, VBase, Scissor } Kind;
81    llvm::Type *Data;
82    union {
83      const FieldDecl *FD;
84      const CXXRecordDecl *RD;
85    };
86    MemberInfo(CharUnits Offset, InfoKind Kind, llvm::Type *Data,
87               const FieldDecl *FD = nullptr)
88      : Offset(Offset), Kind(Kind), Data(Data), FD(FD) {}
89    MemberInfo(CharUnits Offset, InfoKind Kind, llvm::Type *Data,
90               const CXXRecordDecl *RD)
91      : Offset(Offset), Kind(Kind), Data(Data), RD(RD) {}
92    // MemberInfos are sorted so we define a < operator.
93    bool operator <(const MemberInfo& a) const { return Offset < a.Offset; }
94  };
95  // The constructor.
96  CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D);
97  // Short helper routines.
98  /// \brief Constructs a MemberInfo instance from an offset and llvm::Type *.
99  MemberInfo StorageInfo(CharUnits Offset, llvm::Type *Data) {
100    return MemberInfo(Offset, MemberInfo::Field, Data);
101  }
102  bool useMSABI() {
103    return Context.getTargetInfo().getCXXABI().isMicrosoft() ||
104           D->isMsStruct(Context);
105  }
106  /// \brief Wraps llvm::Type::getIntNTy with some implicit arguments.
107  llvm::Type *getIntNType(uint64_t NumBits) {
108    return llvm::Type::getIntNTy(Types.getLLVMContext(),
109        (unsigned)llvm::RoundUpToAlignment(NumBits, 8));
110  }
111  /// \brief Gets an llvm type of size NumBytes and alignment 1.
112  llvm::Type *getByteArrayType(CharUnits NumBytes) {
113    assert(!NumBytes.isZero() && "Empty byte arrays aren't allowed.");
114    llvm::Type *Type = llvm::Type::getInt8Ty(Types.getLLVMContext());
115    return NumBytes == CharUnits::One() ? Type :
116        (llvm::Type *)llvm::ArrayType::get(Type, NumBytes.getQuantity());
117  }
118  /// \brief Gets the storage type for a field decl and handles storage
119  /// for itanium bitfields that are smaller than their declared type.
120  llvm::Type *getStorageType(const FieldDecl *FD) {
121    llvm::Type *Type = Types.ConvertTypeForMem(FD->getType());
122    return useMSABI() || !FD->isBitField() ? Type :
123        getIntNType(std::min(FD->getBitWidthValue(Context),
124                             (unsigned)Context.toBits(getSize(Type))));
125  }
126  /// \brief Gets the llvm Basesubobject type from a CXXRecordDecl.
127  llvm::Type *getStorageType(const CXXRecordDecl *RD) {
128    return Types.getCGRecordLayout(RD).getBaseSubobjectLLVMType();
129  }
130  CharUnits bitsToCharUnits(uint64_t BitOffset) {
131    return Context.toCharUnitsFromBits(BitOffset);
132  }
133  CharUnits getSize(llvm::Type *Type) {
134    return CharUnits::fromQuantity(DataLayout.getTypeAllocSize(Type));
135  }
136  CharUnits getAlignment(llvm::Type *Type) {
137    return CharUnits::fromQuantity(DataLayout.getABITypeAlignment(Type));
138  }
139  bool isZeroInitializable(const FieldDecl *FD) {
140    const Type *Type = FD->getType()->getBaseElementTypeUnsafe();
141    if (const MemberPointerType *MPT = Type->getAs<MemberPointerType>())
142      return Types.getCXXABI().isZeroInitializable(MPT);
143    if (const RecordType *RT = Type->getAs<RecordType>())
144      return isZeroInitializable(RT->getDecl());
145    return true;
146  }
147  bool isZeroInitializable(const RecordDecl *RD) {
148    return Types.getCGRecordLayout(RD).isZeroInitializable();
149  }
150  void appendPaddingBytes(CharUnits Size) {
151    if (!Size.isZero())
152      FieldTypes.push_back(getByteArrayType(Size));
153  }
154  uint64_t getFieldBitOffset(const FieldDecl *FD) {
155    return Layout.getFieldOffset(FD->getFieldIndex());
156  }
157  // Layout routines.
158  void setBitFieldInfo(const FieldDecl *FD, CharUnits StartOffset,
159                       llvm::Type *StorageType);
160  /// \brief Lowers an ASTRecordLayout to a llvm type.
161  void lower(bool NonVirtualBaseType);
162  void lowerUnion();
163  void accumulateFields();
164  void accumulateBitFields(RecordDecl::field_iterator Field,
165                        RecordDecl::field_iterator FieldEnd);
166  void accumulateBases();
167  void accumulateVPtrs();
168  void accumulateVBases();
169  /// \brief Recursively searches all of the bases to find out if a vbase is
170  /// not the primary vbase of some base class.
171  bool hasOwnStorage(const CXXRecordDecl *Decl, const CXXRecordDecl *Query);
172  void calculateZeroInit();
173  /// \brief Lowers bitfield storage types to I8 arrays for bitfields with tail
174  /// padding that is or can potentially be used.
175  void clipTailPadding();
176  /// \brief Determines if we need a packed llvm struct.
177  void determinePacked();
178  /// \brief Inserts padding everwhere it's needed.
179  void insertPadding();
180  /// \brief Fills out the structures that are ultimately consumed.
181  void fillOutputFields();
182  // Input memoization fields.
183  CodeGenTypes &Types;
184  const ASTContext &Context;
185  const RecordDecl *D;
186  const CXXRecordDecl *RD;
187  const ASTRecordLayout &Layout;
188  const llvm::DataLayout &DataLayout;
189  // Helpful intermediate data-structures.
190  std::vector<MemberInfo> Members;
191  // Output fields, consumed by CodeGenTypes::ComputeRecordLayout.
192  SmallVector<llvm::Type *, 16> FieldTypes;
193  llvm::DenseMap<const FieldDecl *, unsigned> Fields;
194  llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
195  llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
196  llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases;
197  bool IsZeroInitializable : 1;
198  bool IsZeroInitializableAsBase : 1;
199  bool Packed : 1;
200private:
201  CGRecordLowering(const CGRecordLowering &) LLVM_DELETED_FUNCTION;
202  void operator =(const CGRecordLowering &) LLVM_DELETED_FUNCTION;
203};
204} // namespace {
205
206CGRecordLowering::CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D)
207  : Types(Types), Context(Types.getContext()), D(D),
208    RD(dyn_cast<CXXRecordDecl>(D)),
209    Layout(Types.getContext().getASTRecordLayout(D)),
210    DataLayout(Types.getDataLayout()), IsZeroInitializable(true),
211    IsZeroInitializableAsBase(true), Packed(false) {}
212
213void CGRecordLowering::setBitFieldInfo(
214    const FieldDecl *FD, CharUnits StartOffset, llvm::Type *StorageType) {
215  CGBitFieldInfo &Info = BitFields[FD->getCanonicalDecl()];
216  Info.IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
217  Info.Offset = (unsigned)(getFieldBitOffset(FD) - Context.toBits(StartOffset));
218  Info.Size = FD->getBitWidthValue(Context);
219  Info.StorageSize = (unsigned)DataLayout.getTypeAllocSizeInBits(StorageType);
220  // Here we calculate the actual storage alignment of the bits.  E.g if we've
221  // got an alignment >= 2 and the bitfield starts at offset 6 we've got an
222  // alignment of 2.
223  Info.StorageAlignment =
224      Layout.getAlignment().alignmentAtOffset(StartOffset).getQuantity();
225  if (Info.Size > Info.StorageSize)
226    Info.Size = Info.StorageSize;
227  // Reverse the bit offsets for big endian machines. Because we represent
228  // a bitfield as a single large integer load, we can imagine the bits
229  // counting from the most-significant-bit instead of the
230  // least-significant-bit.
231  if (DataLayout.isBigEndian())
232    Info.Offset = Info.StorageSize - (Info.Offset + Info.Size);
233}
234
235void CGRecordLowering::lower(bool NVBaseType) {
236  // The lowering process implemented in this function takes a variety of
237  // carefully ordered phases.
238  // 1) Store all members (fields and bases) in a list and sort them by offset.
239  // 2) Add a 1-byte capstone member at the Size of the structure.
240  // 3) Clip bitfield storages members if their tail padding is or might be
241  //    used by another field or base.  The clipping process uses the capstone
242  //    by treating it as another object that occurs after the record.
243  // 4) Determine if the llvm-struct requires packing.  It's important that this
244  //    phase occur after clipping, because clipping changes the llvm type.
245  //    This phase reads the offset of the capstone when determining packedness
246  //    and updates the alignment of the capstone to be equal of the alignment
247  //    of the record after doing so.
248  // 5) Insert padding everywhere it is needed.  This phase requires 'Packed' to
249  //    have been computed and needs to know the alignment of the record in
250  //    order to understand if explicit tail padding is needed.
251  // 6) Remove the capstone, we don't need it anymore.
252  // 7) Determine if this record can be zero-initialized.  This phase could have
253  //    been placed anywhere after phase 1.
254  // 8) Format the complete list of members in a way that can be consumed by
255  //    CodeGenTypes::ComputeRecordLayout.
256  CharUnits Size = NVBaseType ? Layout.getNonVirtualSize() : Layout.getSize();
257  if (D->isUnion())
258    return lowerUnion();
259  accumulateFields();
260  // RD implies C++.
261  if (RD) {
262    accumulateVPtrs();
263    accumulateBases();
264    if (Members.empty())
265      return appendPaddingBytes(Size);
266    if (!NVBaseType)
267      accumulateVBases();
268  }
269  std::stable_sort(Members.begin(), Members.end());
270  Members.push_back(StorageInfo(Size, getIntNType(8)));
271  clipTailPadding();
272  determinePacked();
273  insertPadding();
274  Members.pop_back();
275  calculateZeroInit();
276  fillOutputFields();
277}
278
279void CGRecordLowering::lowerUnion() {
280  CharUnits LayoutSize = Layout.getSize();
281  llvm::Type *StorageType = nullptr;
282  // Compute zero-initializable status.
283  if (!D->field_empty() && !isZeroInitializable(*D->field_begin()))
284    IsZeroInitializable = IsZeroInitializableAsBase = false;
285  // Iterate through the fields setting bitFieldInfo and the Fields array. Also
286  // locate the "most appropriate" storage type.  The heuristic for finding the
287  // storage type isn't necessary, the first (non-0-length-bitfield) field's
288  // type would work fine and be simpler but would be differen than what we've
289  // been doing and cause lit tests to change.
290  for (const auto *Field : D->fields()) {
291    if (Field->isBitField()) {
292      // Skip 0 sized bitfields.
293      if (Field->getBitWidthValue(Context) == 0)
294        continue;
295      llvm::Type *FieldType = getStorageType(Field);
296      if (LayoutSize < getSize(FieldType))
297        FieldType = getByteArrayType(LayoutSize);
298      setBitFieldInfo(Field, CharUnits::Zero(), FieldType);
299    }
300    Fields[Field->getCanonicalDecl()] = 0;
301    llvm::Type *FieldType = getStorageType(Field);
302    // Conditionally update our storage type if we've got a new "better" one.
303    if (!StorageType ||
304        getAlignment(FieldType) >  getAlignment(StorageType) ||
305        (getAlignment(FieldType) == getAlignment(StorageType) &&
306        getSize(FieldType) > getSize(StorageType)))
307      StorageType = FieldType;
308  }
309  // If we have no storage type just pad to the appropriate size and return.
310  if (!StorageType)
311    return appendPaddingBytes(LayoutSize);
312  // If our storage size was bigger than our required size (can happen in the
313  // case of packed bitfields on Itanium) then just use an I8 array.
314  if (LayoutSize < getSize(StorageType))
315    StorageType = getByteArrayType(LayoutSize);
316  FieldTypes.push_back(StorageType);
317  appendPaddingBytes(LayoutSize - getSize(StorageType));
318  // Set packed if we need it.
319  if (LayoutSize % getAlignment(StorageType))
320    Packed = true;
321}
322
323void CGRecordLowering::accumulateFields() {
324  for (RecordDecl::field_iterator Field = D->field_begin(),
325                                  FieldEnd = D->field_end();
326    Field != FieldEnd;)
327    if (Field->isBitField()) {
328      RecordDecl::field_iterator Start = Field;
329      // Iterate to gather the list of bitfields.
330      for (++Field; Field != FieldEnd && Field->isBitField(); ++Field);
331      accumulateBitFields(Start, Field);
332    } else {
333      Members.push_back(MemberInfo(
334          bitsToCharUnits(getFieldBitOffset(*Field)), MemberInfo::Field,
335          getStorageType(*Field), *Field));
336      ++Field;
337    }
338}
339
340void
341CGRecordLowering::accumulateBitFields(RecordDecl::field_iterator Field,
342                                      RecordDecl::field_iterator FieldEnd) {
343  // Run stores the first element of the current run of bitfields.  FieldEnd is
344  // used as a special value to note that we don't have a current run.  A
345  // bitfield run is a contiguous collection of bitfields that can be stored in
346  // the same storage block.  Zero-sized bitfields and bitfields that would
347  // cross an alignment boundary break a run and start a new one.
348  RecordDecl::field_iterator Run = FieldEnd;
349  // Tail is the offset of the first bit off the end of the current run.  It's
350  // used to determine if the ASTRecordLayout is treating these two bitfields as
351  // contiguous.  StartBitOffset is offset of the beginning of the Run.
352  uint64_t StartBitOffset, Tail = 0;
353  if (useMSABI()) {
354    for (; Field != FieldEnd; ++Field) {
355      uint64_t BitOffset = getFieldBitOffset(*Field);
356      // Zero-width bitfields end runs.
357      if (Field->getBitWidthValue(Context) == 0) {
358        Run = FieldEnd;
359        continue;
360      }
361      llvm::Type *Type = Types.ConvertTypeForMem(Field->getType());
362      // If we don't have a run yet, or don't live within the previous run's
363      // allocated storage then we allocate some storage and start a new run.
364      if (Run == FieldEnd || BitOffset >= Tail) {
365        Run = Field;
366        StartBitOffset = BitOffset;
367        Tail = StartBitOffset + DataLayout.getTypeAllocSizeInBits(Type);
368        // Add the storage member to the record.  This must be added to the
369        // record before the bitfield members so that it gets laid out before
370        // the bitfields it contains get laid out.
371        Members.push_back(StorageInfo(bitsToCharUnits(StartBitOffset), Type));
372      }
373      // Bitfields get the offset of their storage but come afterward and remain
374      // there after a stable sort.
375      Members.push_back(MemberInfo(bitsToCharUnits(StartBitOffset),
376                                   MemberInfo::Field, nullptr, *Field));
377    }
378    return;
379  }
380  for (;;) {
381    // Check to see if we need to start a new run.
382    if (Run == FieldEnd) {
383      // If we're out of fields, return.
384      if (Field == FieldEnd)
385        break;
386      // Any non-zero-length bitfield can start a new run.
387      if (Field->getBitWidthValue(Context) != 0) {
388        Run = Field;
389        StartBitOffset = getFieldBitOffset(*Field);
390        Tail = StartBitOffset + Field->getBitWidthValue(Context);
391      }
392      ++Field;
393      continue;
394    }
395    // Add bitfields to the run as long as they qualify.
396    if (Field != FieldEnd && Field->getBitWidthValue(Context) != 0 &&
397        Tail == getFieldBitOffset(*Field)) {
398      Tail += Field->getBitWidthValue(Context);
399      ++Field;
400      continue;
401    }
402    // We've hit a break-point in the run and need to emit a storage field.
403    llvm::Type *Type = getIntNType(Tail - StartBitOffset);
404    // Add the storage member to the record and set the bitfield info for all of
405    // the bitfields in the run.  Bitfields get the offset of their storage but
406    // come afterward and remain there after a stable sort.
407    Members.push_back(StorageInfo(bitsToCharUnits(StartBitOffset), Type));
408    for (; Run != Field; ++Run)
409      Members.push_back(MemberInfo(bitsToCharUnits(StartBitOffset),
410                                   MemberInfo::Field, nullptr, *Run));
411    Run = FieldEnd;
412  }
413}
414
415void CGRecordLowering::accumulateBases() {
416  // If we've got a primary virtual base, we need to add it with the bases.
417  if (Layout.isPrimaryBaseVirtual()) {
418    const CXXRecordDecl *BaseDecl = Layout.getPrimaryBase();
419    Members.push_back(MemberInfo(CharUnits::Zero(), MemberInfo::Base,
420                                 getStorageType(BaseDecl), BaseDecl));
421  }
422  // Accumulate the non-virtual bases.
423  for (const auto &Base : RD->bases()) {
424    if (Base.isVirtual())
425      continue;
426    const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
427    if (!BaseDecl->isEmpty())
428      Members.push_back(MemberInfo(Layout.getBaseClassOffset(BaseDecl),
429          MemberInfo::Base, getStorageType(BaseDecl), BaseDecl));
430  }
431}
432
433void CGRecordLowering::accumulateVPtrs() {
434  if (Layout.hasOwnVFPtr())
435    Members.push_back(MemberInfo(CharUnits::Zero(), MemberInfo::VFPtr,
436        llvm::FunctionType::get(getIntNType(32), /*isVarArg=*/true)->
437            getPointerTo()->getPointerTo()));
438  if (Layout.hasOwnVBPtr())
439    Members.push_back(MemberInfo(Layout.getVBPtrOffset(), MemberInfo::VBPtr,
440        llvm::Type::getInt32PtrTy(Types.getLLVMContext())));
441}
442
443void CGRecordLowering::accumulateVBases() {
444  CharUnits ScissorOffset = Layout.getNonVirtualSize();
445  // In the itanium ABI, it's possible to place a vbase at a dsize that is
446  // smaller than the nvsize.  Here we check to see if such a base is placed
447  // before the nvsize and set the scissor offset to that, instead of the
448  // nvsize.
449  if (!useMSABI())
450    for (const auto &Base : RD->vbases()) {
451      const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
452      if (BaseDecl->isEmpty())
453        continue;
454      // If the vbase is a primary virtual base of some base, then it doesn't
455      // get its own storage location but instead lives inside of that base.
456      if (Context.isNearlyEmpty(BaseDecl) && !hasOwnStorage(RD, BaseDecl))
457        continue;
458      ScissorOffset = std::min(ScissorOffset,
459                               Layout.getVBaseClassOffset(BaseDecl));
460    }
461  Members.push_back(MemberInfo(ScissorOffset, MemberInfo::Scissor, nullptr,
462                               RD));
463  for (const auto &Base : RD->vbases()) {
464    const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
465    if (BaseDecl->isEmpty())
466      continue;
467    CharUnits Offset = Layout.getVBaseClassOffset(BaseDecl);
468    // If the vbase is a primary virtual base of some base, then it doesn't
469    // get its own storage location but instead lives inside of that base.
470    if (!useMSABI() && Context.isNearlyEmpty(BaseDecl) &&
471        !hasOwnStorage(RD, BaseDecl)) {
472      Members.push_back(MemberInfo(Offset, MemberInfo::VBase, nullptr,
473                                   BaseDecl));
474      continue;
475    }
476    // If we've got a vtordisp, add it as a storage type.
477    if (Layout.getVBaseOffsetsMap().find(BaseDecl)->second.hasVtorDisp())
478      Members.push_back(StorageInfo(Offset - CharUnits::fromQuantity(4),
479                                    getIntNType(32)));
480    Members.push_back(MemberInfo(Offset, MemberInfo::VBase,
481                                 getStorageType(BaseDecl), BaseDecl));
482  }
483}
484
485bool CGRecordLowering::hasOwnStorage(const CXXRecordDecl *Decl,
486                                     const CXXRecordDecl *Query) {
487  const ASTRecordLayout &DeclLayout = Context.getASTRecordLayout(Decl);
488  if (DeclLayout.isPrimaryBaseVirtual() && DeclLayout.getPrimaryBase() == Query)
489    return false;
490  for (const auto &Base : Decl->bases())
491    if (!hasOwnStorage(Base.getType()->getAsCXXRecordDecl(), Query))
492      return false;
493  return true;
494}
495
496void CGRecordLowering::calculateZeroInit() {
497  for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
498                                               MemberEnd = Members.end();
499       IsZeroInitializableAsBase && Member != MemberEnd; ++Member) {
500    if (Member->Kind == MemberInfo::Field) {
501      if (!Member->FD || isZeroInitializable(Member->FD))
502        continue;
503      IsZeroInitializable = IsZeroInitializableAsBase = false;
504    } else if (Member->Kind == MemberInfo::Base ||
505               Member->Kind == MemberInfo::VBase) {
506      if (isZeroInitializable(Member->RD))
507        continue;
508      IsZeroInitializable = false;
509      if (Member->Kind == MemberInfo::Base)
510        IsZeroInitializableAsBase = false;
511    }
512  }
513}
514
515void CGRecordLowering::clipTailPadding() {
516  std::vector<MemberInfo>::iterator Prior = Members.begin();
517  CharUnits Tail = getSize(Prior->Data);
518  for (std::vector<MemberInfo>::iterator Member = Prior + 1,
519                                         MemberEnd = Members.end();
520       Member != MemberEnd; ++Member) {
521    // Only members with data and the scissor can cut into tail padding.
522    if (!Member->Data && Member->Kind != MemberInfo::Scissor)
523      continue;
524    if (Member->Offset < Tail) {
525      assert(Prior->Kind == MemberInfo::Field && !Prior->FD &&
526             "Only storage fields have tail padding!");
527      Prior->Data = getByteArrayType(bitsToCharUnits(llvm::RoundUpToAlignment(
528          cast<llvm::IntegerType>(Prior->Data)->getIntegerBitWidth(), 8)));
529    }
530    if (Member->Data)
531      Prior = Member;
532    Tail = Prior->Offset + getSize(Prior->Data);
533  }
534}
535
536void CGRecordLowering::determinePacked() {
537  CharUnits Alignment = CharUnits::One();
538  for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
539                                               MemberEnd = Members.end();
540       Member != MemberEnd; ++Member) {
541    if (!Member->Data)
542      continue;
543    // If any member falls at an offset that it not a multiple of its alignment,
544    // then the entire record must be packed.
545    if (Member->Offset % getAlignment(Member->Data))
546      Packed = true;
547    Alignment = std::max(Alignment, getAlignment(Member->Data));
548  }
549  // If the size of the record (the capstone's offset) is not a multiple of the
550  // record's alignment, it must be packed.
551  if (Members.back().Offset % Alignment)
552    Packed = true;
553  // Update the alignment of the sentinal.
554  if (!Packed)
555    Members.back().Data = getIntNType(Context.toBits(Alignment));
556}
557
558void CGRecordLowering::insertPadding() {
559  std::vector<std::pair<CharUnits, CharUnits> > Padding;
560  CharUnits Size = CharUnits::Zero();
561  for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
562                                               MemberEnd = Members.end();
563       Member != MemberEnd; ++Member) {
564    if (!Member->Data)
565      continue;
566    CharUnits Offset = Member->Offset;
567    assert(Offset >= Size);
568    // Insert padding if we need to.
569    if (Offset != Size.RoundUpToAlignment(Packed ? CharUnits::One() :
570                                          getAlignment(Member->Data)))
571      Padding.push_back(std::make_pair(Size, Offset - Size));
572    Size = Offset + getSize(Member->Data);
573  }
574  if (Padding.empty())
575    return;
576  // Add the padding to the Members list and sort it.
577  for (std::vector<std::pair<CharUnits, CharUnits> >::const_iterator
578        Pad = Padding.begin(), PadEnd = Padding.end();
579        Pad != PadEnd; ++Pad)
580    Members.push_back(StorageInfo(Pad->first, getByteArrayType(Pad->second)));
581  std::stable_sort(Members.begin(), Members.end());
582}
583
584void CGRecordLowering::fillOutputFields() {
585  for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
586                                               MemberEnd = Members.end();
587       Member != MemberEnd; ++Member) {
588    if (Member->Data)
589      FieldTypes.push_back(Member->Data);
590    if (Member->Kind == MemberInfo::Field) {
591      if (Member->FD)
592        Fields[Member->FD->getCanonicalDecl()] = FieldTypes.size() - 1;
593      // A field without storage must be a bitfield.
594      if (!Member->Data)
595        setBitFieldInfo(Member->FD, Member->Offset, FieldTypes.back());
596    } else if (Member->Kind == MemberInfo::Base)
597      NonVirtualBases[Member->RD] = FieldTypes.size() - 1;
598    else if (Member->Kind == MemberInfo::VBase)
599      VirtualBases[Member->RD] = FieldTypes.size() - 1;
600  }
601}
602
603CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
604                                        const FieldDecl *FD,
605                                        uint64_t Offset, uint64_t Size,
606                                        uint64_t StorageSize,
607                                        uint64_t StorageAlignment) {
608  // This function is vestigial from CGRecordLayoutBuilder days but is still
609  // used in GCObjCRuntime.cpp.  That usage has a "fixme" attached to it that
610  // when addressed will allow for the removal of this function.
611  llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType());
612  CharUnits TypeSizeInBytes =
613    CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(Ty));
614  uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
615
616  bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
617
618  if (Size > TypeSizeInBits) {
619    // We have a wide bit-field. The extra bits are only used for padding, so
620    // if we have a bitfield of type T, with size N:
621    //
622    // T t : N;
623    //
624    // We can just assume that it's:
625    //
626    // T t : sizeof(T);
627    //
628    Size = TypeSizeInBits;
629  }
630
631  // Reverse the bit offsets for big endian machines. Because we represent
632  // a bitfield as a single large integer load, we can imagine the bits
633  // counting from the most-significant-bit instead of the
634  // least-significant-bit.
635  if (Types.getDataLayout().isBigEndian()) {
636    Offset = StorageSize - (Offset + Size);
637  }
638
639  return CGBitFieldInfo(Offset, Size, IsSigned, StorageSize, StorageAlignment);
640}
641
642CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D,
643                                                  llvm::StructType *Ty) {
644  CGRecordLowering Builder(*this, D);
645
646  Builder.lower(false);
647
648  // If we're in C++, compute the base subobject type.
649  llvm::StructType *BaseTy = nullptr;
650  if (isa<CXXRecordDecl>(D) && !D->isUnion() && !D->hasAttr<FinalAttr>()) {
651    BaseTy = Ty;
652    if (Builder.Layout.getNonVirtualSize() != Builder.Layout.getSize()) {
653      CGRecordLowering BaseBuilder(*this, D);
654      BaseBuilder.lower(true);
655      BaseTy = llvm::StructType::create(
656          getLLVMContext(), BaseBuilder.FieldTypes, "", BaseBuilder.Packed);
657      addRecordTypeName(D, BaseTy, ".base");
658    }
659  }
660
661  // Fill in the struct *after* computing the base type.  Filling in the body
662  // signifies that the type is no longer opaque and record layout is complete,
663  // but we may need to recursively layout D while laying D out as a base type.
664  Ty->setBody(Builder.FieldTypes, Builder.Packed);
665
666  CGRecordLayout *RL =
667    new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
668                        Builder.IsZeroInitializableAsBase);
669
670  RL->NonVirtualBases.swap(Builder.NonVirtualBases);
671  RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
672
673  // Add all the field numbers.
674  RL->FieldInfo.swap(Builder.Fields);
675
676  // Add bitfield info.
677  RL->BitFields.swap(Builder.BitFields);
678
679  // Dump the layout, if requested.
680  if (getContext().getLangOpts().DumpRecordLayouts) {
681    llvm::outs() << "\n*** Dumping IRgen Record Layout\n";
682    llvm::outs() << "Record: ";
683    D->dump(llvm::outs());
684    llvm::outs() << "\nLayout: ";
685    RL->print(llvm::outs());
686  }
687
688#ifndef NDEBUG
689  // Verify that the computed LLVM struct size matches the AST layout size.
690  const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
691
692  uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
693  assert(TypeSizeInBits == getDataLayout().getTypeAllocSizeInBits(Ty) &&
694         "Type size mismatch!");
695
696  if (BaseTy) {
697    CharUnits NonVirtualSize  = Layout.getNonVirtualSize();
698
699    uint64_t AlignedNonVirtualTypeSizeInBits =
700      getContext().toBits(NonVirtualSize);
701
702    assert(AlignedNonVirtualTypeSizeInBits ==
703           getDataLayout().getTypeAllocSizeInBits(BaseTy) &&
704           "Type size mismatch!");
705  }
706
707  // Verify that the LLVM and AST field offsets agree.
708  llvm::StructType *ST =
709    dyn_cast<llvm::StructType>(RL->getLLVMType());
710  const llvm::StructLayout *SL = getDataLayout().getStructLayout(ST);
711
712  const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
713  RecordDecl::field_iterator it = D->field_begin();
714  for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
715    const FieldDecl *FD = *it;
716
717    // For non-bit-fields, just check that the LLVM struct offset matches the
718    // AST offset.
719    if (!FD->isBitField()) {
720      unsigned FieldNo = RL->getLLVMFieldNo(FD);
721      assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
722             "Invalid field offset!");
723      continue;
724    }
725
726    // Ignore unnamed bit-fields.
727    if (!FD->getDeclName())
728      continue;
729
730    // Don't inspect zero-length bitfields.
731    if (FD->getBitWidthValue(getContext()) == 0)
732      continue;
733
734    const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
735    llvm::Type *ElementTy = ST->getTypeAtIndex(RL->getLLVMFieldNo(FD));
736
737    // Unions have overlapping elements dictating their layout, but for
738    // non-unions we can verify that this section of the layout is the exact
739    // expected size.
740    if (D->isUnion()) {
741      // For unions we verify that the start is zero and the size
742      // is in-bounds. However, on BE systems, the offset may be non-zero, but
743      // the size + offset should match the storage size in that case as it
744      // "starts" at the back.
745      if (getDataLayout().isBigEndian())
746        assert(static_cast<unsigned>(Info.Offset + Info.Size) ==
747               Info.StorageSize &&
748               "Big endian union bitfield does not end at the back");
749      else
750        assert(Info.Offset == 0 &&
751               "Little endian union bitfield with a non-zero offset");
752      assert(Info.StorageSize <= SL->getSizeInBits() &&
753             "Union not large enough for bitfield storage");
754    } else {
755      assert(Info.StorageSize ==
756             getDataLayout().getTypeAllocSizeInBits(ElementTy) &&
757             "Storage size does not match the element type size");
758    }
759    assert(Info.Size > 0 && "Empty bitfield!");
760    assert(static_cast<unsigned>(Info.Offset) + Info.Size <= Info.StorageSize &&
761           "Bitfield outside of its allocated storage");
762  }
763#endif
764
765  return RL;
766}
767
768void CGRecordLayout::print(raw_ostream &OS) const {
769  OS << "<CGRecordLayout\n";
770  OS << "  LLVMType:" << *CompleteObjectType << "\n";
771  if (BaseSubobjectType)
772    OS << "  NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
773  OS << "  IsZeroInitializable:" << IsZeroInitializable << "\n";
774  OS << "  BitFields:[\n";
775
776  // Print bit-field infos in declaration order.
777  std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
778  for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
779         it = BitFields.begin(), ie = BitFields.end();
780       it != ie; ++it) {
781    const RecordDecl *RD = it->first->getParent();
782    unsigned Index = 0;
783    for (RecordDecl::field_iterator
784           it2 = RD->field_begin(); *it2 != it->first; ++it2)
785      ++Index;
786    BFIs.push_back(std::make_pair(Index, &it->second));
787  }
788  llvm::array_pod_sort(BFIs.begin(), BFIs.end());
789  for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
790    OS.indent(4);
791    BFIs[i].second->print(OS);
792    OS << "\n";
793  }
794
795  OS << "]>\n";
796}
797
798void CGRecordLayout::dump() const {
799  print(llvm::errs());
800}
801
802void CGBitFieldInfo::print(raw_ostream &OS) const {
803  OS << "<CGBitFieldInfo"
804     << " Offset:" << Offset
805     << " Size:" << Size
806     << " IsSigned:" << IsSigned
807     << " StorageSize:" << StorageSize
808     << " StorageAlignment:" << StorageAlignment << ">";
809}
810
811void CGBitFieldInfo::dump() const {
812  print(llvm::errs());
813}
814