Daniel Dunbar | 072d0bb | 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 | |
| 10 | #ifndef CLANG_CODEGEN_CGRECORDLAYOUT_H |
| 11 | #define CLANG_CODEGEN_CGRECORDLAYOUT_H |
| 12 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/DenseMap.h" |
John McCall | 5a39bd2 | 2010-11-30 23:21:46 +0000 | [diff] [blame^] | 14 | #include "llvm/DerivedTypes.h" |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 16 | namespace llvm { |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 17 | class raw_ostream; |
Anders Carlsson | 36e2fa8 | 2010-11-24 19:37:16 +0000 | [diff] [blame] | 18 | class StructType; |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 19 | } |
| 20 | |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 21 | namespace clang { |
| 22 | namespace CodeGen { |
| 23 | |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 24 | /// \brief Helper object for describing how to generate the code for access to a |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 25 | /// bit-field. |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 26 | /// |
| 27 | /// This structure is intended to describe the "policy" of how the bit-field |
| 28 | /// should be accessed, which may be target, language, or ABI dependent. |
Daniel Dunbar | cd3d5e7 | 2010-04-05 16:20:44 +0000 | [diff] [blame] | 29 | class CGBitFieldInfo { |
| 30 | public: |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 31 | /// Descriptor for a single component of a bit-field access. The entire |
| 32 | /// bit-field is constituted of a bitwise OR of all of the individual |
| 33 | /// components. |
| 34 | /// |
| 35 | /// Each component describes an accessed value, which is how the component |
| 36 | /// should be transferred to/from memory, and a target placement, which is how |
| 37 | /// that component fits into the constituted bit-field. The pseudo-IR for a |
| 38 | /// load is: |
| 39 | /// |
| 40 | /// %0 = gep %base, 0, FieldIndex |
| 41 | /// %1 = gep (i8*) %0, FieldByteOffset |
| 42 | /// %2 = (i(AccessWidth) *) %1 |
| 43 | /// %3 = load %2, align AccessAlignment |
| 44 | /// %4 = shr %3, FieldBitStart |
| 45 | /// |
| 46 | /// and the composed bit-field is formed as the boolean OR of all accesses, |
| 47 | /// masked to TargetBitWidth bits and shifted to TargetBitOffset. |
| 48 | struct AccessInfo { |
| 49 | /// Offset of the field to load in the LLVM structure, if any. |
| 50 | unsigned FieldIndex; |
| 51 | |
| 52 | /// Byte offset from the field address, if any. This should generally be |
| 53 | /// unused as the cleanest IR comes from having a well-constructed LLVM type |
| 54 | /// with proper GEP instructions, but sometimes its use is required, for |
| 55 | /// example if an access is intended to straddle an LLVM field boundary. |
| 56 | unsigned FieldByteOffset; |
| 57 | |
| 58 | /// Bit offset in the accessed value to use. The width is implied by \see |
| 59 | /// TargetBitWidth. |
| 60 | unsigned FieldBitStart; |
| 61 | |
| 62 | /// Bit width of the memory access to perform. |
| 63 | unsigned AccessWidth; |
| 64 | |
| 65 | /// The alignment of the memory access, or 0 if the default alignment should |
| 66 | /// be used. |
| 67 | // |
| 68 | // FIXME: Remove use of 0 to encode default, instead have IRgen do the right |
| 69 | // thing when it generates the code, if avoiding align directives is |
| 70 | // desired. |
| 71 | unsigned AccessAlignment; |
| 72 | |
| 73 | /// Offset for the target value. |
| 74 | unsigned TargetBitOffset; |
| 75 | |
| 76 | /// Number of bits in the access that are destined for the bit-field. |
| 77 | unsigned TargetBitWidth; |
| 78 | }; |
| 79 | |
| 80 | private: |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 81 | /// The components to use to access the bit-field. We may need up to three |
| 82 | /// separate components to support up to i64 bit-field access (4 + 2 + 1 byte |
| 83 | /// accesses). |
| 84 | // |
| 85 | // FIXME: De-hardcode this, just allocate following the struct. |
| 86 | AccessInfo Components[3]; |
| 87 | |
Daniel Dunbar | bb13845 | 2010-04-15 05:09:28 +0000 | [diff] [blame] | 88 | /// The total size of the bit-field, in bits. |
Daniel Dunbar | cd3d5e7 | 2010-04-05 16:20:44 +0000 | [diff] [blame] | 89 | unsigned Size; |
Daniel Dunbar | bb13845 | 2010-04-15 05:09:28 +0000 | [diff] [blame] | 90 | |
| 91 | /// The number of access components to use. |
| 92 | unsigned NumComponents; |
| 93 | |
| 94 | /// Whether the bit-field is signed. |
Daniel Dunbar | 196ea44 | 2010-04-06 01:07:44 +0000 | [diff] [blame] | 95 | bool IsSigned : 1; |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 96 | |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 97 | public: |
Daniel Dunbar | 9c78d63 | 2010-04-15 05:09:32 +0000 | [diff] [blame] | 98 | CGBitFieldInfo(unsigned Size, unsigned NumComponents, AccessInfo *_Components, |
| 99 | bool IsSigned) : Size(Size), NumComponents(NumComponents), |
| 100 | IsSigned(IsSigned) { |
| 101 | assert(NumComponents <= 3 && "invalid number of components!"); |
| 102 | for (unsigned i = 0; i != NumComponents; ++i) |
| 103 | Components[i] = _Components[i]; |
| 104 | |
| 105 | // Check some invariants. |
| 106 | unsigned AccessedSize = 0; |
| 107 | for (unsigned i = 0, e = getNumComponents(); i != e; ++i) { |
| 108 | const AccessInfo &AI = getComponent(i); |
| 109 | AccessedSize += AI.TargetBitWidth; |
| 110 | |
| 111 | // We shouldn't try to load 0 bits. |
| 112 | assert(AI.TargetBitWidth > 0); |
| 113 | |
| 114 | // We can't load more bits than we accessed. |
| 115 | assert(AI.FieldBitStart + AI.TargetBitWidth <= AI.AccessWidth); |
| 116 | |
| 117 | // We shouldn't put any bits outside the result size. |
| 118 | assert(AI.TargetBitWidth + AI.TargetBitOffset <= Size); |
| 119 | } |
| 120 | |
| 121 | // Check that the total number of target bits matches the total bit-field |
| 122 | // size. |
| 123 | assert(AccessedSize == Size && "Total size does not match accessed size!"); |
| 124 | } |
Daniel Dunbar | bb13845 | 2010-04-15 05:09:28 +0000 | [diff] [blame] | 125 | |
| 126 | public: |
Daniel Dunbar | b2b40a4 | 2010-04-14 04:07:59 +0000 | [diff] [blame] | 127 | /// \brief Check whether this bit-field access is (i.e., should be sign |
| 128 | /// extended on loads). |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 129 | bool isSigned() const { return IsSigned; } |
| 130 | |
Daniel Dunbar | b2b40a4 | 2010-04-14 04:07:59 +0000 | [diff] [blame] | 131 | /// \brief Get the size of the bit-field, in bits. |
| 132 | unsigned getSize() const { return Size; } |
| 133 | |
| 134 | /// @name Component Access |
| 135 | /// @{ |
| 136 | |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 137 | unsigned getNumComponents() const { return NumComponents; } |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 138 | |
| 139 | const AccessInfo &getComponent(unsigned Index) const { |
| 140 | assert(Index < getNumComponents() && "Invalid access!"); |
| 141 | return Components[Index]; |
| 142 | } |
Daniel Dunbar | b935b93 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 143 | |
Daniel Dunbar | b2b40a4 | 2010-04-14 04:07:59 +0000 | [diff] [blame] | 144 | /// @} |
| 145 | |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 146 | void print(llvm::raw_ostream &OS) const; |
| 147 | void dump() const; |
Daniel Dunbar | c7f9bba | 2010-09-02 23:53:28 +0000 | [diff] [blame] | 148 | |
| 149 | /// \brief Given a bit-field decl, build an appropriate helper object for |
| 150 | /// accessing that field (which is expected to have the given offset and |
| 151 | /// size). |
| 152 | static CGBitFieldInfo MakeInfo(class CodeGenTypes &Types, const FieldDecl *FD, |
| 153 | uint64_t FieldOffset, uint64_t FieldSize); |
| 154 | |
| 155 | /// \brief Given a bit-field decl, build an appropriate helper object for |
| 156 | /// accessing that field (which is expected to have the given offset and |
| 157 | /// size). The field decl should be known to be contained within a type of at |
| 158 | /// least the given size and with the given alignment. |
| 159 | static CGBitFieldInfo MakeInfo(CodeGenTypes &Types, const FieldDecl *FD, |
| 160 | uint64_t FieldOffset, uint64_t FieldSize, |
| 161 | uint64_t ContainingTypeSizeInBits, |
| 162 | unsigned ContainingTypeAlign); |
Daniel Dunbar | cd3d5e7 | 2010-04-05 16:20:44 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 165 | /// CGRecordLayout - This class handles struct and union layout info while |
| 166 | /// lowering AST types to LLVM types. |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 167 | /// |
| 168 | /// These layout objects are only created on demand as IR generation requires. |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 169 | class CGRecordLayout { |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 170 | friend class CodeGenTypes; |
| 171 | |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 172 | CGRecordLayout(const CGRecordLayout&); // DO NOT IMPLEMENT |
| 173 | void operator=(const CGRecordLayout&); // DO NOT IMPLEMENT |
| 174 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 175 | private: |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 176 | /// The LLVM type corresponding to this record layout. |
John McCall | 5a39bd2 | 2010-11-30 23:21:46 +0000 | [diff] [blame^] | 177 | llvm::PATypeHolder LLVMType; |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 178 | |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 179 | /// The LLVM type for the non-virtual part of this record layout, used for |
| 180 | /// laying out the record as a base. |
John McCall | 5a39bd2 | 2010-11-30 23:21:46 +0000 | [diff] [blame^] | 181 | llvm::PATypeHolder NonVirtualBaseLLVMType; |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 182 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 183 | /// Map from (non-bit-field) struct field to the corresponding llvm struct |
| 184 | /// type field no. This info is populated by record builder. |
| 185 | llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo; |
| 186 | |
| 187 | /// Map from (bit-field) struct field to the corresponding llvm struct type |
| 188 | /// field no. This info is populated by record builder. |
Daniel Dunbar | cd3d5e7 | 2010-04-05 16:20:44 +0000 | [diff] [blame] | 189 | llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields; |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 190 | |
Anders Carlsson | 061ca52 | 2010-05-18 05:22:06 +0000 | [diff] [blame] | 191 | // FIXME: Maybe we could use a CXXBaseSpecifier as the key and use a single |
| 192 | // map for both virtual and non virtual bases. |
| 193 | llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBaseFields; |
| 194 | |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 195 | /// Whether one of the fields in this record layout is a pointer to data |
| 196 | /// member, or a struct that contains pointer to data member. |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 197 | bool IsZeroInitializable : 1; |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 198 | |
| 199 | public: |
Anders Carlsson | 36e2fa8 | 2010-11-24 19:37:16 +0000 | [diff] [blame] | 200 | CGRecordLayout(const llvm::StructType *LLVMType, |
| 201 | const llvm::StructType *NonVirtualBaseLLVMType, |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 202 | bool IsZeroInitializable) |
Anders Carlsson | a7dd96c | 2010-11-21 23:59:45 +0000 | [diff] [blame] | 203 | : LLVMType(LLVMType), NonVirtualBaseLLVMType(NonVirtualBaseLLVMType), |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 204 | IsZeroInitializable(IsZeroInitializable) {} |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 205 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 206 | /// \brief Return the LLVM type associated with this record. |
Anders Carlsson | 36e2fa8 | 2010-11-24 19:37:16 +0000 | [diff] [blame] | 207 | const llvm::StructType *getLLVMType() const { |
John McCall | 5a39bd2 | 2010-11-30 23:21:46 +0000 | [diff] [blame^] | 208 | return cast<llvm::StructType>(LLVMType.get()); |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Anders Carlsson | 36e2fa8 | 2010-11-24 19:37:16 +0000 | [diff] [blame] | 211 | const llvm::StructType *getNonVirtualBaseLLVMType() const { |
John McCall | 5a39bd2 | 2010-11-30 23:21:46 +0000 | [diff] [blame^] | 212 | return cast<llvm::StructType>(NonVirtualBaseLLVMType.get()); |
Anders Carlsson | c1351ca | 2010-11-09 05:25:47 +0000 | [diff] [blame] | 213 | } |
| 214 | |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 215 | /// \brief Check whether this struct can be C++ zero-initialized |
| 216 | /// with a zeroinitializer. |
| 217 | bool isZeroInitializable() const { |
| 218 | return IsZeroInitializable; |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 219 | } |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 220 | |
Daniel Dunbar | 19d6355 | 2010-04-27 14:51:07 +0000 | [diff] [blame] | 221 | /// \brief Return llvm::StructType element number that corresponds to the |
| 222 | /// field FD. |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 223 | unsigned getLLVMFieldNo(const FieldDecl *FD) const { |
| 224 | assert(!FD->isBitField() && "Invalid call for bit-field decl!"); |
| 225 | assert(FieldInfo.count(FD) && "Invalid field for record!"); |
| 226 | return FieldInfo.lookup(FD); |
| 227 | } |
| 228 | |
Anders Carlsson | 061ca52 | 2010-05-18 05:22:06 +0000 | [diff] [blame] | 229 | unsigned getNonVirtualBaseLLVMFieldNo(const CXXRecordDecl *RD) const { |
| 230 | assert(NonVirtualBaseFields.count(RD) && "Invalid non-virtual base!"); |
| 231 | return NonVirtualBaseFields.lookup(RD); |
| 232 | } |
| 233 | |
Daniel Dunbar | 19d6355 | 2010-04-27 14:51:07 +0000 | [diff] [blame] | 234 | /// \brief Return the BitFieldInfo that corresponds to the field FD. |
Daniel Dunbar | cd3d5e7 | 2010-04-05 16:20:44 +0000 | [diff] [blame] | 235 | const CGBitFieldInfo &getBitFieldInfo(const FieldDecl *FD) const { |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 236 | assert(FD->isBitField() && "Invalid call for non bit-field decl!"); |
Daniel Dunbar | cd3d5e7 | 2010-04-05 16:20:44 +0000 | [diff] [blame] | 237 | llvm::DenseMap<const FieldDecl *, CGBitFieldInfo>::const_iterator |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 238 | it = BitFields.find(FD); |
| 239 | assert(it != BitFields.end() && "Unable to find bitfield info"); |
| 240 | return it->second; |
| 241 | } |
Daniel Dunbar | b97bff9 | 2010-04-12 18:14:18 +0000 | [diff] [blame] | 242 | |
| 243 | void print(llvm::raw_ostream &OS) const; |
| 244 | void dump() const; |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | } // end namespace CodeGen |
| 248 | } // end namespace clang |
| 249 | |
| 250 | #endif |