Daniel Dunbar | 270e203 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 1 | //===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===// |
Anders Carlsson | 45372a6 | 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 | 270e203 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 10 | // Builder implementation for CGRecordLayout objects. |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | 2924ade | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 14 | #include "CGRecordLayout.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "CGCXXABI.h" |
| 16 | #include "CodeGenTypes.h" |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
| 18 | #include "clang/AST/Attr.h" |
Anders Carlsson | 46170f9 | 2010-11-24 22:50:27 +0000 | [diff] [blame] | 19 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 45372a6 | 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" |
Daniel Dunbar | e26bdb9 | 2011-06-21 18:54:46 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/CodeGenOptions.h" |
Chandler Carruth | 3b844ba | 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 | 3b2ae7a | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 28 | #include "llvm/Support/MathExtras.h" |
Daniel Dunbar | 93c6296 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | using namespace CodeGen; |
| 32 | |
John McCall | d0de0ce | 2010-11-30 23:17:27 +0000 | [diff] [blame] | 33 | namespace { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [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. |
| 65 | /// The location of the clip is stored internally as a sentinal of type |
| 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 |
| 77 | // sentinal member type that ensures correct rounding. |
| 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, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 87 | const FieldDecl *FD = nullptr) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [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. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 96 | CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D, bool Packed); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 97 | // Short helper routines. |
| 98 | /// \brief Constructs a MemberInfo instance from an offset and llvm::Type *. |
| 99 | MemberInfo StorageInfo(CharUnits Offset, llvm::Type *Data) { |
| 100 | return MemberInfo(Offset, MemberInfo::Field, Data); |
| 101 | } |
| 102 | bool useMSABI() { |
| 103 | return Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 104 | D->isMsStruct(Context); |
| 105 | } |
| 106 | /// \brief Wraps llvm::Type::getIntNTy with some implicit arguments. |
| 107 | llvm::Type *getIntNType(uint64_t NumBits) { |
| 108 | return llvm::Type::getIntNTy(Types.getLLVMContext(), |
| 109 | (unsigned)llvm::RoundUpToAlignment(NumBits, 8)); |
| 110 | } |
| 111 | /// \brief Gets an llvm type of size NumBytes and alignment 1. |
| 112 | llvm::Type *getByteArrayType(CharUnits NumBytes) { |
| 113 | assert(!NumBytes.isZero() && "Empty byte arrays aren't allowed."); |
| 114 | llvm::Type *Type = llvm::Type::getInt8Ty(Types.getLLVMContext()); |
| 115 | return NumBytes == CharUnits::One() ? Type : |
| 116 | (llvm::Type *)llvm::ArrayType::get(Type, NumBytes.getQuantity()); |
| 117 | } |
| 118 | /// \brief Gets the storage type for a field decl and handles storage |
| 119 | /// for itanium bitfields that are smaller than their declared type. |
| 120 | llvm::Type *getStorageType(const FieldDecl *FD) { |
| 121 | llvm::Type *Type = Types.ConvertTypeForMem(FD->getType()); |
| 122 | return useMSABI() || !FD->isBitField() ? Type : |
| 123 | getIntNType(std::min(FD->getBitWidthValue(Context), |
| 124 | (unsigned)Context.toBits(getSize(Type)))); |
| 125 | } |
| 126 | /// \brief Gets the llvm Basesubobject type from a CXXRecordDecl. |
| 127 | llvm::Type *getStorageType(const CXXRecordDecl *RD) { |
| 128 | return Types.getCGRecordLayout(RD).getBaseSubobjectLLVMType(); |
| 129 | } |
| 130 | CharUnits bitsToCharUnits(uint64_t BitOffset) { |
| 131 | return Context.toCharUnitsFromBits(BitOffset); |
| 132 | } |
| 133 | CharUnits getSize(llvm::Type *Type) { |
| 134 | return CharUnits::fromQuantity(DataLayout.getTypeAllocSize(Type)); |
| 135 | } |
| 136 | CharUnits getAlignment(llvm::Type *Type) { |
| 137 | return CharUnits::fromQuantity(DataLayout.getABITypeAlignment(Type)); |
| 138 | } |
| 139 | bool isZeroInitializable(const FieldDecl *FD) { |
| 140 | const Type *Type = FD->getType()->getBaseElementTypeUnsafe(); |
| 141 | if (const MemberPointerType *MPT = Type->getAs<MemberPointerType>()) |
| 142 | return Types.getCXXABI().isZeroInitializable(MPT); |
| 143 | if (const RecordType *RT = Type->getAs<RecordType>()) |
| 144 | return isZeroInitializable(RT->getDecl()); |
| 145 | return true; |
| 146 | } |
| 147 | bool isZeroInitializable(const RecordDecl *RD) { |
| 148 | return Types.getCGRecordLayout(RD).isZeroInitializable(); |
| 149 | } |
| 150 | void appendPaddingBytes(CharUnits Size) { |
| 151 | if (!Size.isZero()) |
| 152 | FieldTypes.push_back(getByteArrayType(Size)); |
| 153 | } |
| 154 | uint64_t getFieldBitOffset(const FieldDecl *FD) { |
| 155 | return Layout.getFieldOffset(FD->getFieldIndex()); |
| 156 | } |
| 157 | // Layout routines. |
| 158 | void setBitFieldInfo(const FieldDecl *FD, CharUnits StartOffset, |
| 159 | llvm::Type *StorageType); |
| 160 | /// \brief Lowers an ASTRecordLayout to a llvm type. |
| 161 | void lower(bool NonVirtualBaseType); |
| 162 | void lowerUnion(); |
| 163 | void accumulateFields(); |
| 164 | void accumulateBitFields(RecordDecl::field_iterator Field, |
| 165 | RecordDecl::field_iterator FieldEnd); |
| 166 | void accumulateBases(); |
| 167 | void accumulateVPtrs(); |
| 168 | void accumulateVBases(); |
| 169 | /// \brief Recursively searches all of the bases to find out if a vbase is |
| 170 | /// not the primary vbase of some base class. |
| 171 | bool hasOwnStorage(const CXXRecordDecl *Decl, const CXXRecordDecl *Query); |
| 172 | void calculateZeroInit(); |
| 173 | /// \brief Lowers bitfield storage types to I8 arrays for bitfields with tail |
| 174 | /// padding that is or can potentially be used. |
| 175 | void clipTailPadding(); |
| 176 | /// \brief Determines if we need a packed llvm struct. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 177 | void determinePacked(bool NVBaseType); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 178 | /// \brief Inserts padding everwhere it's needed. |
| 179 | void insertPadding(); |
| 180 | /// \brief Fills out the structures that are ultimately consumed. |
| 181 | void fillOutputFields(); |
| 182 | // Input memoization fields. |
| 183 | CodeGenTypes &Types; |
| 184 | const ASTContext &Context; |
| 185 | const RecordDecl *D; |
| 186 | const CXXRecordDecl *RD; |
| 187 | const ASTRecordLayout &Layout; |
| 188 | const llvm::DataLayout &DataLayout; |
| 189 | // Helpful intermediate data-structures. |
| 190 | std::vector<MemberInfo> Members; |
| 191 | // Output fields, consumed by CodeGenTypes::ComputeRecordLayout. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 192 | SmallVector<llvm::Type *, 16> FieldTypes; |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 193 | llvm::DenseMap<const FieldDecl *, unsigned> Fields; |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 194 | llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields; |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 195 | llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases; |
| 196 | llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 197 | bool IsZeroInitializable : 1; |
| 198 | bool IsZeroInitializableAsBase : 1; |
| 199 | bool Packed : 1; |
Daniel Dunbar | 270e203 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 200 | private: |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 201 | CGRecordLowering(const CGRecordLowering &) = delete; |
| 202 | void operator =(const CGRecordLowering &) = delete; |
Daniel Dunbar | 270e203 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 203 | }; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 204 | } // namespace { |
Daniel Dunbar | 270e203 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 205 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 206 | CGRecordLowering::CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D, bool Packed) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 207 | : Types(Types), Context(Types.getContext()), D(D), |
| 208 | RD(dyn_cast<CXXRecordDecl>(D)), |
| 209 | Layout(Types.getContext().getASTRecordLayout(D)), |
| 210 | DataLayout(Types.getDataLayout()), IsZeroInitializable(true), |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 211 | IsZeroInitializableAsBase(true), Packed(Packed) {} |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 212 | |
| 213 | void CGRecordLowering::setBitFieldInfo( |
| 214 | const FieldDecl *FD, CharUnits StartOffset, llvm::Type *StorageType) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 215 | CGBitFieldInfo &Info = BitFields[FD->getCanonicalDecl()]; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 216 | Info.IsSigned = FD->getType()->isSignedIntegerOrEnumerationType(); |
| 217 | Info.Offset = (unsigned)(getFieldBitOffset(FD) - Context.toBits(StartOffset)); |
| 218 | Info.Size = FD->getBitWidthValue(Context); |
| 219 | Info.StorageSize = (unsigned)DataLayout.getTypeAllocSizeInBits(StorageType); |
| 220 | // Here we calculate the actual storage alignment of the bits. E.g if we've |
| 221 | // got an alignment >= 2 and the bitfield starts at offset 6 we've got an |
| 222 | // alignment of 2. |
| 223 | Info.StorageAlignment = |
| 224 | Layout.getAlignment().alignmentAtOffset(StartOffset).getQuantity(); |
| 225 | if (Info.Size > Info.StorageSize) |
| 226 | Info.Size = Info.StorageSize; |
| 227 | // Reverse the bit offsets for big endian machines. Because we represent |
| 228 | // a bitfield as a single large integer load, we can imagine the bits |
| 229 | // counting from the most-significant-bit instead of the |
| 230 | // least-significant-bit. |
| 231 | if (DataLayout.isBigEndian()) |
| 232 | Info.Offset = Info.StorageSize - (Info.Offset + Info.Size); |
Daniel Dunbar | 270e203 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 233 | } |
Daniel Dunbar | 270e203 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 234 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 235 | void CGRecordLowering::lower(bool NVBaseType) { |
| 236 | // The lowering process implemented in this function takes a variety of |
| 237 | // carefully ordered phases. |
| 238 | // 1) Store all members (fields and bases) in a list and sort them by offset. |
| 239 | // 2) Add a 1-byte capstone member at the Size of the structure. |
| 240 | // 3) Clip bitfield storages members if their tail padding is or might be |
| 241 | // used by another field or base. The clipping process uses the capstone |
| 242 | // by treating it as another object that occurs after the record. |
| 243 | // 4) Determine if the llvm-struct requires packing. It's important that this |
| 244 | // phase occur after clipping, because clipping changes the llvm type. |
| 245 | // This phase reads the offset of the capstone when determining packedness |
| 246 | // and updates the alignment of the capstone to be equal of the alignment |
| 247 | // of the record after doing so. |
| 248 | // 5) Insert padding everywhere it is needed. This phase requires 'Packed' to |
| 249 | // have been computed and needs to know the alignment of the record in |
| 250 | // order to understand if explicit tail padding is needed. |
| 251 | // 6) Remove the capstone, we don't need it anymore. |
| 252 | // 7) Determine if this record can be zero-initialized. This phase could have |
| 253 | // been placed anywhere after phase 1. |
| 254 | // 8) Format the complete list of members in a way that can be consumed by |
| 255 | // CodeGenTypes::ComputeRecordLayout. |
| 256 | CharUnits Size = NVBaseType ? Layout.getNonVirtualSize() : Layout.getSize(); |
| 257 | if (D->isUnion()) |
| 258 | return lowerUnion(); |
| 259 | accumulateFields(); |
| 260 | // RD implies C++. |
| 261 | if (RD) { |
| 262 | accumulateVPtrs(); |
| 263 | accumulateBases(); |
| 264 | if (Members.empty()) |
| 265 | return appendPaddingBytes(Size); |
| 266 | if (!NVBaseType) |
| 267 | accumulateVBases(); |
| 268 | } |
| 269 | std::stable_sort(Members.begin(), Members.end()); |
| 270 | Members.push_back(StorageInfo(Size, getIntNType(8))); |
| 271 | clipTailPadding(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 272 | determinePacked(NVBaseType); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 273 | insertPadding(); |
| 274 | Members.pop_back(); |
| 275 | calculateZeroInit(); |
| 276 | fillOutputFields(); |
| 277 | } |
Anders Carlsson | a5dd722 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 278 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 279 | void CGRecordLowering::lowerUnion() { |
| 280 | CharUnits LayoutSize = Layout.getSize(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 281 | llvm::Type *StorageType = nullptr; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 282 | bool SeenNamedMember = false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 283 | // Iterate through the fields setting bitFieldInfo and the Fields array. Also |
| 284 | // locate the "most appropriate" storage type. The heuristic for finding the |
| 285 | // storage type isn't necessary, the first (non-0-length-bitfield) field's |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 286 | // type would work fine and be simpler but would be different than what we've |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 287 | // been doing and cause lit tests to change. |
| 288 | for (const auto *Field : D->fields()) { |
| 289 | if (Field->isBitField()) { |
| 290 | // Skip 0 sized bitfields. |
| 291 | if (Field->getBitWidthValue(Context) == 0) |
| 292 | continue; |
| 293 | llvm::Type *FieldType = getStorageType(Field); |
| 294 | if (LayoutSize < getSize(FieldType)) |
| 295 | FieldType = getByteArrayType(LayoutSize); |
| 296 | setBitFieldInfo(Field, CharUnits::Zero(), FieldType); |
| 297 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 298 | Fields[Field->getCanonicalDecl()] = 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 299 | llvm::Type *FieldType = getStorageType(Field); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 300 | // Compute zero-initializable status. |
| 301 | // This union might not be zero initialized: it may contain a pointer to |
| 302 | // data member which might have some exotic initialization sequence. |
| 303 | // If this is the case, then we aught not to try and come up with a "better" |
| 304 | // type, it might not be very easy to come up with a Constant which |
| 305 | // correctly initializes it. |
| 306 | if (!SeenNamedMember && Field->getDeclName()) { |
| 307 | SeenNamedMember = true; |
| 308 | if (!isZeroInitializable(Field)) { |
| 309 | IsZeroInitializable = IsZeroInitializableAsBase = false; |
| 310 | StorageType = FieldType; |
| 311 | } |
| 312 | } |
| 313 | // Because our union isn't zero initializable, we won't be getting a better |
| 314 | // storage type. |
| 315 | if (!IsZeroInitializable) |
| 316 | continue; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 317 | // Conditionally update our storage type if we've got a new "better" one. |
| 318 | if (!StorageType || |
| 319 | getAlignment(FieldType) > getAlignment(StorageType) || |
| 320 | (getAlignment(FieldType) == getAlignment(StorageType) && |
| 321 | getSize(FieldType) > getSize(StorageType))) |
| 322 | StorageType = FieldType; |
| 323 | } |
| 324 | // If we have no storage type just pad to the appropriate size and return. |
| 325 | if (!StorageType) |
| 326 | return appendPaddingBytes(LayoutSize); |
| 327 | // If our storage size was bigger than our required size (can happen in the |
| 328 | // case of packed bitfields on Itanium) then just use an I8 array. |
| 329 | if (LayoutSize < getSize(StorageType)) |
| 330 | StorageType = getByteArrayType(LayoutSize); |
| 331 | FieldTypes.push_back(StorageType); |
| 332 | appendPaddingBytes(LayoutSize - getSize(StorageType)); |
| 333 | // Set packed if we need it. |
| 334 | if (LayoutSize % getAlignment(StorageType)) |
| 335 | Packed = true; |
| 336 | } |
| 337 | |
| 338 | void CGRecordLowering::accumulateFields() { |
| 339 | for (RecordDecl::field_iterator Field = D->field_begin(), |
| 340 | FieldEnd = D->field_end(); |
| 341 | Field != FieldEnd;) |
| 342 | if (Field->isBitField()) { |
| 343 | RecordDecl::field_iterator Start = Field; |
| 344 | // Iterate to gather the list of bitfields. |
| 345 | for (++Field; Field != FieldEnd && Field->isBitField(); ++Field); |
| 346 | accumulateBitFields(Start, Field); |
| 347 | } else { |
| 348 | Members.push_back(MemberInfo( |
| 349 | bitsToCharUnits(getFieldBitOffset(*Field)), MemberInfo::Field, |
| 350 | getStorageType(*Field), *Field)); |
| 351 | ++Field; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | void |
| 356 | CGRecordLowering::accumulateBitFields(RecordDecl::field_iterator Field, |
| 357 | RecordDecl::field_iterator FieldEnd) { |
| 358 | // Run stores the first element of the current run of bitfields. FieldEnd is |
| 359 | // used as a special value to note that we don't have a current run. A |
| 360 | // bitfield run is a contiguous collection of bitfields that can be stored in |
| 361 | // the same storage block. Zero-sized bitfields and bitfields that would |
| 362 | // cross an alignment boundary break a run and start a new one. |
| 363 | RecordDecl::field_iterator Run = FieldEnd; |
| 364 | // Tail is the offset of the first bit off the end of the current run. It's |
| 365 | // used to determine if the ASTRecordLayout is treating these two bitfields as |
| 366 | // contiguous. StartBitOffset is offset of the beginning of the Run. |
| 367 | uint64_t StartBitOffset, Tail = 0; |
| 368 | if (useMSABI()) { |
| 369 | for (; Field != FieldEnd; ++Field) { |
| 370 | uint64_t BitOffset = getFieldBitOffset(*Field); |
| 371 | // Zero-width bitfields end runs. |
| 372 | if (Field->getBitWidthValue(Context) == 0) { |
| 373 | Run = FieldEnd; |
| 374 | continue; |
| 375 | } |
| 376 | llvm::Type *Type = Types.ConvertTypeForMem(Field->getType()); |
| 377 | // If we don't have a run yet, or don't live within the previous run's |
| 378 | // allocated storage then we allocate some storage and start a new run. |
| 379 | if (Run == FieldEnd || BitOffset >= Tail) { |
| 380 | Run = Field; |
| 381 | StartBitOffset = BitOffset; |
| 382 | Tail = StartBitOffset + DataLayout.getTypeAllocSizeInBits(Type); |
| 383 | // Add the storage member to the record. This must be added to the |
| 384 | // record before the bitfield members so that it gets laid out before |
| 385 | // the bitfields it contains get laid out. |
| 386 | Members.push_back(StorageInfo(bitsToCharUnits(StartBitOffset), Type)); |
| 387 | } |
| 388 | // Bitfields get the offset of their storage but come afterward and remain |
| 389 | // there after a stable sort. |
| 390 | Members.push_back(MemberInfo(bitsToCharUnits(StartBitOffset), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 391 | MemberInfo::Field, nullptr, *Field)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 392 | } |
Anders Carlsson | 5a6e398 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 393 | return; |
| 394 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 395 | for (;;) { |
| 396 | // Check to see if we need to start a new run. |
| 397 | if (Run == FieldEnd) { |
| 398 | // If we're out of fields, return. |
| 399 | if (Field == FieldEnd) |
| 400 | break; |
| 401 | // Any non-zero-length bitfield can start a new run. |
| 402 | if (Field->getBitWidthValue(Context) != 0) { |
| 403 | Run = Field; |
| 404 | StartBitOffset = getFieldBitOffset(*Field); |
| 405 | Tail = StartBitOffset + Field->getBitWidthValue(Context); |
| 406 | } |
| 407 | ++Field; |
| 408 | continue; |
| 409 | } |
| 410 | // Add bitfields to the run as long as they qualify. |
| 411 | if (Field != FieldEnd && Field->getBitWidthValue(Context) != 0 && |
| 412 | Tail == getFieldBitOffset(*Field)) { |
| 413 | Tail += Field->getBitWidthValue(Context); |
| 414 | ++Field; |
| 415 | continue; |
| 416 | } |
| 417 | // We've hit a break-point in the run and need to emit a storage field. |
| 418 | llvm::Type *Type = getIntNType(Tail - StartBitOffset); |
| 419 | // Add the storage member to the record and set the bitfield info for all of |
| 420 | // the bitfields in the run. Bitfields get the offset of their storage but |
| 421 | // come afterward and remain there after a stable sort. |
| 422 | Members.push_back(StorageInfo(bitsToCharUnits(StartBitOffset), Type)); |
| 423 | for (; Run != Field; ++Run) |
| 424 | Members.push_back(MemberInfo(bitsToCharUnits(StartBitOffset), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 425 | MemberInfo::Field, nullptr, *Run)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 426 | Run = FieldEnd; |
| 427 | } |
| 428 | } |
Anders Carlsson | a860e75 | 2009-08-08 18:23:56 +0000 | [diff] [blame] | 429 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 430 | void CGRecordLowering::accumulateBases() { |
| 431 | // If we've got a primary virtual base, we need to add it with the bases. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 432 | if (Layout.isPrimaryBaseVirtual()) { |
| 433 | const CXXRecordDecl *BaseDecl = Layout.getPrimaryBase(); |
| 434 | Members.push_back(MemberInfo(CharUnits::Zero(), MemberInfo::Base, |
| 435 | getStorageType(BaseDecl), BaseDecl)); |
| 436 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 437 | // Accumulate the non-virtual bases. |
| 438 | for (const auto &Base : RD->bases()) { |
| 439 | if (Base.isVirtual()) |
| 440 | continue; |
| 441 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
| 442 | if (!BaseDecl->isEmpty()) |
| 443 | Members.push_back(MemberInfo(Layout.getBaseClassOffset(BaseDecl), |
| 444 | MemberInfo::Base, getStorageType(BaseDecl), BaseDecl)); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | void CGRecordLowering::accumulateVPtrs() { |
| 449 | if (Layout.hasOwnVFPtr()) |
| 450 | Members.push_back(MemberInfo(CharUnits::Zero(), MemberInfo::VFPtr, |
| 451 | llvm::FunctionType::get(getIntNType(32), /*isVarArg=*/true)-> |
| 452 | getPointerTo()->getPointerTo())); |
| 453 | if (Layout.hasOwnVBPtr()) |
| 454 | Members.push_back(MemberInfo(Layout.getVBPtrOffset(), MemberInfo::VBPtr, |
| 455 | llvm::Type::getInt32PtrTy(Types.getLLVMContext()))); |
| 456 | } |
| 457 | |
| 458 | void CGRecordLowering::accumulateVBases() { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 459 | CharUnits ScissorOffset = Layout.getNonVirtualSize(); |
| 460 | // In the itanium ABI, it's possible to place a vbase at a dsize that is |
| 461 | // smaller than the nvsize. Here we check to see if such a base is placed |
| 462 | // before the nvsize and set the scissor offset to that, instead of the |
| 463 | // nvsize. |
| 464 | if (!useMSABI()) |
| 465 | for (const auto &Base : RD->vbases()) { |
| 466 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
| 467 | if (BaseDecl->isEmpty()) |
| 468 | continue; |
| 469 | // If the vbase is a primary virtual base of some base, then it doesn't |
| 470 | // get its own storage location but instead lives inside of that base. |
| 471 | if (Context.isNearlyEmpty(BaseDecl) && !hasOwnStorage(RD, BaseDecl)) |
| 472 | continue; |
| 473 | ScissorOffset = std::min(ScissorOffset, |
| 474 | Layout.getVBaseClassOffset(BaseDecl)); |
| 475 | } |
| 476 | Members.push_back(MemberInfo(ScissorOffset, MemberInfo::Scissor, nullptr, |
| 477 | RD)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 478 | for (const auto &Base : RD->vbases()) { |
| 479 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
| 480 | if (BaseDecl->isEmpty()) |
| 481 | continue; |
| 482 | CharUnits Offset = Layout.getVBaseClassOffset(BaseDecl); |
| 483 | // If the vbase is a primary virtual base of some base, then it doesn't |
| 484 | // get its own storage location but instead lives inside of that base. |
| 485 | if (!useMSABI() && Context.isNearlyEmpty(BaseDecl) && |
| 486 | !hasOwnStorage(RD, BaseDecl)) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 487 | Members.push_back(MemberInfo(Offset, MemberInfo::VBase, nullptr, |
| 488 | BaseDecl)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 489 | continue; |
| 490 | } |
| 491 | // If we've got a vtordisp, add it as a storage type. |
| 492 | if (Layout.getVBaseOffsetsMap().find(BaseDecl)->second.hasVtorDisp()) |
| 493 | Members.push_back(StorageInfo(Offset - CharUnits::fromQuantity(4), |
| 494 | getIntNType(32))); |
| 495 | Members.push_back(MemberInfo(Offset, MemberInfo::VBase, |
| 496 | getStorageType(BaseDecl), BaseDecl)); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | bool CGRecordLowering::hasOwnStorage(const CXXRecordDecl *Decl, |
| 501 | const CXXRecordDecl *Query) { |
| 502 | const ASTRecordLayout &DeclLayout = Context.getASTRecordLayout(Decl); |
| 503 | if (DeclLayout.isPrimaryBaseVirtual() && DeclLayout.getPrimaryBase() == Query) |
| 504 | return false; |
| 505 | for (const auto &Base : Decl->bases()) |
| 506 | if (!hasOwnStorage(Base.getType()->getAsCXXRecordDecl(), Query)) |
| 507 | return false; |
| 508 | return true; |
| 509 | } |
| 510 | |
| 511 | void CGRecordLowering::calculateZeroInit() { |
| 512 | for (std::vector<MemberInfo>::const_iterator Member = Members.begin(), |
| 513 | MemberEnd = Members.end(); |
| 514 | IsZeroInitializableAsBase && Member != MemberEnd; ++Member) { |
| 515 | if (Member->Kind == MemberInfo::Field) { |
| 516 | if (!Member->FD || isZeroInitializable(Member->FD)) |
| 517 | continue; |
| 518 | IsZeroInitializable = IsZeroInitializableAsBase = false; |
| 519 | } else if (Member->Kind == MemberInfo::Base || |
| 520 | Member->Kind == MemberInfo::VBase) { |
| 521 | if (isZeroInitializable(Member->RD)) |
| 522 | continue; |
| 523 | IsZeroInitializable = false; |
| 524 | if (Member->Kind == MemberInfo::Base) |
| 525 | IsZeroInitializableAsBase = false; |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | void CGRecordLowering::clipTailPadding() { |
| 531 | std::vector<MemberInfo>::iterator Prior = Members.begin(); |
| 532 | CharUnits Tail = getSize(Prior->Data); |
| 533 | for (std::vector<MemberInfo>::iterator Member = Prior + 1, |
| 534 | MemberEnd = Members.end(); |
| 535 | Member != MemberEnd; ++Member) { |
| 536 | // Only members with data and the scissor can cut into tail padding. |
| 537 | if (!Member->Data && Member->Kind != MemberInfo::Scissor) |
| 538 | continue; |
| 539 | if (Member->Offset < Tail) { |
| 540 | assert(Prior->Kind == MemberInfo::Field && !Prior->FD && |
| 541 | "Only storage fields have tail padding!"); |
| 542 | Prior->Data = getByteArrayType(bitsToCharUnits(llvm::RoundUpToAlignment( |
| 543 | cast<llvm::IntegerType>(Prior->Data)->getIntegerBitWidth(), 8))); |
| 544 | } |
| 545 | if (Member->Data) |
| 546 | Prior = Member; |
| 547 | Tail = Prior->Offset + getSize(Prior->Data); |
| 548 | } |
| 549 | } |
| 550 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 551 | void CGRecordLowering::determinePacked(bool NVBaseType) { |
| 552 | if (Packed) |
| 553 | return; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 554 | CharUnits Alignment = CharUnits::One(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 555 | CharUnits NVAlignment = CharUnits::One(); |
| 556 | CharUnits NVSize = |
| 557 | !NVBaseType && RD ? Layout.getNonVirtualSize() : CharUnits::Zero(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 558 | for (std::vector<MemberInfo>::const_iterator Member = Members.begin(), |
| 559 | MemberEnd = Members.end(); |
| 560 | Member != MemberEnd; ++Member) { |
| 561 | if (!Member->Data) |
| 562 | continue; |
| 563 | // If any member falls at an offset that it not a multiple of its alignment, |
| 564 | // then the entire record must be packed. |
| 565 | if (Member->Offset % getAlignment(Member->Data)) |
| 566 | Packed = true; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 567 | if (Member->Offset < NVSize) |
| 568 | NVAlignment = std::max(NVAlignment, getAlignment(Member->Data)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 569 | Alignment = std::max(Alignment, getAlignment(Member->Data)); |
| 570 | } |
| 571 | // If the size of the record (the capstone's offset) is not a multiple of the |
| 572 | // record's alignment, it must be packed. |
| 573 | if (Members.back().Offset % Alignment) |
| 574 | Packed = true; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 575 | // If the non-virtual sub-object is not a multiple of the non-virtual |
| 576 | // sub-object's alignment, it must be packed. We cannot have a packed |
| 577 | // non-virtual sub-object and an unpacked complete object or vise versa. |
| 578 | if (NVSize % NVAlignment) |
| 579 | Packed = true; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 580 | // Update the alignment of the sentinal. |
| 581 | if (!Packed) |
| 582 | Members.back().Data = getIntNType(Context.toBits(Alignment)); |
| 583 | } |
| 584 | |
| 585 | void CGRecordLowering::insertPadding() { |
| 586 | std::vector<std::pair<CharUnits, CharUnits> > Padding; |
| 587 | CharUnits Size = CharUnits::Zero(); |
| 588 | for (std::vector<MemberInfo>::const_iterator Member = Members.begin(), |
| 589 | MemberEnd = Members.end(); |
| 590 | Member != MemberEnd; ++Member) { |
| 591 | if (!Member->Data) |
| 592 | continue; |
| 593 | CharUnits Offset = Member->Offset; |
| 594 | assert(Offset >= Size); |
| 595 | // Insert padding if we need to. |
| 596 | if (Offset != Size.RoundUpToAlignment(Packed ? CharUnits::One() : |
| 597 | getAlignment(Member->Data))) |
| 598 | Padding.push_back(std::make_pair(Size, Offset - Size)); |
| 599 | Size = Offset + getSize(Member->Data); |
| 600 | } |
| 601 | if (Padding.empty()) |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 602 | return; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 603 | // Add the padding to the Members list and sort it. |
| 604 | for (std::vector<std::pair<CharUnits, CharUnits> >::const_iterator |
| 605 | Pad = Padding.begin(), PadEnd = Padding.end(); |
| 606 | Pad != PadEnd; ++Pad) |
| 607 | Members.push_back(StorageInfo(Pad->first, getByteArrayType(Pad->second))); |
| 608 | std::stable_sort(Members.begin(), Members.end()); |
| 609 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 611 | void CGRecordLowering::fillOutputFields() { |
| 612 | for (std::vector<MemberInfo>::const_iterator Member = Members.begin(), |
| 613 | MemberEnd = Members.end(); |
| 614 | Member != MemberEnd; ++Member) { |
| 615 | if (Member->Data) |
| 616 | FieldTypes.push_back(Member->Data); |
| 617 | if (Member->Kind == MemberInfo::Field) { |
| 618 | if (Member->FD) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 619 | Fields[Member->FD->getCanonicalDecl()] = FieldTypes.size() - 1; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 620 | // A field without storage must be a bitfield. |
| 621 | if (!Member->Data) |
| 622 | setBitFieldInfo(Member->FD, Member->Offset, FieldTypes.back()); |
| 623 | } else if (Member->Kind == MemberInfo::Base) |
| 624 | NonVirtualBases[Member->RD] = FieldTypes.size() - 1; |
| 625 | else if (Member->Kind == MemberInfo::VBase) |
| 626 | VirtualBases[Member->RD] = FieldTypes.size() - 1; |
| 627 | } |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Daniel Dunbar | e7a80bd | 2010-09-02 23:53:28 +0000 | [diff] [blame] | 630 | CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types, |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 631 | const FieldDecl *FD, |
| 632 | uint64_t Offset, uint64_t Size, |
| 633 | uint64_t StorageSize, |
| 634 | uint64_t StorageAlignment) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 635 | // This function is vestigial from CGRecordLayoutBuilder days but is still |
| 636 | // used in GCObjCRuntime.cpp. That usage has a "fixme" attached to it that |
| 637 | // when addressed will allow for the removal of this function. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 638 | llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType()); |
John McCall | 92ee7ca | 2011-02-26 08:41:59 +0000 | [diff] [blame] | 639 | CharUnits TypeSizeInBytes = |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 640 | CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(Ty)); |
John McCall | 92ee7ca | 2011-02-26 08:41:59 +0000 | [diff] [blame] | 641 | uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes); |
Daniel Dunbar | 9b28daf | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 642 | |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 643 | bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType(); |
Daniel Dunbar | 9b28daf | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 644 | |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 645 | if (Size > TypeSizeInBits) { |
Anders Carlsson | 6ba3815 | 2010-04-17 22:54:57 +0000 | [diff] [blame] | 646 | // We have a wide bit-field. The extra bits are only used for padding, so |
| 647 | // if we have a bitfield of type T, with size N: |
| 648 | // |
| 649 | // T t : N; |
| 650 | // |
| 651 | // We can just assume that it's: |
| 652 | // |
| 653 | // T t : sizeof(T); |
| 654 | // |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 655 | Size = TypeSizeInBits; |
Anders Carlsson | 1c7658f | 2010-04-16 16:23:02 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Chandler Carruth | ef8d516 | 2012-12-09 07:26:04 +0000 | [diff] [blame] | 658 | // Reverse the bit offsets for big endian machines. Because we represent |
| 659 | // a bitfield as a single large integer load, we can imagine the bits |
| 660 | // counting from the most-significant-bit instead of the |
| 661 | // least-significant-bit. |
| 662 | if (Types.getDataLayout().isBigEndian()) { |
| 663 | Offset = StorageSize - (Offset + Size); |
| 664 | } |
Chris Lattner | d8df5b6 | 2011-02-17 22:09:58 +0000 | [diff] [blame] | 665 | |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 666 | return CGBitFieldInfo(Offset, Size, IsSigned, StorageSize, StorageAlignment); |
Daniel Dunbar | 9b28daf | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 669 | CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D, |
| 670 | llvm::StructType *Ty) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 671 | CGRecordLowering Builder(*this, D, /*Packed=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 672 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 673 | Builder.lower(/*NonVirtualBaseType=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 674 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 675 | // If we're in C++, compute the base subobject type. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 676 | llvm::StructType *BaseTy = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 677 | if (isa<CXXRecordDecl>(D) && !D->isUnion() && !D->hasAttr<FinalAttr>()) { |
| 678 | BaseTy = Ty; |
| 679 | if (Builder.Layout.getNonVirtualSize() != Builder.Layout.getSize()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 680 | CGRecordLowering BaseBuilder(*this, D, /*Packed=*/Builder.Packed); |
| 681 | BaseBuilder.lower(/*NonVirtualBaseType=*/true); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 682 | BaseTy = llvm::StructType::create( |
| 683 | getLLVMContext(), BaseBuilder.FieldTypes, "", BaseBuilder.Packed); |
| 684 | addRecordTypeName(D, BaseTy, ".base"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 685 | // BaseTy and Ty must agree on their packedness for getLLVMFieldNo to work |
| 686 | // on both of them with the same index. |
| 687 | assert(Builder.Packed == BaseBuilder.Packed && |
| 688 | "Non-virtual and complete types must agree on packedness"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 689 | } |
Anders Carlsson | 3d155e6 | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 692 | // Fill in the struct *after* computing the base type. Filling in the body |
| 693 | // signifies that the type is no longer opaque and record layout is complete, |
| 694 | // but we may need to recursively layout D while laying D out as a base type. |
| 695 | Ty->setBody(Builder.FieldTypes, Builder.Packed); |
| 696 | |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 697 | CGRecordLayout *RL = |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 698 | new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 699 | Builder.IsZeroInitializableAsBase); |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 700 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 701 | RL->NonVirtualBases.swap(Builder.NonVirtualBases); |
| 702 | RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases); |
Anders Carlsson | c6772ce | 2010-05-18 05:22:06 +0000 | [diff] [blame] | 703 | |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 704 | // Add all the field numbers. |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 705 | RL->FieldInfo.swap(Builder.Fields); |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 706 | |
| 707 | // Add bitfield info. |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 708 | RL->BitFields.swap(Builder.BitFields); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 709 | |
Daniel Dunbar | 2e7b7c2 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 710 | // Dump the layout, if requested. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 711 | if (getContext().getLangOpts().DumpRecordLayouts) { |
Argyrios Kyrtzidis | c250192 | 2013-07-12 22:30:03 +0000 | [diff] [blame] | 712 | llvm::outs() << "\n*** Dumping IRgen Record Layout\n"; |
| 713 | llvm::outs() << "Record: "; |
| 714 | D->dump(llvm::outs()); |
| 715 | llvm::outs() << "\nLayout: "; |
| 716 | RL->print(llvm::outs()); |
Daniel Dunbar | ab970f9 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 717 | } |
Daniel Dunbar | 93c6296 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 718 | |
Daniel Dunbar | e1467a4 | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 719 | #ifndef NDEBUG |
Daniel Dunbar | 2e7b7c2 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 720 | // Verify that the computed LLVM struct size matches the AST layout size. |
Anders Carlsson | 3d155e6 | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 721 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D); |
| 722 | |
Ken Dyck | dd76a9a | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 723 | uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize()); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 724 | assert(TypeSizeInBits == getDataLayout().getTypeAllocSizeInBits(Ty) && |
Daniel Dunbar | 2e7b7c2 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 725 | "Type size mismatch!"); |
| 726 | |
Anders Carlsson | 3d155e6 | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 727 | if (BaseTy) { |
Ken Dyck | 68cf1a5 | 2011-02-08 02:02:47 +0000 | [diff] [blame] | 728 | CharUnits NonVirtualSize = Layout.getNonVirtualSize(); |
Ken Dyck | 68cf1a5 | 2011-02-08 02:02:47 +0000 | [diff] [blame] | 729 | |
| 730 | uint64_t AlignedNonVirtualTypeSizeInBits = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 731 | getContext().toBits(NonVirtualSize); |
Anders Carlsson | 3d155e6 | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 732 | |
| 733 | assert(AlignedNonVirtualTypeSizeInBits == |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 734 | getDataLayout().getTypeAllocSizeInBits(BaseTy) && |
Anders Carlsson | 3d155e6 | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 735 | "Type size mismatch!"); |
| 736 | } |
| 737 | |
Daniel Dunbar | 3b2ae7a | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 738 | // Verify that the LLVM and AST field offsets agree. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 739 | llvm::StructType *ST = |
Daniel Dunbar | 3b2ae7a | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 740 | dyn_cast<llvm::StructType>(RL->getLLVMType()); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 741 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(ST); |
Daniel Dunbar | 3b2ae7a | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 742 | |
| 743 | const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D); |
| 744 | RecordDecl::field_iterator it = D->field_begin(); |
| 745 | for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 746 | const FieldDecl *FD = *it; |
Daniel Dunbar | e1467a4 | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 747 | |
| 748 | // For non-bit-fields, just check that the LLVM struct offset matches the |
| 749 | // AST offset. |
| 750 | if (!FD->isBitField()) { |
Daniel Dunbar | 3b2ae7a | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 751 | unsigned FieldNo = RL->getLLVMFieldNo(FD); |
| 752 | assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) && |
| 753 | "Invalid field offset!"); |
Daniel Dunbar | e1467a4 | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 754 | continue; |
| 755 | } |
Fariborz Jahanian | 62055b0 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 756 | |
Daniel Dunbar | e1467a4 | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 757 | // Ignore unnamed bit-fields. |
Eli Friedman | cd06f26 | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 758 | if (!FD->getDeclName()) |
Daniel Dunbar | e1467a4 | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 759 | continue; |
Daniel Dunbar | e1467a4 | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 760 | |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 761 | // Don't inspect zero-length bitfields. |
| 762 | if (FD->getBitWidthValue(getContext()) == 0) |
| 763 | continue; |
| 764 | |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 765 | const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD); |
Chandler Carruth | b50d7a3 | 2012-12-09 10:33:27 +0000 | [diff] [blame] | 766 | llvm::Type *ElementTy = ST->getTypeAtIndex(RL->getLLVMFieldNo(FD)); |
| 767 | |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 768 | // Unions have overlapping elements dictating their layout, but for |
| 769 | // non-unions we can verify that this section of the layout is the exact |
Chandler Carruth | b50d7a3 | 2012-12-09 10:33:27 +0000 | [diff] [blame] | 770 | // expected size. |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 771 | if (D->isUnion()) { |
Chandler Carruth | b50d7a3 | 2012-12-09 10:33:27 +0000 | [diff] [blame] | 772 | // For unions we verify that the start is zero and the size |
| 773 | // is in-bounds. However, on BE systems, the offset may be non-zero, but |
| 774 | // the size + offset should match the storage size in that case as it |
| 775 | // "starts" at the back. |
| 776 | if (getDataLayout().isBigEndian()) |
David Greene | ed6355d | 2013-01-15 23:13:49 +0000 | [diff] [blame] | 777 | assert(static_cast<unsigned>(Info.Offset + Info.Size) == |
| 778 | Info.StorageSize && |
Chandler Carruth | b50d7a3 | 2012-12-09 10:33:27 +0000 | [diff] [blame] | 779 | "Big endian union bitfield does not end at the back"); |
| 780 | else |
| 781 | assert(Info.Offset == 0 && |
| 782 | "Little endian union bitfield with a non-zero offset"); |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 783 | assert(Info.StorageSize <= SL->getSizeInBits() && |
| 784 | "Union not large enough for bitfield storage"); |
| 785 | } else { |
| 786 | assert(Info.StorageSize == |
| 787 | getDataLayout().getTypeAllocSizeInBits(ElementTy) && |
| 788 | "Storage size does not match the element type size"); |
Daniel Dunbar | 3b2ae7a | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 789 | } |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 790 | assert(Info.Size > 0 && "Empty bitfield!"); |
Eli Bendersky | 6bd1203 | 2012-12-18 18:53:14 +0000 | [diff] [blame] | 791 | assert(static_cast<unsigned>(Info.Offset) + Info.Size <= Info.StorageSize && |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 792 | "Bitfield outside of its allocated storage"); |
Daniel Dunbar | 3b2ae7a | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 793 | } |
| 794 | #endif |
Daniel Dunbar | 2e7b7c2 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 795 | |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 796 | return RL; |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 797 | } |
Daniel Dunbar | 93c6296 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 798 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 799 | void CGRecordLayout::print(raw_ostream &OS) const { |
Daniel Dunbar | 93c6296 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 800 | OS << "<CGRecordLayout\n"; |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 801 | OS << " LLVMType:" << *CompleteObjectType << "\n"; |
| 802 | if (BaseSubobjectType) |
| 803 | OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n"; |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 804 | OS << " IsZeroInitializable:" << IsZeroInitializable << "\n"; |
Daniel Dunbar | 93c6296 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 805 | OS << " BitFields:[\n"; |
Daniel Dunbar | ad75953 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 806 | |
| 807 | // Print bit-field infos in declaration order. |
| 808 | std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs; |
Daniel Dunbar | 93c6296 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 809 | for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator |
| 810 | it = BitFields.begin(), ie = BitFields.end(); |
| 811 | it != ie; ++it) { |
Daniel Dunbar | ad75953 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 812 | const RecordDecl *RD = it->first->getParent(); |
| 813 | unsigned Index = 0; |
| 814 | for (RecordDecl::field_iterator |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 815 | it2 = RD->field_begin(); *it2 != it->first; ++it2) |
Daniel Dunbar | ad75953 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 816 | ++Index; |
| 817 | BFIs.push_back(std::make_pair(Index, &it->second)); |
| 818 | } |
| 819 | llvm::array_pod_sort(BFIs.begin(), BFIs.end()); |
| 820 | for (unsigned i = 0, e = BFIs.size(); i != e; ++i) { |
Daniel Dunbar | ab970f9 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 821 | OS.indent(4); |
Daniel Dunbar | ad75953 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 822 | BFIs[i].second->print(OS); |
Daniel Dunbar | 93c6296 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 823 | OS << "\n"; |
| 824 | } |
Daniel Dunbar | ad75953 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 825 | |
Daniel Dunbar | 93c6296 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 826 | OS << "]>\n"; |
| 827 | } |
| 828 | |
| 829 | void CGRecordLayout::dump() const { |
| 830 | print(llvm::errs()); |
| 831 | } |
| 832 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 833 | void CGBitFieldInfo::print(raw_ostream &OS) const { |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 834 | OS << "<CGBitFieldInfo" |
| 835 | << " Offset:" << Offset |
| 836 | << " Size:" << Size |
| 837 | << " IsSigned:" << IsSigned |
| 838 | << " StorageSize:" << StorageSize |
| 839 | << " StorageAlignment:" << StorageAlignment << ">"; |
Daniel Dunbar | 93c6296 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | void CGBitFieldInfo::dump() const { |
| 843 | print(llvm::errs()); |
| 844 | } |