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,
51 /*DontDefer=*/true, /*IsThunk=*/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 =
163 std::find_if(EntryBB->begin(), EntryBB->end(), [&](llvm::Instruction &I) {
164 return isa<llvm::StoreInst>(I) && I.getOperand(0) == ThisPtr;
166 assert(ThisStore && "Store of this should be in entry block?");
167 // Adjust "this", if necessary.
168 Builder.SetInsertPoint(ThisStore);
169 llvm::Value *AdjustedThisPtr =
170 CGM.getCXXABI().performThisAdjustment(*this, ThisPtr, Thunk.This);
171 ThisStore->setOperand(0, AdjustedThisPtr);
173 if (!Thunk.Return.isEmpty()) {
174 // Fix up the returned value, if necessary.
175 for (llvm::Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++) {
176 llvm::Instruction *T = I->getTerminator();
177 if (isa<llvm::ReturnInst>(T)) {
178 RValue RV = RValue::get(T->getOperand(0));
179 T->eraseFromParent();
180 Builder.SetInsertPoint(&*I);
181 RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
182 Builder.CreateRet(RV.getScalarVal());
189 void CodeGenFunction::StartThunk(llvm::Function *Fn, GlobalDecl GD,
190 const CGFunctionInfo &FnInfo) {
191 assert(!CurGD.getDecl() && "CurGD was already set!");
193 CurFuncIsThunk = true;
195 // Build FunctionArgs.
196 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
197 QualType ThisType = MD->getThisType(getContext());
198 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
199 QualType ResultType = CGM.getCXXABI().HasThisReturn(GD)
201 : CGM.getCXXABI().hasMostDerivedReturn(GD)
202 ? CGM.getContext().VoidPtrTy
203 : 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 FunctionArgs.append(MD->param_begin(), MD->param_end());
212 if (isa<CXXDestructorDecl>(MD))
213 CGM.getCXXABI().addImplicitStructorParams(*this, ResultType, FunctionArgs);
215 // Start defining the function.
216 StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs,
217 MD->getLocation(), MD->getLocation());
219 // Since we didn't pass a GlobalDecl to StartFunction, do this ourselves.
220 CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
221 CXXThisValue = CXXABIThisValue;
224 void CodeGenFunction::EmitCallAndReturnForThunk(llvm::Value *Callee,
225 const ThunkInfo *Thunk) {
226 assert(isa<CXXMethodDecl>(CurGD.getDecl()) &&
227 "Please use a new CGF for this thunk");
228 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CurGD.getDecl());
230 // Adjust the 'this' pointer if necessary
231 llvm::Value *AdjustedThisPtr = Thunk ? CGM.getCXXABI().performThisAdjustment(
232 *this, LoadCXXThis(), Thunk->This)
235 if (CurFnInfo->usesInAlloca()) {
236 // We don't handle return adjusting thunks, because they require us to call
237 // the copy constructor. For now, fall through and pretend the return
238 // adjustment was empty so we don't crash.
239 if (Thunk && !Thunk->Return.isEmpty()) {
240 CGM.ErrorUnsupported(
241 MD, "non-trivial argument copy for return-adjusting thunk");
243 EmitMustTailThunk(MD, AdjustedThisPtr, Callee);
247 // Start building CallArgs.
248 CallArgList CallArgs;
249 QualType ThisType = MD->getThisType(getContext());
250 CallArgs.add(RValue::get(AdjustedThisPtr), ThisType);
252 if (isa<CXXDestructorDecl>(MD))
253 CGM.getCXXABI().adjustCallArgsForDestructorThunk(*this, CurGD, CallArgs);
255 // Add the rest of the arguments.
256 for (const ParmVarDecl *PD : MD->params())
257 EmitDelegateCallArg(CallArgs, PD, PD->getLocStart());
259 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
262 const CGFunctionInfo &CallFnInfo =
263 CGM.getTypes().arrangeCXXMethodCall(CallArgs, FPT,
264 RequiredArgs::forPrototypePlus(FPT, 1));
265 assert(CallFnInfo.getRegParm() == CurFnInfo->getRegParm() &&
266 CallFnInfo.isNoReturn() == CurFnInfo->isNoReturn() &&
267 CallFnInfo.getCallingConvention() == CurFnInfo->getCallingConvention());
268 assert(isa<CXXDestructorDecl>(MD) || // ignore dtor return types
269 similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(),
270 CurFnInfo->getReturnInfo(), CurFnInfo->getReturnType()));
271 assert(CallFnInfo.arg_size() == CurFnInfo->arg_size());
272 for (unsigned i = 0, e = CurFnInfo->arg_size(); i != e; ++i)
273 assert(similar(CallFnInfo.arg_begin()[i].info,
274 CallFnInfo.arg_begin()[i].type,
275 CurFnInfo->arg_begin()[i].info,
276 CurFnInfo->arg_begin()[i].type));
279 // Determine whether we have a return value slot to use.
280 QualType ResultType = CGM.getCXXABI().HasThisReturn(CurGD)
282 : CGM.getCXXABI().hasMostDerivedReturn(CurGD)
283 ? CGM.getContext().VoidPtrTy
284 : FPT->getReturnType();
285 ReturnValueSlot Slot;
286 if (!ResultType->isVoidType() &&
287 CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
288 !hasScalarEvaluationKind(CurFnInfo->getReturnType()))
289 Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified());
291 // Now emit our call.
292 llvm::Instruction *CallOrInvoke;
293 RValue RV = EmitCall(*CurFnInfo, Callee, Slot, CallArgs, MD, &CallOrInvoke);
295 // Consider return adjustment if we have ThunkInfo.
296 if (Thunk && !Thunk->Return.isEmpty())
297 RV = PerformReturnAdjustment(*this, ResultType, RV, *Thunk);
300 if (!ResultType->isVoidType() && Slot.isNull())
301 CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType);
303 // Disable the final ARC autorelease.
304 AutoreleaseResult = false;
309 void CodeGenFunction::EmitMustTailThunk(const CXXMethodDecl *MD,
310 llvm::Value *AdjustedThisPtr,
311 llvm::Value *Callee) {
312 // Emitting a musttail call thunk doesn't use any of the CGCall.cpp machinery
313 // to translate AST arguments into LLVM IR arguments. For thunks, we know
314 // that the caller prototype more or less matches the callee prototype with
315 // the exception of 'this'.
316 SmallVector<llvm::Value *, 8> Args;
317 for (llvm::Argument &A : CurFn->args())
320 // Set the adjusted 'this' pointer.
321 const ABIArgInfo &ThisAI = CurFnInfo->arg_begin()->info;
322 if (ThisAI.isDirect()) {
323 const ABIArgInfo &RetAI = CurFnInfo->getReturnInfo();
324 int ThisArgNo = RetAI.isIndirect() && !RetAI.isSRetAfterThis() ? 1 : 0;
325 llvm::Type *ThisType = Args[ThisArgNo]->getType();
326 if (ThisType != AdjustedThisPtr->getType())
327 AdjustedThisPtr = Builder.CreateBitCast(AdjustedThisPtr, ThisType);
328 Args[ThisArgNo] = AdjustedThisPtr;
330 assert(ThisAI.isInAlloca() && "this is passed directly or inalloca");
331 llvm::Value *ThisAddr = GetAddrOfLocalVar(CXXABIThisDecl);
332 llvm::Type *ThisType =
333 cast<llvm::PointerType>(ThisAddr->getType())->getElementType();
334 if (ThisType != AdjustedThisPtr->getType())
335 AdjustedThisPtr = Builder.CreateBitCast(AdjustedThisPtr, ThisType);
336 Builder.CreateStore(AdjustedThisPtr, ThisAddr);
339 // Emit the musttail call manually. Even if the prologue pushed cleanups, we
340 // don't actually want to run them.
341 llvm::CallInst *Call = Builder.CreateCall(Callee, Args);
342 Call->setTailCallKind(llvm::CallInst::TCK_MustTail);
344 // Apply the standard set of call attributes.
345 unsigned CallingConv;
346 CodeGen::AttributeListType AttributeList;
347 CGM.ConstructAttributeList(*CurFnInfo, MD, AttributeList, CallingConv,
348 /*AttrOnCallSite=*/true);
349 llvm::AttributeSet Attrs =
350 llvm::AttributeSet::get(getLLVMContext(), AttributeList);
351 Call->setAttributes(Attrs);
352 Call->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
354 if (Call->getType()->isVoidTy())
355 Builder.CreateRetVoid();
357 Builder.CreateRet(Call);
359 // Finish the function to maintain CodeGenFunction invariants.
360 // FIXME: Don't emit unreachable code.
361 EmitBlock(createBasicBlock());
365 void CodeGenFunction::GenerateThunk(llvm::Function *Fn,
366 const CGFunctionInfo &FnInfo,
367 GlobalDecl GD, const ThunkInfo &Thunk) {
368 StartThunk(Fn, GD, FnInfo);
372 CGM.getTypes().GetFunctionType(CGM.getTypes().arrangeGlobalDeclaration(GD));
373 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
375 // Make the call and return the result.
376 EmitCallAndReturnForThunk(Callee, &Thunk);
378 // Set the right linkage.
379 CGM.setFunctionLinkage(GD, Fn);
381 if (CGM.supportsCOMDAT() && Fn->isWeakForLinker())
382 Fn->setComdat(CGM.getModule().getOrInsertComdat(Fn->getName()));
384 // Set the right visibility.
385 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
386 setThunkVisibility(CGM, MD, Thunk, Fn);
389 void CodeGenVTables::emitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
391 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeGlobalDeclaration(GD);
393 // FIXME: re-use FnInfo in this computation.
394 llvm::Constant *C = CGM.GetAddrOfThunk(GD, Thunk);
395 llvm::GlobalValue *Entry;
397 // Strip off a bitcast if we got one back.
398 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(C)) {
399 assert(CE->getOpcode() == llvm::Instruction::BitCast);
400 Entry = cast<llvm::GlobalValue>(CE->getOperand(0));
402 Entry = cast<llvm::GlobalValue>(C);
405 // There's already a declaration with the same name, check if it has the same
406 // type or if we need to replace it.
407 if (Entry->getType()->getElementType() !=
408 CGM.getTypes().GetFunctionTypeForVTable(GD)) {
409 llvm::GlobalValue *OldThunkFn = Entry;
411 // If the types mismatch then we have to rewrite the definition.
412 assert(OldThunkFn->isDeclaration() &&
413 "Shouldn't replace non-declaration");
415 // Remove the name from the old thunk function and get a new thunk.
416 OldThunkFn->setName(StringRef());
417 Entry = cast<llvm::GlobalValue>(CGM.GetAddrOfThunk(GD, Thunk));
419 // If needed, replace the old thunk with a bitcast.
420 if (!OldThunkFn->use_empty()) {
421 llvm::Constant *NewPtrForOldDecl =
422 llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType());
423 OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
426 // Remove the old thunk.
427 OldThunkFn->eraseFromParent();
430 llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
431 bool ABIHasKeyFunctions = CGM.getTarget().getCXXABI().hasKeyFunctions();
432 bool UseAvailableExternallyLinkage = ForVTable && ABIHasKeyFunctions;
434 if (!ThunkFn->isDeclaration()) {
435 if (!ABIHasKeyFunctions || UseAvailableExternallyLinkage) {
436 // There is already a thunk emitted for this function, do nothing.
440 // Change the linkage.
441 CGM.setFunctionLinkage(GD, ThunkFn);
445 CGM.SetLLVMFunctionAttributesForDefinition(GD.getDecl(), ThunkFn);
447 if (ThunkFn->isVarArg()) {
448 // Varargs thunks are special; we can't just generate a call because
449 // we can't copy the varargs. Our implementation is rather
450 // expensive/sucky at the moment, so don't generate the thunk unless
452 // FIXME: Do something better here; GenerateVarArgsThunk is extremely ugly.
453 if (!UseAvailableExternallyLinkage) {
454 CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk);
455 CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable, GD,
456 !Thunk.Return.isEmpty());
459 // Normal thunk body generation.
460 CodeGenFunction(CGM).GenerateThunk(ThunkFn, FnInfo, GD, Thunk);
461 CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable, GD,
462 !Thunk.Return.isEmpty());
466 void CodeGenVTables::maybeEmitThunkForVTable(GlobalDecl GD,
467 const ThunkInfo &Thunk) {
468 // If the ABI has key functions, only the TU with the key function should emit
469 // the thunk. However, we can allow inlining of thunks if we emit them with
470 // available_externally linkage together with vtables when optimizations are
472 if (CGM.getTarget().getCXXABI().hasKeyFunctions() &&
473 !CGM.getCodeGenOpts().OptimizationLevel)
476 // We can't emit thunks for member functions with incomplete types.
477 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
478 if (!CGM.getTypes().isFuncTypeConvertible(
479 MD->getType()->castAs<FunctionType>()))
482 emitThunk(GD, Thunk, /*ForVTable=*/true);
485 void CodeGenVTables::EmitThunks(GlobalDecl GD)
487 const CXXMethodDecl *MD =
488 cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
490 // We don't need to generate thunks for the base destructor.
491 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
494 const VTableContextBase::ThunkInfoVectorTy *ThunkInfoVector =
495 VTContext->getThunkInfo(GD);
497 if (!ThunkInfoVector)
500 for (unsigned I = 0, E = ThunkInfoVector->size(); I != E; ++I)
501 emitThunk(GD, (*ThunkInfoVector)[I], /*ForVTable=*/false);
504 llvm::Constant *CodeGenVTables::CreateVTableInitializer(
505 const CXXRecordDecl *RD, const VTableComponent *Components,
506 unsigned NumComponents, const VTableLayout::VTableThunkTy *VTableThunks,
507 unsigned NumVTableThunks, llvm::Constant *RTTI) {
508 SmallVector<llvm::Constant *, 64> Inits;
510 llvm::Type *Int8PtrTy = CGM.Int8PtrTy;
512 llvm::Type *PtrDiffTy =
513 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
515 unsigned NextVTableThunkIndex = 0;
517 llvm::Constant *PureVirtualFn = nullptr, *DeletedVirtualFn = nullptr;
519 for (unsigned I = 0; I != NumComponents; ++I) {
520 VTableComponent Component = Components[I];
522 llvm::Constant *Init = nullptr;
524 switch (Component.getKind()) {
525 case VTableComponent::CK_VCallOffset:
526 Init = llvm::ConstantInt::get(PtrDiffTy,
527 Component.getVCallOffset().getQuantity());
528 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
530 case VTableComponent::CK_VBaseOffset:
531 Init = llvm::ConstantInt::get(PtrDiffTy,
532 Component.getVBaseOffset().getQuantity());
533 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
535 case VTableComponent::CK_OffsetToTop:
536 Init = llvm::ConstantInt::get(PtrDiffTy,
537 Component.getOffsetToTop().getQuantity());
538 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
540 case VTableComponent::CK_RTTI:
541 Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
543 case VTableComponent::CK_FunctionPointer:
544 case VTableComponent::CK_CompleteDtorPointer:
545 case VTableComponent::CK_DeletingDtorPointer: {
548 // Get the right global decl.
549 switch (Component.getKind()) {
551 llvm_unreachable("Unexpected vtable component kind");
552 case VTableComponent::CK_FunctionPointer:
553 GD = Component.getFunctionDecl();
555 case VTableComponent::CK_CompleteDtorPointer:
556 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
558 case VTableComponent::CK_DeletingDtorPointer:
559 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
563 if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
564 // We have a pure virtual member function.
565 if (!PureVirtualFn) {
566 llvm::FunctionType *Ty =
567 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
568 StringRef PureCallName = CGM.getCXXABI().GetPureVirtualCallName();
569 PureVirtualFn = CGM.CreateRuntimeFunction(Ty, PureCallName);
570 PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn,
573 Init = PureVirtualFn;
574 } else if (cast<CXXMethodDecl>(GD.getDecl())->isDeleted()) {
575 if (!DeletedVirtualFn) {
576 llvm::FunctionType *Ty =
577 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
578 StringRef DeletedCallName =
579 CGM.getCXXABI().GetDeletedVirtualCallName();
580 DeletedVirtualFn = CGM.CreateRuntimeFunction(Ty, DeletedCallName);
581 DeletedVirtualFn = llvm::ConstantExpr::getBitCast(DeletedVirtualFn,
584 Init = DeletedVirtualFn;
586 // Check if we should use a thunk.
587 if (NextVTableThunkIndex < NumVTableThunks &&
588 VTableThunks[NextVTableThunkIndex].first == I) {
589 const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
591 maybeEmitThunkForVTable(GD, Thunk);
592 Init = CGM.GetAddrOfThunk(GD, Thunk);
594 NextVTableThunkIndex++;
596 llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD);
598 Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
601 Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
606 case VTableComponent::CK_UnusedFunctionPointer:
607 Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
611 Inits.push_back(Init);
614 llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
615 return llvm::ConstantArray::get(ArrayType, Inits);
618 llvm::GlobalVariable *
619 CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD,
620 const BaseSubobject &Base,
622 llvm::GlobalVariable::LinkageTypes Linkage,
623 VTableAddressPointsMapTy& AddressPoints) {
624 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
625 DI->completeClassData(Base.getBase());
627 std::unique_ptr<VTableLayout> VTLayout(
628 getItaniumVTableContext().createConstructionVTableLayout(
629 Base.getBase(), Base.getBaseOffset(), BaseIsVirtual, RD));
631 // Add the address points.
632 AddressPoints = VTLayout->getAddressPoints();
634 // Get the mangled construction vtable name.
635 SmallString<256> OutName;
636 llvm::raw_svector_ostream Out(OutName);
637 cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
638 .mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(),
639 Base.getBase(), Out);
641 StringRef Name = OutName.str();
643 llvm::ArrayType *ArrayType =
644 llvm::ArrayType::get(CGM.Int8PtrTy, VTLayout->getNumVTableComponents());
646 // Construction vtable symbols are not part of the Itanium ABI, so we cannot
647 // guarantee that they actually will be available externally. Instead, when
648 // emitting an available_externally VTT, we provide references to an internal
649 // linkage construction vtable. The ABI only requires complete-object vtables
650 // to be the same for all instances of a type, not construction vtables.
651 if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
652 Linkage = llvm::GlobalVariable::InternalLinkage;
654 // Create the variable that will hold the construction vtable.
655 llvm::GlobalVariable *VTable =
656 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, Linkage);
657 CGM.setGlobalVisibility(VTable, RD);
659 // V-tables are always unnamed_addr.
660 VTable->setUnnamedAddr(true);
662 llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(
663 CGM.getContext().getTagDeclType(Base.getBase()));
665 // Create and set the initializer.
666 llvm::Constant *Init = CreateVTableInitializer(
667 Base.getBase(), VTLayout->vtable_component_begin(),
668 VTLayout->getNumVTableComponents(), VTLayout->vtable_thunk_begin(),
669 VTLayout->getNumVTableThunks(), RTTI);
670 VTable->setInitializer(Init);
672 CGM.EmitVTableBitSetEntries(VTable, *VTLayout.get());
677 /// Compute the required linkage of the v-table for the given class.
679 /// Note that we only call this at the end of the translation unit.
680 llvm::GlobalVariable::LinkageTypes
681 CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
682 if (!RD->isExternallyVisible())
683 return llvm::GlobalVariable::InternalLinkage;
685 // We're at the end of the translation unit, so the current key
686 // function is fully correct.
687 const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction(RD);
688 if (keyFunction && !RD->hasAttr<DLLImportAttr>()) {
689 // If this class has a key function, use that to determine the
690 // linkage of the vtable.
691 const FunctionDecl *def = nullptr;
692 if (keyFunction->hasBody(def))
693 keyFunction = cast<CXXMethodDecl>(def);
695 switch (keyFunction->getTemplateSpecializationKind()) {
697 case TSK_ExplicitSpecialization:
698 assert(def && "Should not have been asked to emit this");
699 if (keyFunction->isInlined())
700 return !Context.getLangOpts().AppleKext ?
701 llvm::GlobalVariable::LinkOnceODRLinkage :
702 llvm::Function::InternalLinkage;
704 return llvm::GlobalVariable::ExternalLinkage;
706 case TSK_ImplicitInstantiation:
707 return !Context.getLangOpts().AppleKext ?
708 llvm::GlobalVariable::LinkOnceODRLinkage :
709 llvm::Function::InternalLinkage;
711 case TSK_ExplicitInstantiationDefinition:
712 return !Context.getLangOpts().AppleKext ?
713 llvm::GlobalVariable::WeakODRLinkage :
714 llvm::Function::InternalLinkage;
716 case TSK_ExplicitInstantiationDeclaration:
717 llvm_unreachable("Should not have been asked to emit this");
721 // -fapple-kext mode does not support weak linkage, so we must use
723 if (Context.getLangOpts().AppleKext)
724 return llvm::Function::InternalLinkage;
726 llvm::GlobalVariable::LinkageTypes DiscardableODRLinkage =
727 llvm::GlobalValue::LinkOnceODRLinkage;
728 llvm::GlobalVariable::LinkageTypes NonDiscardableODRLinkage =
729 llvm::GlobalValue::WeakODRLinkage;
730 if (RD->hasAttr<DLLExportAttr>()) {
731 // Cannot discard exported vtables.
732 DiscardableODRLinkage = NonDiscardableODRLinkage;
733 } else if (RD->hasAttr<DLLImportAttr>()) {
734 // Imported vtables are available externally.
735 DiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage;
736 NonDiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage;
739 switch (RD->getTemplateSpecializationKind()) {
741 case TSK_ExplicitSpecialization:
742 case TSK_ImplicitInstantiation:
743 return DiscardableODRLinkage;
745 case TSK_ExplicitInstantiationDeclaration:
746 return llvm::GlobalVariable::ExternalLinkage;
748 case TSK_ExplicitInstantiationDefinition:
749 return NonDiscardableODRLinkage;
752 llvm_unreachable("Invalid TemplateSpecializationKind!");
755 /// This is a callback from Sema to tell us that that a particular v-table is
756 /// required to be emitted in this translation unit.
758 /// This is only called for vtables that _must_ be emitted (mainly due to key
759 /// functions). For weak vtables, CodeGen tracks when they are needed and
760 /// emits them as-needed.
761 void CodeGenModule::EmitVTable(CXXRecordDecl *theClass) {
762 VTables.GenerateClassData(theClass);
766 CodeGenVTables::GenerateClassData(const CXXRecordDecl *RD) {
767 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
768 DI->completeClassData(RD);
770 if (RD->getNumVBases())
771 CGM.getCXXABI().emitVirtualInheritanceTables(RD);
773 CGM.getCXXABI().emitVTableDefinitions(*this, RD);
776 /// At this point in the translation unit, does it appear that can we
777 /// rely on the vtable being defined elsewhere in the program?
779 /// The response is really only definitive when called at the end of
780 /// the translation unit.
782 /// The only semantic restriction here is that the object file should
783 /// not contain a v-table definition when that v-table is defined
784 /// strongly elsewhere. Otherwise, we'd just like to avoid emitting
785 /// v-tables when unnecessary.
786 bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
787 assert(RD->isDynamicClass() && "Non-dynamic classes have no VTable.");
789 // If we have an explicit instantiation declaration (and not a
790 // definition), the v-table is defined elsewhere.
791 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
792 if (TSK == TSK_ExplicitInstantiationDeclaration)
795 // Otherwise, if the class is an instantiated template, the
796 // v-table must be defined here.
797 if (TSK == TSK_ImplicitInstantiation ||
798 TSK == TSK_ExplicitInstantiationDefinition)
801 // Otherwise, if the class doesn't have a key function (possibly
802 // anymore), the v-table must be defined here.
803 const CXXMethodDecl *keyFunction = CGM.getContext().getCurrentKeyFunction(RD);
807 // Otherwise, if we don't have a definition of the key function, the
808 // v-table must be defined somewhere else.
809 return !keyFunction->hasBody();
812 /// Given that we're currently at the end of the translation unit, and
813 /// we've emitted a reference to the v-table for this class, should
814 /// we define that v-table?
815 static bool shouldEmitVTableAtEndOfTranslationUnit(CodeGenModule &CGM,
816 const CXXRecordDecl *RD) {
817 return !CGM.getVTables().isVTableExternal(RD);
820 /// Given that at some point we emitted a reference to one or more
821 /// v-tables, and that we are now at the end of the translation unit,
822 /// decide whether we should emit them.
823 void CodeGenModule::EmitDeferredVTables() {
825 // Remember the size of DeferredVTables, because we're going to assume
826 // that this entire operation doesn't modify it.
827 size_t savedSize = DeferredVTables.size();
830 typedef std::vector<const CXXRecordDecl *>::const_iterator const_iterator;
831 for (const_iterator i = DeferredVTables.begin(),
832 e = DeferredVTables.end(); i != e; ++i) {
833 const CXXRecordDecl *RD = *i;
834 if (shouldEmitVTableAtEndOfTranslationUnit(*this, RD))
835 VTables.GenerateClassData(RD);
838 assert(savedSize == DeferredVTables.size() &&
839 "deferred extra v-tables during v-table emission?");
840 DeferredVTables.clear();
843 void CodeGenModule::EmitVTableBitSetEntries(llvm::GlobalVariable *VTable,
844 const VTableLayout &VTLayout) {
845 if (!LangOpts.Sanitize.has(SanitizerKind::CFIVCall) &&
846 !LangOpts.Sanitize.has(SanitizerKind::CFINVCall) &&
847 !LangOpts.Sanitize.has(SanitizerKind::CFIDerivedCast) &&
848 !LangOpts.Sanitize.has(SanitizerKind::CFIUnrelatedCast))
851 CharUnits PointerWidth =
852 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
854 std::vector<llvm::MDTuple *> BitsetEntries;
855 // Create a bit set entry for each address point.
856 for (auto &&AP : VTLayout.getAddressPoints()) {
857 // FIXME: Add blacklisting scheme.
858 if (AP.first.getBase()->isInStdNamespace())
861 BitsetEntries.push_back(CreateVTableBitSetEntry(
862 VTable, PointerWidth * AP.second, AP.first.getBase()));
865 // Sort the bit set entries for determinism.
866 std::sort(BitsetEntries.begin(), BitsetEntries.end(), [](llvm::MDTuple *T1,
871 StringRef S1 = cast<llvm::MDString>(T1->getOperand(0))->getString();
872 StringRef S2 = cast<llvm::MDString>(T2->getOperand(0))->getString();
878 uint64_t Offset1 = cast<llvm::ConstantInt>(
879 cast<llvm::ConstantAsMetadata>(T1->getOperand(2))
880 ->getValue())->getZExtValue();
881 uint64_t Offset2 = cast<llvm::ConstantInt>(
882 cast<llvm::ConstantAsMetadata>(T2->getOperand(2))
883 ->getValue())->getZExtValue();
884 assert(Offset1 != Offset2);
885 return Offset1 < Offset2;
888 llvm::NamedMDNode *BitsetsMD =
889 getModule().getOrInsertNamedMetadata("llvm.bitsets");
890 for (auto BitsetEntry : BitsetEntries)
891 BitsetsMD->addOperand(BitsetEntry);