blob: 9714bd9d9678b931ae39ebf12823341571bff6e8 [file] [log] [blame]
Anders Carlsson58b7eee2010-01-21 16:50:45 +00001//===--- 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 "clang/AST/RecordLayout.h"
16using namespace clang;
17using namespace CodeGen;
18
19#define D1(x)
20
21namespace {
22class VTTBuilder {
23 /// Inits - The list of values built for the VTT.
24 std::vector<llvm::Constant *> &Inits;
25 /// Class - The most derived class that this vtable is being built for.
26 const CXXRecordDecl *Class;
27 CodeGenModule &CGM; // Per-module state.
28 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
29 /// BLayout - Layout for the most derived class that this vtable is being
30 /// built for.
31 const ASTRecordLayout &BLayout;
32 CGVtableInfo::AddrMap_t &AddressPoints;
33 // vtbl - A pointer to the vtable for Class.
34 llvm::Constant *ClassVtbl;
35 llvm::LLVMContext &VMContext;
36
37 /// SeenVBasesInSecondary - The seen virtual bases when building the
38 /// secondary virtual pointers.
39 llvm::SmallPtrSet<const CXXRecordDecl *, 32> SeenVBasesInSecondary;
40
41 llvm::DenseMap<const CXXRecordDecl *, uint64_t> SubVTTIndicies;
42
43 bool GenerateDefinition;
44
45 llvm::DenseMap<BaseSubobject, llvm::Constant *> CtorVtables;
46 llvm::DenseMap<std::pair<const CXXRecordDecl *, BaseSubobject>, uint64_t>
47 CtorVtableAddressPoints;
48
49 llvm::Constant *getCtorVtable(const BaseSubobject &Base) {
50 if (!GenerateDefinition)
51 return 0;
52
53 llvm::Constant *&CtorVtable = CtorVtables[Base];
54 if (!CtorVtable) {
55 // Build the vtable.
56 CGVtableInfo::CtorVtableInfo Info
57 = CGM.getVtableInfo().getCtorVtable(Class, Base);
58
59 CtorVtable = Info.Vtable;
60
61 // Add the address points for this base.
62 for (CGVtableInfo::AddressPointsMapTy::const_iterator I =
63 Info.AddressPoints.begin(), E = Info.AddressPoints.end();
64 I != E; ++I) {
65 uint64_t &AddressPoint =
66 CtorVtableAddressPoints[std::make_pair(Base.getBase(), I->first)];
67
68 // Check if we already have the address points for this base.
69 if (AddressPoint)
70 break;
71
72 // Otherwise, insert it.
73 AddressPoint = I->second;
74 }
75 }
76
77 return CtorVtable;
78 }
79
80
81 /// BuildVtablePtr - Build up a referene to the given secondary vtable
82 llvm::Constant *BuildVtablePtr(llvm::Constant *Vtable,
83 const CXXRecordDecl *VtableClass,
84 const CXXRecordDecl *RD,
85 uint64_t Offset) {
86 if (!GenerateDefinition)
87 return 0;
88
89 uint64_t AddressPoint;
90
91 if (VtableClass != Class) {
92 // We have a ctor vtable, look for the address point in the ctor vtable
93 // address points.
94 AddressPoint =
95 CtorVtableAddressPoints[std::make_pair(VtableClass,
96 BaseSubobject(RD, Offset))];
97 } else {
98 AddressPoint =
99 (*AddressPoints[VtableClass])[std::make_pair(RD, Offset)];
100 }
101
102 // FIXME: We can never have 0 address point. Do this for now so gepping
103 // retains the same structure. Later we'll just assert.
104 if (AddressPoint == 0)
105 AddressPoint = 1;
106 D1(printf("XXX address point for %s in %s layout %s at offset %d was %d\n",
107 RD->getNameAsCString(), VtblClass->getNameAsCString(),
108 Class->getNameAsCString(), (int)Offset, (int)AddressPoint));
109
110 llvm::Value *Idxs[] = {
111 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 0),
112 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), AddressPoint)
113 };
114
115 llvm::Constant *Init =
116 llvm::ConstantExpr::getInBoundsGetElementPtr(Vtable, Idxs, 2);
117
118 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
119 return llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
120 }
121
122 /// Secondary - Add the secondary vtable pointers to Inits. Offset is the
123 /// current offset in bits to the object we're working on.
124 void Secondary(const CXXRecordDecl *RD, llvm::Constant *vtbl,
125 const CXXRecordDecl *VtblClass, uint64_t Offset=0,
126 bool MorallyVirtual=false) {
127 if (RD->getNumVBases() == 0 && ! MorallyVirtual)
128 return;
129
130 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
131 e = RD->bases_end(); i != e; ++i) {
132 const CXXRecordDecl *Base =
133 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
134
135 // We only want to visit each virtual base once.
136 if (i->isVirtual() && SeenVBasesInSecondary.count(Base))
137 continue;
138
139 // Itanium C++ ABI 2.6.2:
140 // Secondary virtual pointers are present for all bases with either
141 // virtual bases or virtual function declarations overridden along a
142 // virtual path.
143 //
144 // If the base class is not dynamic, we don't want to add it, nor any
145 // of its base classes.
146 if (!Base->isDynamicClass())
147 continue;
148
149 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
150 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
151 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
152 bool NonVirtualPrimaryBase;
153 NonVirtualPrimaryBase = !PrimaryBaseWasVirtual && Base == PrimaryBase;
154 bool BaseMorallyVirtual = MorallyVirtual | i->isVirtual();
155 uint64_t BaseOffset;
156 if (!i->isVirtual()) {
157 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
158 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
159 } else
160 BaseOffset = BLayout.getVBaseClassOffset(Base);
161 llvm::Constant *subvtbl = vtbl;
162 const CXXRecordDecl *subVtblClass = VtblClass;
163 if ((Base->getNumVBases() || BaseMorallyVirtual)
164 && !NonVirtualPrimaryBase) {
165 llvm::Constant *init;
166 if (BaseMorallyVirtual || VtblClass == Class)
167 init = BuildVtablePtr(vtbl, VtblClass, Base, BaseOffset);
168 else {
169 init = getCtorVtable(BaseSubobject(Base, BaseOffset));
170
171 subvtbl = init;
172 subVtblClass = Base;
173
174 init = BuildVtablePtr(init, Class, Base, BaseOffset);
175 }
176
177 Inits.push_back(init);
178 }
179
180 if (i->isVirtual())
181 SeenVBasesInSecondary.insert(Base);
182
183 Secondary(Base, subvtbl, subVtblClass, BaseOffset, BaseMorallyVirtual);
184 }
185 }
186
187 /// BuiltVTT - Add the VTT to Inits. Offset is the offset in bits to the
188 /// currnet object we're working on.
189 void BuildVTT(const CXXRecordDecl *RD, uint64_t Offset, bool MorallyVirtual) {
190 // Itanium C++ ABI 2.6.2:
191 // An array of virtual table addresses, called the VTT, is declared for
192 // each class type that has indirect or direct virtual base classes.
193 if (RD->getNumVBases() == 0)
194 return;
195
196 // Remember the sub-VTT index.
197 SubVTTIndicies[RD] = Inits.size();
198
199 llvm::Constant *Vtable;
200 const CXXRecordDecl *VtableClass;
201
202 // First comes the primary virtual table pointer...
203 if (MorallyVirtual) {
204 Vtable = ClassVtbl;
205 VtableClass = Class;
206 } else {
207 Vtable = getCtorVtable(BaseSubobject(RD, Offset));
208 VtableClass = RD;
209 }
210
211 llvm::Constant *Init = BuildVtablePtr(Vtable, VtableClass, RD, Offset);
212 Inits.push_back(Init);
213
214 // then the secondary VTTs....
215 SecondaryVTTs(RD, Offset, MorallyVirtual);
216
217 // Make sure to clear the set of seen virtual bases.
218 SeenVBasesInSecondary.clear();
219
220 // and last the secondary vtable pointers.
221 Secondary(RD, Vtable, VtableClass, Offset, MorallyVirtual);
222 }
223
224 /// SecondaryVTTs - Add the secondary VTTs to Inits. The secondary VTTs are
225 /// built from each direct non-virtual proper base that requires a VTT in
226 /// declaration order.
227 void SecondaryVTTs(const CXXRecordDecl *RD, uint64_t Offset=0,
228 bool MorallyVirtual=false) {
229 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
230 e = RD->bases_end(); i != e; ++i) {
231 const CXXRecordDecl *Base =
232 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
233 if (i->isVirtual())
234 continue;
235 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
236 uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base);
237
238 BuildVTT(Base, BaseOffset, MorallyVirtual);
239 }
240 }
241
242 /// VirtualVTTs - Add the VTT for each proper virtual base in inheritance
243 /// graph preorder.
244 void VirtualVTTs(const CXXRecordDecl *RD) {
245 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
246 e = RD->bases_end(); i != e; ++i) {
247 const CXXRecordDecl *Base =
248 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
249 if (i->isVirtual() && !SeenVBase.count(Base)) {
250 SeenVBase.insert(Base);
251 uint64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
252 BuildVTT(Base, BaseOffset, false);
253 }
254 VirtualVTTs(Base);
255 }
256 }
257
258public:
259 VTTBuilder(std::vector<llvm::Constant *> &inits, const CXXRecordDecl *c,
260 CodeGenModule &cgm, bool GenerateDefinition)
261 : Inits(inits), Class(c), CGM(cgm),
262 BLayout(cgm.getContext().getASTRecordLayout(c)),
263 AddressPoints(*cgm.getVtableInfo().AddressPoints[c]),
264 VMContext(cgm.getModule().getContext()),
265 GenerateDefinition(GenerateDefinition) {
266
267 // First comes the primary virtual table pointer for the complete class...
268 ClassVtbl = GenerateDefinition ? CGM.getVtableInfo().getVtable(Class) : 0;
269
270 llvm::Constant *Init = BuildVtablePtr(ClassVtbl, Class, Class, 0);
271 Inits.push_back(Init);
272
273 // then the secondary VTTs...
274 SecondaryVTTs(Class);
275
276 // Make sure to clear the set of seen virtual bases.
277 SeenVBasesInSecondary.clear();
278
279 // then the secondary vtable pointers...
280 Secondary(Class, ClassVtbl, Class);
281
282 // and last, the virtual VTTs.
283 VirtualVTTs(Class);
284 }
285
286 llvm::DenseMap<const CXXRecordDecl *, uint64_t> &getSubVTTIndicies() {
287 return SubVTTIndicies;
288 }
289};
290}
291
292llvm::GlobalVariable *
293CGVtableInfo::GenerateVTT(llvm::GlobalVariable::LinkageTypes Linkage,
294 bool GenerateDefinition,
295 const CXXRecordDecl *RD) {
296 // Only classes that have virtual bases need a VTT.
297 if (RD->getNumVBases() == 0)
298 return 0;
299
300 llvm::SmallString<256> OutName;
301 CGM.getMangleContext().mangleCXXVTT(RD, OutName);
302 llvm::StringRef Name = OutName.str();
303
304 D1(printf("vtt %s\n", RD->getNameAsCString()));
305
306 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
307 if (GV == 0 || GV->isDeclaration()) {
308 const llvm::Type *Int8PtrTy =
309 llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
310
311 std::vector<llvm::Constant *> inits;
312 VTTBuilder b(inits, RD, CGM, GenerateDefinition);
313
314 const llvm::ArrayType *Type = llvm::ArrayType::get(Int8PtrTy, inits.size());
315 llvm::Constant *Init = 0;
316 if (GenerateDefinition)
317 Init = llvm::ConstantArray::get(Type, inits);
318
319 llvm::GlobalVariable *OldGV = GV;
320 GV = new llvm::GlobalVariable(CGM.getModule(), Type, /*isConstant=*/true,
321 Linkage, Init, Name);
322 CGM.setGlobalVisibility(GV, RD);
323
324 if (OldGV) {
325 GV->takeName(OldGV);
326 llvm::Constant *NewPtr =
327 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
328 OldGV->replaceAllUsesWith(NewPtr);
329 OldGV->eraseFromParent();
330 }
331 }
332
333 return GV;
334}
335
336CGVtableInfo::CtorVtableInfo
337CGVtableInfo::getCtorVtable(const CXXRecordDecl *RD,
338 const BaseSubobject &Base) {
339 CtorVtableInfo Info;
340
341 Info.Vtable = GenerateVtable(llvm::GlobalValue::InternalLinkage,
342 /*GenerateDefinition=*/true,
343 RD, Base.getBase(), Base.getBaseOffset(),
344 Info.AddressPoints);
345 return Info;
346}
347
348llvm::GlobalVariable *CGVtableInfo::getVTT(const CXXRecordDecl *RD) {
349 return GenerateVTT(llvm::GlobalValue::ExternalLinkage,
350 /*GenerateDefinition=*/false, RD);
351
352}
353
354
355bool CGVtableInfo::needsVTTParameter(GlobalDecl GD) {
356 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
357
358 // We don't have any virtual bases, just return early.
359 if (!MD->getParent()->getNumVBases())
360 return false;
361
362 // Check if we have a base constructor.
363 if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base)
364 return true;
365
366 // Check if we have a base destructor.
367 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
368 return true;
369
370 return false;
371}
372
373uint64_t CGVtableInfo::getSubVTTIndex(const CXXRecordDecl *RD,
374 const CXXRecordDecl *Base) {
375 ClassPairTy ClassPair(RD, Base);
376
377 SubVTTIndiciesTy::iterator I =
378 SubVTTIndicies.find(ClassPair);
379 if (I != SubVTTIndicies.end())
380 return I->second;
381
382 std::vector<llvm::Constant *> inits;
383 VTTBuilder Builder(inits, RD, CGM, /*GenerateDefinition=*/false);
384
385 for (llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
386 Builder.getSubVTTIndicies().begin(),
387 E = Builder.getSubVTTIndicies().end(); I != E; ++I) {
388 // Insert all indices.
389 ClassPairTy ClassPair(RD, I->first);
390
391 SubVTTIndicies.insert(std::make_pair(ClassPair, I->second));
392 }
393
394 I = SubVTTIndicies.find(ClassPair);
395 assert(I != SubVTTIndicies.end() && "Did not find index!");
396
397 return I->second;
398}