CGVTT.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. // Set the right visibility.
  86. CGM.setGVProperties(VTT, RD);
  87. }
  88. llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) {
  89. assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT");
  90. SmallString<256> OutName;
  91. llvm::raw_svector_ostream Out(OutName);
  92. cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
  93. .mangleCXXVTT(RD, Out);
  94. StringRef Name = OutName.str();
  95. // This will also defer the definition of the VTT.
  96. (void) CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
  97. VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
  98. llvm::ArrayType *ArrayType =
  99. llvm::ArrayType::get(CGM.Int8PtrTy, Builder.getVTTComponents().size());
  100. unsigned Align = CGM.getDataLayout().getABITypeAlignment(CGM.Int8PtrTy);
  101. llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable(
  102. Name, ArrayType, llvm::GlobalValue::ExternalLinkage, Align);
  103. GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
  104. return GV;
  105. }
  106. uint64_t CodeGenVTables::getSubVTTIndex(const CXXRecordDecl *RD,
  107. BaseSubobject Base) {
  108. BaseSubobjectPairTy ClassSubobjectPair(RD, Base);
  109. SubVTTIndiciesMapTy::iterator I = SubVTTIndicies.find(ClassSubobjectPair);
  110. if (I != SubVTTIndicies.end())
  111. return I->second;
  112. VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
  113. for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
  114. Builder.getSubVTTIndicies().begin(),
  115. E = Builder.getSubVTTIndicies().end(); I != E; ++I) {
  116. // Insert all indices.
  117. BaseSubobjectPairTy ClassSubobjectPair(RD, I->first);
  118. SubVTTIndicies.insert(std::make_pair(ClassSubobjectPair, I->second));
  119. }
  120. I = SubVTTIndicies.find(ClassSubobjectPair);
  121. assert(I != SubVTTIndicies.end() && "Did not find index!");
  122. return I->second;
  123. }
  124. uint64_t
  125. CodeGenVTables::getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
  126. BaseSubobject Base) {
  127. SecondaryVirtualPointerIndicesMapTy::iterator I =
  128. SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
  129. if (I != SecondaryVirtualPointerIndices.end())
  130. return I->second;
  131. VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
  132. // Insert all secondary vpointer indices.
  133. for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
  134. Builder.getSecondaryVirtualPointerIndices().begin(),
  135. E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; ++I) {
  136. std::pair<const CXXRecordDecl *, BaseSubobject> Pair =
  137. std::make_pair(RD, I->first);
  138. SecondaryVirtualPointerIndices.insert(std::make_pair(Pair, I->second));
  139. }
  140. I = SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
  141. assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!");
  142. return I->second;
  143. }