Coverage Report

Created: 2021-08-24 07:12

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/CGRecordLayoutBuilder.cpp
Line
Count
Source (jump to first uncovered line)
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 "CGRecordLayout.h"
14
#include "CGCXXABI.h"
15
#include "CodeGenTypes.h"
16
#include "clang/AST/ASTContext.h"
17
#include "clang/AST/Attr.h"
18
#include "clang/AST/CXXInheritance.h"
19
#include "clang/AST/DeclCXX.h"
20
#include "clang/AST/Expr.h"
21
#include "clang/AST/RecordLayout.h"
22
#include "clang/Basic/CodeGenOptions.h"
23
#include "llvm/IR/DataLayout.h"
24
#include "llvm/IR/DerivedTypes.h"
25
#include "llvm/IR/Type.h"
26
#include "llvm/Support/Debug.h"
27
#include "llvm/Support/MathExtras.h"
28
#include "llvm/Support/raw_ostream.h"
29
using namespace clang;
30
using namespace CodeGen;
31
32
namespace {
33
/// The CGRecordLowering is responsible for lowering an ASTRecordLayout to an
34
/// llvm::Type.  Some of the lowering is straightforward, some is not.  Here we
35
/// detail some of the complexities and weirdnesses here.
36
/// * LLVM does not have unions - Unions can, in theory be represented by any
37
///   llvm::Type with correct size.  We choose a field via a specific heuristic
38
///   and add padding if necessary.
39
/// * LLVM does not have bitfields - Bitfields are collected into contiguous
40
///   runs and allocated as a single storage type for the run.  ASTRecordLayout
41
///   contains enough information to determine where the runs break.  Microsoft
42
///   and Itanium follow different rules and use different codepaths.
43
/// * It is desired that, when possible, bitfields use the appropriate iN type
44
///   when lowered to llvm types.  For example unsigned x : 24 gets lowered to
45
///   i24.  This isn't always possible because i24 has storage size of 32 bit
46
///   and if it is possible to use that extra byte of padding we must use
47
///   [i8 x 3] instead of i24.  The function clipTailPadding does this.
48
///   C++ examples that require clipping:
49
///   struct { int a : 24; char b; }; // a must be clipped, b goes at offset 3
50
///   struct A { int a : 24; }; // a must be clipped because a struct like B
51
//    could exist: struct B : A { char b; }; // b goes at offset 3
52
/// * Clang ignores 0 sized bitfields and 0 sized bases but *not* zero sized
53
///   fields.  The existing asserts suggest that LLVM assumes that *every* field
54
///   has an underlying storage type.  Therefore empty structures containing
55
///   zero sized subobjects such as empty records or zero sized arrays still get
56
///   a zero sized (empty struct) storage type.
57
/// * Clang reads the complete type rather than the base type when generating
58
///   code to access fields.  Bitfields in tail position with tail padding may
59
///   be clipped in the base class but not the complete class (we may discover
60
///   that the tail padding is not used in the complete class.) However,
61
///   because LLVM reads from the complete type it can generate incorrect code
62
///   if we do not clip the tail padding off of the bitfield in the complete
63
///   layout.  This introduces a somewhat awkward extra unnecessary clip stage.
64
///   The location of the clip is stored internally as a sentinel of type
65
///   SCISSOR.  If LLVM were updated to read base types (which it probably
66
///   should because locations of things such as VBases are bogus in the llvm
67
///   type anyway) then we could eliminate the SCISSOR.
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, Scissor } Kind;
80
    llvm::Type *Data;
81
    union {
82
      const FieldDecl *FD;
83
      const CXXRecordDecl *RD;
84
    };
85
    MemberInfo(CharUnits Offset, InfoKind Kind, llvm::Type *Data,
86
               const FieldDecl *FD = nullptr)
87
413k
      : Offset(Offset), Kind(Kind), Data(Data), FD(FD) {}
88
    MemberInfo(CharUnits Offset, InfoKind Kind, llvm::Type *Data,
89
               const CXXRecordDecl *RD)
90
73.1k
      : Offset(Offset), Kind(Kind), Data(Data), RD(RD) {}
91
    // MemberInfos are sorted so we define a < operator.
92
262k
    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 *.
98
127k
  MemberInfo StorageInfo(CharUnits Offset, llvm::Type *Data) {
99
127k
    return MemberInfo(Offset, MemberInfo::Field, Data);
100
127k
  }
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.
107
3.28k
  bool isDiscreteBitFieldABI() {
108
3.28k
    return Context.getTargetInfo().getCXXABI().isMicrosoft() ||
109
3.28k
           
D->isMsStruct(Context)3.26k
;
110
3.28k
  }
111
112
  /// Helper function to check if we are targeting AAPCS.
113
148k
  bool isAAPCS() const {
114
148k
    return Context.getTargetInfo().getABI().startswith("aapcs");
115
148k
  }
116
117
  /// Helper function to check if the target machine is BigEndian.
118
791
  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.
124
62.7k
  bool isOverlappingVBaseABI() {
125
62.7k
    return !Context.getTargetInfo().getCXXABI().isMicrosoft();
126
62.7k
  }
127
128
  /// Wraps llvm::Type::getIntNTy with some implicit arguments.
129
244k
  llvm::Type *getIntNType(uint64_t NumBits) {
130
244k
    unsigned AlignedBits = llvm::alignTo(NumBits, Context.getCharWidth());
131
244k
    return llvm::Type::getIntNTy(Types.getLLVMContext(), AlignedBits);
132
244k
  }
133
  /// Get the LLVM type sized as one character unit.
134
19.6k
  llvm::Type *getCharType() {
135
19.6k
    return llvm::Type::getIntNTy(Types.getLLVMContext(),
136
19.6k
                                 Context.getCharWidth());
137
19.6k
  }
138
  /// Gets an llvm type of size NumChars and alignment 1.
139
19.6k
  llvm::Type *getByteArrayType(CharUnits NumChars) {
140
19.6k
    assert(!NumChars.isZero() && "Empty byte arrays aren't allowed.");
141
0
    llvm::Type *Type = getCharType();
142
19.6k
    return NumChars == CharUnits::One() ? 
Type16.7k
:
143
19.6k
        
(llvm::Type *)llvm::ArrayType::get(Type, NumChars.getQuantity())2.92k
;
144
19.6k
  }
145
  /// Gets the storage type for a field decl and handles storage
146
  /// for itanium bitfields that are smaller than their declared type.
147
279k
  llvm::Type *getStorageType(const FieldDecl *FD) {
148
279k
    llvm::Type *Type = Types.ConvertTypeForMem(FD->getType());
149
279k
    if (!FD->isBitField()) 
return Type279k
;
150
216
    if (isDiscreteBitFieldABI()) 
return Type10
;
151
206
    return getIntNType(std::min(FD->getBitWidthValue(Context),
152
206
                             (unsigned)Context.toBits(getSize(Type))));
153
216
  }
154
  /// Gets the llvm Basesubobject type from a CXXRecordDecl.
155
11.4k
  llvm::Type *getStorageType(const CXXRecordDecl *RD) {
156
11.4k
    return Types.getCGRecordLayout(RD).getBaseSubobjectLLVMType();
157
11.4k
  }
158
285k
  CharUnits bitsToCharUnits(uint64_t BitOffset) {
159
285k
    return Context.toCharUnitsFromBits(BitOffset);
160
285k
  }
161
898k
  CharUnits getSize(llvm::Type *Type) {
162
898k
    return CharUnits::fromQuantity(DataLayout.getTypeAllocSize(Type));
163
898k
  }
164
1.39M
  CharUnits getAlignment(llvm::Type *Type) {
165
1.39M
    return CharUnits::fromQuantity(DataLayout.getABITypeAlignment(Type));
166
1.39M
  }
167
284k
  bool isZeroInitializable(const FieldDecl *FD) {
168
284k
    return Types.isZeroInitializable(FD->getType());
169
284k
  }
170
11.6k
  bool isZeroInitializable(const RecordDecl *RD) {
171
11.6k
    return Types.isZeroInitializable(RD);
172
11.6k
  }
173
27.2k
  void appendPaddingBytes(CharUnits Size) {
174
27.2k
    if (!Size.isZero())
175
16.7k
      FieldTypes.push_back(getByteArrayType(Size));
176
27.2k
  }
177
292k
  uint64_t getFieldBitOffset(const FieldDecl *FD) {
178
292k
    return Layout.getFieldOffset(FD->getFieldIndex());
179
292k
  }
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();
186
  void accumulateFields();
187
  void accumulateBitFields(RecordDecl::field_iterator Field,
188
                           RecordDecl::field_iterator FieldEnd);
189
  void computeVolatileBitfields();
190
  void accumulateBases();
191
  void accumulateVPtrs();
192
  void accumulateVBases();
193
  /// Recursively searches all of the bases to find out if a vbase is
194
  /// not the primary vbase of some base class.
195
  bool hasOwnStorage(const CXXRecordDecl *Decl, const CXXRecordDecl *Query);
196
  void calculateZeroInit();
197
  /// Lowers bitfield storage types to I8 arrays for bitfields with tail
198
  /// padding that is or can potentially be used.
199
  void clipTailPadding();
200
  /// Determines if we need a packed llvm struct.
201
  void determinePacked(bool NVBaseType);
202
  /// Inserts padding everywhere it's needed.
203
  void insertPadding();
204
  /// Fills out the structures that are ultimately consumed.
205
  void fillOutputFields();
206
  // Input memoization fields.
207
  CodeGenTypes &Types;
208
  const ASTContext &Context;
209
  const RecordDecl *D;
210
  const CXXRecordDecl *RD;
211
  const ASTRecordLayout &Layout;
212
  const llvm::DataLayout &DataLayout;
213
  // Helpful intermediate data-structures.
214
  std::vector<MemberInfo> Members;
215
  // Output fields, consumed by CodeGenTypes::ComputeRecordLayout.
216
  SmallVector<llvm::Type *, 16> FieldTypes;
217
  llvm::DenseMap<const FieldDecl *, unsigned> Fields;
218
  llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
219
  llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
220
  llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases;
221
  bool IsZeroInitializable : 1;
222
  bool IsZeroInitializableAsBase : 1;
223
  bool Packed : 1;
224
private:
225
  CGRecordLowering(const CGRecordLowering &) = delete;
226
  void operator =(const CGRecordLowering &) = delete;
227
};
228
} // namespace {
229
230
CGRecordLowering::CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D,
231
                                   bool Packed)
232
    : Types(Types), Context(Types.getContext()), D(D),
233
      RD(dyn_cast<CXXRecordDecl>(D)),
234
      Layout(Types.getContext().getASTRecordLayout(D)),
235
      DataLayout(Types.getDataLayout()), IsZeroInitializable(true),
236
148k
      IsZeroInitializableAsBase(true), Packed(Packed) {}
237
238
void CGRecordLowering::setBitFieldInfo(
239
10.2k
    const FieldDecl *FD, CharUnits StartOffset, llvm::Type *StorageType) {
240
10.2k
  CGBitFieldInfo &Info = BitFields[FD->getCanonicalDecl()];
241
10.2k
  Info.IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
242
10.2k
  Info.Offset = (unsigned)(getFieldBitOffset(FD) - Context.toBits(StartOffset));
243
10.2k
  Info.Size = FD->getBitWidthValue(Context);
244
10.2k
  Info.StorageSize = (unsigned)DataLayout.getTypeAllocSizeInBits(StorageType);
245
10.2k
  Info.StorageOffset = StartOffset;
246
10.2k
  if (Info.Size > Info.StorageSize)
247
11
    Info.Size = Info.StorageSize;
248
  // Reverse the bit offsets for big endian machines. Because we represent
249
  // a bitfield as a single large integer load, we can imagine the bits
250
  // counting from the most-significant-bit instead of the
251
  // least-significant-bit.
252
10.2k
  if (DataLayout.isBigEndian())
253
330
    Info.Offset = Info.StorageSize - (Info.Offset + Info.Size);
254
255
10.2k
  Info.VolatileStorageSize = 0;
256
10.2k
  Info.VolatileOffset = 0;
257
10.2k
  Info.VolatileStorageOffset = CharUnits::Zero();
258
10.2k
}
259
260
148k
void CGRecordLowering::lower(bool NVBaseType) {
261
  // The lowering process implemented in this function takes a variety of
262
  // carefully ordered phases.
263
  // 1) Store all members (fields and bases) in a list and sort them by offset.
264
  // 2) Add a 1-byte capstone member at the Size of the structure.
265
  // 3) Clip bitfield storages members if their tail padding is or might be
266
  //    used by another field or base.  The clipping process uses the capstone
267
  //    by treating it as another object that occurs after the record.
268
  // 4) Determine if the llvm-struct requires packing.  It's important that this
269
  //    phase occur after clipping, because clipping changes the llvm type.
270
  //    This phase reads the offset of the capstone when determining packedness
271
  //    and updates the alignment of the capstone to be equal of the alignment
272
  //    of the record after doing so.
273
  // 5) Insert padding everywhere it is needed.  This phase requires 'Packed' to
274
  //    have been computed and needs to know the alignment of the record in
275
  //    order to understand if explicit tail padding is needed.
276
  // 6) Remove the capstone, we don't need it anymore.
277
  // 7) Determine if this record can be zero-initialized.  This phase could have
278
  //    been placed anywhere after phase 1.
279
  // 8) Format the complete list of members in a way that can be consumed by
280
  //    CodeGenTypes::ComputeRecordLayout.
281
148k
  CharUnits Size = NVBaseType ? 
Layout.getNonVirtualSize()8.41k
:
Layout.getSize()140k
;
282
148k
  if (D->isUnion()) {
283
3.51k
    lowerUnion();
284
3.51k
    computeVolatileBitfields();
285
3.51k
    return;
286
3.51k
  }
287
144k
  accumulateFields();
288
  // RD implies C++.
289
144k
  if (RD) {
290
87.6k
    accumulateVPtrs();
291
87.6k
    accumulateBases();
292
87.6k
    if (Members.empty()) {
293
23.7k
      appendPaddingBytes(Size);
294
23.7k
      computeVolatileBitfields();
295
23.7k
      return;
296
23.7k
    }
297
63.9k
    if (!NVBaseType)
298
61.5k
      accumulateVBases();
299
63.9k
  }
300
121k
  llvm::stable_sort(Members);
301
121k
  Members.push_back(StorageInfo(Size, getIntNType(8)));
302
121k
  clipTailPadding();
303
121k
  determinePacked(NVBaseType);
304
121k
  insertPadding();
305
121k
  Members.pop_back();
306
121k
  calculateZeroInit();
307
121k
  fillOutputFields();
308
121k
  computeVolatileBitfields();
309
121k
}
310
311
3.51k
void CGRecordLowering::lowerUnion() {
312
3.51k
  CharUnits LayoutSize = Layout.getSize();
313
3.51k
  llvm::Type *StorageType = nullptr;
314
3.51k
  bool SeenNamedMember = false;
315
  // Iterate through the fields setting bitFieldInfo and the Fields array. Also
316
  // locate the "most appropriate" storage type.  The heuristic for finding the
317
  // storage type isn't necessary, the first (non-0-length-bitfield) field's
318
  // type would work fine and be simpler but would be different than what we've
319
  // been doing and cause lit tests to change.
320
7.87k
  for (const auto *Field : D->fields()) {
321
7.87k
    if (Field->isBitField()) {
322
120
      if (Field->isZeroLengthBitField(Context))
323
12
        continue;
324
108
      llvm::Type *FieldType = getStorageType(Field);
325
108
      if (LayoutSize < getSize(FieldType))
326
1
        FieldType = getByteArrayType(LayoutSize);
327
108
      setBitFieldInfo(Field, CharUnits::Zero(), FieldType);
328
108
    }
329
7.86k
    Fields[Field->getCanonicalDecl()] = 0;
330
7.86k
    llvm::Type *FieldType = getStorageType(Field);
331
    // Compute zero-initializable status.
332
    // This union might not be zero initialized: it may contain a pointer to
333
    // data member which might have some exotic initialization sequence.
334
    // If this is the case, then we aught not to try and come up with a "better"
335
    // type, it might not be very easy to come up with a Constant which
336
    // correctly initializes it.
337
7.86k
    if (!SeenNamedMember) {
338
3.87k
      SeenNamedMember = Field->getIdentifier();
339
3.87k
      if (!SeenNamedMember)
340
837
        if (const auto *FieldRD = Field->getType()->getAsRecordDecl())
341
20
          SeenNamedMember = FieldRD->findFirstNamedDataMember();
342
3.87k
      if (SeenNamedMember && 
!isZeroInitializable(Field)3.05k
) {
343
5
        IsZeroInitializable = IsZeroInitializableAsBase = false;
344
5
        StorageType = FieldType;
345
5
      }
346
3.87k
    }
347
    // Because our union isn't zero initializable, we won't be getting a better
348
    // storage type.
349
7.86k
    if (!IsZeroInitializable)
350
7
      continue;
351
    // Conditionally update our storage type if we've got a new "better" one.
352
7.85k
    if (!StorageType ||
353
7.85k
        
getAlignment(FieldType) > getAlignment(StorageType)4.39k
||
354
7.85k
        
(3.25k
getAlignment(FieldType) == getAlignment(StorageType)3.25k
&&
355
3.25k
        
getSize(FieldType) > getSize(StorageType)2.82k
))
356
4.64k
      StorageType = FieldType;
357
7.85k
  }
358
  // If we have no storage type just pad to the appropriate size and return.
359
3.51k
  if (!StorageType)
360
42
    return appendPaddingBytes(LayoutSize);
361
  // If our storage size was bigger than our required size (can happen in the
362
  // case of packed bitfields on Itanium) then just use an I8 array.
363
3.47k
  if (LayoutSize < getSize(StorageType))
364
1
    StorageType = getByteArrayType(LayoutSize);
365
3.47k
  FieldTypes.push_back(StorageType);
366
3.47k
  appendPaddingBytes(LayoutSize - getSize(StorageType));
367
  // Set packed if we need it.
368
3.47k
  if (LayoutSize % getAlignment(StorageType))
369
4
    Packed = true;
370
3.47k
}
371
372
144k
void CGRecordLowering::accumulateFields() {
373
144k
  for (RecordDecl::field_iterator Field = D->field_begin(),
374
144k
                                  FieldEnd = D->field_end();
375
419k
    Field != FieldEnd;) {
376
274k
    if (Field->isBitField()) {
377
3.06k
      RecordDecl::field_iterator Start = Field;
378
      // Iterate to gather the list of bitfields.
379
10.4k
      for (++Field; Field != FieldEnd && 
Field->isBitField()7.94k
;
++Field7.35k
)
;7.35k
380
3.06k
      accumulateBitFields(Start, Field);
381
271k
    } else if (!Field->isZeroSize(Context)) {
382
271k
      Members.push_back(MemberInfo(
383
271k
          bitsToCharUnits(getFieldBitOffset(*Field)), MemberInfo::Field,
384
271k
          getStorageType(*Field), *Field));
385
271k
      ++Field;
386
271k
    } else {
387
88
      ++Field;
388
88
    }
389
274k
  }
390
144k
}
391
392
void
393
CGRecordLowering::accumulateBitFields(RecordDecl::field_iterator Field,
394
3.06k
                                      RecordDecl::field_iterator FieldEnd) {
395
  // Run stores the first element of the current run of bitfields.  FieldEnd is
396
  // used as a special value to note that we don't have a current run.  A
397
  // bitfield run is a contiguous collection of bitfields that can be stored in
398
  // the same storage block.  Zero-sized bitfields and bitfields that would
399
  // cross an alignment boundary break a run and start a new one.
400
3.06k
  RecordDecl::field_iterator Run = FieldEnd;
401
  // Tail is the offset of the first bit off the end of the current run.  It's
402
  // used to determine if the ASTRecordLayout is treating these two bitfields as
403
  // contiguous.  StartBitOffset is offset of the beginning of the Run.
404
3.06k
  uint64_t StartBitOffset, Tail = 0;
405
3.06k
  if (isDiscreteBitFieldABI()) {
406
320
    for (; Field != FieldEnd; 
++Field223
) {
407
223
      uint64_t BitOffset = getFieldBitOffset(*Field);
408
      // Zero-width bitfields end runs.
409
223
      if (Field->isZeroLengthBitField(Context)) {
410
106
        Run = FieldEnd;
411
106
        continue;
412
106
      }
413
117
      llvm::Type *Type =
414
117
          Types.ConvertTypeForMem(Field->getType(), /*ForBitFields=*/true);
415
      // If we don't have a run yet, or don't live within the previous run's
416
      // allocated storage then we allocate some storage and start a new run.
417
117
      if (Run == FieldEnd || 
BitOffset >= Tail62
) {
418
84
        Run = Field;
419
84
        StartBitOffset = BitOffset;
420
84
        Tail = StartBitOffset + DataLayout.getTypeAllocSizeInBits(Type);
421
        // Add the storage member to the record.  This must be added to the
422
        // record before the bitfield members so that it gets laid out before
423
        // the bitfields it contains get laid out.
424
84
        Members.push_back(StorageInfo(bitsToCharUnits(StartBitOffset), Type));
425
84
      }
426
      // Bitfields get the offset of their storage but come afterward and remain
427
      // there after a stable sort.
428
117
      Members.push_back(MemberInfo(bitsToCharUnits(StartBitOffset),
429
117
                                   MemberInfo::Field, nullptr, *Field));
430
117
    }
431
97
    return;
432
97
  }
433
434
  // Check if OffsetInRecord (the size in bits of the current run) is better
435
  // as a single field run. When OffsetInRecord has legal integer width, and
436
  // its bitfield offset is naturally aligned, it is better to make the
437
  // bitfield a separate storage component so as it can be accessed directly
438
  // with lower cost.
439
2.97k
  auto IsBetterAsSingleFieldRun = [&](uint64_t OffsetInRecord,
440
10.1k
                                      uint64_t StartBitOffset) {
441
10.1k
    if (!Types.getCodeGenOpts().FineGrainedBitfieldAccesses)
442
10.0k
      return false;
443
35
    if (OffsetInRecord < 8 || 
!llvm::isPowerOf2_64(OffsetInRecord)30
||
444
35
        
!DataLayout.fitsInLegalInteger(OffsetInRecord)12
)
445
23
      return false;
446
    // Make sure StartBitOffset is naturally aligned if it is treated as an
447
    // IType integer.
448
12
    if (StartBitOffset %
449
12
            Context.toBits(getAlignment(getIntNType(OffsetInRecord))) !=
450
12
        0)
451
0
      return false;
452
12
    return true;
453
12
  };
454
455
  // The start field is better as a single field run.
456
2.97k
  bool StartFieldAsSingleRun = false;
457
16.1k
  for (;;) {
458
    // Check to see if we need to start a new run.
459
16.1k
    if (Run == FieldEnd) {
460
      // If we're out of fields, return.
461
6.11k
      if (Field == FieldEnd)
462
2.97k
        break;
463
      // Any non-zero-length bitfield can start a new run.
464
3.14k
      if (!Field->isZeroLengthBitField(Context)) {
465
2.95k
        Run = Field;
466
2.95k
        StartBitOffset = getFieldBitOffset(*Field);
467
2.95k
        Tail = StartBitOffset + Field->getBitWidthValue(Context);
468
2.95k
        StartFieldAsSingleRun = IsBetterAsSingleFieldRun(Tail - StartBitOffset,
469
2.95k
                                                         StartBitOffset);
470
2.95k
      }
471
3.14k
      ++Field;
472
3.14k
      continue;
473
6.11k
    }
474
475
    // If the start field of a new run is better as a single run, or
476
    // if current field (or consecutive fields) is better as a single run, or
477
    // if current field has zero width bitfield and either
478
    // UseZeroLengthBitfieldAlignment or UseBitFieldTypeAlignment is set to
479
    // true, or
480
    // if the offset of current field is inconsistent with the offset of
481
    // previous field plus its offset,
482
    // skip the block below and go ahead to emit the storage.
483
    // Otherwise, try to add bitfields to the run.
484
10.0k
    if (!StartFieldAsSingleRun && 
Field != FieldEnd10.0k
&&
485
10.0k
        
!IsBetterAsSingleFieldRun(Tail - StartBitOffset, StartBitOffset)7.16k
&&
486
10.0k
        
(7.16k
!Field->isZeroLengthBitField(Context)7.16k
||
487
7.16k
         
(50
!Context.getTargetInfo().useZeroLengthBitfieldAlignment()50
&&
488
50
          
!Context.getTargetInfo().useBitFieldTypeAlignment()23
)) &&
489
10.0k
        
Tail == getFieldBitOffset(*Field)7.11k
) {
490
7.05k
      Tail += Field->getBitWidthValue(Context);
491
7.05k
      ++Field;
492
7.05k
      continue;
493
7.05k
    }
494
495
    // We've hit a break-point in the run and need to emit a storage field.
496
2.95k
    llvm::Type *Type = getIntNType(Tail - StartBitOffset);
497
    // Add the storage member to the record and set the bitfield info for all of
498
    // the bitfields in the run.  Bitfields get the offset of their storage but
499
    // come afterward and remain there after a stable sort.
500
2.95k
    Members.push_back(StorageInfo(bitsToCharUnits(StartBitOffset), Type));
501
12.9k
    for (; Run != Field; 
++Run10.0k
)
502
10.0k
      Members.push_back(MemberInfo(bitsToCharUnits(StartBitOffset),
503
10.0k
                                   MemberInfo::Field, nullptr, *Run));
504
2.95k
    Run = FieldEnd;
505
2.95k
    StartFieldAsSingleRun = false;
506
2.95k
  }
507
2.97k
}
508
509
87.6k
void CGRecordLowering::accumulateBases() {
510
  // If we've got a primary virtual base, we need to add it with the bases.
511
87.6k
  if (Layout.isPrimaryBaseVirtual()) {
512
174
    const CXXRecordDecl *BaseDecl = Layout.getPrimaryBase();
513
174
    Members.push_back(MemberInfo(CharUnits::Zero(), MemberInfo::Base,
514
174
                                 getStorageType(BaseDecl), BaseDecl));
515
174
  }
516
  // Accumulate the non-virtual bases.
517
87.6k
  for (const auto &Base : RD->bases()) {
518
21.2k
    if (Base.isVirtual())
519
1.53k
      continue;
520
521
    // Bases can be zero-sized even if not technically empty if they
522
    // contain only a trailing array member.
523
19.6k
    const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
524
19.6k
    if (!BaseDecl->isEmpty() &&
525
19.6k
        
!Context.getASTRecordLayout(BaseDecl).getNonVirtualSize().isZero()11.2k
)
526
10.2k
      Members.push_back(MemberInfo(Layout.getBaseClassOffset(BaseDecl),
527
10.2k
          MemberInfo::Base, getStorageType(BaseDecl), BaseDecl));
528
19.6k
  }
529
87.6k
}
530
531
/// The AAPCS that defines that, when possible, bit-fields should
532
/// be accessed using containers of the declared type width:
533
/// When a volatile bit-field is read, and its container does not overlap with
534
/// any non-bit-field member or any zero length bit-field member, its container
535
/// must be read exactly once using the access width appropriate to the type of
536
/// the container. When a volatile bit-field is written, and its container does
537
/// not overlap with any non-bit-field member or any zero-length bit-field
538
/// member, its container must be read exactly once and written exactly once
539
/// using the access width appropriate to the type of the container. The two
540
/// accesses are not atomic.
541
///
542
/// Enforcing the width restriction can be disabled using
543
/// -fno-aapcs-bitfield-width.
544
148k
void CGRecordLowering::computeVolatileBitfields() {
545
148k
  if (!isAAPCS() || 
!Types.getCodeGenOpts().AAPCSBitfieldWidth2.61k
)
546
145k
    return;
547
548
2.52k
  for (auto &I : BitFields) {
549
464
    const FieldDecl *Field = I.first;
550
464
    CGBitFieldInfo &Info = I.second;
551
464
    llvm::Type *ResLTy = Types.ConvertTypeForMem(Field->getType());
552
    // If the record alignment is less than the type width, we can't enforce a
553
    // aligned load, bail out.
554
464
    if ((uint64_t)(Context.toBits(Layout.getAlignment())) <
555
464
        ResLTy->getPrimitiveSizeInBits())
556
12
      continue;
557
    // CGRecordLowering::setBitFieldInfo() pre-adjusts the bit-field offsets
558
    // for big-endian targets, but it assumes a container of width
559
    // Info.StorageSize. Since AAPCS uses a different container size (width
560
    // of the type), we first undo that calculation here and redo it once
561
    // the bit-field offset within the new container is calculated.
562
452
    const unsigned OldOffset =
563
452
        isBE() ? 
Info.StorageSize - (Info.Offset + Info.Size)189
:
Info.Offset263
;
564
    // Offset to the bit-field from the beginning of the struct.
565
452
    const unsigned AbsoluteOffset =
566
452
        Context.toBits(Info.StorageOffset) + OldOffset;
567
568
    // Container size is the width of the bit-field type.
569
452
    const unsigned StorageSize = ResLTy->getPrimitiveSizeInBits();
570
    // Nothing to do if the access uses the desired
571
    // container width and is naturally aligned.
572
452
    if (Info.StorageSize == StorageSize && 
(OldOffset % StorageSize == 0)267
)
573
109
      continue;
574
575
    // Offset within the container.
576
343
    unsigned Offset = AbsoluteOffset & (StorageSize - 1);
577
    // Bail out if an aligned load of the container cannot cover the entire
578
    // bit-field. This can happen for example, if the bit-field is part of a
579
    // packed struct. AAPCS does not define access rules for such cases, we let
580
    // clang to follow its own rules.
581
343
    if (Offset + Info.Size > StorageSize)
582
4
      continue;
583
584
    // Re-adjust offsets for big-endian targets.
585
339
    if (isBE())
586
142
      Offset = StorageSize - (Offset + Info.Size);
587
588
339
    const CharUnits StorageOffset =
589
339
        Context.toCharUnitsFromBits(AbsoluteOffset & ~(StorageSize - 1));
590
339
    const CharUnits End = StorageOffset +
591
339
                          Context.toCharUnitsFromBits(StorageSize) -
592
339
                          CharUnits::One();
593
594
339
    const ASTRecordLayout &Layout =
595
339
        Context.getASTRecordLayout(Field->getParent());
596
    // If we access outside memory outside the record, than bail out.
597
339
    const CharUnits RecordSize = Layout.getSize();
598
339
    if (End >= RecordSize)
599
0
      continue;
600
601
    // Bail out if performing this load would access non-bit-fields members.
602
339
    bool Conflict = false;
603
1.11k
    for (const auto *F : D->fields()) {
604
      // Allow sized bit-fields overlaps.
605
1.11k
      if (F->isBitField() && 
!F->isZeroLengthBitField(Context)973
)
606
950
        continue;
607
608
161
      const CharUnits FOffset = Context.toCharUnitsFromBits(
609
161
          Layout.getFieldOffset(F->getFieldIndex()));
610
611
      // As C11 defines, a zero sized bit-field defines a barrier, so
612
      // fields after and before it should be race condition free.
613
      // The AAPCS acknowledges it and imposes no restritions when the
614
      // natural container overlaps a zero-length bit-field.
615
161
      if (F->isZeroLengthBitField(Context)) {
616
23
        if (End > FOffset && 
StorageOffset < FOffset12
) {
617
8
          Conflict = true;
618
8
          break;
619
8
        }
620
23
      }
621
622
153
      const CharUnits FEnd =
623
153
          FOffset +
624
153
          Context.toCharUnitsFromBits(
625
153
              Types.ConvertTypeForMem(F->getType())->getPrimitiveSizeInBits()) -
626
153
          CharUnits::One();
627
      // If no overlap, continue.
628
153
      if (End < FOffset || 
FEnd < StorageOffset134
)
629
103
        continue;
630
631
      // The desired load overlaps a non-bit-field member, bail out.
632
50
      Conflict = true;
633
50
      break;
634
153
    }
635
636
339
    if (Conflict)
637
58
      continue;
638
    // Write the new bit-field access parameters.
639
    // As the storage offset now is defined as the number of elements from the
640
    // start of the structure, we should divide the Offset by the element size.
641
281
    Info.VolatileStorageOffset =
642
281
        StorageOffset / Context.toCharUnitsFromBits(StorageSize).getQuantity();
643
281
    Info.VolatileStorageSize = StorageSize;
644
281
    Info.VolatileOffset = Offset;
645
281
  }
646
2.52k
}
647
648
87.6k
void CGRecordLowering::accumulateVPtrs() {
649
87.6k
  if (Layout.hasOwnVFPtr())
650
3.89k
    Members.push_back(MemberInfo(CharUnits::Zero(), MemberInfo::VFPtr,
651
3.89k
        llvm::FunctionType::get(getIntNType(32), /*isVarArg=*/true)->
652
3.89k
            getPointerTo()->getPointerTo()));
653
87.6k
  if (Layout.hasOwnVBPtr())
654
603
    Members.push_back(MemberInfo(Layout.getVBPtrOffset(), MemberInfo::VBPtr,
655
603
        llvm::Type::getInt32PtrTy(Types.getLLVMContext())));
656
87.6k
}
657
658
61.5k
void CGRecordLowering::accumulateVBases() {
659
61.5k
  CharUnits ScissorOffset = Layout.getNonVirtualSize();
660
  // In the itanium ABI, it's possible to place a vbase at a dsize that is
661
  // smaller than the nvsize.  Here we check to see if such a base is placed
662
  // before the nvsize and set the scissor offset to that, instead of the
663
  // nvsize.
664
61.5k
  if (isOverlappingVBaseABI())
665
59.6k
    for (const auto &Base : RD->vbases()) {
666
690
      const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
667
690
      if (BaseDecl->isEmpty())
668
86
        continue;
669
      // If the vbase is a primary virtual base of some base, then it doesn't
670
      // get its own storage location but instead lives inside of that base.
671
604
      if (Context.isNearlyEmpty(BaseDecl) && 
!hasOwnStorage(RD, BaseDecl)253
)
672
227
        continue;
673
377
      ScissorOffset = std::min(ScissorOffset,
674
377
                               Layout.getVBaseClassOffset(BaseDecl));
675
377
    }
676
61.5k
  Members.push_back(MemberInfo(ScissorOffset, MemberInfo::Scissor, nullptr,
677
61.5k
                               RD));
678
61.5k
  for (const auto &Base : RD->vbases()) {
679
1.34k
    const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
680
1.34k
    if (BaseDecl->isEmpty())
681
153
      continue;
682
1.18k
    CharUnits Offset = Layout.getVBaseClassOffset(BaseDecl);
683
    // If the vbase is a primary virtual base of some base, then it doesn't
684
    // get its own storage location but instead lives inside of that base.
685
1.18k
    if (isOverlappingVBaseABI() &&
686
1.18k
        
Context.isNearlyEmpty(BaseDecl)604
&&
687
1.18k
        
!hasOwnStorage(RD, BaseDecl)253
) {
688
227
      Members.push_back(MemberInfo(Offset, MemberInfo::VBase, nullptr,
689
227
                                   BaseDecl));
690
227
      continue;
691
227
    }
692
    // If we've got a vtordisp, add it as a storage type.
693
962
    if (Layout.getVBaseOffsetsMap().find(BaseDecl)->second.hasVtorDisp())
694
89
      Members.push_back(StorageInfo(Offset - CharUnits::fromQuantity(4),
695
89
                                    getIntNType(32)));
696
962
    Members.push_back(MemberInfo(Offset, MemberInfo::VBase,
697
962
                                 getStorageType(BaseDecl), BaseDecl));
698
962
  }
699
61.5k
}
700
701
bool CGRecordLowering::hasOwnStorage(const CXXRecordDecl *Decl,
702
982
                                     const CXXRecordDecl *Query) {
703
982
  const ASTRecordLayout &DeclLayout = Context.getASTRecordLayout(Decl);
704
982
  if (DeclLayout.isPrimaryBaseVirtual() && 
DeclLayout.getPrimaryBase() == Query588
)
705
454
    return false;
706
528
  for (const auto &Base : Decl->bases())
707
476
    if (!hasOwnStorage(Base.getType()->getAsCXXRecordDecl(), Query))
708
190
      return false;
709
338
  return true;
710
528
}
711
712
121k
void CGRecordLowering::calculateZeroInit() {
713
121k
  for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
714
121k
                                               MemberEnd = Members.end();
715
486k
       IsZeroInitializableAsBase && 
Member != MemberEnd486k
;
++Member365k
) {
716
365k
    if (Member->Kind == MemberInfo::Field) {
717
287k
      if (!Member->FD || 
isZeroInitializable(Member->FD)281k
)
718
287k
        continue;
719
69
      IsZeroInitializable = IsZeroInitializableAsBase = false;
720
77.5k
    } else if (Member->Kind == MemberInfo::Base ||
721
77.5k
               
Member->Kind == MemberInfo::VBase67.1k
) {
722
11.6k
      if (isZeroInitializable(Member->RD))
723
11.6k
        continue;
724
16
      IsZeroInitializable = false;
725
16
      if (Member->Kind == MemberInfo::Base)
726
14
        IsZeroInitializableAsBase = false;
727
16
    }
728
365k
  }
729
121k
}
730
731
121k
void CGRecordLowering::clipTailPadding() {
732
121k
  std::vector<MemberInfo>::iterator Prior = Members.begin();
733
121k
  CharUnits Tail = getSize(Prior->Data);
734
121k
  for (std::vector<MemberInfo>::iterator Member = Prior + 1,
735
121k
                                         MemberEnd = Members.end();
736
483k
       Member != MemberEnd; 
++Member362k
) {
737
    // Only members with data and the scissor can cut into tail padding.
738
362k
    if (!Member->Data && 
Member->Kind != MemberInfo::Scissor71.8k
)
739
10.3k
      continue;
740
352k
    if (Member->Offset < Tail) {
741
198
      assert(Prior->Kind == MemberInfo::Field &&
742
198
             "Only storage fields have tail padding!");
743
198
      if (!Prior->FD || 
Prior->FD->isBitField()12
)
744
186
        Prior->Data = getByteArrayType(bitsToCharUnits(llvm::alignTo(
745
186
            cast<llvm::IntegerType>(Prior->Data)->getIntegerBitWidth(), 8)));
746
12
      else {
747
12
        assert(Prior->FD->hasAttr<NoUniqueAddressAttr>() &&
748
12
               "should not have reused this field's tail padding");
749
0
        Prior->Data = getByteArrayType(
750
12
            Context.getTypeInfoDataSizeInChars(Prior->FD->getType()).Width);
751
12
      }
752
198
    }
753
352k
    if (Member->Data)
754
290k
      Prior = Member;
755
352k
    Tail = Prior->Offset + getSize(Prior->Data);
756
352k
  }
757
121k
}
758
759
121k
void CGRecordLowering::determinePacked(bool NVBaseType) {
760
121k
  if (Packed)
761
1.30k
    return;
762
119k
  CharUnits Alignment = CharUnits::One();
763
119k
  CharUnits NVAlignment = CharUnits::One();
764
119k
  CharUnits NVSize =
765
119k
      !NVBaseType && 
RD118k
?
Layout.getNonVirtualSize()61.5k
:
CharUnits::Zero()58.4k
;
766
119k
  for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
767
119k
                                               MemberEnd = Members.end();
768
599k
       Member != MemberEnd; 
++Member479k
) {
769
479k
    if (!Member->Data)
770
71.8k
      continue;
771
    // If any member falls at an offset that it not a multiple of its alignment,
772
    // then the entire record must be packed.
773
407k
    if (Member->Offset % getAlignment(Member->Data))
774
134
      Packed = true;
775
407k
    if (Member->Offset < NVSize)
776
165k
      NVAlignment = std::max(NVAlignment, getAlignment(Member->Data));
777
407k
    Alignment = std::max(Alignment, getAlignment(Member->Data));
778
407k
  }
779
  // If the size of the record (the capstone's offset) is not a multiple of the
780
  // record's alignment, it must be packed.
781
119k
  if (Members.back().Offset % Alignment)
782
2.54k
    Packed = true;
783
  // If the non-virtual sub-object is not a multiple of the non-virtual
784
  // sub-object's alignment, it must be packed.  We cannot have a packed
785
  // non-virtual sub-object and an unpacked complete object or vise versa.
786
119k
  if (NVSize % NVAlignment)
787
3.76k
    Packed = true;
788
  // Update the alignment of the sentinel.
789
119k
  if (!Packed)
790
116k
    Members.back().Data = getIntNType(Context.toBits(Alignment));
791
119k
}
792
793
121k
void CGRecordLowering::insertPadding() {
794
121k
  std::vector<std::pair<CharUnits, CharUnits> > Padding;
795
121k
  CharUnits Size = CharUnits::Zero();
796
121k
  for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
797
121k
                                               MemberEnd = Members.end();
798
605k
       Member != MemberEnd; 
++Member483k
) {
799
483k
    if (!Member->Data)
800
71.8k
      continue;
801
412k
    CharUnits Offset = Member->Offset;
802
412k
    assert(Offset >= Size);
803
    // Insert padding if we need to.
804
412k
    if (Offset !=
805
412k
        Size.alignTo(Packed ? 
CharUnits::One()17.1k
:
getAlignment(Member->Data)394k
))
806
2.71k
      Padding.push_back(std::make_pair(Size, Offset - Size));
807
412k
    Size = Offset + getSize(Member->Data);
808
412k
  }
809
121k
  if (Padding.empty())
810
118k
    return;
811
  // Add the padding to the Members list and sort it.
812
2.49k
  for (std::vector<std::pair<CharUnits, CharUnits> >::const_iterator
813
2.49k
        Pad = Padding.begin(), PadEnd = Padding.end();
814
5.20k
        Pad != PadEnd; 
++Pad2.71k
)
815
2.71k
    Members.push_back(StorageInfo(Pad->first, getByteArrayType(Pad->second)));
816
2.49k
  llvm::stable_sort(Members);
817
2.49k
}
818
819
121k
void CGRecordLowering::fillOutputFields() {
820
121k
  for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
821
121k
                                               MemberEnd = Members.end();
822
486k
       Member != MemberEnd; 
++Member365k
) {
823
365k
    if (Member->Data)
824
293k
      FieldTypes.push_back(Member->Data);
825
365k
    if (Member->Kind == MemberInfo::Field) {
826
287k
      if (Member->FD)
827
281k
        Fields[Member->FD->getCanonicalDecl()] = FieldTypes.size() - 1;
828
      // A field without storage must be a bitfield.
829
287k
      if (!Member->Data)
830
10.1k
        setBitFieldInfo(Member->FD, Member->Offset, FieldTypes.back());
831
287k
    } else 
if (77.6k
Member->Kind == MemberInfo::Base77.6k
)
832
10.4k
      NonVirtualBases[Member->RD] = FieldTypes.size() - 1;
833
67.2k
    else if (Member->Kind == MemberInfo::VBase)
834
1.18k
      VirtualBases[Member->RD] = FieldTypes.size() - 1;
835
365k
  }
836
121k
}
837
838
CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
839
                                        const FieldDecl *FD,
840
                                        uint64_t Offset, uint64_t Size,
841
                                        uint64_t StorageSize,
842
130
                                        CharUnits StorageOffset) {
843
  // This function is vestigial from CGRecordLayoutBuilder days but is still
844
  // used in GCObjCRuntime.cpp.  That usage has a "fixme" attached to it that
845
  // when addressed will allow for the removal of this function.
846
130
  llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType());
847
130
  CharUnits TypeSizeInBytes =
848
130
    CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(Ty));
849
130
  uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
850
851
130
  bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
852
853
130
  if (Size > TypeSizeInBits) {
854
    // We have a wide bit-field. The extra bits are only used for padding, so
855
    // if we have a bitfield of type T, with size N:
856
    //
857
    // T t : N;
858
    //
859
    // We can just assume that it's:
860
    //
861
    // T t : sizeof(T);
862
    //
863
0
    Size = TypeSizeInBits;
864
0
  }
865
866
  // Reverse the bit offsets for big endian machines. Because we represent
867
  // a bitfield as a single large integer load, we can imagine the bits
868
  // counting from the most-significant-bit instead of the
869
  // least-significant-bit.
870
130
  if (Types.getDataLayout().isBigEndian()) {
871
0
    Offset = StorageSize - (Offset + Size);
872
0
  }
873
874
130
  return CGBitFieldInfo(Offset, Size, IsSigned, StorageSize, StorageOffset);
875
130
}
876
877
std::unique_ptr<CGRecordLayout>
878
140k
CodeGenTypes::ComputeRecordLayout(const RecordDecl *D, llvm::StructType *Ty) {
879
140k
  CGRecordLowering Builder(*this, D, /*Packed=*/false);
880
881
140k
  Builder.lower(/*NonVirtualBaseType=*/false);
882
883
  // If we're in C++, compute the base subobject type.
884
140k
  llvm::StructType *BaseTy = nullptr;
885
140k
  if (isa<CXXRecordDecl>(D) && 
!D->isUnion()82.2k
&&
!D->hasAttr<FinalAttr>()79.1k
) {
886
79.1k
    BaseTy = Ty;
887
79.1k
    if (Builder.Layout.getNonVirtualSize() != Builder.Layout.getSize()) {
888
8.41k
      CGRecordLowering BaseBuilder(*this, D, /*Packed=*/Builder.Packed);
889
8.41k
      BaseBuilder.lower(/*NonVirtualBaseType=*/true);
890
8.41k
      BaseTy = llvm::StructType::create(
891
8.41k
          getLLVMContext(), BaseBuilder.FieldTypes, "", BaseBuilder.Packed);
892
8.41k
      addRecordTypeName(D, BaseTy, ".base");
893
      // BaseTy and Ty must agree on their packedness for getLLVMFieldNo to work
894
      // on both of them with the same index.
895
8.41k
      assert(Builder.Packed == BaseBuilder.Packed &&
896
8.41k
             "Non-virtual and complete types must agree on packedness");
897
8.41k
    }
898
79.1k
  }
899
900
  // Fill in the struct *after* computing the base type.  Filling in the body
901
  // signifies that the type is no longer opaque and record layout is complete,
902
  // but we may need to recursively layout D while laying D out as a base type.
903
0
  Ty->setBody(Builder.FieldTypes, Builder.Packed);
904
905
140k
  auto RL = std::make_unique<CGRecordLayout>(
906
140k
      Ty, BaseTy, (bool)Builder.IsZeroInitializable,
907
140k
      (bool)Builder.IsZeroInitializableAsBase);
908
909
140k
  RL->NonVirtualBases.swap(Builder.NonVirtualBases);
910
140k
  RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
911
912
  // Add all the field numbers.
913
140k
  RL->FieldInfo.swap(Builder.Fields);
914
915
  // Add bitfield info.
916
140k
  RL->BitFields.swap(Builder.BitFields);
917
918
  // Dump the layout, if requested.
919
140k
  if (getContext().getLangOpts().DumpRecordLayouts) {
920
73
    llvm::outs() << "\n*** Dumping IRgen Record Layout\n";
921
73
    llvm::outs() << "Record: ";
922
73
    D->dump(llvm::outs());
923
73
    llvm::outs() << "\nLayout: ";
924
73
    RL->print(llvm::outs());
925
73
  }
926
927
140k
#ifndef NDEBUG
928
  // Verify that the computed LLVM struct size matches the AST layout size.
929
140k
  const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
930
931
140k
  uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
932
140k
  assert(TypeSizeInBits == getDataLayout().getTypeAllocSizeInBits(Ty) &&
933
140k
         "Type size mismatch!");
934
935
140k
  if (BaseTy) {
936
79.1k
    CharUnits NonVirtualSize  = Layout.getNonVirtualSize();
937
938
79.1k
    uint64_t AlignedNonVirtualTypeSizeInBits =
939
79.1k
      getContext().toBits(NonVirtualSize);
940
941
79.1k
    assert(AlignedNonVirtualTypeSizeInBits ==
942
79.1k
           getDataLayout().getTypeAllocSizeInBits(BaseTy) &&
943
79.1k
           "Type size mismatch!");
944
79.1k
  }
945
946
  // Verify that the LLVM and AST field offsets agree.
947
0
  llvm::StructType *ST = RL->getLLVMType();
948
140k
  const llvm::StructLayout *SL = getDataLayout().getStructLayout(ST);
949
950
140k
  const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
951
140k
  RecordDecl::field_iterator it = D->field_begin();
952
426k
  for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; 
++i, ++it286k
) {
953
286k
    const FieldDecl *FD = *it;
954
955
    // Ignore zero-sized fields.
956
286k
    if (FD->isZeroSize(getContext()))
957
367
      continue;
958
959
    // For non-bit-fields, just check that the LLVM struct offset matches the
960
    // AST offset.
961
286k
    if (!FD->isBitField()) {
962
276k
      unsigned FieldNo = RL->getLLVMFieldNo(FD);
963
276k
      assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
964
276k
             "Invalid field offset!");
965
0
      continue;
966
276k
    }
967
968
    // Ignore unnamed bit-fields.
969
10.1k
    if (!FD->getDeclName())
970
548
      continue;
971
972
9.57k
    const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
973
9.57k
    llvm::Type *ElementTy = ST->getTypeAtIndex(RL->getLLVMFieldNo(FD));
974
975
    // Unions have overlapping elements dictating their layout, but for
976
    // non-unions we can verify that this section of the layout is the exact
977
    // expected size.
978
9.57k
    if (D->isUnion()) {
979
      // For unions we verify that the start is zero and the size
980
      // is in-bounds. However, on BE systems, the offset may be non-zero, but
981
      // the size + offset should match the storage size in that case as it
982
      // "starts" at the back.
983
85
      if (getDataLayout().isBigEndian())
984
11
        assert(static_cast<unsigned>(Info.Offset + Info.Size) ==
985
85
               Info.StorageSize &&
986
85
               "Big endian union bitfield does not end at the back");
987
74
      else
988
74
        assert(Info.Offset == 0 &&
989
85
               "Little endian union bitfield with a non-zero offset");
990
0
      assert(Info.StorageSize <= SL->getSizeInBits() &&
991
85
             "Union not large enough for bitfield storage");
992
9.48k
    } else {
993
9.48k
      assert((Info.StorageSize ==
994
9.48k
                  getDataLayout().getTypeAllocSizeInBits(ElementTy) ||
995
9.48k
              Info.VolatileStorageSize ==
996
9.48k
                  getDataLayout().getTypeAllocSizeInBits(ElementTy)) &&
997
9.48k
             "Storage size does not match the element type size");
998
9.48k
    }
999
0
    assert(Info.Size > 0 && "Empty bitfield!");
1000
0
    assert(static_cast<unsigned>(Info.Offset) + Info.Size <= Info.StorageSize &&
1001
9.57k
           "Bitfield outside of its allocated storage");
1002
9.57k
  }
1003
140k
#endif
1004
1005
140k
  return RL;
1006
140k
}
1007
1008
73
void CGRecordLayout::print(raw_ostream &OS) const {
1009
73
  OS << "<CGRecordLayout\n";
1010
73
  OS << "  LLVMType:" << *CompleteObjectType << "\n";
1011
73
  if (BaseSubobjectType)
1012
36
    OS << "  NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
1013
73
  OS << "  IsZeroInitializable:" << IsZeroInitializable << "\n";
1014
73
  OS << "  BitFields:[\n";
1015
1016
  // Print bit-field infos in declaration order.
1017
73
  std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
1018
73
  for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
1019
73
         it = BitFields.begin(), ie = BitFields.end();
1020
120
       it != ie; 
++it47
) {
1021
47
    const RecordDecl *RD = it->first->getParent();
1022
47
    unsigned Index = 0;
1023
47
    for (RecordDecl::field_iterator
1024
121
           it2 = RD->field_begin(); *it2 != it->first; 
++it274
)
1025
74
      ++Index;
1026
47
    BFIs.push_back(std::make_pair(Index, &it->second));
1027
47
  }
1028
73
  llvm::array_pod_sort(BFIs.begin(), BFIs.end());
1029
120
  for (unsigned i = 0, e = BFIs.size(); i != e; 
++i47
) {
1030
47
    OS.indent(4);
1031
47
    BFIs[i].second->print(OS);
1032
47
    OS << "\n";
1033
47
  }
1034
1035
73
  OS << "]>\n";
1036
73
}
1037
1038
0
LLVM_DUMP_METHOD void CGRecordLayout::dump() const {
1039
0
  print(llvm::errs());
1040
0
}
1041
1042
47
void CGBitFieldInfo::print(raw_ostream &OS) const {
1043
47
  OS << "<CGBitFieldInfo"
1044
47
     << " Offset:" << Offset << " Size:" << Size << " IsSigned:" << IsSigned
1045
47
     << " StorageSize:" << StorageSize
1046
47
     << " StorageOffset:" << StorageOffset.getQuantity()
1047
47
     << " VolatileOffset:" << VolatileOffset
1048
47
     << " VolatileStorageSize:" << VolatileStorageSize
1049
47
     << " VolatileStorageOffset:" << VolatileStorageOffset.getQuantity() << ">";
1050
47
}
1051
1052
0
LLVM_DUMP_METHOD void CGBitFieldInfo::dump() const {
1053
0
  print(llvm::errs());
1054
0
}