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 "CodeGenFunction.h"
15 #include "CGCXXABI.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"
26 #include <algorithm>
27 #include <cstdio>
28
29 using namespace clang;
30 using namespace CodeGen;
31
CodeGenVTables(CodeGenModule & CGM)32 CodeGenVTables::CodeGenVTables(CodeGenModule &CGM)
33 : CGM(CGM), VTContext(CGM.getContext().getVTableContext()) {}
34
GetAddrOfThunk(GlobalDecl GD,const ThunkInfo & Thunk)35 llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
36 const ThunkInfo &Thunk) {
37 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
38
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(),
44 Thunk.This, Out);
45 else
46 getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out);
47 Out.flush();
48
49 llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD);
50 return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true,
51 /*DontDefer*/ true);
52 }
53
setThunkVisibility(CodeGenModule & CGM,const CXXMethodDecl * MD,const ThunkInfo & Thunk,llvm::Function * Fn)54 static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD,
55 const ThunkInfo &Thunk, llvm::Function *Fn) {
56 CGM.setGlobalVisibility(Fn, MD);
57 }
58
59 #ifndef NDEBUG
similar(const ABIArgInfo & infoL,CanQualType typeL,const ABIArgInfo & infoR,CanQualType typeR)60 static bool similar(const ABIArgInfo &infoL, CanQualType typeL,
61 const ABIArgInfo &infoR, CanQualType typeR) {
62 return (infoL.getKind() == infoR.getKind() &&
63 (typeL == typeR ||
64 (isa<PointerType>(typeL) && isa<PointerType>(typeR)) ||
65 (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR))));
66 }
67 #endif
68
PerformReturnAdjustment(CodeGenFunction & CGF,QualType ResultType,RValue RV,const ThunkInfo & Thunk)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();
74
75 llvm::BasicBlock *AdjustNull = nullptr;
76 llvm::BasicBlock *AdjustNotNull = nullptr;
77 llvm::BasicBlock *AdjustEnd = nullptr;
78
79 llvm::Value *ReturnValue = RV.getScalarVal();
80
81 if (NullCheckValue) {
82 AdjustNull = CGF.createBasicBlock("adjust.null");
83 AdjustNotNull = CGF.createBasicBlock("adjust.notnull");
84 AdjustEnd = CGF.createBasicBlock("adjust.end");
85
86 llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue);
87 CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
88 CGF.EmitBlock(AdjustNotNull);
89 }
90
91 ReturnValue = CGF.CGM.getCXXABI().performReturnAdjustment(CGF, ReturnValue,
92 Thunk.Return);
93
94 if (NullCheckValue) {
95 CGF.Builder.CreateBr(AdjustEnd);
96 CGF.EmitBlock(AdjustNull);
97 CGF.Builder.CreateBr(AdjustEnd);
98 CGF.EmitBlock(AdjustEnd);
99
100 llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2);
101 PHI->addIncoming(ReturnValue, AdjustNotNull);
102 PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
103 AdjustNull);
104 ReturnValue = PHI;
105 }
106
107 return RValue::get(ReturnValue);
108 }
109
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.
GenerateVarArgsThunk(llvm::Function * Fn,const CGFunctionInfo & FnInfo,GlobalDecl GD,const ThunkInfo & Thunk)126 void CodeGenFunction::GenerateVarArgsThunk(
127 llvm::Function *Fn,
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();
133
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);
139
140 // Clone to thunk.
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);
146 NewFn->takeName(Fn);
147 Fn->eraseFromParent();
148 Fn = NewFn;
149
150 // "Initialize" CGF (minimally).
151 CurFn = Fn;
152
153 // Get the "this" value
154 llvm::Function::arg_iterator AI = Fn->arg_begin();
155 if (CGM.ReturnTypeUsesSRet(FnInfo))
156 ++AI;
157
158 // Find the first store of "this", which will be to the alloca associated
159 // with "this".
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();
164 I != E; I++) {
165 if (isa<llvm::StoreInst>(I) && I->getOperand(0) == ThisPtr) {
166 ThisStore = cast<llvm::StoreInst>(I);
167 break;
168 }
169 }
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);
176
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());
187 break;
188 }
189 }
190 }
191 }
192
StartThunk(llvm::Function * Fn,GlobalDecl GD,const CGFunctionInfo & FnInfo)193 void CodeGenFunction::StartThunk(llvm::Function *Fn, GlobalDecl GD,
194 const CGFunctionInfo &FnInfo) {
195 assert(!CurGD.getDecl() && "CurGD was already set!");
196 CurGD = GD;
197
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;
205
206 // Create the implicit 'this' parameter declaration.
207 CGM.getCXXABI().buildThisParam(*this, FunctionArgs);
208
209 // Add the rest of the parameters.
210 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
211 E = MD->param_end();
212 I != E; ++I)
213 FunctionArgs.push_back(*I);
214
215 if (isa<CXXDestructorDecl>(MD))
216 CGM.getCXXABI().addImplicitStructorParams(*this, ResultType, FunctionArgs);
217
218 // Start defining the function.
219 StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs,
220 MD->getLocation(), SourceLocation());
221
222 // Since we didn't pass a GlobalDecl to StartFunction, do this ourselves.
223 CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
224 CXXThisValue = CXXABIThisValue;
225 }
226
EmitCallAndReturnForThunk(GlobalDecl GD,llvm::Value * Callee,const ThunkInfo * Thunk)227 void CodeGenFunction::EmitCallAndReturnForThunk(GlobalDecl GD,
228 llvm::Value *Callee,
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());
233
234 // Adjust the 'this' pointer if necessary
235 llvm::Value *AdjustedThisPtr = Thunk ? CGM.getCXXABI().performThisAdjustment(
236 *this, LoadCXXThis(), Thunk->This)
237 : LoadCXXThis();
238
239 // Start building CallArgs.
240 CallArgList CallArgs;
241 QualType ThisType = MD->getThisType(getContext());
242 CallArgs.add(RValue::get(AdjustedThisPtr), ThisType);
243
244 if (isa<CXXDestructorDecl>(MD))
245 CGM.getCXXABI().adjustCallArgsForDestructorThunk(*this, GD, CallArgs);
246
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());
251
252 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
253
254 #ifndef NDEBUG
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));
270 #endif
271
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());
280
281 // Now emit our call.
282 RValue RV = EmitCall(*CurFnInfo, Callee, Slot, CallArgs, MD);
283
284 // Consider return adjustment if we have ThunkInfo.
285 if (Thunk && !Thunk->Return.isEmpty())
286 RV = PerformReturnAdjustment(*this, ResultType, RV, *Thunk);
287
288 // Emit return.
289 if (!ResultType->isVoidType() && Slot.isNull())
290 CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType);
291
292 // Disable the final ARC autorelease.
293 AutoreleaseResult = false;
294
295 FinishFunction();
296 }
297
GenerateThunk(llvm::Function * Fn,const CGFunctionInfo & FnInfo,GlobalDecl GD,const ThunkInfo & Thunk)298 void CodeGenFunction::GenerateThunk(llvm::Function *Fn,
299 const CGFunctionInfo &FnInfo,
300 GlobalDecl GD, const ThunkInfo &Thunk) {
301 StartThunk(Fn, GD, FnInfo);
302
303 // Get our callee.
304 llvm::Type *Ty =
305 CGM.getTypes().GetFunctionType(CGM.getTypes().arrangeGlobalDeclaration(GD));
306 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
307
308 // Make the call and return the result.
309 EmitCallAndReturnForThunk(GD, Callee, &Thunk);
310
311 // Set the right linkage.
312 CGM.setFunctionLinkage(GD, Fn);
313
314 // Set the right visibility.
315 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
316 setThunkVisibility(CGM, MD, Thunk, Fn);
317 }
318
emitThunk(GlobalDecl GD,const ThunkInfo & Thunk,bool ForVTable)319 void CodeGenVTables::emitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
320 bool ForVTable) {
321 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeGlobalDeclaration(GD);
322
323 // FIXME: re-use FnInfo in this computation.
324 llvm::Constant *C = CGM.GetAddrOfThunk(GD, Thunk);
325 llvm::GlobalValue *Entry;
326
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));
331 } else {
332 Entry = cast<llvm::GlobalValue>(C);
333 }
334
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;
340
341 // If the types mismatch then we have to rewrite the definition.
342 assert(OldThunkFn->isDeclaration() &&
343 "Shouldn't replace non-declaration");
344
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));
348
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);
354 }
355
356 // Remove the old thunk.
357 OldThunkFn->eraseFromParent();
358 }
359
360 llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
361 bool ABIHasKeyFunctions = CGM.getTarget().getCXXABI().hasKeyFunctions();
362 bool UseAvailableExternallyLinkage = ForVTable && ABIHasKeyFunctions;
363
364 if (!ThunkFn->isDeclaration()) {
365 if (!ABIHasKeyFunctions || UseAvailableExternallyLinkage) {
366 // There is already a thunk emitted for this function, do nothing.
367 return;
368 }
369
370 // Change the linkage.
371 CGM.setFunctionLinkage(GD, ThunkFn);
372 return;
373 }
374
375 CGM.SetLLVMFunctionAttributesForDefinition(GD.getDecl(), ThunkFn);
376
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
381 // we have to.
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());
387 }
388 } else {
389 // Normal thunk body generation.
390 CodeGenFunction(CGM).GenerateThunk(ThunkFn, FnInfo, GD, Thunk);
391 CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable, GD,
392 !Thunk.Return.isEmpty());
393 }
394 }
395
maybeEmitThunkForVTable(GlobalDecl GD,const ThunkInfo & Thunk)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
401 // enabled.
402 if (CGM.getTarget().getCXXABI().hasKeyFunctions() &&
403 !CGM.getCodeGenOpts().OptimizationLevel)
404 return;
405
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>()))
410 return;
411
412 emitThunk(GD, Thunk, /*ForVTable=*/true);
413 }
414
EmitThunks(GlobalDecl GD)415 void CodeGenVTables::EmitThunks(GlobalDecl GD)
416 {
417 const CXXMethodDecl *MD =
418 cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
419
420 // We don't need to generate thunks for the base destructor.
421 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
422 return;
423
424 const VTableContextBase::ThunkInfoVectorTy *ThunkInfoVector =
425 VTContext->getThunkInfo(GD);
426
427 if (!ThunkInfoVector)
428 return;
429
430 for (unsigned I = 0, E = ThunkInfoVector->size(); I != E; ++I)
431 emitThunk(GD, (*ThunkInfoVector)[I], /*ForVTable=*/false);
432 }
433
CreateVTableInitializer(const CXXRecordDecl * RD,const VTableComponent * Components,unsigned NumComponents,const VTableLayout::VTableThunkTy * VTableThunks,unsigned NumVTableThunks,llvm::Constant * RTTI)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;
439
440 llvm::Type *Int8PtrTy = CGM.Int8PtrTy;
441
442 llvm::Type *PtrDiffTy =
443 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
444
445 unsigned NextVTableThunkIndex = 0;
446
447 llvm::Constant *PureVirtualFn = nullptr, *DeletedVirtualFn = nullptr;
448
449 for (unsigned I = 0; I != NumComponents; ++I) {
450 VTableComponent Component = Components[I];
451
452 llvm::Constant *Init = nullptr;
453
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);
459 break;
460 case VTableComponent::CK_VBaseOffset:
461 Init = llvm::ConstantInt::get(PtrDiffTy,
462 Component.getVBaseOffset().getQuantity());
463 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
464 break;
465 case VTableComponent::CK_OffsetToTop:
466 Init = llvm::ConstantInt::get(PtrDiffTy,
467 Component.getOffsetToTop().getQuantity());
468 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
469 break;
470 case VTableComponent::CK_RTTI:
471 Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
472 break;
473 case VTableComponent::CK_FunctionPointer:
474 case VTableComponent::CK_CompleteDtorPointer:
475 case VTableComponent::CK_DeletingDtorPointer: {
476 GlobalDecl GD;
477
478 // Get the right global decl.
479 switch (Component.getKind()) {
480 default:
481 llvm_unreachable("Unexpected vtable component kind");
482 case VTableComponent::CK_FunctionPointer:
483 GD = Component.getFunctionDecl();
484 break;
485 case VTableComponent::CK_CompleteDtorPointer:
486 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
487 break;
488 case VTableComponent::CK_DeletingDtorPointer:
489 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
490 break;
491 }
492
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,
501 CGM.Int8PtrTy);
502 }
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,
512 CGM.Int8PtrTy);
513 }
514 Init = DeletedVirtualFn;
515 } else {
516 // Check if we should use a thunk.
517 if (NextVTableThunkIndex < NumVTableThunks &&
518 VTableThunks[NextVTableThunkIndex].first == I) {
519 const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
520
521 maybeEmitThunkForVTable(GD, Thunk);
522 Init = CGM.GetAddrOfThunk(GD, Thunk);
523
524 NextVTableThunkIndex++;
525 } else {
526 llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD);
527
528 Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
529 }
530
531 Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
532 }
533 break;
534 }
535
536 case VTableComponent::CK_UnusedFunctionPointer:
537 Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
538 break;
539 };
540
541 Inits.push_back(Init);
542 }
543
544 llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
545 return llvm::ConstantArray::get(ArrayType, Inits);
546 }
547
548 llvm::GlobalVariable *
GenerateConstructionVTable(const CXXRecordDecl * RD,const BaseSubobject & Base,bool BaseIsVirtual,llvm::GlobalVariable::LinkageTypes Linkage,VTableAddressPointsMapTy & AddressPoints)549 CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD,
550 const BaseSubobject &Base,
551 bool BaseIsVirtual,
552 llvm::GlobalVariable::LinkageTypes Linkage,
553 VTableAddressPointsMapTy& AddressPoints) {
554 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
555 DI->completeClassData(Base.getBase());
556
557 std::unique_ptr<VTableLayout> VTLayout(
558 getItaniumVTableContext().createConstructionVTableLayout(
559 Base.getBase(), Base.getBaseOffset(), BaseIsVirtual, RD));
560
561 // Add the address points.
562 AddressPoints = VTLayout->getAddressPoints();
563
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);
570 Out.flush();
571 StringRef Name = OutName.str();
572
573 llvm::ArrayType *ArrayType =
574 llvm::ArrayType::get(CGM.Int8PtrTy, VTLayout->getNumVTableComponents());
575
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;
583
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);
588
589 // V-tables are always unnamed_addr.
590 VTable->setUnnamedAddr(true);
591
592 llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(
593 CGM.getContext().getTagDeclType(Base.getBase()));
594
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);
601
602 return VTable;
603 }
604
605 /// Compute the required linkage of the v-table for the given class.
606 ///
607 /// Note that we only call this at the end of the translation unit.
608 llvm::GlobalVariable::LinkageTypes
getVTableLinkage(const CXXRecordDecl * RD)609 CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
610 if (!RD->isExternallyVisible())
611 return llvm::GlobalVariable::InternalLinkage;
612
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);
621
622 switch (keyFunction->getTemplateSpecializationKind()) {
623 case TSK_Undeclared:
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;
630
631 return llvm::GlobalVariable::ExternalLinkage;
632
633 case TSK_ImplicitInstantiation:
634 return !Context.getLangOpts().AppleKext ?
635 llvm::GlobalVariable::LinkOnceODRLinkage :
636 llvm::Function::InternalLinkage;
637
638 case TSK_ExplicitInstantiationDefinition:
639 return !Context.getLangOpts().AppleKext ?
640 llvm::GlobalVariable::WeakODRLinkage :
641 llvm::Function::InternalLinkage;
642
643 case TSK_ExplicitInstantiationDeclaration:
644 llvm_unreachable("Should not have been asked to emit this");
645 }
646 }
647
648 // -fapple-kext mode does not support weak linkage, so we must use
649 // internal linkage.
650 if (Context.getLangOpts().AppleKext)
651 return llvm::Function::InternalLinkage;
652
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;
664 }
665
666 switch (RD->getTemplateSpecializationKind()) {
667 case TSK_Undeclared:
668 case TSK_ExplicitSpecialization:
669 case TSK_ImplicitInstantiation:
670 return DiscardableODRLinkage;
671
672 case TSK_ExplicitInstantiationDeclaration:
673 llvm_unreachable("Should not have been asked to emit this");
674
675 case TSK_ExplicitInstantiationDefinition:
676 return NonDiscardableODRLinkage;
677 }
678
679 llvm_unreachable("Invalid TemplateSpecializationKind!");
680 }
681
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
684 /// unit.
685 ///
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.
689 ///
690 /// \param isRequired - if true, the v-table is mandatory, e.g.
691 /// because the translation unit defines the key function
EmitVTable(CXXRecordDecl * theClass,bool isRequired)692 void CodeGenModule::EmitVTable(CXXRecordDecl *theClass, bool isRequired) {
693 if (!isRequired) return;
694
695 VTables.GenerateClassData(theClass);
696 }
697
698 void
GenerateClassData(const CXXRecordDecl * RD)699 CodeGenVTables::GenerateClassData(const CXXRecordDecl *RD) {
700 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
701 DI->completeClassData(RD);
702
703 if (RD->getNumVBases())
704 CGM.getCXXABI().emitVirtualInheritanceTables(RD);
705
706 CGM.getCXXABI().emitVTableDefinitions(*this, RD);
707 }
708
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?
711 ///
712 /// The response is really only definitive when called at the end of
713 /// the translation unit.
714 ///
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.
isVTableExternal(const CXXRecordDecl * RD)719 bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
720 assert(RD->isDynamicClass() && "Non-dynamic classes have no VTable.");
721
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)
726 return true;
727
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)
732 return false;
733
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);
737 if (!keyFunction)
738 return false;
739
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();
743 }
744
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?
shouldEmitVTableAtEndOfTranslationUnit(CodeGenModule & CGM,const CXXRecordDecl * RD)748 static bool shouldEmitVTableAtEndOfTranslationUnit(CodeGenModule &CGM,
749 const CXXRecordDecl *RD) {
750 return !CGM.getVTables().isVTableExternal(RD);
751 }
752
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.
EmitDeferredVTables()756 void CodeGenModule::EmitDeferredVTables() {
757 #ifndef NDEBUG
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();
761 #endif
762
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);
769 }
770
771 assert(savedSize == DeferredVTables.size() &&
772 "deferred extra v-tables during v-table emission?");
773 DeferredVTables.clear();
774 }
775