CGRecordLayout.h 7.9 KB

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