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"
17using namespace clang;
18using namespace CodeGen;
19
20static llvm::GlobalVariable *
21GetAddrOfVTTVTable(CodeGenVTables &CGVT, CodeGenModule &CGM,
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(RD: MostDerivedClass, VPtrOffset: CharUnits());
31 }
32
33 return CGVT.GenerateConstructionVTable(RD: MostDerivedClass,
34 Base: VTable.getBaseSubobject(),
35 BaseIsVirtual: 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 ElementType: CGM.GlobalsInt8PtrTy, NumElements: Builder.getVTTComponents().size());
47
48 SmallVector<llvm::GlobalVariable *, 8> VTables;
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(Elt: VTableAddressPointsMapTy());
53 VTables.push_back(Elt: GetAddrOfVTTVTable(CGVT&: *this, CGM, MostDerivedClass: RD, VTable: *i, Linkage,
54 AddressPoints&: VTableAddressPoints.back()));
55 }
56
57 SmallVector<llvm::Constant *, 8> VTTComponents;
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];
62 VTableLayout::AddressPointLocation AddressPoint;
63 if (VTTVT.getBase() == RD) {
64 // Just get the address point for the regular vtable.
65 AddressPoint =
66 getItaniumVTableContext().getVTableLayout(RD).getAddressPoint(
67 Base: i->VTableBase);
68 } else {
69 AddressPoint = VTableAddressPoints[i->VTableIndex].lookup(Val: 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(Ty: CGM.Int32Ty, V: 0),
76 llvm::ConstantInt::get(Ty: CGM.Int32Ty, V: AddressPoint.VTableIndex),
77 llvm::ConstantInt::get(Ty: CGM.Int32Ty, V: AddressPoint.AddressPointIndex),
78 };
79
80 // Add inrange attribute to indicate that only the VTableIndex can be
81 // accessed.
82 unsigned ComponentSize =
83 CGM.getDataLayout().getTypeAllocSize(Ty: getVTableComponentType());
84 unsigned VTableSize = CGM.getDataLayout().getTypeAllocSize(
85 Ty: cast<llvm::StructType>(Val: VTable->getValueType())
86 ->getElementType(N: AddressPoint.VTableIndex));
87 unsigned Offset = ComponentSize * AddressPoint.AddressPointIndex;
88 llvm::ConstantRange InRange(llvm::APInt(32, -Offset, true),
89 llvm::APInt(32, VTableSize - Offset, true));
90 llvm::Constant *Init = llvm::ConstantExpr::getGetElementPtr(
91 Ty: VTable->getValueType(), C: VTable, IdxList: Idxs, /*InBounds=*/true, InRange);
92
93 VTTComponents.push_back(Elt: Init);
94 }
95
96 llvm::Constant *Init = llvm::ConstantArray::get(T: ArrayType, V: VTTComponents);
97
98 VTT->setInitializer(Init);
99
100 // Set the correct linkage.
101 VTT->setLinkage(Linkage);
102
103 if (CGM.supportsCOMDAT() && VTT->isWeakForLinker())
104 VTT->setComdat(CGM.getModule().getOrInsertComdat(Name: VTT->getName()));
105
106 // Set the visibility. This will already have been set on the VTT declaration.
107 // Set it again, now that we have a definition, as the implicit visibility can
108 // apply differently to definitions.
109 CGM.setGVProperties(VTT, RD);
110}
111
112llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) {
113 assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT");
114
115 SmallString<256> OutName;
116 llvm::raw_svector_ostream Out(OutName);
117 cast<ItaniumMangleContext>(Val&: CGM.getCXXABI().getMangleContext())
118 .mangleCXXVTT(RD, Out);
119 StringRef Name = OutName.str();
120
121 // This will also defer the definition of the VTT.
122 (void) CGM.getCXXABI().getAddrOfVTable(RD, VPtrOffset: CharUnits());
123
124 VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
125
126 llvm::ArrayType *ArrayType = llvm::ArrayType::get(
127 ElementType: CGM.GlobalsInt8PtrTy, NumElements: Builder.getVTTComponents().size());
128 llvm::Align Align = CGM.getDataLayout().getABITypeAlign(Ty: CGM.GlobalsInt8PtrTy);
129
130 llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable(
131 Name, Ty: ArrayType, Linkage: llvm::GlobalValue::ExternalLinkage, Alignment: Align);
132 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
133 CGM.setGVProperties(GV, RD);
134 return GV;
135}
136
137uint64_t CodeGenVTables::getSubVTTIndex(const CXXRecordDecl *RD,
138 BaseSubobject Base) {
139 BaseSubobjectPairTy ClassSubobjectPair(RD, Base);
140
141 SubVTTIndiciesMapTy::iterator I = SubVTTIndicies.find(Val: ClassSubobjectPair);
142 if (I != SubVTTIndicies.end())
143 return I->second;
144
145 VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
146
147 for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
148 Builder.getSubVTTIndicies().begin(),
149 E = Builder.getSubVTTIndicies().end(); I != E; ++I) {
150 // Insert all indices.
151 BaseSubobjectPairTy ClassSubobjectPair(RD, I->first);
152
153 SubVTTIndicies.insert(KV: std::make_pair(x&: ClassSubobjectPair, y: I->second));
154 }
155
156 I = SubVTTIndicies.find(Val: ClassSubobjectPair);
157 assert(I != SubVTTIndicies.end() && "Did not find index!");
158
159 return I->second;
160}
161
162uint64_t
163CodeGenVTables::getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
164 BaseSubobject Base) {
165 SecondaryVirtualPointerIndicesMapTy::iterator I =
166 SecondaryVirtualPointerIndices.find(Val: std::make_pair(x&: RD, y&: Base));
167
168 if (I != SecondaryVirtualPointerIndices.end())
169 return I->second;
170
171 VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
172
173 // Insert all secondary vpointer indices.
174 for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
175 Builder.getSecondaryVirtualPointerIndices().begin(),
176 E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; ++I) {
177 std::pair<const CXXRecordDecl *, BaseSubobject> Pair =
178 std::make_pair(x&: RD, y: I->first);
179
180 SecondaryVirtualPointerIndices.insert(KV: std::make_pair(x&: Pair, y: I->second));
181 }
182
183 I = SecondaryVirtualPointerIndices.find(Val: std::make_pair(x&: RD, y&: Base));
184 assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!");
185
186 return I->second;
187}
188

source code of clang/lib/CodeGen/CGVTT.cpp