Coverage Report

Created: 2020-02-25 14:32

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