clang 24.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"
17using namespace clang;
18using namespace CodeGen;
19
20static 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
40void
41CodeGenVTables::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 = llvm::ArrayType::get(
46 CGM.GlobalsInt8PtrTy, Builder.getVTTComponents().size());
47
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 auto &[Idx, C] : llvm::enumerate(Builder.getVTTComponents())) {
59 const VTTVTable &VTTVT = Builder.getVTTVTables()[C.VTableIndex];
60 llvm::GlobalVariable *VTable = VTables[C.VTableIndex];
62 if (VTTVT.getBase() == RD) {
63 // Just get the address point for the regular vtable.
64 AddressPoint =
66 C.VTableBase);
67 } else {
68 AddressPoint = VTableAddressPoints[C.VTableIndex].lookup(C.VTableBase);
69 assert(AddressPoint.AddressPointIndex != 0 &&
70 "Did not find ctor vtable address point!");
71 }
72
73 llvm::Constant *Idxs[] = {
74 llvm::ConstantInt::get(CGM.Int32Ty, 0),
75 llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.VTableIndex),
76 llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.AddressPointIndex),
77 };
78
79 // Add inrange attribute to indicate that only the VTableIndex can be
80 // accessed.
81 unsigned ComponentSize =
82 CGM.getDataLayout().getTypeAllocSize(getVTableComponentType());
83 unsigned VTableSize = CGM.getDataLayout().getTypeAllocSize(
84 cast<llvm::StructType>(VTable->getValueType())
85 ->getElementType(AddressPoint.VTableIndex));
86 unsigned Offset = ComponentSize * AddressPoint.AddressPointIndex;
87 llvm::ConstantRange InRange(
88 llvm::APInt(32, (int)-Offset, true),
89 llvm::APInt(32, (int)(VTableSize - Offset), true));
90 llvm::Constant *Init = llvm::ConstantExpr::getGetElementPtr(
91 CGM.getDataLayout(), VTable->getValueType(), VTable, Idxs,
92 llvm::GEPNoWrapFlags::inBounds(), InRange);
93
94 if (auto PAuthQual =
95 CGM.getVTablePointerAuthentication(VTTVT.getBase(),
96 /*IsVTTEntry=*/true)) {
97 llvm::Constant *Address = nullptr;
98 if (PAuthQual->isAddressDiscriminated())
99 Address = llvm::ConstantExpr::getGetElementPtr(
100 CGM.getDataLayout(), VTT->getType(), VTT,
101 llvm::ConstantInt::get(CGM.Int32Ty, Idx));
102 auto *Discriminator = llvm::ConstantInt::get(
103 CGM.IntPtrTy, PAuthQual->getExtraDiscriminator());
104 Init = CGM.getConstantSignedPointer(Init, PAuthQual->getKey(), Address,
105 Discriminator);
106 }
107 VTTComponents.push_back(Init);
108 }
109
110 llvm::Constant *Init = llvm::ConstantArray::get(ArrayType, VTTComponents);
111
112 VTT->setInitializer(Init);
113
114 // Set the correct linkage.
115 VTT->setLinkage(Linkage);
116
117 if (CGM.supportsCOMDAT() && VTT->isWeakForLinker())
118 VTT->setComdat(CGM.getModule().getOrInsertComdat(VTT->getName()));
119
120 // Set the visibility. This will already have been set on the VTT declaration.
121 // Set it again, now that we have a definition, as the implicit visibility can
122 // apply differently to definitions.
123 CGM.setGVProperties(VTT, RD);
124}
125
126llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) {
127 assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT");
128
129 SmallString<256> OutName;
130 llvm::raw_svector_ostream Out(OutName);
131 cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
132 .mangleCXXVTT(RD, Out);
133 StringRef Name = OutName.str();
134
135 // This will also defer the definition of the VTT.
136 (void) CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
137
138 VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
139
140 llvm::ArrayType *ArrayType = llvm::ArrayType::get(
141 CGM.GlobalsInt8PtrTy, Builder.getVTTComponents().size());
142 llvm::Align Align = CGM.getDataLayout().getABITypeAlign(CGM.GlobalsInt8PtrTy);
143
144 llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable(
145 Name, ArrayType, llvm::GlobalValue::ExternalLinkage, Align);
146 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
147 CGM.setGVProperties(GV, RD);
148 return GV;
149}
150
153 BaseSubobjectPairTy ClassSubobjectPair(RD, Base);
154
155 SubVTTIndicesMapTy::iterator I = SubVTTIndices.find(ClassSubobjectPair);
156 if (I != SubVTTIndices.end())
157 return I->second;
158
159 VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
160
161 for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator
162 I = Builder.getSubVTTIndices().begin(),
163 E = Builder.getSubVTTIndices().end();
164 I != E; ++I) {
165 // Insert all indices.
166 BaseSubobjectPairTy ClassSubobjectPair(RD, I->first);
167
168 SubVTTIndices.insert(std::make_pair(ClassSubobjectPair, I->second));
169 }
170
171 I = SubVTTIndices.find(ClassSubobjectPair);
172 assert(I != SubVTTIndices.end() && "Did not find index!");
173
174 return I->second;
175}
176
177uint64_t
180 SecondaryVirtualPointerIndicesMapTy::iterator I =
181 SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
182
183 if (I != SecondaryVirtualPointerIndices.end())
184 return I->second;
185
186 VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
187
188 // Insert all secondary vpointer indices.
189 for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
190 Builder.getSecondaryVirtualPointerIndices().begin(),
191 E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; ++I) {
192 std::pair<const CXXRecordDecl *, BaseSubobject> Pair =
193 std::make_pair(RD, I->first);
194
195 SecondaryVirtualPointerIndices.insert(std::make_pair(Pair, I->second));
196 }
197
198 I = SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
199 assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!");
200
201 return I->second;
202}
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
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition TypeBase.h:3813
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
unsigned getNumVBases() const
Retrieves the number of virtual base classes of this class.
Definition DeclCXX.h:624
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition CharUnits.h:122
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
Definition Address.h:128
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...
This class organizes the cross-function state that is used while generating LLVM code.
llvm::GlobalVariable * GetAddrOfVTT(const CXXRecordDecl *RD)
GetAddrOfVTT - Get the address of the VTT for the given record decl.
Definition CGVTT.cpp:126
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:151
ItaniumVTableContext & getItaniumVTableContext()
Definition CGVTables.h:87
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.
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:178
llvm::Type * getVTableComponentType() const
Return the type used as components for a vtable.
void EmitVTTDefinition(llvm::GlobalVariable *VTT, llvm::GlobalVariable::LinkageTypes Linkage, const CXXRecordDecl *RD)
EmitVTTDefinition - Emit the definition of the given vtable.
Definition CGVTT.cpp:41
const VTableLayout & getVTableLayout(const CXXRecordDecl *RD)
Class for building VTT layout information.
Definition VTTBuilder.h:71
CharUnits getBaseOffset() const
Definition VTTBuilder.h:48
bool isVirtual() const
Definition VTTBuilder.h:52
const CXXRecordDecl * getBase() const
Definition VTTBuilder.h:44
BaseSubobject getBaseSubobject() const
Definition VTTBuilder.h:56
llvm::DenseMap< BaseSubobject, AddressPointLocation > AddressPointsMapTy
AddressPointLocation getAddressPoint(BaseSubobject Base) const
Top level wrappers for InstallAPI frontend operations.
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
Definition Linkage.h:24
U cast(CodeGen::Address addr)
Definition Address.h:327