clang  14.0.0git
CGVTT.cpp
Go to the documentation of this file.
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 
13 #include "CodeGenModule.h"
14 #include "CGCXXABI.h"
15 #include "clang/AST/RecordLayout.h"
16 #include "clang/AST/VTTBuilder.h"
17 using namespace clang;
18 using namespace CodeGen;
19 
20 static llvm::GlobalVariable *
22  const CXXRecordDecl *MostDerivedClass,
23  const VTTVTable &VTable,
24  llvm::GlobalVariable::LinkageTypes Linkage,
25  VTableLayout::AddressPointsMapTy &AddressPoints) {
26  if (VTable.getBase() == MostDerivedClass) {
27  assert(VTable.getBaseOffset().isZero() &&
28  "Most derived class vtable must have a zero offset!");
29  // This is a regular vtable.
30  return CGM.getCXXABI().getAddrOfVTable(MostDerivedClass, CharUnits());
31  }
32 
33  return CGVT.GenerateConstructionVTable(MostDerivedClass,
34  VTable.getBaseSubobject(),
35  VTable.isVirtual(),
36  Linkage,
37  AddressPoints);
38 }
39 
40 void
41 CodeGenVTables::EmitVTTDefinition(llvm::GlobalVariable *VTT,
42  llvm::GlobalVariable::LinkageTypes Linkage,
43  const CXXRecordDecl *RD) {
44  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/true);
45  llvm::ArrayType *ArrayType =
46  llvm::ArrayType::get(CGM.Int8PtrTy, Builder.getVTTComponents().size());
47 
49  SmallVector<VTableAddressPointsMapTy, 8> VTableAddressPoints;
50  for (const VTTVTable *i = Builder.getVTTVTables().begin(),
51  *e = Builder.getVTTVTables().end(); i != e; ++i) {
52  VTableAddressPoints.push_back(VTableAddressPointsMapTy());
53  VTables.push_back(GetAddrOfVTTVTable(*this, CGM, RD, *i, Linkage,
54  VTableAddressPoints.back()));
55  }
56 
58  for (const VTTComponent *i = Builder.getVTTComponents().begin(),
59  *e = Builder.getVTTComponents().end(); i != e; ++i) {
60  const VTTVTable &VTTVT = Builder.getVTTVTables()[i->VTableIndex];
61  llvm::GlobalVariable *VTable = VTables[i->VTableIndex];
63  if (VTTVT.getBase() == RD) {
64  // Just get the address point for the regular vtable.
65  AddressPoint =
67  i->VTableBase);
68  } else {
69  AddressPoint = VTableAddressPoints[i->VTableIndex].lookup(i->VTableBase);
70  assert(AddressPoint.AddressPointIndex != 0 &&
71  "Did not find ctor vtable address point!");
72  }
73 
74  llvm::Value *Idxs[] = {
75  llvm::ConstantInt::get(CGM.Int32Ty, 0),
76  llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.VTableIndex),
77  llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.AddressPointIndex),
78  };
79 
80  llvm::Constant *Init = llvm::ConstantExpr::getGetElementPtr(
81  VTable->getValueType(), VTable, Idxs, /*InBounds=*/true,
82  /*InRangeIndex=*/1);
83 
84  Init = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(Init,
85  CGM.Int8PtrTy);
86 
87  VTTComponents.push_back(Init);
88  }
89 
90  llvm::Constant *Init = llvm::ConstantArray::get(ArrayType, VTTComponents);
91 
92  VTT->setInitializer(Init);
93 
94  // Set the correct linkage.
95  VTT->setLinkage(Linkage);
96 
97  if (CGM.supportsCOMDAT() && VTT->isWeakForLinker())
98  VTT->setComdat(CGM.getModule().getOrInsertComdat(VTT->getName()));
99 
100  // Set the right visibility.
101  CGM.setGVProperties(VTT, RD);
102 }
103 
104 llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) {
105  assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT");
106 
107  SmallString<256> OutName;
108  llvm::raw_svector_ostream Out(OutName);
109  cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
110  .mangleCXXVTT(RD, Out);
111  StringRef Name = OutName.str();
112 
113  // This will also defer the definition of the VTT.
114  (void) CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
115 
116  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
117 
118  llvm::ArrayType *ArrayType =
119  llvm::ArrayType::get(CGM.Int8PtrTy, Builder.getVTTComponents().size());
120  unsigned Align = CGM.getDataLayout().getABITypeAlignment(CGM.Int8PtrTy);
121 
122  llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable(
124  GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
125  return GV;
126 }
127 
130  BaseSubobjectPairTy ClassSubobjectPair(RD, Base);
131 
132  SubVTTIndiciesMapTy::iterator I = SubVTTIndicies.find(ClassSubobjectPair);
133  if (I != SubVTTIndicies.end())
134  return I->second;
135 
136  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
137 
138  for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
139  Builder.getSubVTTIndicies().begin(),
140  E = Builder.getSubVTTIndicies().end(); I != E; ++I) {
141  // Insert all indices.
142  BaseSubobjectPairTy ClassSubobjectPair(RD, I->first);
143 
144  SubVTTIndicies.insert(std::make_pair(ClassSubobjectPair, I->second));
145  }
146 
147  I = SubVTTIndicies.find(ClassSubobjectPair);
148  assert(I != SubVTTIndicies.end() && "Did not find index!");
149 
150  return I->second;
151 }
152 
153 uint64_t
156  SecondaryVirtualPointerIndicesMapTy::iterator I =
157  SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
158 
159  if (I != SecondaryVirtualPointerIndices.end())
160  return I->second;
161 
162  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
163 
164  // Insert all secondary vpointer indices.
165  for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
166  Builder.getSecondaryVirtualPointerIndices().begin(),
167  E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; ++I) {
168  std::pair<const CXXRecordDecl *, BaseSubobject> Pair =
169  std::make_pair(RD, I->first);
170 
171  SecondaryVirtualPointerIndices.insert(std::make_pair(Pair, I->second));
172  }
173 
174  I = SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
175  assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!");
176 
177  return I->second;
178 }
clang::CodeGen::CodeGenTypeCache::Int8PtrTy
llvm::PointerType * Int8PtrTy
Definition: CodeGenTypeCache.h:57
VTTBuilder.h
llvm::SmallVector
Definition: LLVM.h:38
clang::VTTVTable::getBaseSubobject
BaseSubobject getBaseSubobject() const
Definition: VTTBuilder.h:56
clang::CodeGen::CGCXXABI::getMangleContext
MangleContext & getMangleContext()
Gets the mangle context.
Definition: CGCXXABI.h:97
clang::CodeGen::CodeGenVTables::GenerateConstructionVTable
llvm::GlobalVariable * GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base, bool BaseIsVirtual, llvm::GlobalVariable::LinkageTypes Linkage, VTableAddressPointsMapTy &AddressPoints)
GenerateConstructionVTable - Generate a construction vtable for the given base subobject.
Definition: CGVTables.cpp:856
clang::CodeGen::CodeGenModule::getContext
ASTContext & getContext() const
Definition: CodeGenModule.h:702
clang::CodeGen::CodeGenVTables::GetAddrOfVTT
llvm::GlobalVariable * GetAddrOfVTT(const CXXRecordDecl *RD)
GetAddrOfVTT - Get the address of the VTT for the given record decl.
Definition: CGVTT.cpp:104
GetAddrOfVTTVTable
static llvm::GlobalVariable * GetAddrOfVTTVTable(CodeGenVTables &CGVT, CodeGenModule &CGM, const CXXRecordDecl *MostDerivedClass, const VTTVTable &VTable, llvm::GlobalVariable::LinkageTypes Linkage, VTableLayout::AddressPointsMapTy &AddressPoints)
Definition: CGVTT.cpp:21
clang::VTTBuilder
Class for building VTT layout information.
Definition: VTTBuilder.h:71
clang::VTTVTable::getBase
const CXXRecordDecl * getBase() const
Definition: VTTBuilder.h:44
clang::CodeGen::CGCXXABI::getAddrOfVTable
virtual llvm::GlobalVariable * getAddrOfVTable(const CXXRecordDecl *RD, CharUnits VPtrOffset)=0
Get the address of the vtable for the given record decl which should be used for the vptr at the give...
clang::VTTComponent
Definition: VTTBuilder.h:61
clang::VTableLayout::AddressPointLocation
Definition: VTableBuilder.h:236
clang::Linkage
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
Definition: Linkage.h:23
clang::CodeGen::CodeGenVTables::getSecondaryVirtualPointerIndex
uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD, BaseSubobject Base)
getSecondaryVirtualPointerIndex - Return the index in the VTT where the virtual pointer for the given...
Definition: CGVTT.cpp:154
clang::BaseSubobject
Definition: BaseSubobject.h:30
clang::ArrayType
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:2883
clang::VTTVTable::getBaseOffset
CharUnits getBaseOffset() const
Definition: VTTBuilder.h:48
clang::CodeGen::CodeGenModule::getModule
llvm::Module & getModule() const
Definition: CodeGenModule.h:709
llvm::SmallString
Definition: LLVM.h:37
Base
CodeGenModule.h
clang::VTableLayout::AddressPointLocation::VTableIndex
unsigned VTableIndex
Definition: VTableBuilder.h:237
clang::VTableLayout::AddressPointLocation::AddressPointIndex
unsigned AddressPointIndex
Definition: VTableBuilder.h:237
clang::CodeGen::CodeGenTypeCache::Int32Ty
llvm::IntegerType * Int32Ty
Definition: CodeGenTypeCache.h:37
clang::VTableLayout::AddressPointsMapTy
llvm::DenseMap< BaseSubobject, AddressPointLocation > AddressPointsMapTy
Definition: VTableBuilder.h:240
clang::CodeGen::CodeGenVTables::EmitVTTDefinition
void EmitVTTDefinition(llvm::GlobalVariable *VTT, llvm::GlobalVariable::LinkageTypes Linkage, const CXXRecordDecl *RD)
EmitVTTDefinition - Emit the definition of the given vtable.
Definition: CGVTT.cpp:41
clang::CodeGen::CodeGenModule::getDataLayout
const llvm::DataLayout & getDataLayout() const
Definition: CodeGenModule.h:711
clang::CXXRecordDecl
Represents a C++ struct/union/class.
Definition: DeclCXX.h:255
clang::VTTVTable::isVirtual
bool isVirtual() const
Definition: VTTBuilder.h:52
clang::CXXRecordDecl::getNumVBases
unsigned getNumVBases() const
Retrieves the number of virtual base classes of this class.
Definition: DeclCXX.h:604
clang::VTableLayout::getAddressPoint
AddressPointLocation getAddressPoint(BaseSubobject Base) const
Definition: VTableBuilder.h:280
clang::CharUnits::isZero
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition: CharUnits.h:116
clang::CodeGen::CodeGenModule::getCXXABI
CGCXXABI & getCXXABI() const
Definition: CodeGenModule.h:719
clang::CodeGen::CodeGenModule
This class organizes the cross-function state that is used while generating LLVM code.
Definition: CodeGenModule.h:284
Value
Value
Definition: UninitializedValues.cpp:102
clang::CodeGen::CodeGenVTables::getSubVTTIndex
uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base)
getSubVTTIndex - Return the index of the sub-VTT for the base class of the given record decl.
Definition: CGVTT.cpp:128
clang::CodeGen::CodeGenModule::supportsCOMDAT
bool supportsCOMDAT() const
Definition: TargetInfo.cpp:11079
CGCXXABI.h
clang::VTTVTable
Definition: VTTBuilder.h:32
clang
Definition: CalledOnceCheck.h:17
clang::ExternalLinkage
@ ExternalLinkage
External linkage, which indicates that the entity can be referred to from other translation units.
Definition: Linkage.h:59
clang::ItaniumVTableContext::getVTableLayout
const VTableLayout & getVTableLayout(const CXXRecordDecl *RD)
Definition: VTableBuilder.h:403
clang::CodeGen::CodeGenVTables
Definition: CGVTables.h:32
clang::CharUnits
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
clang::CodeGen::CodeGenVTables::getItaniumVTableContext
ItaniumVTableContext & getItaniumVTableContext()
Definition: CGVTables.h:101
clang::CodeGen::CodeGenModule::CreateOrReplaceCXXRuntimeVariable
llvm::GlobalVariable * CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage, unsigned Alignment)
Will return a global variable of the given type.
Definition: CodeGenModule.cpp:4078
clang::CodeGen::CodeGenModule::setGVProperties
void setGVProperties(llvm::GlobalValue *GV, GlobalDecl GD) const
Set visibility, dllimport/dllexport and dso_local.
Definition: CodeGenModule.cpp:1130
RecordLayout.h