xref: /freebsd/contrib/llvm-project/clang/lib/CodeGen/CGRecordLayoutBuilder.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder  ----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Builder implementation for CGRecordLayout objects.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "ABIInfoImpl.h"
14 #include "CGCXXABI.h"
15 #include "CGRecordLayout.h"
16 #include "CodeGenTypes.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/Attr.h"
19 #include "clang/AST/CXXInheritance.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/Expr.h"
22 #include "clang/AST/RecordLayout.h"
23 #include "clang/Basic/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 [i8 x
48 ///   3] instead of i24. This is computed when accumulating bitfields in
49 ///   accumulateBitfields.
50 ///   C++ examples that require clipping:
51 ///   struct { int a : 24; char b; }; // a must be clipped, b goes at offset 3
52 ///   struct A { int a : 24; ~A(); }; // a must be clipped because:
53 ///   struct B : A { char b; }; // b goes at offset 3
54 /// * The allocation of bitfield access units is described in more detail in
55 ///   CGRecordLowering::accumulateBitFields.
56 /// * Clang ignores 0 sized bitfields and 0 sized bases but *not* zero sized
57 ///   fields.  The existing asserts suggest that LLVM assumes that *every* field
58 ///   has an underlying storage type.  Therefore empty structures containing
59 ///   zero sized subobjects such as empty records or zero sized arrays still get
60 ///   a zero sized (empty struct) storage type.
61 /// * Clang reads the complete type rather than the base type when generating
62 ///   code to access fields.  Bitfields in tail position with tail padding may
63 ///   be clipped in the base class but not the complete class (we may discover
64 ///   that the tail padding is not used in the complete class.) However,
65 ///   because LLVM reads from the complete type it can generate incorrect code
66 ///   if we do not clip the tail padding off of the bitfield in the complete
67 ///   layout.
68 /// * Itanium allows nearly empty primary virtual bases.  These bases don't get
69 ///   get their own storage because they're laid out as part of another base
70 ///   or at the beginning of the structure.  Determining if a VBase actually
71 ///   gets storage awkwardly involves a walk of all bases.
72 /// * VFPtrs and VBPtrs do *not* make a record NotZeroInitializable.
73 struct CGRecordLowering {
74   // MemberInfo is a helper structure that contains information about a record
75   // member.  In additional to the standard member types, there exists a
76   // sentinel member type that ensures correct rounding.
77   struct MemberInfo {
78     CharUnits Offset;
79     enum InfoKind { VFPtr, VBPtr, Field, Base, VBase } Kind;
80     llvm::Type *Data;
81     union {
82       const FieldDecl *FD;
83       const CXXRecordDecl *RD;
84     };
MemberInfo__anon34b7e94c0111::CGRecordLowering::MemberInfo85     MemberInfo(CharUnits Offset, InfoKind Kind, llvm::Type *Data,
86                const FieldDecl *FD = nullptr)
87       : Offset(Offset), Kind(Kind), Data(Data), FD(FD) {}
MemberInfo__anon34b7e94c0111::CGRecordLowering::MemberInfo88     MemberInfo(CharUnits Offset, InfoKind Kind, llvm::Type *Data,
89                const CXXRecordDecl *RD)
90       : Offset(Offset), Kind(Kind), Data(Data), RD(RD) {}
91     // MemberInfos are sorted so we define a < operator.
operator <__anon34b7e94c0111::CGRecordLowering::MemberInfo92     bool operator <(const MemberInfo& a) const { return Offset < a.Offset; }
93   };
94   // The constructor.
95   CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D, bool Packed);
96   // Short helper routines.
97   /// Constructs a MemberInfo instance from an offset and llvm::Type *.
StorageInfo__anon34b7e94c0111::CGRecordLowering98   static MemberInfo StorageInfo(CharUnits Offset, llvm::Type *Data) {
99     return MemberInfo(Offset, MemberInfo::Field, Data);
100   }
101 
102   /// The Microsoft bitfield layout rule allocates discrete storage
103   /// units of the field's formal type and only combines adjacent
104   /// fields of the same formal type.  We want to emit a layout with
105   /// these discrete storage units instead of combining them into a
106   /// continuous run.
isDiscreteBitFieldABI__anon34b7e94c0111::CGRecordLowering107   bool isDiscreteBitFieldABI() const {
108     return Context.getTargetInfo().getCXXABI().isMicrosoft() ||
109            D->isMsStruct(Context);
110   }
111 
112   /// Helper function to check if we are targeting AAPCS.
isAAPCS__anon34b7e94c0111::CGRecordLowering113   bool isAAPCS() const {
114     return Context.getTargetInfo().getABI().starts_with("aapcs");
115   }
116 
117   /// Helper function to check if the target machine is BigEndian.
isBE__anon34b7e94c0111::CGRecordLowering118   bool isBE() const { return Context.getTargetInfo().isBigEndian(); }
119 
120   /// The Itanium base layout rule allows virtual bases to overlap
121   /// other bases, which complicates layout in specific ways.
122   ///
123   /// Note specifically that the ms_struct attribute doesn't change this.
isOverlappingVBaseABI__anon34b7e94c0111::CGRecordLowering124   bool isOverlappingVBaseABI() const {
125     return !Context.getTargetInfo().getCXXABI().isMicrosoft();
126   }
127 
128   /// Wraps llvm::Type::getIntNTy with some implicit arguments.
getIntNType__anon34b7e94c0111::CGRecordLowering129   llvm::Type *getIntNType(uint64_t NumBits) const {
130     unsigned AlignedBits = llvm::alignTo(NumBits, Context.getCharWidth());
131     return llvm::Type::getIntNTy(Types.getLLVMContext(), AlignedBits);
132   }
133   /// Get the LLVM type sized as one character unit.
getCharType__anon34b7e94c0111::CGRecordLowering134   llvm::Type *getCharType() const {
135     return llvm::Type::getIntNTy(Types.getLLVMContext(),
136                                  Context.getCharWidth());
137   }
138   /// Gets an llvm type of size NumChars and alignment 1.
getByteArrayType__anon34b7e94c0111::CGRecordLowering139   llvm::Type *getByteArrayType(CharUnits NumChars) const {
140     assert(!NumChars.isZero() && "Empty byte arrays aren't allowed.");
141     llvm::Type *Type = getCharType();
142     return NumChars == CharUnits::One() ? Type :
143         (llvm::Type *)llvm::ArrayType::get(Type, NumChars.getQuantity());
144   }
145   /// Gets the storage type for a field decl and handles storage
146   /// for itanium bitfields that are smaller than their declared type.
getStorageType__anon34b7e94c0111::CGRecordLowering147   llvm::Type *getStorageType(const FieldDecl *FD) const {
148     llvm::Type *Type = Types.ConvertTypeForMem(FD->getType());
149     if (!FD->isBitField()) return Type;
150     if (isDiscreteBitFieldABI()) return Type;
151     return getIntNType(std::min(FD->getBitWidthValue(Context),
152                              (unsigned)Context.toBits(getSize(Type))));
153   }
154   /// Gets the llvm Basesubobject type from a CXXRecordDecl.
getStorageType__anon34b7e94c0111::CGRecordLowering155   llvm::Type *getStorageType(const CXXRecordDecl *RD) const {
156     return Types.getCGRecordLayout(RD).getBaseSubobjectLLVMType();
157   }
bitsToCharUnits__anon34b7e94c0111::CGRecordLowering158   CharUnits bitsToCharUnits(uint64_t BitOffset) const {
159     return Context.toCharUnitsFromBits(BitOffset);
160   }
getSize__anon34b7e94c0111::CGRecordLowering161   CharUnits getSize(llvm::Type *Type) const {
162     return CharUnits::fromQuantity(DataLayout.getTypeAllocSize(Type));
163   }
getAlignment__anon34b7e94c0111::CGRecordLowering164   CharUnits getAlignment(llvm::Type *Type) const {
165     return CharUnits::fromQuantity(DataLayout.getABITypeAlign(Type));
166   }
isZeroInitializable__anon34b7e94c0111::CGRecordLowering167   bool isZeroInitializable(const FieldDecl *FD) const {
168     return Types.isZeroInitializable(FD->getType());
169   }
isZeroInitializable__anon34b7e94c0111::CGRecordLowering170   bool isZeroInitializable(const RecordDecl *RD) const {
171     return Types.isZeroInitializable(RD);
172   }
appendPaddingBytes__anon34b7e94c0111::CGRecordLowering173   void appendPaddingBytes(CharUnits Size) {
174     if (!Size.isZero())
175       FieldTypes.push_back(getByteArrayType(Size));
176   }
getFieldBitOffset__anon34b7e94c0111::CGRecordLowering177   uint64_t getFieldBitOffset(const FieldDecl *FD) const {
178     return Layout.getFieldOffset(FD->getFieldIndex());
179   }
180   // Layout routines.
181   void setBitFieldInfo(const FieldDecl *FD, CharUnits StartOffset,
182                        llvm::Type *StorageType);
183   /// Lowers an ASTRecordLayout to a llvm type.
184   void lower(bool NonVirtualBaseType);
185   void lowerUnion(bool isNoUniqueAddress);
186   void accumulateFields(bool isNonVirtualBaseType);
187   RecordDecl::field_iterator
188   accumulateBitFields(bool isNonVirtualBaseType,
189                       RecordDecl::field_iterator Field,
190                       RecordDecl::field_iterator FieldEnd);
191   void computeVolatileBitfields();
192   void accumulateBases();
193   void accumulateVPtrs();
194   void accumulateVBases();
195   /// Recursively searches all of the bases to find out if a vbase is
196   /// not the primary vbase of some base class.
197   bool hasOwnStorage(const CXXRecordDecl *Decl,
198                      const CXXRecordDecl *Query) const;
199   void calculateZeroInit();
200   CharUnits calculateTailClippingOffset(bool isNonVirtualBaseType) const;
201   void checkBitfieldClipping(bool isNonVirtualBaseType) const;
202   /// Determines if we need a packed llvm struct.
203   void determinePacked(bool NVBaseType);
204   /// Inserts padding everywhere it's needed.
205   void insertPadding();
206   /// Fills out the structures that are ultimately consumed.
207   void fillOutputFields();
208   // Input memoization fields.
209   CodeGenTypes &Types;
210   const ASTContext &Context;
211   const RecordDecl *D;
212   const CXXRecordDecl *RD;
213   const ASTRecordLayout &Layout;
214   const llvm::DataLayout &DataLayout;
215   // Helpful intermediate data-structures.
216   std::vector<MemberInfo> Members;
217   // Output fields, consumed by CodeGenTypes::ComputeRecordLayout.
218   SmallVector<llvm::Type *, 16> FieldTypes;
219   llvm::DenseMap<const FieldDecl *, unsigned> Fields;
220   llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
221   llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
222   llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases;
223   bool IsZeroInitializable : 1;
224   bool IsZeroInitializableAsBase : 1;
225   bool Packed : 1;
226 private:
227   CGRecordLowering(const CGRecordLowering &) = delete;
228   void operator =(const CGRecordLowering &) = delete;
229 };
230 } // namespace {
231 
CGRecordLowering(CodeGenTypes & Types,const RecordDecl * D,bool Packed)232 CGRecordLowering::CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D,
233                                    bool Packed)
234     : Types(Types), Context(Types.getContext()), D(D),
235       RD(dyn_cast<CXXRecordDecl>(D)),
236       Layout(Types.getContext().getASTRecordLayout(D)),
237       DataLayout(Types.getDataLayout()), IsZeroInitializable(true),
238       IsZeroInitializableAsBase(true), Packed(Packed) {}
239 
setBitFieldInfo(const FieldDecl * FD,CharUnits StartOffset,llvm::Type * StorageType)240 void CGRecordLowering::setBitFieldInfo(
241     const FieldDecl *FD, CharUnits StartOffset, llvm::Type *StorageType) {
242   CGBitFieldInfo &Info = BitFields[FD->getCanonicalDecl()];
243   Info.IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
244   Info.Offset = (unsigned)(getFieldBitOffset(FD) - Context.toBits(StartOffset));
245   Info.Size = FD->getBitWidthValue(Context);
246   Info.StorageSize = (unsigned)DataLayout.getTypeAllocSizeInBits(StorageType);
247   Info.StorageOffset = StartOffset;
248   if (Info.Size > Info.StorageSize)
249     Info.Size = Info.StorageSize;
250   // Reverse the bit offsets for big endian machines. Because we represent
251   // a bitfield as a single large integer load, we can imagine the bits
252   // counting from the most-significant-bit instead of the
253   // least-significant-bit.
254   if (DataLayout.isBigEndian())
255     Info.Offset = Info.StorageSize - (Info.Offset + Info.Size);
256 
257   Info.VolatileStorageSize = 0;
258   Info.VolatileOffset = 0;
259   Info.VolatileStorageOffset = CharUnits::Zero();
260 }
261 
lower(bool NVBaseType)262 void CGRecordLowering::lower(bool NVBaseType) {
263   // The lowering process implemented in this function takes a variety of
264   // carefully ordered phases.
265   // 1) Store all members (fields and bases) in a list and sort them by offset.
266   // 2) Add a 1-byte capstone member at the Size of the structure.
267   // 3) Clip bitfield storages members if their tail padding is or might be
268   //    used by another field or base.  The clipping process uses the capstone
269   //    by treating it as another object that occurs after the record.
270   // 4) Determine if the llvm-struct requires packing.  It's important that this
271   //    phase occur after clipping, because clipping changes the llvm type.
272   //    This phase reads the offset of the capstone when determining packedness
273   //    and updates the alignment of the capstone to be equal of the alignment
274   //    of the record after doing so.
275   // 5) Insert padding everywhere it is needed.  This phase requires 'Packed' to
276   //    have been computed and needs to know the alignment of the record in
277   //    order to understand if explicit tail padding is needed.
278   // 6) Remove the capstone, we don't need it anymore.
279   // 7) Determine if this record can be zero-initialized.  This phase could have
280   //    been placed anywhere after phase 1.
281   // 8) Format the complete list of members in a way that can be consumed by
282   //    CodeGenTypes::ComputeRecordLayout.
283   CharUnits Size = NVBaseType ? Layout.getNonVirtualSize() : Layout.getSize();
284   if (D->isUnion()) {
285     lowerUnion(NVBaseType);
286     computeVolatileBitfields();
287     return;
288   }
289   accumulateFields(NVBaseType);
290   // RD implies C++.
291   if (RD) {
292     accumulateVPtrs();
293     accumulateBases();
294     if (Members.empty()) {
295       appendPaddingBytes(Size);
296       computeVolatileBitfields();
297       return;
298     }
299     if (!NVBaseType)
300       accumulateVBases();
301   }
302   llvm::stable_sort(Members);
303   checkBitfieldClipping(NVBaseType);
304   Members.push_back(StorageInfo(Size, getIntNType(8)));
305   determinePacked(NVBaseType);
306   insertPadding();
307   Members.pop_back();
308   calculateZeroInit();
309   fillOutputFields();
310   computeVolatileBitfields();
311 }
312 
lowerUnion(bool isNoUniqueAddress)313 void CGRecordLowering::lowerUnion(bool isNoUniqueAddress) {
314   CharUnits LayoutSize =
315       isNoUniqueAddress ? Layout.getDataSize() : Layout.getSize();
316   llvm::Type *StorageType = nullptr;
317   bool SeenNamedMember = false;
318   // Iterate through the fields setting bitFieldInfo and the Fields array. Also
319   // locate the "most appropriate" storage type.  The heuristic for finding the
320   // storage type isn't necessary, the first (non-0-length-bitfield) field's
321   // type would work fine and be simpler but would be different than what we've
322   // been doing and cause lit tests to change.
323   for (const auto *Field : D->fields()) {
324     if (Field->isBitField()) {
325       if (Field->isZeroLengthBitField(Context))
326         continue;
327       llvm::Type *FieldType = getStorageType(Field);
328       if (LayoutSize < getSize(FieldType))
329         FieldType = getByteArrayType(LayoutSize);
330       setBitFieldInfo(Field, CharUnits::Zero(), FieldType);
331     }
332     Fields[Field->getCanonicalDecl()] = 0;
333     llvm::Type *FieldType = getStorageType(Field);
334     // Compute zero-initializable status.
335     // This union might not be zero initialized: it may contain a pointer to
336     // data member which might have some exotic initialization sequence.
337     // If this is the case, then we aught not to try and come up with a "better"
338     // type, it might not be very easy to come up with a Constant which
339     // correctly initializes it.
340     if (!SeenNamedMember) {
341       SeenNamedMember = Field->getIdentifier();
342       if (!SeenNamedMember)
343         if (const auto *FieldRD = Field->getType()->getAsRecordDecl())
344           SeenNamedMember = FieldRD->findFirstNamedDataMember();
345       if (SeenNamedMember && !isZeroInitializable(Field)) {
346         IsZeroInitializable = IsZeroInitializableAsBase = false;
347         StorageType = FieldType;
348       }
349     }
350     // Because our union isn't zero initializable, we won't be getting a better
351     // storage type.
352     if (!IsZeroInitializable)
353       continue;
354     // Conditionally update our storage type if we've got a new "better" one.
355     if (!StorageType ||
356         getAlignment(FieldType) >  getAlignment(StorageType) ||
357         (getAlignment(FieldType) == getAlignment(StorageType) &&
358         getSize(FieldType) > getSize(StorageType)))
359       StorageType = FieldType;
360   }
361   // If we have no storage type just pad to the appropriate size and return.
362   if (!StorageType)
363     return appendPaddingBytes(LayoutSize);
364   // If our storage size was bigger than our required size (can happen in the
365   // case of packed bitfields on Itanium) then just use an I8 array.
366   if (LayoutSize < getSize(StorageType))
367     StorageType = getByteArrayType(LayoutSize);
368   FieldTypes.push_back(StorageType);
369   appendPaddingBytes(LayoutSize - getSize(StorageType));
370   // Set packed if we need it.
371   const auto StorageAlignment = getAlignment(StorageType);
372   assert((Layout.getSize() % StorageAlignment == 0 ||
373           Layout.getDataSize() % StorageAlignment) &&
374          "Union's standard layout and no_unique_address layout must agree on "
375          "packedness");
376   if (Layout.getDataSize() % StorageAlignment)
377     Packed = true;
378 }
379 
accumulateFields(bool isNonVirtualBaseType)380 void CGRecordLowering::accumulateFields(bool isNonVirtualBaseType) {
381   for (RecordDecl::field_iterator Field = D->field_begin(),
382                                   FieldEnd = D->field_end();
383        Field != FieldEnd;) {
384     if (Field->isBitField()) {
385       Field = accumulateBitFields(isNonVirtualBaseType, Field, FieldEnd);
386       assert((Field == FieldEnd || !Field->isBitField()) &&
387              "Failed to accumulate all the bitfields");
388     } else if (isEmptyFieldForLayout(Context, *Field)) {
389       // Empty fields have no storage.
390       ++Field;
391     } else {
392       // Use base subobject layout for the potentially-overlapping field,
393       // as it is done in RecordLayoutBuilder
394       Members.push_back(MemberInfo(
395           bitsToCharUnits(getFieldBitOffset(*Field)), MemberInfo::Field,
396           Field->isPotentiallyOverlapping()
397               ? getStorageType(Field->getType()->getAsCXXRecordDecl())
398               : getStorageType(*Field),
399           *Field));
400       ++Field;
401     }
402   }
403 }
404 
405 // Create members for bitfields. Field is a bitfield, and FieldEnd is the end
406 // iterator of the record. Return the first non-bitfield encountered.  We need
407 // to know whether this is the base or complete layout, as virtual bases could
408 // affect the upper bound of bitfield access unit allocation.
409 RecordDecl::field_iterator
accumulateBitFields(bool isNonVirtualBaseType,RecordDecl::field_iterator Field,RecordDecl::field_iterator FieldEnd)410 CGRecordLowering::accumulateBitFields(bool isNonVirtualBaseType,
411                                       RecordDecl::field_iterator Field,
412                                       RecordDecl::field_iterator FieldEnd) {
413   if (isDiscreteBitFieldABI()) {
414     // Run stores the first element of the current run of bitfields. FieldEnd is
415     // used as a special value to note that we don't have a current run. A
416     // bitfield run is a contiguous collection of bitfields that can be stored
417     // in the same storage block. Zero-sized bitfields and bitfields that would
418     // cross an alignment boundary break a run and start a new one.
419     RecordDecl::field_iterator Run = FieldEnd;
420     // Tail is the offset of the first bit off the end of the current run. It's
421     // used to determine if the ASTRecordLayout is treating these two bitfields
422     // as contiguous. StartBitOffset is offset of the beginning of the Run.
423     uint64_t StartBitOffset, Tail = 0;
424     for (; Field != FieldEnd && Field->isBitField(); ++Field) {
425       // Zero-width bitfields end runs.
426       if (Field->isZeroLengthBitField(Context)) {
427         Run = FieldEnd;
428         continue;
429       }
430       uint64_t BitOffset = getFieldBitOffset(*Field);
431       llvm::Type *Type = Types.ConvertTypeForMem(Field->getType());
432       // If we don't have a run yet, or don't live within the previous run's
433       // allocated storage then we allocate some storage and start a new run.
434       if (Run == FieldEnd || BitOffset >= Tail) {
435         Run = Field;
436         StartBitOffset = BitOffset;
437         Tail = StartBitOffset + DataLayout.getTypeAllocSizeInBits(Type);
438         // Add the storage member to the record.  This must be added to the
439         // record before the bitfield members so that it gets laid out before
440         // the bitfields it contains get laid out.
441         Members.push_back(StorageInfo(bitsToCharUnits(StartBitOffset), Type));
442       }
443       // Bitfields get the offset of their storage but come afterward and remain
444       // there after a stable sort.
445       Members.push_back(MemberInfo(bitsToCharUnits(StartBitOffset),
446                                    MemberInfo::Field, nullptr, *Field));
447     }
448     return Field;
449   }
450 
451   // The SysV ABI can overlap bitfield storage units with both other bitfield
452   // storage units /and/ other non-bitfield data members. Accessing a sequence
453   // of bitfields mustn't interfere with adjacent non-bitfields -- they're
454   // permitted to be accessed in separate threads for instance.
455 
456   // We split runs of bit-fields into a sequence of "access units". When we emit
457   // a load or store of a bit-field, we'll load/store the entire containing
458   // access unit. As mentioned, the standard requires that these loads and
459   // stores must not interfere with accesses to other memory locations, and it
460   // defines the bit-field's memory location as the current run of
461   // non-zero-width bit-fields. So an access unit must never overlap with
462   // non-bit-field storage or cross a zero-width bit-field. Otherwise, we're
463   // free to draw the lines as we see fit.
464 
465   // Drawing these lines well can be complicated. LLVM generally can't modify a
466   // program to access memory that it didn't before, so using very narrow access
467   // units can prevent the compiler from using optimal access patterns. For
468   // example, suppose a run of bit-fields occupies four bytes in a struct. If we
469   // split that into four 1-byte access units, then a sequence of assignments
470   // that doesn't touch all four bytes may have to be emitted with multiple
471   // 8-bit stores instead of a single 32-bit store. On the other hand, if we use
472   // very wide access units, we may find ourselves emitting accesses to
473   // bit-fields we didn't really need to touch, just because LLVM was unable to
474   // clean up after us.
475 
476   // It is desirable to have access units be aligned powers of 2 no larger than
477   // a register. (On non-strict alignment ISAs, the alignment requirement can be
478   // dropped.) A three byte access unit will be accessed using 2-byte and 1-byte
479   // accesses and bit manipulation. If no bitfield straddles across the two
480   // separate accesses, it is better to have separate 2-byte and 1-byte access
481   // units, as then LLVM will not generate unnecessary memory accesses, or bit
482   // manipulation. Similarly, on a strict-alignment architecture, it is better
483   // to keep access-units naturally aligned, to avoid similar bit
484   // manipulation synthesizing larger unaligned accesses.
485 
486   // Bitfields that share parts of a single byte are, of necessity, placed in
487   // the same access unit. That unit will encompass a consecutive run where
488   // adjacent bitfields share parts of a byte. (The first bitfield of such an
489   // access unit will start at the beginning of a byte.)
490 
491   // We then try and accumulate adjacent access units when the combined unit is
492   // naturally sized, no larger than a register, and (on a strict alignment
493   // ISA), naturally aligned. Note that this requires lookahead to one or more
494   // subsequent access units. For instance, consider a 2-byte access-unit
495   // followed by 2 1-byte units. We can merge that into a 4-byte access-unit,
496   // but we would not want to merge a 2-byte followed by a single 1-byte (and no
497   // available tail padding). We keep track of the best access unit seen so far,
498   // and use that when we determine we cannot accumulate any more. Then we start
499   // again at the bitfield following that best one.
500 
501   // The accumulation is also prevented when:
502   // *) it would cross a character-aigned zero-width bitfield, or
503   // *) fine-grained bitfield access option is in effect.
504 
505   CharUnits RegSize =
506       bitsToCharUnits(Context.getTargetInfo().getRegisterWidth());
507   unsigned CharBits = Context.getCharWidth();
508 
509   // Limit of useable tail padding at end of the record. Computed lazily and
510   // cached here.
511   CharUnits ScissorOffset = CharUnits::Zero();
512 
513   // Data about the start of the span we're accumulating to create an access
514   // unit from. Begin is the first bitfield of the span. If Begin is FieldEnd,
515   // we've not got a current span. The span starts at the BeginOffset character
516   // boundary. BitSizeSinceBegin is the size (in bits) of the span -- this might
517   // include padding when we've advanced to a subsequent bitfield run.
518   RecordDecl::field_iterator Begin = FieldEnd;
519   CharUnits BeginOffset;
520   uint64_t BitSizeSinceBegin;
521 
522   // The (non-inclusive) end of the largest acceptable access unit we've found
523   // since Begin. If this is Begin, we're gathering the initial set of bitfields
524   // of a new span. BestEndOffset is the end of that acceptable access unit --
525   // it might extend beyond the last character of the bitfield run, using
526   // available padding characters.
527   RecordDecl::field_iterator BestEnd = Begin;
528   CharUnits BestEndOffset;
529   bool BestClipped; // Whether the representation must be in a byte array.
530 
531   for (;;) {
532     // AtAlignedBoundary is true iff Field is the (potential) start of a new
533     // span (or the end of the bitfields). When true, LimitOffset is the
534     // character offset of that span and Barrier indicates whether the new
535     // span cannot be merged into the current one.
536     bool AtAlignedBoundary = false;
537     bool Barrier = false;
538 
539     if (Field != FieldEnd && Field->isBitField()) {
540       uint64_t BitOffset = getFieldBitOffset(*Field);
541       if (Begin == FieldEnd) {
542         // Beginning a new span.
543         Begin = Field;
544         BestEnd = Begin;
545 
546         assert((BitOffset % CharBits) == 0 && "Not at start of char");
547         BeginOffset = bitsToCharUnits(BitOffset);
548         BitSizeSinceBegin = 0;
549       } else if ((BitOffset % CharBits) != 0) {
550         // Bitfield occupies the same character as previous bitfield, it must be
551         // part of the same span. This can include zero-length bitfields, should
552         // the target not align them to character boundaries. Such non-alignment
553         // is at variance with the standards, which require zero-length
554         // bitfields be a barrier between access units. But of course we can't
555         // achieve that in the middle of a character.
556         assert(BitOffset == Context.toBits(BeginOffset) + BitSizeSinceBegin &&
557                "Concatenating non-contiguous bitfields");
558       } else {
559         // Bitfield potentially begins a new span. This includes zero-length
560         // bitfields on non-aligning targets that lie at character boundaries
561         // (those are barriers to merging).
562         if (Field->isZeroLengthBitField(Context))
563           Barrier = true;
564         AtAlignedBoundary = true;
565       }
566     } else {
567       // We've reached the end of the bitfield run. Either we're done, or this
568       // is a barrier for the current span.
569       if (Begin == FieldEnd)
570         break;
571 
572       Barrier = true;
573       AtAlignedBoundary = true;
574     }
575 
576     // InstallBest indicates whether we should create an access unit for the
577     // current best span: fields [Begin, BestEnd) occupying characters
578     // [BeginOffset, BestEndOffset).
579     bool InstallBest = false;
580     if (AtAlignedBoundary) {
581       // Field is the start of a new span or the end of the bitfields. The
582       // just-seen span now extends to BitSizeSinceBegin.
583 
584       // Determine if we can accumulate that just-seen span into the current
585       // accumulation.
586       CharUnits AccessSize = bitsToCharUnits(BitSizeSinceBegin + CharBits - 1);
587       if (BestEnd == Begin) {
588         // This is the initial run at the start of a new span. By definition,
589         // this is the best seen so far.
590         BestEnd = Field;
591         BestEndOffset = BeginOffset + AccessSize;
592         // Assume clipped until proven not below.
593         BestClipped = true;
594         if (!BitSizeSinceBegin)
595           // A zero-sized initial span -- this will install nothing and reset
596           // for another.
597           InstallBest = true;
598       } else if (AccessSize > RegSize)
599         // Accumulating the just-seen span would create a multi-register access
600         // unit, which would increase register pressure.
601         InstallBest = true;
602 
603       if (!InstallBest) {
604         // Determine if accumulating the just-seen span will create an expensive
605         // access unit or not.
606         llvm::Type *Type = getIntNType(Context.toBits(AccessSize));
607         if (!Context.getTargetInfo().hasCheapUnalignedBitFieldAccess()) {
608           // Unaligned accesses are expensive. Only accumulate if the new unit
609           // is naturally aligned. Otherwise install the best we have, which is
610           // either the initial access unit (can't do better), or a naturally
611           // aligned accumulation (since we would have already installed it if
612           // it wasn't naturally aligned).
613           CharUnits Align = getAlignment(Type);
614           if (Align > Layout.getAlignment())
615             // The alignment required is greater than the containing structure
616             // itself.
617             InstallBest = true;
618           else if (!BeginOffset.isMultipleOf(Align))
619             // The access unit is not at a naturally aligned offset within the
620             // structure.
621             InstallBest = true;
622 
623           if (InstallBest && BestEnd == Field)
624             // We're installing the first span, whose clipping was presumed
625             // above. Compute it correctly.
626             if (getSize(Type) == AccessSize)
627               BestClipped = false;
628         }
629 
630         if (!InstallBest) {
631           // Find the next used storage offset to determine what the limit of
632           // the current span is. That's either the offset of the next field
633           // with storage (which might be Field itself) or the end of the
634           // non-reusable tail padding.
635           CharUnits LimitOffset;
636           for (auto Probe = Field; Probe != FieldEnd; ++Probe)
637             if (!isEmptyFieldForLayout(Context, *Probe)) {
638               // A member with storage sets the limit.
639               assert((getFieldBitOffset(*Probe) % CharBits) == 0 &&
640                      "Next storage is not byte-aligned");
641               LimitOffset = bitsToCharUnits(getFieldBitOffset(*Probe));
642               goto FoundLimit;
643             }
644           // We reached the end of the fields, determine the bounds of useable
645           // tail padding. As this can be complex for C++, we cache the result.
646           if (ScissorOffset.isZero()) {
647             ScissorOffset = calculateTailClippingOffset(isNonVirtualBaseType);
648             assert(!ScissorOffset.isZero() && "Tail clipping at zero");
649           }
650 
651           LimitOffset = ScissorOffset;
652         FoundLimit:;
653 
654           CharUnits TypeSize = getSize(Type);
655           if (BeginOffset + TypeSize <= LimitOffset) {
656             // There is space before LimitOffset to create a naturally-sized
657             // access unit.
658             BestEndOffset = BeginOffset + TypeSize;
659             BestEnd = Field;
660             BestClipped = false;
661           }
662 
663           if (Barrier)
664             // The next field is a barrier that we cannot merge across.
665             InstallBest = true;
666           else if (Types.getCodeGenOpts().FineGrainedBitfieldAccesses)
667             // Fine-grained access, so no merging of spans.
668             InstallBest = true;
669           else
670             // Otherwise, we're not installing. Update the bit size
671             // of the current span to go all the way to LimitOffset, which is
672             // the (aligned) offset of next bitfield to consider.
673             BitSizeSinceBegin = Context.toBits(LimitOffset - BeginOffset);
674         }
675       }
676     }
677 
678     if (InstallBest) {
679       assert((Field == FieldEnd || !Field->isBitField() ||
680               (getFieldBitOffset(*Field) % CharBits) == 0) &&
681              "Installing but not at an aligned bitfield or limit");
682       CharUnits AccessSize = BestEndOffset - BeginOffset;
683       if (!AccessSize.isZero()) {
684         // Add the storage member for the access unit to the record. The
685         // bitfields get the offset of their storage but come afterward and
686         // remain there after a stable sort.
687         llvm::Type *Type;
688         if (BestClipped) {
689           assert(getSize(getIntNType(Context.toBits(AccessSize))) >
690                      AccessSize &&
691                  "Clipped access need not be clipped");
692           Type = getByteArrayType(AccessSize);
693         } else {
694           Type = getIntNType(Context.toBits(AccessSize));
695           assert(getSize(Type) == AccessSize &&
696                  "Unclipped access must be clipped");
697         }
698         Members.push_back(StorageInfo(BeginOffset, Type));
699         for (; Begin != BestEnd; ++Begin)
700           if (!Begin->isZeroLengthBitField(Context))
701             Members.push_back(
702                 MemberInfo(BeginOffset, MemberInfo::Field, nullptr, *Begin));
703       }
704       // Reset to start a new span.
705       Field = BestEnd;
706       Begin = FieldEnd;
707     } else {
708       assert(Field != FieldEnd && Field->isBitField() &&
709              "Accumulating past end of bitfields");
710       assert(!Barrier && "Accumulating across barrier");
711       // Accumulate this bitfield into the current (potential) span.
712       BitSizeSinceBegin += Field->getBitWidthValue(Context);
713       ++Field;
714     }
715   }
716 
717   return Field;
718 }
719 
accumulateBases()720 void CGRecordLowering::accumulateBases() {
721   // If we've got a primary virtual base, we need to add it with the bases.
722   if (Layout.isPrimaryBaseVirtual()) {
723     const CXXRecordDecl *BaseDecl = Layout.getPrimaryBase();
724     Members.push_back(MemberInfo(CharUnits::Zero(), MemberInfo::Base,
725                                  getStorageType(BaseDecl), BaseDecl));
726   }
727   // Accumulate the non-virtual bases.
728   for (const auto &Base : RD->bases()) {
729     if (Base.isVirtual())
730       continue;
731 
732     // Bases can be zero-sized even if not technically empty if they
733     // contain only a trailing array member.
734     const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
735     if (!isEmptyRecordForLayout(Context, Base.getType()) &&
736         !Context.getASTRecordLayout(BaseDecl).getNonVirtualSize().isZero())
737       Members.push_back(MemberInfo(Layout.getBaseClassOffset(BaseDecl),
738           MemberInfo::Base, getStorageType(BaseDecl), BaseDecl));
739   }
740 }
741 
742 /// The AAPCS that defines that, when possible, bit-fields should
743 /// be accessed using containers of the declared type width:
744 /// When a volatile bit-field is read, and its container does not overlap with
745 /// any non-bit-field member or any zero length bit-field member, its container
746 /// must be read exactly once using the access width appropriate to the type of
747 /// the container. When a volatile bit-field is written, and its container does
748 /// not overlap with any non-bit-field member or any zero-length bit-field
749 /// member, its container must be read exactly once and written exactly once
750 /// using the access width appropriate to the type of the container. The two
751 /// accesses are not atomic.
752 ///
753 /// Enforcing the width restriction can be disabled using
754 /// -fno-aapcs-bitfield-width.
computeVolatileBitfields()755 void CGRecordLowering::computeVolatileBitfields() {
756   if (!isAAPCS() || !Types.getCodeGenOpts().AAPCSBitfieldWidth)
757     return;
758 
759   for (auto &I : BitFields) {
760     const FieldDecl *Field = I.first;
761     CGBitFieldInfo &Info = I.second;
762     llvm::Type *ResLTy = Types.ConvertTypeForMem(Field->getType());
763     // If the record alignment is less than the type width, we can't enforce a
764     // aligned load, bail out.
765     if ((uint64_t)(Context.toBits(Layout.getAlignment())) <
766         ResLTy->getPrimitiveSizeInBits())
767       continue;
768     // CGRecordLowering::setBitFieldInfo() pre-adjusts the bit-field offsets
769     // for big-endian targets, but it assumes a container of width
770     // Info.StorageSize. Since AAPCS uses a different container size (width
771     // of the type), we first undo that calculation here and redo it once
772     // the bit-field offset within the new container is calculated.
773     const unsigned OldOffset =
774         isBE() ? Info.StorageSize - (Info.Offset + Info.Size) : Info.Offset;
775     // Offset to the bit-field from the beginning of the struct.
776     const unsigned AbsoluteOffset =
777         Context.toBits(Info.StorageOffset) + OldOffset;
778 
779     // Container size is the width of the bit-field type.
780     const unsigned StorageSize = ResLTy->getPrimitiveSizeInBits();
781     // Nothing to do if the access uses the desired
782     // container width and is naturally aligned.
783     if (Info.StorageSize == StorageSize && (OldOffset % StorageSize == 0))
784       continue;
785 
786     // Offset within the container.
787     unsigned Offset = AbsoluteOffset & (StorageSize - 1);
788     // Bail out if an aligned load of the container cannot cover the entire
789     // bit-field. This can happen for example, if the bit-field is part of a
790     // packed struct. AAPCS does not define access rules for such cases, we let
791     // clang to follow its own rules.
792     if (Offset + Info.Size > StorageSize)
793       continue;
794 
795     // Re-adjust offsets for big-endian targets.
796     if (isBE())
797       Offset = StorageSize - (Offset + Info.Size);
798 
799     const CharUnits StorageOffset =
800         Context.toCharUnitsFromBits(AbsoluteOffset & ~(StorageSize - 1));
801     const CharUnits End = StorageOffset +
802                           Context.toCharUnitsFromBits(StorageSize) -
803                           CharUnits::One();
804 
805     const ASTRecordLayout &Layout =
806         Context.getASTRecordLayout(Field->getParent());
807     // If we access outside memory outside the record, than bail out.
808     const CharUnits RecordSize = Layout.getSize();
809     if (End >= RecordSize)
810       continue;
811 
812     // Bail out if performing this load would access non-bit-fields members.
813     bool Conflict = false;
814     for (const auto *F : D->fields()) {
815       // Allow sized bit-fields overlaps.
816       if (F->isBitField() && !F->isZeroLengthBitField(Context))
817         continue;
818 
819       const CharUnits FOffset = Context.toCharUnitsFromBits(
820           Layout.getFieldOffset(F->getFieldIndex()));
821 
822       // As C11 defines, a zero sized bit-field defines a barrier, so
823       // fields after and before it should be race condition free.
824       // The AAPCS acknowledges it and imposes no restritions when the
825       // natural container overlaps a zero-length bit-field.
826       if (F->isZeroLengthBitField(Context)) {
827         if (End > FOffset && StorageOffset < FOffset) {
828           Conflict = true;
829           break;
830         }
831       }
832 
833       const CharUnits FEnd =
834           FOffset +
835           Context.toCharUnitsFromBits(
836               Types.ConvertTypeForMem(F->getType())->getPrimitiveSizeInBits()) -
837           CharUnits::One();
838       // If no overlap, continue.
839       if (End < FOffset || FEnd < StorageOffset)
840         continue;
841 
842       // The desired load overlaps a non-bit-field member, bail out.
843       Conflict = true;
844       break;
845     }
846 
847     if (Conflict)
848       continue;
849     // Write the new bit-field access parameters.
850     // As the storage offset now is defined as the number of elements from the
851     // start of the structure, we should divide the Offset by the element size.
852     Info.VolatileStorageOffset =
853         StorageOffset / Context.toCharUnitsFromBits(StorageSize).getQuantity();
854     Info.VolatileStorageSize = StorageSize;
855     Info.VolatileOffset = Offset;
856   }
857 }
858 
accumulateVPtrs()859 void CGRecordLowering::accumulateVPtrs() {
860   if (Layout.hasOwnVFPtr())
861     Members.push_back(
862         MemberInfo(CharUnits::Zero(), MemberInfo::VFPtr,
863                    llvm::PointerType::getUnqual(Types.getLLVMContext())));
864   if (Layout.hasOwnVBPtr())
865     Members.push_back(
866         MemberInfo(Layout.getVBPtrOffset(), MemberInfo::VBPtr,
867                    llvm::PointerType::getUnqual(Types.getLLVMContext())));
868 }
869 
870 CharUnits
calculateTailClippingOffset(bool isNonVirtualBaseType) const871 CGRecordLowering::calculateTailClippingOffset(bool isNonVirtualBaseType) const {
872   if (!RD)
873     return Layout.getDataSize();
874 
875   CharUnits ScissorOffset = Layout.getNonVirtualSize();
876   // In the itanium ABI, it's possible to place a vbase at a dsize that is
877   // smaller than the nvsize.  Here we check to see if such a base is placed
878   // before the nvsize and set the scissor offset to that, instead of the
879   // nvsize.
880   if (!isNonVirtualBaseType && isOverlappingVBaseABI())
881     for (const auto &Base : RD->vbases()) {
882       const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
883       if (isEmptyRecordForLayout(Context, Base.getType()))
884         continue;
885       // If the vbase is a primary virtual base of some base, then it doesn't
886       // get its own storage location but instead lives inside of that base.
887       if (Context.isNearlyEmpty(BaseDecl) && !hasOwnStorage(RD, BaseDecl))
888         continue;
889       ScissorOffset = std::min(ScissorOffset,
890                                Layout.getVBaseClassOffset(BaseDecl));
891     }
892 
893   return ScissorOffset;
894 }
895 
accumulateVBases()896 void CGRecordLowering::accumulateVBases() {
897   for (const auto &Base : RD->vbases()) {
898     const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
899     if (isEmptyRecordForLayout(Context, Base.getType()))
900       continue;
901     CharUnits Offset = Layout.getVBaseClassOffset(BaseDecl);
902     // If the vbase is a primary virtual base of some base, then it doesn't
903     // get its own storage location but instead lives inside of that base.
904     if (isOverlappingVBaseABI() &&
905         Context.isNearlyEmpty(BaseDecl) &&
906         !hasOwnStorage(RD, BaseDecl)) {
907       Members.push_back(MemberInfo(Offset, MemberInfo::VBase, nullptr,
908                                    BaseDecl));
909       continue;
910     }
911     // If we've got a vtordisp, add it as a storage type.
912     if (Layout.getVBaseOffsetsMap().find(BaseDecl)->second.hasVtorDisp())
913       Members.push_back(StorageInfo(Offset - CharUnits::fromQuantity(4),
914                                     getIntNType(32)));
915     Members.push_back(MemberInfo(Offset, MemberInfo::VBase,
916                                  getStorageType(BaseDecl), BaseDecl));
917   }
918 }
919 
hasOwnStorage(const CXXRecordDecl * Decl,const CXXRecordDecl * Query) const920 bool CGRecordLowering::hasOwnStorage(const CXXRecordDecl *Decl,
921                                      const CXXRecordDecl *Query) const {
922   const ASTRecordLayout &DeclLayout = Context.getASTRecordLayout(Decl);
923   if (DeclLayout.isPrimaryBaseVirtual() && DeclLayout.getPrimaryBase() == Query)
924     return false;
925   for (const auto &Base : Decl->bases())
926     if (!hasOwnStorage(Base.getType()->getAsCXXRecordDecl(), Query))
927       return false;
928   return true;
929 }
930 
calculateZeroInit()931 void CGRecordLowering::calculateZeroInit() {
932   for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
933                                                MemberEnd = Members.end();
934        IsZeroInitializableAsBase && Member != MemberEnd; ++Member) {
935     if (Member->Kind == MemberInfo::Field) {
936       if (!Member->FD || isZeroInitializable(Member->FD))
937         continue;
938       IsZeroInitializable = IsZeroInitializableAsBase = false;
939     } else if (Member->Kind == MemberInfo::Base ||
940                Member->Kind == MemberInfo::VBase) {
941       if (isZeroInitializable(Member->RD))
942         continue;
943       IsZeroInitializable = false;
944       if (Member->Kind == MemberInfo::Base)
945         IsZeroInitializableAsBase = false;
946     }
947   }
948 }
949 
950 // Verify accumulateBitfields computed the correct storage representations.
checkBitfieldClipping(bool IsNonVirtualBaseType) const951 void CGRecordLowering::checkBitfieldClipping(bool IsNonVirtualBaseType) const {
952 #ifndef NDEBUG
953   auto ScissorOffset = calculateTailClippingOffset(IsNonVirtualBaseType);
954   auto Tail = CharUnits::Zero();
955   for (const auto &M : Members) {
956     // Only members with data could possibly overlap.
957     if (!M.Data)
958       continue;
959 
960     assert(M.Offset >= Tail && "Bitfield access unit is not clipped");
961     Tail = M.Offset + getSize(M.Data);
962     assert((Tail <= ScissorOffset || M.Offset >= ScissorOffset) &&
963            "Bitfield straddles scissor offset");
964   }
965 #endif
966 }
967 
determinePacked(bool NVBaseType)968 void CGRecordLowering::determinePacked(bool NVBaseType) {
969   if (Packed)
970     return;
971   CharUnits Alignment = CharUnits::One();
972   CharUnits NVAlignment = CharUnits::One();
973   CharUnits NVSize =
974       !NVBaseType && RD ? Layout.getNonVirtualSize() : CharUnits::Zero();
975   for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
976                                                MemberEnd = Members.end();
977        Member != MemberEnd; ++Member) {
978     if (!Member->Data)
979       continue;
980     // If any member falls at an offset that it not a multiple of its alignment,
981     // then the entire record must be packed.
982     if (Member->Offset % getAlignment(Member->Data))
983       Packed = true;
984     if (Member->Offset < NVSize)
985       NVAlignment = std::max(NVAlignment, getAlignment(Member->Data));
986     Alignment = std::max(Alignment, getAlignment(Member->Data));
987   }
988   // If the size of the record (the capstone's offset) is not a multiple of the
989   // record's alignment, it must be packed.
990   if (Members.back().Offset % Alignment)
991     Packed = true;
992   // If the non-virtual sub-object is not a multiple of the non-virtual
993   // sub-object's alignment, it must be packed.  We cannot have a packed
994   // non-virtual sub-object and an unpacked complete object or vise versa.
995   if (NVSize % NVAlignment)
996     Packed = true;
997   // Update the alignment of the sentinel.
998   if (!Packed)
999     Members.back().Data = getIntNType(Context.toBits(Alignment));
1000 }
1001 
insertPadding()1002 void CGRecordLowering::insertPadding() {
1003   std::vector<std::pair<CharUnits, CharUnits> > Padding;
1004   CharUnits Size = CharUnits::Zero();
1005   for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
1006                                                MemberEnd = Members.end();
1007        Member != MemberEnd; ++Member) {
1008     if (!Member->Data)
1009       continue;
1010     CharUnits Offset = Member->Offset;
1011     assert(Offset >= Size);
1012     // Insert padding if we need to.
1013     if (Offset !=
1014         Size.alignTo(Packed ? CharUnits::One() : getAlignment(Member->Data)))
1015       Padding.push_back(std::make_pair(Size, Offset - Size));
1016     Size = Offset + getSize(Member->Data);
1017   }
1018   if (Padding.empty())
1019     return;
1020   // Add the padding to the Members list and sort it.
1021   for (std::vector<std::pair<CharUnits, CharUnits> >::const_iterator
1022         Pad = Padding.begin(), PadEnd = Padding.end();
1023         Pad != PadEnd; ++Pad)
1024     Members.push_back(StorageInfo(Pad->first, getByteArrayType(Pad->second)));
1025   llvm::stable_sort(Members);
1026 }
1027 
fillOutputFields()1028 void CGRecordLowering::fillOutputFields() {
1029   for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
1030                                                MemberEnd = Members.end();
1031        Member != MemberEnd; ++Member) {
1032     if (Member->Data)
1033       FieldTypes.push_back(Member->Data);
1034     if (Member->Kind == MemberInfo::Field) {
1035       if (Member->FD)
1036         Fields[Member->FD->getCanonicalDecl()] = FieldTypes.size() - 1;
1037       // A field without storage must be a bitfield.
1038       if (!Member->Data)
1039         setBitFieldInfo(Member->FD, Member->Offset, FieldTypes.back());
1040     } else if (Member->Kind == MemberInfo::Base)
1041       NonVirtualBases[Member->RD] = FieldTypes.size() - 1;
1042     else if (Member->Kind == MemberInfo::VBase)
1043       VirtualBases[Member->RD] = FieldTypes.size() - 1;
1044   }
1045 }
1046 
MakeInfo(CodeGenTypes & Types,const FieldDecl * FD,uint64_t Offset,uint64_t Size,uint64_t StorageSize,CharUnits StorageOffset)1047 CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
1048                                         const FieldDecl *FD,
1049                                         uint64_t Offset, uint64_t Size,
1050                                         uint64_t StorageSize,
1051                                         CharUnits StorageOffset) {
1052   // This function is vestigial from CGRecordLayoutBuilder days but is still
1053   // used in GCObjCRuntime.cpp.  That usage has a "fixme" attached to it that
1054   // when addressed will allow for the removal of this function.
1055   llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType());
1056   CharUnits TypeSizeInBytes =
1057     CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(Ty));
1058   uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
1059 
1060   bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
1061 
1062   if (Size > TypeSizeInBits) {
1063     // We have a wide bit-field. The extra bits are only used for padding, so
1064     // if we have a bitfield of type T, with size N:
1065     //
1066     // T t : N;
1067     //
1068     // We can just assume that it's:
1069     //
1070     // T t : sizeof(T);
1071     //
1072     Size = TypeSizeInBits;
1073   }
1074 
1075   // Reverse the bit offsets for big endian machines. Because we represent
1076   // a bitfield as a single large integer load, we can imagine the bits
1077   // counting from the most-significant-bit instead of the
1078   // least-significant-bit.
1079   if (Types.getDataLayout().isBigEndian()) {
1080     Offset = StorageSize - (Offset + Size);
1081   }
1082 
1083   return CGBitFieldInfo(Offset, Size, IsSigned, StorageSize, StorageOffset);
1084 }
1085 
1086 std::unique_ptr<CGRecordLayout>
ComputeRecordLayout(const RecordDecl * D,llvm::StructType * Ty)1087 CodeGenTypes::ComputeRecordLayout(const RecordDecl *D, llvm::StructType *Ty) {
1088   CGRecordLowering Builder(*this, D, /*Packed=*/false);
1089 
1090   Builder.lower(/*NonVirtualBaseType=*/false);
1091 
1092   // If we're in C++, compute the base subobject type.
1093   llvm::StructType *BaseTy = nullptr;
1094   if (isa<CXXRecordDecl>(D)) {
1095     BaseTy = Ty;
1096     if (Builder.Layout.getNonVirtualSize() != Builder.Layout.getSize()) {
1097       CGRecordLowering BaseBuilder(*this, D, /*Packed=*/Builder.Packed);
1098       BaseBuilder.lower(/*NonVirtualBaseType=*/true);
1099       BaseTy = llvm::StructType::create(
1100           getLLVMContext(), BaseBuilder.FieldTypes, "", BaseBuilder.Packed);
1101       addRecordTypeName(D, BaseTy, ".base");
1102       // BaseTy and Ty must agree on their packedness for getLLVMFieldNo to work
1103       // on both of them with the same index.
1104       assert(Builder.Packed == BaseBuilder.Packed &&
1105              "Non-virtual and complete types must agree on packedness");
1106     }
1107   }
1108 
1109   // Fill in the struct *after* computing the base type.  Filling in the body
1110   // signifies that the type is no longer opaque and record layout is complete,
1111   // but we may need to recursively layout D while laying D out as a base type.
1112   Ty->setBody(Builder.FieldTypes, Builder.Packed);
1113 
1114   auto RL = std::make_unique<CGRecordLayout>(
1115       Ty, BaseTy, (bool)Builder.IsZeroInitializable,
1116       (bool)Builder.IsZeroInitializableAsBase);
1117 
1118   RL->NonVirtualBases.swap(Builder.NonVirtualBases);
1119   RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
1120 
1121   // Add all the field numbers.
1122   RL->FieldInfo.swap(Builder.Fields);
1123 
1124   // Add bitfield info.
1125   RL->BitFields.swap(Builder.BitFields);
1126 
1127   // Dump the layout, if requested.
1128   if (getContext().getLangOpts().DumpRecordLayouts) {
1129     llvm::outs() << "\n*** Dumping IRgen Record Layout\n";
1130     llvm::outs() << "Record: ";
1131     D->dump(llvm::outs());
1132     llvm::outs() << "\nLayout: ";
1133     RL->print(llvm::outs());
1134   }
1135 
1136 #ifndef NDEBUG
1137   // Verify that the computed LLVM struct size matches the AST layout size.
1138   const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
1139 
1140   uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
1141   assert(TypeSizeInBits == getDataLayout().getTypeAllocSizeInBits(Ty) &&
1142          "Type size mismatch!");
1143 
1144   if (BaseTy) {
1145     CharUnits NonVirtualSize  = Layout.getNonVirtualSize();
1146 
1147     uint64_t AlignedNonVirtualTypeSizeInBits =
1148       getContext().toBits(NonVirtualSize);
1149 
1150     assert(AlignedNonVirtualTypeSizeInBits ==
1151            getDataLayout().getTypeAllocSizeInBits(BaseTy) &&
1152            "Type size mismatch!");
1153   }
1154 
1155   // Verify that the LLVM and AST field offsets agree.
1156   llvm::StructType *ST = RL->getLLVMType();
1157   const llvm::StructLayout *SL = getDataLayout().getStructLayout(ST);
1158 
1159   const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
1160   RecordDecl::field_iterator it = D->field_begin();
1161   for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
1162     const FieldDecl *FD = *it;
1163 
1164     // Ignore zero-sized fields.
1165     if (isEmptyFieldForLayout(getContext(), FD))
1166       continue;
1167 
1168     // For non-bit-fields, just check that the LLVM struct offset matches the
1169     // AST offset.
1170     if (!FD->isBitField()) {
1171       unsigned FieldNo = RL->getLLVMFieldNo(FD);
1172       assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
1173              "Invalid field offset!");
1174       continue;
1175     }
1176 
1177     // Ignore unnamed bit-fields.
1178     if (!FD->getDeclName())
1179       continue;
1180 
1181     const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
1182     llvm::Type *ElementTy = ST->getTypeAtIndex(RL->getLLVMFieldNo(FD));
1183 
1184     // Unions have overlapping elements dictating their layout, but for
1185     // non-unions we can verify that this section of the layout is the exact
1186     // expected size.
1187     if (D->isUnion()) {
1188       // For unions we verify that the start is zero and the size
1189       // is in-bounds. However, on BE systems, the offset may be non-zero, but
1190       // the size + offset should match the storage size in that case as it
1191       // "starts" at the back.
1192       if (getDataLayout().isBigEndian())
1193         assert(static_cast<unsigned>(Info.Offset + Info.Size) ==
1194                Info.StorageSize &&
1195                "Big endian union bitfield does not end at the back");
1196       else
1197         assert(Info.Offset == 0 &&
1198                "Little endian union bitfield with a non-zero offset");
1199       assert(Info.StorageSize <= SL->getSizeInBits() &&
1200              "Union not large enough for bitfield storage");
1201     } else {
1202       assert((Info.StorageSize ==
1203                   getDataLayout().getTypeAllocSizeInBits(ElementTy) ||
1204               Info.VolatileStorageSize ==
1205                   getDataLayout().getTypeAllocSizeInBits(ElementTy)) &&
1206              "Storage size does not match the element type size");
1207     }
1208     assert(Info.Size > 0 && "Empty bitfield!");
1209     assert(static_cast<unsigned>(Info.Offset) + Info.Size <= Info.StorageSize &&
1210            "Bitfield outside of its allocated storage");
1211   }
1212 #endif
1213 
1214   return RL;
1215 }
1216 
print(raw_ostream & OS) const1217 void CGRecordLayout::print(raw_ostream &OS) const {
1218   OS << "<CGRecordLayout\n";
1219   OS << "  LLVMType:" << *CompleteObjectType << "\n";
1220   if (BaseSubobjectType)
1221     OS << "  NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
1222   OS << "  IsZeroInitializable:" << IsZeroInitializable << "\n";
1223   OS << "  BitFields:[\n";
1224 
1225   // Print bit-field infos in declaration order.
1226   std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
1227   for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
1228          it = BitFields.begin(), ie = BitFields.end();
1229        it != ie; ++it) {
1230     const RecordDecl *RD = it->first->getParent();
1231     unsigned Index = 0;
1232     for (RecordDecl::field_iterator
1233            it2 = RD->field_begin(); *it2 != it->first; ++it2)
1234       ++Index;
1235     BFIs.push_back(std::make_pair(Index, &it->second));
1236   }
1237   llvm::array_pod_sort(BFIs.begin(), BFIs.end());
1238   for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
1239     OS.indent(4);
1240     BFIs[i].second->print(OS);
1241     OS << "\n";
1242   }
1243 
1244   OS << "]>\n";
1245 }
1246 
dump() const1247 LLVM_DUMP_METHOD void CGRecordLayout::dump() const {
1248   print(llvm::errs());
1249 }
1250 
print(raw_ostream & OS) const1251 void CGBitFieldInfo::print(raw_ostream &OS) const {
1252   OS << "<CGBitFieldInfo"
1253      << " Offset:" << Offset << " Size:" << Size << " IsSigned:" << IsSigned
1254      << " StorageSize:" << StorageSize
1255      << " StorageOffset:" << StorageOffset.getQuantity()
1256      << " VolatileOffset:" << VolatileOffset
1257      << " VolatileStorageSize:" << VolatileStorageSize
1258      << " VolatileStorageOffset:" << VolatileStorageOffset.getQuantity() << ">";
1259 }
1260 
dump() const1261 LLVM_DUMP_METHOD void CGBitFieldInfo::dump() const {
1262   print(llvm::errs());
1263 }
1264