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"
30 using namespace clang;
31 using namespace CodeGen;
32 
33 namespace {
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 sentinel 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.
74 struct CGRecordLowering {
75   // MemberInfo is a helper structure that contains information about a record
76   // member.  In additional to the standard member types, there exists a
77   // sentinel member type that ensures correct rounding.
78   struct MemberInfo {
79     CharUnits Offset;
80     enum InfoKind { VFPtr, VBPtr, Field, Base, VBase, 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, bool Packed);
97   // Short helper routines.
98   /// 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 
103   /// The Microsoft bitfield layout rule allocates discrete storage
104   /// units of the field's formal type and only combines adjacent
105   /// fields of the same formal type.  We want to emit a layout with
106   /// these discrete storage units instead of combining them into a
107   /// continuous run.
108   bool isDiscreteBitFieldABI() {
109     return Context.getTargetInfo().getCXXABI().isMicrosoft() ||
110            D->isMsStruct(Context);
111   }
112 
113   /// The Itanium base layout rule allows virtual bases to overlap
114   /// other bases, which complicates layout in specific ways.
115   ///
116   /// Note specifically that the ms_struct attribute doesn't change this.
117   bool isOverlappingVBaseABI() {
118     return !Context.getTargetInfo().getCXXABI().isMicrosoft();
119   }
120 
121   /// Wraps llvm::Type::getIntNTy with some implicit arguments.
122   llvm::Type *getIntNType(uint64_t NumBits) {
123     return llvm::Type::getIntNTy(Types.getLLVMContext(),
124                                  (unsigned)llvm::alignTo(NumBits, 8));
125   }
126   /// Gets an llvm type of size NumBytes and alignment 1.
127   llvm::Type *getByteArrayType(CharUnits NumBytes) {
128     assert(!NumBytes.isZero() && "Empty byte arrays aren't allowed.");
129     llvm::Type *Type = llvm::Type::getInt8Ty(Types.getLLVMContext());
130     return NumBytes == CharUnits::One() ? Type :
131         (llvm::Type *)llvm::ArrayType::get(Type, NumBytes.getQuantity());
132   }
133   /// Gets the storage type for a field decl and handles storage
134   /// for itanium bitfields that are smaller than their declared type.
135   llvm::Type *getStorageType(const FieldDecl *FD) {
136     llvm::Type *Type = Types.ConvertTypeForMem(FD->getType());
137     if (!FD->isBitField()) return Type;
138     if (isDiscreteBitFieldABI()) return Type;
139     return getIntNType(std::min(FD->getBitWidthValue(Context),
140                              (unsigned)Context.toBits(getSize(Type))));
141   }
142   /// Gets the llvm Basesubobject type from a CXXRecordDecl.
143   llvm::Type *getStorageType(const CXXRecordDecl *RD) {
144     return Types.getCGRecordLayout(RD).getBaseSubobjectLLVMType();
145   }
146   CharUnits bitsToCharUnits(uint64_t BitOffset) {
147     return Context.toCharUnitsFromBits(BitOffset);
148   }
149   CharUnits getSize(llvm::Type *Type) {
150     return CharUnits::fromQuantity(DataLayout.getTypeAllocSize(Type));
151   }
152   CharUnits getAlignment(llvm::Type *Type) {
153     return CharUnits::fromQuantity(DataLayout.getABITypeAlignment(Type));
154   }
155   bool isZeroInitializable(const FieldDecl *FD) {
156     return Types.isZeroInitializable(FD->getType());
157   }
158   bool isZeroInitializable(const RecordDecl *RD) {
159     return Types.isZeroInitializable(RD);
160   }
161   void appendPaddingBytes(CharUnits Size) {
162     if (!Size.isZero())
163       FieldTypes.push_back(getByteArrayType(Size));
164   }
165   uint64_t getFieldBitOffset(const FieldDecl *FD) {
166     return Layout.getFieldOffset(FD->getFieldIndex());
167   }
168   // Layout routines.
169   void setBitFieldInfo(const FieldDecl *FD, CharUnits StartOffset,
170                        llvm::Type *StorageType);
171   /// Lowers an ASTRecordLayout to a llvm type.
172   void lower(bool NonVirtualBaseType);
173   void lowerUnion();
174   void accumulateFields();
175   void accumulateBitFields(RecordDecl::field_iterator Field,
176                         RecordDecl::field_iterator FieldEnd);
177   void accumulateBases();
178   void accumulateVPtrs();
179   void accumulateVBases();
180   /// Recursively searches all of the bases to find out if a vbase is
181   /// not the primary vbase of some base class.
182   bool hasOwnStorage(const CXXRecordDecl *Decl, const CXXRecordDecl *Query);
183   void calculateZeroInit();
184   /// Lowers bitfield storage types to I8 arrays for bitfields with tail
185   /// padding that is or can potentially be used.
186   void clipTailPadding();
187   /// Determines if we need a packed llvm struct.
188   void determinePacked(bool NVBaseType);
189   /// Inserts padding everywhere it's needed.
190   void insertPadding();
191   /// Fills out the structures that are ultimately consumed.
192   void fillOutputFields();
193   // Input memoization fields.
194   CodeGenTypes &Types;
195   const ASTContext &Context;
196   const RecordDecl *D;
197   const CXXRecordDecl *RD;
198   const ASTRecordLayout &Layout;
199   const llvm::DataLayout &DataLayout;
200   // Helpful intermediate data-structures.
201   std::vector<MemberInfo> Members;
202   // Output fields, consumed by CodeGenTypes::ComputeRecordLayout.
203   SmallVector<llvm::Type *, 16> FieldTypes;
204   llvm::DenseMap<const FieldDecl *, unsigned> Fields;
205   llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
206   llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
207   llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases;
208   bool IsZeroInitializable : 1;
209   bool IsZeroInitializableAsBase : 1;
210   bool Packed : 1;
211 private:
212   CGRecordLowering(const CGRecordLowering &) = delete;
213   void operator =(const CGRecordLowering &) = delete;
214 };
215 } // namespace {
216 
217 CGRecordLowering::CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D,
218                                    bool Packed)
219     : Types(Types), Context(Types.getContext()), D(D),
220       RD(dyn_cast<CXXRecordDecl>(D)),
221       Layout(Types.getContext().getASTRecordLayout(D)),
222       DataLayout(Types.getDataLayout()), IsZeroInitializable(true),
223       IsZeroInitializableAsBase(true), Packed(Packed) {}
224 
225 void CGRecordLowering::setBitFieldInfo(
226     const FieldDecl *FD, CharUnits StartOffset, llvm::Type *StorageType) {
227   CGBitFieldInfo &Info = BitFields[FD->getCanonicalDecl()];
228   Info.IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
229   Info.Offset = (unsigned)(getFieldBitOffset(FD) - Context.toBits(StartOffset));
230   Info.Size = FD->getBitWidthValue(Context);
231   Info.StorageSize = (unsigned)DataLayout.getTypeAllocSizeInBits(StorageType);
232   Info.StorageOffset = StartOffset;
233   if (Info.Size > Info.StorageSize)
234     Info.Size = Info.StorageSize;
235   // Reverse the bit offsets for big endian machines. Because we represent
236   // a bitfield as a single large integer load, we can imagine the bits
237   // counting from the most-significant-bit instead of the
238   // least-significant-bit.
239   if (DataLayout.isBigEndian())
240     Info.Offset = Info.StorageSize - (Info.Offset + Info.Size);
241 }
242 
243 void CGRecordLowering::lower(bool NVBaseType) {
244   // The lowering process implemented in this function takes a variety of
245   // carefully ordered phases.
246   // 1) Store all members (fields and bases) in a list and sort them by offset.
247   // 2) Add a 1-byte capstone member at the Size of the structure.
248   // 3) Clip bitfield storages members if their tail padding is or might be
249   //    used by another field or base.  The clipping process uses the capstone
250   //    by treating it as another object that occurs after the record.
251   // 4) Determine if the llvm-struct requires packing.  It's important that this
252   //    phase occur after clipping, because clipping changes the llvm type.
253   //    This phase reads the offset of the capstone when determining packedness
254   //    and updates the alignment of the capstone to be equal of the alignment
255   //    of the record after doing so.
256   // 5) Insert padding everywhere it is needed.  This phase requires 'Packed' to
257   //    have been computed and needs to know the alignment of the record in
258   //    order to understand if explicit tail padding is needed.
259   // 6) Remove the capstone, we don't need it anymore.
260   // 7) Determine if this record can be zero-initialized.  This phase could have
261   //    been placed anywhere after phase 1.
262   // 8) Format the complete list of members in a way that can be consumed by
263   //    CodeGenTypes::ComputeRecordLayout.
264   CharUnits Size = NVBaseType ? Layout.getNonVirtualSize() : Layout.getSize();
265   if (D->isUnion())
266     return lowerUnion();
267   accumulateFields();
268   // RD implies C++.
269   if (RD) {
270     accumulateVPtrs();
271     accumulateBases();
272     if (Members.empty())
273       return appendPaddingBytes(Size);
274     if (!NVBaseType)
275       accumulateVBases();
276   }
277   std::stable_sort(Members.begin(), Members.end());
278   Members.push_back(StorageInfo(Size, getIntNType(8)));
279   clipTailPadding();
280   determinePacked(NVBaseType);
281   insertPadding();
282   Members.pop_back();
283   calculateZeroInit();
284   fillOutputFields();
285 }
286 
287 void CGRecordLowering::lowerUnion() {
288   CharUnits LayoutSize = Layout.getSize();
289   llvm::Type *StorageType = nullptr;
290   bool SeenNamedMember = false;
291   // Iterate through the fields setting bitFieldInfo and the Fields array. Also
292   // locate the "most appropriate" storage type.  The heuristic for finding the
293   // storage type isn't necessary, the first (non-0-length-bitfield) field's
294   // type would work fine and be simpler but would be different than what we've
295   // been doing and cause lit tests to change.
296   for (const auto *Field : D->fields()) {
297     if (Field->isBitField()) {
298       if (Field->isZeroLengthBitField(Context))
299         continue;
300       llvm::Type *FieldType = getStorageType(Field);
301       if (LayoutSize < getSize(FieldType))
302         FieldType = getByteArrayType(LayoutSize);
303       setBitFieldInfo(Field, CharUnits::Zero(), FieldType);
304     }
305     Fields[Field->getCanonicalDecl()] = 0;
306     llvm::Type *FieldType = getStorageType(Field);
307     // Compute zero-initializable status.
308     // This union might not be zero initialized: it may contain a pointer to
309     // data member which might have some exotic initialization sequence.
310     // If this is the case, then we aught not to try and come up with a "better"
311     // type, it might not be very easy to come up with a Constant which
312     // correctly initializes it.
313     if (!SeenNamedMember) {
314       SeenNamedMember = Field->getIdentifier();
315       if (!SeenNamedMember)
316         if (const auto *FieldRD =
317                 dyn_cast_or_null<RecordDecl>(Field->getType()->getAsTagDecl()))
318         SeenNamedMember = FieldRD->findFirstNamedDataMember();
319       if (SeenNamedMember && !isZeroInitializable(Field)) {
320         IsZeroInitializable = IsZeroInitializableAsBase = false;
321         StorageType = FieldType;
322       }
323     }
324     // Because our union isn't zero initializable, we won't be getting a better
325     // storage type.
326     if (!IsZeroInitializable)
327       continue;
328     // Conditionally update our storage type if we've got a new "better" one.
329     if (!StorageType ||
330         getAlignment(FieldType) >  getAlignment(StorageType) ||
331         (getAlignment(FieldType) == getAlignment(StorageType) &&
332         getSize(FieldType) > getSize(StorageType)))
333       StorageType = FieldType;
334   }
335   // If we have no storage type just pad to the appropriate size and return.
336   if (!StorageType)
337     return appendPaddingBytes(LayoutSize);
338   // If our storage size was bigger than our required size (can happen in the
339   // case of packed bitfields on Itanium) then just use an I8 array.
340   if (LayoutSize < getSize(StorageType))
341     StorageType = getByteArrayType(LayoutSize);
342   FieldTypes.push_back(StorageType);
343   appendPaddingBytes(LayoutSize - getSize(StorageType));
344   // Set packed if we need it.
345   if (LayoutSize % getAlignment(StorageType))
346     Packed = true;
347 }
348 
349 void CGRecordLowering::accumulateFields() {
350   for (RecordDecl::field_iterator Field = D->field_begin(),
351                                   FieldEnd = D->field_end();
352     Field != FieldEnd;)
353     if (Field->isBitField()) {
354       RecordDecl::field_iterator Start = Field;
355       // Iterate to gather the list of bitfields.
356       for (++Field; Field != FieldEnd && Field->isBitField(); ++Field);
357       accumulateBitFields(Start, Field);
358     } else {
359       Members.push_back(MemberInfo(
360           bitsToCharUnits(getFieldBitOffset(*Field)), MemberInfo::Field,
361           getStorageType(*Field), *Field));
362       ++Field;
363     }
364 }
365 
366 void
367 CGRecordLowering::accumulateBitFields(RecordDecl::field_iterator Field,
368                                       RecordDecl::field_iterator FieldEnd) {
369   // Run stores the first element of the current run of bitfields.  FieldEnd is
370   // used as a special value to note that we don't have a current run.  A
371   // bitfield run is a contiguous collection of bitfields that can be stored in
372   // the same storage block.  Zero-sized bitfields and bitfields that would
373   // cross an alignment boundary break a run and start a new one.
374   RecordDecl::field_iterator Run = FieldEnd;
375   // Tail is the offset of the first bit off the end of the current run.  It's
376   // used to determine if the ASTRecordLayout is treating these two bitfields as
377   // contiguous.  StartBitOffset is offset of the beginning of the Run.
378   uint64_t StartBitOffset, Tail = 0;
379   if (isDiscreteBitFieldABI()) {
380     for (; Field != FieldEnd; ++Field) {
381       uint64_t BitOffset = getFieldBitOffset(*Field);
382       // Zero-width bitfields end runs.
383       if (Field->isZeroLengthBitField(Context)) {
384         Run = FieldEnd;
385         continue;
386       }
387       llvm::Type *Type = Types.ConvertTypeForMem(Field->getType());
388       // If we don't have a run yet, or don't live within the previous run's
389       // allocated storage then we allocate some storage and start a new run.
390       if (Run == FieldEnd || BitOffset >= Tail) {
391         Run = Field;
392         StartBitOffset = BitOffset;
393         Tail = StartBitOffset + DataLayout.getTypeAllocSizeInBits(Type);
394         // Add the storage member to the record.  This must be added to the
395         // record before the bitfield members so that it gets laid out before
396         // the bitfields it contains get laid out.
397         Members.push_back(StorageInfo(bitsToCharUnits(StartBitOffset), Type));
398       }
399       // Bitfields get the offset of their storage but come afterward and remain
400       // there after a stable sort.
401       Members.push_back(MemberInfo(bitsToCharUnits(StartBitOffset),
402                                    MemberInfo::Field, nullptr, *Field));
403     }
404     return;
405   }
406 
407   // Check if OffsetInRecord is better as a single field run. When OffsetInRecord
408   // has legal integer width, and its bitfield offset is naturally aligned, it
409   // is better to make the bitfield a separate storage component so as it can be
410   // accessed directly with lower cost.
411   auto IsBetterAsSingleFieldRun = [&](uint64_t OffsetInRecord,
412                                       uint64_t StartBitOffset) {
413     if (!Types.getCodeGenOpts().FineGrainedBitfieldAccesses)
414       return false;
415     if (!DataLayout.isLegalInteger(OffsetInRecord))
416       return false;
417     // Make sure StartBitOffset is natually aligned if it is treated as an
418     // IType integer.
419      if (StartBitOffset %
420             Context.toBits(getAlignment(getIntNType(OffsetInRecord))) !=
421         0)
422       return false;
423     return true;
424   };
425 
426   // The start field is better as a single field run.
427   bool StartFieldAsSingleRun = false;
428   for (;;) {
429     // Check to see if we need to start a new run.
430     if (Run == FieldEnd) {
431       // If we're out of fields, return.
432       if (Field == FieldEnd)
433         break;
434       // Any non-zero-length bitfield can start a new run.
435       if (!Field->isZeroLengthBitField(Context)) {
436         Run = Field;
437         StartBitOffset = getFieldBitOffset(*Field);
438         Tail = StartBitOffset + Field->getBitWidthValue(Context);
439         StartFieldAsSingleRun = IsBetterAsSingleFieldRun(Tail - StartBitOffset,
440                                                          StartBitOffset);
441       }
442       ++Field;
443       continue;
444     }
445 
446     // If the start field of a new run is better as a single run, or
447     // if current field (or consecutive fields) is better as a single run, or
448     // if current field has zero width bitfield and either
449     // UseZeroLengthBitfieldAlignment or UseBitFieldTypeAlignment is set to
450     // true, or
451     // if the offset of current field is inconsistent with the offset of
452     // previous field plus its offset,
453     // skip the block below and go ahead to emit the storage.
454     // Otherwise, try to add bitfields to the run.
455     if (!StartFieldAsSingleRun && Field != FieldEnd &&
456         !IsBetterAsSingleFieldRun(Tail - StartBitOffset, StartBitOffset) &&
457         (!Field->isZeroLengthBitField(Context) ||
458          (!Context.getTargetInfo().useZeroLengthBitfieldAlignment() &&
459           !Context.getTargetInfo().useBitFieldTypeAlignment())) &&
460         Tail == getFieldBitOffset(*Field)) {
461       Tail += Field->getBitWidthValue(Context);
462       ++Field;
463       continue;
464     }
465 
466     // We've hit a break-point in the run and need to emit a storage field.
467     llvm::Type *Type = getIntNType(Tail - StartBitOffset);
468     // Add the storage member to the record and set the bitfield info for all of
469     // the bitfields in the run.  Bitfields get the offset of their storage but
470     // come afterward and remain there after a stable sort.
471     Members.push_back(StorageInfo(bitsToCharUnits(StartBitOffset), Type));
472     for (; Run != Field; ++Run)
473       Members.push_back(MemberInfo(bitsToCharUnits(StartBitOffset),
474                                    MemberInfo::Field, nullptr, *Run));
475     Run = FieldEnd;
476     StartFieldAsSingleRun = false;
477   }
478 }
479 
480 void CGRecordLowering::accumulateBases() {
481   // If we've got a primary virtual base, we need to add it with the bases.
482   if (Layout.isPrimaryBaseVirtual()) {
483     const CXXRecordDecl *BaseDecl = Layout.getPrimaryBase();
484     Members.push_back(MemberInfo(CharUnits::Zero(), MemberInfo::Base,
485                                  getStorageType(BaseDecl), BaseDecl));
486   }
487   // Accumulate the non-virtual bases.
488   for (const auto &Base : RD->bases()) {
489     if (Base.isVirtual())
490       continue;
491 
492     // Bases can be zero-sized even if not technically empty if they
493     // contain only a trailing array member.
494     const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
495     if (!BaseDecl->isEmpty() &&
496         !Context.getASTRecordLayout(BaseDecl).getNonVirtualSize().isZero())
497       Members.push_back(MemberInfo(Layout.getBaseClassOffset(BaseDecl),
498           MemberInfo::Base, getStorageType(BaseDecl), BaseDecl));
499   }
500 }
501 
502 void CGRecordLowering::accumulateVPtrs() {
503   if (Layout.hasOwnVFPtr())
504     Members.push_back(MemberInfo(CharUnits::Zero(), MemberInfo::VFPtr,
505         llvm::FunctionType::get(getIntNType(32), /*isVarArg=*/true)->
506             getPointerTo()->getPointerTo()));
507   if (Layout.hasOwnVBPtr())
508     Members.push_back(MemberInfo(Layout.getVBPtrOffset(), MemberInfo::VBPtr,
509         llvm::Type::getInt32PtrTy(Types.getLLVMContext())));
510 }
511 
512 void CGRecordLowering::accumulateVBases() {
513   CharUnits ScissorOffset = Layout.getNonVirtualSize();
514   // In the itanium ABI, it's possible to place a vbase at a dsize that is
515   // smaller than the nvsize.  Here we check to see if such a base is placed
516   // before the nvsize and set the scissor offset to that, instead of the
517   // nvsize.
518   if (isOverlappingVBaseABI())
519     for (const auto &Base : RD->vbases()) {
520       const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
521       if (BaseDecl->isEmpty())
522         continue;
523       // If the vbase is a primary virtual base of some base, then it doesn't
524       // get its own storage location but instead lives inside of that base.
525       if (Context.isNearlyEmpty(BaseDecl) && !hasOwnStorage(RD, BaseDecl))
526         continue;
527       ScissorOffset = std::min(ScissorOffset,
528                                Layout.getVBaseClassOffset(BaseDecl));
529     }
530   Members.push_back(MemberInfo(ScissorOffset, MemberInfo::Scissor, nullptr,
531                                RD));
532   for (const auto &Base : RD->vbases()) {
533     const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
534     if (BaseDecl->isEmpty())
535       continue;
536     CharUnits Offset = Layout.getVBaseClassOffset(BaseDecl);
537     // If the vbase is a primary virtual base of some base, then it doesn't
538     // get its own storage location but instead lives inside of that base.
539     if (isOverlappingVBaseABI() &&
540         Context.isNearlyEmpty(BaseDecl) &&
541         !hasOwnStorage(RD, BaseDecl)) {
542       Members.push_back(MemberInfo(Offset, MemberInfo::VBase, nullptr,
543                                    BaseDecl));
544       continue;
545     }
546     // If we've got a vtordisp, add it as a storage type.
547     if (Layout.getVBaseOffsetsMap().find(BaseDecl)->second.hasVtorDisp())
548       Members.push_back(StorageInfo(Offset - CharUnits::fromQuantity(4),
549                                     getIntNType(32)));
550     Members.push_back(MemberInfo(Offset, MemberInfo::VBase,
551                                  getStorageType(BaseDecl), BaseDecl));
552   }
553 }
554 
555 bool CGRecordLowering::hasOwnStorage(const CXXRecordDecl *Decl,
556                                      const CXXRecordDecl *Query) {
557   const ASTRecordLayout &DeclLayout = Context.getASTRecordLayout(Decl);
558   if (DeclLayout.isPrimaryBaseVirtual() && DeclLayout.getPrimaryBase() == Query)
559     return false;
560   for (const auto &Base : Decl->bases())
561     if (!hasOwnStorage(Base.getType()->getAsCXXRecordDecl(), Query))
562       return false;
563   return true;
564 }
565 
566 void CGRecordLowering::calculateZeroInit() {
567   for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
568                                                MemberEnd = Members.end();
569        IsZeroInitializableAsBase && Member != MemberEnd; ++Member) {
570     if (Member->Kind == MemberInfo::Field) {
571       if (!Member->FD || isZeroInitializable(Member->FD))
572         continue;
573       IsZeroInitializable = IsZeroInitializableAsBase = false;
574     } else if (Member->Kind == MemberInfo::Base ||
575                Member->Kind == MemberInfo::VBase) {
576       if (isZeroInitializable(Member->RD))
577         continue;
578       IsZeroInitializable = false;
579       if (Member->Kind == MemberInfo::Base)
580         IsZeroInitializableAsBase = false;
581     }
582   }
583 }
584 
585 void CGRecordLowering::clipTailPadding() {
586   std::vector<MemberInfo>::iterator Prior = Members.begin();
587   CharUnits Tail = getSize(Prior->Data);
588   for (std::vector<MemberInfo>::iterator Member = Prior + 1,
589                                          MemberEnd = Members.end();
590        Member != MemberEnd; ++Member) {
591     // Only members with data and the scissor can cut into tail padding.
592     if (!Member->Data && Member->Kind != MemberInfo::Scissor)
593       continue;
594     if (Member->Offset < Tail) {
595       assert(Prior->Kind == MemberInfo::Field && !Prior->FD &&
596              "Only storage fields have tail padding!");
597       Prior->Data = getByteArrayType(bitsToCharUnits(llvm::alignTo(
598           cast<llvm::IntegerType>(Prior->Data)->getIntegerBitWidth(), 8)));
599     }
600     if (Member->Data)
601       Prior = Member;
602     Tail = Prior->Offset + getSize(Prior->Data);
603   }
604 }
605 
606 void CGRecordLowering::determinePacked(bool NVBaseType) {
607   if (Packed)
608     return;
609   CharUnits Alignment = CharUnits::One();
610   CharUnits NVAlignment = CharUnits::One();
611   CharUnits NVSize =
612       !NVBaseType && RD ? Layout.getNonVirtualSize() : CharUnits::Zero();
613   for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
614                                                MemberEnd = Members.end();
615        Member != MemberEnd; ++Member) {
616     if (!Member->Data)
617       continue;
618     // If any member falls at an offset that it not a multiple of its alignment,
619     // then the entire record must be packed.
620     if (Member->Offset % getAlignment(Member->Data))
621       Packed = true;
622     if (Member->Offset < NVSize)
623       NVAlignment = std::max(NVAlignment, getAlignment(Member->Data));
624     Alignment = std::max(Alignment, getAlignment(Member->Data));
625   }
626   // If the size of the record (the capstone's offset) is not a multiple of the
627   // record's alignment, it must be packed.
628   if (Members.back().Offset % Alignment)
629     Packed = true;
630   // If the non-virtual sub-object is not a multiple of the non-virtual
631   // sub-object's alignment, it must be packed.  We cannot have a packed
632   // non-virtual sub-object and an unpacked complete object or vise versa.
633   if (NVSize % NVAlignment)
634     Packed = true;
635   // Update the alignment of the sentinel.
636   if (!Packed)
637     Members.back().Data = getIntNType(Context.toBits(Alignment));
638 }
639 
640 void CGRecordLowering::insertPadding() {
641   std::vector<std::pair<CharUnits, CharUnits> > Padding;
642   CharUnits Size = CharUnits::Zero();
643   for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
644                                                MemberEnd = Members.end();
645        Member != MemberEnd; ++Member) {
646     if (!Member->Data)
647       continue;
648     CharUnits Offset = Member->Offset;
649     assert(Offset >= Size);
650     // Insert padding if we need to.
651     if (Offset !=
652         Size.alignTo(Packed ? CharUnits::One() : getAlignment(Member->Data)))
653       Padding.push_back(std::make_pair(Size, Offset - Size));
654     Size = Offset + getSize(Member->Data);
655   }
656   if (Padding.empty())
657     return;
658   // Add the padding to the Members list and sort it.
659   for (std::vector<std::pair<CharUnits, CharUnits> >::const_iterator
660         Pad = Padding.begin(), PadEnd = Padding.end();
661         Pad != PadEnd; ++Pad)
662     Members.push_back(StorageInfo(Pad->first, getByteArrayType(Pad->second)));
663   std::stable_sort(Members.begin(), Members.end());
664 }
665 
666 void CGRecordLowering::fillOutputFields() {
667   for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
668                                                MemberEnd = Members.end();
669        Member != MemberEnd; ++Member) {
670     if (Member->Data)
671       FieldTypes.push_back(Member->Data);
672     if (Member->Kind == MemberInfo::Field) {
673       if (Member->FD)
674         Fields[Member->FD->getCanonicalDecl()] = FieldTypes.size() - 1;
675       // A field without storage must be a bitfield.
676       if (!Member->Data)
677         setBitFieldInfo(Member->FD, Member->Offset, FieldTypes.back());
678     } else if (Member->Kind == MemberInfo::Base)
679       NonVirtualBases[Member->RD] = FieldTypes.size() - 1;
680     else if (Member->Kind == MemberInfo::VBase)
681       VirtualBases[Member->RD] = FieldTypes.size() - 1;
682   }
683 }
684 
685 CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
686                                         const FieldDecl *FD,
687                                         uint64_t Offset, uint64_t Size,
688                                         uint64_t StorageSize,
689                                         CharUnits StorageOffset) {
690   // This function is vestigial from CGRecordLayoutBuilder days but is still
691   // used in GCObjCRuntime.cpp.  That usage has a "fixme" attached to it that
692   // when addressed will allow for the removal of this function.
693   llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType());
694   CharUnits TypeSizeInBytes =
695     CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(Ty));
696   uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
697 
698   bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
699 
700   if (Size > TypeSizeInBits) {
701     // We have a wide bit-field. The extra bits are only used for padding, so
702     // if we have a bitfield of type T, with size N:
703     //
704     // T t : N;
705     //
706     // We can just assume that it's:
707     //
708     // T t : sizeof(T);
709     //
710     Size = TypeSizeInBits;
711   }
712 
713   // Reverse the bit offsets for big endian machines. Because we represent
714   // a bitfield as a single large integer load, we can imagine the bits
715   // counting from the most-significant-bit instead of the
716   // least-significant-bit.
717   if (Types.getDataLayout().isBigEndian()) {
718     Offset = StorageSize - (Offset + Size);
719   }
720 
721   return CGBitFieldInfo(Offset, Size, IsSigned, StorageSize, StorageOffset);
722 }
723 
724 CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D,
725                                                   llvm::StructType *Ty) {
726   CGRecordLowering Builder(*this, D, /*Packed=*/false);
727 
728   Builder.lower(/*NonVirtualBaseType=*/false);
729 
730   // If we're in C++, compute the base subobject type.
731   llvm::StructType *BaseTy = nullptr;
732   if (isa<CXXRecordDecl>(D) && !D->isUnion() && !D->hasAttr<FinalAttr>()) {
733     BaseTy = Ty;
734     if (Builder.Layout.getNonVirtualSize() != Builder.Layout.getSize()) {
735       CGRecordLowering BaseBuilder(*this, D, /*Packed=*/Builder.Packed);
736       BaseBuilder.lower(/*NonVirtualBaseType=*/true);
737       BaseTy = llvm::StructType::create(
738           getLLVMContext(), BaseBuilder.FieldTypes, "", BaseBuilder.Packed);
739       addRecordTypeName(D, BaseTy, ".base");
740       // BaseTy and Ty must agree on their packedness for getLLVMFieldNo to work
741       // on both of them with the same index.
742       assert(Builder.Packed == BaseBuilder.Packed &&
743              "Non-virtual and complete types must agree on packedness");
744     }
745   }
746 
747   // Fill in the struct *after* computing the base type.  Filling in the body
748   // signifies that the type is no longer opaque and record layout is complete,
749   // but we may need to recursively layout D while laying D out as a base type.
750   Ty->setBody(Builder.FieldTypes, Builder.Packed);
751 
752   CGRecordLayout *RL =
753     new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
754                         Builder.IsZeroInitializableAsBase);
755 
756   RL->NonVirtualBases.swap(Builder.NonVirtualBases);
757   RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
758 
759   // Add all the field numbers.
760   RL->FieldInfo.swap(Builder.Fields);
761 
762   // Add bitfield info.
763   RL->BitFields.swap(Builder.BitFields);
764 
765   // Dump the layout, if requested.
766   if (getContext().getLangOpts().DumpRecordLayouts) {
767     llvm::outs() << "\n*** Dumping IRgen Record Layout\n";
768     llvm::outs() << "Record: ";
769     D->dump(llvm::outs());
770     llvm::outs() << "\nLayout: ";
771     RL->print(llvm::outs());
772   }
773 
774 #ifndef NDEBUG
775   // Verify that the computed LLVM struct size matches the AST layout size.
776   const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
777 
778   uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
779   assert(TypeSizeInBits == getDataLayout().getTypeAllocSizeInBits(Ty) &&
780          "Type size mismatch!");
781 
782   if (BaseTy) {
783     CharUnits NonVirtualSize  = Layout.getNonVirtualSize();
784 
785     uint64_t AlignedNonVirtualTypeSizeInBits =
786       getContext().toBits(NonVirtualSize);
787 
788     assert(AlignedNonVirtualTypeSizeInBits ==
789            getDataLayout().getTypeAllocSizeInBits(BaseTy) &&
790            "Type size mismatch!");
791   }
792 
793   // Verify that the LLVM and AST field offsets agree.
794   llvm::StructType *ST = RL->getLLVMType();
795   const llvm::StructLayout *SL = getDataLayout().getStructLayout(ST);
796 
797   const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
798   RecordDecl::field_iterator it = D->field_begin();
799   for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
800     const FieldDecl *FD = *it;
801 
802     // For non-bit-fields, just check that the LLVM struct offset matches the
803     // AST offset.
804     if (!FD->isBitField()) {
805       unsigned FieldNo = RL->getLLVMFieldNo(FD);
806       assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
807              "Invalid field offset!");
808       continue;
809     }
810 
811     // Ignore unnamed bit-fields.
812     if (!FD->getDeclName())
813       continue;
814 
815     // Don't inspect zero-length bitfields.
816     if (FD->isZeroLengthBitField(getContext()))
817       continue;
818 
819     const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
820     llvm::Type *ElementTy = ST->getTypeAtIndex(RL->getLLVMFieldNo(FD));
821 
822     // Unions have overlapping elements dictating their layout, but for
823     // non-unions we can verify that this section of the layout is the exact
824     // expected size.
825     if (D->isUnion()) {
826       // For unions we verify that the start is zero and the size
827       // is in-bounds. However, on BE systems, the offset may be non-zero, but
828       // the size + offset should match the storage size in that case as it
829       // "starts" at the back.
830       if (getDataLayout().isBigEndian())
831         assert(static_cast<unsigned>(Info.Offset + Info.Size) ==
832                Info.StorageSize &&
833                "Big endian union bitfield does not end at the back");
834       else
835         assert(Info.Offset == 0 &&
836                "Little endian union bitfield with a non-zero offset");
837       assert(Info.StorageSize <= SL->getSizeInBits() &&
838              "Union not large enough for bitfield storage");
839     } else {
840       assert(Info.StorageSize ==
841              getDataLayout().getTypeAllocSizeInBits(ElementTy) &&
842              "Storage size does not match the element type size");
843     }
844     assert(Info.Size > 0 && "Empty bitfield!");
845     assert(static_cast<unsigned>(Info.Offset) + Info.Size <= Info.StorageSize &&
846            "Bitfield outside of its allocated storage");
847   }
848 #endif
849 
850   return RL;
851 }
852 
853 void CGRecordLayout::print(raw_ostream &OS) const {
854   OS << "<CGRecordLayout\n";
855   OS << "  LLVMType:" << *CompleteObjectType << "\n";
856   if (BaseSubobjectType)
857     OS << "  NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
858   OS << "  IsZeroInitializable:" << IsZeroInitializable << "\n";
859   OS << "  BitFields:[\n";
860 
861   // Print bit-field infos in declaration order.
862   std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
863   for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
864          it = BitFields.begin(), ie = BitFields.end();
865        it != ie; ++it) {
866     const RecordDecl *RD = it->first->getParent();
867     unsigned Index = 0;
868     for (RecordDecl::field_iterator
869            it2 = RD->field_begin(); *it2 != it->first; ++it2)
870       ++Index;
871     BFIs.push_back(std::make_pair(Index, &it->second));
872   }
873   llvm::array_pod_sort(BFIs.begin(), BFIs.end());
874   for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
875     OS.indent(4);
876     BFIs[i].second->print(OS);
877     OS << "\n";
878   }
879 
880   OS << "]>\n";
881 }
882 
883 LLVM_DUMP_METHOD void CGRecordLayout::dump() const {
884   print(llvm::errs());
885 }
886 
887 void CGBitFieldInfo::print(raw_ostream &OS) const {
888   OS << "<CGBitFieldInfo"
889      << " Offset:" << Offset
890      << " Size:" << Size
891      << " IsSigned:" << IsSigned
892      << " StorageSize:" << StorageSize
893      << " StorageOffset:" << StorageOffset.getQuantity() << ">";
894 }
895 
896 LLVM_DUMP_METHOD void CGBitFieldInfo::dump() const {
897   print(llvm::errs());
898 }
899