| 1 | //===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Builder implementation for CGRecordLayout objects. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "ABIInfoImpl.h" |
| 14 | #include "CGCXXABI.h" |
| 15 | #include "CGRecordLayout.h" |
| 16 | #include "CodeGenTypes.h" |
| 17 | #include "clang/AST/ASTContext.h" |
| 18 | #include "clang/AST/Attr.h" |
| 19 | #include "clang/AST/CXXInheritance.h" |
| 20 | #include "clang/AST/DeclCXX.h" |
| 21 | #include "clang/AST/Expr.h" |
| 22 | #include "clang/AST/RecordLayout.h" |
| 23 | #include "clang/Basic/CodeGenOptions.h" |
| 24 | #include "llvm/IR/DataLayout.h" |
| 25 | #include "llvm/IR/DerivedTypes.h" |
| 26 | #include "llvm/IR/Type.h" |
| 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/MathExtras.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | using namespace clang; |
| 31 | using namespace CodeGen; |
| 32 | |
| 33 | namespace { |
| 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 [i8 x |
| 48 | /// 3] instead of i24. This is computed when accumulating bitfields in |
| 49 | /// accumulateBitfields. |
| 50 | /// C++ examples that require clipping: |
| 51 | /// struct { int a : 24; char b; }; // a must be clipped, b goes at offset 3 |
| 52 | /// struct A { int a : 24; ~A(); }; // a must be clipped because: |
| 53 | /// struct B : A { char b; }; // b goes at offset 3 |
| 54 | /// * The allocation of bitfield access units is described in more detail in |
| 55 | /// CGRecordLowering::accumulateBitFields. |
| 56 | /// * Clang ignores 0 sized bitfields and 0 sized bases but *not* zero sized |
| 57 | /// fields. The existing asserts suggest that LLVM assumes that *every* field |
| 58 | /// has an underlying storage type. Therefore empty structures containing |
| 59 | /// zero sized subobjects such as empty records or zero sized arrays still get |
| 60 | /// a zero sized (empty struct) storage type. |
| 61 | /// * Clang reads the complete type rather than the base type when generating |
| 62 | /// code to access fields. Bitfields in tail position with tail padding may |
| 63 | /// be clipped in the base class but not the complete class (we may discover |
| 64 | /// that the tail padding is not used in the complete class.) However, |
| 65 | /// because LLVM reads from the complete type it can generate incorrect code |
| 66 | /// if we do not clip the tail padding off of the bitfield in the complete |
| 67 | /// layout. |
| 68 | /// * Itanium allows nearly empty primary virtual bases. These bases don't get |
| 69 | /// get their own storage because they're laid out as part of another base |
| 70 | /// or at the beginning of the structure. Determining if a VBase actually |
| 71 | /// gets storage awkwardly involves a walk of all bases. |
| 72 | /// * VFPtrs and VBPtrs do *not* make a record NotZeroInitializable. |
| 73 | struct CGRecordLowering { |
| 74 | // MemberInfo is a helper structure that contains information about a record |
| 75 | // member. In additional to the standard member types, there exists a |
| 76 | // sentinel member type that ensures correct rounding. |
| 77 | struct MemberInfo { |
| 78 | CharUnits Offset; |
| 79 | enum InfoKind { VFPtr, VBPtr, Field, Base, VBase } Kind; |
| 80 | llvm::Type *Data; |
| 81 | union { |
| 82 | const FieldDecl *FD; |
| 83 | const CXXRecordDecl *RD; |
| 84 | }; |
| 85 | MemberInfo(CharUnits Offset, InfoKind Kind, llvm::Type *Data, |
| 86 | const FieldDecl *FD = nullptr) |
| 87 | : Offset(Offset), Kind(Kind), Data(Data), FD(FD) {} |
| 88 | MemberInfo(CharUnits Offset, InfoKind Kind, llvm::Type *Data, |
| 89 | const CXXRecordDecl *RD) |
| 90 | : Offset(Offset), Kind(Kind), Data(Data), RD(RD) {} |
| 91 | // MemberInfos are sorted so we define a < operator. |
| 92 | bool operator <(const MemberInfo& a) const { return Offset < a.Offset; } |
| 93 | }; |
| 94 | // The constructor. |
| 95 | CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D, bool Packed); |
| 96 | // Short helper routines. |
| 97 | /// Constructs a MemberInfo instance from an offset and llvm::Type *. |
| 98 | static MemberInfo StorageInfo(CharUnits Offset, llvm::Type *Data) { |
| 99 | return MemberInfo(Offset, MemberInfo::Field, Data); |
| 100 | } |
| 101 | |
| 102 | /// The Microsoft bitfield layout rule allocates discrete storage |
| 103 | /// units of the field's formal type and only combines adjacent |
| 104 | /// fields of the same formal type. We want to emit a layout with |
| 105 | /// these discrete storage units instead of combining them into a |
| 106 | /// continuous run. |
| 107 | bool isDiscreteBitFieldABI() const { |
| 108 | return Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 109 | D->isMsStruct(C: Context); |
| 110 | } |
| 111 | |
| 112 | /// Helper function to check if we are targeting AAPCS. |
| 113 | bool isAAPCS() const { |
| 114 | return Context.getTargetInfo().getABI().starts_with(Prefix: "aapcs" ); |
| 115 | } |
| 116 | |
| 117 | /// Helper function to check if the target machine is BigEndian. |
| 118 | bool isBE() const { return Context.getTargetInfo().isBigEndian(); } |
| 119 | |
| 120 | /// The Itanium base layout rule allows virtual bases to overlap |
| 121 | /// other bases, which complicates layout in specific ways. |
| 122 | /// |
| 123 | /// Note specifically that the ms_struct attribute doesn't change this. |
| 124 | bool isOverlappingVBaseABI() const { |
| 125 | return !Context.getTargetInfo().getCXXABI().isMicrosoft(); |
| 126 | } |
| 127 | |
| 128 | /// Wraps llvm::Type::getIntNTy with some implicit arguments. |
| 129 | llvm::Type *getIntNType(uint64_t NumBits) const { |
| 130 | unsigned AlignedBits = llvm::alignTo(Value: NumBits, Align: Context.getCharWidth()); |
| 131 | return llvm::Type::getIntNTy(C&: Types.getLLVMContext(), N: AlignedBits); |
| 132 | } |
| 133 | /// Get the LLVM type sized as one character unit. |
| 134 | llvm::Type *getCharType() const { |
| 135 | return llvm::Type::getIntNTy(C&: Types.getLLVMContext(), |
| 136 | N: Context.getCharWidth()); |
| 137 | } |
| 138 | /// Gets an llvm type of size NumChars and alignment 1. |
| 139 | llvm::Type *getByteArrayType(CharUnits NumChars) const { |
| 140 | assert(!NumChars.isZero() && "Empty byte arrays aren't allowed." ); |
| 141 | llvm::Type *Type = getCharType(); |
| 142 | return NumChars == CharUnits::One() ? Type : |
| 143 | (llvm::Type *)llvm::ArrayType::get(ElementType: Type, NumElements: NumChars.getQuantity()); |
| 144 | } |
| 145 | /// Gets the storage type for a field decl and handles storage |
| 146 | /// for itanium bitfields that are smaller than their declared type. |
| 147 | llvm::Type *getStorageType(const FieldDecl *FD) const { |
| 148 | llvm::Type *Type = Types.ConvertTypeForMem(T: FD->getType()); |
| 149 | if (!FD->isBitField()) return Type; |
| 150 | if (isDiscreteBitFieldABI()) return Type; |
| 151 | return getIntNType(NumBits: std::min(a: FD->getBitWidthValue(), |
| 152 | b: (unsigned)Context.toBits(CharSize: getSize(Type)))); |
| 153 | } |
| 154 | /// Gets the llvm Basesubobject type from a CXXRecordDecl. |
| 155 | llvm::Type *getStorageType(const CXXRecordDecl *RD) const { |
| 156 | return Types.getCGRecordLayout(RD).getBaseSubobjectLLVMType(); |
| 157 | } |
| 158 | CharUnits bitsToCharUnits(uint64_t BitOffset) const { |
| 159 | return Context.toCharUnitsFromBits(BitSize: BitOffset); |
| 160 | } |
| 161 | CharUnits getSize(llvm::Type *Type) const { |
| 162 | return CharUnits::fromQuantity(Quantity: DataLayout.getTypeAllocSize(Ty: Type)); |
| 163 | } |
| 164 | CharUnits getAlignment(llvm::Type *Type) const { |
| 165 | return CharUnits::fromQuantity(Quantity: DataLayout.getABITypeAlign(Ty: Type)); |
| 166 | } |
| 167 | bool isZeroInitializable(const FieldDecl *FD) const { |
| 168 | return Types.isZeroInitializable(T: FD->getType()); |
| 169 | } |
| 170 | bool isZeroInitializable(const RecordDecl *RD) const { |
| 171 | return Types.isZeroInitializable(RD); |
| 172 | } |
| 173 | void appendPaddingBytes(CharUnits Size) { |
| 174 | if (!Size.isZero()) |
| 175 | FieldTypes.push_back(Elt: getByteArrayType(NumChars: Size)); |
| 176 | } |
| 177 | uint64_t getFieldBitOffset(const FieldDecl *FD) const { |
| 178 | return Layout.getFieldOffset(FieldNo: FD->getFieldIndex()); |
| 179 | } |
| 180 | // Layout routines. |
| 181 | void setBitFieldInfo(const FieldDecl *FD, CharUnits StartOffset, |
| 182 | llvm::Type *StorageType); |
| 183 | /// Lowers an ASTRecordLayout to a llvm type. |
| 184 | void lower(bool NonVirtualBaseType); |
| 185 | void lowerUnion(bool isNonVirtualBaseType); |
| 186 | void accumulateFields(bool isNonVirtualBaseType); |
| 187 | RecordDecl::field_iterator |
| 188 | accumulateBitFields(bool isNonVirtualBaseType, |
| 189 | RecordDecl::field_iterator Field, |
| 190 | RecordDecl::field_iterator FieldEnd); |
| 191 | void computeVolatileBitfields(); |
| 192 | void accumulateBases(); |
| 193 | void accumulateVPtrs(); |
| 194 | void accumulateVBases(); |
| 195 | /// Recursively searches all of the bases to find out if a vbase is |
| 196 | /// not the primary vbase of some base class. |
| 197 | bool hasOwnStorage(const CXXRecordDecl *Decl, |
| 198 | const CXXRecordDecl *Query) const; |
| 199 | void calculateZeroInit(); |
| 200 | CharUnits calculateTailClippingOffset(bool isNonVirtualBaseType) const; |
| 201 | void checkBitfieldClipping(bool isNonVirtualBaseType) const; |
| 202 | /// Determines if we need a packed llvm struct. |
| 203 | void determinePacked(bool NVBaseType); |
| 204 | /// Inserts padding everywhere it's needed. |
| 205 | void insertPadding(); |
| 206 | /// Fills out the structures that are ultimately consumed. |
| 207 | void fillOutputFields(); |
| 208 | // Input memoization fields. |
| 209 | CodeGenTypes &Types; |
| 210 | const ASTContext &Context; |
| 211 | const RecordDecl *D; |
| 212 | const CXXRecordDecl *RD; |
| 213 | const ASTRecordLayout &Layout; |
| 214 | const llvm::DataLayout &DataLayout; |
| 215 | // Helpful intermediate data-structures. |
| 216 | std::vector<MemberInfo> Members; |
| 217 | // Output fields, consumed by CodeGenTypes::ComputeRecordLayout. |
| 218 | SmallVector<llvm::Type *, 16> FieldTypes; |
| 219 | llvm::DenseMap<const FieldDecl *, unsigned> Fields; |
| 220 | llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields; |
| 221 | llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases; |
| 222 | llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases; |
| 223 | bool IsZeroInitializable : 1; |
| 224 | bool IsZeroInitializableAsBase : 1; |
| 225 | bool Packed : 1; |
| 226 | private: |
| 227 | CGRecordLowering(const CGRecordLowering &) = delete; |
| 228 | void operator =(const CGRecordLowering &) = delete; |
| 229 | }; |
| 230 | } // namespace { |
| 231 | |
| 232 | CGRecordLowering::CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D, |
| 233 | bool Packed) |
| 234 | : Types(Types), Context(Types.getContext()), D(D), |
| 235 | RD(dyn_cast<CXXRecordDecl>(Val: D)), |
| 236 | Layout(Types.getContext().getASTRecordLayout(D)), |
| 237 | DataLayout(Types.getDataLayout()), IsZeroInitializable(true), |
| 238 | IsZeroInitializableAsBase(true), Packed(Packed) {} |
| 239 | |
| 240 | void CGRecordLowering::setBitFieldInfo( |
| 241 | const FieldDecl *FD, CharUnits StartOffset, llvm::Type *StorageType) { |
| 242 | CGBitFieldInfo &Info = BitFields[FD->getCanonicalDecl()]; |
| 243 | Info.IsSigned = FD->getType()->isSignedIntegerOrEnumerationType(); |
| 244 | Info.Offset = (unsigned)(getFieldBitOffset(FD) - Context.toBits(CharSize: StartOffset)); |
| 245 | Info.Size = FD->getBitWidthValue(); |
| 246 | Info.StorageSize = (unsigned)DataLayout.getTypeAllocSizeInBits(Ty: StorageType); |
| 247 | Info.StorageOffset = StartOffset; |
| 248 | if (Info.Size > Info.StorageSize) |
| 249 | Info.Size = Info.StorageSize; |
| 250 | // Reverse the bit offsets for big endian machines. Because we represent |
| 251 | // a bitfield as a single large integer load, we can imagine the bits |
| 252 | // counting from the most-significant-bit instead of the |
| 253 | // least-significant-bit. |
| 254 | if (DataLayout.isBigEndian()) |
| 255 | Info.Offset = Info.StorageSize - (Info.Offset + Info.Size); |
| 256 | |
| 257 | Info.VolatileStorageSize = 0; |
| 258 | Info.VolatileOffset = 0; |
| 259 | Info.VolatileStorageOffset = CharUnits::Zero(); |
| 260 | } |
| 261 | |
| 262 | void CGRecordLowering::lower(bool NVBaseType) { |
| 263 | // The lowering process implemented in this function takes a variety of |
| 264 | // carefully ordered phases. |
| 265 | // 1) Store all members (fields and bases) in a list and sort them by offset. |
| 266 | // 2) Add a 1-byte capstone member at the Size of the structure. |
| 267 | // 3) Clip bitfield storages members if their tail padding is or might be |
| 268 | // used by another field or base. The clipping process uses the capstone |
| 269 | // by treating it as another object that occurs after the record. |
| 270 | // 4) Determine if the llvm-struct requires packing. It's important that this |
| 271 | // phase occur after clipping, because clipping changes the llvm type. |
| 272 | // This phase reads the offset of the capstone when determining packedness |
| 273 | // and updates the alignment of the capstone to be equal of the alignment |
| 274 | // of the record after doing so. |
| 275 | // 5) Insert padding everywhere it is needed. This phase requires 'Packed' to |
| 276 | // have been computed and needs to know the alignment of the record in |
| 277 | // order to understand if explicit tail padding is needed. |
| 278 | // 6) Remove the capstone, we don't need it anymore. |
| 279 | // 7) Determine if this record can be zero-initialized. This phase could have |
| 280 | // been placed anywhere after phase 1. |
| 281 | // 8) Format the complete list of members in a way that can be consumed by |
| 282 | // CodeGenTypes::ComputeRecordLayout. |
| 283 | CharUnits Size = NVBaseType ? Layout.getNonVirtualSize() : Layout.getSize(); |
| 284 | if (D->isUnion()) { |
| 285 | lowerUnion(isNonVirtualBaseType: NVBaseType); |
| 286 | computeVolatileBitfields(); |
| 287 | return; |
| 288 | } |
| 289 | accumulateFields(isNonVirtualBaseType: NVBaseType); |
| 290 | // RD implies C++. |
| 291 | if (RD) { |
| 292 | accumulateVPtrs(); |
| 293 | accumulateBases(); |
| 294 | if (Members.empty()) { |
| 295 | appendPaddingBytes(Size); |
| 296 | computeVolatileBitfields(); |
| 297 | return; |
| 298 | } |
| 299 | if (!NVBaseType) |
| 300 | accumulateVBases(); |
| 301 | } |
| 302 | llvm::stable_sort(Range&: Members); |
| 303 | checkBitfieldClipping(isNonVirtualBaseType: NVBaseType); |
| 304 | Members.push_back(x: StorageInfo(Offset: Size, Data: getIntNType(NumBits: 8))); |
| 305 | determinePacked(NVBaseType); |
| 306 | insertPadding(); |
| 307 | Members.pop_back(); |
| 308 | calculateZeroInit(); |
| 309 | fillOutputFields(); |
| 310 | computeVolatileBitfields(); |
| 311 | } |
| 312 | |
| 313 | void CGRecordLowering::lowerUnion(bool isNonVirtualBaseType) { |
| 314 | CharUnits LayoutSize = |
| 315 | isNonVirtualBaseType ? Layout.getDataSize() : Layout.getSize(); |
| 316 | llvm::Type *StorageType = nullptr; |
| 317 | bool SeenNamedMember = false; |
| 318 | // Iterate through the fields setting bitFieldInfo and the Fields array. Also |
| 319 | // locate the "most appropriate" storage type. The heuristic for finding the |
| 320 | // storage type isn't necessary, the first (non-0-length-bitfield) field's |
| 321 | // type would work fine and be simpler but would be different than what we've |
| 322 | // been doing and cause lit tests to change. |
| 323 | for (const auto *Field : D->fields()) { |
| 324 | if (Field->isBitField()) { |
| 325 | if (Field->isZeroLengthBitField()) |
| 326 | continue; |
| 327 | llvm::Type *FieldType = getStorageType(FD: Field); |
| 328 | if (LayoutSize < getSize(Type: FieldType)) |
| 329 | FieldType = getByteArrayType(NumChars: LayoutSize); |
| 330 | setBitFieldInfo(FD: Field, StartOffset: CharUnits::Zero(), StorageType: FieldType); |
| 331 | } |
| 332 | Fields[Field->getCanonicalDecl()] = 0; |
| 333 | llvm::Type *FieldType = getStorageType(FD: Field); |
| 334 | // Compute zero-initializable status. |
| 335 | // This union might not be zero initialized: it may contain a pointer to |
| 336 | // data member which might have some exotic initialization sequence. |
| 337 | // If this is the case, then we aught not to try and come up with a "better" |
| 338 | // type, it might not be very easy to come up with a Constant which |
| 339 | // correctly initializes it. |
| 340 | if (!SeenNamedMember) { |
| 341 | SeenNamedMember = Field->getIdentifier(); |
| 342 | if (!SeenNamedMember) |
| 343 | if (const auto *FieldRD = Field->getType()->getAsRecordDecl()) |
| 344 | SeenNamedMember = FieldRD->findFirstNamedDataMember(); |
| 345 | if (SeenNamedMember && !isZeroInitializable(FD: Field)) { |
| 346 | IsZeroInitializable = IsZeroInitializableAsBase = false; |
| 347 | StorageType = FieldType; |
| 348 | } |
| 349 | } |
| 350 | // Because our union isn't zero initializable, we won't be getting a better |
| 351 | // storage type. |
| 352 | if (!IsZeroInitializable) |
| 353 | continue; |
| 354 | // Conditionally update our storage type if we've got a new "better" one. |
| 355 | if (!StorageType || |
| 356 | getAlignment(Type: FieldType) > getAlignment(Type: StorageType) || |
| 357 | (getAlignment(Type: FieldType) == getAlignment(Type: StorageType) && |
| 358 | getSize(Type: FieldType) > getSize(Type: StorageType))) |
| 359 | StorageType = FieldType; |
| 360 | } |
| 361 | // If we have no storage type just pad to the appropriate size and return. |
| 362 | if (!StorageType) |
| 363 | return appendPaddingBytes(Size: LayoutSize); |
| 364 | // If our storage size was bigger than our required size (can happen in the |
| 365 | // case of packed bitfields on Itanium) then just use an I8 array. |
| 366 | if (LayoutSize < getSize(Type: StorageType)) |
| 367 | StorageType = getByteArrayType(NumChars: LayoutSize); |
| 368 | FieldTypes.push_back(Elt: StorageType); |
| 369 | appendPaddingBytes(Size: LayoutSize - getSize(Type: StorageType)); |
| 370 | // Set packed if we need it. |
| 371 | const auto StorageAlignment = getAlignment(Type: StorageType); |
| 372 | assert((Layout.getSize() % StorageAlignment == 0 || |
| 373 | Layout.getDataSize() % StorageAlignment) && |
| 374 | "Union's standard layout and no_unique_address layout must agree on " |
| 375 | "packedness" ); |
| 376 | if (Layout.getDataSize() % StorageAlignment) |
| 377 | Packed = true; |
| 378 | } |
| 379 | |
| 380 | void CGRecordLowering::accumulateFields(bool isNonVirtualBaseType) { |
| 381 | for (RecordDecl::field_iterator Field = D->field_begin(), |
| 382 | FieldEnd = D->field_end(); |
| 383 | Field != FieldEnd;) { |
| 384 | if (Field->isBitField()) { |
| 385 | Field = accumulateBitFields(isNonVirtualBaseType, Field, FieldEnd); |
| 386 | assert((Field == FieldEnd || !Field->isBitField()) && |
| 387 | "Failed to accumulate all the bitfields" ); |
| 388 | } else if (isEmptyFieldForLayout(Context, FD: *Field)) { |
| 389 | // Empty fields have no storage. |
| 390 | ++Field; |
| 391 | } else { |
| 392 | // Use base subobject layout for the potentially-overlapping field, |
| 393 | // as it is done in RecordLayoutBuilder |
| 394 | Members.push_back(x: MemberInfo( |
| 395 | bitsToCharUnits(BitOffset: getFieldBitOffset(FD: *Field)), MemberInfo::Field, |
| 396 | Field->isPotentiallyOverlapping() |
| 397 | ? getStorageType(RD: Field->getType()->getAsCXXRecordDecl()) |
| 398 | : getStorageType(FD: *Field), |
| 399 | *Field)); |
| 400 | ++Field; |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | // Create members for bitfields. Field is a bitfield, and FieldEnd is the end |
| 406 | // iterator of the record. Return the first non-bitfield encountered. We need |
| 407 | // to know whether this is the base or complete layout, as virtual bases could |
| 408 | // affect the upper bound of bitfield access unit allocation. |
| 409 | RecordDecl::field_iterator |
| 410 | CGRecordLowering::accumulateBitFields(bool isNonVirtualBaseType, |
| 411 | RecordDecl::field_iterator Field, |
| 412 | RecordDecl::field_iterator FieldEnd) { |
| 413 | if (isDiscreteBitFieldABI()) { |
| 414 | // Run stores the first element of the current run of bitfields. FieldEnd is |
| 415 | // used as a special value to note that we don't have a current run. A |
| 416 | // bitfield run is a contiguous collection of bitfields that can be stored |
| 417 | // in the same storage block. Zero-sized bitfields and bitfields that would |
| 418 | // cross an alignment boundary break a run and start a new one. |
| 419 | RecordDecl::field_iterator Run = FieldEnd; |
| 420 | // Tail is the offset of the first bit off the end of the current run. It's |
| 421 | // used to determine if the ASTRecordLayout is treating these two bitfields |
| 422 | // as contiguous. StartBitOffset is offset of the beginning of the Run. |
| 423 | uint64_t StartBitOffset, Tail = 0; |
| 424 | for (; Field != FieldEnd && Field->isBitField(); ++Field) { |
| 425 | // Zero-width bitfields end runs. |
| 426 | if (Field->isZeroLengthBitField()) { |
| 427 | Run = FieldEnd; |
| 428 | continue; |
| 429 | } |
| 430 | uint64_t BitOffset = getFieldBitOffset(FD: *Field); |
| 431 | llvm::Type *Type = Types.ConvertTypeForMem(T: Field->getType()); |
| 432 | // If we don't have a run yet, or don't live within the previous run's |
| 433 | // allocated storage then we allocate some storage and start a new run. |
| 434 | if (Run == FieldEnd || BitOffset >= Tail) { |
| 435 | Run = Field; |
| 436 | StartBitOffset = BitOffset; |
| 437 | Tail = StartBitOffset + DataLayout.getTypeAllocSizeInBits(Ty: Type); |
| 438 | // Add the storage member to the record. This must be added to the |
| 439 | // record before the bitfield members so that it gets laid out before |
| 440 | // the bitfields it contains get laid out. |
| 441 | Members.push_back(x: StorageInfo(Offset: bitsToCharUnits(BitOffset: StartBitOffset), Data: Type)); |
| 442 | } |
| 443 | // Bitfields get the offset of their storage but come afterward and remain |
| 444 | // there after a stable sort. |
| 445 | Members.push_back(x: MemberInfo(bitsToCharUnits(BitOffset: StartBitOffset), |
| 446 | MemberInfo::Field, nullptr, *Field)); |
| 447 | } |
| 448 | return Field; |
| 449 | } |
| 450 | |
| 451 | // The SysV ABI can overlap bitfield storage units with both other bitfield |
| 452 | // storage units /and/ other non-bitfield data members. Accessing a sequence |
| 453 | // of bitfields mustn't interfere with adjacent non-bitfields -- they're |
| 454 | // permitted to be accessed in separate threads for instance. |
| 455 | |
| 456 | // We split runs of bit-fields into a sequence of "access units". When we emit |
| 457 | // a load or store of a bit-field, we'll load/store the entire containing |
| 458 | // access unit. As mentioned, the standard requires that these loads and |
| 459 | // stores must not interfere with accesses to other memory locations, and it |
| 460 | // defines the bit-field's memory location as the current run of |
| 461 | // non-zero-width bit-fields. So an access unit must never overlap with |
| 462 | // non-bit-field storage or cross a zero-width bit-field. Otherwise, we're |
| 463 | // free to draw the lines as we see fit. |
| 464 | |
| 465 | // Drawing these lines well can be complicated. LLVM generally can't modify a |
| 466 | // program to access memory that it didn't before, so using very narrow access |
| 467 | // units can prevent the compiler from using optimal access patterns. For |
| 468 | // example, suppose a run of bit-fields occupies four bytes in a struct. If we |
| 469 | // split that into four 1-byte access units, then a sequence of assignments |
| 470 | // that doesn't touch all four bytes may have to be emitted with multiple |
| 471 | // 8-bit stores instead of a single 32-bit store. On the other hand, if we use |
| 472 | // very wide access units, we may find ourselves emitting accesses to |
| 473 | // bit-fields we didn't really need to touch, just because LLVM was unable to |
| 474 | // clean up after us. |
| 475 | |
| 476 | // It is desirable to have access units be aligned powers of 2 no larger than |
| 477 | // a register. (On non-strict alignment ISAs, the alignment requirement can be |
| 478 | // dropped.) A three byte access unit will be accessed using 2-byte and 1-byte |
| 479 | // accesses and bit manipulation. If no bitfield straddles across the two |
| 480 | // separate accesses, it is better to have separate 2-byte and 1-byte access |
| 481 | // units, as then LLVM will not generate unnecessary memory accesses, or bit |
| 482 | // manipulation. Similarly, on a strict-alignment architecture, it is better |
| 483 | // to keep access-units naturally aligned, to avoid similar bit |
| 484 | // manipulation synthesizing larger unaligned accesses. |
| 485 | |
| 486 | // Bitfields that share parts of a single byte are, of necessity, placed in |
| 487 | // the same access unit. That unit will encompass a consecutive run where |
| 488 | // adjacent bitfields share parts of a byte. (The first bitfield of such an |
| 489 | // access unit will start at the beginning of a byte.) |
| 490 | |
| 491 | // We then try and accumulate adjacent access units when the combined unit is |
| 492 | // naturally sized, no larger than a register, and (on a strict alignment |
| 493 | // ISA), naturally aligned. Note that this requires lookahead to one or more |
| 494 | // subsequent access units. For instance, consider a 2-byte access-unit |
| 495 | // followed by 2 1-byte units. We can merge that into a 4-byte access-unit, |
| 496 | // but we would not want to merge a 2-byte followed by a single 1-byte (and no |
| 497 | // available tail padding). We keep track of the best access unit seen so far, |
| 498 | // and use that when we determine we cannot accumulate any more. Then we start |
| 499 | // again at the bitfield following that best one. |
| 500 | |
| 501 | // The accumulation is also prevented when: |
| 502 | // *) it would cross a character-aigned zero-width bitfield, or |
| 503 | // *) fine-grained bitfield access option is in effect. |
| 504 | |
| 505 | CharUnits RegSize = |
| 506 | bitsToCharUnits(BitOffset: Context.getTargetInfo().getRegisterWidth()); |
| 507 | unsigned CharBits = Context.getCharWidth(); |
| 508 | |
| 509 | // Limit of useable tail padding at end of the record. Computed lazily and |
| 510 | // cached here. |
| 511 | CharUnits ScissorOffset = CharUnits::Zero(); |
| 512 | |
| 513 | // Data about the start of the span we're accumulating to create an access |
| 514 | // unit from. Begin is the first bitfield of the span. If Begin is FieldEnd, |
| 515 | // we've not got a current span. The span starts at the BeginOffset character |
| 516 | // boundary. BitSizeSinceBegin is the size (in bits) of the span -- this might |
| 517 | // include padding when we've advanced to a subsequent bitfield run. |
| 518 | RecordDecl::field_iterator Begin = FieldEnd; |
| 519 | CharUnits BeginOffset; |
| 520 | uint64_t BitSizeSinceBegin; |
| 521 | |
| 522 | // The (non-inclusive) end of the largest acceptable access unit we've found |
| 523 | // since Begin. If this is Begin, we're gathering the initial set of bitfields |
| 524 | // of a new span. BestEndOffset is the end of that acceptable access unit -- |
| 525 | // it might extend beyond the last character of the bitfield run, using |
| 526 | // available padding characters. |
| 527 | RecordDecl::field_iterator BestEnd = Begin; |
| 528 | CharUnits BestEndOffset; |
| 529 | bool BestClipped; // Whether the representation must be in a byte array. |
| 530 | |
| 531 | for (;;) { |
| 532 | // AtAlignedBoundary is true iff Field is the (potential) start of a new |
| 533 | // span (or the end of the bitfields). When true, LimitOffset is the |
| 534 | // character offset of that span and Barrier indicates whether the new |
| 535 | // span cannot be merged into the current one. |
| 536 | bool AtAlignedBoundary = false; |
| 537 | bool Barrier = false; |
| 538 | |
| 539 | if (Field != FieldEnd && Field->isBitField()) { |
| 540 | uint64_t BitOffset = getFieldBitOffset(FD: *Field); |
| 541 | if (Begin == FieldEnd) { |
| 542 | // Beginning a new span. |
| 543 | Begin = Field; |
| 544 | BestEnd = Begin; |
| 545 | |
| 546 | assert((BitOffset % CharBits) == 0 && "Not at start of char" ); |
| 547 | BeginOffset = bitsToCharUnits(BitOffset); |
| 548 | BitSizeSinceBegin = 0; |
| 549 | } else if ((BitOffset % CharBits) != 0) { |
| 550 | // Bitfield occupies the same character as previous bitfield, it must be |
| 551 | // part of the same span. This can include zero-length bitfields, should |
| 552 | // the target not align them to character boundaries. Such non-alignment |
| 553 | // is at variance with the standards, which require zero-length |
| 554 | // bitfields be a barrier between access units. But of course we can't |
| 555 | // achieve that in the middle of a character. |
| 556 | assert(BitOffset == Context.toBits(BeginOffset) + BitSizeSinceBegin && |
| 557 | "Concatenating non-contiguous bitfields" ); |
| 558 | } else { |
| 559 | // Bitfield potentially begins a new span. This includes zero-length |
| 560 | // bitfields on non-aligning targets that lie at character boundaries |
| 561 | // (those are barriers to merging). |
| 562 | if (Field->isZeroLengthBitField()) |
| 563 | Barrier = true; |
| 564 | AtAlignedBoundary = true; |
| 565 | } |
| 566 | } else { |
| 567 | // We've reached the end of the bitfield run. Either we're done, or this |
| 568 | // is a barrier for the current span. |
| 569 | if (Begin == FieldEnd) |
| 570 | break; |
| 571 | |
| 572 | Barrier = true; |
| 573 | AtAlignedBoundary = true; |
| 574 | } |
| 575 | |
| 576 | // InstallBest indicates whether we should create an access unit for the |
| 577 | // current best span: fields [Begin, BestEnd) occupying characters |
| 578 | // [BeginOffset, BestEndOffset). |
| 579 | bool InstallBest = false; |
| 580 | if (AtAlignedBoundary) { |
| 581 | // Field is the start of a new span or the end of the bitfields. The |
| 582 | // just-seen span now extends to BitSizeSinceBegin. |
| 583 | |
| 584 | // Determine if we can accumulate that just-seen span into the current |
| 585 | // accumulation. |
| 586 | CharUnits AccessSize = bitsToCharUnits(BitOffset: BitSizeSinceBegin + CharBits - 1); |
| 587 | if (BestEnd == Begin) { |
| 588 | // This is the initial run at the start of a new span. By definition, |
| 589 | // this is the best seen so far. |
| 590 | BestEnd = Field; |
| 591 | BestEndOffset = BeginOffset + AccessSize; |
| 592 | // Assume clipped until proven not below. |
| 593 | BestClipped = true; |
| 594 | if (!BitSizeSinceBegin) |
| 595 | // A zero-sized initial span -- this will install nothing and reset |
| 596 | // for another. |
| 597 | InstallBest = true; |
| 598 | } else if (AccessSize > RegSize) |
| 599 | // Accumulating the just-seen span would create a multi-register access |
| 600 | // unit, which would increase register pressure. |
| 601 | InstallBest = true; |
| 602 | |
| 603 | if (!InstallBest) { |
| 604 | // Determine if accumulating the just-seen span will create an expensive |
| 605 | // access unit or not. |
| 606 | llvm::Type *Type = getIntNType(NumBits: Context.toBits(CharSize: AccessSize)); |
| 607 | if (!Context.getTargetInfo().hasCheapUnalignedBitFieldAccess()) { |
| 608 | // Unaligned accesses are expensive. Only accumulate if the new unit |
| 609 | // is naturally aligned. Otherwise install the best we have, which is |
| 610 | // either the initial access unit (can't do better), or a naturally |
| 611 | // aligned accumulation (since we would have already installed it if |
| 612 | // it wasn't naturally aligned). |
| 613 | CharUnits Align = getAlignment(Type); |
| 614 | if (Align > Layout.getAlignment()) |
| 615 | // The alignment required is greater than the containing structure |
| 616 | // itself. |
| 617 | InstallBest = true; |
| 618 | else if (!BeginOffset.isMultipleOf(N: Align)) |
| 619 | // The access unit is not at a naturally aligned offset within the |
| 620 | // structure. |
| 621 | InstallBest = true; |
| 622 | |
| 623 | if (InstallBest && BestEnd == Field) |
| 624 | // We're installing the first span, whose clipping was presumed |
| 625 | // above. Compute it correctly. |
| 626 | if (getSize(Type) == AccessSize) |
| 627 | BestClipped = false; |
| 628 | } |
| 629 | |
| 630 | if (!InstallBest) { |
| 631 | // Find the next used storage offset to determine what the limit of |
| 632 | // the current span is. That's either the offset of the next field |
| 633 | // with storage (which might be Field itself) or the end of the |
| 634 | // non-reusable tail padding. |
| 635 | CharUnits LimitOffset; |
| 636 | for (auto Probe = Field; Probe != FieldEnd; ++Probe) |
| 637 | if (!isEmptyFieldForLayout(Context, FD: *Probe)) { |
| 638 | // A member with storage sets the limit. |
| 639 | assert((getFieldBitOffset(*Probe) % CharBits) == 0 && |
| 640 | "Next storage is not byte-aligned" ); |
| 641 | LimitOffset = bitsToCharUnits(BitOffset: getFieldBitOffset(FD: *Probe)); |
| 642 | goto FoundLimit; |
| 643 | } |
| 644 | // We reached the end of the fields, determine the bounds of useable |
| 645 | // tail padding. As this can be complex for C++, we cache the result. |
| 646 | if (ScissorOffset.isZero()) { |
| 647 | ScissorOffset = calculateTailClippingOffset(isNonVirtualBaseType); |
| 648 | assert(!ScissorOffset.isZero() && "Tail clipping at zero" ); |
| 649 | } |
| 650 | |
| 651 | LimitOffset = ScissorOffset; |
| 652 | FoundLimit:; |
| 653 | |
| 654 | CharUnits TypeSize = getSize(Type); |
| 655 | if (BeginOffset + TypeSize <= LimitOffset) { |
| 656 | // There is space before LimitOffset to create a naturally-sized |
| 657 | // access unit. |
| 658 | BestEndOffset = BeginOffset + TypeSize; |
| 659 | BestEnd = Field; |
| 660 | BestClipped = false; |
| 661 | } |
| 662 | |
| 663 | if (Barrier) |
| 664 | // The next field is a barrier that we cannot merge across. |
| 665 | InstallBest = true; |
| 666 | else if (Types.getCodeGenOpts().FineGrainedBitfieldAccesses) |
| 667 | // Fine-grained access, so no merging of spans. |
| 668 | InstallBest = true; |
| 669 | else |
| 670 | // Otherwise, we're not installing. Update the bit size |
| 671 | // of the current span to go all the way to LimitOffset, which is |
| 672 | // the (aligned) offset of next bitfield to consider. |
| 673 | BitSizeSinceBegin = Context.toBits(CharSize: LimitOffset - BeginOffset); |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | if (InstallBest) { |
| 679 | assert((Field == FieldEnd || !Field->isBitField() || |
| 680 | (getFieldBitOffset(*Field) % CharBits) == 0) && |
| 681 | "Installing but not at an aligned bitfield or limit" ); |
| 682 | CharUnits AccessSize = BestEndOffset - BeginOffset; |
| 683 | if (!AccessSize.isZero()) { |
| 684 | // Add the storage member for the access unit to the record. The |
| 685 | // bitfields get the offset of their storage but come afterward and |
| 686 | // remain there after a stable sort. |
| 687 | llvm::Type *Type; |
| 688 | if (BestClipped) { |
| 689 | assert(getSize(getIntNType(Context.toBits(AccessSize))) > |
| 690 | AccessSize && |
| 691 | "Clipped access need not be clipped" ); |
| 692 | Type = getByteArrayType(NumChars: AccessSize); |
| 693 | } else { |
| 694 | Type = getIntNType(NumBits: Context.toBits(CharSize: AccessSize)); |
| 695 | assert(getSize(Type) == AccessSize && |
| 696 | "Unclipped access must be clipped" ); |
| 697 | } |
| 698 | Members.push_back(x: StorageInfo(Offset: BeginOffset, Data: Type)); |
| 699 | for (; Begin != BestEnd; ++Begin) |
| 700 | if (!Begin->isZeroLengthBitField()) |
| 701 | Members.push_back( |
| 702 | x: MemberInfo(BeginOffset, MemberInfo::Field, nullptr, *Begin)); |
| 703 | } |
| 704 | // Reset to start a new span. |
| 705 | Field = BestEnd; |
| 706 | Begin = FieldEnd; |
| 707 | } else { |
| 708 | assert(Field != FieldEnd && Field->isBitField() && |
| 709 | "Accumulating past end of bitfields" ); |
| 710 | assert(!Barrier && "Accumulating across barrier" ); |
| 711 | // Accumulate this bitfield into the current (potential) span. |
| 712 | BitSizeSinceBegin += Field->getBitWidthValue(); |
| 713 | ++Field; |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | return Field; |
| 718 | } |
| 719 | |
| 720 | void CGRecordLowering::accumulateBases() { |
| 721 | // If we've got a primary virtual base, we need to add it with the bases. |
| 722 | if (Layout.isPrimaryBaseVirtual()) { |
| 723 | const CXXRecordDecl *BaseDecl = Layout.getPrimaryBase(); |
| 724 | Members.push_back(x: MemberInfo(CharUnits::Zero(), MemberInfo::Base, |
| 725 | getStorageType(RD: BaseDecl), BaseDecl)); |
| 726 | } |
| 727 | // Accumulate the non-virtual bases. |
| 728 | for (const auto &Base : RD->bases()) { |
| 729 | if (Base.isVirtual()) |
| 730 | continue; |
| 731 | |
| 732 | // Bases can be zero-sized even if not technically empty if they |
| 733 | // contain only a trailing array member. |
| 734 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
| 735 | if (!isEmptyRecordForLayout(Context, T: Base.getType()) && |
| 736 | !Context.getASTRecordLayout(D: BaseDecl).getNonVirtualSize().isZero()) |
| 737 | Members.push_back(x: MemberInfo(Layout.getBaseClassOffset(Base: BaseDecl), |
| 738 | MemberInfo::Base, getStorageType(RD: BaseDecl), BaseDecl)); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | /// The AAPCS that defines that, when possible, bit-fields should |
| 743 | /// be accessed using containers of the declared type width: |
| 744 | /// When a volatile bit-field is read, and its container does not overlap with |
| 745 | /// any non-bit-field member or any zero length bit-field member, its container |
| 746 | /// must be read exactly once using the access width appropriate to the type of |
| 747 | /// the container. When a volatile bit-field is written, and its container does |
| 748 | /// not overlap with any non-bit-field member or any zero-length bit-field |
| 749 | /// member, its container must be read exactly once and written exactly once |
| 750 | /// using the access width appropriate to the type of the container. The two |
| 751 | /// accesses are not atomic. |
| 752 | /// |
| 753 | /// Enforcing the width restriction can be disabled using |
| 754 | /// -fno-aapcs-bitfield-width. |
| 755 | void CGRecordLowering::computeVolatileBitfields() { |
| 756 | if (!isAAPCS() || !Types.getCodeGenOpts().AAPCSBitfieldWidth) |
| 757 | return; |
| 758 | |
| 759 | for (auto &I : BitFields) { |
| 760 | const FieldDecl *Field = I.first; |
| 761 | CGBitFieldInfo &Info = I.second; |
| 762 | llvm::Type *ResLTy = Types.ConvertTypeForMem(T: Field->getType()); |
| 763 | // If the record alignment is less than the type width, we can't enforce a |
| 764 | // aligned load, bail out. |
| 765 | if ((uint64_t)(Context.toBits(CharSize: Layout.getAlignment())) < |
| 766 | ResLTy->getPrimitiveSizeInBits()) |
| 767 | continue; |
| 768 | // CGRecordLowering::setBitFieldInfo() pre-adjusts the bit-field offsets |
| 769 | // for big-endian targets, but it assumes a container of width |
| 770 | // Info.StorageSize. Since AAPCS uses a different container size (width |
| 771 | // of the type), we first undo that calculation here and redo it once |
| 772 | // the bit-field offset within the new container is calculated. |
| 773 | const unsigned OldOffset = |
| 774 | isBE() ? Info.StorageSize - (Info.Offset + Info.Size) : Info.Offset; |
| 775 | // Offset to the bit-field from the beginning of the struct. |
| 776 | const unsigned AbsoluteOffset = |
| 777 | Context.toBits(CharSize: Info.StorageOffset) + OldOffset; |
| 778 | |
| 779 | // Container size is the width of the bit-field type. |
| 780 | const unsigned StorageSize = ResLTy->getPrimitiveSizeInBits(); |
| 781 | // Nothing to do if the access uses the desired |
| 782 | // container width and is naturally aligned. |
| 783 | if (Info.StorageSize == StorageSize && (OldOffset % StorageSize == 0)) |
| 784 | continue; |
| 785 | |
| 786 | // Offset within the container. |
| 787 | unsigned Offset = AbsoluteOffset & (StorageSize - 1); |
| 788 | // Bail out if an aligned load of the container cannot cover the entire |
| 789 | // bit-field. This can happen for example, if the bit-field is part of a |
| 790 | // packed struct. AAPCS does not define access rules for such cases, we let |
| 791 | // clang to follow its own rules. |
| 792 | if (Offset + Info.Size > StorageSize) |
| 793 | continue; |
| 794 | |
| 795 | // Re-adjust offsets for big-endian targets. |
| 796 | if (isBE()) |
| 797 | Offset = StorageSize - (Offset + Info.Size); |
| 798 | |
| 799 | const CharUnits StorageOffset = |
| 800 | Context.toCharUnitsFromBits(BitSize: AbsoluteOffset & ~(StorageSize - 1)); |
| 801 | const CharUnits End = StorageOffset + |
| 802 | Context.toCharUnitsFromBits(BitSize: StorageSize) - |
| 803 | CharUnits::One(); |
| 804 | |
| 805 | const ASTRecordLayout &Layout = |
| 806 | Context.getASTRecordLayout(D: Field->getParent()); |
| 807 | // If we access outside memory outside the record, than bail out. |
| 808 | const CharUnits RecordSize = Layout.getSize(); |
| 809 | if (End >= RecordSize) |
| 810 | continue; |
| 811 | |
| 812 | // Bail out if performing this load would access non-bit-fields members. |
| 813 | bool Conflict = false; |
| 814 | for (const auto *F : D->fields()) { |
| 815 | // Allow sized bit-fields overlaps. |
| 816 | if (F->isBitField() && !F->isZeroLengthBitField()) |
| 817 | continue; |
| 818 | |
| 819 | const CharUnits FOffset = Context.toCharUnitsFromBits( |
| 820 | BitSize: Layout.getFieldOffset(FieldNo: F->getFieldIndex())); |
| 821 | |
| 822 | // As C11 defines, a zero sized bit-field defines a barrier, so |
| 823 | // fields after and before it should be race condition free. |
| 824 | // The AAPCS acknowledges it and imposes no restritions when the |
| 825 | // natural container overlaps a zero-length bit-field. |
| 826 | if (F->isZeroLengthBitField()) { |
| 827 | if (End > FOffset && StorageOffset < FOffset) { |
| 828 | Conflict = true; |
| 829 | break; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | const CharUnits FEnd = |
| 834 | FOffset + |
| 835 | Context.toCharUnitsFromBits( |
| 836 | BitSize: Types.ConvertTypeForMem(T: F->getType())->getPrimitiveSizeInBits()) - |
| 837 | CharUnits::One(); |
| 838 | // If no overlap, continue. |
| 839 | if (End < FOffset || FEnd < StorageOffset) |
| 840 | continue; |
| 841 | |
| 842 | // The desired load overlaps a non-bit-field member, bail out. |
| 843 | Conflict = true; |
| 844 | break; |
| 845 | } |
| 846 | |
| 847 | if (Conflict) |
| 848 | continue; |
| 849 | // Write the new bit-field access parameters. |
| 850 | // As the storage offset now is defined as the number of elements from the |
| 851 | // start of the structure, we should divide the Offset by the element size. |
| 852 | Info.VolatileStorageOffset = |
| 853 | StorageOffset / Context.toCharUnitsFromBits(BitSize: StorageSize).getQuantity(); |
| 854 | Info.VolatileStorageSize = StorageSize; |
| 855 | Info.VolatileOffset = Offset; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | void CGRecordLowering::accumulateVPtrs() { |
| 860 | if (Layout.hasOwnVFPtr()) |
| 861 | Members.push_back( |
| 862 | x: MemberInfo(CharUnits::Zero(), MemberInfo::VFPtr, |
| 863 | llvm::PointerType::getUnqual(C&: Types.getLLVMContext()))); |
| 864 | if (Layout.hasOwnVBPtr()) |
| 865 | Members.push_back( |
| 866 | x: MemberInfo(Layout.getVBPtrOffset(), MemberInfo::VBPtr, |
| 867 | llvm::PointerType::getUnqual(C&: Types.getLLVMContext()))); |
| 868 | } |
| 869 | |
| 870 | CharUnits |
| 871 | CGRecordLowering::calculateTailClippingOffset(bool isNonVirtualBaseType) const { |
| 872 | if (!RD) |
| 873 | return Layout.getDataSize(); |
| 874 | |
| 875 | CharUnits ScissorOffset = Layout.getNonVirtualSize(); |
| 876 | // In the itanium ABI, it's possible to place a vbase at a dsize that is |
| 877 | // smaller than the nvsize. Here we check to see if such a base is placed |
| 878 | // before the nvsize and set the scissor offset to that, instead of the |
| 879 | // nvsize. |
| 880 | if (!isNonVirtualBaseType && isOverlappingVBaseABI()) |
| 881 | for (const auto &Base : RD->vbases()) { |
| 882 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
| 883 | if (isEmptyRecordForLayout(Context, T: Base.getType())) |
| 884 | continue; |
| 885 | // If the vbase is a primary virtual base of some base, then it doesn't |
| 886 | // get its own storage location but instead lives inside of that base. |
| 887 | if (Context.isNearlyEmpty(RD: BaseDecl) && !hasOwnStorage(Decl: RD, Query: BaseDecl)) |
| 888 | continue; |
| 889 | ScissorOffset = std::min(a: ScissorOffset, |
| 890 | b: Layout.getVBaseClassOffset(VBase: BaseDecl)); |
| 891 | } |
| 892 | |
| 893 | return ScissorOffset; |
| 894 | } |
| 895 | |
| 896 | void CGRecordLowering::accumulateVBases() { |
| 897 | for (const auto &Base : RD->vbases()) { |
| 898 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
| 899 | if (isEmptyRecordForLayout(Context, T: Base.getType())) |
| 900 | continue; |
| 901 | CharUnits Offset = Layout.getVBaseClassOffset(VBase: BaseDecl); |
| 902 | // If the vbase is a primary virtual base of some base, then it doesn't |
| 903 | // get its own storage location but instead lives inside of that base. |
| 904 | if (isOverlappingVBaseABI() && |
| 905 | Context.isNearlyEmpty(RD: BaseDecl) && |
| 906 | !hasOwnStorage(Decl: RD, Query: BaseDecl)) { |
| 907 | Members.push_back(x: MemberInfo(Offset, MemberInfo::VBase, nullptr, |
| 908 | BaseDecl)); |
| 909 | continue; |
| 910 | } |
| 911 | // If we've got a vtordisp, add it as a storage type. |
| 912 | if (Layout.getVBaseOffsetsMap().find(Val: BaseDecl)->second.hasVtorDisp()) |
| 913 | Members.push_back(x: StorageInfo(Offset: Offset - CharUnits::fromQuantity(Quantity: 4), |
| 914 | Data: getIntNType(NumBits: 32))); |
| 915 | Members.push_back(x: MemberInfo(Offset, MemberInfo::VBase, |
| 916 | getStorageType(RD: BaseDecl), BaseDecl)); |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | bool CGRecordLowering::hasOwnStorage(const CXXRecordDecl *Decl, |
| 921 | const CXXRecordDecl *Query) const { |
| 922 | const ASTRecordLayout &DeclLayout = Context.getASTRecordLayout(D: Decl); |
| 923 | if (DeclLayout.isPrimaryBaseVirtual() && DeclLayout.getPrimaryBase() == Query) |
| 924 | return false; |
| 925 | for (const auto &Base : Decl->bases()) |
| 926 | if (!hasOwnStorage(Decl: Base.getType()->getAsCXXRecordDecl(), Query)) |
| 927 | return false; |
| 928 | return true; |
| 929 | } |
| 930 | |
| 931 | void CGRecordLowering::calculateZeroInit() { |
| 932 | for (std::vector<MemberInfo>::const_iterator Member = Members.begin(), |
| 933 | MemberEnd = Members.end(); |
| 934 | IsZeroInitializableAsBase && Member != MemberEnd; ++Member) { |
| 935 | if (Member->Kind == MemberInfo::Field) { |
| 936 | if (!Member->FD || isZeroInitializable(FD: Member->FD)) |
| 937 | continue; |
| 938 | IsZeroInitializable = IsZeroInitializableAsBase = false; |
| 939 | } else if (Member->Kind == MemberInfo::Base || |
| 940 | Member->Kind == MemberInfo::VBase) { |
| 941 | if (isZeroInitializable(RD: Member->RD)) |
| 942 | continue; |
| 943 | IsZeroInitializable = false; |
| 944 | if (Member->Kind == MemberInfo::Base) |
| 945 | IsZeroInitializableAsBase = false; |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | // Verify accumulateBitfields computed the correct storage representations. |
| 951 | void CGRecordLowering::checkBitfieldClipping(bool IsNonVirtualBaseType) const { |
| 952 | #ifndef NDEBUG |
| 953 | auto ScissorOffset = calculateTailClippingOffset(IsNonVirtualBaseType); |
| 954 | auto Tail = CharUnits::Zero(); |
| 955 | for (const auto &M : Members) { |
| 956 | // Only members with data could possibly overlap. |
| 957 | if (!M.Data) |
| 958 | continue; |
| 959 | |
| 960 | assert(M.Offset >= Tail && "Bitfield access unit is not clipped" ); |
| 961 | Tail = M.Offset + getSize(M.Data); |
| 962 | assert((Tail <= ScissorOffset || M.Offset >= ScissorOffset) && |
| 963 | "Bitfield straddles scissor offset" ); |
| 964 | } |
| 965 | #endif |
| 966 | } |
| 967 | |
| 968 | void CGRecordLowering::determinePacked(bool NVBaseType) { |
| 969 | if (Packed) |
| 970 | return; |
| 971 | CharUnits Alignment = CharUnits::One(); |
| 972 | CharUnits NVAlignment = CharUnits::One(); |
| 973 | CharUnits NVSize = |
| 974 | !NVBaseType && RD ? Layout.getNonVirtualSize() : CharUnits::Zero(); |
| 975 | for (const MemberInfo &Member : Members) { |
| 976 | if (!Member.Data) |
| 977 | continue; |
| 978 | // If any member falls at an offset that it not a multiple of its alignment, |
| 979 | // then the entire record must be packed. |
| 980 | if (Member.Offset % getAlignment(Type: Member.Data)) |
| 981 | Packed = true; |
| 982 | if (Member.Offset < NVSize) |
| 983 | NVAlignment = std::max(a: NVAlignment, b: getAlignment(Type: Member.Data)); |
| 984 | Alignment = std::max(a: Alignment, b: getAlignment(Type: Member.Data)); |
| 985 | } |
| 986 | // If the size of the record (the capstone's offset) is not a multiple of the |
| 987 | // record's alignment, it must be packed. |
| 988 | if (Members.back().Offset % Alignment) |
| 989 | Packed = true; |
| 990 | // If the non-virtual sub-object is not a multiple of the non-virtual |
| 991 | // sub-object's alignment, it must be packed. We cannot have a packed |
| 992 | // non-virtual sub-object and an unpacked complete object or vise versa. |
| 993 | if (NVSize % NVAlignment) |
| 994 | Packed = true; |
| 995 | // Update the alignment of the sentinel. |
| 996 | if (!Packed) |
| 997 | Members.back().Data = getIntNType(NumBits: Context.toBits(CharSize: Alignment)); |
| 998 | } |
| 999 | |
| 1000 | void CGRecordLowering::insertPadding() { |
| 1001 | std::vector<std::pair<CharUnits, CharUnits> > Padding; |
| 1002 | CharUnits Size = CharUnits::Zero(); |
| 1003 | for (const MemberInfo &Member : Members) { |
| 1004 | if (!Member.Data) |
| 1005 | continue; |
| 1006 | CharUnits Offset = Member.Offset; |
| 1007 | assert(Offset >= Size); |
| 1008 | // Insert padding if we need to. |
| 1009 | if (Offset != |
| 1010 | Size.alignTo(Align: Packed ? CharUnits::One() : getAlignment(Type: Member.Data))) |
| 1011 | Padding.push_back(x: std::make_pair(x&: Size, y: Offset - Size)); |
| 1012 | Size = Offset + getSize(Type: Member.Data); |
| 1013 | } |
| 1014 | if (Padding.empty()) |
| 1015 | return; |
| 1016 | // Add the padding to the Members list and sort it. |
| 1017 | for (const auto &Pad : Padding) |
| 1018 | Members.push_back(x: StorageInfo(Offset: Pad.first, Data: getByteArrayType(NumChars: Pad.second))); |
| 1019 | llvm::stable_sort(Range&: Members); |
| 1020 | } |
| 1021 | |
| 1022 | void CGRecordLowering::fillOutputFields() { |
| 1023 | for (const MemberInfo &Member : Members) { |
| 1024 | if (Member.Data) |
| 1025 | FieldTypes.push_back(Elt: Member.Data); |
| 1026 | if (Member.Kind == MemberInfo::Field) { |
| 1027 | if (Member.FD) |
| 1028 | Fields[Member.FD->getCanonicalDecl()] = FieldTypes.size() - 1; |
| 1029 | // A field without storage must be a bitfield. |
| 1030 | if (!Member.Data) |
| 1031 | setBitFieldInfo(FD: Member.FD, StartOffset: Member.Offset, StorageType: FieldTypes.back()); |
| 1032 | } else if (Member.Kind == MemberInfo::Base) |
| 1033 | NonVirtualBases[Member.RD] = FieldTypes.size() - 1; |
| 1034 | else if (Member.Kind == MemberInfo::VBase) |
| 1035 | VirtualBases[Member.RD] = FieldTypes.size() - 1; |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types, |
| 1040 | const FieldDecl *FD, |
| 1041 | uint64_t Offset, uint64_t Size, |
| 1042 | uint64_t StorageSize, |
| 1043 | CharUnits StorageOffset) { |
| 1044 | // This function is vestigial from CGRecordLayoutBuilder days but is still |
| 1045 | // used in GCObjCRuntime.cpp. That usage has a "fixme" attached to it that |
| 1046 | // when addressed will allow for the removal of this function. |
| 1047 | llvm::Type *Ty = Types.ConvertTypeForMem(T: FD->getType()); |
| 1048 | CharUnits TypeSizeInBytes = |
| 1049 | CharUnits::fromQuantity(Quantity: Types.getDataLayout().getTypeAllocSize(Ty)); |
| 1050 | uint64_t TypeSizeInBits = Types.getContext().toBits(CharSize: TypeSizeInBytes); |
| 1051 | |
| 1052 | bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType(); |
| 1053 | |
| 1054 | if (Size > TypeSizeInBits) { |
| 1055 | // We have a wide bit-field. The extra bits are only used for padding, so |
| 1056 | // if we have a bitfield of type T, with size N: |
| 1057 | // |
| 1058 | // T t : N; |
| 1059 | // |
| 1060 | // We can just assume that it's: |
| 1061 | // |
| 1062 | // T t : sizeof(T); |
| 1063 | // |
| 1064 | Size = TypeSizeInBits; |
| 1065 | } |
| 1066 | |
| 1067 | // Reverse the bit offsets for big endian machines. Because we represent |
| 1068 | // a bitfield as a single large integer load, we can imagine the bits |
| 1069 | // counting from the most-significant-bit instead of the |
| 1070 | // least-significant-bit. |
| 1071 | if (Types.getDataLayout().isBigEndian()) { |
| 1072 | Offset = StorageSize - (Offset + Size); |
| 1073 | } |
| 1074 | |
| 1075 | return CGBitFieldInfo(Offset, Size, IsSigned, StorageSize, StorageOffset); |
| 1076 | } |
| 1077 | |
| 1078 | std::unique_ptr<CGRecordLayout> |
| 1079 | CodeGenTypes::ComputeRecordLayout(const RecordDecl *D, llvm::StructType *Ty) { |
| 1080 | CGRecordLowering Builder(*this, D, /*Packed=*/false); |
| 1081 | |
| 1082 | Builder.lower(/*NonVirtualBaseType=*/NVBaseType: false); |
| 1083 | |
| 1084 | // If we're in C++, compute the base subobject type. |
| 1085 | llvm::StructType *BaseTy = nullptr; |
| 1086 | if (isa<CXXRecordDecl>(Val: D)) { |
| 1087 | BaseTy = Ty; |
| 1088 | if (Builder.Layout.getNonVirtualSize() != Builder.Layout.getSize()) { |
| 1089 | CGRecordLowering BaseBuilder(*this, D, /*Packed=*/Builder.Packed); |
| 1090 | BaseBuilder.lower(/*NonVirtualBaseType=*/NVBaseType: true); |
| 1091 | BaseTy = llvm::StructType::create( |
| 1092 | Context&: getLLVMContext(), Elements: BaseBuilder.FieldTypes, Name: "" , isPacked: BaseBuilder.Packed); |
| 1093 | addRecordTypeName(RD: D, Ty: BaseTy, suffix: ".base" ); |
| 1094 | // BaseTy and Ty must agree on their packedness for getLLVMFieldNo to work |
| 1095 | // on both of them with the same index. |
| 1096 | assert(Builder.Packed == BaseBuilder.Packed && |
| 1097 | "Non-virtual and complete types must agree on packedness" ); |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | // Fill in the struct *after* computing the base type. Filling in the body |
| 1102 | // signifies that the type is no longer opaque and record layout is complete, |
| 1103 | // but we may need to recursively layout D while laying D out as a base type. |
| 1104 | Ty->setBody(Elements: Builder.FieldTypes, isPacked: Builder.Packed); |
| 1105 | |
| 1106 | auto RL = std::make_unique<CGRecordLayout>( |
| 1107 | args&: Ty, args&: BaseTy, args: (bool)Builder.IsZeroInitializable, |
| 1108 | args: (bool)Builder.IsZeroInitializableAsBase); |
| 1109 | |
| 1110 | RL->NonVirtualBases.swap(RHS&: Builder.NonVirtualBases); |
| 1111 | RL->CompleteObjectVirtualBases.swap(RHS&: Builder.VirtualBases); |
| 1112 | |
| 1113 | // Add all the field numbers. |
| 1114 | RL->FieldInfo.swap(RHS&: Builder.Fields); |
| 1115 | |
| 1116 | // Add bitfield info. |
| 1117 | RL->BitFields.swap(RHS&: Builder.BitFields); |
| 1118 | |
| 1119 | // Dump the layout, if requested. |
| 1120 | if (getContext().getLangOpts().DumpRecordLayouts) { |
| 1121 | llvm::outs() << "\n*** Dumping IRgen Record Layout\n" ; |
| 1122 | llvm::outs() << "Record: " ; |
| 1123 | D->dump(Out&: llvm::outs()); |
| 1124 | llvm::outs() << "\nLayout: " ; |
| 1125 | RL->print(OS&: llvm::outs()); |
| 1126 | } |
| 1127 | |
| 1128 | #ifndef NDEBUG |
| 1129 | // Verify that the computed LLVM struct size matches the AST layout size. |
| 1130 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D); |
| 1131 | |
| 1132 | uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize()); |
| 1133 | assert(TypeSizeInBits == getDataLayout().getTypeAllocSizeInBits(Ty) && |
| 1134 | "Type size mismatch!" ); |
| 1135 | |
| 1136 | if (BaseTy) { |
| 1137 | CharUnits NonVirtualSize = Layout.getNonVirtualSize(); |
| 1138 | |
| 1139 | uint64_t AlignedNonVirtualTypeSizeInBits = |
| 1140 | getContext().toBits(NonVirtualSize); |
| 1141 | |
| 1142 | assert(AlignedNonVirtualTypeSizeInBits == |
| 1143 | getDataLayout().getTypeAllocSizeInBits(BaseTy) && |
| 1144 | "Type size mismatch!" ); |
| 1145 | } |
| 1146 | |
| 1147 | // Verify that the LLVM and AST field offsets agree. |
| 1148 | llvm::StructType *ST = RL->getLLVMType(); |
| 1149 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(ST); |
| 1150 | |
| 1151 | const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D); |
| 1152 | RecordDecl::field_iterator it = D->field_begin(); |
| 1153 | for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) { |
| 1154 | const FieldDecl *FD = *it; |
| 1155 | |
| 1156 | // Ignore zero-sized fields. |
| 1157 | if (isEmptyFieldForLayout(getContext(), FD)) |
| 1158 | continue; |
| 1159 | |
| 1160 | // For non-bit-fields, just check that the LLVM struct offset matches the |
| 1161 | // AST offset. |
| 1162 | if (!FD->isBitField()) { |
| 1163 | unsigned FieldNo = RL->getLLVMFieldNo(FD); |
| 1164 | assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) && |
| 1165 | "Invalid field offset!" ); |
| 1166 | continue; |
| 1167 | } |
| 1168 | |
| 1169 | // Ignore unnamed bit-fields. |
| 1170 | if (!FD->getDeclName()) |
| 1171 | continue; |
| 1172 | |
| 1173 | const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD); |
| 1174 | llvm::Type *ElementTy = ST->getTypeAtIndex(RL->getLLVMFieldNo(FD)); |
| 1175 | |
| 1176 | // Unions have overlapping elements dictating their layout, but for |
| 1177 | // non-unions we can verify that this section of the layout is the exact |
| 1178 | // expected size. |
| 1179 | if (D->isUnion()) { |
| 1180 | // For unions we verify that the start is zero and the size |
| 1181 | // is in-bounds. However, on BE systems, the offset may be non-zero, but |
| 1182 | // the size + offset should match the storage size in that case as it |
| 1183 | // "starts" at the back. |
| 1184 | if (getDataLayout().isBigEndian()) |
| 1185 | assert(static_cast<unsigned>(Info.Offset + Info.Size) == |
| 1186 | Info.StorageSize && |
| 1187 | "Big endian union bitfield does not end at the back" ); |
| 1188 | else |
| 1189 | assert(Info.Offset == 0 && |
| 1190 | "Little endian union bitfield with a non-zero offset" ); |
| 1191 | assert(Info.StorageSize <= SL->getSizeInBits() && |
| 1192 | "Union not large enough for bitfield storage" ); |
| 1193 | } else { |
| 1194 | assert((Info.StorageSize == |
| 1195 | getDataLayout().getTypeAllocSizeInBits(ElementTy) || |
| 1196 | Info.VolatileStorageSize == |
| 1197 | getDataLayout().getTypeAllocSizeInBits(ElementTy)) && |
| 1198 | "Storage size does not match the element type size" ); |
| 1199 | } |
| 1200 | assert(Info.Size > 0 && "Empty bitfield!" ); |
| 1201 | assert(static_cast<unsigned>(Info.Offset) + Info.Size <= Info.StorageSize && |
| 1202 | "Bitfield outside of its allocated storage" ); |
| 1203 | } |
| 1204 | #endif |
| 1205 | |
| 1206 | return RL; |
| 1207 | } |
| 1208 | |
| 1209 | void CGRecordLayout::print(raw_ostream &OS) const { |
| 1210 | OS << "<CGRecordLayout\n" ; |
| 1211 | OS << " LLVMType:" << *CompleteObjectType << "\n" ; |
| 1212 | if (BaseSubobjectType) |
| 1213 | OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n" ; |
| 1214 | OS << " IsZeroInitializable:" << IsZeroInitializable << "\n" ; |
| 1215 | OS << " BitFields:[\n" ; |
| 1216 | |
| 1217 | // Print bit-field infos in declaration order. |
| 1218 | std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs; |
| 1219 | for (const auto &BitField : BitFields) { |
| 1220 | const RecordDecl *RD = BitField.first->getParent(); |
| 1221 | unsigned Index = 0; |
| 1222 | for (RecordDecl::field_iterator it2 = RD->field_begin(); |
| 1223 | *it2 != BitField.first; ++it2) |
| 1224 | ++Index; |
| 1225 | BFIs.push_back(x: std::make_pair(x&: Index, y: &BitField.second)); |
| 1226 | } |
| 1227 | llvm::array_pod_sort(Start: BFIs.begin(), End: BFIs.end()); |
| 1228 | for (auto &BFI : BFIs) { |
| 1229 | OS.indent(NumSpaces: 4); |
| 1230 | BFI.second->print(OS); |
| 1231 | OS << "\n" ; |
| 1232 | } |
| 1233 | |
| 1234 | OS << "]>\n" ; |
| 1235 | } |
| 1236 | |
| 1237 | LLVM_DUMP_METHOD void CGRecordLayout::dump() const { |
| 1238 | print(OS&: llvm::errs()); |
| 1239 | } |
| 1240 | |
| 1241 | void CGBitFieldInfo::print(raw_ostream &OS) const { |
| 1242 | OS << "<CGBitFieldInfo" |
| 1243 | << " Offset:" << Offset << " Size:" << Size << " IsSigned:" << IsSigned |
| 1244 | << " StorageSize:" << StorageSize |
| 1245 | << " StorageOffset:" << StorageOffset.getQuantity() |
| 1246 | << " VolatileOffset:" << VolatileOffset |
| 1247 | << " VolatileStorageSize:" << VolatileStorageSize |
| 1248 | << " VolatileStorageOffset:" << VolatileStorageOffset.getQuantity() << ">" ; |
| 1249 | } |
| 1250 | |
| 1251 | LLVM_DUMP_METHOD void CGBitFieldInfo::dump() const { |
| 1252 | print(OS&: llvm::errs()); |
| 1253 | } |
| 1254 | |