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