CGVTT.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //===--- CGVTT.cpp - Emit LLVM Code for C++ VTTs --------------------------===//
  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. // This contains code dealing with C++ code generation of VTTs (vtable tables).
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "CodeGenModule.h"
  13. #include "CGCXXABI.h"
  14. #include "clang/AST/RecordLayout.h"
  15. #include "clang/AST/VTTBuilder.h"
  16. using namespace clang;
  17. using namespace CodeGen;
  18. static llvm::GlobalVariable *
  19. GetAddrOfVTTVTable(CodeGenVTables &CGVT, CodeGenModule &CGM,
  20. const CXXRecordDecl *MostDerivedClass,
  21. const VTTVTable &VTable,
  22. llvm::GlobalVariable::LinkageTypes Linkage,
  23. VTableLayout::AddressPointsMapTy &AddressPoints) {
  24. if (VTable.getBase() == MostDerivedClass) {
  25. assert(VTable.getBaseOffset().isZero() &&
  26. "Most derived class vtable must have a zero offset!");
  27. // This is a regular vtable.
  28. return CGM.getCXXABI().getAddrOfVTable(MostDerivedClass, CharUnits());
  29. }
  30. return CGVT.GenerateConstructionVTable(MostDerivedClass,
  31. VTable.getBaseSubobject(),
  32. VTable.isVirtual(),
  33. Linkage,
  34. AddressPoints);
  35. }
  36. void
  37. CodeGenVTables::EmitVTTDefinition(llvm::GlobalVariable *VTT,
  38. llvm::GlobalVariable::LinkageTypes Linkage,
  39. const CXXRecordDecl *RD) {
  40. VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/true);
  41. llvm::ArrayType *ArrayType =
  42. llvm::ArrayType::get(CGM.Int8PtrTy, Builder.getVTTComponents().size());
  43. SmallVector<llvm::GlobalVariable *, 8> VTables;
  44. SmallVector<VTableAddressPointsMapTy, 8> VTableAddressPoints;
  45. for (const VTTVTable *i = Builder.getVTTVTables().begin(),
  46. *e = Builder.getVTTVTables().end(); i != e; ++i) {
  47. VTableAddressPoints.push_back(VTableAddressPointsMapTy());
  48. VTables.push_back(GetAddrOfVTTVTable(*this, CGM, RD, *i, Linkage,
  49. VTableAddressPoints.back()));
  50. }
  51. SmallVector<llvm::Constant *, 8> VTTComponents;
  52. for (const VTTComponent *i = Builder.getVTTComponents().begin(),
  53. *e = Builder.getVTTComponents().end(); i != e; ++i) {
  54. const VTTVTable &VTTVT = Builder.getVTTVTables()[i->VTableIndex];
  55. llvm::GlobalVariable *VTable = VTables[i->VTableIndex];
  56. VTableLayout::AddressPointLocation AddressPoint;
  57. if (VTTVT.getBase() == RD) {
  58. // Just get the address point for the regular vtable.
  59. AddressPoint =
  60. getItaniumVTableContext().getVTableLayout(RD).getAddressPoint(
  61. i->VTableBase);
  62. } else {
  63. AddressPoint = VTableAddressPoints[i->VTableIndex].lookup(i->VTableBase);
  64. assert(AddressPoint.AddressPointIndex != 0 &&
  65. "Did not find ctor vtable address point!");
  66. }
  67. llvm::Value *Idxs[] = {
  68. llvm::ConstantInt::get(CGM.Int32Ty, 0),
  69. llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.VTableIndex),
  70. llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.AddressPointIndex),
  71. };
  72. llvm::Constant *Init = llvm::ConstantExpr::getGetElementPtr(
  73. VTable->getValueType(), VTable, Idxs, /*InBounds=*/true,
  74. /*InRangeIndex=*/1);
  75. Init = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(Init,
  76. CGM.Int8PtrTy);
  77. VTTComponents.push_back(Init);
  78. }
  79. llvm::Constant *Init = llvm::ConstantArray::get(ArrayType, VTTComponents);
  80. VTT->setInitializer(Init);
  81. // Set the correct linkage.
  82. VTT->setLinkage(Linkage);
  83. if (CGM.supportsCOMDAT() && VTT->isWeakForLinker())
  84. VTT->setComdat(CGM.getModule().getOrInsertComdat(VTT->getName()));
  85. }
  86. llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) {
  87. assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT");
  88. SmallString<256> OutName;
  89. llvm::raw_svector_ostream Out(OutName);
  90. cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
  91. .mangleCXXVTT(RD, Out);
  92. StringRef Name = OutName.str();
  93. // This will also defer the definition of the VTT.
  94. (void) CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
  95. VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
  96. llvm::ArrayType *ArrayType =
  97. llvm::ArrayType::get(CGM.Int8PtrTy, Builder.getVTTComponents().size());
  98. llvm::Align Align = CGM.getDataLayout().getABITypeAlign(CGM.Int8PtrTy);
  99. llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable(
  100. Name, ArrayType, llvm::GlobalValue::ExternalLinkage, Align);
  101. GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
  102. CGM.setGVProperties(GV, RD);
  103. return GV;
  104. }
  105. uint64_t CodeGenVTables::getSubVTTIndex(const CXXRecordDecl *RD,
  106. BaseSubobject Base) {
  107. BaseSubobjectPairTy ClassSubobjectPair(RD, Base);
  108. SubVTTIndiciesMapTy::iterator I = SubVTTIndicies.find(ClassSubobjectPair);
  109. if (I != SubVTTIndicies.end())
  110. return I->second;
  111. VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
  112. for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
  113. Builder.getSubVTTIndicies().begin(),
  114. E = Builder.getSubVTTIndicies().end(); I != E; ++I) {
  115. // Insert all indices.
  116. BaseSubobjectPairTy ClassSubobjectPair(RD, I->first);
  117. SubVTTIndicies.insert(std::make_pair(ClassSubobjectPair, I->second));
  118. }
  119. I = SubVTTIndicies.find(ClassSubobjectPair);
  120. assert(I != SubVTTIndicies.end() && "Did not find index!");
  121. return I->second;
  122. }
  123. uint64_t
  124. CodeGenVTables::getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
  125. BaseSubobject Base) {
  126. SecondaryVirtualPointerIndicesMapTy::iterator I =
  127. SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
  128. if (I != SecondaryVirtualPointerIndices.end())
  129. return I->second;
  130. VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
  131. // Insert all secondary vpointer indices.
  132. for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
  133. Builder.getSecondaryVirtualPointerIndices().begin(),
  134. E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; ++I) {
  135. std::pair<const CXXRecordDecl *, BaseSubobject> Pair =
  136. std::make_pair(RD, I->first);
  137. SecondaryVirtualPointerIndices.insert(std::make_pair(Pair, I->second));
  138. }
  139. I = SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
  140. assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!");
  141. return I->second;
  142. }