blob: 3db5e0483bab538646fd25732c5b3d0a825ceed6 [file] [log] [blame]
Daniel Dunbar072d0bb2010-03-30 22:26:10 +00001//===--- 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
Ken Dyckf76759c2011-04-24 10:04:59 +000013#include "clang/AST/CharUnits.h"
Daniel Dunbar034299e2010-03-31 01:09:11 +000014#include "clang/AST/Decl.h"
Chris Lattner95c664b2011-07-23 10:35:09 +000015#include "clang/Basic/LLVM.h"
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/DerivedTypes.h"
18
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000019namespace llvm {
Anders Carlsson36e2fa82010-11-24 19:37:16 +000020 class StructType;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000021}
22
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000023namespace clang {
24namespace CodeGen {
25
Daniel Dunbarb935b932010-04-13 20:58:55 +000026/// \brief Helper object for describing how to generate the code for access to a
Daniel Dunbarb97bff92010-04-12 18:14:18 +000027/// bit-field.
Daniel Dunbarb935b932010-04-13 20:58:55 +000028///
29/// This structure is intended to describe the "policy" of how the bit-field
30/// should be accessed, which may be target, language, or ABI dependent.
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000031class CGBitFieldInfo {
32public:
Daniel Dunbarb935b932010-04-13 20:58:55 +000033 /// Descriptor for a single component of a bit-field access. The entire
34 /// bit-field is constituted of a bitwise OR of all of the individual
35 /// components.
36 ///
37 /// Each component describes an accessed value, which is how the component
38 /// should be transferred to/from memory, and a target placement, which is how
39 /// that component fits into the constituted bit-field. The pseudo-IR for a
40 /// load is:
41 ///
42 /// %0 = gep %base, 0, FieldIndex
43 /// %1 = gep (i8*) %0, FieldByteOffset
44 /// %2 = (i(AccessWidth) *) %1
45 /// %3 = load %2, align AccessAlignment
46 /// %4 = shr %3, FieldBitStart
47 ///
48 /// and the composed bit-field is formed as the boolean OR of all accesses,
49 /// masked to TargetBitWidth bits and shifted to TargetBitOffset.
50 struct AccessInfo {
51 /// Offset of the field to load in the LLVM structure, if any.
52 unsigned FieldIndex;
53
54 /// Byte offset from the field address, if any. This should generally be
55 /// unused as the cleanest IR comes from having a well-constructed LLVM type
56 /// with proper GEP instructions, but sometimes its use is required, for
57 /// example if an access is intended to straddle an LLVM field boundary.
Ken Dyckf76759c2011-04-24 10:04:59 +000058 CharUnits FieldByteOffset;
Daniel Dunbarb935b932010-04-13 20:58:55 +000059
60 /// Bit offset in the accessed value to use. The width is implied by \see
61 /// TargetBitWidth.
62 unsigned FieldBitStart;
63
64 /// Bit width of the memory access to perform.
65 unsigned AccessWidth;
66
Eli Friedmanc24e2fb2012-06-27 21:19:48 +000067 /// The alignment of the memory access, assuming the parent is aligned.
Ken Dyck27337a82011-04-24 10:13:17 +000068 CharUnits AccessAlignment;
Daniel Dunbarb935b932010-04-13 20:58:55 +000069
70 /// Offset for the target value.
71 unsigned TargetBitOffset;
72
73 /// Number of bits in the access that are destined for the bit-field.
74 unsigned TargetBitWidth;
75 };
76
77private:
Daniel Dunbarb935b932010-04-13 20:58:55 +000078 /// The components to use to access the bit-field. We may need up to three
79 /// separate components to support up to i64 bit-field access (4 + 2 + 1 byte
80 /// accesses).
81 //
82 // FIXME: De-hardcode this, just allocate following the struct.
83 AccessInfo Components[3];
84
Daniel Dunbarbb138452010-04-15 05:09:28 +000085 /// The total size of the bit-field, in bits.
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000086 unsigned Size;
Daniel Dunbarbb138452010-04-15 05:09:28 +000087
88 /// The number of access components to use.
89 unsigned NumComponents;
90
91 /// Whether the bit-field is signed.
Daniel Dunbar196ea442010-04-06 01:07:44 +000092 bool IsSigned : 1;
Daniel Dunbarb97bff92010-04-12 18:14:18 +000093
Daniel Dunbarb935b932010-04-13 20:58:55 +000094public:
Daniel Dunbar9c78d632010-04-15 05:09:32 +000095 CGBitFieldInfo(unsigned Size, unsigned NumComponents, AccessInfo *_Components,
96 bool IsSigned) : Size(Size), NumComponents(NumComponents),
97 IsSigned(IsSigned) {
98 assert(NumComponents <= 3 && "invalid number of components!");
99 for (unsigned i = 0; i != NumComponents; ++i)
100 Components[i] = _Components[i];
101
102 // Check some invariants.
103 unsigned AccessedSize = 0;
104 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
105 const AccessInfo &AI = getComponent(i);
106 AccessedSize += AI.TargetBitWidth;
107
108 // We shouldn't try to load 0 bits.
109 assert(AI.TargetBitWidth > 0);
110
111 // We can't load more bits than we accessed.
112 assert(AI.FieldBitStart + AI.TargetBitWidth <= AI.AccessWidth);
113
114 // We shouldn't put any bits outside the result size.
115 assert(AI.TargetBitWidth + AI.TargetBitOffset <= Size);
116 }
117
118 // Check that the total number of target bits matches the total bit-field
119 // size.
120 assert(AccessedSize == Size && "Total size does not match accessed size!");
121 }
Daniel Dunbarbb138452010-04-15 05:09:28 +0000122
123public:
Daniel Dunbarb2b40a42010-04-14 04:07:59 +0000124 /// \brief Check whether this bit-field access is (i.e., should be sign
125 /// extended on loads).
Daniel Dunbarb935b932010-04-13 20:58:55 +0000126 bool isSigned() const { return IsSigned; }
127
Daniel Dunbarb2b40a42010-04-14 04:07:59 +0000128 /// \brief Get the size of the bit-field, in bits.
129 unsigned getSize() const { return Size; }
130
131 /// @name Component Access
132 /// @{
133
Daniel Dunbarb935b932010-04-13 20:58:55 +0000134 unsigned getNumComponents() const { return NumComponents; }
Daniel Dunbarb935b932010-04-13 20:58:55 +0000135
136 const AccessInfo &getComponent(unsigned Index) const {
137 assert(Index < getNumComponents() && "Invalid access!");
138 return Components[Index];
139 }
Daniel Dunbarb935b932010-04-13 20:58:55 +0000140
Daniel Dunbarb2b40a42010-04-14 04:07:59 +0000141 /// @}
142
Chris Lattner62ff6e82011-07-20 07:06:53 +0000143 void print(raw_ostream &OS) const;
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000144 void dump() const;
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000145
146 /// \brief Given a bit-field decl, build an appropriate helper object for
147 /// accessing that field (which is expected to have the given offset and
148 /// size).
149 static CGBitFieldInfo MakeInfo(class CodeGenTypes &Types, const FieldDecl *FD,
150 uint64_t FieldOffset, uint64_t FieldSize);
151
152 /// \brief Given a bit-field decl, build an appropriate helper object for
153 /// accessing that field (which is expected to have the given offset and
154 /// size). The field decl should be known to be contained within a type of at
155 /// least the given size and with the given alignment.
156 static CGBitFieldInfo MakeInfo(CodeGenTypes &Types, const FieldDecl *FD,
157 uint64_t FieldOffset, uint64_t FieldSize,
158 uint64_t ContainingTypeSizeInBits,
159 unsigned ContainingTypeAlign);
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +0000160};
161
Daniel Dunbar072d0bb2010-03-30 22:26:10 +0000162/// CGRecordLayout - This class handles struct and union layout info while
163/// lowering AST types to LLVM types.
Daniel Dunbar034299e2010-03-31 01:09:11 +0000164///
165/// These layout objects are only created on demand as IR generation requires.
Daniel Dunbar072d0bb2010-03-30 22:26:10 +0000166class CGRecordLayout {
Daniel Dunbar034299e2010-03-31 01:09:11 +0000167 friend class CodeGenTypes;
168
Dmitri Gribenkoa664e5b2012-09-15 20:20:27 +0000169 CGRecordLayout(const CGRecordLayout &) LLVM_DELETED_FUNCTION;
170 void operator=(const CGRecordLayout &) LLVM_DELETED_FUNCTION;
Daniel Dunbar072d0bb2010-03-30 22:26:10 +0000171
Daniel Dunbar034299e2010-03-31 01:09:11 +0000172private:
John McCall0217dfc22011-02-15 06:40:56 +0000173 /// The LLVM type corresponding to this record layout; used when
174 /// laying it out as a complete object.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000175 llvm::StructType *CompleteObjectType;
Daniel Dunbar072d0bb2010-03-30 22:26:10 +0000176
John McCall0217dfc22011-02-15 06:40:56 +0000177 /// The LLVM type for the non-virtual part of this record layout;
178 /// used when laying it out as a base subobject.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000179 llvm::StructType *BaseSubobjectType;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000180
Daniel Dunbar034299e2010-03-31 01:09:11 +0000181 /// Map from (non-bit-field) struct field to the corresponding llvm struct
182 /// type field no. This info is populated by record builder.
183 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
184
185 /// Map from (bit-field) struct field to the corresponding llvm struct type
186 /// field no. This info is populated by record builder.
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +0000187 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
Daniel Dunbar034299e2010-03-31 01:09:11 +0000188
Anders Carlsson061ca522010-05-18 05:22:06 +0000189 // FIXME: Maybe we could use a CXXBaseSpecifier as the key and use a single
190 // map for both virtual and non virtual bases.
John McCall0217dfc22011-02-15 06:40:56 +0000191 llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
Anders Carlsson061ca522010-05-18 05:22:06 +0000192
John McCall0217dfc22011-02-15 06:40:56 +0000193 /// Map from virtual bases to their field index in the complete object.
194 llvm::DenseMap<const CXXRecordDecl *, unsigned> CompleteObjectVirtualBases;
195
196 /// False if any direct or indirect subobject of this class, when
197 /// considered as a complete object, requires a non-zero bitpattern
198 /// when zero-initialized.
John McCall614dbdc2010-08-22 21:01:12 +0000199 bool IsZeroInitializable : 1;
Daniel Dunbar072d0bb2010-03-30 22:26:10 +0000200
John McCall0217dfc22011-02-15 06:40:56 +0000201 /// False if any direct or indirect subobject of this class, when
202 /// considered as a base subobject, requires a non-zero bitpattern
203 /// when zero-initialized.
204 bool IsZeroInitializableAsBase : 1;
Daniel Dunbar072d0bb2010-03-30 22:26:10 +0000205
John McCall0217dfc22011-02-15 06:40:56 +0000206public:
Chris Lattnera5f58b02011-07-09 17:41:47 +0000207 CGRecordLayout(llvm::StructType *CompleteObjectType,
208 llvm::StructType *BaseSubobjectType,
John McCall0217dfc22011-02-15 06:40:56 +0000209 bool IsZeroInitializable,
210 bool IsZeroInitializableAsBase)
211 : CompleteObjectType(CompleteObjectType),
212 BaseSubobjectType(BaseSubobjectType),
213 IsZeroInitializable(IsZeroInitializable),
214 IsZeroInitializableAsBase(IsZeroInitializableAsBase) {}
215
216 /// \brief Return the "complete object" LLVM type associated with
217 /// this record.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000218 llvm::StructType *getLLVMType() const {
219 return CompleteObjectType;
Daniel Dunbar072d0bb2010-03-30 22:26:10 +0000220 }
221
John McCall0217dfc22011-02-15 06:40:56 +0000222 /// \brief Return the "base subobject" LLVM type associated with
223 /// this record.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000224 llvm::StructType *getBaseSubobjectLLVMType() const {
225 return BaseSubobjectType;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000226 }
227
John McCall614dbdc2010-08-22 21:01:12 +0000228 /// \brief Check whether this struct can be C++ zero-initialized
229 /// with a zeroinitializer.
230 bool isZeroInitializable() const {
231 return IsZeroInitializable;
Daniel Dunbar072d0bb2010-03-30 22:26:10 +0000232 }
Daniel Dunbar034299e2010-03-31 01:09:11 +0000233
John McCall0217dfc22011-02-15 06:40:56 +0000234 /// \brief Check whether this struct can be C++ zero-initialized
235 /// with a zeroinitializer when considered as a base subobject.
236 bool isZeroInitializableAsBase() const {
237 return IsZeroInitializableAsBase;
238 }
239
Daniel Dunbar19d63552010-04-27 14:51:07 +0000240 /// \brief Return llvm::StructType element number that corresponds to the
241 /// field FD.
Daniel Dunbar034299e2010-03-31 01:09:11 +0000242 unsigned getLLVMFieldNo(const FieldDecl *FD) const {
243 assert(!FD->isBitField() && "Invalid call for bit-field decl!");
244 assert(FieldInfo.count(FD) && "Invalid field for record!");
245 return FieldInfo.lookup(FD);
246 }
247
Anders Carlsson061ca522010-05-18 05:22:06 +0000248 unsigned getNonVirtualBaseLLVMFieldNo(const CXXRecordDecl *RD) const {
John McCall0217dfc22011-02-15 06:40:56 +0000249 assert(NonVirtualBases.count(RD) && "Invalid non-virtual base!");
250 return NonVirtualBases.lookup(RD);
251 }
252
253 /// \brief Return the LLVM field index corresponding to the given
254 /// virtual base. Only valid when operating on the complete object.
255 unsigned getVirtualBaseIndex(const CXXRecordDecl *base) const {
256 assert(CompleteObjectVirtualBases.count(base) && "Invalid virtual base!");
257 return CompleteObjectVirtualBases.lookup(base);
Anders Carlsson061ca522010-05-18 05:22:06 +0000258 }
259
Daniel Dunbar19d63552010-04-27 14:51:07 +0000260 /// \brief Return the BitFieldInfo that corresponds to the field FD.
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +0000261 const CGBitFieldInfo &getBitFieldInfo(const FieldDecl *FD) const {
Daniel Dunbar034299e2010-03-31 01:09:11 +0000262 assert(FD->isBitField() && "Invalid call for non bit-field decl!");
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +0000263 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo>::const_iterator
Daniel Dunbar034299e2010-03-31 01:09:11 +0000264 it = BitFields.find(FD);
John McCall0217dfc22011-02-15 06:40:56 +0000265 assert(it != BitFields.end() && "Unable to find bitfield info");
Daniel Dunbar034299e2010-03-31 01:09:11 +0000266 return it->second;
267 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000268
Chris Lattner62ff6e82011-07-20 07:06:53 +0000269 void print(raw_ostream &OS) const;
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000270 void dump() const;
Daniel Dunbar072d0bb2010-03-30 22:26:10 +0000271};
272
273} // end namespace CodeGen
274} // end namespace clang
275
276#endif