CGRecordLayout.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 alignmnet.
  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. CGBitFieldInfo()
  74. : Offset(), Size(), IsSigned(), StorageSize(), StorageOffset() {}
  75. CGBitFieldInfo(unsigned Offset, unsigned Size, bool IsSigned,
  76. unsigned StorageSize, CharUnits StorageOffset)
  77. : Offset(Offset), Size(Size), IsSigned(IsSigned),
  78. StorageSize(StorageSize), StorageOffset(StorageOffset) {}
  79. void print(raw_ostream &OS) const;
  80. void dump() const;
  81. /// Given a bit-field decl, build an appropriate helper object for
  82. /// accessing that field (which is expected to have the given offset and
  83. /// size).
  84. static CGBitFieldInfo MakeInfo(class CodeGenTypes &Types,
  85. const FieldDecl *FD,
  86. uint64_t Offset, uint64_t Size,
  87. uint64_t StorageSize,
  88. CharUnits StorageOffset);
  89. };
  90. /// CGRecordLayout - This class handles struct and union layout info while
  91. /// lowering AST types to LLVM types.
  92. ///
  93. /// These layout objects are only created on demand as IR generation requires.
  94. class CGRecordLayout {
  95. friend class CodeGenTypes;
  96. CGRecordLayout(const CGRecordLayout &) = delete;
  97. void operator=(const CGRecordLayout &) = delete;
  98. private:
  99. /// The LLVM type corresponding to this record layout; used when
  100. /// laying it out as a complete object.
  101. llvm::StructType *CompleteObjectType;
  102. /// The LLVM type for the non-virtual part of this record layout;
  103. /// used when laying it out as a base subobject.
  104. llvm::StructType *BaseSubobjectType;
  105. /// Map from (non-bit-field) struct field to the corresponding llvm struct
  106. /// type field no. This info is populated by record builder.
  107. llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
  108. /// Map from (bit-field) struct field to the corresponding llvm struct type
  109. /// field no. This info is populated by record builder.
  110. llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
  111. // FIXME: Maybe we could use a CXXBaseSpecifier as the key and use a single
  112. // map for both virtual and non-virtual bases.
  113. llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
  114. /// Map from virtual bases to their field index in the complete object.
  115. llvm::DenseMap<const CXXRecordDecl *, unsigned> CompleteObjectVirtualBases;
  116. /// False if any direct or indirect subobject of this class, when
  117. /// considered as a complete object, requires a non-zero bitpattern
  118. /// when zero-initialized.
  119. bool IsZeroInitializable : 1;
  120. /// False if any direct or indirect subobject of this class, when
  121. /// considered as a base subobject, requires a non-zero bitpattern
  122. /// when zero-initialized.
  123. bool IsZeroInitializableAsBase : 1;
  124. public:
  125. CGRecordLayout(llvm::StructType *CompleteObjectType,
  126. llvm::StructType *BaseSubobjectType,
  127. bool IsZeroInitializable,
  128. bool IsZeroInitializableAsBase)
  129. : CompleteObjectType(CompleteObjectType),
  130. BaseSubobjectType(BaseSubobjectType),
  131. IsZeroInitializable(IsZeroInitializable),
  132. IsZeroInitializableAsBase(IsZeroInitializableAsBase) {}
  133. /// Return the "complete object" LLVM type associated with
  134. /// this record.
  135. llvm::StructType *getLLVMType() const {
  136. return CompleteObjectType;
  137. }
  138. /// Return the "base subobject" LLVM type associated with
  139. /// this record.
  140. llvm::StructType *getBaseSubobjectLLVMType() const {
  141. return BaseSubobjectType;
  142. }
  143. /// Check whether this struct can be C++ zero-initialized
  144. /// with a zeroinitializer.
  145. bool isZeroInitializable() const {
  146. return IsZeroInitializable;
  147. }
  148. /// Check whether this struct can be C++ zero-initialized
  149. /// with a zeroinitializer when considered as a base subobject.
  150. bool isZeroInitializableAsBase() const {
  151. return IsZeroInitializableAsBase;
  152. }
  153. /// Return llvm::StructType element number that corresponds to the
  154. /// field FD.
  155. unsigned getLLVMFieldNo(const FieldDecl *FD) const {
  156. FD = FD->getCanonicalDecl();
  157. assert(FieldInfo.count(FD) && "Invalid field for record!");
  158. return FieldInfo.lookup(FD);
  159. }
  160. unsigned getNonVirtualBaseLLVMFieldNo(const CXXRecordDecl *RD) const {
  161. assert(NonVirtualBases.count(RD) && "Invalid non-virtual base!");
  162. return NonVirtualBases.lookup(RD);
  163. }
  164. /// Return the LLVM field index corresponding to the given
  165. /// virtual base. Only valid when operating on the complete object.
  166. unsigned getVirtualBaseIndex(const CXXRecordDecl *base) const {
  167. assert(CompleteObjectVirtualBases.count(base) && "Invalid virtual base!");
  168. return CompleteObjectVirtualBases.lookup(base);
  169. }
  170. /// Return the BitFieldInfo that corresponds to the field FD.
  171. const CGBitFieldInfo &getBitFieldInfo(const FieldDecl *FD) const {
  172. FD = FD->getCanonicalDecl();
  173. assert(FD->isBitField() && "Invalid call for non-bit-field decl!");
  174. llvm::DenseMap<const FieldDecl *, CGBitFieldInfo>::const_iterator
  175. it = BitFields.find(FD);
  176. assert(it != BitFields.end() && "Unable to find bitfield info");
  177. return it->second;
  178. }
  179. void print(raw_ostream &OS) const;
  180. void dump() const;
  181. };
  182. } // end namespace CodeGen
  183. } // end namespace clang
  184. #endif