Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 1 | //===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===// |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 10 | // Builder implementation for CGRecordLayout objects. |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 14 | #include "CGRecordLayout.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "CGCXXABI.h" |
| 16 | #include "CodeGenTypes.h" |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
| 18 | #include "clang/AST/Attr.h" |
Anders Carlsson | 4131f00 | 2010-11-24 22:50:27 +0000 | [diff] [blame] | 19 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclCXX.h" |
| 21 | #include "clang/AST/Expr.h" |
| 22 | #include "clang/AST/RecordLayout.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/CodeGenOptions.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 24 | #include "llvm/IR/DataLayout.h" |
| 25 | #include "llvm/IR/DerivedTypes.h" |
| 26 | #include "llvm/IR/Type.h" |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MathExtras.h" |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | using namespace CodeGen; |
| 32 | |
John McCall | bcd3821 | 2010-11-30 23:17:27 +0000 | [diff] [blame] | 33 | namespace { |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 34 | /// The CGRecordLowering is responsible for lowering an ASTRecordLayout to an |
| 35 | /// llvm::Type. Some of the lowering is straightforward, some is not. Here we |
| 36 | /// detail some of the complexities and weirdnesses here. |
| 37 | /// * LLVM does not have unions - Unions can, in theory be represented by any |
| 38 | /// llvm::Type with correct size. We choose a field via a specific heuristic |
| 39 | /// and add padding if necessary. |
| 40 | /// * LLVM does not have bitfields - Bitfields are collected into contiguous |
| 41 | /// runs and allocated as a single storage type for the run. ASTRecordLayout |
| 42 | /// contains enough information to determine where the runs break. Microsoft |
| 43 | /// and Itanium follow different rules and use different codepaths. |
| 44 | /// * It is desired that, when possible, bitfields use the appropriate iN type |
| 45 | /// when lowered to llvm types. For example unsigned x : 24 gets lowered to |
| 46 | /// i24. This isn't always possible because i24 has storage size of 32 bit |
| 47 | /// and if it is possible to use that extra byte of padding we must use |
| 48 | /// [i8 x 3] instead of i24. The function clipTailPadding does this. |
| 49 | /// C++ examples that require clipping: |
| 50 | /// struct { int a : 24; char b; }; // a must be clipped, b goes at offset 3 |
| 51 | /// struct A { int a : 24; }; // a must be clipped because a struct like B |
| 52 | // could exist: struct B : A { char b; }; // b goes at offset 3 |
| 53 | /// * Clang ignores 0 sized bitfields and 0 sized bases but *not* zero sized |
| 54 | /// fields. The existing asserts suggest that LLVM assumes that *every* field |
| 55 | /// has an underlying storage type. Therefore empty structures containing |
| 56 | /// zero sized subobjects such as empty records or zero sized arrays still get |
| 57 | /// a zero sized (empty struct) storage type. |
| 58 | /// * Clang reads the complete type rather than the base type when generating |
| 59 | /// code to access fields. Bitfields in tail position with tail padding may |
| 60 | /// be clipped in the base class but not the complete class (we may discover |
| 61 | /// that the tail padding is not used in the complete class.) However, |
| 62 | /// because LLVM reads from the complete type it can generate incorrect code |
| 63 | /// if we do not clip the tail padding off of the bitfield in the complete |
| 64 | /// layout. This introduces a somewhat awkward extra unnecessary clip stage. |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 65 | /// The location of the clip is stored internally as a sentinel of type |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 66 | /// SCISSOR. If LLVM were updated to read base types (which it probably |
| 67 | /// should because locations of things such as VBases are bogus in the llvm |
| 68 | /// type anyway) then we could eliminate the SCISSOR. |
| 69 | /// * Itanium allows nearly empty primary virtual bases. These bases don't get |
| 70 | /// get their own storage because they're laid out as part of another base |
| 71 | /// or at the beginning of the structure. Determining if a VBase actually |
| 72 | /// gets storage awkwardly involves a walk of all bases. |
| 73 | /// * VFPtrs and VBPtrs do *not* make a record NotZeroInitializable. |
| 74 | struct CGRecordLowering { |
| 75 | // MemberInfo is a helper structure that contains information about a record |
| 76 | // member. In additional to the standard member types, there exists a |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 77 | // sentinel member type that ensures correct rounding. |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 78 | struct MemberInfo { |
| 79 | CharUnits Offset; |
| 80 | enum InfoKind { VFPtr, VBPtr, Field, Base, VBase, Scissor } Kind; |
| 81 | llvm::Type *Data; |
| 82 | union { |
| 83 | const FieldDecl *FD; |
| 84 | const CXXRecordDecl *RD; |
| 85 | }; |
| 86 | MemberInfo(CharUnits Offset, InfoKind Kind, llvm::Type *Data, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 87 | const FieldDecl *FD = nullptr) |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 88 | : Offset(Offset), Kind(Kind), Data(Data), FD(FD) {} |
| 89 | MemberInfo(CharUnits Offset, InfoKind Kind, llvm::Type *Data, |
| 90 | const CXXRecordDecl *RD) |
| 91 | : Offset(Offset), Kind(Kind), Data(Data), RD(RD) {} |
| 92 | // MemberInfos are sorted so we define a < operator. |
| 93 | bool operator <(const MemberInfo& a) const { return Offset < a.Offset; } |
| 94 | }; |
| 95 | // The constructor. |
David Majnemer | bb51300 | 2014-09-28 06:39:30 +0000 | [diff] [blame] | 96 | CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D, bool Packed); |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 97 | // Short helper routines. |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 98 | /// Constructs a MemberInfo instance from an offset and llvm::Type *. |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 99 | MemberInfo StorageInfo(CharUnits Offset, llvm::Type *Data) { |
| 100 | return MemberInfo(Offset, MemberInfo::Field, Data); |
| 101 | } |
John McCall | f3e86a7 | 2015-04-28 00:17:18 +0000 | [diff] [blame] | 102 | |
| 103 | /// The Microsoft bitfield layout rule allocates discrete storage |
| 104 | /// units of the field's formal type and only combines adjacent |
| 105 | /// fields of the same formal type. We want to emit a layout with |
| 106 | /// these discrete storage units instead of combining them into a |
| 107 | /// continuous run. |
| 108 | bool isDiscreteBitFieldABI() { |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 109 | return Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 110 | D->isMsStruct(Context); |
| 111 | } |
John McCall | f3e86a7 | 2015-04-28 00:17:18 +0000 | [diff] [blame] | 112 | |
| 113 | /// The Itanium base layout rule allows virtual bases to overlap |
| 114 | /// other bases, which complicates layout in specific ways. |
| 115 | /// |
| 116 | /// Note specifically that the ms_struct attribute doesn't change this. |
| 117 | bool isOverlappingVBaseABI() { |
| 118 | return !Context.getTargetInfo().getCXXABI().isMicrosoft(); |
| 119 | } |
| 120 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 121 | /// Wraps llvm::Type::getIntNTy with some implicit arguments. |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 122 | llvm::Type *getIntNType(uint64_t NumBits) { |
| 123 | return llvm::Type::getIntNTy(Types.getLLVMContext(), |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 124 | (unsigned)llvm::alignTo(NumBits, 8)); |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 125 | } |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 126 | /// Gets an llvm type of size NumBytes and alignment 1. |
David Majnemer | 7a72601 | 2014-02-22 00:41:07 +0000 | [diff] [blame] | 127 | llvm::Type *getByteArrayType(CharUnits NumBytes) { |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 128 | assert(!NumBytes.isZero() && "Empty byte arrays aren't allowed."); |
| 129 | llvm::Type *Type = llvm::Type::getInt8Ty(Types.getLLVMContext()); |
| 130 | return NumBytes == CharUnits::One() ? Type : |
| 131 | (llvm::Type *)llvm::ArrayType::get(Type, NumBytes.getQuantity()); |
| 132 | } |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 133 | /// Gets the storage type for a field decl and handles storage |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 134 | /// for itanium bitfields that are smaller than their declared type. |
| 135 | llvm::Type *getStorageType(const FieldDecl *FD) { |
| 136 | llvm::Type *Type = Types.ConvertTypeForMem(FD->getType()); |
John McCall | f3e86a7 | 2015-04-28 00:17:18 +0000 | [diff] [blame] | 137 | if (!FD->isBitField()) return Type; |
| 138 | if (isDiscreteBitFieldABI()) return Type; |
| 139 | return getIntNType(std::min(FD->getBitWidthValue(Context), |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 140 | (unsigned)Context.toBits(getSize(Type)))); |
| 141 | } |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 142 | /// Gets the llvm Basesubobject type from a CXXRecordDecl. |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 143 | llvm::Type *getStorageType(const CXXRecordDecl *RD) { |
| 144 | return Types.getCGRecordLayout(RD).getBaseSubobjectLLVMType(); |
| 145 | } |
| 146 | CharUnits bitsToCharUnits(uint64_t BitOffset) { |
| 147 | return Context.toCharUnitsFromBits(BitOffset); |
| 148 | } |
| 149 | CharUnits getSize(llvm::Type *Type) { |
| 150 | return CharUnits::fromQuantity(DataLayout.getTypeAllocSize(Type)); |
| 151 | } |
| 152 | CharUnits getAlignment(llvm::Type *Type) { |
| 153 | return CharUnits::fromQuantity(DataLayout.getABITypeAlignment(Type)); |
| 154 | } |
| 155 | bool isZeroInitializable(const FieldDecl *FD) { |
David Majnemer | 67fa0b8 | 2015-05-26 21:28:50 +0000 | [diff] [blame] | 156 | return Types.isZeroInitializable(FD->getType()); |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 157 | } |
| 158 | bool isZeroInitializable(const RecordDecl *RD) { |
David Majnemer | 67fa0b8 | 2015-05-26 21:28:50 +0000 | [diff] [blame] | 159 | return Types.isZeroInitializable(RD); |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 160 | } |
| 161 | void appendPaddingBytes(CharUnits Size) { |
| 162 | if (!Size.isZero()) |
| 163 | FieldTypes.push_back(getByteArrayType(Size)); |
| 164 | } |
| 165 | uint64_t getFieldBitOffset(const FieldDecl *FD) { |
| 166 | return Layout.getFieldOffset(FD->getFieldIndex()); |
| 167 | } |
| 168 | // Layout routines. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 169 | void setBitFieldInfo(const FieldDecl *FD, CharUnits StartOffset, |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 170 | llvm::Type *StorageType); |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 171 | /// Lowers an ASTRecordLayout to a llvm type. |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 172 | void lower(bool NonVirtualBaseType); |
| 173 | void lowerUnion(); |
| 174 | void accumulateFields(); |
| 175 | void accumulateBitFields(RecordDecl::field_iterator Field, |
| 176 | RecordDecl::field_iterator FieldEnd); |
| 177 | void accumulateBases(); |
| 178 | void accumulateVPtrs(); |
| 179 | void accumulateVBases(); |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 180 | /// Recursively searches all of the bases to find out if a vbase is |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 181 | /// not the primary vbase of some base class. |
| 182 | bool hasOwnStorage(const CXXRecordDecl *Decl, const CXXRecordDecl *Query); |
| 183 | void calculateZeroInit(); |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 184 | /// Lowers bitfield storage types to I8 arrays for bitfields with tail |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 185 | /// padding that is or can potentially be used. |
| 186 | void clipTailPadding(); |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 187 | /// Determines if we need a packed llvm struct. |
David Majnemer | bb51300 | 2014-09-28 06:39:30 +0000 | [diff] [blame] | 188 | void determinePacked(bool NVBaseType); |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 189 | /// Inserts padding everywhere it's needed. |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 190 | void insertPadding(); |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 191 | /// Fills out the structures that are ultimately consumed. |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 192 | void fillOutputFields(); |
| 193 | // Input memoization fields. |
| 194 | CodeGenTypes &Types; |
| 195 | const ASTContext &Context; |
| 196 | const RecordDecl *D; |
| 197 | const CXXRecordDecl *RD; |
| 198 | const ASTRecordLayout &Layout; |
| 199 | const llvm::DataLayout &DataLayout; |
| 200 | // Helpful intermediate data-structures. |
| 201 | std::vector<MemberInfo> Members; |
| 202 | // Output fields, consumed by CodeGenTypes::ComputeRecordLayout. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 203 | SmallVector<llvm::Type *, 16> FieldTypes; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 204 | llvm::DenseMap<const FieldDecl *, unsigned> Fields; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 205 | llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 206 | llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases; |
| 207 | llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases; |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 208 | bool IsZeroInitializable : 1; |
| 209 | bool IsZeroInitializableAsBase : 1; |
| 210 | bool Packed : 1; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 211 | private: |
Aaron Ballman | abc1892 | 2015-02-15 22:54:08 +0000 | [diff] [blame] | 212 | CGRecordLowering(const CGRecordLowering &) = delete; |
| 213 | void operator =(const CGRecordLowering &) = delete; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 214 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 215 | } // namespace { |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 216 | |
Erich Keane | cf3c4a9 | 2018-04-12 20:46:31 +0000 | [diff] [blame] | 217 | CGRecordLowering::CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D, |
| 218 | bool Packed) |
| 219 | : Types(Types), Context(Types.getContext()), D(D), |
| 220 | RD(dyn_cast<CXXRecordDecl>(D)), |
| 221 | Layout(Types.getContext().getASTRecordLayout(D)), |
| 222 | DataLayout(Types.getDataLayout()), IsZeroInitializable(true), |
| 223 | IsZeroInitializableAsBase(true), Packed(Packed) {} |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 224 | |
| 225 | void CGRecordLowering::setBitFieldInfo( |
| 226 | const FieldDecl *FD, CharUnits StartOffset, llvm::Type *StorageType) { |
Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 227 | CGBitFieldInfo &Info = BitFields[FD->getCanonicalDecl()]; |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 228 | Info.IsSigned = FD->getType()->isSignedIntegerOrEnumerationType(); |
| 229 | Info.Offset = (unsigned)(getFieldBitOffset(FD) - Context.toBits(StartOffset)); |
| 230 | Info.Size = FD->getBitWidthValue(Context); |
| 231 | Info.StorageSize = (unsigned)DataLayout.getTypeAllocSizeInBits(StorageType); |
Ulrich Weigand | 03ce2a1 | 2015-07-10 17:30:00 +0000 | [diff] [blame] | 232 | Info.StorageOffset = StartOffset; |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 233 | if (Info.Size > Info.StorageSize) |
| 234 | Info.Size = Info.StorageSize; |
| 235 | // Reverse the bit offsets for big endian machines. Because we represent |
| 236 | // a bitfield as a single large integer load, we can imagine the bits |
| 237 | // counting from the most-significant-bit instead of the |
| 238 | // least-significant-bit. |
| 239 | if (DataLayout.isBigEndian()) |
| 240 | Info.Offset = Info.StorageSize - (Info.Offset + Info.Size); |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 241 | } |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 242 | |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 243 | void CGRecordLowering::lower(bool NVBaseType) { |
| 244 | // The lowering process implemented in this function takes a variety of |
| 245 | // carefully ordered phases. |
| 246 | // 1) Store all members (fields and bases) in a list and sort them by offset. |
| 247 | // 2) Add a 1-byte capstone member at the Size of the structure. |
| 248 | // 3) Clip bitfield storages members if their tail padding is or might be |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 249 | // used by another field or base. The clipping process uses the capstone |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 250 | // by treating it as another object that occurs after the record. |
| 251 | // 4) Determine if the llvm-struct requires packing. It's important that this |
| 252 | // phase occur after clipping, because clipping changes the llvm type. |
| 253 | // This phase reads the offset of the capstone when determining packedness |
| 254 | // and updates the alignment of the capstone to be equal of the alignment |
| 255 | // of the record after doing so. |
| 256 | // 5) Insert padding everywhere it is needed. This phase requires 'Packed' to |
| 257 | // have been computed and needs to know the alignment of the record in |
| 258 | // order to understand if explicit tail padding is needed. |
| 259 | // 6) Remove the capstone, we don't need it anymore. |
| 260 | // 7) Determine if this record can be zero-initialized. This phase could have |
| 261 | // been placed anywhere after phase 1. |
| 262 | // 8) Format the complete list of members in a way that can be consumed by |
| 263 | // CodeGenTypes::ComputeRecordLayout. |
| 264 | CharUnits Size = NVBaseType ? Layout.getNonVirtualSize() : Layout.getSize(); |
| 265 | if (D->isUnion()) |
| 266 | return lowerUnion(); |
| 267 | accumulateFields(); |
| 268 | // RD implies C++. |
| 269 | if (RD) { |
| 270 | accumulateVPtrs(); |
| 271 | accumulateBases(); |
| 272 | if (Members.empty()) |
| 273 | return appendPaddingBytes(Size); |
| 274 | if (!NVBaseType) |
| 275 | accumulateVBases(); |
| 276 | } |
| 277 | std::stable_sort(Members.begin(), Members.end()); |
| 278 | Members.push_back(StorageInfo(Size, getIntNType(8))); |
| 279 | clipTailPadding(); |
David Majnemer | bb51300 | 2014-09-28 06:39:30 +0000 | [diff] [blame] | 280 | determinePacked(NVBaseType); |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 281 | insertPadding(); |
| 282 | Members.pop_back(); |
| 283 | calculateZeroInit(); |
| 284 | fillOutputFields(); |
| 285 | } |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 286 | |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 287 | void CGRecordLowering::lowerUnion() { |
Warren Hunt | fed5597 | 2014-03-01 00:38:40 +0000 | [diff] [blame] | 288 | CharUnits LayoutSize = Layout.getSize(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 289 | llvm::Type *StorageType = nullptr; |
David Majnemer | b00ddf3 | 2014-10-15 07:57:41 +0000 | [diff] [blame] | 290 | bool SeenNamedMember = false; |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 291 | // Iterate through the fields setting bitFieldInfo and the Fields array. Also |
| 292 | // locate the "most appropriate" storage type. The heuristic for finding the |
| 293 | // storage type isn't necessary, the first (non-0-length-bitfield) field's |
David Majnemer | 2e29b40 | 2014-10-15 07:57:38 +0000 | [diff] [blame] | 294 | // type would work fine and be simpler but would be different than what we've |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 295 | // been doing and cause lit tests to change. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 296 | for (const auto *Field : D->fields()) { |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 297 | if (Field->isBitField()) { |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 298 | if (Field->isZeroLengthBitField(Context)) |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 299 | continue; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 300 | llvm::Type *FieldType = getStorageType(Field); |
Warren Hunt | fed5597 | 2014-03-01 00:38:40 +0000 | [diff] [blame] | 301 | if (LayoutSize < getSize(FieldType)) |
| 302 | FieldType = getByteArrayType(LayoutSize); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 303 | setBitFieldInfo(Field, CharUnits::Zero(), FieldType); |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 304 | } |
Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 305 | Fields[Field->getCanonicalDecl()] = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 306 | llvm::Type *FieldType = getStorageType(Field); |
David Majnemer | 8476abe | 2014-10-15 16:36:11 +0000 | [diff] [blame] | 307 | // Compute zero-initializable status. |
David Majnemer | b00ddf3 | 2014-10-15 07:57:41 +0000 | [diff] [blame] | 308 | // This union might not be zero initialized: it may contain a pointer to |
| 309 | // data member which might have some exotic initialization sequence. |
| 310 | // If this is the case, then we aught not to try and come up with a "better" |
| 311 | // type, it might not be very easy to come up with a Constant which |
| 312 | // correctly initializes it. |
David Majnemer | 4e51dfc | 2015-05-30 09:12:07 +0000 | [diff] [blame] | 313 | if (!SeenNamedMember) { |
| 314 | SeenNamedMember = Field->getIdentifier(); |
| 315 | if (!SeenNamedMember) |
George Karpenkov | 39e5137 | 2018-07-28 02:16:13 +0000 | [diff] [blame] | 316 | if (const auto *FieldRD = Field->getType()->getAsRecordDecl()) |
| 317 | SeenNamedMember = FieldRD->findFirstNamedDataMember(); |
David Majnemer | 4e51dfc | 2015-05-30 09:12:07 +0000 | [diff] [blame] | 318 | if (SeenNamedMember && !isZeroInitializable(Field)) { |
David Majnemer | 8476abe | 2014-10-15 16:36:11 +0000 | [diff] [blame] | 319 | IsZeroInitializable = IsZeroInitializableAsBase = false; |
David Majnemer | b00ddf3 | 2014-10-15 07:57:41 +0000 | [diff] [blame] | 320 | StorageType = FieldType; |
| 321 | } |
| 322 | } |
David Majnemer | 8476abe | 2014-10-15 16:36:11 +0000 | [diff] [blame] | 323 | // Because our union isn't zero initializable, we won't be getting a better |
| 324 | // storage type. |
| 325 | if (!IsZeroInitializable) |
| 326 | continue; |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 327 | // Conditionally update our storage type if we've got a new "better" one. |
| 328 | if (!StorageType || |
| 329 | getAlignment(FieldType) > getAlignment(StorageType) || |
| 330 | (getAlignment(FieldType) == getAlignment(StorageType) && |
| 331 | getSize(FieldType) > getSize(StorageType))) |
David Majnemer | 8476abe | 2014-10-15 16:36:11 +0000 | [diff] [blame] | 332 | StorageType = FieldType; |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 333 | } |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 334 | // If we have no storage type just pad to the appropriate size and return. |
| 335 | if (!StorageType) |
| 336 | return appendPaddingBytes(LayoutSize); |
| 337 | // If our storage size was bigger than our required size (can happen in the |
| 338 | // case of packed bitfields on Itanium) then just use an I8 array. |
| 339 | if (LayoutSize < getSize(StorageType)) |
| 340 | StorageType = getByteArrayType(LayoutSize); |
| 341 | FieldTypes.push_back(StorageType); |
| 342 | appendPaddingBytes(LayoutSize - getSize(StorageType)); |
| 343 | // Set packed if we need it. |
| 344 | if (LayoutSize % getAlignment(StorageType)) |
| 345 | Packed = true; |
| 346 | } |
| 347 | |
| 348 | void CGRecordLowering::accumulateFields() { |
| 349 | for (RecordDecl::field_iterator Field = D->field_begin(), |
| 350 | FieldEnd = D->field_end(); |
| 351 | Field != FieldEnd;) |
| 352 | if (Field->isBitField()) { |
| 353 | RecordDecl::field_iterator Start = Field; |
| 354 | // Iterate to gather the list of bitfields. |
| 355 | for (++Field; Field != FieldEnd && Field->isBitField(); ++Field); |
| 356 | accumulateBitFields(Start, Field); |
| 357 | } else { |
| 358 | Members.push_back(MemberInfo( |
| 359 | bitsToCharUnits(getFieldBitOffset(*Field)), MemberInfo::Field, |
| 360 | getStorageType(*Field), *Field)); |
| 361 | ++Field; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | void |
| 366 | CGRecordLowering::accumulateBitFields(RecordDecl::field_iterator Field, |
| 367 | RecordDecl::field_iterator FieldEnd) { |
| 368 | // Run stores the first element of the current run of bitfields. FieldEnd is |
| 369 | // used as a special value to note that we don't have a current run. A |
| 370 | // bitfield run is a contiguous collection of bitfields that can be stored in |
| 371 | // the same storage block. Zero-sized bitfields and bitfields that would |
| 372 | // cross an alignment boundary break a run and start a new one. |
| 373 | RecordDecl::field_iterator Run = FieldEnd; |
| 374 | // Tail is the offset of the first bit off the end of the current run. It's |
| 375 | // used to determine if the ASTRecordLayout is treating these two bitfields as |
| 376 | // contiguous. StartBitOffset is offset of the beginning of the Run. |
| 377 | uint64_t StartBitOffset, Tail = 0; |
John McCall | f3e86a7 | 2015-04-28 00:17:18 +0000 | [diff] [blame] | 378 | if (isDiscreteBitFieldABI()) { |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 379 | for (; Field != FieldEnd; ++Field) { |
| 380 | uint64_t BitOffset = getFieldBitOffset(*Field); |
| 381 | // Zero-width bitfields end runs. |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 382 | if (Field->isZeroLengthBitField(Context)) { |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 383 | Run = FieldEnd; |
| 384 | continue; |
| 385 | } |
| 386 | llvm::Type *Type = Types.ConvertTypeForMem(Field->getType()); |
| 387 | // If we don't have a run yet, or don't live within the previous run's |
| 388 | // allocated storage then we allocate some storage and start a new run. |
| 389 | if (Run == FieldEnd || BitOffset >= Tail) { |
| 390 | Run = Field; |
| 391 | StartBitOffset = BitOffset; |
| 392 | Tail = StartBitOffset + DataLayout.getTypeAllocSizeInBits(Type); |
| 393 | // Add the storage member to the record. This must be added to the |
| 394 | // record before the bitfield members so that it gets laid out before |
| 395 | // the bitfields it contains get laid out. |
| 396 | Members.push_back(StorageInfo(bitsToCharUnits(StartBitOffset), Type)); |
| 397 | } |
| 398 | // Bitfields get the offset of their storage but come afterward and remain |
| 399 | // there after a stable sort. |
| 400 | Members.push_back(MemberInfo(bitsToCharUnits(StartBitOffset), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 401 | MemberInfo::Field, nullptr, *Field)); |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 402 | } |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 403 | return; |
| 404 | } |
Wei Mi | 9b3d627 | 2017-10-16 16:50:27 +0000 | [diff] [blame] | 405 | |
Strahinja Petrovic | 0f274c0 | 2018-05-10 12:31:12 +0000 | [diff] [blame] | 406 | // Check if OffsetInRecord is better as a single field run. When OffsetInRecord |
Wei Mi | 9b3d627 | 2017-10-16 16:50:27 +0000 | [diff] [blame] | 407 | // has legal integer width, and its bitfield offset is naturally aligned, it |
| 408 | // is better to make the bitfield a separate storage component so as it can be |
| 409 | // accessed directly with lower cost. |
Strahinja Petrovic | 0f274c0 | 2018-05-10 12:31:12 +0000 | [diff] [blame] | 410 | auto IsBetterAsSingleFieldRun = [&](uint64_t OffsetInRecord, |
| 411 | uint64_t StartBitOffset) { |
Wei Mi | 9b3d627 | 2017-10-16 16:50:27 +0000 | [diff] [blame] | 412 | if (!Types.getCodeGenOpts().FineGrainedBitfieldAccesses) |
| 413 | return false; |
Strahinja Petrovic | 0f274c0 | 2018-05-10 12:31:12 +0000 | [diff] [blame] | 414 | if (!DataLayout.isLegalInteger(OffsetInRecord)) |
Wei Mi | 9b3d627 | 2017-10-16 16:50:27 +0000 | [diff] [blame] | 415 | return false; |
Strahinja Petrovic | 0f274c0 | 2018-05-10 12:31:12 +0000 | [diff] [blame] | 416 | // Make sure StartBitOffset is natually aligned if it is treated as an |
| 417 | // IType integer. |
| 418 | if (StartBitOffset % |
| 419 | Context.toBits(getAlignment(getIntNType(OffsetInRecord))) != |
Wei Mi | 9b3d627 | 2017-10-16 16:50:27 +0000 | [diff] [blame] | 420 | 0) |
| 421 | return false; |
| 422 | return true; |
| 423 | }; |
| 424 | |
| 425 | // The start field is better as a single field run. |
| 426 | bool StartFieldAsSingleRun = false; |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 427 | for (;;) { |
| 428 | // Check to see if we need to start a new run. |
| 429 | if (Run == FieldEnd) { |
| 430 | // If we're out of fields, return. |
| 431 | if (Field == FieldEnd) |
| 432 | break; |
| 433 | // Any non-zero-length bitfield can start a new run. |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 434 | if (!Field->isZeroLengthBitField(Context)) { |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 435 | Run = Field; |
| 436 | StartBitOffset = getFieldBitOffset(*Field); |
| 437 | Tail = StartBitOffset + Field->getBitWidthValue(Context); |
Strahinja Petrovic | 0f274c0 | 2018-05-10 12:31:12 +0000 | [diff] [blame] | 438 | StartFieldAsSingleRun = IsBetterAsSingleFieldRun(Tail - StartBitOffset, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 439 | StartBitOffset); |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 440 | } |
| 441 | ++Field; |
| 442 | continue; |
| 443 | } |
Wei Mi | 9b3d627 | 2017-10-16 16:50:27 +0000 | [diff] [blame] | 444 | |
| 445 | // If the start field of a new run is better as a single run, or |
Strahinja Petrovic | 0f274c0 | 2018-05-10 12:31:12 +0000 | [diff] [blame] | 446 | // if current field (or consecutive fields) is better as a single run, or |
Akira Hatanaka | fc681ef | 2018-02-01 03:04:15 +0000 | [diff] [blame] | 447 | // if current field has zero width bitfield and either |
| 448 | // UseZeroLengthBitfieldAlignment or UseBitFieldTypeAlignment is set to |
| 449 | // true, or |
Wei Mi | 9b3d627 | 2017-10-16 16:50:27 +0000 | [diff] [blame] | 450 | // if the offset of current field is inconsistent with the offset of |
| 451 | // previous field plus its offset, |
| 452 | // skip the block below and go ahead to emit the storage. |
| 453 | // Otherwise, try to add bitfields to the run. |
| 454 | if (!StartFieldAsSingleRun && Field != FieldEnd && |
Strahinja Petrovic | 0f274c0 | 2018-05-10 12:31:12 +0000 | [diff] [blame] | 455 | !IsBetterAsSingleFieldRun(Tail - StartBitOffset, StartBitOffset) && |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 456 | (!Field->isZeroLengthBitField(Context) || |
Akira Hatanaka | fc681ef | 2018-02-01 03:04:15 +0000 | [diff] [blame] | 457 | (!Context.getTargetInfo().useZeroLengthBitfieldAlignment() && |
| 458 | !Context.getTargetInfo().useBitFieldTypeAlignment())) && |
Justin Bogner | 085c4b2 | 2014-08-14 15:44:29 +0000 | [diff] [blame] | 459 | Tail == getFieldBitOffset(*Field)) { |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 460 | Tail += Field->getBitWidthValue(Context); |
| 461 | ++Field; |
| 462 | continue; |
| 463 | } |
Wei Mi | 9b3d627 | 2017-10-16 16:50:27 +0000 | [diff] [blame] | 464 | |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 465 | // We've hit a break-point in the run and need to emit a storage field. |
| 466 | llvm::Type *Type = getIntNType(Tail - StartBitOffset); |
| 467 | // Add the storage member to the record and set the bitfield info for all of |
| 468 | // the bitfields in the run. Bitfields get the offset of their storage but |
| 469 | // come afterward and remain there after a stable sort. |
| 470 | Members.push_back(StorageInfo(bitsToCharUnits(StartBitOffset), Type)); |
| 471 | for (; Run != Field; ++Run) |
| 472 | Members.push_back(MemberInfo(bitsToCharUnits(StartBitOffset), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 473 | MemberInfo::Field, nullptr, *Run)); |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 474 | Run = FieldEnd; |
Wei Mi | 9b3d627 | 2017-10-16 16:50:27 +0000 | [diff] [blame] | 475 | StartFieldAsSingleRun = false; |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 476 | } |
| 477 | } |
Anders Carlsson | 68e0b68 | 2009-08-08 18:23:56 +0000 | [diff] [blame] | 478 | |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 479 | void CGRecordLowering::accumulateBases() { |
| 480 | // If we've got a primary virtual base, we need to add it with the bases. |
Warren Hunt | f0ffdb2 | 2014-04-25 21:56:30 +0000 | [diff] [blame] | 481 | if (Layout.isPrimaryBaseVirtual()) { |
| 482 | const CXXRecordDecl *BaseDecl = Layout.getPrimaryBase(); |
| 483 | Members.push_back(MemberInfo(CharUnits::Zero(), MemberInfo::Base, |
| 484 | getStorageType(BaseDecl), BaseDecl)); |
| 485 | } |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 486 | // Accumulate the non-virtual bases. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 487 | for (const auto &Base : RD->bases()) { |
| 488 | if (Base.isVirtual()) |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 489 | continue; |
John McCall | 9fc700e | 2015-04-26 04:43:26 +0000 | [diff] [blame] | 490 | |
| 491 | // Bases can be zero-sized even if not technically empty if they |
| 492 | // contain only a trailing array member. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 493 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
John McCall | 9fc700e | 2015-04-26 04:43:26 +0000 | [diff] [blame] | 494 | if (!BaseDecl->isEmpty() && |
David Majnemer | 78945d0 | 2015-10-22 18:04:22 +0000 | [diff] [blame] | 495 | !Context.getASTRecordLayout(BaseDecl).getNonVirtualSize().isZero()) |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 496 | Members.push_back(MemberInfo(Layout.getBaseClassOffset(BaseDecl), |
| 497 | MemberInfo::Base, getStorageType(BaseDecl), BaseDecl)); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | void CGRecordLowering::accumulateVPtrs() { |
| 502 | if (Layout.hasOwnVFPtr()) |
| 503 | Members.push_back(MemberInfo(CharUnits::Zero(), MemberInfo::VFPtr, |
| 504 | llvm::FunctionType::get(getIntNType(32), /*isVarArg=*/true)-> |
| 505 | getPointerTo()->getPointerTo())); |
| 506 | if (Layout.hasOwnVBPtr()) |
| 507 | Members.push_back(MemberInfo(Layout.getVBPtrOffset(), MemberInfo::VBPtr, |
| 508 | llvm::Type::getInt32PtrTy(Types.getLLVMContext()))); |
| 509 | } |
| 510 | |
| 511 | void CGRecordLowering::accumulateVBases() { |
Warren Hunt | f0ffdb2 | 2014-04-25 21:56:30 +0000 | [diff] [blame] | 512 | CharUnits ScissorOffset = Layout.getNonVirtualSize(); |
| 513 | // In the itanium ABI, it's possible to place a vbase at a dsize that is |
| 514 | // smaller than the nvsize. Here we check to see if such a base is placed |
| 515 | // before the nvsize and set the scissor offset to that, instead of the |
| 516 | // nvsize. |
John McCall | f3e86a7 | 2015-04-28 00:17:18 +0000 | [diff] [blame] | 517 | if (isOverlappingVBaseABI()) |
Warren Hunt | f0ffdb2 | 2014-04-25 21:56:30 +0000 | [diff] [blame] | 518 | for (const auto &Base : RD->vbases()) { |
| 519 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
| 520 | if (BaseDecl->isEmpty()) |
| 521 | continue; |
| 522 | // If the vbase is a primary virtual base of some base, then it doesn't |
| 523 | // get its own storage location but instead lives inside of that base. |
| 524 | if (Context.isNearlyEmpty(BaseDecl) && !hasOwnStorage(RD, BaseDecl)) |
| 525 | continue; |
| 526 | ScissorOffset = std::min(ScissorOffset, |
| 527 | Layout.getVBaseClassOffset(BaseDecl)); |
| 528 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 529 | Members.push_back(MemberInfo(ScissorOffset, MemberInfo::Scissor, nullptr, |
| 530 | RD)); |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 531 | for (const auto &Base : RD->vbases()) { |
| 532 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 533 | if (BaseDecl->isEmpty()) |
| 534 | continue; |
| 535 | CharUnits Offset = Layout.getVBaseClassOffset(BaseDecl); |
| 536 | // If the vbase is a primary virtual base of some base, then it doesn't |
| 537 | // get its own storage location but instead lives inside of that base. |
John McCall | f3e86a7 | 2015-04-28 00:17:18 +0000 | [diff] [blame] | 538 | if (isOverlappingVBaseABI() && |
| 539 | Context.isNearlyEmpty(BaseDecl) && |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 540 | !hasOwnStorage(RD, BaseDecl)) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 541 | Members.push_back(MemberInfo(Offset, MemberInfo::VBase, nullptr, |
| 542 | BaseDecl)); |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 543 | continue; |
| 544 | } |
| 545 | // If we've got a vtordisp, add it as a storage type. |
| 546 | if (Layout.getVBaseOffsetsMap().find(BaseDecl)->second.hasVtorDisp()) |
| 547 | Members.push_back(StorageInfo(Offset - CharUnits::fromQuantity(4), |
| 548 | getIntNType(32))); |
| 549 | Members.push_back(MemberInfo(Offset, MemberInfo::VBase, |
| 550 | getStorageType(BaseDecl), BaseDecl)); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | bool CGRecordLowering::hasOwnStorage(const CXXRecordDecl *Decl, |
| 555 | const CXXRecordDecl *Query) { |
| 556 | const ASTRecordLayout &DeclLayout = Context.getASTRecordLayout(Decl); |
| 557 | if (DeclLayout.isPrimaryBaseVirtual() && DeclLayout.getPrimaryBase() == Query) |
| 558 | return false; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 559 | for (const auto &Base : Decl->bases()) |
| 560 | if (!hasOwnStorage(Base.getType()->getAsCXXRecordDecl(), Query)) |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 561 | return false; |
| 562 | return true; |
| 563 | } |
| 564 | |
| 565 | void CGRecordLowering::calculateZeroInit() { |
| 566 | for (std::vector<MemberInfo>::const_iterator Member = Members.begin(), |
| 567 | MemberEnd = Members.end(); |
| 568 | IsZeroInitializableAsBase && Member != MemberEnd; ++Member) { |
| 569 | if (Member->Kind == MemberInfo::Field) { |
| 570 | if (!Member->FD || isZeroInitializable(Member->FD)) |
| 571 | continue; |
| 572 | IsZeroInitializable = IsZeroInitializableAsBase = false; |
| 573 | } else if (Member->Kind == MemberInfo::Base || |
| 574 | Member->Kind == MemberInfo::VBase) { |
| 575 | if (isZeroInitializable(Member->RD)) |
| 576 | continue; |
| 577 | IsZeroInitializable = false; |
| 578 | if (Member->Kind == MemberInfo::Base) |
| 579 | IsZeroInitializableAsBase = false; |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | void CGRecordLowering::clipTailPadding() { |
| 585 | std::vector<MemberInfo>::iterator Prior = Members.begin(); |
| 586 | CharUnits Tail = getSize(Prior->Data); |
| 587 | for (std::vector<MemberInfo>::iterator Member = Prior + 1, |
| 588 | MemberEnd = Members.end(); |
| 589 | Member != MemberEnd; ++Member) { |
| 590 | // Only members with data and the scissor can cut into tail padding. |
| 591 | if (!Member->Data && Member->Kind != MemberInfo::Scissor) |
| 592 | continue; |
| 593 | if (Member->Offset < Tail) { |
| 594 | assert(Prior->Kind == MemberInfo::Field && !Prior->FD && |
| 595 | "Only storage fields have tail padding!"); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 596 | Prior->Data = getByteArrayType(bitsToCharUnits(llvm::alignTo( |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 597 | cast<llvm::IntegerType>(Prior->Data)->getIntegerBitWidth(), 8))); |
| 598 | } |
| 599 | if (Member->Data) |
| 600 | Prior = Member; |
| 601 | Tail = Prior->Offset + getSize(Prior->Data); |
| 602 | } |
| 603 | } |
| 604 | |
David Majnemer | bb51300 | 2014-09-28 06:39:30 +0000 | [diff] [blame] | 605 | void CGRecordLowering::determinePacked(bool NVBaseType) { |
| 606 | if (Packed) |
| 607 | return; |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 608 | CharUnits Alignment = CharUnits::One(); |
David Majnemer | bb51300 | 2014-09-28 06:39:30 +0000 | [diff] [blame] | 609 | CharUnits NVAlignment = CharUnits::One(); |
| 610 | CharUnits NVSize = |
| 611 | !NVBaseType && RD ? Layout.getNonVirtualSize() : CharUnits::Zero(); |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 612 | for (std::vector<MemberInfo>::const_iterator Member = Members.begin(), |
| 613 | MemberEnd = Members.end(); |
| 614 | Member != MemberEnd; ++Member) { |
| 615 | if (!Member->Data) |
| 616 | continue; |
| 617 | // If any member falls at an offset that it not a multiple of its alignment, |
| 618 | // then the entire record must be packed. |
| 619 | if (Member->Offset % getAlignment(Member->Data)) |
| 620 | Packed = true; |
David Majnemer | bb51300 | 2014-09-28 06:39:30 +0000 | [diff] [blame] | 621 | if (Member->Offset < NVSize) |
| 622 | NVAlignment = std::max(NVAlignment, getAlignment(Member->Data)); |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 623 | Alignment = std::max(Alignment, getAlignment(Member->Data)); |
| 624 | } |
| 625 | // If the size of the record (the capstone's offset) is not a multiple of the |
| 626 | // record's alignment, it must be packed. |
| 627 | if (Members.back().Offset % Alignment) |
| 628 | Packed = true; |
David Majnemer | bb51300 | 2014-09-28 06:39:30 +0000 | [diff] [blame] | 629 | // If the non-virtual sub-object is not a multiple of the non-virtual |
| 630 | // sub-object's alignment, it must be packed. We cannot have a packed |
| 631 | // non-virtual sub-object and an unpacked complete object or vise versa. |
| 632 | if (NVSize % NVAlignment) |
| 633 | Packed = true; |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 634 | // Update the alignment of the sentinel. |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 635 | if (!Packed) |
| 636 | Members.back().Data = getIntNType(Context.toBits(Alignment)); |
| 637 | } |
| 638 | |
| 639 | void CGRecordLowering::insertPadding() { |
| 640 | std::vector<std::pair<CharUnits, CharUnits> > Padding; |
| 641 | CharUnits Size = CharUnits::Zero(); |
| 642 | for (std::vector<MemberInfo>::const_iterator Member = Members.begin(), |
| 643 | MemberEnd = Members.end(); |
| 644 | Member != MemberEnd; ++Member) { |
| 645 | if (!Member->Data) |
| 646 | continue; |
| 647 | CharUnits Offset = Member->Offset; |
| 648 | assert(Offset >= Size); |
| 649 | // Insert padding if we need to. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 650 | if (Offset != |
| 651 | Size.alignTo(Packed ? CharUnits::One() : getAlignment(Member->Data))) |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 652 | Padding.push_back(std::make_pair(Size, Offset - Size)); |
| 653 | Size = Offset + getSize(Member->Data); |
| 654 | } |
| 655 | if (Padding.empty()) |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 656 | return; |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 657 | // Add the padding to the Members list and sort it. |
| 658 | for (std::vector<std::pair<CharUnits, CharUnits> >::const_iterator |
| 659 | Pad = Padding.begin(), PadEnd = Padding.end(); |
| 660 | Pad != PadEnd; ++Pad) |
| 661 | Members.push_back(StorageInfo(Pad->first, getByteArrayType(Pad->second))); |
| 662 | std::stable_sort(Members.begin(), Members.end()); |
| 663 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 665 | void CGRecordLowering::fillOutputFields() { |
| 666 | for (std::vector<MemberInfo>::const_iterator Member = Members.begin(), |
| 667 | MemberEnd = Members.end(); |
| 668 | Member != MemberEnd; ++Member) { |
| 669 | if (Member->Data) |
| 670 | FieldTypes.push_back(Member->Data); |
| 671 | if (Member->Kind == MemberInfo::Field) { |
| 672 | if (Member->FD) |
Richard Smith | cd45dbc | 2014-04-19 03:48:30 +0000 | [diff] [blame] | 673 | Fields[Member->FD->getCanonicalDecl()] = FieldTypes.size() - 1; |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 674 | // A field without storage must be a bitfield. |
| 675 | if (!Member->Data) |
| 676 | setBitFieldInfo(Member->FD, Member->Offset, FieldTypes.back()); |
| 677 | } else if (Member->Kind == MemberInfo::Base) |
| 678 | NonVirtualBases[Member->RD] = FieldTypes.size() - 1; |
| 679 | else if (Member->Kind == MemberInfo::VBase) |
| 680 | VirtualBases[Member->RD] = FieldTypes.size() - 1; |
| 681 | } |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Daniel Dunbar | c7f9bba | 2010-09-02 23:53:28 +0000 | [diff] [blame] | 684 | CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types, |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 685 | const FieldDecl *FD, |
| 686 | uint64_t Offset, uint64_t Size, |
| 687 | uint64_t StorageSize, |
Ulrich Weigand | 03ce2a1 | 2015-07-10 17:30:00 +0000 | [diff] [blame] | 688 | CharUnits StorageOffset) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 689 | // This function is vestigial from CGRecordLayoutBuilder days but is still |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 690 | // used in GCObjCRuntime.cpp. That usage has a "fixme" attached to it that |
| 691 | // when addressed will allow for the removal of this function. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 692 | llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType()); |
John McCall | 8a3c555 | 2011-02-26 08:41:59 +0000 | [diff] [blame] | 693 | CharUnits TypeSizeInBytes = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 694 | CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(Ty)); |
John McCall | 8a3c555 | 2011-02-26 08:41:59 +0000 | [diff] [blame] | 695 | uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes); |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 696 | |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 697 | bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType(); |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 698 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 699 | if (Size > TypeSizeInBits) { |
Anders Carlsson | d5f27b0 | 2010-04-17 22:54:57 +0000 | [diff] [blame] | 700 | // We have a wide bit-field. The extra bits are only used for padding, so |
| 701 | // if we have a bitfield of type T, with size N: |
| 702 | // |
| 703 | // T t : N; |
| 704 | // |
| 705 | // We can just assume that it's: |
| 706 | // |
| 707 | // T t : sizeof(T); |
| 708 | // |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 709 | Size = TypeSizeInBits; |
Anders Carlsson | be6f318 | 2010-04-16 16:23:02 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Chandler Carruth | fd8eca2 | 2012-12-09 07:26:04 +0000 | [diff] [blame] | 712 | // Reverse the bit offsets for big endian machines. Because we represent |
| 713 | // a bitfield as a single large integer load, we can imagine the bits |
| 714 | // counting from the most-significant-bit instead of the |
| 715 | // least-significant-bit. |
| 716 | if (Types.getDataLayout().isBigEndian()) { |
| 717 | Offset = StorageSize - (Offset + Size); |
| 718 | } |
Chris Lattner | fb59c7c | 2011-02-17 22:09:58 +0000 | [diff] [blame] | 719 | |
Ulrich Weigand | 03ce2a1 | 2015-07-10 17:30:00 +0000 | [diff] [blame] | 720 | return CGBitFieldInfo(Offset, Size, IsSigned, StorageSize, StorageOffset); |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 723 | CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D, |
| 724 | llvm::StructType *Ty) { |
David Majnemer | bb51300 | 2014-09-28 06:39:30 +0000 | [diff] [blame] | 725 | CGRecordLowering Builder(*this, D, /*Packed=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 726 | |
David Majnemer | bb51300 | 2014-09-28 06:39:30 +0000 | [diff] [blame] | 727 | Builder.lower(/*NonVirtualBaseType=*/false); |
Anders Carlsson | e1d5ca5 | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 728 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 729 | // If we're in C++, compute the base subobject type. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 730 | llvm::StructType *BaseTy = nullptr; |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 731 | if (isa<CXXRecordDecl>(D) && !D->isUnion() && !D->hasAttr<FinalAttr>()) { |
| 732 | BaseTy = Ty; |
| 733 | if (Builder.Layout.getNonVirtualSize() != Builder.Layout.getSize()) { |
David Majnemer | bb51300 | 2014-09-28 06:39:30 +0000 | [diff] [blame] | 734 | CGRecordLowering BaseBuilder(*this, D, /*Packed=*/Builder.Packed); |
| 735 | BaseBuilder.lower(/*NonVirtualBaseType=*/true); |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 736 | BaseTy = llvm::StructType::create( |
| 737 | getLLVMContext(), BaseBuilder.FieldTypes, "", BaseBuilder.Packed); |
| 738 | addRecordTypeName(D, BaseTy, ".base"); |
David Majnemer | bb51300 | 2014-09-28 06:39:30 +0000 | [diff] [blame] | 739 | // BaseTy and Ty must agree on their packedness for getLLVMFieldNo to work |
| 740 | // on both of them with the same index. |
| 741 | assert(Builder.Packed == BaseBuilder.Packed && |
| 742 | "Non-virtual and complete types must agree on packedness"); |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 743 | } |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Reid Kleckner | c497f1d | 2014-02-27 00:03:39 +0000 | [diff] [blame] | 746 | // Fill in the struct *after* computing the base type. Filling in the body |
| 747 | // signifies that the type is no longer opaque and record layout is complete, |
| 748 | // but we may need to recursively layout D while laying D out as a base type. |
| 749 | Ty->setBody(Builder.FieldTypes, Builder.Packed); |
| 750 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 751 | CGRecordLayout *RL = |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 752 | new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable, |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 753 | Builder.IsZeroInitializableAsBase); |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 754 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 755 | RL->NonVirtualBases.swap(Builder.NonVirtualBases); |
| 756 | RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases); |
Anders Carlsson | 061ca52 | 2010-05-18 05:22:06 +0000 | [diff] [blame] | 757 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 758 | // Add all the field numbers. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 759 | RL->FieldInfo.swap(Builder.Fields); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 760 | |
| 761 | // Add bitfield info. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 762 | RL->BitFields.swap(Builder.BitFields); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 763 | |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 764 | // Dump the layout, if requested. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 765 | if (getContext().getLangOpts().DumpRecordLayouts) { |
Argyrios Kyrtzidis | 8ade08e | 2013-07-12 22:30:03 +0000 | [diff] [blame] | 766 | llvm::outs() << "\n*** Dumping IRgen Record Layout\n"; |
| 767 | llvm::outs() << "Record: "; |
| 768 | D->dump(llvm::outs()); |
| 769 | llvm::outs() << "\nLayout: "; |
| 770 | RL->print(llvm::outs()); |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 771 | } |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 772 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 773 | #ifndef NDEBUG |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 774 | // Verify that the computed LLVM struct size matches the AST layout size. |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 775 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D); |
| 776 | |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 777 | uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize()); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 778 | assert(TypeSizeInBits == getDataLayout().getTypeAllocSizeInBits(Ty) && |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 779 | "Type size mismatch!"); |
| 780 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 781 | if (BaseTy) { |
Ken Dyck | bec0285 | 2011-02-08 02:02:47 +0000 | [diff] [blame] | 782 | CharUnits NonVirtualSize = Layout.getNonVirtualSize(); |
Ken Dyck | bec0285 | 2011-02-08 02:02:47 +0000 | [diff] [blame] | 783 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 784 | uint64_t AlignedNonVirtualTypeSizeInBits = |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 785 | getContext().toBits(NonVirtualSize); |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 786 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 787 | assert(AlignedNonVirtualTypeSizeInBits == |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 788 | getDataLayout().getTypeAllocSizeInBits(BaseTy) && |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 789 | "Type size mismatch!"); |
| 790 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 791 | |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 792 | // Verify that the LLVM and AST field offsets agree. |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 793 | llvm::StructType *ST = RL->getLLVMType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 794 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(ST); |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 795 | |
| 796 | const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D); |
| 797 | RecordDecl::field_iterator it = D->field_begin(); |
| 798 | for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 799 | const FieldDecl *FD = *it; |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 800 | |
| 801 | // For non-bit-fields, just check that the LLVM struct offset matches the |
| 802 | // AST offset. |
| 803 | if (!FD->isBitField()) { |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 804 | unsigned FieldNo = RL->getLLVMFieldNo(FD); |
| 805 | assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) && |
| 806 | "Invalid field offset!"); |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 807 | continue; |
| 808 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 809 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 810 | // Ignore unnamed bit-fields. |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 811 | if (!FD->getDeclName()) |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 812 | continue; |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 813 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 814 | // Don't inspect zero-length bitfields. |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 815 | if (FD->isZeroLengthBitField(getContext())) |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 816 | continue; |
| 817 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 818 | const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD); |
Chandler Carruth | ed72cdc | 2012-12-09 10:33:27 +0000 | [diff] [blame] | 819 | llvm::Type *ElementTy = ST->getTypeAtIndex(RL->getLLVMFieldNo(FD)); |
| 820 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 821 | // Unions have overlapping elements dictating their layout, but for |
| 822 | // non-unions we can verify that this section of the layout is the exact |
Chandler Carruth | ed72cdc | 2012-12-09 10:33:27 +0000 | [diff] [blame] | 823 | // expected size. |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 824 | if (D->isUnion()) { |
Chandler Carruth | ed72cdc | 2012-12-09 10:33:27 +0000 | [diff] [blame] | 825 | // For unions we verify that the start is zero and the size |
| 826 | // is in-bounds. However, on BE systems, the offset may be non-zero, but |
| 827 | // the size + offset should match the storage size in that case as it |
| 828 | // "starts" at the back. |
| 829 | if (getDataLayout().isBigEndian()) |
David Greene | 464d219 | 2013-01-15 23:13:49 +0000 | [diff] [blame] | 830 | assert(static_cast<unsigned>(Info.Offset + Info.Size) == |
| 831 | Info.StorageSize && |
Chandler Carruth | ed72cdc | 2012-12-09 10:33:27 +0000 | [diff] [blame] | 832 | "Big endian union bitfield does not end at the back"); |
| 833 | else |
| 834 | assert(Info.Offset == 0 && |
| 835 | "Little endian union bitfield with a non-zero offset"); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 836 | assert(Info.StorageSize <= SL->getSizeInBits() && |
| 837 | "Union not large enough for bitfield storage"); |
| 838 | } else { |
| 839 | assert(Info.StorageSize == |
| 840 | getDataLayout().getTypeAllocSizeInBits(ElementTy) && |
| 841 | "Storage size does not match the element type size"); |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 842 | } |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 843 | assert(Info.Size > 0 && "Empty bitfield!"); |
Eli Bendersky | 76bd3d8 | 2012-12-18 18:53:14 +0000 | [diff] [blame] | 844 | assert(static_cast<unsigned>(Info.Offset) + Info.Size <= Info.StorageSize && |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 845 | "Bitfield outside of its allocated storage"); |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 846 | } |
| 847 | #endif |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 848 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 849 | return RL; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 850 | } |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 851 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 852 | void CGRecordLayout::print(raw_ostream &OS) const { |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 853 | OS << "<CGRecordLayout\n"; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 854 | OS << " LLVMType:" << *CompleteObjectType << "\n"; |
| 855 | if (BaseSubobjectType) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 856 | OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n"; |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 857 | OS << " IsZeroInitializable:" << IsZeroInitializable << "\n"; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 858 | OS << " BitFields:[\n"; |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 859 | |
| 860 | // Print bit-field infos in declaration order. |
| 861 | std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 862 | for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator |
| 863 | it = BitFields.begin(), ie = BitFields.end(); |
| 864 | it != ie; ++it) { |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 865 | const RecordDecl *RD = it->first->getParent(); |
| 866 | unsigned Index = 0; |
| 867 | for (RecordDecl::field_iterator |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 868 | it2 = RD->field_begin(); *it2 != it->first; ++it2) |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 869 | ++Index; |
| 870 | BFIs.push_back(std::make_pair(Index, &it->second)); |
| 871 | } |
| 872 | llvm::array_pod_sort(BFIs.begin(), BFIs.end()); |
| 873 | for (unsigned i = 0, e = BFIs.size(); i != e; ++i) { |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 874 | OS.indent(4); |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 875 | BFIs[i].second->print(OS); |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 876 | OS << "\n"; |
| 877 | } |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 878 | |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 879 | OS << "]>\n"; |
| 880 | } |
| 881 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 882 | LLVM_DUMP_METHOD void CGRecordLayout::dump() const { |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 883 | print(llvm::errs()); |
| 884 | } |
| 885 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 886 | void CGBitFieldInfo::print(raw_ostream &OS) const { |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 887 | OS << "<CGBitFieldInfo" |
| 888 | << " Offset:" << Offset |
| 889 | << " Size:" << Size |
| 890 | << " IsSigned:" << IsSigned |
| 891 | << " StorageSize:" << StorageSize |
Ulrich Weigand | 03ce2a1 | 2015-07-10 17:30:00 +0000 | [diff] [blame] | 892 | << " StorageOffset:" << StorageOffset.getQuantity() << ">"; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 895 | LLVM_DUMP_METHOD void CGBitFieldInfo::dump() const { |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 896 | print(llvm::errs()); |
| 897 | } |