CGRecordLayout.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //===--- CGRecordLayout.h - LLVM Record Layout Information ------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLVM_CLANG_LIB_CODEGEN_CGRECORDLAYOUT_H
  9. #define LLVM_CLANG_LIB_CODEGEN_CGRECORDLAYOUT_H
  10. #include "clang/AST/CharUnits.h"
  11. #include "clang/AST/DeclCXX.h"
  12. #include "clang/Basic/LLVM.h"
  13. #include "llvm/ADT/DenseMap.h"
  14. #include "llvm/IR/DerivedTypes.h"
  15. namespace llvm {
  16. class StructType;
  17. }
  18. namespace clang {
  19. namespace CodeGen {
  20. /// Structure with information about how a bitfield should be accessed.
  21. ///
  22. /// Often we layout a sequence of bitfields as a contiguous sequence of bits.
  23. /// When the AST record layout does this, we represent it in the LLVM IR's type
  24. /// as either a sequence of i8 members or a byte array to reserve the number of
  25. /// bytes touched without forcing any particular alignment beyond the basic
  26. /// character alignment.
  27. ///
  28. /// Then accessing a particular bitfield involves converting this byte array
  29. /// into a single integer of that size (i24 or i40 -- may not be power-of-two
  30. /// size), loading it, and shifting and masking to extract the particular
  31. /// subsequence of bits which make up that particular bitfield. This structure
  32. /// encodes the information used to construct the extraction code sequences.
  33. /// The CGRecordLayout also has a field index which encodes which byte-sequence
  34. /// this bitfield falls within. Let's assume the following C struct:
  35. ///
  36. /// struct S {
  37. /// char a, b, c;
  38. /// unsigned bits : 3;
  39. /// unsigned more_bits : 4;
  40. /// unsigned still_more_bits : 7;
  41. /// };
  42. ///
  43. /// This will end up as the following LLVM type. The first array is the
  44. /// bitfield, and the second is the padding out to a 4-byte alignment.
  45. ///
  46. /// %t = type { i8, i8, i8, i8, i8, [3 x i8] }
  47. ///
  48. /// When generating code to access more_bits, we'll generate something
  49. /// essentially like this:
  50. ///
  51. /// define i32 @foo(%t* %base) {
  52. /// %0 = gep %t* %base, i32 0, i32 3
  53. /// %2 = load i8* %1
  54. /// %3 = lshr i8 %2, 3
  55. /// %4 = and i8 %3, 15
  56. /// %5 = zext i8 %4 to i32
  57. /// ret i32 %i
  58. /// }
  59. ///
  60. struct CGBitFieldInfo {
  61. /// The offset within a contiguous run of bitfields that are represented as
  62. /// a single "field" within the LLVM struct type. This offset is in bits.
  63. unsigned Offset : 16;
  64. /// The total size of the bit-field, in bits.
  65. unsigned Size : 15;
  66. /// Whether the bit-field is signed.
  67. unsigned IsSigned : 1;
  68. /// The storage size in bits which should be used when accessing this
  69. /// bitfield.
  70. unsigned StorageSize;
  71. /// The offset of the bitfield storage from the start of the struct.
  72. CharUnits StorageOffset;
  73. /// The offset within a contiguous run of bitfields that are represented as a
  74. /// single "field" within the LLVM struct type, taking into account the AAPCS
  75. /// rules for volatile bitfields. This offset is in bits.
  76. unsigned VolatileOffset : 16;
  77. /// The storage size in bits which should be used when accessing this
  78. /// bitfield.
  79. unsigned VolatileStorageSize;
  80. /// The offset of the bitfield storage from the start of the struct.
  81. CharUnits VolatileStorageOffset;
  82. CGBitFieldInfo()
  83. : Offset(), Size(), IsSigned(), StorageSize(), VolatileOffset(),
  84. VolatileStorageSize() {}
  85. CGBitFieldInfo(unsigned Offset, unsigned Size, bool IsSigned,
  86. unsigned StorageSize, CharUnits StorageOffset)
  87. : Offset(Offset), Size(Size), IsSigned(IsSigned),
  88. StorageSize(StorageSize), StorageOffset(StorageOffset) {}
  89. void print(raw_ostream &OS) const;
  90. void dump() const;
  91. /// Given a bit-field decl, build an appropriate helper object for
  92. /// accessing that field (which is expected to have the given offset and
  93. /// size).
  94. static CGBitFieldInfo MakeInfo(class CodeGenTypes &Types,
  95. const FieldDecl *FD,
  96. uint64_t Offset, uint64_t Size,
  97. uint64_t StorageSize,
  98. CharUnits StorageOffset);
  99. };
  100. /// CGRecordLayout - This class handles struct and union layout info while
  101. /// lowering AST types to LLVM types.
  102. ///
  103. /// These layout objects are only created on demand as IR generation requires.
  104. class CGRecordLayout {
  105. friend class CodeGenTypes;
  106. CGRecordLayout(const CGRecordLayout &) = delete;
  107. void operator=(const CGRecordLayout &) = delete;
  108. private:
  109. /// The LLVM type corresponding to this record layout; used when
  110. /// laying it out as a complete object.
  111. llvm::StructType *CompleteObjectType;
  112. /// The LLVM type for the non-virtual part of this record layout;
  113. /// used when laying it out as a base subobject.
  114. llvm::StructType *BaseSubobjectType;
  115. /// Map from (non-bit-field) struct field to the corresponding llvm struct
  116. /// type field no. This info is populated by record builder.
  117. llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
  118. /// Map from (bit-field) struct field to the corresponding llvm struct type
  119. /// field no. This info is populated by record builder.
  120. llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
  121. // FIXME: Maybe we could use a CXXBaseSpecifier as the key and use a single
  122. // map for both virtual and non-virtual bases.
  123. llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
  124. /// Map from virtual bases to their field index in the complete object.
  125. llvm::DenseMap<const CXXRecordDecl *, unsigned> CompleteObjectVirtualBases;
  126. /// False if any direct or indirect subobject of this class, when
  127. /// considered as a complete object, requires a non-zero bitpattern
  128. /// when zero-initialized.
  129. bool IsZeroInitializable : 1;
  130. /// False if any direct or indirect subobject of this class, when
  131. /// considered as a base subobject, requires a non-zero bitpattern
  132. /// when zero-initialized.
  133. bool IsZeroInitializableAsBase : 1;
  134. public:
  135. CGRecordLayout(llvm::StructType *CompleteObjectType,
  136. llvm::StructType *BaseSubobjectType,
  137. bool IsZeroInitializable,
  138. bool IsZeroInitializableAsBase)
  139. : CompleteObjectType(CompleteObjectType),
  140. BaseSubobjectType(BaseSubobjectType),
  141. IsZeroInitializable(IsZeroInitializable),
  142. IsZeroInitializableAsBase(IsZeroInitializableAsBase) {}
  143. /// Return the "complete object" LLVM type associated with
  144. /// this record.
  145. llvm::StructType *getLLVMType() const {
  146. return CompleteObjectType;
  147. }
  148. /// Return the "base subobject" LLVM type associated with
  149. /// this record.
  150. llvm::StructType *getBaseSubobjectLLVMType() const {
  151. return BaseSubobjectType;
  152. }
  153. /// Check whether this struct can be C++ zero-initialized
  154. /// with a zeroinitializer.
  155. bool isZeroInitializable() const {
  156. return IsZeroInitializable;
  157. }
  158. /// Check whether this struct can be C++ zero-initialized
  159. /// with a zeroinitializer when considered as a base subobject.
  160. bool isZeroInitializableAsBase() const {
  161. return IsZeroInitializableAsBase;
  162. }
  163. /// Return llvm::StructType element number that corresponds to the
  164. /// field FD.
  165. unsigned getLLVMFieldNo(const FieldDecl *FD) const {
  166. FD = FD->getCanonicalDecl();
  167. assert(FieldInfo.count(FD) && "Invalid field for record!");
  168. return FieldInfo.lookup(FD);
  169. }
  170. unsigned getNonVirtualBaseLLVMFieldNo(const CXXRecordDecl *RD) const {
  171. assert(NonVirtualBases.count(RD) && "Invalid non-virtual base!");
  172. return NonVirtualBases.lookup(RD);
  173. }
  174. /// Return the LLVM field index corresponding to the given
  175. /// virtual base. Only valid when operating on the complete object.
  176. unsigned getVirtualBaseIndex(const CXXRecordDecl *base) const {
  177. assert(CompleteObjectVirtualBases.count(base) && "Invalid virtual base!");
  178. return CompleteObjectVirtualBases.lookup(base);
  179. }
  180. /// Return the BitFieldInfo that corresponds to the field FD.
  181. const CGBitFieldInfo &getBitFieldInfo(const FieldDecl *FD) const {
  182. FD = FD->getCanonicalDecl();
  183. assert(FD->isBitField() && "Invalid call for non-bit-field decl!");
  184. llvm::DenseMap<const FieldDecl *, CGBitFieldInfo>::const_iterator
  185. it = BitFields.find(FD);
  186. assert(it != BitFields.end() && "Unable to find bitfield info");
  187. return it->second;
  188. }
  189. void print(raw_ostream &OS) const;
  190. void dump() const;
  191. };
  192. } // end namespace CodeGen
  193. } // end namespace clang
  194. #endif