blob: 2de0b2f87fc153a8f6946e2e53a1f00f5e4c3942 [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
Stephen Hines176edba2014-12-01 14:53:08 -080010#ifndef LLVM_CLANG_LIB_CODEGEN_CGRECORDLAYOUT_H
11#define LLVM_CLANG_LIB_CODEGEN_CGRECORDLAYOUT_H
Daniel Dunbar2924ade2010-03-30 22:26:10 +000012
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"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000017#include "llvm/IR/DerivedTypes.h"
Chris Lattnerd47d3b02011-07-23 10:35:09 +000018
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
Chandler Carruth72d2dab2012-12-06 11:14:44 +000026/// \brief Structure with information about how a bitfield should be accessed.
Daniel Dunbarab970f92010-04-13 20:58:55 +000027///
Chandler Carruth72d2dab2012-12-06 11:14:44 +000028/// 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///
66struct 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 Dunbarab970f92010-04-13 20:58:55 +000070
Daniel Dunbar7fb61952010-04-15 05:09:28 +000071 /// The total size of the bit-field, in bits.
Chandler Carruth72d2dab2012-12-06 11:14:44 +000072 unsigned Size : 15;
Daniel Dunbar7fb61952010-04-15 05:09:28 +000073
74 /// Whether the bit-field is signed.
Chandler Carruth72d2dab2012-12-06 11:14:44 +000075 unsigned IsSigned : 1;
Daniel Dunbar93c62962010-04-12 18:14:18 +000076
Chandler Carruth72d2dab2012-12-06 11:14:44 +000077 /// The storage size in bits which should be used when accessing this
78 /// bitfield.
79 unsigned StorageSize;
Daniel Dunbar2df25692010-04-15 05:09:32 +000080
Chandler Carruth72d2dab2012-12-06 11:14:44 +000081 /// The alignment which should be used when accessing the bitfield.
82 unsigned StorageAlignment;
Daniel Dunbar2df25692010-04-15 05:09:32 +000083
Chandler Carruth72d2dab2012-12-06 11:14:44 +000084 CGBitFieldInfo()
85 : Offset(), Size(), IsSigned(), StorageSize(), StorageAlignment() {}
Daniel Dunbar2df25692010-04-15 05:09:32 +000086
Chandler Carruth72d2dab2012-12-06 11:14:44 +000087 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 Dunbare7925842010-04-14 04:07:59 +000091
Chris Lattner8cc488f2011-07-20 07:06:53 +000092 void print(raw_ostream &OS) const;
Daniel Dunbar93c62962010-04-12 18:14:18 +000093 void dump() const;
Daniel Dunbare7a80bd2010-09-02 23:53:28 +000094
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 Carruth72d2dab2012-12-06 11:14:44 +000098 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 Dunbar2eec0b22010-04-05 16:20:44 +0000103};
104
Daniel Dunbar2924ade2010-03-30 22:26:10 +0000105/// CGRecordLayout - This class handles struct and union layout info while
106/// lowering AST types to LLVM types.
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000107///
108/// These layout objects are only created on demand as IR generation requires.
Daniel Dunbar2924ade2010-03-30 22:26:10 +0000109class CGRecordLayout {
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000110 friend class CodeGenTypes;
111
Dmitri Gribenkof56faa02012-09-15 20:20:27 +0000112 CGRecordLayout(const CGRecordLayout &) LLVM_DELETED_FUNCTION;
113 void operator=(const CGRecordLayout &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2924ade2010-03-30 22:26:10 +0000114
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000115private:
John McCall9b7da1c2011-02-15 06:40:56 +0000116 /// The LLVM type corresponding to this record layout; used when
117 /// laying it out as a complete object.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000118 llvm::StructType *CompleteObjectType;
Daniel Dunbar2924ade2010-03-30 22:26:10 +0000119
John McCall9b7da1c2011-02-15 06:40:56 +0000120 /// The LLVM type for the non-virtual part of this record layout;
121 /// used when laying it out as a base subobject.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000122 llvm::StructType *BaseSubobjectType;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000123
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000124 /// 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 Dunbar2eec0b22010-04-05 16:20:44 +0000130 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000131
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000132 // FIXME: Maybe we could use a CXXBaseSpecifier as the key and use a single
Stephen Hines651f13c2014-04-23 16:59:28 -0700133 // map for both virtual and non-virtual bases.
John McCall9b7da1c2011-02-15 06:40:56 +0000134 llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000135
John McCall9b7da1c2011-02-15 06:40:56 +0000136 /// 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 McCallf16aa102010-08-22 21:01:12 +0000142 bool IsZeroInitializable : 1;
Daniel Dunbar2924ade2010-03-30 22:26:10 +0000143
John McCall9b7da1c2011-02-15 06:40:56 +0000144 /// 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 Dunbar2924ade2010-03-30 22:26:10 +0000148
John McCall9b7da1c2011-02-15 06:40:56 +0000149public:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000150 CGRecordLayout(llvm::StructType *CompleteObjectType,
151 llvm::StructType *BaseSubobjectType,
John McCall9b7da1c2011-02-15 06:40:56 +0000152 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 Lattner9cbe4f02011-07-09 17:41:47 +0000161 llvm::StructType *getLLVMType() const {
162 return CompleteObjectType;
Daniel Dunbar2924ade2010-03-30 22:26:10 +0000163 }
164
John McCall9b7da1c2011-02-15 06:40:56 +0000165 /// \brief Return the "base subobject" LLVM type associated with
166 /// this record.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000167 llvm::StructType *getBaseSubobjectLLVMType() const {
168 return BaseSubobjectType;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000169 }
170
John McCallf16aa102010-08-22 21:01:12 +0000171 /// \brief Check whether this struct can be C++ zero-initialized
172 /// with a zeroinitializer.
173 bool isZeroInitializable() const {
174 return IsZeroInitializable;
Daniel Dunbar2924ade2010-03-30 22:26:10 +0000175 }
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000176
John McCall9b7da1c2011-02-15 06:40:56 +0000177 /// \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 Dunbar50810d32010-04-27 14:51:07 +0000183 /// \brief Return llvm::StructType element number that corresponds to the
184 /// field FD.
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000185 unsigned getLLVMFieldNo(const FieldDecl *FD) const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700186 FD = FD->getCanonicalDecl();
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000187 assert(FieldInfo.count(FD) && "Invalid field for record!");
188 return FieldInfo.lookup(FD);
189 }
190
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000191 unsigned getNonVirtualBaseLLVMFieldNo(const CXXRecordDecl *RD) const {
John McCall9b7da1c2011-02-15 06:40:56 +0000192 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 Carlssonc6772ce2010-05-18 05:22:06 +0000201 }
202
Daniel Dunbar50810d32010-04-27 14:51:07 +0000203 /// \brief Return the BitFieldInfo that corresponds to the field FD.
Daniel Dunbar2eec0b22010-04-05 16:20:44 +0000204 const CGBitFieldInfo &getBitFieldInfo(const FieldDecl *FD) const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700205 FD = FD->getCanonicalDecl();
Stephen Hines651f13c2014-04-23 16:59:28 -0700206 assert(FD->isBitField() && "Invalid call for non-bit-field decl!");
Daniel Dunbar2eec0b22010-04-05 16:20:44 +0000207 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo>::const_iterator
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000208 it = BitFields.find(FD);
John McCall9b7da1c2011-02-15 06:40:56 +0000209 assert(it != BitFields.end() && "Unable to find bitfield info");
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000210 return it->second;
211 }
Daniel Dunbar93c62962010-04-12 18:14:18 +0000212
Chris Lattner8cc488f2011-07-20 07:06:53 +0000213 void print(raw_ostream &OS) const;
Daniel Dunbar93c62962010-04-12 18:14:18 +0000214 void dump() const;
Daniel Dunbar2924ade2010-03-30 22:26:10 +0000215};
216
217} // end namespace CodeGen
218} // end namespace clang
219
220#endif