CGRecordLayoutBuilder.cpp 36 KB

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