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 "CodeGenModule.h"
15 #include "CodeGenFunction.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"
28 using namespace clang;
29 using namespace CodeGen;
31 CodeGenVTables::CodeGenVTables(CodeGenModule &CGM)
32 : CGM(CGM), VTContext(CGM.getContext()) { }
34 bool CodeGenVTables::ShouldEmitVTableInThisTU(const CXXRecordDecl *RD) {
35 assert(RD->isDynamicClass() && "Non dynamic classes have no VTable.");
37 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
38 if (TSK == TSK_ExplicitInstantiationDeclaration)
41 const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD);
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)
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.getLangOptions().AppleKext)
58 return KeyFunction->hasBody();
61 llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
62 const ThunkInfo &Thunk) {
63 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
65 // Compute the mangled name.
66 llvm::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(),
72 getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out);
75 llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD);
76 return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true);
79 static llvm::Value *PerformTypeAdjustment(CodeGenFunction &CGF,
81 int64_t NonVirtualAdjustment,
82 int64_t VirtualAdjustment) {
83 if (!NonVirtualAdjustment && !VirtualAdjustment)
86 llvm::Type *Int8PtrTy =
87 llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
89 llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy);
91 if (NonVirtualAdjustment) {
92 // Do the non-virtual adjustment.
93 V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment);
96 if (VirtualAdjustment) {
97 llvm::Type *PtrDiffTy =
98 CGF.ConvertType(CGF.getContext().getPointerDiffType());
100 // Do the virtual adjustment.
101 llvm::Value *VTablePtrPtr =
102 CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo());
104 llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr);
106 llvm::Value *OffsetPtr =
107 CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
109 OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo());
111 // Load the adjustment offset from the vtable.
112 llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr);
114 // Adjust our pointer.
115 V = CGF.Builder.CreateInBoundsGEP(V, Offset);
118 // Cast back to the original type.
119 return CGF.Builder.CreateBitCast(V, Ptr->getType());
122 static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD,
123 const ThunkInfo &Thunk, llvm::Function *Fn) {
124 CGM.setGlobalVisibility(Fn, MD);
126 if (!CGM.getCodeGenOpts().HiddenWeakVTables)
129 // If the thunk has weak/linkonce linkage, but the function must be
130 // emitted in every translation unit that references it, then we can
131 // emit its thunks with hidden visibility, since its thunks must be
132 // emitted when the function is.
134 // This follows CodeGenModule::setTypeVisibility; see the comments
135 // there for explanation.
137 if ((Fn->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage &&
138 Fn->getLinkage() != llvm::GlobalVariable::WeakODRLinkage) ||
139 Fn->getVisibility() != llvm::GlobalVariable::DefaultVisibility)
142 if (MD->getExplicitVisibility())
145 switch (MD->getTemplateSpecializationKind()) {
146 case TSK_ExplicitInstantiationDefinition:
147 case TSK_ExplicitInstantiationDeclaration:
153 case TSK_ExplicitSpecialization:
154 case TSK_ImplicitInstantiation:
155 if (!CGM.getCodeGenOpts().HiddenWeakTemplateVTables)
160 // If there's an explicit definition, and that definition is
161 // out-of-line, then we can't assume that all users will have a
162 // definition to emit.
163 const FunctionDecl *Def = 0;
164 if (MD->hasBody(Def) && Def->isOutOfLine())
167 Fn->setVisibility(llvm::GlobalValue::HiddenVisibility);
171 static bool similar(const ABIArgInfo &infoL, CanQualType typeL,
172 const ABIArgInfo &infoR, CanQualType typeR) {
173 return (infoL.getKind() == infoR.getKind() &&
175 (isa<PointerType>(typeL) && isa<PointerType>(typeR)) ||
176 (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR))));
180 static RValue PerformReturnAdjustment(CodeGenFunction &CGF,
181 QualType ResultType, RValue RV,
182 const ThunkInfo &Thunk) {
183 // Emit the return adjustment.
184 bool NullCheckValue = !ResultType->isReferenceType();
186 llvm::BasicBlock *AdjustNull = 0;
187 llvm::BasicBlock *AdjustNotNull = 0;
188 llvm::BasicBlock *AdjustEnd = 0;
190 llvm::Value *ReturnValue = RV.getScalarVal();
192 if (NullCheckValue) {
193 AdjustNull = CGF.createBasicBlock("adjust.null");
194 AdjustNotNull = CGF.createBasicBlock("adjust.notnull");
195 AdjustEnd = CGF.createBasicBlock("adjust.end");
197 llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue);
198 CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
199 CGF.EmitBlock(AdjustNotNull);
202 ReturnValue = PerformTypeAdjustment(CGF, ReturnValue,
203 Thunk.Return.NonVirtual,
204 Thunk.Return.VBaseOffsetOffset);
206 if (NullCheckValue) {
207 CGF.Builder.CreateBr(AdjustEnd);
208 CGF.EmitBlock(AdjustNull);
209 CGF.Builder.CreateBr(AdjustEnd);
210 CGF.EmitBlock(AdjustEnd);
212 llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2);
213 PHI->addIncoming(ReturnValue, AdjustNotNull);
214 PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
219 return RValue::get(ReturnValue);
222 // This function does roughly the same thing as GenerateThunk, but in a
223 // very different way, so that va_start and va_end work correctly.
224 // FIXME: This function assumes "this" is the first non-sret LLVM argument of
225 // a function, and that there is an alloca built in the entry block
226 // for all accesses to "this".
227 // FIXME: This function assumes there is only one "ret" statement per function.
228 // FIXME: Cloning isn't correct in the presence of indirect goto!
229 // FIXME: This implementation of thunks bloats codesize by duplicating the
230 // function definition. There are alternatives:
231 // 1. Add some sort of stub support to LLVM for cases where we can
232 // do a this adjustment, then a sibcall.
233 // 2. We could transform the definition to take a va_list instead of an
234 // actual variable argument list, then have the thunks (including a
235 // no-op thunk for the regular definition) call va_start/va_end.
236 // There's a bit of per-call overhead for this solution, but it's
237 // better for codesize if the definition is long.
238 void CodeGenFunction::GenerateVarArgsThunk(
240 const CGFunctionInfo &FnInfo,
241 GlobalDecl GD, const ThunkInfo &Thunk) {
242 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
243 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
244 QualType ResultType = FPT->getResultType();
246 // Get the original function
248 CGM.getTypes().GetFunctionType(FnInfo, /*IsVariadic*/true);
249 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
250 llvm::Function *BaseFn = cast<llvm::Function>(Callee);
253 llvm::Function *NewFn = llvm::CloneFunction(BaseFn);
254 CGM.getModule().getFunctionList().push_back(NewFn);
255 Fn->replaceAllUsesWith(NewFn);
257 Fn->eraseFromParent();
260 // "Initialize" CGF (minimally).
263 // Get the "this" value
264 llvm::Function::arg_iterator AI = Fn->arg_begin();
265 if (CGM.ReturnTypeUsesSRet(FnInfo))
268 // Find the first store of "this", which will be to the alloca associated
270 llvm::Value *ThisPtr = &*AI;
271 llvm::BasicBlock *EntryBB = Fn->begin();
272 llvm::Instruction *ThisStore = 0;
273 for (llvm::BasicBlock::iterator I = EntryBB->begin(), E = EntryBB->end();
275 if (isa<llvm::StoreInst>(I) && I->getOperand(0) == ThisPtr) {
276 ThisStore = cast<llvm::StoreInst>(I);
280 assert(ThisStore && "Store of this should be in entry block?");
281 // Adjust "this", if necessary.
282 Builder.SetInsertPoint(ThisStore);
283 llvm::Value *AdjustedThisPtr =
284 PerformTypeAdjustment(*this, ThisPtr,
285 Thunk.This.NonVirtual,
286 Thunk.This.VCallOffsetOffset);
287 ThisStore->setOperand(0, AdjustedThisPtr);
289 if (!Thunk.Return.isEmpty()) {
290 // Fix up the returned value, if necessary.
291 for (llvm::Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++) {
292 llvm::Instruction *T = I->getTerminator();
293 if (isa<llvm::ReturnInst>(T)) {
294 RValue RV = RValue::get(T->getOperand(0));
295 T->eraseFromParent();
296 Builder.SetInsertPoint(&*I);
297 RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
298 Builder.CreateRet(RV.getScalarVal());
305 void CodeGenFunction::GenerateThunk(llvm::Function *Fn,
306 const CGFunctionInfo &FnInfo,
307 GlobalDecl GD, const ThunkInfo &Thunk) {
308 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
309 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
310 QualType ResultType = FPT->getResultType();
311 QualType ThisType = MD->getThisType(getContext());
313 FunctionArgList FunctionArgs;
315 // FIXME: It would be nice if more of this code could be shared with
316 // CodeGenFunction::GenerateCode.
318 // Create the implicit 'this' parameter declaration.
320 CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResultType, FunctionArgs);
322 // Add the rest of the parameters.
323 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
324 E = MD->param_end(); I != E; ++I) {
325 ParmVarDecl *Param = *I;
327 FunctionArgs.push_back(Param);
330 StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs,
333 CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
335 // Adjust the 'this' pointer if necessary.
336 llvm::Value *AdjustedThisPtr =
337 PerformTypeAdjustment(*this, LoadCXXThis(),
338 Thunk.This.NonVirtual,
339 Thunk.This.VCallOffsetOffset);
341 CallArgList CallArgs;
343 // Add our adjusted 'this' pointer.
344 CallArgs.add(RValue::get(AdjustedThisPtr), ThisType);
346 // Add the rest of the parameters.
347 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
348 E = MD->param_end(); I != E; ++I) {
349 ParmVarDecl *param = *I;
350 EmitDelegateCallArg(CallArgs, param);
355 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(GD),
357 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
360 const CGFunctionInfo &CallFnInfo =
361 CGM.getTypes().getFunctionInfo(ResultType, CallArgs, FPT->getExtInfo());
362 assert(CallFnInfo.getRegParm() == FnInfo.getRegParm() &&
363 CallFnInfo.isNoReturn() == FnInfo.isNoReturn() &&
364 CallFnInfo.getCallingConvention() == FnInfo.getCallingConvention());
365 assert(similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(),
366 FnInfo.getReturnInfo(), FnInfo.getReturnType()));
367 assert(CallFnInfo.arg_size() == FnInfo.arg_size());
368 for (unsigned i = 0, e = FnInfo.arg_size(); i != e; ++i)
369 assert(similar(CallFnInfo.arg_begin()[i].info,
370 CallFnInfo.arg_begin()[i].type,
371 FnInfo.arg_begin()[i].info, FnInfo.arg_begin()[i].type));
374 // Determine whether we have a return value slot to use.
375 ReturnValueSlot Slot;
376 if (!ResultType->isVoidType() &&
377 FnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect &&
378 hasAggregateLLVMType(CurFnInfo->getReturnType()))
379 Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified());
381 // Now emit our call.
382 RValue RV = EmitCall(FnInfo, Callee, Slot, CallArgs, MD);
384 if (!Thunk.Return.isEmpty())
385 RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
387 if (!ResultType->isVoidType() && Slot.isNull())
388 CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType);
392 // Set the right linkage.
393 CGM.setFunctionLinkage(MD, Fn);
395 // Set the right visibility.
396 setThunkVisibility(CGM, MD, Thunk, Fn);
399 void CodeGenVTables::EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
400 bool UseAvailableExternallyLinkage)
402 const CGFunctionInfo &FnInfo = CGM.getTypes().getFunctionInfo(GD);
404 // FIXME: re-use FnInfo in this computation.
405 llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk);
407 // Strip off a bitcast if we got one back.
408 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
409 assert(CE->getOpcode() == llvm::Instruction::BitCast);
410 Entry = CE->getOperand(0);
413 // There's already a declaration with the same name, check if it has the same
414 // type or if we need to replace it.
415 if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() !=
416 CGM.getTypes().GetFunctionTypeForVTable(GD)) {
417 llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(Entry);
419 // If the types mismatch then we have to rewrite the definition.
420 assert(OldThunkFn->isDeclaration() &&
421 "Shouldn't replace non-declaration");
423 // Remove the name from the old thunk function and get a new thunk.
424 OldThunkFn->setName(StringRef());
425 Entry = CGM.GetAddrOfThunk(GD, Thunk);
427 // If needed, replace the old thunk with a bitcast.
428 if (!OldThunkFn->use_empty()) {
429 llvm::Constant *NewPtrForOldDecl =
430 llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType());
431 OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
434 // Remove the old thunk.
435 OldThunkFn->eraseFromParent();
438 llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
440 if (!ThunkFn->isDeclaration()) {
441 if (UseAvailableExternallyLinkage) {
442 // There is already a thunk emitted for this function, do nothing.
446 // If a function has a body, it should have available_externally linkage.
447 assert(ThunkFn->hasAvailableExternallyLinkage() &&
448 "Function should have available_externally linkage!");
450 // Change the linkage.
451 CGM.setFunctionLinkage(cast<CXXMethodDecl>(GD.getDecl()), ThunkFn);
455 if (ThunkFn->isVarArg()) {
456 // Varargs thunks are special; we can't just generate a call because
457 // we can't copy the varargs. Our implementation is rather
458 // expensive/sucky at the moment, so don't generate the thunk unless
460 // FIXME: Do something better here; GenerateVarArgsThunk is extremely ugly.
461 if (!UseAvailableExternallyLinkage)
462 CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk);
464 // Normal thunk body generation.
465 CodeGenFunction(CGM).GenerateThunk(ThunkFn, FnInfo, GD, Thunk);
468 if (UseAvailableExternallyLinkage)
469 ThunkFn->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
472 void CodeGenVTables::MaybeEmitThunkAvailableExternally(GlobalDecl GD,
473 const ThunkInfo &Thunk) {
474 // We only want to do this when building with optimizations.
475 if (!CGM.getCodeGenOpts().OptimizationLevel)
478 // We can't emit thunks for member functions with incomplete types.
479 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
480 if (!CGM.getTypes().isFuncTypeConvertible(
481 cast<FunctionType>(MD->getType().getTypePtr())))
484 EmitThunk(GD, Thunk, /*UseAvailableExternallyLinkage=*/true);
487 void CodeGenVTables::EmitThunks(GlobalDecl GD)
489 const CXXMethodDecl *MD =
490 cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
492 // We don't need to generate thunks for the base destructor.
493 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
496 const VTableContext::ThunkInfoVectorTy *ThunkInfoVector =
497 VTContext.getThunkInfo(MD);
498 if (!ThunkInfoVector)
501 for (unsigned I = 0, E = ThunkInfoVector->size(); I != E; ++I)
502 EmitThunk(GD, (*ThunkInfoVector)[I],
503 /*UseAvailableExternallyLinkage=*/false);
507 CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD,
508 const VTableComponent *Components,
509 unsigned NumComponents,
510 const VTableLayout::VTableThunkTy *VTableThunks,
511 unsigned NumVTableThunks) {
512 SmallVector<llvm::Constant *, 64> Inits;
514 llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
516 llvm::Type *PtrDiffTy =
517 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
519 QualType ClassType = CGM.getContext().getTagDeclType(RD);
520 llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType);
522 unsigned NextVTableThunkIndex = 0;
524 llvm::Constant* PureVirtualFn = 0;
526 for (unsigned I = 0; I != NumComponents; ++I) {
527 VTableComponent Component = Components[I];
529 llvm::Constant *Init = 0;
531 switch (Component.getKind()) {
532 case VTableComponent::CK_VCallOffset:
533 Init = llvm::ConstantInt::get(PtrDiffTy,
534 Component.getVCallOffset().getQuantity());
535 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
537 case VTableComponent::CK_VBaseOffset:
538 Init = llvm::ConstantInt::get(PtrDiffTy,
539 Component.getVBaseOffset().getQuantity());
540 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
542 case VTableComponent::CK_OffsetToTop:
543 Init = llvm::ConstantInt::get(PtrDiffTy,
544 Component.getOffsetToTop().getQuantity());
545 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
547 case VTableComponent::CK_RTTI:
548 Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
550 case VTableComponent::CK_FunctionPointer:
551 case VTableComponent::CK_CompleteDtorPointer:
552 case VTableComponent::CK_DeletingDtorPointer: {
555 // Get the right global decl.
556 switch (Component.getKind()) {
558 llvm_unreachable("Unexpected vtable component kind");
559 case VTableComponent::CK_FunctionPointer:
560 GD = Component.getFunctionDecl();
562 case VTableComponent::CK_CompleteDtorPointer:
563 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
565 case VTableComponent::CK_DeletingDtorPointer:
566 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
570 if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
571 // We have a pure virtual member function.
572 if (!PureVirtualFn) {
573 llvm::FunctionType *Ty =
574 llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()),
577 CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual");
578 PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn,
582 Init = PureVirtualFn;
584 // Check if we should use a thunk.
585 if (NextVTableThunkIndex < NumVTableThunks &&
586 VTableThunks[NextVTableThunkIndex].first == I) {
587 const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
589 Init = CGM.GetAddrOfThunk(GD, Thunk);
590 MaybeEmitThunkAvailableExternally(GD, Thunk);
592 NextVTableThunkIndex++;
594 llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD);
596 Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
599 Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
604 case VTableComponent::CK_UnusedFunctionPointer:
605 Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
609 Inits.push_back(Init);
612 llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
613 return llvm::ConstantArray::get(ArrayType, Inits);
616 llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTable(const CXXRecordDecl *RD) {
617 llvm::GlobalVariable *&VTable = VTables[RD];
621 // We may need to generate a definition for this vtable.
622 if (ShouldEmitVTableInThisTU(RD))
623 CGM.DeferredVTables.push_back(RD);
625 llvm::SmallString<256> OutName;
626 llvm::raw_svector_ostream Out(OutName);
627 CGM.getCXXABI().getMangleContext().mangleCXXVTable(RD, Out);
629 StringRef Name = OutName.str();
631 llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
632 llvm::ArrayType *ArrayType =
633 llvm::ArrayType::get(Int8PtrTy,
634 VTContext.getVTableLayout(RD).getNumVTableComponents());
637 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType,
638 llvm::GlobalValue::ExternalLinkage);
639 VTable->setUnnamedAddr(true);
644 CodeGenVTables::EmitVTableDefinition(llvm::GlobalVariable *VTable,
645 llvm::GlobalVariable::LinkageTypes Linkage,
646 const CXXRecordDecl *RD) {
647 const VTableLayout &VTLayout = VTContext.getVTableLayout(RD);
649 // Create and set the initializer.
650 llvm::Constant *Init =
651 CreateVTableInitializer(RD,
652 VTLayout.vtable_component_begin(),
653 VTLayout.getNumVTableComponents(),
654 VTLayout.vtable_thunk_begin(),
655 VTLayout.getNumVTableThunks());
656 VTable->setInitializer(Init);
658 // Set the correct linkage.
659 VTable->setLinkage(Linkage);
661 // Set the right visibility.
662 CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForVTable);
665 llvm::GlobalVariable *
666 CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD,
667 const BaseSubobject &Base,
669 llvm::GlobalVariable::LinkageTypes Linkage,
670 VTableAddressPointsMapTy& AddressPoints) {
671 llvm::OwningPtr<VTableLayout> VTLayout(
672 VTContext.createConstructionVTableLayout(Base.getBase(),
673 Base.getBaseOffset(),
676 // Add the address points.
677 AddressPoints = VTLayout->getAddressPoints();
679 // Get the mangled construction vtable name.
680 llvm::SmallString<256> OutName;
681 llvm::raw_svector_ostream Out(OutName);
682 CGM.getCXXABI().getMangleContext().
683 mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(), Base.getBase(),
686 StringRef Name = OutName.str();
688 llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
689 llvm::ArrayType *ArrayType =
690 llvm::ArrayType::get(Int8PtrTy, VTLayout->getNumVTableComponents());
692 // Create the variable that will hold the construction vtable.
693 llvm::GlobalVariable *VTable =
694 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, Linkage);
695 CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForConstructionVTable);
697 // V-tables are always unnamed_addr.
698 VTable->setUnnamedAddr(true);
700 // Create and set the initializer.
701 llvm::Constant *Init =
702 CreateVTableInitializer(Base.getBase(),
703 VTLayout->vtable_component_begin(),
704 VTLayout->getNumVTableComponents(),
705 VTLayout->vtable_thunk_begin(),
706 VTLayout->getNumVTableThunks());
707 VTable->setInitializer(Init);
713 CodeGenVTables::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
714 const CXXRecordDecl *RD) {
715 llvm::GlobalVariable *VTable = GetAddrOfVTable(RD);
716 if (VTable->hasInitializer())
719 EmitVTableDefinition(VTable, Linkage, RD);
721 if (RD->getNumVBases()) {
722 llvm::GlobalVariable *VTT = GetAddrOfVTT(RD);
723 EmitVTTDefinition(VTT, Linkage, RD);
726 // If this is the magic class __cxxabiv1::__fundamental_type_info,
727 // we will emit the typeinfo for the fundamental types. This is the
728 // same behaviour as GCC.
729 const DeclContext *DC = RD->getDeclContext();
730 if (RD->getIdentifier() &&
731 RD->getIdentifier()->isStr("__fundamental_type_info") &&
732 isa<NamespaceDecl>(DC) &&
733 cast<NamespaceDecl>(DC)->getIdentifier() &&
734 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") &&
735 DC->getParent()->isTranslationUnit())
736 CGM.EmitFundamentalRTTIDescriptors();