1 //===--- CGVTables.cpp - Emit LLVM Code for C++ vtables -------------------===//
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 virtual tables.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "CodeGenModule.h"
15 #include "CodeGenFunction.h"
16 #include "CGCXXABI.h"
17 #include "clang/AST/CXXInheritance.h"
18 #include "clang/AST/RecordLayout.h"
19 #include "clang/Frontend/CodeGenOptions.h"
20 #include "llvm/ADT/DenseSet.h"
21 #include "llvm/ADT/SetVector.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/Format.h"
24 #include "llvm/Transforms/Utils/Cloning.h"
25 #include <algorithm>
26 #include <cstdio>
27
28 using namespace clang;
29 using namespace CodeGen;
30
CodeGenVTables(CodeGenModule & CGM)31 CodeGenVTables::CodeGenVTables(CodeGenModule &CGM)
32 : CGM(CGM), VTContext(CGM.getContext()) { }
33
ShouldEmitVTableInThisTU(const CXXRecordDecl * RD)34 bool CodeGenVTables::ShouldEmitVTableInThisTU(const CXXRecordDecl *RD) {
35 assert(RD->isDynamicClass() && "Non dynamic classes have no VTable.");
36
37 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
38 if (TSK == TSK_ExplicitInstantiationDeclaration)
39 return false;
40
41 const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD);
42 if (!KeyFunction)
43 return true;
44
45 // Itanium C++ ABI, 5.2.6 Instantiated Templates:
46 // An instantiation of a class template requires:
47 // - In the object where instantiated, the virtual table...
48 if (TSK == TSK_ImplicitInstantiation ||
49 TSK == TSK_ExplicitInstantiationDefinition)
50 return true;
51
52 // If we're building with optimization, we always emit VTables since that
53 // allows for virtual function calls to be devirtualized.
54 // (We don't want to do this in -fapple-kext mode however).
55 if (CGM.getCodeGenOpts().OptimizationLevel && !CGM.getLangOpts().AppleKext)
56 return true;
57
58 return KeyFunction->hasBody();
59 }
60
GetAddrOfThunk(GlobalDecl GD,const ThunkInfo & Thunk)61 llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
62 const ThunkInfo &Thunk) {
63 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
64
65 // Compute the mangled name.
66 SmallString<256> Name;
67 llvm::raw_svector_ostream Out(Name);
68 if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
69 getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(),
70 Thunk.This, Out);
71 else
72 getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out);
73 Out.flush();
74
75 llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD);
76 return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true);
77 }
78
PerformTypeAdjustment(CodeGenFunction & CGF,llvm::Value * Ptr,int64_t NonVirtualAdjustment,int64_t VirtualAdjustment)79 static llvm::Value *PerformTypeAdjustment(CodeGenFunction &CGF,
80 llvm::Value *Ptr,
81 int64_t NonVirtualAdjustment,
82 int64_t VirtualAdjustment) {
83 if (!NonVirtualAdjustment && !VirtualAdjustment)
84 return Ptr;
85
86 llvm::Type *Int8PtrTy = CGF.Int8PtrTy;
87 llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy);
88
89 if (NonVirtualAdjustment) {
90 // Do the non-virtual adjustment.
91 V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment);
92 }
93
94 if (VirtualAdjustment) {
95 llvm::Type *PtrDiffTy =
96 CGF.ConvertType(CGF.getContext().getPointerDiffType());
97
98 // Do the virtual adjustment.
99 llvm::Value *VTablePtrPtr =
100 CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo());
101
102 llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr);
103
104 llvm::Value *OffsetPtr =
105 CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
106
107 OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo());
108
109 // Load the adjustment offset from the vtable.
110 llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr);
111
112 // Adjust our pointer.
113 V = CGF.Builder.CreateInBoundsGEP(V, Offset);
114 }
115
116 // Cast back to the original type.
117 return CGF.Builder.CreateBitCast(V, Ptr->getType());
118 }
119
setThunkVisibility(CodeGenModule & CGM,const CXXMethodDecl * MD,const ThunkInfo & Thunk,llvm::Function * Fn)120 static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD,
121 const ThunkInfo &Thunk, llvm::Function *Fn) {
122 CGM.setGlobalVisibility(Fn, MD);
123
124 if (!CGM.getCodeGenOpts().HiddenWeakVTables)
125 return;
126
127 // If the thunk has weak/linkonce linkage, but the function must be
128 // emitted in every translation unit that references it, then we can
129 // emit its thunks with hidden visibility, since its thunks must be
130 // emitted when the function is.
131
132 // This follows CodeGenModule::setTypeVisibility; see the comments
133 // there for explanation.
134
135 if ((Fn->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage &&
136 Fn->getLinkage() != llvm::GlobalVariable::WeakODRLinkage) ||
137 Fn->getVisibility() != llvm::GlobalVariable::DefaultVisibility)
138 return;
139
140 if (MD->getExplicitVisibility())
141 return;
142
143 switch (MD->getTemplateSpecializationKind()) {
144 case TSK_ExplicitInstantiationDefinition:
145 case TSK_ExplicitInstantiationDeclaration:
146 return;
147
148 case TSK_Undeclared:
149 break;
150
151 case TSK_ExplicitSpecialization:
152 case TSK_ImplicitInstantiation:
153 if (!CGM.getCodeGenOpts().HiddenWeakTemplateVTables)
154 return;
155 break;
156 }
157
158 // If there's an explicit definition, and that definition is
159 // out-of-line, then we can't assume that all users will have a
160 // definition to emit.
161 const FunctionDecl *Def = 0;
162 if (MD->hasBody(Def) && Def->isOutOfLine())
163 return;
164
165 Fn->setVisibility(llvm::GlobalValue::HiddenVisibility);
166 }
167
168 #ifndef NDEBUG
similar(const ABIArgInfo & infoL,CanQualType typeL,const ABIArgInfo & infoR,CanQualType typeR)169 static bool similar(const ABIArgInfo &infoL, CanQualType typeL,
170 const ABIArgInfo &infoR, CanQualType typeR) {
171 return (infoL.getKind() == infoR.getKind() &&
172 (typeL == typeR ||
173 (isa<PointerType>(typeL) && isa<PointerType>(typeR)) ||
174 (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR))));
175 }
176 #endif
177
PerformReturnAdjustment(CodeGenFunction & CGF,QualType ResultType,RValue RV,const ThunkInfo & Thunk)178 static RValue PerformReturnAdjustment(CodeGenFunction &CGF,
179 QualType ResultType, RValue RV,
180 const ThunkInfo &Thunk) {
181 // Emit the return adjustment.
182 bool NullCheckValue = !ResultType->isReferenceType();
183
184 llvm::BasicBlock *AdjustNull = 0;
185 llvm::BasicBlock *AdjustNotNull = 0;
186 llvm::BasicBlock *AdjustEnd = 0;
187
188 llvm::Value *ReturnValue = RV.getScalarVal();
189
190 if (NullCheckValue) {
191 AdjustNull = CGF.createBasicBlock("adjust.null");
192 AdjustNotNull = CGF.createBasicBlock("adjust.notnull");
193 AdjustEnd = CGF.createBasicBlock("adjust.end");
194
195 llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue);
196 CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
197 CGF.EmitBlock(AdjustNotNull);
198 }
199
200 ReturnValue = PerformTypeAdjustment(CGF, ReturnValue,
201 Thunk.Return.NonVirtual,
202 Thunk.Return.VBaseOffsetOffset);
203
204 if (NullCheckValue) {
205 CGF.Builder.CreateBr(AdjustEnd);
206 CGF.EmitBlock(AdjustNull);
207 CGF.Builder.CreateBr(AdjustEnd);
208 CGF.EmitBlock(AdjustEnd);
209
210 llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2);
211 PHI->addIncoming(ReturnValue, AdjustNotNull);
212 PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
213 AdjustNull);
214 ReturnValue = PHI;
215 }
216
217 return RValue::get(ReturnValue);
218 }
219
220 // This function does roughly the same thing as GenerateThunk, but in a
221 // very different way, so that va_start and va_end work correctly.
222 // FIXME: This function assumes "this" is the first non-sret LLVM argument of
223 // a function, and that there is an alloca built in the entry block
224 // for all accesses to "this".
225 // FIXME: This function assumes there is only one "ret" statement per function.
226 // FIXME: Cloning isn't correct in the presence of indirect goto!
227 // FIXME: This implementation of thunks bloats codesize by duplicating the
228 // function definition. There are alternatives:
229 // 1. Add some sort of stub support to LLVM for cases where we can
230 // do a this adjustment, then a sibcall.
231 // 2. We could transform the definition to take a va_list instead of an
232 // actual variable argument list, then have the thunks (including a
233 // no-op thunk for the regular definition) call va_start/va_end.
234 // There's a bit of per-call overhead for this solution, but it's
235 // better for codesize if the definition is long.
GenerateVarArgsThunk(llvm::Function * Fn,const CGFunctionInfo & FnInfo,GlobalDecl GD,const ThunkInfo & Thunk)236 void CodeGenFunction::GenerateVarArgsThunk(
237 llvm::Function *Fn,
238 const CGFunctionInfo &FnInfo,
239 GlobalDecl GD, const ThunkInfo &Thunk) {
240 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
241 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
242 QualType ResultType = FPT->getResultType();
243
244 // Get the original function
245 assert(FnInfo.isVariadic());
246 llvm::Type *Ty = CGM.getTypes().GetFunctionType(FnInfo);
247 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
248 llvm::Function *BaseFn = cast<llvm::Function>(Callee);
249
250 // Clone to thunk.
251 llvm::Function *NewFn = llvm::CloneFunction(BaseFn);
252 CGM.getModule().getFunctionList().push_back(NewFn);
253 Fn->replaceAllUsesWith(NewFn);
254 NewFn->takeName(Fn);
255 Fn->eraseFromParent();
256 Fn = NewFn;
257
258 // "Initialize" CGF (minimally).
259 CurFn = Fn;
260
261 // Get the "this" value
262 llvm::Function::arg_iterator AI = Fn->arg_begin();
263 if (CGM.ReturnTypeUsesSRet(FnInfo))
264 ++AI;
265
266 // Find the first store of "this", which will be to the alloca associated
267 // with "this".
268 llvm::Value *ThisPtr = &*AI;
269 llvm::BasicBlock *EntryBB = Fn->begin();
270 llvm::Instruction *ThisStore = 0;
271 for (llvm::BasicBlock::iterator I = EntryBB->begin(), E = EntryBB->end();
272 I != E; I++) {
273 if (isa<llvm::StoreInst>(I) && I->getOperand(0) == ThisPtr) {
274 ThisStore = cast<llvm::StoreInst>(I);
275 break;
276 }
277 }
278 assert(ThisStore && "Store of this should be in entry block?");
279 // Adjust "this", if necessary.
280 Builder.SetInsertPoint(ThisStore);
281 llvm::Value *AdjustedThisPtr =
282 PerformTypeAdjustment(*this, ThisPtr,
283 Thunk.This.NonVirtual,
284 Thunk.This.VCallOffsetOffset);
285 ThisStore->setOperand(0, AdjustedThisPtr);
286
287 if (!Thunk.Return.isEmpty()) {
288 // Fix up the returned value, if necessary.
289 for (llvm::Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++) {
290 llvm::Instruction *T = I->getTerminator();
291 if (isa<llvm::ReturnInst>(T)) {
292 RValue RV = RValue::get(T->getOperand(0));
293 T->eraseFromParent();
294 Builder.SetInsertPoint(&*I);
295 RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
296 Builder.CreateRet(RV.getScalarVal());
297 break;
298 }
299 }
300 }
301 }
302
GenerateThunk(llvm::Function * Fn,const CGFunctionInfo & FnInfo,GlobalDecl GD,const ThunkInfo & Thunk)303 void CodeGenFunction::GenerateThunk(llvm::Function *Fn,
304 const CGFunctionInfo &FnInfo,
305 GlobalDecl GD, const ThunkInfo &Thunk) {
306 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
307 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
308 QualType ResultType = FPT->getResultType();
309 QualType ThisType = MD->getThisType(getContext());
310
311 FunctionArgList FunctionArgs;
312
313 // FIXME: It would be nice if more of this code could be shared with
314 // CodeGenFunction::GenerateCode.
315
316 // Create the implicit 'this' parameter declaration.
317 CurGD = GD;
318 CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResultType, FunctionArgs);
319
320 // Add the rest of the parameters.
321 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
322 E = MD->param_end(); I != E; ++I) {
323 ParmVarDecl *Param = *I;
324
325 FunctionArgs.push_back(Param);
326 }
327
328 StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs,
329 SourceLocation());
330
331 CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
332 CXXThisValue = CXXABIThisValue;
333
334 // Adjust the 'this' pointer if necessary.
335 llvm::Value *AdjustedThisPtr =
336 PerformTypeAdjustment(*this, LoadCXXThis(),
337 Thunk.This.NonVirtual,
338 Thunk.This.VCallOffsetOffset);
339
340 CallArgList CallArgs;
341
342 // Add our adjusted 'this' pointer.
343 CallArgs.add(RValue::get(AdjustedThisPtr), ThisType);
344
345 // Add the rest of the parameters.
346 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
347 E = MD->param_end(); I != E; ++I) {
348 ParmVarDecl *param = *I;
349 EmitDelegateCallArg(CallArgs, param);
350 }
351
352 // Get our callee.
353 llvm::Type *Ty =
354 CGM.getTypes().GetFunctionType(CGM.getTypes().arrangeGlobalDeclaration(GD));
355 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
356
357 #ifndef NDEBUG
358 const CGFunctionInfo &CallFnInfo =
359 CGM.getTypes().arrangeFunctionCall(ResultType, CallArgs, FPT->getExtInfo(),
360 RequiredArgs::forPrototypePlus(FPT, 1));
361 assert(CallFnInfo.getRegParm() == FnInfo.getRegParm() &&
362 CallFnInfo.isNoReturn() == FnInfo.isNoReturn() &&
363 CallFnInfo.getCallingConvention() == FnInfo.getCallingConvention());
364 assert(similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(),
365 FnInfo.getReturnInfo(), FnInfo.getReturnType()));
366 assert(CallFnInfo.arg_size() == FnInfo.arg_size());
367 for (unsigned i = 0, e = FnInfo.arg_size(); i != e; ++i)
368 assert(similar(CallFnInfo.arg_begin()[i].info,
369 CallFnInfo.arg_begin()[i].type,
370 FnInfo.arg_begin()[i].info, FnInfo.arg_begin()[i].type));
371 #endif
372
373 // Determine whether we have a return value slot to use.
374 ReturnValueSlot Slot;
375 if (!ResultType->isVoidType() &&
376 FnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect &&
377 hasAggregateLLVMType(CurFnInfo->getReturnType()))
378 Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified());
379
380 // Now emit our call.
381 RValue RV = EmitCall(FnInfo, Callee, Slot, CallArgs, MD);
382
383 if (!Thunk.Return.isEmpty())
384 RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
385
386 if (!ResultType->isVoidType() && Slot.isNull())
387 CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType);
388
389 FinishFunction();
390
391 // Set the right linkage.
392 CGM.setFunctionLinkage(MD, Fn);
393
394 // Set the right visibility.
395 setThunkVisibility(CGM, MD, Thunk, Fn);
396 }
397
EmitThunk(GlobalDecl GD,const ThunkInfo & Thunk,bool UseAvailableExternallyLinkage)398 void CodeGenVTables::EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
399 bool UseAvailableExternallyLinkage)
400 {
401 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeGlobalDeclaration(GD);
402
403 // FIXME: re-use FnInfo in this computation.
404 llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk);
405
406 // Strip off a bitcast if we got one back.
407 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
408 assert(CE->getOpcode() == llvm::Instruction::BitCast);
409 Entry = CE->getOperand(0);
410 }
411
412 // There's already a declaration with the same name, check if it has the same
413 // type or if we need to replace it.
414 if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() !=
415 CGM.getTypes().GetFunctionTypeForVTable(GD)) {
416 llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(Entry);
417
418 // If the types mismatch then we have to rewrite the definition.
419 assert(OldThunkFn->isDeclaration() &&
420 "Shouldn't replace non-declaration");
421
422 // Remove the name from the old thunk function and get a new thunk.
423 OldThunkFn->setName(StringRef());
424 Entry = CGM.GetAddrOfThunk(GD, Thunk);
425
426 // If needed, replace the old thunk with a bitcast.
427 if (!OldThunkFn->use_empty()) {
428 llvm::Constant *NewPtrForOldDecl =
429 llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType());
430 OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
431 }
432
433 // Remove the old thunk.
434 OldThunkFn->eraseFromParent();
435 }
436
437 llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
438
439 if (!ThunkFn->isDeclaration()) {
440 if (UseAvailableExternallyLinkage) {
441 // There is already a thunk emitted for this function, do nothing.
442 return;
443 }
444
445 // If a function has a body, it should have available_externally linkage.
446 assert(ThunkFn->hasAvailableExternallyLinkage() &&
447 "Function should have available_externally linkage!");
448
449 // Change the linkage.
450 CGM.setFunctionLinkage(cast<CXXMethodDecl>(GD.getDecl()), ThunkFn);
451 return;
452 }
453
454 if (ThunkFn->isVarArg()) {
455 // Varargs thunks are special; we can't just generate a call because
456 // we can't copy the varargs. Our implementation is rather
457 // expensive/sucky at the moment, so don't generate the thunk unless
458 // we have to.
459 // FIXME: Do something better here; GenerateVarArgsThunk is extremely ugly.
460 if (!UseAvailableExternallyLinkage)
461 CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk);
462 } else {
463 // Normal thunk body generation.
464 CodeGenFunction(CGM).GenerateThunk(ThunkFn, FnInfo, GD, Thunk);
465 }
466
467 if (UseAvailableExternallyLinkage)
468 ThunkFn->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
469 }
470
MaybeEmitThunkAvailableExternally(GlobalDecl GD,const ThunkInfo & Thunk)471 void CodeGenVTables::MaybeEmitThunkAvailableExternally(GlobalDecl GD,
472 const ThunkInfo &Thunk) {
473 // We only want to do this when building with optimizations.
474 if (!CGM.getCodeGenOpts().OptimizationLevel)
475 return;
476
477 // We can't emit thunks for member functions with incomplete types.
478 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
479 if (!CGM.getTypes().isFuncTypeConvertible(
480 cast<FunctionType>(MD->getType().getTypePtr())))
481 return;
482
483 EmitThunk(GD, Thunk, /*UseAvailableExternallyLinkage=*/true);
484 }
485
EmitThunks(GlobalDecl GD)486 void CodeGenVTables::EmitThunks(GlobalDecl GD)
487 {
488 const CXXMethodDecl *MD =
489 cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
490
491 // We don't need to generate thunks for the base destructor.
492 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
493 return;
494
495 const VTableContext::ThunkInfoVectorTy *ThunkInfoVector =
496 VTContext.getThunkInfo(MD);
497 if (!ThunkInfoVector)
498 return;
499
500 for (unsigned I = 0, E = ThunkInfoVector->size(); I != E; ++I)
501 EmitThunk(GD, (*ThunkInfoVector)[I],
502 /*UseAvailableExternallyLinkage=*/false);
503 }
504
505 llvm::Constant *
CreateVTableInitializer(const CXXRecordDecl * RD,const VTableComponent * Components,unsigned NumComponents,const VTableLayout::VTableThunkTy * VTableThunks,unsigned NumVTableThunks)506 CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD,
507 const VTableComponent *Components,
508 unsigned NumComponents,
509 const VTableLayout::VTableThunkTy *VTableThunks,
510 unsigned NumVTableThunks) {
511 SmallVector<llvm::Constant *, 64> Inits;
512
513 llvm::Type *Int8PtrTy = CGM.Int8PtrTy;
514
515 llvm::Type *PtrDiffTy =
516 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
517
518 QualType ClassType = CGM.getContext().getTagDeclType(RD);
519 llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType);
520
521 unsigned NextVTableThunkIndex = 0;
522
523 llvm::Constant* PureVirtualFn = 0;
524
525 for (unsigned I = 0; I != NumComponents; ++I) {
526 VTableComponent Component = Components[I];
527
528 llvm::Constant *Init = 0;
529
530 switch (Component.getKind()) {
531 case VTableComponent::CK_VCallOffset:
532 Init = llvm::ConstantInt::get(PtrDiffTy,
533 Component.getVCallOffset().getQuantity());
534 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
535 break;
536 case VTableComponent::CK_VBaseOffset:
537 Init = llvm::ConstantInt::get(PtrDiffTy,
538 Component.getVBaseOffset().getQuantity());
539 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
540 break;
541 case VTableComponent::CK_OffsetToTop:
542 Init = llvm::ConstantInt::get(PtrDiffTy,
543 Component.getOffsetToTop().getQuantity());
544 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
545 break;
546 case VTableComponent::CK_RTTI:
547 Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
548 break;
549 case VTableComponent::CK_FunctionPointer:
550 case VTableComponent::CK_CompleteDtorPointer:
551 case VTableComponent::CK_DeletingDtorPointer: {
552 GlobalDecl GD;
553
554 // Get the right global decl.
555 switch (Component.getKind()) {
556 default:
557 llvm_unreachable("Unexpected vtable component kind");
558 case VTableComponent::CK_FunctionPointer:
559 GD = Component.getFunctionDecl();
560 break;
561 case VTableComponent::CK_CompleteDtorPointer:
562 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
563 break;
564 case VTableComponent::CK_DeletingDtorPointer:
565 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
566 break;
567 }
568
569 if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
570 // We have a pure virtual member function.
571 if (!PureVirtualFn) {
572 llvm::FunctionType *Ty =
573 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
574 PureVirtualFn =
575 CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual");
576 PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn,
577 Int8PtrTy);
578 }
579
580 Init = PureVirtualFn;
581 } else {
582 // Check if we should use a thunk.
583 if (NextVTableThunkIndex < NumVTableThunks &&
584 VTableThunks[NextVTableThunkIndex].first == I) {
585 const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
586
587 MaybeEmitThunkAvailableExternally(GD, Thunk);
588 Init = CGM.GetAddrOfThunk(GD, Thunk);
589
590 NextVTableThunkIndex++;
591 } else {
592 llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD);
593
594 Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
595 }
596
597 Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
598 }
599 break;
600 }
601
602 case VTableComponent::CK_UnusedFunctionPointer:
603 Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
604 break;
605 };
606
607 Inits.push_back(Init);
608 }
609
610 llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
611 return llvm::ConstantArray::get(ArrayType, Inits);
612 }
613
GetAddrOfVTable(const CXXRecordDecl * RD)614 llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTable(const CXXRecordDecl *RD) {
615 llvm::GlobalVariable *&VTable = VTables[RD];
616 if (VTable)
617 return VTable;
618
619 // We may need to generate a definition for this vtable.
620 if (ShouldEmitVTableInThisTU(RD))
621 CGM.DeferredVTables.push_back(RD);
622
623 SmallString<256> OutName;
624 llvm::raw_svector_ostream Out(OutName);
625 CGM.getCXXABI().getMangleContext().mangleCXXVTable(RD, Out);
626 Out.flush();
627 StringRef Name = OutName.str();
628
629 llvm::ArrayType *ArrayType =
630 llvm::ArrayType::get(CGM.Int8PtrTy,
631 VTContext.getVTableLayout(RD).getNumVTableComponents());
632
633 VTable =
634 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType,
635 llvm::GlobalValue::ExternalLinkage);
636 VTable->setUnnamedAddr(true);
637 return VTable;
638 }
639
640 void
EmitVTableDefinition(llvm::GlobalVariable * VTable,llvm::GlobalVariable::LinkageTypes Linkage,const CXXRecordDecl * RD)641 CodeGenVTables::EmitVTableDefinition(llvm::GlobalVariable *VTable,
642 llvm::GlobalVariable::LinkageTypes Linkage,
643 const CXXRecordDecl *RD) {
644 const VTableLayout &VTLayout = VTContext.getVTableLayout(RD);
645
646 // Create and set the initializer.
647 llvm::Constant *Init =
648 CreateVTableInitializer(RD,
649 VTLayout.vtable_component_begin(),
650 VTLayout.getNumVTableComponents(),
651 VTLayout.vtable_thunk_begin(),
652 VTLayout.getNumVTableThunks());
653 VTable->setInitializer(Init);
654
655 // Set the correct linkage.
656 VTable->setLinkage(Linkage);
657
658 // Set the right visibility.
659 CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForVTable);
660 }
661
662 llvm::GlobalVariable *
GenerateConstructionVTable(const CXXRecordDecl * RD,const BaseSubobject & Base,bool BaseIsVirtual,llvm::GlobalVariable::LinkageTypes Linkage,VTableAddressPointsMapTy & AddressPoints)663 CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD,
664 const BaseSubobject &Base,
665 bool BaseIsVirtual,
666 llvm::GlobalVariable::LinkageTypes Linkage,
667 VTableAddressPointsMapTy& AddressPoints) {
668 OwningPtr<VTableLayout> VTLayout(
669 VTContext.createConstructionVTableLayout(Base.getBase(),
670 Base.getBaseOffset(),
671 BaseIsVirtual, RD));
672
673 // Add the address points.
674 AddressPoints = VTLayout->getAddressPoints();
675
676 // Get the mangled construction vtable name.
677 SmallString<256> OutName;
678 llvm::raw_svector_ostream Out(OutName);
679 CGM.getCXXABI().getMangleContext().
680 mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(), Base.getBase(),
681 Out);
682 Out.flush();
683 StringRef Name = OutName.str();
684
685 llvm::ArrayType *ArrayType =
686 llvm::ArrayType::get(CGM.Int8PtrTy, VTLayout->getNumVTableComponents());
687
688 // Create the variable that will hold the construction vtable.
689 llvm::GlobalVariable *VTable =
690 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, Linkage);
691 CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForConstructionVTable);
692
693 // V-tables are always unnamed_addr.
694 VTable->setUnnamedAddr(true);
695
696 // Create and set the initializer.
697 llvm::Constant *Init =
698 CreateVTableInitializer(Base.getBase(),
699 VTLayout->vtable_component_begin(),
700 VTLayout->getNumVTableComponents(),
701 VTLayout->vtable_thunk_begin(),
702 VTLayout->getNumVTableThunks());
703 VTable->setInitializer(Init);
704
705 return VTable;
706 }
707
708 void
GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,const CXXRecordDecl * RD)709 CodeGenVTables::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
710 const CXXRecordDecl *RD) {
711 llvm::GlobalVariable *VTable = GetAddrOfVTable(RD);
712 if (VTable->hasInitializer())
713 return;
714
715 EmitVTableDefinition(VTable, Linkage, RD);
716
717 if (RD->getNumVBases()) {
718 llvm::GlobalVariable *VTT = GetAddrOfVTT(RD);
719 EmitVTTDefinition(VTT, Linkage, RD);
720 }
721
722 // If this is the magic class __cxxabiv1::__fundamental_type_info,
723 // we will emit the typeinfo for the fundamental types. This is the
724 // same behaviour as GCC.
725 const DeclContext *DC = RD->getDeclContext();
726 if (RD->getIdentifier() &&
727 RD->getIdentifier()->isStr("__fundamental_type_info") &&
728 isa<NamespaceDecl>(DC) &&
729 cast<NamespaceDecl>(DC)->getIdentifier() &&
730 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") &&
731 DC->getParent()->isTranslationUnit())
732 CGM.EmitFundamentalRTTIDescriptors();
733 }
734