blob: 25a0a508f188f3d66051b68db568e6527fb2abd3 [file] [log] [blame]
Daniel Dunbar2924ade2010-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 Dyck28ebde52011-04-24 10:04:59 +000013#include "clang/AST/CharUnits.h"
Daniel Dunbar198bcb42010-03-31 01:09:11 +000014#include "clang/AST/Decl.h"
Chris Lattnerd47d3b02011-07-23 10:35:09 +000015#include "clang/Basic/LLVM.h"
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/DerivedTypes.h"
18
Daniel Dunbar270e2032010-03-31 00:11:27 +000019namespace llvm {
Anders Carlssonba2c2ee2010-11-24 19:37:16 +000020 class StructType;
Daniel Dunbar270e2032010-03-31 00:11:27 +000021}
22
Daniel Dunbar2924ade2010-03-30 22:26:10 +000023namespace clang {
24namespace CodeGen {
25
Daniel Dunbarab970f92010-04-13 20:58:55 +000026/// \brief Helper object for describing how to generate the code for access to a
Daniel Dunbar93c62962010-04-12 18:14:18 +000027/// bit-field.
Daniel Dunbarab970f92010-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 Dunbar2eec0b22010-04-05 16:20:44 +000031class CGBitFieldInfo {
32public:
Daniel Dunbarab970f92010-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 Dyck28ebde52011-04-24 10:04:59 +000058 CharUnits FieldByteOffset;
Daniel Dunbarab970f92010-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
67 /// The alignment of the memory access, or 0 if the default alignment should
68 /// be used.
69 //
70 // FIXME: Remove use of 0 to encode default, instead have IRgen do the right
71 // thing when it generates the code, if avoiding align directives is
72 // desired.
Ken Dyckb9e6b2c2011-04-24 10:13:17 +000073 CharUnits AccessAlignment;
Daniel Dunbarab970f92010-04-13 20:58:55 +000074
75 /// Offset for the target value.
76 unsigned TargetBitOffset;
77
78 /// Number of bits in the access that are destined for the bit-field.
79 unsigned TargetBitWidth;
80 };
81
82private:
Daniel Dunbarab970f92010-04-13 20:58:55 +000083 /// The components to use to access the bit-field. We may need up to three
84 /// separate components to support up to i64 bit-field access (4 + 2 + 1 byte
85 /// accesses).
86 //
87 // FIXME: De-hardcode this, just allocate following the struct.
88 AccessInfo Components[3];
89
Daniel Dunbar7fb61952010-04-15 05:09:28 +000090 /// The total size of the bit-field, in bits.
Daniel Dunbar2eec0b22010-04-05 16:20:44 +000091 unsigned Size;
Daniel Dunbar7fb61952010-04-15 05:09:28 +000092
93 /// The number of access components to use.
94 unsigned NumComponents;
95
96 /// Whether the bit-field is signed.
Daniel Dunbarefbf4872010-04-06 01:07:44 +000097 bool IsSigned : 1;
Daniel Dunbar93c62962010-04-12 18:14:18 +000098
Daniel Dunbarab970f92010-04-13 20:58:55 +000099public:
Daniel Dunbar2df25692010-04-15 05:09:32 +0000100 CGBitFieldInfo(unsigned Size, unsigned NumComponents, AccessInfo *_Components,
101 bool IsSigned) : Size(Size), NumComponents(NumComponents),
102 IsSigned(IsSigned) {
103 assert(NumComponents <= 3 && "invalid number of components!");
104 for (unsigned i = 0; i != NumComponents; ++i)
105 Components[i] = _Components[i];
106
107 // Check some invariants.
108 unsigned AccessedSize = 0;
109 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
110 const AccessInfo &AI = getComponent(i);
111 AccessedSize += AI.TargetBitWidth;
112
113 // We shouldn't try to load 0 bits.
114 assert(AI.TargetBitWidth > 0);
115
116 // We can't load more bits than we accessed.
117 assert(AI.FieldBitStart + AI.TargetBitWidth <= AI.AccessWidth);
118
119 // We shouldn't put any bits outside the result size.
120 assert(AI.TargetBitWidth + AI.TargetBitOffset <= Size);
121 }
122
123 // Check that the total number of target bits matches the total bit-field
124 // size.
125 assert(AccessedSize == Size && "Total size does not match accessed size!");
126 }
Daniel Dunbar7fb61952010-04-15 05:09:28 +0000127
128public:
Daniel Dunbare7925842010-04-14 04:07:59 +0000129 /// \brief Check whether this bit-field access is (i.e., should be sign
130 /// extended on loads).
Daniel Dunbarab970f92010-04-13 20:58:55 +0000131 bool isSigned() const { return IsSigned; }
132
Daniel Dunbare7925842010-04-14 04:07:59 +0000133 /// \brief Get the size of the bit-field, in bits.
134 unsigned getSize() const { return Size; }
135
136 /// @name Component Access
137 /// @{
138
Daniel Dunbarab970f92010-04-13 20:58:55 +0000139 unsigned getNumComponents() const { return NumComponents; }
Daniel Dunbarab970f92010-04-13 20:58:55 +0000140
141 const AccessInfo &getComponent(unsigned Index) const {
142 assert(Index < getNumComponents() && "Invalid access!");
143 return Components[Index];
144 }
Daniel Dunbarab970f92010-04-13 20:58:55 +0000145
Daniel Dunbare7925842010-04-14 04:07:59 +0000146 /// @}
147
Chris Lattner8cc488f2011-07-20 07:06:53 +0000148 void print(raw_ostream &OS) const;
Daniel Dunbar93c62962010-04-12 18:14:18 +0000149 void dump() const;
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000150
151 /// \brief Given a bit-field decl, build an appropriate helper object for
152 /// accessing that field (which is expected to have the given offset and
153 /// size).
154 static CGBitFieldInfo MakeInfo(class CodeGenTypes &Types, const FieldDecl *FD,
155 uint64_t FieldOffset, uint64_t FieldSize);
156
157 /// \brief Given a bit-field decl, build an appropriate helper object for
158 /// accessing that field (which is expected to have the given offset and
159 /// size). The field decl should be known to be contained within a type of at
160 /// least the given size and with the given alignment.
161 static CGBitFieldInfo MakeInfo(CodeGenTypes &Types, const FieldDecl *FD,
162 uint64_t FieldOffset, uint64_t FieldSize,
163 uint64_t ContainingTypeSizeInBits,
164 unsigned ContainingTypeAlign);
Daniel Dunbar2eec0b22010-04-05 16:20:44 +0000165};
166
Daniel Dunbar2924ade2010-03-30 22:26:10 +0000167/// CGRecordLayout - This class handles struct and union layout info while
168/// lowering AST types to LLVM types.
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000169///
170/// These layout objects are only created on demand as IR generation requires.
Daniel Dunbar2924ade2010-03-30 22:26:10 +0000171class CGRecordLayout {
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000172 friend class CodeGenTypes;
173
Daniel Dunbar2924ade2010-03-30 22:26:10 +0000174 CGRecordLayout(const CGRecordLayout&); // DO NOT IMPLEMENT
175 void operator=(const CGRecordLayout&); // DO NOT IMPLEMENT
176
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000177private:
John McCall9b7da1c2011-02-15 06:40:56 +0000178 /// The LLVM type corresponding to this record layout; used when
179 /// laying it out as a complete object.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000180 llvm::StructType *CompleteObjectType;
Daniel Dunbar2924ade2010-03-30 22:26:10 +0000181
John McCall9b7da1c2011-02-15 06:40:56 +0000182 /// The LLVM type for the non-virtual part of this record layout;
183 /// used when laying it out as a base subobject.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000184 llvm::StructType *BaseSubobjectType;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000185
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000186 /// Map from (non-bit-field) struct field to the corresponding llvm struct
187 /// type field no. This info is populated by record builder.
188 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
189
190 /// Map from (bit-field) struct field to the corresponding llvm struct type
191 /// field no. This info is populated by record builder.
Daniel Dunbar2eec0b22010-04-05 16:20:44 +0000192 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000193
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000194 // FIXME: Maybe we could use a CXXBaseSpecifier as the key and use a single
195 // map for both virtual and non virtual bases.
John McCall9b7da1c2011-02-15 06:40:56 +0000196 llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000197
John McCall9b7da1c2011-02-15 06:40:56 +0000198 /// Map from virtual bases to their field index in the complete object.
199 llvm::DenseMap<const CXXRecordDecl *, unsigned> CompleteObjectVirtualBases;
200
201 /// False if any direct or indirect subobject of this class, when
202 /// considered as a complete object, requires a non-zero bitpattern
203 /// when zero-initialized.
John McCallf16aa102010-08-22 21:01:12 +0000204 bool IsZeroInitializable : 1;
Daniel Dunbar2924ade2010-03-30 22:26:10 +0000205
John McCall9b7da1c2011-02-15 06:40:56 +0000206 /// False if any direct or indirect subobject of this class, when
207 /// considered as a base subobject, requires a non-zero bitpattern
208 /// when zero-initialized.
209 bool IsZeroInitializableAsBase : 1;
Daniel Dunbar2924ade2010-03-30 22:26:10 +0000210
John McCall9b7da1c2011-02-15 06:40:56 +0000211public:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000212 CGRecordLayout(llvm::StructType *CompleteObjectType,
213 llvm::StructType *BaseSubobjectType,
John McCall9b7da1c2011-02-15 06:40:56 +0000214 bool IsZeroInitializable,
215 bool IsZeroInitializableAsBase)
216 : CompleteObjectType(CompleteObjectType),
217 BaseSubobjectType(BaseSubobjectType),
218 IsZeroInitializable(IsZeroInitializable),
219 IsZeroInitializableAsBase(IsZeroInitializableAsBase) {}
220
221 /// \brief Return the "complete object" LLVM type associated with
222 /// this record.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000223 llvm::StructType *getLLVMType() const {
224 return CompleteObjectType;
Daniel Dunbar2924ade2010-03-30 22:26:10 +0000225 }
226
John McCall9b7da1c2011-02-15 06:40:56 +0000227 /// \brief Return the "base subobject" LLVM type associated with
228 /// this record.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000229 llvm::StructType *getBaseSubobjectLLVMType() const {
230 return BaseSubobjectType;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000231 }
232
John McCallf16aa102010-08-22 21:01:12 +0000233 /// \brief Check whether this struct can be C++ zero-initialized
234 /// with a zeroinitializer.
235 bool isZeroInitializable() const {
236 return IsZeroInitializable;
Daniel Dunbar2924ade2010-03-30 22:26:10 +0000237 }
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000238
John McCall9b7da1c2011-02-15 06:40:56 +0000239 /// \brief Check whether this struct can be C++ zero-initialized
240 /// with a zeroinitializer when considered as a base subobject.
241 bool isZeroInitializableAsBase() const {
242 return IsZeroInitializableAsBase;
243 }
244
Daniel Dunbar50810d32010-04-27 14:51:07 +0000245 /// \brief Return llvm::StructType element number that corresponds to the
246 /// field FD.
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000247 unsigned getLLVMFieldNo(const FieldDecl *FD) const {
248 assert(!FD->isBitField() && "Invalid call for bit-field decl!");
249 assert(FieldInfo.count(FD) && "Invalid field for record!");
250 return FieldInfo.lookup(FD);
251 }
252
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000253 unsigned getNonVirtualBaseLLVMFieldNo(const CXXRecordDecl *RD) const {
John McCall9b7da1c2011-02-15 06:40:56 +0000254 assert(NonVirtualBases.count(RD) && "Invalid non-virtual base!");
255 return NonVirtualBases.lookup(RD);
256 }
257
258 /// \brief Return the LLVM field index corresponding to the given
259 /// virtual base. Only valid when operating on the complete object.
260 unsigned getVirtualBaseIndex(const CXXRecordDecl *base) const {
261 assert(CompleteObjectVirtualBases.count(base) && "Invalid virtual base!");
262 return CompleteObjectVirtualBases.lookup(base);
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000263 }
264
Daniel Dunbar50810d32010-04-27 14:51:07 +0000265 /// \brief Return the BitFieldInfo that corresponds to the field FD.
Daniel Dunbar2eec0b22010-04-05 16:20:44 +0000266 const CGBitFieldInfo &getBitFieldInfo(const FieldDecl *FD) const {
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000267 assert(FD->isBitField() && "Invalid call for non bit-field decl!");
Daniel Dunbar2eec0b22010-04-05 16:20:44 +0000268 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo>::const_iterator
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000269 it = BitFields.find(FD);
John McCall9b7da1c2011-02-15 06:40:56 +0000270 assert(it != BitFields.end() && "Unable to find bitfield info");
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000271 return it->second;
272 }
Daniel Dunbar93c62962010-04-12 18:14:18 +0000273
Chris Lattner8cc488f2011-07-20 07:06:53 +0000274 void print(raw_ostream &OS) const;
Daniel Dunbar93c62962010-04-12 18:14:18 +0000275 void dump() const;
Daniel Dunbar2924ade2010-03-30 22:26:10 +0000276};
277
278} // end namespace CodeGen
279} // end namespace clang
280
281#endif