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 "CGCXXABI.h"
15 #include "CodeGenFunction.h"
16 #include "CodeGenModule.h"
17 #include "clang/CodeGen/ConstantInitBuilder.h"
18 #include "clang/AST/CXXInheritance.h"
19 #include "clang/AST/RecordLayout.h"
20 #include "clang/CodeGen/CGFunctionInfo.h"
21 #include "clang/Frontend/CodeGenOptions.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/Transforms/Utils/Cloning.h"
24 #include <algorithm>
25 #include <cstdio>
26 
27 using namespace clang;
28 using namespace CodeGen;
29 
30 CodeGenVTables::CodeGenVTables(CodeGenModule &CGM)
31     : CGM(CGM), VTContext(CGM.getContext().getVTableContext()) {}
32 
33 llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
34                                               const ThunkInfo &Thunk) {
35   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
36 
37   // Compute the mangled name.
38   SmallString<256> Name;
39   llvm::raw_svector_ostream Out(Name);
40   if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
41     getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(),
42                                                       Thunk.This, Out);
43   else
44     getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out);
45 
46   llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD);
47   return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true,
48                                  /*DontDefer=*/true, /*IsThunk=*/true);
49 }
50 
51 static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD,
52                                const ThunkInfo &Thunk, llvm::Function *Fn) {
53   CGM.setGlobalVisibility(Fn, MD);
54 }
55 
56 static void setThunkProperties(CodeGenModule &CGM, const ThunkInfo &Thunk,
57                                llvm::Function *ThunkFn, bool ForVTable,
58                                GlobalDecl GD) {
59   CGM.setFunctionLinkage(GD, ThunkFn);
60   CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable, GD,
61                                   !Thunk.Return.isEmpty());
62 
63   // Set the right visibility.
64   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
65   setThunkVisibility(CGM, MD, Thunk, ThunkFn);
66 
67   // Propagate dllexport storage.
68   if (MD->hasAttr<DLLExportAttr>())
69     ThunkFn->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
70 
71   if (CGM.supportsCOMDAT() && ThunkFn->isWeakForLinker())
72     ThunkFn->setComdat(CGM.getModule().getOrInsertComdat(ThunkFn->getName()));
73 }
74 
75 #ifndef NDEBUG
76 static bool similar(const ABIArgInfo &infoL, CanQualType typeL,
77                     const ABIArgInfo &infoR, CanQualType typeR) {
78   return (infoL.getKind() == infoR.getKind() &&
79           (typeL == typeR ||
80            (isa<PointerType>(typeL) && isa<PointerType>(typeR)) ||
81            (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR))));
82 }
83 #endif
84 
85 static RValue PerformReturnAdjustment(CodeGenFunction &CGF,
86                                       QualType ResultType, RValue RV,
87                                       const ThunkInfo &Thunk) {
88   // Emit the return adjustment.
89   bool NullCheckValue = !ResultType->isReferenceType();
90 
91   llvm::BasicBlock *AdjustNull = nullptr;
92   llvm::BasicBlock *AdjustNotNull = nullptr;
93   llvm::BasicBlock *AdjustEnd = nullptr;
94 
95   llvm::Value *ReturnValue = RV.getScalarVal();
96 
97   if (NullCheckValue) {
98     AdjustNull = CGF.createBasicBlock("adjust.null");
99     AdjustNotNull = CGF.createBasicBlock("adjust.notnull");
100     AdjustEnd = CGF.createBasicBlock("adjust.end");
101 
102     llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue);
103     CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
104     CGF.EmitBlock(AdjustNotNull);
105   }
106 
107   auto ClassDecl = ResultType->getPointeeType()->getAsCXXRecordDecl();
108   auto ClassAlign = CGF.CGM.getClassPointerAlignment(ClassDecl);
109   ReturnValue = CGF.CGM.getCXXABI().performReturnAdjustment(CGF,
110                                             Address(ReturnValue, ClassAlign),
111                                             Thunk.Return);
112 
113   if (NullCheckValue) {
114     CGF.Builder.CreateBr(AdjustEnd);
115     CGF.EmitBlock(AdjustNull);
116     CGF.Builder.CreateBr(AdjustEnd);
117     CGF.EmitBlock(AdjustEnd);
118 
119     llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2);
120     PHI->addIncoming(ReturnValue, AdjustNotNull);
121     PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
122                      AdjustNull);
123     ReturnValue = PHI;
124   }
125 
126   return RValue::get(ReturnValue);
127 }
128 
129 // This function does roughly the same thing as GenerateThunk, but in a
130 // very different way, so that va_start and va_end work correctly.
131 // FIXME: This function assumes "this" is the first non-sret LLVM argument of
132 //        a function, and that there is an alloca built in the entry block
133 //        for all accesses to "this".
134 // FIXME: This function assumes there is only one "ret" statement per function.
135 // FIXME: Cloning isn't correct in the presence of indirect goto!
136 // FIXME: This implementation of thunks bloats codesize by duplicating the
137 //        function definition.  There are alternatives:
138 //        1. Add some sort of stub support to LLVM for cases where we can
139 //           do a this adjustment, then a sibcall.
140 //        2. We could transform the definition to take a va_list instead of an
141 //           actual variable argument list, then have the thunks (including a
142 //           no-op thunk for the regular definition) call va_start/va_end.
143 //           There's a bit of per-call overhead for this solution, but it's
144 //           better for codesize if the definition is long.
145 llvm::Function *
146 CodeGenFunction::GenerateVarArgsThunk(llvm::Function *Fn,
147                                       const CGFunctionInfo &FnInfo,
148                                       GlobalDecl GD, const ThunkInfo &Thunk) {
149   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
150   const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
151   QualType ResultType = FPT->getReturnType();
152 
153   // Get the original function
154   assert(FnInfo.isVariadic());
155   llvm::Type *Ty = CGM.getTypes().GetFunctionType(FnInfo);
156   llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
157   llvm::Function *BaseFn = cast<llvm::Function>(Callee);
158 
159   // Clone to thunk.
160   llvm::ValueToValueMapTy VMap;
161   llvm::Function *NewFn = llvm::CloneFunction(BaseFn, VMap);
162   Fn->replaceAllUsesWith(NewFn);
163   NewFn->takeName(Fn);
164   Fn->eraseFromParent();
165   Fn = NewFn;
166 
167   // "Initialize" CGF (minimally).
168   CurFn = Fn;
169 
170   // Get the "this" value
171   llvm::Function::arg_iterator AI = Fn->arg_begin();
172   if (CGM.ReturnTypeUsesSRet(FnInfo))
173     ++AI;
174 
175   // Find the first store of "this", which will be to the alloca associated
176   // with "this".
177   Address ThisPtr(&*AI, CGM.getClassPointerAlignment(MD->getParent()));
178   llvm::BasicBlock *EntryBB = &Fn->front();
179   llvm::BasicBlock::iterator ThisStore =
180       std::find_if(EntryBB->begin(), EntryBB->end(), [&](llvm::Instruction &I) {
181         return isa<llvm::StoreInst>(I) &&
182                I.getOperand(0) == ThisPtr.getPointer();
183       });
184   assert(ThisStore != EntryBB->end() &&
185          "Store of this should be in entry block?");
186   // Adjust "this", if necessary.
187   Builder.SetInsertPoint(&*ThisStore);
188   llvm::Value *AdjustedThisPtr =
189       CGM.getCXXABI().performThisAdjustment(*this, ThisPtr, Thunk.This);
190   ThisStore->setOperand(0, AdjustedThisPtr);
191 
192   if (!Thunk.Return.isEmpty()) {
193     // Fix up the returned value, if necessary.
194     for (llvm::BasicBlock &BB : *Fn) {
195       llvm::Instruction *T = BB.getTerminator();
196       if (isa<llvm::ReturnInst>(T)) {
197         RValue RV = RValue::get(T->getOperand(0));
198         T->eraseFromParent();
199         Builder.SetInsertPoint(&BB);
200         RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
201         Builder.CreateRet(RV.getScalarVal());
202         break;
203       }
204     }
205   }
206 
207   return Fn;
208 }
209 
210 void CodeGenFunction::StartThunk(llvm::Function *Fn, GlobalDecl GD,
211                                  const CGFunctionInfo &FnInfo) {
212   assert(!CurGD.getDecl() && "CurGD was already set!");
213   CurGD = GD;
214   CurFuncIsThunk = true;
215 
216   // Build FunctionArgs.
217   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
218   QualType ThisType = MD->getThisType(getContext());
219   const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
220   QualType ResultType = CGM.getCXXABI().HasThisReturn(GD)
221                             ? ThisType
222                             : CGM.getCXXABI().hasMostDerivedReturn(GD)
223                                   ? CGM.getContext().VoidPtrTy
224                                   : FPT->getReturnType();
225   FunctionArgList FunctionArgs;
226 
227   // Create the implicit 'this' parameter declaration.
228   CGM.getCXXABI().buildThisParam(*this, FunctionArgs);
229 
230   // Add the rest of the parameters.
231   FunctionArgs.append(MD->param_begin(), MD->param_end());
232 
233   if (isa<CXXDestructorDecl>(MD))
234     CGM.getCXXABI().addImplicitStructorParams(*this, ResultType, FunctionArgs);
235 
236   // Start defining the function.
237   auto NL = ApplyDebugLocation::CreateEmpty(*this);
238   StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs,
239                 MD->getLocation());
240   // Create a scope with an artificial location for the body of this function.
241   auto AL = ApplyDebugLocation::CreateArtificial(*this);
242 
243   // Since we didn't pass a GlobalDecl to StartFunction, do this ourselves.
244   CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
245   CXXThisValue = CXXABIThisValue;
246   CurCodeDecl = MD;
247   CurFuncDecl = MD;
248 }
249 
250 void CodeGenFunction::FinishThunk() {
251   // Clear these to restore the invariants expected by
252   // StartFunction/FinishFunction.
253   CurCodeDecl = nullptr;
254   CurFuncDecl = nullptr;
255 
256   FinishFunction();
257 }
258 
259 void CodeGenFunction::EmitCallAndReturnForThunk(llvm::Constant *CalleePtr,
260                                                 const ThunkInfo *Thunk) {
261   assert(isa<CXXMethodDecl>(CurGD.getDecl()) &&
262          "Please use a new CGF for this thunk");
263   const CXXMethodDecl *MD = cast<CXXMethodDecl>(CurGD.getDecl());
264 
265   // Adjust the 'this' pointer if necessary
266   llvm::Value *AdjustedThisPtr =
267     Thunk ? CGM.getCXXABI().performThisAdjustment(
268                           *this, LoadCXXThisAddress(), Thunk->This)
269           : LoadCXXThis();
270 
271   if (CurFnInfo->usesInAlloca()) {
272     // We don't handle return adjusting thunks, because they require us to call
273     // the copy constructor.  For now, fall through and pretend the return
274     // adjustment was empty so we don't crash.
275     if (Thunk && !Thunk->Return.isEmpty()) {
276       CGM.ErrorUnsupported(
277           MD, "non-trivial argument copy for return-adjusting thunk");
278     }
279     EmitMustTailThunk(MD, AdjustedThisPtr, CalleePtr);
280     return;
281   }
282 
283   // Start building CallArgs.
284   CallArgList CallArgs;
285   QualType ThisType = MD->getThisType(getContext());
286   CallArgs.add(RValue::get(AdjustedThisPtr), ThisType);
287 
288   if (isa<CXXDestructorDecl>(MD))
289     CGM.getCXXABI().adjustCallArgsForDestructorThunk(*this, CurGD, CallArgs);
290 
291 #ifndef NDEBUG
292   unsigned PrefixArgs = CallArgs.size() - 1;
293 #endif
294   // Add the rest of the arguments.
295   for (const ParmVarDecl *PD : MD->parameters())
296     EmitDelegateCallArg(CallArgs, PD, SourceLocation());
297 
298   const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
299 
300 #ifndef NDEBUG
301   const CGFunctionInfo &CallFnInfo = CGM.getTypes().arrangeCXXMethodCall(
302       CallArgs, FPT, RequiredArgs::forPrototypePlus(FPT, 1, MD), PrefixArgs);
303   assert(CallFnInfo.getRegParm() == CurFnInfo->getRegParm() &&
304          CallFnInfo.isNoReturn() == CurFnInfo->isNoReturn() &&
305          CallFnInfo.getCallingConvention() == CurFnInfo->getCallingConvention());
306   assert(isa<CXXDestructorDecl>(MD) || // ignore dtor return types
307          similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(),
308                  CurFnInfo->getReturnInfo(), CurFnInfo->getReturnType()));
309   assert(CallFnInfo.arg_size() == CurFnInfo->arg_size());
310   for (unsigned i = 0, e = CurFnInfo->arg_size(); i != e; ++i)
311     assert(similar(CallFnInfo.arg_begin()[i].info,
312                    CallFnInfo.arg_begin()[i].type,
313                    CurFnInfo->arg_begin()[i].info,
314                    CurFnInfo->arg_begin()[i].type));
315 #endif
316 
317   // Determine whether we have a return value slot to use.
318   QualType ResultType = CGM.getCXXABI().HasThisReturn(CurGD)
319                             ? ThisType
320                             : CGM.getCXXABI().hasMostDerivedReturn(CurGD)
321                                   ? CGM.getContext().VoidPtrTy
322                                   : FPT->getReturnType();
323   ReturnValueSlot Slot;
324   if (!ResultType->isVoidType() &&
325       CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
326       !hasScalarEvaluationKind(CurFnInfo->getReturnType()))
327     Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified());
328 
329   // Now emit our call.
330   llvm::Instruction *CallOrInvoke;
331   CGCallee Callee = CGCallee::forDirect(CalleePtr, MD);
332   RValue RV = EmitCall(*CurFnInfo, Callee, Slot, CallArgs, &CallOrInvoke);
333 
334   // Consider return adjustment if we have ThunkInfo.
335   if (Thunk && !Thunk->Return.isEmpty())
336     RV = PerformReturnAdjustment(*this, ResultType, RV, *Thunk);
337   else if (llvm::CallInst* Call = dyn_cast<llvm::CallInst>(CallOrInvoke))
338     Call->setTailCallKind(llvm::CallInst::TCK_Tail);
339 
340   // Emit return.
341   if (!ResultType->isVoidType() && Slot.isNull())
342     CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType);
343 
344   // Disable the final ARC autorelease.
345   AutoreleaseResult = false;
346 
347   FinishThunk();
348 }
349 
350 void CodeGenFunction::EmitMustTailThunk(const CXXMethodDecl *MD,
351                                         llvm::Value *AdjustedThisPtr,
352                                         llvm::Value *CalleePtr) {
353   // Emitting a musttail call thunk doesn't use any of the CGCall.cpp machinery
354   // to translate AST arguments into LLVM IR arguments.  For thunks, we know
355   // that the caller prototype more or less matches the callee prototype with
356   // the exception of 'this'.
357   SmallVector<llvm::Value *, 8> Args;
358   for (llvm::Argument &A : CurFn->args())
359     Args.push_back(&A);
360 
361   // Set the adjusted 'this' pointer.
362   const ABIArgInfo &ThisAI = CurFnInfo->arg_begin()->info;
363   if (ThisAI.isDirect()) {
364     const ABIArgInfo &RetAI = CurFnInfo->getReturnInfo();
365     int ThisArgNo = RetAI.isIndirect() && !RetAI.isSRetAfterThis() ? 1 : 0;
366     llvm::Type *ThisType = Args[ThisArgNo]->getType();
367     if (ThisType != AdjustedThisPtr->getType())
368       AdjustedThisPtr = Builder.CreateBitCast(AdjustedThisPtr, ThisType);
369     Args[ThisArgNo] = AdjustedThisPtr;
370   } else {
371     assert(ThisAI.isInAlloca() && "this is passed directly or inalloca");
372     Address ThisAddr = GetAddrOfLocalVar(CXXABIThisDecl);
373     llvm::Type *ThisType = ThisAddr.getElementType();
374     if (ThisType != AdjustedThisPtr->getType())
375       AdjustedThisPtr = Builder.CreateBitCast(AdjustedThisPtr, ThisType);
376     Builder.CreateStore(AdjustedThisPtr, ThisAddr);
377   }
378 
379   // Emit the musttail call manually.  Even if the prologue pushed cleanups, we
380   // don't actually want to run them.
381   llvm::CallInst *Call = Builder.CreateCall(CalleePtr, Args);
382   Call->setTailCallKind(llvm::CallInst::TCK_MustTail);
383 
384   // Apply the standard set of call attributes.
385   unsigned CallingConv;
386   llvm::AttributeList Attrs;
387   CGM.ConstructAttributeList(CalleePtr->getName(), *CurFnInfo, MD, Attrs,
388                              CallingConv, /*AttrOnCallSite=*/true);
389   Call->setAttributes(Attrs);
390   Call->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
391 
392   if (Call->getType()->isVoidTy())
393     Builder.CreateRetVoid();
394   else
395     Builder.CreateRet(Call);
396 
397   // Finish the function to maintain CodeGenFunction invariants.
398   // FIXME: Don't emit unreachable code.
399   EmitBlock(createBasicBlock());
400   FinishFunction();
401 }
402 
403 void CodeGenFunction::generateThunk(llvm::Function *Fn,
404                                     const CGFunctionInfo &FnInfo,
405                                     GlobalDecl GD, const ThunkInfo &Thunk) {
406   StartThunk(Fn, GD, FnInfo);
407   // Create a scope with an artificial location for the body of this function.
408   auto AL = ApplyDebugLocation::CreateArtificial(*this);
409 
410   // Get our callee.
411   llvm::Type *Ty =
412     CGM.getTypes().GetFunctionType(CGM.getTypes().arrangeGlobalDeclaration(GD));
413   llvm::Constant *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
414 
415   // Make the call and return the result.
416   EmitCallAndReturnForThunk(Callee, &Thunk);
417 }
418 
419 void CodeGenVTables::emitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
420                                bool ForVTable) {
421   const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeGlobalDeclaration(GD);
422 
423   // FIXME: re-use FnInfo in this computation.
424   llvm::Constant *C = CGM.GetAddrOfThunk(GD, Thunk);
425   llvm::GlobalValue *Entry;
426 
427   // Strip off a bitcast if we got one back.
428   if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(C)) {
429     assert(CE->getOpcode() == llvm::Instruction::BitCast);
430     Entry = cast<llvm::GlobalValue>(CE->getOperand(0));
431   } else {
432     Entry = cast<llvm::GlobalValue>(C);
433   }
434 
435   // There's already a declaration with the same name, check if it has the same
436   // type or if we need to replace it.
437   if (Entry->getType()->getElementType() !=
438       CGM.getTypes().GetFunctionTypeForVTable(GD)) {
439     llvm::GlobalValue *OldThunkFn = Entry;
440 
441     // If the types mismatch then we have to rewrite the definition.
442     assert(OldThunkFn->isDeclaration() &&
443            "Shouldn't replace non-declaration");
444 
445     // Remove the name from the old thunk function and get a new thunk.
446     OldThunkFn->setName(StringRef());
447     Entry = cast<llvm::GlobalValue>(CGM.GetAddrOfThunk(GD, Thunk));
448 
449     // If needed, replace the old thunk with a bitcast.
450     if (!OldThunkFn->use_empty()) {
451       llvm::Constant *NewPtrForOldDecl =
452         llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType());
453       OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
454     }
455 
456     // Remove the old thunk.
457     OldThunkFn->eraseFromParent();
458   }
459 
460   llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
461   bool ABIHasKeyFunctions = CGM.getTarget().getCXXABI().hasKeyFunctions();
462   bool UseAvailableExternallyLinkage = ForVTable && ABIHasKeyFunctions;
463 
464   if (!ThunkFn->isDeclaration()) {
465     if (!ABIHasKeyFunctions || UseAvailableExternallyLinkage) {
466       // There is already a thunk emitted for this function, do nothing.
467       return;
468     }
469 
470     setThunkProperties(CGM, Thunk, ThunkFn, ForVTable, GD);
471     return;
472   }
473 
474   CGM.SetLLVMFunctionAttributesForDefinition(GD.getDecl(), ThunkFn);
475 
476   if (ThunkFn->isVarArg()) {
477     // Varargs thunks are special; we can't just generate a call because
478     // we can't copy the varargs.  Our implementation is rather
479     // expensive/sucky at the moment, so don't generate the thunk unless
480     // we have to.
481     // FIXME: Do something better here; GenerateVarArgsThunk is extremely ugly.
482     if (UseAvailableExternallyLinkage)
483       return;
484     ThunkFn =
485         CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk);
486   } else {
487     // Normal thunk body generation.
488     CodeGenFunction(CGM).generateThunk(ThunkFn, FnInfo, GD, Thunk);
489   }
490 
491   setThunkProperties(CGM, Thunk, ThunkFn, ForVTable, GD);
492 }
493 
494 void CodeGenVTables::maybeEmitThunkForVTable(GlobalDecl GD,
495                                              const ThunkInfo &Thunk) {
496   // If the ABI has key functions, only the TU with the key function should emit
497   // the thunk. However, we can allow inlining of thunks if we emit them with
498   // available_externally linkage together with vtables when optimizations are
499   // enabled.
500   if (CGM.getTarget().getCXXABI().hasKeyFunctions() &&
501       !CGM.getCodeGenOpts().OptimizationLevel)
502     return;
503 
504   // We can't emit thunks for member functions with incomplete types.
505   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
506   if (!CGM.getTypes().isFuncTypeConvertible(
507            MD->getType()->castAs<FunctionType>()))
508     return;
509 
510   emitThunk(GD, Thunk, /*ForVTable=*/true);
511 }
512 
513 void CodeGenVTables::EmitThunks(GlobalDecl GD)
514 {
515   const CXXMethodDecl *MD =
516     cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
517 
518   // We don't need to generate thunks for the base destructor.
519   if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
520     return;
521 
522   const VTableContextBase::ThunkInfoVectorTy *ThunkInfoVector =
523       VTContext->getThunkInfo(GD);
524 
525   if (!ThunkInfoVector)
526     return;
527 
528   for (const ThunkInfo& Thunk : *ThunkInfoVector)
529     emitThunk(GD, Thunk, /*ForVTable=*/false);
530 }
531 
532 void CodeGenVTables::addVTableComponent(
533     ConstantArrayBuilder &builder, const VTableLayout &layout,
534     unsigned idx, llvm::Constant *rtti, unsigned &nextVTableThunkIndex) {
535   auto &component = layout.vtable_components()[idx];
536 
537   auto addOffsetConstant = [&](CharUnits offset) {
538     builder.add(llvm::ConstantExpr::getIntToPtr(
539         llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity()),
540         CGM.Int8PtrTy));
541   };
542 
543   switch (component.getKind()) {
544   case VTableComponent::CK_VCallOffset:
545     return addOffsetConstant(component.getVCallOffset());
546 
547   case VTableComponent::CK_VBaseOffset:
548     return addOffsetConstant(component.getVBaseOffset());
549 
550   case VTableComponent::CK_OffsetToTop:
551     return addOffsetConstant(component.getOffsetToTop());
552 
553   case VTableComponent::CK_RTTI:
554     return builder.add(llvm::ConstantExpr::getBitCast(rtti, CGM.Int8PtrTy));
555 
556   case VTableComponent::CK_FunctionPointer:
557   case VTableComponent::CK_CompleteDtorPointer:
558   case VTableComponent::CK_DeletingDtorPointer: {
559     GlobalDecl GD;
560 
561     // Get the right global decl.
562     switch (component.getKind()) {
563     default:
564       llvm_unreachable("Unexpected vtable component kind");
565     case VTableComponent::CK_FunctionPointer:
566       GD = component.getFunctionDecl();
567       break;
568     case VTableComponent::CK_CompleteDtorPointer:
569       GD = GlobalDecl(component.getDestructorDecl(), Dtor_Complete);
570       break;
571     case VTableComponent::CK_DeletingDtorPointer:
572       GD = GlobalDecl(component.getDestructorDecl(), Dtor_Deleting);
573       break;
574     }
575 
576     if (CGM.getLangOpts().CUDA) {
577       // Emit NULL for methods we can't codegen on this
578       // side. Otherwise we'd end up with vtable with unresolved
579       // references.
580       const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
581       // OK on device side: functions w/ __device__ attribute
582       // OK on host side: anything except __device__-only functions.
583       bool CanEmitMethod =
584           CGM.getLangOpts().CUDAIsDevice
585               ? MD->hasAttr<CUDADeviceAttr>()
586               : (MD->hasAttr<CUDAHostAttr>() || !MD->hasAttr<CUDADeviceAttr>());
587       if (!CanEmitMethod)
588         return builder.addNullPointer(CGM.Int8PtrTy);
589       // Method is acceptable, continue processing as usual.
590     }
591 
592     auto getSpecialVirtualFn = [&](StringRef name) {
593       llvm::FunctionType *fnTy =
594           llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
595       llvm::Constant *fn = CGM.CreateRuntimeFunction(fnTy, name);
596       if (auto f = dyn_cast<llvm::Function>(fn))
597         f->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
598       return llvm::ConstantExpr::getBitCast(fn, CGM.Int8PtrTy);
599     };
600 
601     llvm::Constant *fnPtr;
602 
603     // Pure virtual member functions.
604     if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
605       if (!PureVirtualFn)
606         PureVirtualFn =
607           getSpecialVirtualFn(CGM.getCXXABI().GetPureVirtualCallName());
608       fnPtr = PureVirtualFn;
609 
610     // Deleted virtual member functions.
611     } else if (cast<CXXMethodDecl>(GD.getDecl())->isDeleted()) {
612       if (!DeletedVirtualFn)
613         DeletedVirtualFn =
614           getSpecialVirtualFn(CGM.getCXXABI().GetDeletedVirtualCallName());
615       fnPtr = DeletedVirtualFn;
616 
617     // Thunks.
618     } else if (nextVTableThunkIndex < layout.vtable_thunks().size() &&
619                layout.vtable_thunks()[nextVTableThunkIndex].first == idx) {
620       auto &thunkInfo = layout.vtable_thunks()[nextVTableThunkIndex].second;
621 
622       maybeEmitThunkForVTable(GD, thunkInfo);
623       nextVTableThunkIndex++;
624       fnPtr = CGM.GetAddrOfThunk(GD, thunkInfo);
625 
626     // Otherwise we can use the method definition directly.
627     } else {
628       llvm::Type *fnTy = CGM.getTypes().GetFunctionTypeForVTable(GD);
629       fnPtr = CGM.GetAddrOfFunction(GD, fnTy, /*ForVTable=*/true);
630     }
631 
632     fnPtr = llvm::ConstantExpr::getBitCast(fnPtr, CGM.Int8PtrTy);
633     builder.add(fnPtr);
634     return;
635   }
636 
637   case VTableComponent::CK_UnusedFunctionPointer:
638     return builder.addNullPointer(CGM.Int8PtrTy);
639   }
640 
641   llvm_unreachable("Unexpected vtable component kind");
642 }
643 
644 llvm::Type *CodeGenVTables::getVTableType(const VTableLayout &layout) {
645   SmallVector<llvm::Type *, 4> tys;
646   for (unsigned i = 0, e = layout.getNumVTables(); i != e; ++i) {
647     tys.push_back(llvm::ArrayType::get(CGM.Int8PtrTy, layout.getVTableSize(i)));
648   }
649 
650   return llvm::StructType::get(CGM.getLLVMContext(), tys);
651 }
652 
653 void CodeGenVTables::createVTableInitializer(ConstantStructBuilder &builder,
654                                              const VTableLayout &layout,
655                                              llvm::Constant *rtti) {
656   unsigned nextVTableThunkIndex = 0;
657   for (unsigned i = 0, e = layout.getNumVTables(); i != e; ++i) {
658     auto vtableElem = builder.beginArray(CGM.Int8PtrTy);
659     size_t thisIndex = layout.getVTableOffset(i);
660     size_t nextIndex = thisIndex + layout.getVTableSize(i);
661     for (unsigned i = thisIndex; i != nextIndex; ++i) {
662       addVTableComponent(vtableElem, layout, i, rtti, nextVTableThunkIndex);
663     }
664     vtableElem.finishAndAddTo(builder);
665   }
666 }
667 
668 llvm::GlobalVariable *
669 CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD,
670                                       const BaseSubobject &Base,
671                                       bool BaseIsVirtual,
672                                    llvm::GlobalVariable::LinkageTypes Linkage,
673                                       VTableAddressPointsMapTy& AddressPoints) {
674   if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
675     DI->completeClassData(Base.getBase());
676 
677   std::unique_ptr<VTableLayout> VTLayout(
678       getItaniumVTableContext().createConstructionVTableLayout(
679           Base.getBase(), Base.getBaseOffset(), BaseIsVirtual, RD));
680 
681   // Add the address points.
682   AddressPoints = VTLayout->getAddressPoints();
683 
684   // Get the mangled construction vtable name.
685   SmallString<256> OutName;
686   llvm::raw_svector_ostream Out(OutName);
687   cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
688       .mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(),
689                            Base.getBase(), Out);
690   StringRef Name = OutName.str();
691 
692   llvm::Type *VTType = getVTableType(*VTLayout);
693 
694   // Construction vtable symbols are not part of the Itanium ABI, so we cannot
695   // guarantee that they actually will be available externally. Instead, when
696   // emitting an available_externally VTT, we provide references to an internal
697   // linkage construction vtable. The ABI only requires complete-object vtables
698   // to be the same for all instances of a type, not construction vtables.
699   if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
700     Linkage = llvm::GlobalVariable::InternalLinkage;
701 
702   // Create the variable that will hold the construction vtable.
703   llvm::GlobalVariable *VTable =
704     CGM.CreateOrReplaceCXXRuntimeVariable(Name, VTType, Linkage);
705   CGM.setGlobalVisibility(VTable, RD);
706 
707   // V-tables are always unnamed_addr.
708   VTable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
709 
710   llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(
711       CGM.getContext().getTagDeclType(Base.getBase()));
712 
713   // Create and set the initializer.
714   ConstantInitBuilder builder(CGM);
715   auto components = builder.beginStruct();
716   createVTableInitializer(components, *VTLayout, RTTI);
717   components.finishAndSetAsInitializer(VTable);
718 
719   CGM.EmitVTableTypeMetadata(VTable, *VTLayout.get());
720 
721   return VTable;
722 }
723 
724 static bool shouldEmitAvailableExternallyVTable(const CodeGenModule &CGM,
725                                                 const CXXRecordDecl *RD) {
726   return CGM.getCodeGenOpts().OptimizationLevel > 0 &&
727          CGM.getCXXABI().canSpeculativelyEmitVTable(RD);
728 }
729 
730 /// Compute the required linkage of the vtable for the given class.
731 ///
732 /// Note that we only call this at the end of the translation unit.
733 llvm::GlobalVariable::LinkageTypes
734 CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
735   if (!RD->isExternallyVisible())
736     return llvm::GlobalVariable::InternalLinkage;
737 
738   // We're at the end of the translation unit, so the current key
739   // function is fully correct.
740   const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction(RD);
741   if (keyFunction && !RD->hasAttr<DLLImportAttr>()) {
742     // If this class has a key function, use that to determine the
743     // linkage of the vtable.
744     const FunctionDecl *def = nullptr;
745     if (keyFunction->hasBody(def))
746       keyFunction = cast<CXXMethodDecl>(def);
747 
748     switch (keyFunction->getTemplateSpecializationKind()) {
749       case TSK_Undeclared:
750       case TSK_ExplicitSpecialization:
751         assert((def || CodeGenOpts.OptimizationLevel > 0 ||
752                 CodeGenOpts.getDebugInfo() != codegenoptions::NoDebugInfo) &&
753                "Shouldn't query vtable linkage without key function, "
754                "optimizations, or debug info");
755         if (!def && CodeGenOpts.OptimizationLevel > 0)
756           return llvm::GlobalVariable::AvailableExternallyLinkage;
757 
758         if (keyFunction->isInlined())
759           return !Context.getLangOpts().AppleKext ?
760                    llvm::GlobalVariable::LinkOnceODRLinkage :
761                    llvm::Function::InternalLinkage;
762 
763         return llvm::GlobalVariable::ExternalLinkage;
764 
765       case TSK_ImplicitInstantiation:
766         return !Context.getLangOpts().AppleKext ?
767                  llvm::GlobalVariable::LinkOnceODRLinkage :
768                  llvm::Function::InternalLinkage;
769 
770       case TSK_ExplicitInstantiationDefinition:
771         return !Context.getLangOpts().AppleKext ?
772                  llvm::GlobalVariable::WeakODRLinkage :
773                  llvm::Function::InternalLinkage;
774 
775       case TSK_ExplicitInstantiationDeclaration:
776         llvm_unreachable("Should not have been asked to emit this");
777     }
778   }
779 
780   // -fapple-kext mode does not support weak linkage, so we must use
781   // internal linkage.
782   if (Context.getLangOpts().AppleKext)
783     return llvm::Function::InternalLinkage;
784 
785   llvm::GlobalVariable::LinkageTypes DiscardableODRLinkage =
786       llvm::GlobalValue::LinkOnceODRLinkage;
787   llvm::GlobalVariable::LinkageTypes NonDiscardableODRLinkage =
788       llvm::GlobalValue::WeakODRLinkage;
789   if (RD->hasAttr<DLLExportAttr>()) {
790     // Cannot discard exported vtables.
791     DiscardableODRLinkage = NonDiscardableODRLinkage;
792   } else if (RD->hasAttr<DLLImportAttr>()) {
793     // Imported vtables are available externally.
794     DiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage;
795     NonDiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage;
796   }
797 
798   switch (RD->getTemplateSpecializationKind()) {
799     case TSK_Undeclared:
800     case TSK_ExplicitSpecialization:
801     case TSK_ImplicitInstantiation:
802       return DiscardableODRLinkage;
803 
804     case TSK_ExplicitInstantiationDeclaration:
805       // Explicit instantiations in MSVC do not provide vtables, so we must emit
806       // our own.
807       if (getTarget().getCXXABI().isMicrosoft())
808         return DiscardableODRLinkage;
809       return shouldEmitAvailableExternallyVTable(*this, RD)
810                  ? llvm::GlobalVariable::AvailableExternallyLinkage
811                  : llvm::GlobalVariable::ExternalLinkage;
812 
813     case TSK_ExplicitInstantiationDefinition:
814       return NonDiscardableODRLinkage;
815   }
816 
817   llvm_unreachable("Invalid TemplateSpecializationKind!");
818 }
819 
820 /// This is a callback from Sema to tell us that that a particular vtable is
821 /// required to be emitted in this translation unit.
822 ///
823 /// This is only called for vtables that _must_ be emitted (mainly due to key
824 /// functions).  For weak vtables, CodeGen tracks when they are needed and
825 /// emits them as-needed.
826 void CodeGenModule::EmitVTable(CXXRecordDecl *theClass) {
827   VTables.GenerateClassData(theClass);
828 }
829 
830 void
831 CodeGenVTables::GenerateClassData(const CXXRecordDecl *RD) {
832   if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
833     DI->completeClassData(RD);
834 
835   if (RD->getNumVBases())
836     CGM.getCXXABI().emitVirtualInheritanceTables(RD);
837 
838   CGM.getCXXABI().emitVTableDefinitions(*this, RD);
839 }
840 
841 /// At this point in the translation unit, does it appear that can we
842 /// rely on the vtable being defined elsewhere in the program?
843 ///
844 /// The response is really only definitive when called at the end of
845 /// the translation unit.
846 ///
847 /// The only semantic restriction here is that the object file should
848 /// not contain a vtable definition when that vtable is defined
849 /// strongly elsewhere.  Otherwise, we'd just like to avoid emitting
850 /// vtables when unnecessary.
851 bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
852   assert(RD->isDynamicClass() && "Non-dynamic classes have no VTable.");
853 
854   // We always synthesize vtables if they are needed in the MS ABI. MSVC doesn't
855   // emit them even if there is an explicit template instantiation.
856   if (CGM.getTarget().getCXXABI().isMicrosoft())
857     return false;
858 
859   // If we have an explicit instantiation declaration (and not a
860   // definition), the vtable is defined elsewhere.
861   TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
862   if (TSK == TSK_ExplicitInstantiationDeclaration)
863     return true;
864 
865   // Otherwise, if the class is an instantiated template, the
866   // vtable must be defined here.
867   if (TSK == TSK_ImplicitInstantiation ||
868       TSK == TSK_ExplicitInstantiationDefinition)
869     return false;
870 
871   // Otherwise, if the class doesn't have a key function (possibly
872   // anymore), the vtable must be defined here.
873   const CXXMethodDecl *keyFunction = CGM.getContext().getCurrentKeyFunction(RD);
874   if (!keyFunction)
875     return false;
876 
877   // Otherwise, if we don't have a definition of the key function, the
878   // vtable must be defined somewhere else.
879   return !keyFunction->hasBody();
880 }
881 
882 /// Given that we're currently at the end of the translation unit, and
883 /// we've emitted a reference to the vtable for this class, should
884 /// we define that vtable?
885 static bool shouldEmitVTableAtEndOfTranslationUnit(CodeGenModule &CGM,
886                                                    const CXXRecordDecl *RD) {
887   // If vtable is internal then it has to be done.
888   if (!CGM.getVTables().isVTableExternal(RD))
889     return true;
890 
891   // If it's external then maybe we will need it as available_externally.
892   return shouldEmitAvailableExternallyVTable(CGM, RD);
893 }
894 
895 /// Given that at some point we emitted a reference to one or more
896 /// vtables, and that we are now at the end of the translation unit,
897 /// decide whether we should emit them.
898 void CodeGenModule::EmitDeferredVTables() {
899 #ifndef NDEBUG
900   // Remember the size of DeferredVTables, because we're going to assume
901   // that this entire operation doesn't modify it.
902   size_t savedSize = DeferredVTables.size();
903 #endif
904 
905   for (const CXXRecordDecl *RD : DeferredVTables)
906     if (shouldEmitVTableAtEndOfTranslationUnit(*this, RD))
907       VTables.GenerateClassData(RD);
908     else if (shouldOpportunisticallyEmitVTables())
909       OpportunisticVTables.push_back(RD);
910 
911   assert(savedSize == DeferredVTables.size() &&
912          "deferred extra vtables during vtable emission?");
913   DeferredVTables.clear();
914 }
915 
916 bool CodeGenModule::HasHiddenLTOVisibility(const CXXRecordDecl *RD) {
917   LinkageInfo LV = RD->getLinkageAndVisibility();
918   if (!isExternallyVisible(LV.getLinkage()))
919     return true;
920 
921   if (RD->hasAttr<LTOVisibilityPublicAttr>() || RD->hasAttr<UuidAttr>())
922     return false;
923 
924   if (getTriple().isOSBinFormatCOFF()) {
925     if (RD->hasAttr<DLLExportAttr>() || RD->hasAttr<DLLImportAttr>())
926       return false;
927   } else {
928     if (LV.getVisibility() != HiddenVisibility)
929       return false;
930   }
931 
932   if (getCodeGenOpts().LTOVisibilityPublicStd) {
933     const DeclContext *DC = RD;
934     while (1) {
935       auto *D = cast<Decl>(DC);
936       DC = DC->getParent();
937       if (isa<TranslationUnitDecl>(DC->getRedeclContext())) {
938         if (auto *ND = dyn_cast<NamespaceDecl>(D))
939           if (const IdentifierInfo *II = ND->getIdentifier())
940             if (II->isStr("std") || II->isStr("stdext"))
941               return false;
942         break;
943       }
944     }
945   }
946 
947   return true;
948 }
949 
950 void CodeGenModule::EmitVTableTypeMetadata(llvm::GlobalVariable *VTable,
951                                            const VTableLayout &VTLayout) {
952   if (!getCodeGenOpts().LTOUnit)
953     return;
954 
955   CharUnits PointerWidth =
956       Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
957 
958   typedef std::pair<const CXXRecordDecl *, unsigned> BSEntry;
959   std::vector<BSEntry> BitsetEntries;
960   // Create a bit set entry for each address point.
961   for (auto &&AP : VTLayout.getAddressPoints())
962     BitsetEntries.push_back(
963         std::make_pair(AP.first.getBase(),
964                        VTLayout.getVTableOffset(AP.second.VTableIndex) +
965                            AP.second.AddressPointIndex));
966 
967   // Sort the bit set entries for determinism.
968   std::sort(BitsetEntries.begin(), BitsetEntries.end(),
969             [this](const BSEntry &E1, const BSEntry &E2) {
970     if (&E1 == &E2)
971       return false;
972 
973     std::string S1;
974     llvm::raw_string_ostream O1(S1);
975     getCXXABI().getMangleContext().mangleTypeName(
976         QualType(E1.first->getTypeForDecl(), 0), O1);
977     O1.flush();
978 
979     std::string S2;
980     llvm::raw_string_ostream O2(S2);
981     getCXXABI().getMangleContext().mangleTypeName(
982         QualType(E2.first->getTypeForDecl(), 0), O2);
983     O2.flush();
984 
985     if (S1 < S2)
986       return true;
987     if (S1 != S2)
988       return false;
989 
990     return E1.second < E2.second;
991   });
992 
993   for (auto BitsetEntry : BitsetEntries)
994     AddVTableTypeMetadata(VTable, PointerWidth * BitsetEntry.second,
995                           BitsetEntry.first);
996 }
997