CGRecordLayoutBuilder.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. //===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- 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. //
  9. // Builder implementation for CGRecordLayout objects.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "CGRecordLayout.h"
  13. #include "CGCXXABI.h"
  14. #include "CodeGenTypes.h"
  15. #include "clang/AST/ASTContext.h"
  16. #include "clang/AST/Attr.h"
  17. #include "clang/AST/CXXInheritance.h"
  18. #include "clang/AST/DeclCXX.h"
  19. #include "clang/AST/Expr.h"
  20. #include "clang/AST/RecordLayout.h"
  21. #include "clang/Basic/CodeGenOptions.h"
  22. #include "llvm/IR/DataLayout.h"
  23. #include "llvm/IR/DerivedTypes.h"
  24. #include "llvm/IR/Type.h"
  25. #include "llvm/Support/Debug.h"
  26. #include "llvm/Support/MathExtras.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. using namespace clang;
  29. using namespace CodeGen;
  30. namespace {
  31. /// The CGRecordLowering is responsible for lowering an ASTRecordLayout to an
  32. /// llvm::Type. Some of the lowering is straightforward, some is not. Here we
  33. /// detail some of the complexities and weirdnesses here.
  34. /// * LLVM does not have unions - Unions can, in theory be represented by any
  35. /// llvm::Type with correct size. We choose a field via a specific heuristic
  36. /// and add padding if necessary.
  37. /// * LLVM does not have bitfields - Bitfields are collected into contiguous
  38. /// runs and allocated as a single storage type for the run. ASTRecordLayout
  39. /// contains enough information to determine where the runs break. Microsoft
  40. /// and Itanium follow different rules and use different codepaths.
  41. /// * It is desired that, when possible, bitfields use the appropriate iN type
  42. /// when lowered to llvm types. For example unsigned x : 24 gets lowered to
  43. /// i24. This isn't always possible because i24 has storage size of 32 bit
  44. /// and if it is possible to use that extra byte of padding we must use
  45. /// [i8 x 3] instead of i24. The function clipTailPadding does this.
  46. /// C++ examples that require clipping:
  47. /// struct { int a : 24; char b; }; // a must be clipped, b goes at offset 3
  48. /// struct A { int a : 24; }; // a must be clipped because a struct like B
  49. // could exist: struct B : A { char b; }; // b goes at offset 3
  50. /// * Clang ignores 0 sized bitfields and 0 sized bases but *not* zero sized
  51. /// fields. The existing asserts suggest that LLVM assumes that *every* field
  52. /// has an underlying storage type. Therefore empty structures containing
  53. /// zero sized subobjects such as empty records or zero sized arrays still get
  54. /// a zero sized (empty struct) storage type.
  55. /// * Clang reads the complete type rather than the base type when generating
  56. /// code to access fields. Bitfields in tail position with tail padding may
  57. /// be clipped in the base class but not the complete class (we may discover
  58. /// that the tail padding is not used in the complete class.) However,
  59. /// because LLVM reads from the complete type it can generate incorrect code
  60. /// if we do not clip the tail padding off of the bitfield in the complete
  61. /// layout. This introduces a somewhat awkward extra unnecessary clip stage.
  62. /// The location of the clip is stored internally as a sentinel of type
  63. /// SCISSOR. If LLVM were updated to read base types (which it probably
  64. /// should because locations of things such as VBases are bogus in the llvm
  65. /// type anyway) then we could eliminate the SCISSOR.
  66. /// * Itanium allows nearly empty primary virtual bases. These bases don't get
  67. /// get their own storage because they're laid out as part of another base
  68. /// or at the beginning of the structure. Determining if a VBase actually
  69. /// gets storage awkwardly involves a walk of all bases.
  70. /// * VFPtrs and VBPtrs do *not* make a record NotZeroInitializable.
  71. struct CGRecordLowering {
  72. // MemberInfo is a helper structure that contains information about a record
  73. // member. In additional to the standard member types, there exists a
  74. // sentinel member type that ensures correct rounding.
  75. struct MemberInfo {
  76. CharUnits Offset;
  77. enum InfoKind { VFPtr, VBPtr, Field, Base, VBase, Scissor } Kind;
  78. llvm::Type *Data;
  79. union {
  80. const FieldDecl *FD;
  81. const CXXRecordDecl *RD;
  82. };
  83. MemberInfo(CharUnits Offset, InfoKind Kind, llvm::Type *Data,
  84. const FieldDecl *FD = nullptr)
  85. : Offset(Offset), Kind(Kind), Data(Data), FD(FD) {}
  86. MemberInfo(CharUnits Offset, InfoKind Kind, llvm::Type *Data,
  87. const CXXRecordDecl *RD)
  88. : Offset(Offset), Kind(Kind), Data(Data), RD(RD) {}
  89. // MemberInfos are sorted so we define a < operator.
  90. bool operator <(const MemberInfo& a) const { return Offset < a.Offset; }
  91. };
  92. // The constructor.
  93. CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D, bool Packed);
  94. // Short helper routines.
  95. /// Constructs a MemberInfo instance from an offset and llvm::Type *.
  96. MemberInfo StorageInfo(CharUnits Offset, llvm::Type *Data) {
  97. return MemberInfo(Offset, MemberInfo::Field, Data);
  98. }
  99. /// The Microsoft bitfield layout rule allocates discrete storage
  100. /// units of the field's formal type and only combines adjacent
  101. /// fields of the same formal type. We want to emit a layout with
  102. /// these discrete storage units instead of combining them into a
  103. /// continuous run.
  104. bool isDiscreteBitFieldABI() {
  105. return Context.getTargetInfo().getCXXABI().isMicrosoft() ||
  106. D->isMsStruct(Context);
  107. }
  108. /// The Itanium base layout rule allows virtual bases to overlap
  109. /// other bases, which complicates layout in specific ways.
  110. ///
  111. /// Note specifically that the ms_struct attribute doesn't change this.
  112. bool isOverlappingVBaseABI() {
  113. return !Context.getTargetInfo().getCXXABI().isMicrosoft();
  114. }
  115. /// Wraps llvm::Type::getIntNTy with some implicit arguments.
  116. llvm::Type *getIntNType(uint64_t NumBits) {
  117. return llvm::Type::getIntNTy(Types.getLLVMContext(),
  118. (unsigned)llvm::alignTo(NumBits, 8));
  119. }
  120. /// Gets an llvm type of size NumBytes and alignment 1.
  121. llvm::Type *getByteArrayType(CharUnits NumBytes) {
  122. assert(!NumBytes.isZero() && "Empty byte arrays aren't allowed.");
  123. llvm::Type *Type = llvm::Type::getInt8Ty(Types.getLLVMContext());
  124. return NumBytes == CharUnits::One() ? Type :
  125. (llvm::Type *)llvm::ArrayType::get(Type, NumBytes.getQuantity());
  126. }
  127. /// Gets the storage type for a field decl and handles storage
  128. /// for itanium bitfields that are smaller than their declared type.
  129. llvm::Type *getStorageType(const FieldDecl *FD) {
  130. llvm::Type *Type = Types.ConvertTypeForMem(FD->getType());
  131. if (!FD->isBitField()) return Type;
  132. if (isDiscreteBitFieldABI()) return Type;
  133. return getIntNType(std::min(FD->getBitWidthValue(Context),
  134. (unsigned)Context.toBits(getSize(Type))));
  135. }
  136. /// Gets the llvm Basesubobject type from a CXXRecordDecl.
  137. llvm::Type *getStorageType(const CXXRecordDecl *RD) {
  138. return Types.getCGRecordLayout(RD).getBaseSubobjectLLVMType();
  139. }
  140. CharUnits bitsToCharUnits(uint64_t BitOffset) {
  141. return Context.toCharUnitsFromBits(BitOffset);
  142. }
  143. CharUnits getSize(llvm::Type *Type) {
  144. return CharUnits::fromQuantity(DataLayout.getTypeAllocSize(Type));
  145. }
  146. CharUnits getAlignment(llvm::Type *Type) {
  147. return CharUnits::fromQuantity(DataLayout.getABITypeAlignment(Type));
  148. }
  149. bool isZeroInitializable(const FieldDecl *FD) {
  150. return Types.isZeroInitializable(FD->getType());
  151. }
  152. bool isZeroInitializable(const RecordDecl *RD) {
  153. return Types.isZeroInitializable(RD);
  154. }
  155. void appendPaddingBytes(CharUnits Size) {
  156. if (!Size.isZero())
  157. FieldTypes.push_back(getByteArrayType(Size));
  158. }
  159. uint64_t getFieldBitOffset(const FieldDecl *FD) {
  160. return Layout.getFieldOffset(FD->getFieldIndex());
  161. }
  162. // Layout routines.
  163. void setBitFieldInfo(const FieldDecl *FD, CharUnits StartOffset,
  164. llvm::Type *StorageType);
  165. /// Lowers an ASTRecordLayout to a llvm type.
  166. void lower(bool NonVirtualBaseType);
  167. void lowerUnion();
  168. void accumulateFields();
  169. void accumulateBitFields(RecordDecl::field_iterator Field,
  170. RecordDecl::field_iterator FieldEnd);
  171. void accumulateBases();
  172. void accumulateVPtrs();
  173. void accumulateVBases();
  174. /// Recursively searches all of the bases to find out if a vbase is
  175. /// not the primary vbase of some base class.
  176. bool hasOwnStorage(const CXXRecordDecl *Decl, const CXXRecordDecl *Query);
  177. void calculateZeroInit();
  178. /// Lowers bitfield storage types to I8 arrays for bitfields with tail
  179. /// padding that is or can potentially be used.
  180. void clipTailPadding();
  181. /// Determines if we need a packed llvm struct.
  182. void determinePacked(bool NVBaseType);
  183. /// Inserts padding everywhere it's needed.
  184. void insertPadding();
  185. /// Fills out the structures that are ultimately consumed.
  186. void fillOutputFields();
  187. // Input memoization fields.
  188. CodeGenTypes &Types;
  189. const ASTContext &Context;
  190. const RecordDecl *D;
  191. const CXXRecordDecl *RD;
  192. const ASTRecordLayout &Layout;
  193. const llvm::DataLayout &DataLayout;
  194. // Helpful intermediate data-structures.
  195. std::vector<MemberInfo> Members;
  196. // Output fields, consumed by CodeGenTypes::ComputeRecordLayout.
  197. SmallVector<llvm::Type *, 16> FieldTypes;
  198. llvm::DenseMap<const FieldDecl *, unsigned> Fields;
  199. llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
  200. llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
  201. llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases;
  202. bool IsZeroInitializable : 1;
  203. bool IsZeroInitializableAsBase : 1;
  204. bool Packed : 1;
  205. private:
  206. CGRecordLowering(const CGRecordLowering &) = delete;
  207. void operator =(const CGRecordLowering &) = delete;
  208. };
  209. } // namespace {
  210. CGRecordLowering::CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D,
  211. bool Packed)
  212. : Types(Types), Context(Types.getContext()), D(D),
  213. RD(dyn_cast<CXXRecordDecl>(D)),
  214. Layout(Types.getContext().getASTRecordLayout(D)),
  215. DataLayout(Types.getDataLayout()), IsZeroInitializable(true),
  216. IsZeroInitializableAsBase(true), Packed(Packed) {}
  217. void CGRecordLowering::setBitFieldInfo(
  218. const FieldDecl *FD, CharUnits StartOffset, llvm::Type *StorageType) {
  219. CGBitFieldInfo &Info = BitFields[FD->getCanonicalDecl()];
  220. Info.IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
  221. Info.Offset = (unsigned)(getFieldBitOffset(FD) - Context.toBits(StartOffset));
  222. Info.Size = FD->getBitWidthValue(Context);
  223. Info.StorageSize = (unsigned)DataLayout.getTypeAllocSizeInBits(StorageType);
  224. Info.StorageOffset = StartOffset;
  225. if (Info.Size > Info.StorageSize)
  226. Info.Size = Info.StorageSize;
  227. // Reverse the bit offsets for big endian machines. Because we represent
  228. // a bitfield as a single large integer load, we can imagine the bits
  229. // counting from the most-significant-bit instead of the
  230. // least-significant-bit.
  231. if (DataLayout.isBigEndian())
  232. Info.Offset = Info.StorageSize - (Info.Offset + Info.Size);
  233. }
  234. void CGRecordLowering::lower(bool NVBaseType) {
  235. // The lowering process implemented in this function takes a variety of
  236. // carefully ordered phases.
  237. // 1) Store all members (fields and bases) in a list and sort them by offset.
  238. // 2) Add a 1-byte capstone member at the Size of the structure.
  239. // 3) Clip bitfield storages members if their tail padding is or might be
  240. // used by another field or base. The clipping process uses the capstone
  241. // by treating it as another object that occurs after the record.
  242. // 4) Determine if the llvm-struct requires packing. It's important that this
  243. // phase occur after clipping, because clipping changes the llvm type.
  244. // This phase reads the offset of the capstone when determining packedness
  245. // and updates the alignment of the capstone to be equal of the alignment
  246. // of the record after doing so.
  247. // 5) Insert padding everywhere it is needed. This phase requires 'Packed' to
  248. // have been computed and needs to know the alignment of the record in
  249. // order to understand if explicit tail padding is needed.
  250. // 6) Remove the capstone, we don't need it anymore.
  251. // 7) Determine if this record can be zero-initialized. This phase could have
  252. // been placed anywhere after phase 1.
  253. // 8) Format the complete list of members in a way that can be consumed by
  254. // CodeGenTypes::ComputeRecordLayout.
  255. CharUnits Size = NVBaseType ? Layout.getNonVirtualSize() : Layout.getSize();
  256. if (D->isUnion())
  257. return lowerUnion();
  258. accumulateFields();
  259. // RD implies C++.
  260. if (RD) {
  261. accumulateVPtrs();
  262. accumulateBases();
  263. if (Members.empty())
  264. return appendPaddingBytes(Size);
  265. if (!NVBaseType)
  266. accumulateVBases();
  267. }
  268. llvm::stable_sort(Members);
  269. Members.push_back(StorageInfo(Size, getIntNType(8)));
  270. clipTailPadding();
  271. determinePacked(NVBaseType);
  272. insertPadding();
  273. Members.pop_back();
  274. calculateZeroInit();
  275. fillOutputFields();
  276. }
  277. void CGRecordLowering::lowerUnion() {
  278. CharUnits LayoutSize = Layout.getSize();
  279. llvm::Type *StorageType = nullptr;
  280. bool SeenNamedMember = false;
  281. // Iterate through the fields setting bitFieldInfo and the Fields array. Also
  282. // locate the "most appropriate" storage type. The heuristic for finding the
  283. // storage type isn't necessary, the first (non-0-length-bitfield) field's
  284. // type would work fine and be simpler but would be different than what we've
  285. // been doing and cause lit tests to change.
  286. for (const auto *Field : D->fields()) {
  287. if (Field->isBitField()) {
  288. if (Field->isZeroLengthBitField(Context))
  289. continue;
  290. llvm::Type *FieldType = getStorageType(Field);
  291. if (LayoutSize < getSize(FieldType))
  292. FieldType = getByteArrayType(LayoutSize);
  293. setBitFieldInfo(Field, CharUnits::Zero(), FieldType);
  294. }
  295. Fields[Field->getCanonicalDecl()] = 0;
  296. llvm::Type *FieldType = getStorageType(Field);
  297. // Compute zero-initializable status.
  298. // This union might not be zero initialized: it may contain a pointer to
  299. // data member which might have some exotic initialization sequence.
  300. // If this is the case, then we aught not to try and come up with a "better"
  301. // type, it might not be very easy to come up with a Constant which
  302. // correctly initializes it.
  303. if (!SeenNamedMember) {
  304. SeenNamedMember = Field->getIdentifier();
  305. if (!SeenNamedMember)
  306. if (const auto *FieldRD = Field->getType()->getAsRecordDecl())
  307. SeenNamedMember = FieldRD->findFirstNamedDataMember();
  308. if (SeenNamedMember && !isZeroInitializable(Field)) {
  309. IsZeroInitializable = IsZeroInitializableAsBase = false;
  310. StorageType = FieldType;
  311. }
  312. }
  313. // Because our union isn't zero initializable, we won't be getting a better
  314. // storage type.
  315. if (!IsZeroInitializable)
  316. continue;
  317. // Conditionally update our storage type if we've got a new "better" one.
  318. if (!StorageType ||
  319. getAlignment(FieldType) > getAlignment(StorageType) ||
  320. (getAlignment(FieldType) == getAlignment(StorageType) &&
  321. getSize(FieldType) > getSize(StorageType)))
  322. StorageType = FieldType;
  323. }
  324. // If we have no storage type just pad to the appropriate size and return.
  325. if (!StorageType)
  326. return appendPaddingBytes(LayoutSize);
  327. // If our storage size was bigger than our required size (can happen in the
  328. // case of packed bitfields on Itanium) then just use an I8 array.
  329. if (LayoutSize < getSize(StorageType))
  330. StorageType = getByteArrayType(LayoutSize);
  331. FieldTypes.push_back(StorageType);
  332. appendPaddingBytes(LayoutSize - getSize(StorageType));
  333. // Set packed if we need it.
  334. if (LayoutSize % getAlignment(StorageType))
  335. Packed = true;
  336. }
  337. void CGRecordLowering::accumulateFields() {
  338. for (RecordDecl::field_iterator Field = D->field_begin(),
  339. FieldEnd = D->field_end();
  340. Field != FieldEnd;) {
  341. if (Field->isBitField()) {
  342. RecordDecl::field_iterator Start = Field;
  343. // Iterate to gather the list of bitfields.
  344. for (++Field; Field != FieldEnd && Field->isBitField(); ++Field);
  345. accumulateBitFields(Start, Field);
  346. } else if (!Field->isZeroSize(Context)) {
  347. Members.push_back(MemberInfo(
  348. bitsToCharUnits(getFieldBitOffset(*Field)), MemberInfo::Field,
  349. getStorageType(*Field), *Field));
  350. ++Field;
  351. } else {
  352. ++Field;
  353. }
  354. }
  355. }
  356. void
  357. CGRecordLowering::accumulateBitFields(RecordDecl::field_iterator Field,
  358. RecordDecl::field_iterator FieldEnd) {
  359. // Run stores the first element of the current run of bitfields. FieldEnd is
  360. // used as a special value to note that we don't have a current run. A
  361. // bitfield run is a contiguous collection of bitfields that can be stored in
  362. // the same storage block. Zero-sized bitfields and bitfields that would
  363. // cross an alignment boundary break a run and start a new one.
  364. RecordDecl::field_iterator Run = FieldEnd;
  365. // Tail is the offset of the first bit off the end of the current run. It's
  366. // used to determine if the ASTRecordLayout is treating these two bitfields as
  367. // contiguous. StartBitOffset is offset of the beginning of the Run.
  368. uint64_t StartBitOffset, Tail = 0;
  369. if (isDiscreteBitFieldABI()) {
  370. for (; Field != FieldEnd; ++Field) {
  371. uint64_t BitOffset = getFieldBitOffset(*Field);
  372. // Zero-width bitfields end runs.
  373. if (Field->isZeroLengthBitField(Context)) {
  374. Run = FieldEnd;
  375. continue;
  376. }
  377. llvm::Type *Type = Types.ConvertTypeForMem(Field->getType());
  378. // If we don't have a run yet, or don't live within the previous run's
  379. // allocated storage then we allocate some storage and start a new run.
  380. if (Run == FieldEnd || BitOffset >= Tail) {
  381. Run = Field;
  382. StartBitOffset = BitOffset;
  383. Tail = StartBitOffset + DataLayout.getTypeAllocSizeInBits(Type);
  384. // Add the storage member to the record. This must be added to the
  385. // record before the bitfield members so that it gets laid out before
  386. // the bitfields it contains get laid out.
  387. Members.push_back(StorageInfo(bitsToCharUnits(StartBitOffset), Type));
  388. }
  389. // Bitfields get the offset of their storage but come afterward and remain
  390. // there after a stable sort.
  391. Members.push_back(MemberInfo(bitsToCharUnits(StartBitOffset),
  392. MemberInfo::Field, nullptr, *Field));
  393. }
  394. return;
  395. }
  396. // Check if OffsetInRecord is better as a single field run. When OffsetInRecord
  397. // has legal integer width, and its bitfield offset is naturally aligned, it
  398. // is better to make the bitfield a separate storage component so as it can be
  399. // accessed directly with lower cost.
  400. auto IsBetterAsSingleFieldRun = [&](uint64_t OffsetInRecord,
  401. uint64_t StartBitOffset) {
  402. if (!Types.getCodeGenOpts().FineGrainedBitfieldAccesses)
  403. return false;
  404. if (!DataLayout.isLegalInteger(OffsetInRecord))
  405. return false;
  406. // Make sure StartBitOffset is natually aligned if it is treated as an
  407. // IType integer.
  408. if (StartBitOffset %
  409. Context.toBits(getAlignment(getIntNType(OffsetInRecord))) !=
  410. 0)
  411. return false;
  412. return true;
  413. };
  414. // The start field is better as a single field run.
  415. bool StartFieldAsSingleRun = false;
  416. for (;;) {
  417. // Check to see if we need to start a new run.
  418. if (Run == FieldEnd) {
  419. // If we're out of fields, return.
  420. if (Field == FieldEnd)
  421. break;
  422. // Any non-zero-length bitfield can start a new run.
  423. if (!Field->isZeroLengthBitField(Context)) {
  424. Run = Field;
  425. StartBitOffset = getFieldBitOffset(*Field);
  426. Tail = StartBitOffset + Field->getBitWidthValue(Context);
  427. StartFieldAsSingleRun = IsBetterAsSingleFieldRun(Tail - StartBitOffset,
  428. StartBitOffset);
  429. }
  430. ++Field;
  431. continue;
  432. }
  433. // If the start field of a new run is better as a single run, or
  434. // if current field (or consecutive fields) is better as a single run, or
  435. // if current field has zero width bitfield and either
  436. // UseZeroLengthBitfieldAlignment or UseBitFieldTypeAlignment is set to
  437. // true, or
  438. // if the offset of current field is inconsistent with the offset of
  439. // previous field plus its offset,
  440. // skip the block below and go ahead to emit the storage.
  441. // Otherwise, try to add bitfields to the run.
  442. if (!StartFieldAsSingleRun && Field != FieldEnd &&
  443. !IsBetterAsSingleFieldRun(Tail - StartBitOffset, StartBitOffset) &&
  444. (!Field->isZeroLengthBitField(Context) ||
  445. (!Context.getTargetInfo().useZeroLengthBitfieldAlignment() &&
  446. !Context.getTargetInfo().useBitFieldTypeAlignment())) &&
  447. Tail == getFieldBitOffset(*Field)) {
  448. Tail += Field->getBitWidthValue(Context);
  449. ++Field;
  450. continue;
  451. }
  452. // We've hit a break-point in the run and need to emit a storage field.
  453. llvm::Type *Type = getIntNType(Tail - StartBitOffset);
  454. // Add the storage member to the record and set the bitfield info for all of
  455. // the bitfields in the run. Bitfields get the offset of their storage but
  456. // come afterward and remain there after a stable sort.
  457. Members.push_back(StorageInfo(bitsToCharUnits(StartBitOffset), Type));
  458. for (; Run != Field; ++Run)
  459. Members.push_back(MemberInfo(bitsToCharUnits(StartBitOffset),
  460. MemberInfo::Field, nullptr, *Run));
  461. Run = FieldEnd;
  462. StartFieldAsSingleRun = false;
  463. }
  464. }
  465. void CGRecordLowering::accumulateBases() {
  466. // If we've got a primary virtual base, we need to add it with the bases.
  467. if (Layout.isPrimaryBaseVirtual()) {
  468. const CXXRecordDecl *BaseDecl = Layout.getPrimaryBase();
  469. Members.push_back(MemberInfo(CharUnits::Zero(), MemberInfo::Base,
  470. getStorageType(BaseDecl), BaseDecl));
  471. }
  472. // Accumulate the non-virtual bases.
  473. for (const auto &Base : RD->bases()) {
  474. if (Base.isVirtual())
  475. continue;
  476. // Bases can be zero-sized even if not technically empty if they
  477. // contain only a trailing array member.
  478. const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
  479. if (!BaseDecl->isEmpty() &&
  480. !Context.getASTRecordLayout(BaseDecl).getNonVirtualSize().isZero())
  481. Members.push_back(MemberInfo(Layout.getBaseClassOffset(BaseDecl),
  482. MemberInfo::Base, getStorageType(BaseDecl), BaseDecl));
  483. }
  484. }
  485. void CGRecordLowering::accumulateVPtrs() {
  486. if (Layout.hasOwnVFPtr())
  487. Members.push_back(MemberInfo(CharUnits::Zero(), MemberInfo::VFPtr,
  488. llvm::FunctionType::get(getIntNType(32), /*isVarArg=*/true)->
  489. getPointerTo()->getPointerTo()));
  490. if (Layout.hasOwnVBPtr())
  491. Members.push_back(MemberInfo(Layout.getVBPtrOffset(), MemberInfo::VBPtr,
  492. llvm::Type::getInt32PtrTy(Types.getLLVMContext())));
  493. }
  494. void CGRecordLowering::accumulateVBases() {
  495. CharUnits ScissorOffset = Layout.getNonVirtualSize();
  496. // In the itanium ABI, it's possible to place a vbase at a dsize that is
  497. // smaller than the nvsize. Here we check to see if such a base is placed
  498. // before the nvsize and set the scissor offset to that, instead of the
  499. // nvsize.
  500. if (isOverlappingVBaseABI())
  501. for (const auto &Base : RD->vbases()) {
  502. const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
  503. if (BaseDecl->isEmpty())
  504. continue;
  505. // If the vbase is a primary virtual base of some base, then it doesn't
  506. // get its own storage location but instead lives inside of that base.
  507. if (Context.isNearlyEmpty(BaseDecl) && !hasOwnStorage(RD, BaseDecl))
  508. continue;
  509. ScissorOffset = std::min(ScissorOffset,
  510. Layout.getVBaseClassOffset(BaseDecl));
  511. }
  512. Members.push_back(MemberInfo(ScissorOffset, MemberInfo::Scissor, nullptr,
  513. RD));
  514. for (const auto &Base : RD->vbases()) {
  515. const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
  516. if (BaseDecl->isEmpty())
  517. continue;
  518. CharUnits Offset = Layout.getVBaseClassOffset(BaseDecl);
  519. // If the vbase is a primary virtual base of some base, then it doesn't
  520. // get its own storage location but instead lives inside of that base.
  521. if (isOverlappingVBaseABI() &&
  522. Context.isNearlyEmpty(BaseDecl) &&
  523. !hasOwnStorage(RD, BaseDecl)) {
  524. Members.push_back(MemberInfo(Offset, MemberInfo::VBase, nullptr,
  525. BaseDecl));
  526. continue;
  527. }
  528. // If we've got a vtordisp, add it as a storage type.
  529. if (Layout.getVBaseOffsetsMap().find(BaseDecl)->second.hasVtorDisp())
  530. Members.push_back(StorageInfo(Offset - CharUnits::fromQuantity(4),
  531. getIntNType(32)));
  532. Members.push_back(MemberInfo(Offset, MemberInfo::VBase,
  533. getStorageType(BaseDecl), BaseDecl));
  534. }
  535. }
  536. bool CGRecordLowering::hasOwnStorage(const CXXRecordDecl *Decl,
  537. const CXXRecordDecl *Query) {
  538. const ASTRecordLayout &DeclLayout = Context.getASTRecordLayout(Decl);
  539. if (DeclLayout.isPrimaryBaseVirtual() && DeclLayout.getPrimaryBase() == Query)
  540. return false;
  541. for (const auto &Base : Decl->bases())
  542. if (!hasOwnStorage(Base.getType()->getAsCXXRecordDecl(), Query))
  543. return false;
  544. return true;
  545. }
  546. void CGRecordLowering::calculateZeroInit() {
  547. for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
  548. MemberEnd = Members.end();
  549. IsZeroInitializableAsBase && Member != MemberEnd; ++Member) {
  550. if (Member->Kind == MemberInfo::Field) {
  551. if (!Member->FD || isZeroInitializable(Member->FD))
  552. continue;
  553. IsZeroInitializable = IsZeroInitializableAsBase = false;
  554. } else if (Member->Kind == MemberInfo::Base ||
  555. Member->Kind == MemberInfo::VBase) {
  556. if (isZeroInitializable(Member->RD))
  557. continue;
  558. IsZeroInitializable = false;
  559. if (Member->Kind == MemberInfo::Base)
  560. IsZeroInitializableAsBase = false;
  561. }
  562. }
  563. }
  564. void CGRecordLowering::clipTailPadding() {
  565. std::vector<MemberInfo>::iterator Prior = Members.begin();
  566. CharUnits Tail = getSize(Prior->Data);
  567. for (std::vector<MemberInfo>::iterator Member = Prior + 1,
  568. MemberEnd = Members.end();
  569. Member != MemberEnd; ++Member) {
  570. // Only members with data and the scissor can cut into tail padding.
  571. if (!Member->Data && Member->Kind != MemberInfo::Scissor)
  572. continue;
  573. if (Member->Offset < Tail) {
  574. assert(Prior->Kind == MemberInfo::Field &&
  575. "Only storage fields have tail padding!");
  576. if (!Prior->FD || Prior->FD->isBitField())
  577. Prior->Data = getByteArrayType(bitsToCharUnits(llvm::alignTo(
  578. cast<llvm::IntegerType>(Prior->Data)->getIntegerBitWidth(), 8)));
  579. else {
  580. assert(Prior->FD->hasAttr<NoUniqueAddressAttr>() &&
  581. "should not have reused this field's tail padding");
  582. Prior->Data = getByteArrayType(
  583. Context.getTypeInfoDataSizeInChars(Prior->FD->getType()).first);
  584. }
  585. }
  586. if (Member->Data)
  587. Prior = Member;
  588. Tail = Prior->Offset + getSize(Prior->Data);
  589. }
  590. }
  591. void CGRecordLowering::determinePacked(bool NVBaseType) {
  592. if (Packed)
  593. return;
  594. CharUnits Alignment = CharUnits::One();
  595. CharUnits NVAlignment = CharUnits::One();
  596. CharUnits NVSize =
  597. !NVBaseType && RD ? Layout.getNonVirtualSize() : CharUnits::Zero();
  598. for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
  599. MemberEnd = Members.end();
  600. Member != MemberEnd; ++Member) {
  601. if (!Member->Data)
  602. continue;
  603. // If any member falls at an offset that it not a multiple of its alignment,
  604. // then the entire record must be packed.
  605. if (Member->Offset % getAlignment(Member->Data))
  606. Packed = true;
  607. if (Member->Offset < NVSize)
  608. NVAlignment = std::max(NVAlignment, getAlignment(Member->Data));
  609. Alignment = std::max(Alignment, getAlignment(Member->Data));
  610. }
  611. // If the size of the record (the capstone's offset) is not a multiple of the
  612. // record's alignment, it must be packed.
  613. if (Members.back().Offset % Alignment)
  614. Packed = true;
  615. // If the non-virtual sub-object is not a multiple of the non-virtual
  616. // sub-object's alignment, it must be packed. We cannot have a packed
  617. // non-virtual sub-object and an unpacked complete object or vise versa.
  618. if (NVSize % NVAlignment)
  619. Packed = true;
  620. // Update the alignment of the sentinel.
  621. if (!Packed)
  622. Members.back().Data = getIntNType(Context.toBits(Alignment));
  623. }
  624. void CGRecordLowering::insertPadding() {
  625. std::vector<std::pair<CharUnits, CharUnits> > Padding;
  626. CharUnits Size = CharUnits::Zero();
  627. for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
  628. MemberEnd = Members.end();
  629. Member != MemberEnd; ++Member) {
  630. if (!Member->Data)
  631. continue;
  632. CharUnits Offset = Member->Offset;
  633. assert(Offset >= Size);
  634. // Insert padding if we need to.
  635. if (Offset !=
  636. Size.alignTo(Packed ? CharUnits::One() : getAlignment(Member->Data)))
  637. Padding.push_back(std::make_pair(Size, Offset - Size));
  638. Size = Offset + getSize(Member->Data);
  639. }
  640. if (Padding.empty())
  641. return;
  642. // Add the padding to the Members list and sort it.
  643. for (std::vector<std::pair<CharUnits, CharUnits> >::const_iterator
  644. Pad = Padding.begin(), PadEnd = Padding.end();
  645. Pad != PadEnd; ++Pad)
  646. Members.push_back(StorageInfo(Pad->first, getByteArrayType(Pad->second)));
  647. llvm::stable_sort(Members);
  648. }
  649. void CGRecordLowering::fillOutputFields() {
  650. for (std::vector<MemberInfo>::const_iterator Member = Members.begin(),
  651. MemberEnd = Members.end();
  652. Member != MemberEnd; ++Member) {
  653. if (Member->Data)
  654. FieldTypes.push_back(Member->Data);
  655. if (Member->Kind == MemberInfo::Field) {
  656. if (Member->FD)
  657. Fields[Member->FD->getCanonicalDecl()] = FieldTypes.size() - 1;
  658. // A field without storage must be a bitfield.
  659. if (!Member->Data)
  660. setBitFieldInfo(Member->FD, Member->Offset, FieldTypes.back());
  661. } else if (Member->Kind == MemberInfo::Base)
  662. NonVirtualBases[Member->RD] = FieldTypes.size() - 1;
  663. else if (Member->Kind == MemberInfo::VBase)
  664. VirtualBases[Member->RD] = FieldTypes.size() - 1;
  665. }
  666. }
  667. CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
  668. const FieldDecl *FD,
  669. uint64_t Offset, uint64_t Size,
  670. uint64_t StorageSize,
  671. CharUnits StorageOffset) {
  672. // This function is vestigial from CGRecordLayoutBuilder days but is still
  673. // used in GCObjCRuntime.cpp. That usage has a "fixme" attached to it that
  674. // when addressed will allow for the removal of this function.
  675. llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType());
  676. CharUnits TypeSizeInBytes =
  677. CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(Ty));
  678. uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
  679. bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
  680. if (Size > TypeSizeInBits) {
  681. // We have a wide bit-field. The extra bits are only used for padding, so
  682. // if we have a bitfield of type T, with size N:
  683. //
  684. // T t : N;
  685. //
  686. // We can just assume that it's:
  687. //
  688. // T t : sizeof(T);
  689. //
  690. Size = TypeSizeInBits;
  691. }
  692. // Reverse the bit offsets for big endian machines. Because we represent
  693. // a bitfield as a single large integer load, we can imagine the bits
  694. // counting from the most-significant-bit instead of the
  695. // least-significant-bit.
  696. if (Types.getDataLayout().isBigEndian()) {
  697. Offset = StorageSize - (Offset + Size);
  698. }
  699. return CGBitFieldInfo(Offset, Size, IsSigned, StorageSize, StorageOffset);
  700. }
  701. CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D,
  702. llvm::StructType *Ty) {
  703. CGRecordLowering Builder(*this, D, /*Packed=*/false);
  704. Builder.lower(/*NonVirtualBaseType=*/false);
  705. // If we're in C++, compute the base subobject type.
  706. llvm::StructType *BaseTy = nullptr;
  707. if (isa<CXXRecordDecl>(D) && !D->isUnion() && !D->hasAttr<FinalAttr>()) {
  708. BaseTy = Ty;
  709. if (Builder.Layout.getNonVirtualSize() != Builder.Layout.getSize()) {
  710. CGRecordLowering BaseBuilder(*this, D, /*Packed=*/Builder.Packed);
  711. BaseBuilder.lower(/*NonVirtualBaseType=*/true);
  712. BaseTy = llvm::StructType::create(
  713. getLLVMContext(), BaseBuilder.FieldTypes, "", BaseBuilder.Packed);
  714. addRecordTypeName(D, BaseTy, ".base");
  715. // BaseTy and Ty must agree on their packedness for getLLVMFieldNo to work
  716. // on both of them with the same index.
  717. assert(Builder.Packed == BaseBuilder.Packed &&
  718. "Non-virtual and complete types must agree on packedness");
  719. }
  720. }
  721. // Fill in the struct *after* computing the base type. Filling in the body
  722. // signifies that the type is no longer opaque and record layout is complete,
  723. // but we may need to recursively layout D while laying D out as a base type.
  724. Ty->setBody(Builder.FieldTypes, Builder.Packed);
  725. CGRecordLayout *RL =
  726. new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
  727. Builder.IsZeroInitializableAsBase);
  728. RL->NonVirtualBases.swap(Builder.NonVirtualBases);
  729. RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
  730. // Add all the field numbers.
  731. RL->FieldInfo.swap(Builder.Fields);
  732. // Add bitfield info.
  733. RL->BitFields.swap(Builder.BitFields);
  734. // Dump the layout, if requested.
  735. if (getContext().getLangOpts().DumpRecordLayouts) {
  736. llvm::outs() << "\n*** Dumping IRgen Record Layout\n";
  737. llvm::outs() << "Record: ";
  738. D->dump(llvm::outs());
  739. llvm::outs() << "\nLayout: ";
  740. RL->print(llvm::outs());
  741. }
  742. #ifndef NDEBUG
  743. // Verify that the computed LLVM struct size matches the AST layout size.
  744. const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
  745. uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
  746. assert(TypeSizeInBits == getDataLayout().getTypeAllocSizeInBits(Ty) &&
  747. "Type size mismatch!");
  748. if (BaseTy) {
  749. CharUnits NonVirtualSize = Layout.getNonVirtualSize();
  750. uint64_t AlignedNonVirtualTypeSizeInBits =
  751. getContext().toBits(NonVirtualSize);
  752. assert(AlignedNonVirtualTypeSizeInBits ==
  753. getDataLayout().getTypeAllocSizeInBits(BaseTy) &&
  754. "Type size mismatch!");
  755. }
  756. // Verify that the LLVM and AST field offsets agree.
  757. llvm::StructType *ST = RL->getLLVMType();
  758. const llvm::StructLayout *SL = getDataLayout().getStructLayout(ST);
  759. const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
  760. RecordDecl::field_iterator it = D->field_begin();
  761. for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
  762. const FieldDecl *FD = *it;
  763. // Ignore zero-sized fields.
  764. if (FD->isZeroSize(getContext()))
  765. continue;
  766. // For non-bit-fields, just check that the LLVM struct offset matches the
  767. // AST offset.
  768. if (!FD->isBitField()) {
  769. unsigned FieldNo = RL->getLLVMFieldNo(FD);
  770. assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
  771. "Invalid field offset!");
  772. continue;
  773. }
  774. // Ignore unnamed bit-fields.
  775. if (!FD->getDeclName())
  776. continue;
  777. const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
  778. llvm::Type *ElementTy = ST->getTypeAtIndex(RL->getLLVMFieldNo(FD));
  779. // Unions have overlapping elements dictating their layout, but for
  780. // non-unions we can verify that this section of the layout is the exact
  781. // expected size.
  782. if (D->isUnion()) {
  783. // For unions we verify that the start is zero and the size
  784. // is in-bounds. However, on BE systems, the offset may be non-zero, but
  785. // the size + offset should match the storage size in that case as it
  786. // "starts" at the back.
  787. if (getDataLayout().isBigEndian())
  788. assert(static_cast<unsigned>(Info.Offset + Info.Size) ==
  789. Info.StorageSize &&
  790. "Big endian union bitfield does not end at the back");
  791. else
  792. assert(Info.Offset == 0 &&
  793. "Little endian union bitfield with a non-zero offset");
  794. assert(Info.StorageSize <= SL->getSizeInBits() &&
  795. "Union not large enough for bitfield storage");
  796. } else {
  797. assert(Info.StorageSize ==
  798. getDataLayout().getTypeAllocSizeInBits(ElementTy) &&
  799. "Storage size does not match the element type size");
  800. }
  801. assert(Info.Size > 0 && "Empty bitfield!");
  802. assert(static_cast<unsigned>(Info.Offset) + Info.Size <= Info.StorageSize &&
  803. "Bitfield outside of its allocated storage");
  804. }
  805. #endif
  806. return RL;
  807. }
  808. void CGRecordLayout::print(raw_ostream &OS) const {
  809. OS << "<CGRecordLayout\n";
  810. OS << " LLVMType:" << *CompleteObjectType << "\n";
  811. if (BaseSubobjectType)
  812. OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
  813. OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
  814. OS << " BitFields:[\n";
  815. // Print bit-field infos in declaration order.
  816. std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
  817. for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
  818. it = BitFields.begin(), ie = BitFields.end();
  819. it != ie; ++it) {
  820. const RecordDecl *RD = it->first->getParent();
  821. unsigned Index = 0;
  822. for (RecordDecl::field_iterator
  823. it2 = RD->field_begin(); *it2 != it->first; ++it2)
  824. ++Index;
  825. BFIs.push_back(std::make_pair(Index, &it->second));
  826. }
  827. llvm::array_pod_sort(BFIs.begin(), BFIs.end());
  828. for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
  829. OS.indent(4);
  830. BFIs[i].second->print(OS);
  831. OS << "\n";
  832. }
  833. OS << "]>\n";
  834. }
  835. LLVM_DUMP_METHOD void CGRecordLayout::dump() const {
  836. print(llvm::errs());
  837. }
  838. void CGBitFieldInfo::print(raw_ostream &OS) const {
  839. OS << "<CGBitFieldInfo"
  840. << " Offset:" << Offset
  841. << " Size:" << Size
  842. << " IsSigned:" << IsSigned
  843. << " StorageSize:" << StorageSize
  844. << " StorageOffset:" << StorageOffset.getQuantity() << ">";
  845. }
  846. LLVM_DUMP_METHOD void CGBitFieldInfo::dump() const {
  847. print(llvm::errs());
  848. }