Daniel Dunbar | 2924ade | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 1 | //===--- CGRecordLayout.h - LLVM Record Layout Information ------*- C++ -*-===// |
| 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 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 10 | #ifndef LLVM_CLANG_LIB_CODEGEN_CGRECORDLAYOUT_H |
| 11 | #define LLVM_CLANG_LIB_CODEGEN_CGRECORDLAYOUT_H |
Daniel Dunbar | 2924ade | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 12 | |
Ken Dyck | 28ebde5 | 2011-04-24 10:04:59 +0000 | [diff] [blame] | 13 | #include "clang/AST/CharUnits.h" |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 14 | #include "clang/AST/Decl.h" |
Chris Lattner | d47d3b0 | 2011-07-23 10:35:09 +0000 | [diff] [blame] | 15 | #include "clang/Basic/LLVM.h" |
| 16 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 17 | #include "llvm/IR/DerivedTypes.h" |
Chris Lattner | d47d3b0 | 2011-07-23 10:35:09 +0000 | [diff] [blame] | 18 | |
Daniel Dunbar | 270e203 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 19 | namespace llvm { |
Anders Carlsson | ba2c2ee | 2010-11-24 19:37:16 +0000 | [diff] [blame] | 20 | class StructType; |
Daniel Dunbar | 270e203 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 21 | } |
| 22 | |
Daniel Dunbar | 2924ade | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 23 | namespace clang { |
| 24 | namespace CodeGen { |
| 25 | |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 26 | /// \brief Structure with information about how a bitfield should be accessed. |
Daniel Dunbar | ab970f9 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 27 | /// |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 28 | /// Often we layout a sequence of bitfields as a contiguous sequence of bits. |
| 29 | /// When the AST record layout does this, we represent it in the LLVM IR's type |
| 30 | /// as either a sequence of i8 members or a byte array to reserve the number of |
| 31 | /// bytes touched without forcing any particular alignment beyond the basic |
| 32 | /// character alignment. |
| 33 | /// |
| 34 | /// Then accessing a particular bitfield involves converting this byte array |
| 35 | /// into a single integer of that size (i24 or i40 -- may not be power-of-two |
| 36 | /// size), loading it, and shifting and masking to extract the particular |
| 37 | /// subsequence of bits which make up that particular bitfield. This structure |
| 38 | /// encodes the information used to construct the extraction code sequences. |
| 39 | /// The CGRecordLayout also has a field index which encodes which byte-sequence |
| 40 | /// this bitfield falls within. Let's assume the following C struct: |
| 41 | /// |
| 42 | /// struct S { |
| 43 | /// char a, b, c; |
| 44 | /// unsigned bits : 3; |
| 45 | /// unsigned more_bits : 4; |
| 46 | /// unsigned still_more_bits : 7; |
| 47 | /// }; |
| 48 | /// |
| 49 | /// This will end up as the following LLVM type. The first array is the |
| 50 | /// bitfield, and the second is the padding out to a 4-byte alignmnet. |
| 51 | /// |
| 52 | /// %t = type { i8, i8, i8, i8, i8, [3 x i8] } |
| 53 | /// |
| 54 | /// When generating code to access more_bits, we'll generate something |
| 55 | /// essentially like this: |
| 56 | /// |
| 57 | /// define i32 @foo(%t* %base) { |
| 58 | /// %0 = gep %t* %base, i32 0, i32 3 |
| 59 | /// %2 = load i8* %1 |
| 60 | /// %3 = lshr i8 %2, 3 |
| 61 | /// %4 = and i8 %3, 15 |
| 62 | /// %5 = zext i8 %4 to i32 |
| 63 | /// ret i32 %i |
| 64 | /// } |
| 65 | /// |
| 66 | struct CGBitFieldInfo { |
| 67 | /// The offset within a contiguous run of bitfields that are represented as |
| 68 | /// a single "field" within the LLVM struct type. This offset is in bits. |
| 69 | unsigned Offset : 16; |
Daniel Dunbar | ab970f9 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 70 | |
Daniel Dunbar | 7fb6195 | 2010-04-15 05:09:28 +0000 | [diff] [blame] | 71 | /// The total size of the bit-field, in bits. |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 72 | unsigned Size : 15; |
Daniel Dunbar | 7fb6195 | 2010-04-15 05:09:28 +0000 | [diff] [blame] | 73 | |
| 74 | /// Whether the bit-field is signed. |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 75 | unsigned IsSigned : 1; |
Daniel Dunbar | 93c6296 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 76 | |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 77 | /// The storage size in bits which should be used when accessing this |
| 78 | /// bitfield. |
| 79 | unsigned StorageSize; |
Daniel Dunbar | 2df2569 | 2010-04-15 05:09:32 +0000 | [diff] [blame] | 80 | |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 81 | /// The alignment which should be used when accessing the bitfield. |
| 82 | unsigned StorageAlignment; |
Daniel Dunbar | 2df2569 | 2010-04-15 05:09:32 +0000 | [diff] [blame] | 83 | |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 84 | CGBitFieldInfo() |
| 85 | : Offset(), Size(), IsSigned(), StorageSize(), StorageAlignment() {} |
Daniel Dunbar | 2df2569 | 2010-04-15 05:09:32 +0000 | [diff] [blame] | 86 | |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 87 | CGBitFieldInfo(unsigned Offset, unsigned Size, bool IsSigned, |
| 88 | unsigned StorageSize, unsigned StorageAlignment) |
| 89 | : Offset(Offset), Size(Size), IsSigned(IsSigned), |
| 90 | StorageSize(StorageSize), StorageAlignment(StorageAlignment) {} |
Daniel Dunbar | e792584 | 2010-04-14 04:07:59 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 8cc488f | 2011-07-20 07:06:53 +0000 | [diff] [blame] | 92 | void print(raw_ostream &OS) const; |
Daniel Dunbar | 93c6296 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 93 | void dump() const; |
Daniel Dunbar | e7a80bd | 2010-09-02 23:53:28 +0000 | [diff] [blame] | 94 | |
| 95 | /// \brief Given a bit-field decl, build an appropriate helper object for |
| 96 | /// accessing that field (which is expected to have the given offset and |
| 97 | /// size). |
Chandler Carruth | 72d2dab | 2012-12-06 11:14:44 +0000 | [diff] [blame] | 98 | static CGBitFieldInfo MakeInfo(class CodeGenTypes &Types, |
| 99 | const FieldDecl *FD, |
| 100 | uint64_t Offset, uint64_t Size, |
| 101 | uint64_t StorageSize, |
| 102 | uint64_t StorageAlignment); |
Daniel Dunbar | 2eec0b2 | 2010-04-05 16:20:44 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
Daniel Dunbar | 2924ade | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 105 | /// CGRecordLayout - This class handles struct and union layout info while |
| 106 | /// lowering AST types to LLVM types. |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 107 | /// |
| 108 | /// These layout objects are only created on demand as IR generation requires. |
Daniel Dunbar | 2924ade | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 109 | class CGRecordLayout { |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 110 | friend class CodeGenTypes; |
| 111 | |
Dmitri Gribenko | f56faa0 | 2012-09-15 20:20:27 +0000 | [diff] [blame] | 112 | CGRecordLayout(const CGRecordLayout &) LLVM_DELETED_FUNCTION; |
| 113 | void operator=(const CGRecordLayout &) LLVM_DELETED_FUNCTION; |
Daniel Dunbar | 2924ade | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 114 | |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 115 | private: |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 116 | /// The LLVM type corresponding to this record layout; used when |
| 117 | /// laying it out as a complete object. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 118 | llvm::StructType *CompleteObjectType; |
Daniel Dunbar | 2924ade | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 119 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 120 | /// The LLVM type for the non-virtual part of this record layout; |
| 121 | /// used when laying it out as a base subobject. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 122 | llvm::StructType *BaseSubobjectType; |
Anders Carlsson | 3d155e6 | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 123 | |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 124 | /// Map from (non-bit-field) struct field to the corresponding llvm struct |
| 125 | /// type field no. This info is populated by record builder. |
| 126 | llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo; |
| 127 | |
| 128 | /// Map from (bit-field) struct field to the corresponding llvm struct type |
| 129 | /// field no. This info is populated by record builder. |
Daniel Dunbar | 2eec0b2 | 2010-04-05 16:20:44 +0000 | [diff] [blame] | 130 | llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields; |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 131 | |
Anders Carlsson | c6772ce | 2010-05-18 05:22:06 +0000 | [diff] [blame] | 132 | // FIXME: Maybe we could use a CXXBaseSpecifier as the key and use a single |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 133 | // map for both virtual and non-virtual bases. |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 134 | llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases; |
Anders Carlsson | c6772ce | 2010-05-18 05:22:06 +0000 | [diff] [blame] | 135 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 136 | /// Map from virtual bases to their field index in the complete object. |
| 137 | llvm::DenseMap<const CXXRecordDecl *, unsigned> CompleteObjectVirtualBases; |
| 138 | |
| 139 | /// False if any direct or indirect subobject of this class, when |
| 140 | /// considered as a complete object, requires a non-zero bitpattern |
| 141 | /// when zero-initialized. |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 142 | bool IsZeroInitializable : 1; |
Daniel Dunbar | 2924ade | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 143 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 144 | /// False if any direct or indirect subobject of this class, when |
| 145 | /// considered as a base subobject, requires a non-zero bitpattern |
| 146 | /// when zero-initialized. |
| 147 | bool IsZeroInitializableAsBase : 1; |
Daniel Dunbar | 2924ade | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 148 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 149 | public: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 150 | CGRecordLayout(llvm::StructType *CompleteObjectType, |
| 151 | llvm::StructType *BaseSubobjectType, |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 152 | bool IsZeroInitializable, |
| 153 | bool IsZeroInitializableAsBase) |
| 154 | : CompleteObjectType(CompleteObjectType), |
| 155 | BaseSubobjectType(BaseSubobjectType), |
| 156 | IsZeroInitializable(IsZeroInitializable), |
| 157 | IsZeroInitializableAsBase(IsZeroInitializableAsBase) {} |
| 158 | |
| 159 | /// \brief Return the "complete object" LLVM type associated with |
| 160 | /// this record. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 161 | llvm::StructType *getLLVMType() const { |
| 162 | return CompleteObjectType; |
Daniel Dunbar | 2924ade | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 163 | } |
| 164 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 165 | /// \brief Return the "base subobject" LLVM type associated with |
| 166 | /// this record. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 167 | llvm::StructType *getBaseSubobjectLLVMType() const { |
| 168 | return BaseSubobjectType; |
Anders Carlsson | 3d155e6 | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 169 | } |
| 170 | |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 171 | /// \brief Check whether this struct can be C++ zero-initialized |
| 172 | /// with a zeroinitializer. |
| 173 | bool isZeroInitializable() const { |
| 174 | return IsZeroInitializable; |
Daniel Dunbar | 2924ade | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 175 | } |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 176 | |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 177 | /// \brief Check whether this struct can be C++ zero-initialized |
| 178 | /// with a zeroinitializer when considered as a base subobject. |
| 179 | bool isZeroInitializableAsBase() const { |
| 180 | return IsZeroInitializableAsBase; |
| 181 | } |
| 182 | |
Daniel Dunbar | 50810d3 | 2010-04-27 14:51:07 +0000 | [diff] [blame] | 183 | /// \brief Return llvm::StructType element number that corresponds to the |
| 184 | /// field FD. |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 185 | unsigned getLLVMFieldNo(const FieldDecl *FD) const { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 186 | FD = FD->getCanonicalDecl(); |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 187 | assert(FieldInfo.count(FD) && "Invalid field for record!"); |
| 188 | return FieldInfo.lookup(FD); |
| 189 | } |
| 190 | |
Anders Carlsson | c6772ce | 2010-05-18 05:22:06 +0000 | [diff] [blame] | 191 | unsigned getNonVirtualBaseLLVMFieldNo(const CXXRecordDecl *RD) const { |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 192 | assert(NonVirtualBases.count(RD) && "Invalid non-virtual base!"); |
| 193 | return NonVirtualBases.lookup(RD); |
| 194 | } |
| 195 | |
| 196 | /// \brief Return the LLVM field index corresponding to the given |
| 197 | /// virtual base. Only valid when operating on the complete object. |
| 198 | unsigned getVirtualBaseIndex(const CXXRecordDecl *base) const { |
| 199 | assert(CompleteObjectVirtualBases.count(base) && "Invalid virtual base!"); |
| 200 | return CompleteObjectVirtualBases.lookup(base); |
Anders Carlsson | c6772ce | 2010-05-18 05:22:06 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Daniel Dunbar | 50810d3 | 2010-04-27 14:51:07 +0000 | [diff] [blame] | 203 | /// \brief Return the BitFieldInfo that corresponds to the field FD. |
Daniel Dunbar | 2eec0b2 | 2010-04-05 16:20:44 +0000 | [diff] [blame] | 204 | const CGBitFieldInfo &getBitFieldInfo(const FieldDecl *FD) const { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 205 | FD = FD->getCanonicalDecl(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 206 | assert(FD->isBitField() && "Invalid call for non-bit-field decl!"); |
Daniel Dunbar | 2eec0b2 | 2010-04-05 16:20:44 +0000 | [diff] [blame] | 207 | llvm::DenseMap<const FieldDecl *, CGBitFieldInfo>::const_iterator |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 208 | it = BitFields.find(FD); |
John McCall | 9b7da1c | 2011-02-15 06:40:56 +0000 | [diff] [blame] | 209 | assert(it != BitFields.end() && "Unable to find bitfield info"); |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 210 | return it->second; |
| 211 | } |
Daniel Dunbar | 93c6296 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 8cc488f | 2011-07-20 07:06:53 +0000 | [diff] [blame] | 213 | void print(raw_ostream &OS) const; |
Daniel Dunbar | 93c6296 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 214 | void dump() const; |
Daniel Dunbar | 2924ade | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | } // end namespace CodeGen |
| 218 | } // end namespace clang |
| 219 | |
| 220 | #endif |