1 //===--- CGVTables.cpp - Emit LLVM Code for C++ vtables -------------------===//
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 virtual tables.
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenFunction.h"
16 #include "CodeGenModule.h"
17 #include "clang/AST/CXXInheritance.h"
18 #include "clang/AST/RecordLayout.h"
19 #include "clang/CodeGen/CGFunctionInfo.h"
20 #include "clang/Frontend/CodeGenOptions.h"
21 #include "llvm/ADT/DenseSet.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/Format.h"
25 #include "llvm/Transforms/Utils/Cloning.h"
29 using namespace clang;
30 using namespace CodeGen;
32 CodeGenVTables::CodeGenVTables(CodeGenModule &CGM)
33 : CGM(CGM), VTContext(CGM.getContext().getVTableContext()) {}
35 llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
36 const ThunkInfo &Thunk) {
37 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
39 // Compute the mangled name.
40 SmallString<256> Name;
41 llvm::raw_svector_ostream Out(Name);
42 if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
43 getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(),
46 getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out);
49 llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD);
50 return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true,
54 static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD,
55 const ThunkInfo &Thunk, llvm::Function *Fn) {
56 CGM.setGlobalVisibility(Fn, MD);
60 static bool similar(const ABIArgInfo &infoL, CanQualType typeL,
61 const ABIArgInfo &infoR, CanQualType typeR) {
62 return (infoL.getKind() == infoR.getKind() &&
64 (isa<PointerType>(typeL) && isa<PointerType>(typeR)) ||
65 (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR))));
69 static RValue PerformReturnAdjustment(CodeGenFunction &CGF,
70 QualType ResultType, RValue RV,
71 const ThunkInfo &Thunk) {
72 // Emit the return adjustment.
73 bool NullCheckValue = !ResultType->isReferenceType();
75 llvm::BasicBlock *AdjustNull = nullptr;
76 llvm::BasicBlock *AdjustNotNull = nullptr;
77 llvm::BasicBlock *AdjustEnd = nullptr;
79 llvm::Value *ReturnValue = RV.getScalarVal();
82 AdjustNull = CGF.createBasicBlock("adjust.null");
83 AdjustNotNull = CGF.createBasicBlock("adjust.notnull");
84 AdjustEnd = CGF.createBasicBlock("adjust.end");
86 llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue);
87 CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
88 CGF.EmitBlock(AdjustNotNull);
91 ReturnValue = CGF.CGM.getCXXABI().performReturnAdjustment(CGF, ReturnValue,
95 CGF.Builder.CreateBr(AdjustEnd);
96 CGF.EmitBlock(AdjustNull);
97 CGF.Builder.CreateBr(AdjustEnd);
98 CGF.EmitBlock(AdjustEnd);
100 llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2);
101 PHI->addIncoming(ReturnValue, AdjustNotNull);
102 PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
107 return RValue::get(ReturnValue);
110 // This function does roughly the same thing as GenerateThunk, but in a
111 // very different way, so that va_start and va_end work correctly.
112 // FIXME: This function assumes "this" is the first non-sret LLVM argument of
113 // a function, and that there is an alloca built in the entry block
114 // for all accesses to "this".
115 // FIXME: This function assumes there is only one "ret" statement per function.
116 // FIXME: Cloning isn't correct in the presence of indirect goto!
117 // FIXME: This implementation of thunks bloats codesize by duplicating the
118 // function definition. There are alternatives:
119 // 1. Add some sort of stub support to LLVM for cases where we can
120 // do a this adjustment, then a sibcall.
121 // 2. We could transform the definition to take a va_list instead of an
122 // actual variable argument list, then have the thunks (including a
123 // no-op thunk for the regular definition) call va_start/va_end.
124 // There's a bit of per-call overhead for this solution, but it's
125 // better for codesize if the definition is long.
126 void CodeGenFunction::GenerateVarArgsThunk(
128 const CGFunctionInfo &FnInfo,
129 GlobalDecl GD, const ThunkInfo &Thunk) {
130 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
131 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
132 QualType ResultType = FPT->getReturnType();
134 // Get the original function
135 assert(FnInfo.isVariadic());
136 llvm::Type *Ty = CGM.getTypes().GetFunctionType(FnInfo);
137 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
138 llvm::Function *BaseFn = cast<llvm::Function>(Callee);
141 llvm::ValueToValueMapTy VMap;
142 llvm::Function *NewFn = llvm::CloneFunction(BaseFn, VMap,
143 /*ModuleLevelChanges=*/false);
144 CGM.getModule().getFunctionList().push_back(NewFn);
145 Fn->replaceAllUsesWith(NewFn);
147 Fn->eraseFromParent();
150 // "Initialize" CGF (minimally).
153 // Get the "this" value
154 llvm::Function::arg_iterator AI = Fn->arg_begin();
155 if (CGM.ReturnTypeUsesSRet(FnInfo))
158 // Find the first store of "this", which will be to the alloca associated
160 llvm::Value *ThisPtr = &*AI;
161 llvm::BasicBlock *EntryBB = Fn->begin();
162 llvm::Instruction *ThisStore = nullptr;
163 for (llvm::BasicBlock::iterator I = EntryBB->begin(), E = EntryBB->end();
165 if (isa<llvm::StoreInst>(I) && I->getOperand(0) == ThisPtr) {
166 ThisStore = cast<llvm::StoreInst>(I);
170 assert(ThisStore && "Store of this should be in entry block?");
171 // Adjust "this", if necessary.
172 Builder.SetInsertPoint(ThisStore);
173 llvm::Value *AdjustedThisPtr =
174 CGM.getCXXABI().performThisAdjustment(*this, ThisPtr, Thunk.This);
175 ThisStore->setOperand(0, AdjustedThisPtr);
177 if (!Thunk.Return.isEmpty()) {
178 // Fix up the returned value, if necessary.
179 for (llvm::Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++) {
180 llvm::Instruction *T = I->getTerminator();
181 if (isa<llvm::ReturnInst>(T)) {
182 RValue RV = RValue::get(T->getOperand(0));
183 T->eraseFromParent();
184 Builder.SetInsertPoint(&*I);
185 RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
186 Builder.CreateRet(RV.getScalarVal());
193 void CodeGenFunction::StartThunk(llvm::Function *Fn, GlobalDecl GD,
194 const CGFunctionInfo &FnInfo) {
195 assert(!CurGD.getDecl() && "CurGD was already set!");
198 // Build FunctionArgs.
199 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
200 QualType ThisType = MD->getThisType(getContext());
201 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
202 QualType ResultType =
203 CGM.getCXXABI().HasThisReturn(GD) ? ThisType : FPT->getReturnType();
204 FunctionArgList FunctionArgs;
206 // Create the implicit 'this' parameter declaration.
207 CGM.getCXXABI().buildThisParam(*this, FunctionArgs);
209 // Add the rest of the parameters.
210 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
213 FunctionArgs.push_back(*I);
215 if (isa<CXXDestructorDecl>(MD))
216 CGM.getCXXABI().addImplicitStructorParams(*this, ResultType, FunctionArgs);
218 // Start defining the function.
219 StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs,
220 MD->getLocation(), SourceLocation());
222 // Since we didn't pass a GlobalDecl to StartFunction, do this ourselves.
223 CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
224 CXXThisValue = CXXABIThisValue;
227 void CodeGenFunction::EmitCallAndReturnForThunk(GlobalDecl GD,
229 const ThunkInfo *Thunk) {
230 assert(isa<CXXMethodDecl>(CurGD.getDecl()) &&
231 "Please use a new CGF for this thunk");
232 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
234 // Adjust the 'this' pointer if necessary
235 llvm::Value *AdjustedThisPtr = Thunk ? CGM.getCXXABI().performThisAdjustment(
236 *this, LoadCXXThis(), Thunk->This)
239 // Start building CallArgs.
240 CallArgList CallArgs;
241 QualType ThisType = MD->getThisType(getContext());
242 CallArgs.add(RValue::get(AdjustedThisPtr), ThisType);
244 if (isa<CXXDestructorDecl>(MD))
245 CGM.getCXXABI().adjustCallArgsForDestructorThunk(*this, GD, CallArgs);
247 // Add the rest of the arguments.
248 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
249 E = MD->param_end(); I != E; ++I)
250 EmitDelegateCallArg(CallArgs, *I, (*I)->getLocStart());
252 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
255 const CGFunctionInfo &CallFnInfo =
256 CGM.getTypes().arrangeCXXMethodCall(CallArgs, FPT,
257 RequiredArgs::forPrototypePlus(FPT, 1));
258 assert(CallFnInfo.getRegParm() == CurFnInfo->getRegParm() &&
259 CallFnInfo.isNoReturn() == CurFnInfo->isNoReturn() &&
260 CallFnInfo.getCallingConvention() == CurFnInfo->getCallingConvention());
261 assert(isa<CXXDestructorDecl>(MD) || // ignore dtor return types
262 similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(),
263 CurFnInfo->getReturnInfo(), CurFnInfo->getReturnType()));
264 assert(CallFnInfo.arg_size() == CurFnInfo->arg_size());
265 for (unsigned i = 0, e = CurFnInfo->arg_size(); i != e; ++i)
266 assert(similar(CallFnInfo.arg_begin()[i].info,
267 CallFnInfo.arg_begin()[i].type,
268 CurFnInfo->arg_begin()[i].info,
269 CurFnInfo->arg_begin()[i].type));
272 // Determine whether we have a return value slot to use.
273 QualType ResultType =
274 CGM.getCXXABI().HasThisReturn(GD) ? ThisType : FPT->getReturnType();
275 ReturnValueSlot Slot;
276 if (!ResultType->isVoidType() &&
277 CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
278 !hasScalarEvaluationKind(CurFnInfo->getReturnType()))
279 Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified());
281 // Now emit our call.
282 RValue RV = EmitCall(*CurFnInfo, Callee, Slot, CallArgs, MD);
284 // Consider return adjustment if we have ThunkInfo.
285 if (Thunk && !Thunk->Return.isEmpty())
286 RV = PerformReturnAdjustment(*this, ResultType, RV, *Thunk);
289 if (!ResultType->isVoidType() && Slot.isNull())
290 CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType);
292 // Disable the final ARC autorelease.
293 AutoreleaseResult = false;
298 void CodeGenFunction::GenerateThunk(llvm::Function *Fn,
299 const CGFunctionInfo &FnInfo,
300 GlobalDecl GD, const ThunkInfo &Thunk) {
301 StartThunk(Fn, GD, FnInfo);
305 CGM.getTypes().GetFunctionType(CGM.getTypes().arrangeGlobalDeclaration(GD));
306 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
308 // Make the call and return the result.
309 EmitCallAndReturnForThunk(GD, Callee, &Thunk);
311 // Set the right linkage.
312 CGM.setFunctionLinkage(GD, Fn);
314 // Set the right visibility.
315 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
316 setThunkVisibility(CGM, MD, Thunk, Fn);
319 void CodeGenVTables::emitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
321 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeGlobalDeclaration(GD);
323 // FIXME: re-use FnInfo in this computation.
324 llvm::Constant *C = CGM.GetAddrOfThunk(GD, Thunk);
325 llvm::GlobalValue *Entry;
327 // Strip off a bitcast if we got one back.
328 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(C)) {
329 assert(CE->getOpcode() == llvm::Instruction::BitCast);
330 Entry = cast<llvm::GlobalValue>(CE->getOperand(0));
332 Entry = cast<llvm::GlobalValue>(C);
335 // There's already a declaration with the same name, check if it has the same
336 // type or if we need to replace it.
337 if (Entry->getType()->getElementType() !=
338 CGM.getTypes().GetFunctionTypeForVTable(GD)) {
339 llvm::GlobalValue *OldThunkFn = Entry;
341 // If the types mismatch then we have to rewrite the definition.
342 assert(OldThunkFn->isDeclaration() &&
343 "Shouldn't replace non-declaration");
345 // Remove the name from the old thunk function and get a new thunk.
346 OldThunkFn->setName(StringRef());
347 Entry = cast<llvm::GlobalValue>(CGM.GetAddrOfThunk(GD, Thunk));
349 // If needed, replace the old thunk with a bitcast.
350 if (!OldThunkFn->use_empty()) {
351 llvm::Constant *NewPtrForOldDecl =
352 llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType());
353 OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
356 // Remove the old thunk.
357 OldThunkFn->eraseFromParent();
360 llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
361 bool ABIHasKeyFunctions = CGM.getTarget().getCXXABI().hasKeyFunctions();
362 bool UseAvailableExternallyLinkage = ForVTable && ABIHasKeyFunctions;
364 if (!ThunkFn->isDeclaration()) {
365 if (!ABIHasKeyFunctions || UseAvailableExternallyLinkage) {
366 // There is already a thunk emitted for this function, do nothing.
370 // Change the linkage.
371 CGM.setFunctionLinkage(GD, ThunkFn);
375 CGM.SetLLVMFunctionAttributesForDefinition(GD.getDecl(), ThunkFn);
377 if (ThunkFn->isVarArg()) {
378 // Varargs thunks are special; we can't just generate a call because
379 // we can't copy the varargs. Our implementation is rather
380 // expensive/sucky at the moment, so don't generate the thunk unless
382 // FIXME: Do something better here; GenerateVarArgsThunk is extremely ugly.
383 if (!UseAvailableExternallyLinkage) {
384 CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk);
385 CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable, GD,
386 !Thunk.Return.isEmpty());
389 // Normal thunk body generation.
390 CodeGenFunction(CGM).GenerateThunk(ThunkFn, FnInfo, GD, Thunk);
391 CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable, GD,
392 !Thunk.Return.isEmpty());
396 void CodeGenVTables::maybeEmitThunkForVTable(GlobalDecl GD,
397 const ThunkInfo &Thunk) {
398 // If the ABI has key functions, only the TU with the key function should emit
399 // the thunk. However, we can allow inlining of thunks if we emit them with
400 // available_externally linkage together with vtables when optimizations are
402 if (CGM.getTarget().getCXXABI().hasKeyFunctions() &&
403 !CGM.getCodeGenOpts().OptimizationLevel)
406 // We can't emit thunks for member functions with incomplete types.
407 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
408 if (!CGM.getTypes().isFuncTypeConvertible(
409 MD->getType()->castAs<FunctionType>()))
412 emitThunk(GD, Thunk, /*ForVTable=*/true);
415 void CodeGenVTables::EmitThunks(GlobalDecl GD)
417 const CXXMethodDecl *MD =
418 cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
420 // We don't need to generate thunks for the base destructor.
421 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
424 const VTableContextBase::ThunkInfoVectorTy *ThunkInfoVector =
425 VTContext->getThunkInfo(GD);
427 if (!ThunkInfoVector)
430 for (unsigned I = 0, E = ThunkInfoVector->size(); I != E; ++I)
431 emitThunk(GD, (*ThunkInfoVector)[I], /*ForVTable=*/false);
434 llvm::Constant *CodeGenVTables::CreateVTableInitializer(
435 const CXXRecordDecl *RD, const VTableComponent *Components,
436 unsigned NumComponents, const VTableLayout::VTableThunkTy *VTableThunks,
437 unsigned NumVTableThunks, llvm::Constant *RTTI) {
438 SmallVector<llvm::Constant *, 64> Inits;
440 llvm::Type *Int8PtrTy = CGM.Int8PtrTy;
442 llvm::Type *PtrDiffTy =
443 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
445 unsigned NextVTableThunkIndex = 0;
447 llvm::Constant *PureVirtualFn = nullptr, *DeletedVirtualFn = nullptr;
449 for (unsigned I = 0; I != NumComponents; ++I) {
450 VTableComponent Component = Components[I];
452 llvm::Constant *Init = nullptr;
454 switch (Component.getKind()) {
455 case VTableComponent::CK_VCallOffset:
456 Init = llvm::ConstantInt::get(PtrDiffTy,
457 Component.getVCallOffset().getQuantity());
458 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
460 case VTableComponent::CK_VBaseOffset:
461 Init = llvm::ConstantInt::get(PtrDiffTy,
462 Component.getVBaseOffset().getQuantity());
463 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
465 case VTableComponent::CK_OffsetToTop:
466 Init = llvm::ConstantInt::get(PtrDiffTy,
467 Component.getOffsetToTop().getQuantity());
468 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
470 case VTableComponent::CK_RTTI:
471 Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
473 case VTableComponent::CK_FunctionPointer:
474 case VTableComponent::CK_CompleteDtorPointer:
475 case VTableComponent::CK_DeletingDtorPointer: {
478 // Get the right global decl.
479 switch (Component.getKind()) {
481 llvm_unreachable("Unexpected vtable component kind");
482 case VTableComponent::CK_FunctionPointer:
483 GD = Component.getFunctionDecl();
485 case VTableComponent::CK_CompleteDtorPointer:
486 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
488 case VTableComponent::CK_DeletingDtorPointer:
489 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
493 if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
494 // We have a pure virtual member function.
495 if (!PureVirtualFn) {
496 llvm::FunctionType *Ty =
497 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
498 StringRef PureCallName = CGM.getCXXABI().GetPureVirtualCallName();
499 PureVirtualFn = CGM.CreateRuntimeFunction(Ty, PureCallName);
500 PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn,
503 Init = PureVirtualFn;
504 } else if (cast<CXXMethodDecl>(GD.getDecl())->isDeleted()) {
505 if (!DeletedVirtualFn) {
506 llvm::FunctionType *Ty =
507 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
508 StringRef DeletedCallName =
509 CGM.getCXXABI().GetDeletedVirtualCallName();
510 DeletedVirtualFn = CGM.CreateRuntimeFunction(Ty, DeletedCallName);
511 DeletedVirtualFn = llvm::ConstantExpr::getBitCast(DeletedVirtualFn,
514 Init = DeletedVirtualFn;
516 // Check if we should use a thunk.
517 if (NextVTableThunkIndex < NumVTableThunks &&
518 VTableThunks[NextVTableThunkIndex].first == I) {
519 const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
521 maybeEmitThunkForVTable(GD, Thunk);
522 Init = CGM.GetAddrOfThunk(GD, Thunk);
524 NextVTableThunkIndex++;
526 llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD);
528 Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
531 Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
536 case VTableComponent::CK_UnusedFunctionPointer:
537 Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
541 Inits.push_back(Init);
544 llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
545 return llvm::ConstantArray::get(ArrayType, Inits);
548 llvm::GlobalVariable *
549 CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD,
550 const BaseSubobject &Base,
552 llvm::GlobalVariable::LinkageTypes Linkage,
553 VTableAddressPointsMapTy& AddressPoints) {
554 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
555 DI->completeClassData(Base.getBase());
557 std::unique_ptr<VTableLayout> VTLayout(
558 getItaniumVTableContext().createConstructionVTableLayout(
559 Base.getBase(), Base.getBaseOffset(), BaseIsVirtual, RD));
561 // Add the address points.
562 AddressPoints = VTLayout->getAddressPoints();
564 // Get the mangled construction vtable name.
565 SmallString<256> OutName;
566 llvm::raw_svector_ostream Out(OutName);
567 cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
568 .mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(),
569 Base.getBase(), Out);
571 StringRef Name = OutName.str();
573 llvm::ArrayType *ArrayType =
574 llvm::ArrayType::get(CGM.Int8PtrTy, VTLayout->getNumVTableComponents());
576 // Construction vtable symbols are not part of the Itanium ABI, so we cannot
577 // guarantee that they actually will be available externally. Instead, when
578 // emitting an available_externally VTT, we provide references to an internal
579 // linkage construction vtable. The ABI only requires complete-object vtables
580 // to be the same for all instances of a type, not construction vtables.
581 if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
582 Linkage = llvm::GlobalVariable::InternalLinkage;
584 // Create the variable that will hold the construction vtable.
585 llvm::GlobalVariable *VTable =
586 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, Linkage);
587 CGM.setGlobalVisibility(VTable, RD);
589 // V-tables are always unnamed_addr.
590 VTable->setUnnamedAddr(true);
592 llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(
593 CGM.getContext().getTagDeclType(Base.getBase()));
595 // Create and set the initializer.
596 llvm::Constant *Init = CreateVTableInitializer(
597 Base.getBase(), VTLayout->vtable_component_begin(),
598 VTLayout->getNumVTableComponents(), VTLayout->vtable_thunk_begin(),
599 VTLayout->getNumVTableThunks(), RTTI);
600 VTable->setInitializer(Init);
605 /// Compute the required linkage of the v-table for the given class.
607 /// Note that we only call this at the end of the translation unit.
608 llvm::GlobalVariable::LinkageTypes
609 CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
610 if (!RD->isExternallyVisible())
611 return llvm::GlobalVariable::InternalLinkage;
613 // We're at the end of the translation unit, so the current key
614 // function is fully correct.
615 if (const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction(RD)) {
616 // If this class has a key function, use that to determine the
617 // linkage of the vtable.
618 const FunctionDecl *def = nullptr;
619 if (keyFunction->hasBody(def))
620 keyFunction = cast<CXXMethodDecl>(def);
622 switch (keyFunction->getTemplateSpecializationKind()) {
624 case TSK_ExplicitSpecialization:
625 assert(def && "Should not have been asked to emit this");
626 if (keyFunction->isInlined())
627 return !Context.getLangOpts().AppleKext ?
628 llvm::GlobalVariable::LinkOnceODRLinkage :
629 llvm::Function::InternalLinkage;
631 return llvm::GlobalVariable::ExternalLinkage;
633 case TSK_ImplicitInstantiation:
634 return !Context.getLangOpts().AppleKext ?
635 llvm::GlobalVariable::LinkOnceODRLinkage :
636 llvm::Function::InternalLinkage;
638 case TSK_ExplicitInstantiationDefinition:
639 return !Context.getLangOpts().AppleKext ?
640 llvm::GlobalVariable::WeakODRLinkage :
641 llvm::Function::InternalLinkage;
643 case TSK_ExplicitInstantiationDeclaration:
644 llvm_unreachable("Should not have been asked to emit this");
648 // -fapple-kext mode does not support weak linkage, so we must use
650 if (Context.getLangOpts().AppleKext)
651 return llvm::Function::InternalLinkage;
653 llvm::GlobalVariable::LinkageTypes DiscardableODRLinkage =
654 llvm::GlobalValue::LinkOnceODRLinkage;
655 llvm::GlobalVariable::LinkageTypes NonDiscardableODRLinkage =
656 llvm::GlobalValue::WeakODRLinkage;
657 if (RD->hasAttr<DLLExportAttr>()) {
658 // Cannot discard exported vtables.
659 DiscardableODRLinkage = NonDiscardableODRLinkage;
660 } else if (RD->hasAttr<DLLImportAttr>()) {
661 // Imported vtables are available externally.
662 DiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage;
663 NonDiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage;
666 switch (RD->getTemplateSpecializationKind()) {
668 case TSK_ExplicitSpecialization:
669 case TSK_ImplicitInstantiation:
670 return DiscardableODRLinkage;
672 case TSK_ExplicitInstantiationDeclaration:
673 llvm_unreachable("Should not have been asked to emit this");
675 case TSK_ExplicitInstantiationDefinition:
676 return NonDiscardableODRLinkage;
679 llvm_unreachable("Invalid TemplateSpecializationKind!");
682 /// This is a callback from Sema to tell us that it believes that a
683 /// particular v-table is required to be emitted in this translation
686 /// The reason we don't simply trust this callback is because Sema
687 /// will happily report that something is used even when it's used
688 /// only in code that we don't actually have to emit.
690 /// \param isRequired - if true, the v-table is mandatory, e.g.
691 /// because the translation unit defines the key function
692 void CodeGenModule::EmitVTable(CXXRecordDecl *theClass, bool isRequired) {
693 if (!isRequired) return;
695 VTables.GenerateClassData(theClass);
699 CodeGenVTables::GenerateClassData(const CXXRecordDecl *RD) {
700 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
701 DI->completeClassData(RD);
703 if (RD->getNumVBases())
704 CGM.getCXXABI().emitVirtualInheritanceTables(RD);
706 CGM.getCXXABI().emitVTableDefinitions(*this, RD);
709 /// At this point in the translation unit, does it appear that can we
710 /// rely on the vtable being defined elsewhere in the program?
712 /// The response is really only definitive when called at the end of
713 /// the translation unit.
715 /// The only semantic restriction here is that the object file should
716 /// not contain a v-table definition when that v-table is defined
717 /// strongly elsewhere. Otherwise, we'd just like to avoid emitting
718 /// v-tables when unnecessary.
719 bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
720 assert(RD->isDynamicClass() && "Non-dynamic classes have no VTable.");
722 // If we have an explicit instantiation declaration (and not a
723 // definition), the v-table is defined elsewhere.
724 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
725 if (TSK == TSK_ExplicitInstantiationDeclaration)
728 // Otherwise, if the class is an instantiated template, the
729 // v-table must be defined here.
730 if (TSK == TSK_ImplicitInstantiation ||
731 TSK == TSK_ExplicitInstantiationDefinition)
734 // Otherwise, if the class doesn't have a key function (possibly
735 // anymore), the v-table must be defined here.
736 const CXXMethodDecl *keyFunction = CGM.getContext().getCurrentKeyFunction(RD);
740 // Otherwise, if we don't have a definition of the key function, the
741 // v-table must be defined somewhere else.
742 return !keyFunction->hasBody();
745 /// Given that we're currently at the end of the translation unit, and
746 /// we've emitted a reference to the v-table for this class, should
747 /// we define that v-table?
748 static bool shouldEmitVTableAtEndOfTranslationUnit(CodeGenModule &CGM,
749 const CXXRecordDecl *RD) {
750 return !CGM.getVTables().isVTableExternal(RD);
753 /// Given that at some point we emitted a reference to one or more
754 /// v-tables, and that we are now at the end of the translation unit,
755 /// decide whether we should emit them.
756 void CodeGenModule::EmitDeferredVTables() {
758 // Remember the size of DeferredVTables, because we're going to assume
759 // that this entire operation doesn't modify it.
760 size_t savedSize = DeferredVTables.size();
763 typedef std::vector<const CXXRecordDecl *>::const_iterator const_iterator;
764 for (const_iterator i = DeferredVTables.begin(),
765 e = DeferredVTables.end(); i != e; ++i) {
766 const CXXRecordDecl *RD = *i;
767 if (shouldEmitVTableAtEndOfTranslationUnit(*this, RD))
768 VTables.GenerateClassData(RD);
771 assert(savedSize == DeferredVTables.size() &&
772 "deferred extra v-tables during v-table emission?");
773 DeferredVTables.clear();