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" |
Daniel Dunbar | d3f3d93 | 2011-06-21 18:54:46 +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. |
| 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, |
| 87 | const FieldDecl *FD = 0) |
| 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. |
| 96 | CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D); |
| 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. |
David Majnemer | 7a72601 | 2014-02-22 00:41:07 +0000 | [diff] [blame] | 112 | llvm::Type *getByteArrayType(CharUnits NumBytes) { |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 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. |
| 177 | void determinePacked(); |
| 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 | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 192 | SmallVector<llvm::Type *, 16> FieldTypes; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 193 | llvm::DenseMap<const FieldDecl *, unsigned> Fields; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 194 | llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 195 | llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases; |
| 196 | llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases; |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 197 | bool IsZeroInitializable : 1; |
| 198 | bool IsZeroInitializableAsBase : 1; |
| 199 | bool Packed : 1; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 200 | private: |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 201 | CGRecordLowering(const CGRecordLowering &) LLVM_DELETED_FUNCTION; |
| 202 | void operator =(const CGRecordLowering &) LLVM_DELETED_FUNCTION; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 203 | }; |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 204 | } // namespace { |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 205 | |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 206 | CGRecordLowering::CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D) |
Warren Hunt | 0afa2d2 | 2014-02-22 00:22:15 +0000 | [diff] [blame] | 207 | : Types(Types), Context(Types.getContext()), D(D), |
| 208 | RD(dyn_cast<CXXRecordDecl>(D)), |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 209 | Layout(Types.getContext().getASTRecordLayout(D)), |
Warren Hunt | 0afa2d2 | 2014-02-22 00:22:15 +0000 | [diff] [blame] | 210 | DataLayout(Types.getDataLayout()), IsZeroInitializable(true), |
| 211 | IsZeroInitializableAsBase(true), Packed(false) {} |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 212 | |
| 213 | void CGRecordLowering::setBitFieldInfo( |
| 214 | const FieldDecl *FD, CharUnits StartOffset, llvm::Type *StorageType) { |
| 215 | CGBitFieldInfo &Info = BitFields[FD]; |
| 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. |
David Majnemer | e385d89 | 2014-02-25 01:20:15 +0000 | [diff] [blame] | 223 | Info.StorageAlignment = |
| 224 | Layout.getAlignment().alignmentAtOffset(StartOffset).getQuantity(); |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 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 | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 233 | } |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 234 | |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [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(); |
| 272 | determinePacked(); |
| 273 | insertPadding(); |
| 274 | Members.pop_back(); |
| 275 | calculateZeroInit(); |
| 276 | fillOutputFields(); |
| 277 | } |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 278 | |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 279 | void CGRecordLowering::lowerUnion() { |
Warren Hunt | fed5597 | 2014-03-01 00:38:40 +0000 | [diff] [blame^] | 280 | CharUnits LayoutSize = Layout.getSize(); |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 281 | llvm::Type *StorageType = 0; |
| 282 | // Compute zero-initializable status. |
| 283 | if (!D->field_empty() && !isZeroInitializable(*D->field_begin())) |
| 284 | IsZeroInitializable = IsZeroInitializableAsBase = false; |
| 285 | // Iterate through the fields setting bitFieldInfo and the Fields array. Also |
| 286 | // locate the "most appropriate" storage type. The heuristic for finding the |
| 287 | // storage type isn't necessary, the first (non-0-length-bitfield) field's |
| 288 | // type would work fine and be simpler but would be differen than what we've |
| 289 | // been doing and cause lit tests to change. |
| 290 | for (RecordDecl::field_iterator Field = D->field_begin(), |
| 291 | FieldEnd = D->field_end(); |
| 292 | Field != FieldEnd; ++Field) { |
| 293 | if (Field->isBitField()) { |
| 294 | // Skip 0 sized bitfields. |
| 295 | if (Field->getBitWidthValue(Context) == 0) |
| 296 | continue; |
Warren Hunt | fed5597 | 2014-03-01 00:38:40 +0000 | [diff] [blame^] | 297 | llvm::Type *FieldType = getStorageType(*Field); |
| 298 | if (LayoutSize < getSize(FieldType)) |
| 299 | FieldType = getByteArrayType(LayoutSize); |
| 300 | setBitFieldInfo(*Field, CharUnits::Zero(), FieldType); |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 301 | } |
| 302 | Fields[*Field] = 0; |
| 303 | llvm::Type *FieldType = getStorageType(*Field); |
| 304 | // Conditionally update our storage type if we've got a new "better" one. |
| 305 | if (!StorageType || |
| 306 | getAlignment(FieldType) > getAlignment(StorageType) || |
| 307 | (getAlignment(FieldType) == getAlignment(StorageType) && |
| 308 | getSize(FieldType) > getSize(StorageType))) |
| 309 | StorageType = FieldType; |
| 310 | } |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 311 | // If we have no storage type just pad to the appropriate size and return. |
| 312 | if (!StorageType) |
| 313 | return appendPaddingBytes(LayoutSize); |
| 314 | // If our storage size was bigger than our required size (can happen in the |
| 315 | // case of packed bitfields on Itanium) then just use an I8 array. |
| 316 | if (LayoutSize < getSize(StorageType)) |
| 317 | StorageType = getByteArrayType(LayoutSize); |
| 318 | FieldTypes.push_back(StorageType); |
| 319 | appendPaddingBytes(LayoutSize - getSize(StorageType)); |
| 320 | // Set packed if we need it. |
| 321 | if (LayoutSize % getAlignment(StorageType)) |
| 322 | Packed = true; |
| 323 | } |
| 324 | |
| 325 | void CGRecordLowering::accumulateFields() { |
| 326 | for (RecordDecl::field_iterator Field = D->field_begin(), |
| 327 | FieldEnd = D->field_end(); |
| 328 | Field != FieldEnd;) |
| 329 | if (Field->isBitField()) { |
| 330 | RecordDecl::field_iterator Start = Field; |
| 331 | // Iterate to gather the list of bitfields. |
| 332 | for (++Field; Field != FieldEnd && Field->isBitField(); ++Field); |
| 333 | accumulateBitFields(Start, Field); |
| 334 | } else { |
| 335 | Members.push_back(MemberInfo( |
| 336 | bitsToCharUnits(getFieldBitOffset(*Field)), MemberInfo::Field, |
| 337 | getStorageType(*Field), *Field)); |
| 338 | ++Field; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | void |
| 343 | CGRecordLowering::accumulateBitFields(RecordDecl::field_iterator Field, |
| 344 | RecordDecl::field_iterator FieldEnd) { |
| 345 | // Run stores the first element of the current run of bitfields. FieldEnd is |
| 346 | // used as a special value to note that we don't have a current run. A |
| 347 | // bitfield run is a contiguous collection of bitfields that can be stored in |
| 348 | // the same storage block. Zero-sized bitfields and bitfields that would |
| 349 | // cross an alignment boundary break a run and start a new one. |
| 350 | RecordDecl::field_iterator Run = FieldEnd; |
| 351 | // Tail is the offset of the first bit off the end of the current run. It's |
| 352 | // used to determine if the ASTRecordLayout is treating these two bitfields as |
| 353 | // contiguous. StartBitOffset is offset of the beginning of the Run. |
| 354 | uint64_t StartBitOffset, Tail = 0; |
| 355 | if (useMSABI()) { |
| 356 | for (; Field != FieldEnd; ++Field) { |
| 357 | uint64_t BitOffset = getFieldBitOffset(*Field); |
| 358 | // Zero-width bitfields end runs. |
| 359 | if (Field->getBitWidthValue(Context) == 0) { |
| 360 | Run = FieldEnd; |
| 361 | continue; |
| 362 | } |
| 363 | llvm::Type *Type = Types.ConvertTypeForMem(Field->getType()); |
| 364 | // If we don't have a run yet, or don't live within the previous run's |
| 365 | // allocated storage then we allocate some storage and start a new run. |
| 366 | if (Run == FieldEnd || BitOffset >= Tail) { |
| 367 | Run = Field; |
| 368 | StartBitOffset = BitOffset; |
| 369 | Tail = StartBitOffset + DataLayout.getTypeAllocSizeInBits(Type); |
| 370 | // Add the storage member to the record. This must be added to the |
| 371 | // record before the bitfield members so that it gets laid out before |
| 372 | // the bitfields it contains get laid out. |
| 373 | Members.push_back(StorageInfo(bitsToCharUnits(StartBitOffset), Type)); |
| 374 | } |
| 375 | // Bitfields get the offset of their storage but come afterward and remain |
| 376 | // there after a stable sort. |
| 377 | Members.push_back(MemberInfo(bitsToCharUnits(StartBitOffset), |
| 378 | MemberInfo::Field, 0, *Field)); |
| 379 | } |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 380 | return; |
| 381 | } |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 382 | for (;;) { |
| 383 | // Check to see if we need to start a new run. |
| 384 | if (Run == FieldEnd) { |
| 385 | // If we're out of fields, return. |
| 386 | if (Field == FieldEnd) |
| 387 | break; |
| 388 | // Any non-zero-length bitfield can start a new run. |
| 389 | if (Field->getBitWidthValue(Context) != 0) { |
| 390 | Run = Field; |
| 391 | StartBitOffset = getFieldBitOffset(*Field); |
| 392 | Tail = StartBitOffset + Field->getBitWidthValue(Context); |
| 393 | } |
| 394 | ++Field; |
| 395 | continue; |
| 396 | } |
| 397 | // Add bitfields to the run as long as they qualify. |
| 398 | if (Field != FieldEnd && Field->getBitWidthValue(Context) != 0 && |
| 399 | Tail == getFieldBitOffset(*Field)) { |
| 400 | Tail += Field->getBitWidthValue(Context); |
| 401 | ++Field; |
| 402 | continue; |
| 403 | } |
| 404 | // We've hit a break-point in the run and need to emit a storage field. |
| 405 | llvm::Type *Type = getIntNType(Tail - StartBitOffset); |
| 406 | // Add the storage member to the record and set the bitfield info for all of |
| 407 | // the bitfields in the run. Bitfields get the offset of their storage but |
| 408 | // come afterward and remain there after a stable sort. |
| 409 | Members.push_back(StorageInfo(bitsToCharUnits(StartBitOffset), Type)); |
| 410 | for (; Run != Field; ++Run) |
| 411 | Members.push_back(MemberInfo(bitsToCharUnits(StartBitOffset), |
| 412 | MemberInfo::Field, 0, *Run)); |
| 413 | Run = FieldEnd; |
| 414 | } |
| 415 | } |
Anders Carlsson | 68e0b68 | 2009-08-08 18:23:56 +0000 | [diff] [blame] | 416 | |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 417 | void CGRecordLowering::accumulateBases() { |
| 418 | // If we've got a primary virtual base, we need to add it with the bases. |
| 419 | if (Layout.isPrimaryBaseVirtual()) |
| 420 | Members.push_back(StorageInfo( |
| 421 | CharUnits::Zero(), |
| 422 | getStorageType(Layout.getPrimaryBase()))); |
| 423 | // Accumulate the non-virtual bases. |
| 424 | for (CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin(), |
| 425 | BaseEnd = RD->bases_end(); |
| 426 | Base != BaseEnd; ++Base) { |
| 427 | if (Base->isVirtual()) |
| 428 | continue; |
| 429 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 430 | if (!BaseDecl->isEmpty()) |
| 431 | Members.push_back(MemberInfo(Layout.getBaseClassOffset(BaseDecl), |
| 432 | MemberInfo::Base, getStorageType(BaseDecl), BaseDecl)); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | void CGRecordLowering::accumulateVPtrs() { |
| 437 | if (Layout.hasOwnVFPtr()) |
| 438 | Members.push_back(MemberInfo(CharUnits::Zero(), MemberInfo::VFPtr, |
| 439 | llvm::FunctionType::get(getIntNType(32), /*isVarArg=*/true)-> |
| 440 | getPointerTo()->getPointerTo())); |
| 441 | if (Layout.hasOwnVBPtr()) |
| 442 | Members.push_back(MemberInfo(Layout.getVBPtrOffset(), MemberInfo::VBPtr, |
| 443 | llvm::Type::getInt32PtrTy(Types.getLLVMContext()))); |
| 444 | } |
| 445 | |
| 446 | void CGRecordLowering::accumulateVBases() { |
| 447 | Members.push_back(MemberInfo(Layout.getNonVirtualSize(), |
| 448 | MemberInfo::Scissor, 0, RD)); |
| 449 | for (CXXRecordDecl::base_class_const_iterator Base = RD->vbases_begin(), |
| 450 | BaseEnd = RD->vbases_end(); |
| 451 | Base != BaseEnd; ++Base) { |
| 452 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 453 | if (BaseDecl->isEmpty()) |
| 454 | continue; |
| 455 | CharUnits Offset = Layout.getVBaseClassOffset(BaseDecl); |
| 456 | // If the vbase is a primary virtual base of some base, then it doesn't |
| 457 | // get its own storage location but instead lives inside of that base. |
| 458 | if (!useMSABI() && Context.isNearlyEmpty(BaseDecl) && |
| 459 | !hasOwnStorage(RD, BaseDecl)) { |
| 460 | Members.push_back(MemberInfo(Offset, MemberInfo::VBase, 0, BaseDecl)); |
| 461 | continue; |
| 462 | } |
| 463 | // If we've got a vtordisp, add it as a storage type. |
| 464 | if (Layout.getVBaseOffsetsMap().find(BaseDecl)->second.hasVtorDisp()) |
| 465 | Members.push_back(StorageInfo(Offset - CharUnits::fromQuantity(4), |
| 466 | getIntNType(32))); |
| 467 | Members.push_back(MemberInfo(Offset, MemberInfo::VBase, |
| 468 | getStorageType(BaseDecl), BaseDecl)); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | bool CGRecordLowering::hasOwnStorage(const CXXRecordDecl *Decl, |
| 473 | const CXXRecordDecl *Query) { |
| 474 | const ASTRecordLayout &DeclLayout = Context.getASTRecordLayout(Decl); |
| 475 | if (DeclLayout.isPrimaryBaseVirtual() && DeclLayout.getPrimaryBase() == Query) |
| 476 | return false; |
| 477 | for (CXXRecordDecl::base_class_const_iterator Base = Decl->bases_begin(), |
| 478 | BaseEnd = Decl->bases_end(); |
| 479 | Base != BaseEnd; ++Base) |
| 480 | if (!hasOwnStorage(Base->getType()->getAsCXXRecordDecl(), Query)) |
| 481 | return false; |
| 482 | return true; |
| 483 | } |
| 484 | |
| 485 | void CGRecordLowering::calculateZeroInit() { |
| 486 | for (std::vector<MemberInfo>::const_iterator Member = Members.begin(), |
| 487 | MemberEnd = Members.end(); |
| 488 | IsZeroInitializableAsBase && Member != MemberEnd; ++Member) { |
| 489 | if (Member->Kind == MemberInfo::Field) { |
| 490 | if (!Member->FD || isZeroInitializable(Member->FD)) |
| 491 | continue; |
| 492 | IsZeroInitializable = IsZeroInitializableAsBase = false; |
| 493 | } else if (Member->Kind == MemberInfo::Base || |
| 494 | Member->Kind == MemberInfo::VBase) { |
| 495 | if (isZeroInitializable(Member->RD)) |
| 496 | continue; |
| 497 | IsZeroInitializable = false; |
| 498 | if (Member->Kind == MemberInfo::Base) |
| 499 | IsZeroInitializableAsBase = false; |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | void CGRecordLowering::clipTailPadding() { |
| 505 | std::vector<MemberInfo>::iterator Prior = Members.begin(); |
| 506 | CharUnits Tail = getSize(Prior->Data); |
| 507 | for (std::vector<MemberInfo>::iterator Member = Prior + 1, |
| 508 | MemberEnd = Members.end(); |
| 509 | Member != MemberEnd; ++Member) { |
| 510 | // Only members with data and the scissor can cut into tail padding. |
| 511 | if (!Member->Data && Member->Kind != MemberInfo::Scissor) |
| 512 | continue; |
| 513 | if (Member->Offset < Tail) { |
| 514 | assert(Prior->Kind == MemberInfo::Field && !Prior->FD && |
| 515 | "Only storage fields have tail padding!"); |
| 516 | Prior->Data = getByteArrayType(bitsToCharUnits(llvm::RoundUpToAlignment( |
| 517 | cast<llvm::IntegerType>(Prior->Data)->getIntegerBitWidth(), 8))); |
| 518 | } |
| 519 | if (Member->Data) |
| 520 | Prior = Member; |
| 521 | Tail = Prior->Offset + getSize(Prior->Data); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | void CGRecordLowering::determinePacked() { |
| 526 | CharUnits Alignment = CharUnits::One(); |
| 527 | for (std::vector<MemberInfo>::const_iterator Member = Members.begin(), |
| 528 | MemberEnd = Members.end(); |
| 529 | Member != MemberEnd; ++Member) { |
| 530 | if (!Member->Data) |
| 531 | continue; |
| 532 | // If any member falls at an offset that it not a multiple of its alignment, |
| 533 | // then the entire record must be packed. |
| 534 | if (Member->Offset % getAlignment(Member->Data)) |
| 535 | Packed = true; |
| 536 | Alignment = std::max(Alignment, getAlignment(Member->Data)); |
| 537 | } |
| 538 | // If the size of the record (the capstone's offset) is not a multiple of the |
| 539 | // record's alignment, it must be packed. |
| 540 | if (Members.back().Offset % Alignment) |
| 541 | Packed = true; |
| 542 | // Update the alignment of the sentinal. |
| 543 | if (!Packed) |
| 544 | Members.back().Data = getIntNType(Context.toBits(Alignment)); |
| 545 | } |
| 546 | |
| 547 | void CGRecordLowering::insertPadding() { |
| 548 | std::vector<std::pair<CharUnits, CharUnits> > Padding; |
| 549 | CharUnits Size = CharUnits::Zero(); |
| 550 | for (std::vector<MemberInfo>::const_iterator Member = Members.begin(), |
| 551 | MemberEnd = Members.end(); |
| 552 | Member != MemberEnd; ++Member) { |
| 553 | if (!Member->Data) |
| 554 | continue; |
| 555 | CharUnits Offset = Member->Offset; |
| 556 | assert(Offset >= Size); |
| 557 | // Insert padding if we need to. |
| 558 | if (Offset != Size.RoundUpToAlignment(Packed ? CharUnits::One() : |
| 559 | getAlignment(Member->Data))) |
| 560 | Padding.push_back(std::make_pair(Size, Offset - Size)); |
| 561 | Size = Offset + getSize(Member->Data); |
| 562 | } |
| 563 | if (Padding.empty()) |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 564 | return; |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 565 | // Add the padding to the Members list and sort it. |
| 566 | for (std::vector<std::pair<CharUnits, CharUnits> >::const_iterator |
| 567 | Pad = Padding.begin(), PadEnd = Padding.end(); |
| 568 | Pad != PadEnd; ++Pad) |
| 569 | Members.push_back(StorageInfo(Pad->first, getByteArrayType(Pad->second))); |
| 570 | std::stable_sort(Members.begin(), Members.end()); |
| 571 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 573 | void CGRecordLowering::fillOutputFields() { |
| 574 | for (std::vector<MemberInfo>::const_iterator Member = Members.begin(), |
| 575 | MemberEnd = Members.end(); |
| 576 | Member != MemberEnd; ++Member) { |
| 577 | if (Member->Data) |
| 578 | FieldTypes.push_back(Member->Data); |
| 579 | if (Member->Kind == MemberInfo::Field) { |
| 580 | if (Member->FD) |
| 581 | Fields[Member->FD] = FieldTypes.size() - 1; |
| 582 | // A field without storage must be a bitfield. |
| 583 | if (!Member->Data) |
| 584 | setBitFieldInfo(Member->FD, Member->Offset, FieldTypes.back()); |
| 585 | } else if (Member->Kind == MemberInfo::Base) |
| 586 | NonVirtualBases[Member->RD] = FieldTypes.size() - 1; |
| 587 | else if (Member->Kind == MemberInfo::VBase) |
| 588 | VirtualBases[Member->RD] = FieldTypes.size() - 1; |
| 589 | } |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Daniel Dunbar | c7f9bba | 2010-09-02 23:53:28 +0000 | [diff] [blame] | 592 | CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types, |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 593 | const FieldDecl *FD, |
| 594 | uint64_t Offset, uint64_t Size, |
| 595 | uint64_t StorageSize, |
| 596 | uint64_t StorageAlignment) { |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 597 | // This function is vestigial from CGRecordLayoutBuilder days but is still |
| 598 | // used in GCObjCRuntime.cpp. That usage has a "fixme" attached to it that |
| 599 | // when addressed will allow for the removal of this function. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 600 | llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType()); |
John McCall | 8a3c555 | 2011-02-26 08:41:59 +0000 | [diff] [blame] | 601 | CharUnits TypeSizeInBytes = |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 602 | CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(Ty)); |
John McCall | 8a3c555 | 2011-02-26 08:41:59 +0000 | [diff] [blame] | 603 | uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes); |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 604 | |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 605 | bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType(); |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 606 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 607 | if (Size > TypeSizeInBits) { |
Anders Carlsson | d5f27b0 | 2010-04-17 22:54:57 +0000 | [diff] [blame] | 608 | // We have a wide bit-field. The extra bits are only used for padding, so |
| 609 | // if we have a bitfield of type T, with size N: |
| 610 | // |
| 611 | // T t : N; |
| 612 | // |
| 613 | // We can just assume that it's: |
| 614 | // |
| 615 | // T t : sizeof(T); |
| 616 | // |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 617 | Size = TypeSizeInBits; |
Anders Carlsson | be6f318 | 2010-04-16 16:23:02 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Chandler Carruth | fd8eca2 | 2012-12-09 07:26:04 +0000 | [diff] [blame] | 620 | // Reverse the bit offsets for big endian machines. Because we represent |
| 621 | // a bitfield as a single large integer load, we can imagine the bits |
| 622 | // counting from the most-significant-bit instead of the |
| 623 | // least-significant-bit. |
| 624 | if (Types.getDataLayout().isBigEndian()) { |
| 625 | Offset = StorageSize - (Offset + Size); |
| 626 | } |
Chris Lattner | fb59c7c | 2011-02-17 22:09:58 +0000 | [diff] [blame] | 627 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 628 | return CGBitFieldInfo(Offset, Size, IsSigned, StorageSize, StorageAlignment); |
Daniel Dunbar | f9c24f8 | 2010-04-12 21:01:28 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 631 | CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D, |
| 632 | llvm::StructType *Ty) { |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 633 | CGRecordLowering Builder(*this, D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 635 | Builder.lower(false); |
Anders Carlsson | e1d5ca5 | 2009-07-24 15:20:52 +0000 | [diff] [blame] | 636 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 637 | // If we're in C++, compute the base subobject type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 638 | llvm::StructType *BaseTy = 0; |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 639 | if (isa<CXXRecordDecl>(D) && !D->isUnion() && !D->hasAttr<FinalAttr>()) { |
| 640 | BaseTy = Ty; |
| 641 | if (Builder.Layout.getNonVirtualSize() != Builder.Layout.getSize()) { |
| 642 | CGRecordLowering BaseBuilder(*this, D); |
| 643 | BaseBuilder.lower(true); |
| 644 | BaseTy = llvm::StructType::create( |
| 645 | getLLVMContext(), BaseBuilder.FieldTypes, "", BaseBuilder.Packed); |
| 646 | addRecordTypeName(D, BaseTy, ".base"); |
| 647 | } |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Reid Kleckner | c497f1d | 2014-02-27 00:03:39 +0000 | [diff] [blame] | 650 | // Fill in the struct *after* computing the base type. Filling in the body |
| 651 | // signifies that the type is no longer opaque and record layout is complete, |
| 652 | // but we may need to recursively layout D while laying D out as a base type. |
| 653 | Ty->setBody(Builder.FieldTypes, Builder.Packed); |
| 654 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 655 | CGRecordLayout *RL = |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 656 | new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable, |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 657 | Builder.IsZeroInitializableAsBase); |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 658 | |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 659 | RL->NonVirtualBases.swap(Builder.NonVirtualBases); |
| 660 | RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases); |
Anders Carlsson | 061ca52 | 2010-05-18 05:22:06 +0000 | [diff] [blame] | 661 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 662 | // Add all the field numbers. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 663 | RL->FieldInfo.swap(Builder.Fields); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 664 | |
| 665 | // Add bitfield info. |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 666 | RL->BitFields.swap(Builder.BitFields); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 667 | |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 668 | // Dump the layout, if requested. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 669 | if (getContext().getLangOpts().DumpRecordLayouts) { |
Argyrios Kyrtzidis | 8ade08e | 2013-07-12 22:30:03 +0000 | [diff] [blame] | 670 | llvm::outs() << "\n*** Dumping IRgen Record Layout\n"; |
| 671 | llvm::outs() << "Record: "; |
| 672 | D->dump(llvm::outs()); |
| 673 | llvm::outs() << "\nLayout: "; |
| 674 | RL->print(llvm::outs()); |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 675 | } |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 676 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 677 | #ifndef NDEBUG |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 678 | // Verify that the computed LLVM struct size matches the AST layout size. |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 679 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D); |
| 680 | |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 681 | uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize()); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 682 | assert(TypeSizeInBits == getDataLayout().getTypeAllocSizeInBits(Ty) && |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 683 | "Type size mismatch!"); |
| 684 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 685 | if (BaseTy) { |
Ken Dyck | bec0285 | 2011-02-08 02:02:47 +0000 | [diff] [blame] | 686 | CharUnits NonVirtualSize = Layout.getNonVirtualSize(); |
Ken Dyck | bec0285 | 2011-02-08 02:02:47 +0000 | [diff] [blame] | 687 | |
| 688 | uint64_t AlignedNonVirtualTypeSizeInBits = |
Warren Hunt | fb00c88 | 2014-02-21 23:49:50 +0000 | [diff] [blame] | 689 | getContext().toBits(NonVirtualSize); |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 690 | |
| 691 | assert(AlignedNonVirtualTypeSizeInBits == |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 692 | getDataLayout().getTypeAllocSizeInBits(BaseTy) && |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 693 | "Type size mismatch!"); |
| 694 | } |
| 695 | |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 696 | // Verify that the LLVM and AST field offsets agree. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 697 | llvm::StructType *ST = |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 698 | dyn_cast<llvm::StructType>(RL->getLLVMType()); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 699 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(ST); |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 700 | |
| 701 | const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D); |
| 702 | RecordDecl::field_iterator it = D->field_begin(); |
| 703 | for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 704 | const FieldDecl *FD = *it; |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 705 | |
| 706 | // For non-bit-fields, just check that the LLVM struct offset matches the |
| 707 | // AST offset. |
| 708 | if (!FD->isBitField()) { |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 709 | unsigned FieldNo = RL->getLLVMFieldNo(FD); |
| 710 | assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) && |
| 711 | "Invalid field offset!"); |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 712 | continue; |
| 713 | } |
Fariborz Jahanian | bcb23a1 | 2011-04-26 23:52:16 +0000 | [diff] [blame] | 714 | |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 715 | // Ignore unnamed bit-fields. |
Eli Friedman | 2782dac | 2013-06-26 20:50:34 +0000 | [diff] [blame] | 716 | if (!FD->getDeclName()) |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 717 | continue; |
Daniel Dunbar | 488f55c | 2010-04-22 02:35:46 +0000 | [diff] [blame] | 718 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 719 | // Don't inspect zero-length bitfields. |
| 720 | if (FD->getBitWidthValue(getContext()) == 0) |
| 721 | continue; |
| 722 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 723 | const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD); |
Chandler Carruth | ed72cdc | 2012-12-09 10:33:27 +0000 | [diff] [blame] | 724 | llvm::Type *ElementTy = ST->getTypeAtIndex(RL->getLLVMFieldNo(FD)); |
| 725 | |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 726 | // Unions have overlapping elements dictating their layout, but for |
| 727 | // 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] | 728 | // expected size. |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 729 | if (D->isUnion()) { |
Chandler Carruth | ed72cdc | 2012-12-09 10:33:27 +0000 | [diff] [blame] | 730 | // For unions we verify that the start is zero and the size |
| 731 | // is in-bounds. However, on BE systems, the offset may be non-zero, but |
| 732 | // the size + offset should match the storage size in that case as it |
| 733 | // "starts" at the back. |
| 734 | if (getDataLayout().isBigEndian()) |
David Greene | 464d219 | 2013-01-15 23:13:49 +0000 | [diff] [blame] | 735 | assert(static_cast<unsigned>(Info.Offset + Info.Size) == |
| 736 | Info.StorageSize && |
Chandler Carruth | ed72cdc | 2012-12-09 10:33:27 +0000 | [diff] [blame] | 737 | "Big endian union bitfield does not end at the back"); |
| 738 | else |
| 739 | assert(Info.Offset == 0 && |
| 740 | "Little endian union bitfield with a non-zero offset"); |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 741 | assert(Info.StorageSize <= SL->getSizeInBits() && |
| 742 | "Union not large enough for bitfield storage"); |
| 743 | } else { |
| 744 | assert(Info.StorageSize == |
| 745 | getDataLayout().getTypeAllocSizeInBits(ElementTy) && |
| 746 | "Storage size does not match the element type size"); |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 747 | } |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 748 | assert(Info.Size > 0 && "Empty bitfield!"); |
Eli Bendersky | 76bd3d8 | 2012-12-18 18:53:14 +0000 | [diff] [blame] | 749 | assert(static_cast<unsigned>(Info.Offset) + Info.Size <= Info.StorageSize && |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 750 | "Bitfield outside of its allocated storage"); |
Daniel Dunbar | 2ba6744 | 2010-04-21 19:10:49 +0000 | [diff] [blame] | 751 | } |
| 752 | #endif |
Daniel Dunbar | 2ea5183 | 2010-04-19 20:44:47 +0000 | [diff] [blame] | 753 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 754 | return RL; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 755 | } |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 756 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 757 | void CGRecordLayout::print(raw_ostream &OS) const { |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 758 | OS << "<CGRecordLayout\n"; |
John McCall | 0217dfc2 | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 759 | OS << " LLVMType:" << *CompleteObjectType << "\n"; |
| 760 | if (BaseSubobjectType) |
| 761 | OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n"; |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 762 | OS << " IsZeroInitializable:" << IsZeroInitializable << "\n"; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 763 | OS << " BitFields:[\n"; |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 764 | |
| 765 | // Print bit-field infos in declaration order. |
| 766 | std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 767 | for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator |
| 768 | it = BitFields.begin(), ie = BitFields.end(); |
| 769 | it != ie; ++it) { |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 770 | const RecordDecl *RD = it->first->getParent(); |
| 771 | unsigned Index = 0; |
| 772 | for (RecordDecl::field_iterator |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 773 | it2 = RD->field_begin(); *it2 != it->first; ++it2) |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 774 | ++Index; |
| 775 | BFIs.push_back(std::make_pair(Index, &it->second)); |
| 776 | } |
| 777 | llvm::array_pod_sort(BFIs.begin(), BFIs.end()); |
| 778 | for (unsigned i = 0, e = BFIs.size(); i != e; ++i) { |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 779 | OS.indent(4); |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 780 | BFIs[i].second->print(OS); |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 781 | OS << "\n"; |
| 782 | } |
Daniel Dunbar | b6f4b05 | 2010-04-22 02:35:36 +0000 | [diff] [blame] | 783 | |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 784 | OS << "]>\n"; |
| 785 | } |
| 786 | |
| 787 | void CGRecordLayout::dump() const { |
| 788 | print(llvm::errs()); |
| 789 | } |
| 790 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 791 | void CGBitFieldInfo::print(raw_ostream &OS) const { |
Chandler Carruth | ff0e3a1 | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 792 | OS << "<CGBitFieldInfo" |
| 793 | << " Offset:" << Offset |
| 794 | << " Size:" << Size |
| 795 | << " IsSigned:" << IsSigned |
| 796 | << " StorageSize:" << StorageSize |
| 797 | << " StorageAlignment:" << StorageAlignment << ">"; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | void CGBitFieldInfo::dump() const { |
| 801 | print(llvm::errs()); |
| 802 | } |