1 //===--- CGVTT.cpp - Emit LLVM Code for C++ VTTs --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This contains code dealing with C++ code generation of VTTs (vtable tables).
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenModule.h"
16 #include "clang/AST/RecordLayout.h"
17 using namespace clang;
18 using namespace CodeGen;
24 /// VTT builder - Class for building VTT layout information.
29 /// MostDerivedClass - The most derived class for which we're building this
31 const CXXRecordDecl *MostDerivedClass;
33 typedef llvm::SmallVector<llvm::Constant *, 64> VTTComponentsVectorTy;
35 /// VTTComponents - The VTT components.
36 VTTComponentsVectorTy VTTComponents;
38 /// MostDerivedClassLayout - the AST record layout of the most derived class.
39 const ASTRecordLayout &MostDerivedClassLayout;
41 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
43 typedef llvm::DenseMap<BaseSubobject, uint64_t> AddressPointsMapTy;
45 /// SubVTTIndicies - The sub-VTT indices for the bases of the most derived
47 llvm::DenseMap<BaseSubobject, uint64_t> SubVTTIndicies;
49 /// SecondaryVirtualPointerIndices - The secondary virtual pointer indices of
50 /// all subobjects of the most derived class.
51 llvm::DenseMap<BaseSubobject, uint64_t> SecondaryVirtualPointerIndices;
53 /// GenerateDefinition - Whether the VTT builder should generate LLVM IR for
55 bool GenerateDefinition;
57 /// The linkage to use for any construction vtables required by this VTT.
58 /// Only required if we're building a definition.
59 llvm::GlobalVariable::LinkageTypes LinkageForConstructionVTables;
61 /// GetAddrOfVTable - Returns the address of the vtable for the base class in
62 /// the given vtable class.
64 /// \param AddressPoints - If the returned vtable is a construction vtable,
65 /// this will hold the address points for it.
66 llvm::Constant *GetAddrOfVTable(BaseSubobject Base, bool BaseIsVirtual,
67 AddressPointsMapTy& AddressPoints);
69 /// AddVTablePointer - Add a vtable pointer to the VTT currently being built.
71 /// \param AddressPoints - If the vtable is a construction vtable, this has
72 /// the address points for it.
73 void AddVTablePointer(BaseSubobject Base, llvm::Constant *VTable,
74 const CXXRecordDecl *VTableClass,
75 const AddressPointsMapTy& AddressPoints);
77 /// LayoutSecondaryVTTs - Lay out the secondary VTTs of the given base
79 void LayoutSecondaryVTTs(BaseSubobject Base);
81 /// LayoutSecondaryVirtualPointers - Lay out the secondary virtual pointers
82 /// for the given base subobject.
84 /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
85 /// or a direct or indirect base of a virtual base.
87 /// \param AddressPoints - If the vtable is a construction vtable, this has
88 /// the address points for it.
89 void LayoutSecondaryVirtualPointers(BaseSubobject Base,
90 bool BaseIsMorallyVirtual,
91 llvm::Constant *VTable,
92 const CXXRecordDecl *VTableClass,
93 const AddressPointsMapTy& AddressPoints,
94 VisitedVirtualBasesSetTy &VBases);
96 /// LayoutSecondaryVirtualPointers - Lay out the secondary virtual pointers
97 /// for the given base subobject.
99 /// \param AddressPoints - If the vtable is a construction vtable, this has
100 /// the address points for it.
101 void LayoutSecondaryVirtualPointers(BaseSubobject Base,
102 llvm::Constant *VTable,
103 const AddressPointsMapTy& AddressPoints);
105 /// LayoutVirtualVTTs - Lay out the VTTs for the virtual base classes of the
106 /// given record decl.
107 void LayoutVirtualVTTs(const CXXRecordDecl *RD,
108 VisitedVirtualBasesSetTy &VBases);
110 /// LayoutVTT - Will lay out the VTT for the given subobject, including any
111 /// secondary VTTs, secondary virtual pointers and virtual VTTs.
112 void LayoutVTT(BaseSubobject Base, bool BaseIsVirtual);
115 VTTBuilder(CodeGenModule &CGM, const CXXRecordDecl *MostDerivedClass,
116 bool GenerateDefinition,
117 llvm::GlobalVariable::LinkageTypes LinkageForConstructionVTables
118 = (llvm::GlobalVariable::LinkageTypes) -1);
120 // getVTTComponents - Returns a reference to the VTT components.
121 const VTTComponentsVectorTy &getVTTComponents() const {
122 return VTTComponents;
125 /// getSubVTTIndicies - Returns a reference to the sub-VTT indices.
126 const llvm::DenseMap<BaseSubobject, uint64_t> &getSubVTTIndicies() const {
127 return SubVTTIndicies;
130 /// getSecondaryVirtualPointerIndices - Returns a reference to the secondary
131 /// virtual pointer indices.
132 const llvm::DenseMap<BaseSubobject, uint64_t> &
133 getSecondaryVirtualPointerIndices() const {
134 return SecondaryVirtualPointerIndices;
139 VTTBuilder::VTTBuilder(CodeGenModule &CGM,
140 const CXXRecordDecl *MostDerivedClass,
141 bool GenerateDefinition,
142 llvm::GlobalVariable::LinkageTypes LinkageForConstructionVTables)
143 : CGM(CGM), MostDerivedClass(MostDerivedClass),
144 MostDerivedClassLayout(CGM.getContext().getASTRecordLayout(MostDerivedClass)),
145 GenerateDefinition(GenerateDefinition),
146 LinkageForConstructionVTables(LinkageForConstructionVTables) {
147 assert(!GenerateDefinition ||
148 LinkageForConstructionVTables
149 != (llvm::GlobalVariable::LinkageTypes) -1);
152 LayoutVTT(BaseSubobject(MostDerivedClass, CharUnits::Zero()),
153 /*BaseIsVirtual=*/false);
157 VTTBuilder::GetAddrOfVTable(BaseSubobject Base, bool BaseIsVirtual,
158 AddressPointsMapTy& AddressPoints) {
159 if (!GenerateDefinition)
162 if (Base.getBase() == MostDerivedClass) {
163 assert(Base.getBaseOffset().isZero() &&
164 "Most derived class vtable must have a zero offset!");
165 // This is a regular vtable.
166 return CGM.getVTables().GetAddrOfVTable(MostDerivedClass);
169 return CGM.getVTables().GenerateConstructionVTable(MostDerivedClass,
171 LinkageForConstructionVTables,
175 void VTTBuilder::AddVTablePointer(BaseSubobject Base, llvm::Constant *VTable,
176 const CXXRecordDecl *VTableClass,
177 const AddressPointsMapTy& AddressPoints) {
178 // Store the vtable pointer index if we're generating the primary VTT.
179 if (VTableClass == MostDerivedClass) {
180 assert(!SecondaryVirtualPointerIndices.count(Base) &&
181 "A virtual pointer index already exists for this base subobject!");
182 SecondaryVirtualPointerIndices[Base] = VTTComponents.size();
185 if (!GenerateDefinition) {
186 VTTComponents.push_back(0);
190 uint64_t AddressPoint;
191 if (VTableClass != MostDerivedClass) {
192 // The vtable is a construction vtable, look in the construction vtable
194 AddressPoint = AddressPoints.lookup(Base);
195 assert(AddressPoint != 0 && "Did not find ctor vtable address point!");
197 // Just get the address point for the regular vtable.
198 AddressPoint = CGM.getVTables().getAddressPoint(Base, VTableClass);
199 assert(AddressPoint != 0 && "Did not find vtable address point!");
202 if (!AddressPoint) AddressPoint = 0;
204 llvm::Value *Idxs[] = {
205 llvm::ConstantInt::get(llvm::Type::getInt64Ty(CGM.getLLVMContext()), 0),
206 llvm::ConstantInt::get(llvm::Type::getInt64Ty(CGM.getLLVMContext()),
210 llvm::Constant *Init =
211 llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Idxs, 2);
213 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
214 Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
216 VTTComponents.push_back(Init);
219 void VTTBuilder::LayoutSecondaryVTTs(BaseSubobject Base) {
220 const CXXRecordDecl *RD = Base.getBase();
222 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
223 E = RD->bases_end(); I != E; ++I) {
225 // Don't layout virtual bases.
229 const CXXRecordDecl *BaseDecl =
230 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
232 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
233 CharUnits BaseOffset = Base.getBaseOffset() +
234 Layout.getBaseClassOffset(BaseDecl);
236 // Layout the VTT for this base.
237 LayoutVTT(BaseSubobject(BaseDecl, BaseOffset), /*BaseIsVirtual=*/false);
242 VTTBuilder::LayoutSecondaryVirtualPointers(BaseSubobject Base,
243 bool BaseIsMorallyVirtual,
244 llvm::Constant *VTable,
245 const CXXRecordDecl *VTableClass,
246 const AddressPointsMapTy& AddressPoints,
247 VisitedVirtualBasesSetTy &VBases) {
248 const CXXRecordDecl *RD = Base.getBase();
250 // We're not interested in bases that don't have virtual bases, and not
251 // morally virtual bases.
252 if (!RD->getNumVBases() && !BaseIsMorallyVirtual)
255 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
256 E = RD->bases_end(); I != E; ++I) {
257 const CXXRecordDecl *BaseDecl =
258 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
260 // Itanium C++ ABI 2.6.2:
261 // Secondary virtual pointers are present for all bases with either
262 // virtual bases or virtual function declarations overridden along a
265 // If the base class is not dynamic, we don't want to add it, nor any
266 // of its base classes.
267 if (!BaseDecl->isDynamicClass())
270 bool BaseDeclIsMorallyVirtual = BaseIsMorallyVirtual;
271 bool BaseDeclIsNonVirtualPrimaryBase = false;
272 CharUnits BaseOffset;
273 if (I->isVirtual()) {
274 // Ignore virtual bases that we've already visited.
275 if (!VBases.insert(BaseDecl))
278 BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
279 BaseDeclIsMorallyVirtual = true;
281 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
283 BaseOffset = Base.getBaseOffset() +
284 Layout.getBaseClassOffset(BaseDecl);
286 if (!Layout.isPrimaryBaseVirtual() &&
287 Layout.getPrimaryBase() == BaseDecl)
288 BaseDeclIsNonVirtualPrimaryBase = true;
291 // Itanium C++ ABI 2.6.2:
292 // Secondary virtual pointers: for each base class X which (a) has virtual
293 // bases or is reachable along a virtual path from D, and (b) is not a
294 // non-virtual primary base, the address of the virtual table for X-in-D
295 // or an appropriate construction virtual table.
296 if (!BaseDeclIsNonVirtualPrimaryBase &&
297 (BaseDecl->getNumVBases() || BaseDeclIsMorallyVirtual)) {
298 // Add the vtable pointer.
299 AddVTablePointer(BaseSubobject(BaseDecl, BaseOffset), VTable,
300 VTableClass, AddressPoints);
303 // And lay out the secondary virtual pointers for the base class.
304 LayoutSecondaryVirtualPointers(BaseSubobject(BaseDecl, BaseOffset),
305 BaseDeclIsMorallyVirtual, VTable,
306 VTableClass, AddressPoints, VBases);
311 VTTBuilder::LayoutSecondaryVirtualPointers(BaseSubobject Base,
312 llvm::Constant *VTable,
313 const AddressPointsMapTy& AddressPoints) {
314 VisitedVirtualBasesSetTy VBases;
315 LayoutSecondaryVirtualPointers(Base, /*BaseIsMorallyVirtual=*/false,
316 VTable, Base.getBase(), AddressPoints, VBases);
319 void VTTBuilder::LayoutVirtualVTTs(const CXXRecordDecl *RD,
320 VisitedVirtualBasesSetTy &VBases) {
321 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
322 E = RD->bases_end(); I != E; ++I) {
323 const CXXRecordDecl *BaseDecl =
324 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
326 // Check if this is a virtual base.
327 if (I->isVirtual()) {
328 // Check if we've seen this base before.
329 if (!VBases.insert(BaseDecl))
332 CharUnits BaseOffset =
333 MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
335 LayoutVTT(BaseSubobject(BaseDecl, BaseOffset), /*BaseIsVirtual=*/true);
338 // We only need to layout virtual VTTs for this base if it actually has
340 if (BaseDecl->getNumVBases())
341 LayoutVirtualVTTs(BaseDecl, VBases);
345 void VTTBuilder::LayoutVTT(BaseSubobject Base, bool BaseIsVirtual) {
346 const CXXRecordDecl *RD = Base.getBase();
348 // Itanium C++ ABI 2.6.2:
349 // An array of virtual table addresses, called the VTT, is declared for
350 // each class type that has indirect or direct virtual base classes.
351 if (RD->getNumVBases() == 0)
354 bool IsPrimaryVTT = Base.getBase() == MostDerivedClass;
357 // Remember the sub-VTT index.
358 SubVTTIndicies[Base] = VTTComponents.size();
361 AddressPointsMapTy AddressPoints;
362 llvm::Constant *VTable = GetAddrOfVTable(Base, BaseIsVirtual, AddressPoints);
364 // Add the primary vtable pointer.
365 AddVTablePointer(Base, VTable, RD, AddressPoints);
367 // Add the secondary VTTs.
368 LayoutSecondaryVTTs(Base);
370 // Add the secondary virtual pointers.
371 LayoutSecondaryVirtualPointers(Base, VTable, AddressPoints);
373 // If this is the primary VTT, we want to lay out virtual VTTs as well.
375 VisitedVirtualBasesSetTy VBases;
376 LayoutVirtualVTTs(Base.getBase(), VBases);
383 CodeGenVTables::EmitVTTDefinition(llvm::GlobalVariable *VTT,
384 llvm::GlobalVariable::LinkageTypes Linkage,
385 const CXXRecordDecl *RD) {
386 VTTBuilder Builder(CGM, RD, /*GenerateDefinition=*/true, Linkage);
388 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
389 const llvm::ArrayType *ArrayType =
390 llvm::ArrayType::get(Int8PtrTy, Builder.getVTTComponents().size());
392 llvm::Constant *Init =
393 llvm::ConstantArray::get(ArrayType, Builder.getVTTComponents());
395 VTT->setInitializer(Init);
397 // Set the correct linkage.
398 VTT->setLinkage(Linkage);
400 // Set the right visibility.
401 CGM.setTypeVisibility(VTT, RD, CodeGenModule::TVK_ForVTT);
404 llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) {
405 assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT");
407 llvm::SmallString<256> OutName;
408 llvm::raw_svector_ostream Out(OutName);
409 CGM.getCXXABI().getMangleContext().mangleCXXVTT(RD, Out);
411 llvm::StringRef Name = OutName.str();
413 ComputeVTableRelatedInformation(RD, /*VTableRequired=*/true);
415 VTTBuilder Builder(CGM, RD, /*GenerateDefinition=*/false);
417 const llvm::Type *Int8PtrTy =
418 llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
419 const llvm::ArrayType *ArrayType =
420 llvm::ArrayType::get(Int8PtrTy, Builder.getVTTComponents().size());
422 llvm::GlobalVariable *GV =
423 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType,
424 llvm::GlobalValue::ExternalLinkage);
425 GV->setUnnamedAddr(true);
429 bool CodeGenVTables::needsVTTParameter(GlobalDecl GD) {
430 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
432 // We don't have any virtual bases, just return early.
433 if (!MD->getParent()->getNumVBases())
436 // Check if we have a base constructor.
437 if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base)
440 // Check if we have a base destructor.
441 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
447 uint64_t CodeGenVTables::getSubVTTIndex(const CXXRecordDecl *RD,
448 BaseSubobject Base) {
449 BaseSubobjectPairTy ClassSubobjectPair(RD, Base);
451 SubVTTIndiciesMapTy::iterator I = SubVTTIndicies.find(ClassSubobjectPair);
452 if (I != SubVTTIndicies.end())
455 VTTBuilder Builder(CGM, RD, /*GenerateDefinition=*/false);
457 for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
458 Builder.getSubVTTIndicies().begin(),
459 E = Builder.getSubVTTIndicies().end(); I != E; ++I) {
460 // Insert all indices.
461 BaseSubobjectPairTy ClassSubobjectPair(RD, I->first);
463 SubVTTIndicies.insert(std::make_pair(ClassSubobjectPair, I->second));
466 I = SubVTTIndicies.find(ClassSubobjectPair);
467 assert(I != SubVTTIndicies.end() && "Did not find index!");
473 CodeGenVTables::getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
474 BaseSubobject Base) {
475 SecondaryVirtualPointerIndicesMapTy::iterator I =
476 SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
478 if (I != SecondaryVirtualPointerIndices.end())
481 VTTBuilder Builder(CGM, RD, /*GenerateDefinition=*/false);
483 // Insert all secondary vpointer indices.
484 for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
485 Builder.getSecondaryVirtualPointerIndices().begin(),
486 E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; ++I) {
487 std::pair<const CXXRecordDecl *, BaseSubobject> Pair =
488 std::make_pair(RD, I->first);
490 SecondaryVirtualPointerIndices.insert(std::make_pair(Pair, I->second));
493 I = SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
494 assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!");