106f32e7eSjoerg //===--- CGRecordLayout.h - LLVM Record Layout Information ------*- C++ -*-===// 206f32e7eSjoerg // 306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information. 506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 606f32e7eSjoerg // 706f32e7eSjoerg //===----------------------------------------------------------------------===// 806f32e7eSjoerg 906f32e7eSjoerg #ifndef LLVM_CLANG_LIB_CODEGEN_CGRECORDLAYOUT_H 1006f32e7eSjoerg #define LLVM_CLANG_LIB_CODEGEN_CGRECORDLAYOUT_H 1106f32e7eSjoerg 1206f32e7eSjoerg #include "clang/AST/CharUnits.h" 1306f32e7eSjoerg #include "clang/AST/DeclCXX.h" 1406f32e7eSjoerg #include "clang/Basic/LLVM.h" 1506f32e7eSjoerg #include "llvm/ADT/DenseMap.h" 1606f32e7eSjoerg #include "llvm/IR/DerivedTypes.h" 1706f32e7eSjoerg 1806f32e7eSjoerg namespace llvm { 1906f32e7eSjoerg class StructType; 2006f32e7eSjoerg } 2106f32e7eSjoerg 2206f32e7eSjoerg namespace clang { 2306f32e7eSjoerg namespace CodeGen { 2406f32e7eSjoerg 2506f32e7eSjoerg /// Structure with information about how a bitfield should be accessed. 2606f32e7eSjoerg /// 2706f32e7eSjoerg /// Often we layout a sequence of bitfields as a contiguous sequence of bits. 2806f32e7eSjoerg /// When the AST record layout does this, we represent it in the LLVM IR's type 2906f32e7eSjoerg /// as either a sequence of i8 members or a byte array to reserve the number of 3006f32e7eSjoerg /// bytes touched without forcing any particular alignment beyond the basic 3106f32e7eSjoerg /// character alignment. 3206f32e7eSjoerg /// 3306f32e7eSjoerg /// Then accessing a particular bitfield involves converting this byte array 3406f32e7eSjoerg /// into a single integer of that size (i24 or i40 -- may not be power-of-two 3506f32e7eSjoerg /// size), loading it, and shifting and masking to extract the particular 3606f32e7eSjoerg /// subsequence of bits which make up that particular bitfield. This structure 3706f32e7eSjoerg /// encodes the information used to construct the extraction code sequences. 3806f32e7eSjoerg /// The CGRecordLayout also has a field index which encodes which byte-sequence 3906f32e7eSjoerg /// this bitfield falls within. Let's assume the following C struct: 4006f32e7eSjoerg /// 4106f32e7eSjoerg /// struct S { 4206f32e7eSjoerg /// char a, b, c; 4306f32e7eSjoerg /// unsigned bits : 3; 4406f32e7eSjoerg /// unsigned more_bits : 4; 4506f32e7eSjoerg /// unsigned still_more_bits : 7; 4606f32e7eSjoerg /// }; 4706f32e7eSjoerg /// 4806f32e7eSjoerg /// This will end up as the following LLVM type. The first array is the 49*13fbcb42Sjoerg /// bitfield, and the second is the padding out to a 4-byte alignment. 5006f32e7eSjoerg /// 5106f32e7eSjoerg /// %t = type { i8, i8, i8, i8, i8, [3 x i8] } 5206f32e7eSjoerg /// 5306f32e7eSjoerg /// When generating code to access more_bits, we'll generate something 5406f32e7eSjoerg /// essentially like this: 5506f32e7eSjoerg /// 5606f32e7eSjoerg /// define i32 @foo(%t* %base) { 5706f32e7eSjoerg /// %0 = gep %t* %base, i32 0, i32 3 5806f32e7eSjoerg /// %2 = load i8* %1 5906f32e7eSjoerg /// %3 = lshr i8 %2, 3 6006f32e7eSjoerg /// %4 = and i8 %3, 15 6106f32e7eSjoerg /// %5 = zext i8 %4 to i32 6206f32e7eSjoerg /// ret i32 %i 6306f32e7eSjoerg /// } 6406f32e7eSjoerg /// 6506f32e7eSjoerg struct CGBitFieldInfo { 6606f32e7eSjoerg /// The offset within a contiguous run of bitfields that are represented as 6706f32e7eSjoerg /// a single "field" within the LLVM struct type. This offset is in bits. 6806f32e7eSjoerg unsigned Offset : 16; 6906f32e7eSjoerg 7006f32e7eSjoerg /// The total size of the bit-field, in bits. 7106f32e7eSjoerg unsigned Size : 15; 7206f32e7eSjoerg 7306f32e7eSjoerg /// Whether the bit-field is signed. 7406f32e7eSjoerg unsigned IsSigned : 1; 7506f32e7eSjoerg 7606f32e7eSjoerg /// The storage size in bits which should be used when accessing this 7706f32e7eSjoerg /// bitfield. 7806f32e7eSjoerg unsigned StorageSize; 7906f32e7eSjoerg 8006f32e7eSjoerg /// The offset of the bitfield storage from the start of the struct. 8106f32e7eSjoerg CharUnits StorageOffset; 8206f32e7eSjoerg 83*13fbcb42Sjoerg /// The offset within a contiguous run of bitfields that are represented as a 84*13fbcb42Sjoerg /// single "field" within the LLVM struct type, taking into account the AAPCS 85*13fbcb42Sjoerg /// rules for volatile bitfields. This offset is in bits. 86*13fbcb42Sjoerg unsigned VolatileOffset : 16; 87*13fbcb42Sjoerg 88*13fbcb42Sjoerg /// The storage size in bits which should be used when accessing this 89*13fbcb42Sjoerg /// bitfield. 90*13fbcb42Sjoerg unsigned VolatileStorageSize; 91*13fbcb42Sjoerg 92*13fbcb42Sjoerg /// The offset of the bitfield storage from the start of the struct. 93*13fbcb42Sjoerg CharUnits VolatileStorageOffset; 94*13fbcb42Sjoerg CGBitFieldInfoCGBitFieldInfo9506f32e7eSjoerg CGBitFieldInfo() 96*13fbcb42Sjoerg : Offset(), Size(), IsSigned(), StorageSize(), StorageOffset(), 97*13fbcb42Sjoerg VolatileOffset(), VolatileStorageSize(), VolatileStorageOffset() {} 9806f32e7eSjoerg CGBitFieldInfoCGBitFieldInfo9906f32e7eSjoerg CGBitFieldInfo(unsigned Offset, unsigned Size, bool IsSigned, 10006f32e7eSjoerg unsigned StorageSize, CharUnits StorageOffset) 10106f32e7eSjoerg : Offset(Offset), Size(Size), IsSigned(IsSigned), 10206f32e7eSjoerg StorageSize(StorageSize), StorageOffset(StorageOffset) {} 10306f32e7eSjoerg 10406f32e7eSjoerg void print(raw_ostream &OS) const; 10506f32e7eSjoerg void dump() const; 10606f32e7eSjoerg 10706f32e7eSjoerg /// Given a bit-field decl, build an appropriate helper object for 10806f32e7eSjoerg /// accessing that field (which is expected to have the given offset and 10906f32e7eSjoerg /// size). 11006f32e7eSjoerg static CGBitFieldInfo MakeInfo(class CodeGenTypes &Types, 11106f32e7eSjoerg const FieldDecl *FD, 11206f32e7eSjoerg uint64_t Offset, uint64_t Size, 11306f32e7eSjoerg uint64_t StorageSize, 11406f32e7eSjoerg CharUnits StorageOffset); 11506f32e7eSjoerg }; 11606f32e7eSjoerg 11706f32e7eSjoerg /// CGRecordLayout - This class handles struct and union layout info while 11806f32e7eSjoerg /// lowering AST types to LLVM types. 11906f32e7eSjoerg /// 12006f32e7eSjoerg /// These layout objects are only created on demand as IR generation requires. 12106f32e7eSjoerg class CGRecordLayout { 12206f32e7eSjoerg friend class CodeGenTypes; 12306f32e7eSjoerg 12406f32e7eSjoerg CGRecordLayout(const CGRecordLayout &) = delete; 12506f32e7eSjoerg void operator=(const CGRecordLayout &) = delete; 12606f32e7eSjoerg 12706f32e7eSjoerg private: 12806f32e7eSjoerg /// The LLVM type corresponding to this record layout; used when 12906f32e7eSjoerg /// laying it out as a complete object. 13006f32e7eSjoerg llvm::StructType *CompleteObjectType; 13106f32e7eSjoerg 13206f32e7eSjoerg /// The LLVM type for the non-virtual part of this record layout; 13306f32e7eSjoerg /// used when laying it out as a base subobject. 13406f32e7eSjoerg llvm::StructType *BaseSubobjectType; 13506f32e7eSjoerg 13606f32e7eSjoerg /// Map from (non-bit-field) struct field to the corresponding llvm struct 13706f32e7eSjoerg /// type field no. This info is populated by record builder. 13806f32e7eSjoerg llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo; 13906f32e7eSjoerg 14006f32e7eSjoerg /// Map from (bit-field) struct field to the corresponding llvm struct type 14106f32e7eSjoerg /// field no. This info is populated by record builder. 14206f32e7eSjoerg llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields; 14306f32e7eSjoerg 14406f32e7eSjoerg // FIXME: Maybe we could use a CXXBaseSpecifier as the key and use a single 14506f32e7eSjoerg // map for both virtual and non-virtual bases. 14606f32e7eSjoerg llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases; 14706f32e7eSjoerg 14806f32e7eSjoerg /// Map from virtual bases to their field index in the complete object. 14906f32e7eSjoerg llvm::DenseMap<const CXXRecordDecl *, unsigned> CompleteObjectVirtualBases; 15006f32e7eSjoerg 15106f32e7eSjoerg /// False if any direct or indirect subobject of this class, when 15206f32e7eSjoerg /// considered as a complete object, requires a non-zero bitpattern 15306f32e7eSjoerg /// when zero-initialized. 15406f32e7eSjoerg bool IsZeroInitializable : 1; 15506f32e7eSjoerg 15606f32e7eSjoerg /// False if any direct or indirect subobject of this class, when 15706f32e7eSjoerg /// considered as a base subobject, requires a non-zero bitpattern 15806f32e7eSjoerg /// when zero-initialized. 15906f32e7eSjoerg bool IsZeroInitializableAsBase : 1; 16006f32e7eSjoerg 16106f32e7eSjoerg public: CGRecordLayout(llvm::StructType * CompleteObjectType,llvm::StructType * BaseSubobjectType,bool IsZeroInitializable,bool IsZeroInitializableAsBase)16206f32e7eSjoerg CGRecordLayout(llvm::StructType *CompleteObjectType, 16306f32e7eSjoerg llvm::StructType *BaseSubobjectType, 16406f32e7eSjoerg bool IsZeroInitializable, 16506f32e7eSjoerg bool IsZeroInitializableAsBase) 16606f32e7eSjoerg : CompleteObjectType(CompleteObjectType), 16706f32e7eSjoerg BaseSubobjectType(BaseSubobjectType), 16806f32e7eSjoerg IsZeroInitializable(IsZeroInitializable), 16906f32e7eSjoerg IsZeroInitializableAsBase(IsZeroInitializableAsBase) {} 17006f32e7eSjoerg 17106f32e7eSjoerg /// Return the "complete object" LLVM type associated with 17206f32e7eSjoerg /// this record. getLLVMType()17306f32e7eSjoerg llvm::StructType *getLLVMType() const { 17406f32e7eSjoerg return CompleteObjectType; 17506f32e7eSjoerg } 17606f32e7eSjoerg 17706f32e7eSjoerg /// Return the "base subobject" LLVM type associated with 17806f32e7eSjoerg /// this record. getBaseSubobjectLLVMType()17906f32e7eSjoerg llvm::StructType *getBaseSubobjectLLVMType() const { 18006f32e7eSjoerg return BaseSubobjectType; 18106f32e7eSjoerg } 18206f32e7eSjoerg 18306f32e7eSjoerg /// Check whether this struct can be C++ zero-initialized 18406f32e7eSjoerg /// with a zeroinitializer. isZeroInitializable()18506f32e7eSjoerg bool isZeroInitializable() const { 18606f32e7eSjoerg return IsZeroInitializable; 18706f32e7eSjoerg } 18806f32e7eSjoerg 18906f32e7eSjoerg /// Check whether this struct can be C++ zero-initialized 19006f32e7eSjoerg /// with a zeroinitializer when considered as a base subobject. isZeroInitializableAsBase()19106f32e7eSjoerg bool isZeroInitializableAsBase() const { 19206f32e7eSjoerg return IsZeroInitializableAsBase; 19306f32e7eSjoerg } 19406f32e7eSjoerg 19506f32e7eSjoerg /// Return llvm::StructType element number that corresponds to the 19606f32e7eSjoerg /// field FD. getLLVMFieldNo(const FieldDecl * FD)19706f32e7eSjoerg unsigned getLLVMFieldNo(const FieldDecl *FD) const { 19806f32e7eSjoerg FD = FD->getCanonicalDecl(); 19906f32e7eSjoerg assert(FieldInfo.count(FD) && "Invalid field for record!"); 20006f32e7eSjoerg return FieldInfo.lookup(FD); 20106f32e7eSjoerg } 20206f32e7eSjoerg getNonVirtualBaseLLVMFieldNo(const CXXRecordDecl * RD)20306f32e7eSjoerg unsigned getNonVirtualBaseLLVMFieldNo(const CXXRecordDecl *RD) const { 20406f32e7eSjoerg assert(NonVirtualBases.count(RD) && "Invalid non-virtual base!"); 20506f32e7eSjoerg return NonVirtualBases.lookup(RD); 20606f32e7eSjoerg } 20706f32e7eSjoerg 20806f32e7eSjoerg /// Return the LLVM field index corresponding to the given 20906f32e7eSjoerg /// virtual base. Only valid when operating on the complete object. getVirtualBaseIndex(const CXXRecordDecl * base)21006f32e7eSjoerg unsigned getVirtualBaseIndex(const CXXRecordDecl *base) const { 21106f32e7eSjoerg assert(CompleteObjectVirtualBases.count(base) && "Invalid virtual base!"); 21206f32e7eSjoerg return CompleteObjectVirtualBases.lookup(base); 21306f32e7eSjoerg } 21406f32e7eSjoerg 21506f32e7eSjoerg /// Return the BitFieldInfo that corresponds to the field FD. getBitFieldInfo(const FieldDecl * FD)21606f32e7eSjoerg const CGBitFieldInfo &getBitFieldInfo(const FieldDecl *FD) const { 21706f32e7eSjoerg FD = FD->getCanonicalDecl(); 21806f32e7eSjoerg assert(FD->isBitField() && "Invalid call for non-bit-field decl!"); 21906f32e7eSjoerg llvm::DenseMap<const FieldDecl *, CGBitFieldInfo>::const_iterator 22006f32e7eSjoerg it = BitFields.find(FD); 22106f32e7eSjoerg assert(it != BitFields.end() && "Unable to find bitfield info"); 22206f32e7eSjoerg return it->second; 22306f32e7eSjoerg } 22406f32e7eSjoerg 22506f32e7eSjoerg void print(raw_ostream &OS) const; 22606f32e7eSjoerg void dump() const; 22706f32e7eSjoerg }; 22806f32e7eSjoerg 22906f32e7eSjoerg } // end namespace CodeGen 23006f32e7eSjoerg } // end namespace clang 23106f32e7eSjoerg 23206f32e7eSjoerg #endif 233