clang 8.0.0

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