Coverage Report

Created: 2021-01-19 06:58

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