blob: e2fd45dc86bec26d4bc4042a474b468be7064315 [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
Daniel Dunbar034299e2010-03-31 01:09:11 +000013#include "llvm/ADT/DenseMap.h"
John McCall5a39bd22010-11-30 23:21:46 +000014#include "llvm/DerivedTypes.h"
Ken Dyckf76759c2011-04-24 10:04:59 +000015#include "clang/AST/CharUnits.h"
Daniel Dunbar034299e2010-03-31 01:09:11 +000016#include "clang/AST/Decl.h"
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000017namespace llvm {
Daniel Dunbarb97bff92010-04-12 18:14:18 +000018 class raw_ostream;
Anders Carlsson36e2fa82010-11-24 19:37:16 +000019 class StructType;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000020}
21
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000022namespace clang {
23namespace CodeGen {
24
Daniel Dunbarb935b932010-04-13 20:58:55 +000025/// \brief Helper object for describing how to generate the code for access to a
Daniel Dunbarb97bff92010-04-12 18:14:18 +000026/// bit-field.
Daniel Dunbarb935b932010-04-13 20:58:55 +000027///
28/// This structure is intended to describe the "policy" of how the bit-field
29/// should be accessed, which may be target, language, or ABI dependent.
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000030class CGBitFieldInfo {
31public:
Daniel Dunbarb935b932010-04-13 20:58:55 +000032 /// Descriptor for a single component of a bit-field access. The entire
33 /// bit-field is constituted of a bitwise OR of all of the individual
34 /// components.
35 ///
36 /// Each component describes an accessed value, which is how the component
37 /// should be transferred to/from memory, and a target placement, which is how
38 /// that component fits into the constituted bit-field. The pseudo-IR for a
39 /// load is:
40 ///
41 /// %0 = gep %base, 0, FieldIndex
42 /// %1 = gep (i8*) %0, FieldByteOffset
43 /// %2 = (i(AccessWidth) *) %1
44 /// %3 = load %2, align AccessAlignment
45 /// %4 = shr %3, FieldBitStart
46 ///
47 /// and the composed bit-field is formed as the boolean OR of all accesses,
48 /// masked to TargetBitWidth bits and shifted to TargetBitOffset.
49 struct AccessInfo {
50 /// Offset of the field to load in the LLVM structure, if any.
51 unsigned FieldIndex;
52
53 /// Byte offset from the field address, if any. This should generally be
54 /// unused as the cleanest IR comes from having a well-constructed LLVM type
55 /// with proper GEP instructions, but sometimes its use is required, for
56 /// example if an access is intended to straddle an LLVM field boundary.
Ken Dyckf76759c2011-04-24 10:04:59 +000057 CharUnits FieldByteOffset;
Daniel Dunbarb935b932010-04-13 20:58:55 +000058
59 /// Bit offset in the accessed value to use. The width is implied by \see
60 /// TargetBitWidth.
61 unsigned FieldBitStart;
62
63 /// Bit width of the memory access to perform.
64 unsigned AccessWidth;
65
66 /// The alignment of the memory access, or 0 if the default alignment should
67 /// be used.
68 //
69 // FIXME: Remove use of 0 to encode default, instead have IRgen do the right
70 // thing when it generates the code, if avoiding align directives is
71 // desired.
Ken Dyck27337a82011-04-24 10:13:17 +000072 CharUnits AccessAlignment;
Daniel Dunbarb935b932010-04-13 20:58:55 +000073
74 /// Offset for the target value.
75 unsigned TargetBitOffset;
76
77 /// Number of bits in the access that are destined for the bit-field.
78 unsigned TargetBitWidth;
79 };
80
81private:
Daniel Dunbarb935b932010-04-13 20:58:55 +000082 /// The components to use to access the bit-field. We may need up to three
83 /// separate components to support up to i64 bit-field access (4 + 2 + 1 byte
84 /// accesses).
85 //
86 // FIXME: De-hardcode this, just allocate following the struct.
87 AccessInfo Components[3];
88
Daniel Dunbarbb138452010-04-15 05:09:28 +000089 /// The total size of the bit-field, in bits.
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000090 unsigned Size;
Daniel Dunbarbb138452010-04-15 05:09:28 +000091
92 /// The number of access components to use.
93 unsigned NumComponents;
94
95 /// Whether the bit-field is signed.
Daniel Dunbar196ea442010-04-06 01:07:44 +000096 bool IsSigned : 1;
Daniel Dunbarb97bff92010-04-12 18:14:18 +000097
Daniel Dunbarb935b932010-04-13 20:58:55 +000098public:
Daniel Dunbar9c78d632010-04-15 05:09:32 +000099 CGBitFieldInfo(unsigned Size, unsigned NumComponents, AccessInfo *_Components,
100 bool IsSigned) : Size(Size), NumComponents(NumComponents),
101 IsSigned(IsSigned) {
102 assert(NumComponents <= 3 && "invalid number of components!");
103 for (unsigned i = 0; i != NumComponents; ++i)
104 Components[i] = _Components[i];
105
106 // Check some invariants.
107 unsigned AccessedSize = 0;
108 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
109 const AccessInfo &AI = getComponent(i);
110 AccessedSize += AI.TargetBitWidth;
111
112 // We shouldn't try to load 0 bits.
113 assert(AI.TargetBitWidth > 0);
114
115 // We can't load more bits than we accessed.
116 assert(AI.FieldBitStart + AI.TargetBitWidth <= AI.AccessWidth);
117
118 // We shouldn't put any bits outside the result size.
119 assert(AI.TargetBitWidth + AI.TargetBitOffset <= Size);
120 }
121
122 // Check that the total number of target bits matches the total bit-field
123 // size.
124 assert(AccessedSize == Size && "Total size does not match accessed size!");
125 }
Daniel Dunbarbb138452010-04-15 05:09:28 +0000126
127public:
Daniel Dunbarb2b40a42010-04-14 04:07:59 +0000128 /// \brief Check whether this bit-field access is (i.e., should be sign
129 /// extended on loads).
Daniel Dunbarb935b932010-04-13 20:58:55 +0000130 bool isSigned() const { return IsSigned; }
131
Daniel Dunbarb2b40a42010-04-14 04:07:59 +0000132 /// \brief Get the size of the bit-field, in bits.
133 unsigned getSize() const { return Size; }
134
135 /// @name Component Access
136 /// @{
137
Daniel Dunbarb935b932010-04-13 20:58:55 +0000138 unsigned getNumComponents() const { return NumComponents; }
Daniel Dunbarb935b932010-04-13 20:58:55 +0000139
140 const AccessInfo &getComponent(unsigned Index) const {
141 assert(Index < getNumComponents() && "Invalid access!");
142 return Components[Index];
143 }
Daniel Dunbarb935b932010-04-13 20:58:55 +0000144
Daniel Dunbarb2b40a42010-04-14 04:07:59 +0000145 /// @}
146
Chris Lattner62ff6e82011-07-20 07:06:53 +0000147 void print(raw_ostream &OS) const;
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000148 void dump() const;
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000149
150 /// \brief Given a bit-field decl, build an appropriate helper object for
151 /// accessing that field (which is expected to have the given offset and
152 /// size).
153 static CGBitFieldInfo MakeInfo(class CodeGenTypes &Types, const FieldDecl *FD,
154 uint64_t FieldOffset, uint64_t FieldSize);
155
156 /// \brief Given a bit-field decl, build an appropriate helper object for
157 /// accessing that field (which is expected to have the given offset and
158 /// size). The field decl should be known to be contained within a type of at
159 /// least the given size and with the given alignment.
160 static CGBitFieldInfo MakeInfo(CodeGenTypes &Types, const FieldDecl *FD,
161 uint64_t FieldOffset, uint64_t FieldSize,
162 uint64_t ContainingTypeSizeInBits,
163 unsigned ContainingTypeAlign);
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +0000164};
165
Daniel Dunbar072d0bb2010-03-30 22:26:10 +0000166/// CGRecordLayout - This class handles struct and union layout info while
167/// lowering AST types to LLVM types.
Daniel Dunbar034299e2010-03-31 01:09:11 +0000168///
169/// These layout objects are only created on demand as IR generation requires.
Daniel Dunbar072d0bb2010-03-30 22:26:10 +0000170class CGRecordLayout {
Daniel Dunbar034299e2010-03-31 01:09:11 +0000171 friend class CodeGenTypes;
172
Daniel Dunbar072d0bb2010-03-30 22:26:10 +0000173 CGRecordLayout(const CGRecordLayout&); // DO NOT IMPLEMENT
174 void operator=(const CGRecordLayout&); // DO NOT IMPLEMENT
175
Daniel Dunbar034299e2010-03-31 01:09:11 +0000176private:
John McCall0217dfc22011-02-15 06:40:56 +0000177 /// The LLVM type corresponding to this record layout; used when
178 /// laying it out as a complete object.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000179 llvm::StructType *CompleteObjectType;
Daniel Dunbar072d0bb2010-03-30 22:26:10 +0000180
John McCall0217dfc22011-02-15 06:40:56 +0000181 /// The LLVM type for the non-virtual part of this record layout;
182 /// used when laying it out as a base subobject.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000183 llvm::StructType *BaseSubobjectType;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000184
Daniel Dunbar034299e2010-03-31 01:09:11 +0000185 /// Map from (non-bit-field) struct field to the corresponding llvm struct
186 /// type field no. This info is populated by record builder.
187 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
188
189 /// Map from (bit-field) struct field to the corresponding llvm struct type
190 /// field no. This info is populated by record builder.
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +0000191 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
Daniel Dunbar034299e2010-03-31 01:09:11 +0000192
Anders Carlsson061ca522010-05-18 05:22:06 +0000193 // FIXME: Maybe we could use a CXXBaseSpecifier as the key and use a single
194 // map for both virtual and non virtual bases.
John McCall0217dfc22011-02-15 06:40:56 +0000195 llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
Anders Carlsson061ca522010-05-18 05:22:06 +0000196
John McCall0217dfc22011-02-15 06:40:56 +0000197 /// Map from virtual bases to their field index in the complete object.
198 llvm::DenseMap<const CXXRecordDecl *, unsigned> CompleteObjectVirtualBases;
199
200 /// False if any direct or indirect subobject of this class, when
201 /// considered as a complete object, requires a non-zero bitpattern
202 /// when zero-initialized.
John McCall614dbdc2010-08-22 21:01:12 +0000203 bool IsZeroInitializable : 1;
Daniel Dunbar072d0bb2010-03-30 22:26:10 +0000204
John McCall0217dfc22011-02-15 06:40:56 +0000205 /// False if any direct or indirect subobject of this class, when
206 /// considered as a base subobject, requires a non-zero bitpattern
207 /// when zero-initialized.
208 bool IsZeroInitializableAsBase : 1;
Daniel Dunbar072d0bb2010-03-30 22:26:10 +0000209
John McCall0217dfc22011-02-15 06:40:56 +0000210public:
Chris Lattnera5f58b02011-07-09 17:41:47 +0000211 CGRecordLayout(llvm::StructType *CompleteObjectType,
212 llvm::StructType *BaseSubobjectType,
John McCall0217dfc22011-02-15 06:40:56 +0000213 bool IsZeroInitializable,
214 bool IsZeroInitializableAsBase)
215 : CompleteObjectType(CompleteObjectType),
216 BaseSubobjectType(BaseSubobjectType),
217 IsZeroInitializable(IsZeroInitializable),
218 IsZeroInitializableAsBase(IsZeroInitializableAsBase) {}
219
220 /// \brief Return the "complete object" LLVM type associated with
221 /// this record.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000222 llvm::StructType *getLLVMType() const {
223 return CompleteObjectType;
Daniel Dunbar072d0bb2010-03-30 22:26:10 +0000224 }
225
John McCall0217dfc22011-02-15 06:40:56 +0000226 /// \brief Return the "base subobject" LLVM type associated with
227 /// this record.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000228 llvm::StructType *getBaseSubobjectLLVMType() const {
229 return BaseSubobjectType;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000230 }
231
John McCall614dbdc2010-08-22 21:01:12 +0000232 /// \brief Check whether this struct can be C++ zero-initialized
233 /// with a zeroinitializer.
234 bool isZeroInitializable() const {
235 return IsZeroInitializable;
Daniel Dunbar072d0bb2010-03-30 22:26:10 +0000236 }
Daniel Dunbar034299e2010-03-31 01:09:11 +0000237
John McCall0217dfc22011-02-15 06:40:56 +0000238 /// \brief Check whether this struct can be C++ zero-initialized
239 /// with a zeroinitializer when considered as a base subobject.
240 bool isZeroInitializableAsBase() const {
241 return IsZeroInitializableAsBase;
242 }
243
Daniel Dunbar19d63552010-04-27 14:51:07 +0000244 /// \brief Return llvm::StructType element number that corresponds to the
245 /// field FD.
Daniel Dunbar034299e2010-03-31 01:09:11 +0000246 unsigned getLLVMFieldNo(const FieldDecl *FD) const {
247 assert(!FD->isBitField() && "Invalid call for bit-field decl!");
248 assert(FieldInfo.count(FD) && "Invalid field for record!");
249 return FieldInfo.lookup(FD);
250 }
251
Anders Carlsson061ca522010-05-18 05:22:06 +0000252 unsigned getNonVirtualBaseLLVMFieldNo(const CXXRecordDecl *RD) const {
John McCall0217dfc22011-02-15 06:40:56 +0000253 assert(NonVirtualBases.count(RD) && "Invalid non-virtual base!");
254 return NonVirtualBases.lookup(RD);
255 }
256
257 /// \brief Return the LLVM field index corresponding to the given
258 /// virtual base. Only valid when operating on the complete object.
259 unsigned getVirtualBaseIndex(const CXXRecordDecl *base) const {
260 assert(CompleteObjectVirtualBases.count(base) && "Invalid virtual base!");
261 return CompleteObjectVirtualBases.lookup(base);
Anders Carlsson061ca522010-05-18 05:22:06 +0000262 }
263
Daniel Dunbar19d63552010-04-27 14:51:07 +0000264 /// \brief Return the BitFieldInfo that corresponds to the field FD.
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +0000265 const CGBitFieldInfo &getBitFieldInfo(const FieldDecl *FD) const {
Daniel Dunbar034299e2010-03-31 01:09:11 +0000266 assert(FD->isBitField() && "Invalid call for non bit-field decl!");
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +0000267 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo>::const_iterator
Daniel Dunbar034299e2010-03-31 01:09:11 +0000268 it = BitFields.find(FD);
John McCall0217dfc22011-02-15 06:40:56 +0000269 assert(it != BitFields.end() && "Unable to find bitfield info");
Daniel Dunbar034299e2010-03-31 01:09:11 +0000270 return it->second;
271 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000272
Chris Lattner62ff6e82011-07-20 07:06:53 +0000273 void print(raw_ostream &OS) const;
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000274 void dump() const;
Daniel Dunbar072d0bb2010-03-30 22:26:10 +0000275};
276
277} // end namespace CodeGen
278} // end namespace clang
279
280#endif