1 //===--- CGVTables.cpp - Emit LLVM Code for C++ vtables -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This contains code dealing with C++ code generation of virtual tables.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CGCXXABI.h"
14 #include "CodeGenFunction.h"
15 #include "CodeGenModule.h"
16 #include "clang/AST/Attr.h"
17 #include "clang/AST/CXXInheritance.h"
18 #include "clang/AST/RecordLayout.h"
19 #include "clang/Basic/CodeGenOptions.h"
20 #include "clang/CodeGen/CGFunctionInfo.h"
21 #include "clang/CodeGen/ConstantInitBuilder.h"
22 #include "llvm/IR/IntrinsicInst.h"
23 #include "llvm/Support/Format.h"
24 #include "llvm/Transforms/Utils/Cloning.h"
25 #include <algorithm>
26 #include <cstdio>
27 
28 using namespace clang;
29 using namespace CodeGen;
30 
31 CodeGenVTables::CodeGenVTables(CodeGenModule &CGM)
32     : CGM(CGM), VTContext(CGM.getContext().getVTableContext()) {}
33 
34 llvm::Constant *CodeGenModule::GetAddrOfThunk(StringRef Name, llvm::Type *FnTy,
35                                               GlobalDecl GD) {
36   return GetOrCreateLLVMFunction(Name, FnTy, GD, /*ForVTable=*/true,
37                                  /*DontDefer=*/true, /*IsThunk=*/true);
38 }
39 
40 static void setThunkProperties(CodeGenModule &CGM, const ThunkInfo &Thunk,
41                                llvm::Function *ThunkFn, bool ForVTable,
42                                GlobalDecl GD) {
43   CGM.setFunctionLinkage(GD, ThunkFn);
44   CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable, GD,
45                                   !Thunk.Return.isEmpty());
46 
47   // Set the right visibility.
48   CGM.setGVProperties(ThunkFn, GD);
49 
50   if (!CGM.getCXXABI().exportThunk()) {
51     ThunkFn->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
52     ThunkFn->setDSOLocal(true);
53   }
54 
55   if (CGM.supportsCOMDAT() && ThunkFn->isWeakForLinker())
56     ThunkFn->setComdat(CGM.getModule().getOrInsertComdat(ThunkFn->getName()));
57 }
58 
59 #ifndef NDEBUG
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 
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   auto ClassDecl = ResultType->getPointeeType()->getAsCXXRecordDecl();
92   auto ClassAlign = CGF.CGM.getClassPointerAlignment(ClassDecl);
93   ReturnValue = CGF.CGM.getCXXABI().performReturnAdjustment(CGF,
94                                             Address(ReturnValue, ClassAlign),
95                                             Thunk.Return);
96 
97   if (NullCheckValue) {
98     CGF.Builder.CreateBr(AdjustEnd);
99     CGF.EmitBlock(AdjustNull);
100     CGF.Builder.CreateBr(AdjustEnd);
101     CGF.EmitBlock(AdjustEnd);
102 
103     llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2);
104     PHI->addIncoming(ReturnValue, AdjustNotNull);
105     PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
106                      AdjustNull);
107     ReturnValue = PHI;
108   }
109 
110   return RValue::get(ReturnValue);
111 }
112 
113 /// This function clones a function's DISubprogram node and enters it into
114 /// a value map with the intent that the map can be utilized by the cloner
115 /// to short-circuit Metadata node mapping.
116 /// Furthermore, the function resolves any DILocalVariable nodes referenced
117 /// by dbg.value intrinsics so they can be properly mapped during cloning.
118 static void resolveTopLevelMetadata(llvm::Function *Fn,
119                                     llvm::ValueToValueMapTy &VMap) {
120   // Clone the DISubprogram node and put it into the Value map.
121   auto *DIS = Fn->getSubprogram();
122   if (!DIS)
123     return;
124   auto *NewDIS = DIS->replaceWithDistinct(DIS->clone());
125   VMap.MD()[DIS].reset(NewDIS);
126 
127   // Find all llvm.dbg.declare intrinsics and resolve the DILocalVariable nodes
128   // they are referencing.
129   for (auto &BB : Fn->getBasicBlockList()) {
130     for (auto &I : BB) {
131       if (auto *DII = dyn_cast<llvm::DbgVariableIntrinsic>(&I)) {
132         auto *DILocal = DII->getVariable();
133         if (!DILocal->isResolved())
134           DILocal->resolve();
135       }
136     }
137   }
138 }
139 
140 // This function does roughly the same thing as GenerateThunk, but in a
141 // very different way, so that va_start and va_end work correctly.
142 // FIXME: This function assumes "this" is the first non-sret LLVM argument of
143 //        a function, and that there is an alloca built in the entry block
144 //        for all accesses to "this".
145 // FIXME: This function assumes there is only one "ret" statement per function.
146 // FIXME: Cloning isn't correct in the presence of indirect goto!
147 // FIXME: This implementation of thunks bloats codesize by duplicating the
148 //        function definition.  There are alternatives:
149 //        1. Add some sort of stub support to LLVM for cases where we can
150 //           do a this adjustment, then a sibcall.
151 //        2. We could transform the definition to take a va_list instead of an
152 //           actual variable argument list, then have the thunks (including a
153 //           no-op thunk for the regular definition) call va_start/va_end.
154 //           There's a bit of per-call overhead for this solution, but it's
155 //           better for codesize if the definition is long.
156 llvm::Function *
157 CodeGenFunction::GenerateVarArgsThunk(llvm::Function *Fn,
158                                       const CGFunctionInfo &FnInfo,
159                                       GlobalDecl GD, const ThunkInfo &Thunk) {
160   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
161   const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
162   QualType ResultType = FPT->getReturnType();
163 
164   // Get the original function
165   assert(FnInfo.isVariadic());
166   llvm::Type *Ty = CGM.getTypes().GetFunctionType(FnInfo);
167   llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
168   llvm::Function *BaseFn = cast<llvm::Function>(Callee);
169 
170   // Cloning can't work if we don't have a definition. The Microsoft ABI may
171   // require thunks when a definition is not available. Emit an error in these
172   // cases.
173   if (!MD->isDefined()) {
174     CGM.ErrorUnsupported(MD, "return-adjusting thunk with variadic arguments");
175     return Fn;
176   }
177   assert(!BaseFn->isDeclaration() && "cannot clone undefined variadic method");
178 
179   // Clone to thunk.
180   llvm::ValueToValueMapTy VMap;
181 
182   // We are cloning a function while some Metadata nodes are still unresolved.
183   // Ensure that the value mapper does not encounter any of them.
184   resolveTopLevelMetadata(BaseFn, VMap);
185   llvm::Function *NewFn = llvm::CloneFunction(BaseFn, VMap);
186   Fn->replaceAllUsesWith(NewFn);
187   NewFn->takeName(Fn);
188   Fn->eraseFromParent();
189   Fn = NewFn;
190 
191   // "Initialize" CGF (minimally).
192   CurFn = Fn;
193 
194   // Get the "this" value
195   llvm::Function::arg_iterator AI = Fn->arg_begin();
196   if (CGM.ReturnTypeUsesSRet(FnInfo))
197     ++AI;
198 
199   // Find the first store of "this", which will be to the alloca associated
200   // with "this".
201   Address ThisPtr(&*AI, CGM.getClassPointerAlignment(MD->getParent()));
202   llvm::BasicBlock *EntryBB = &Fn->front();
203   llvm::BasicBlock::iterator ThisStore =
204       std::find_if(EntryBB->begin(), EntryBB->end(), [&](llvm::Instruction &I) {
205         return isa<llvm::StoreInst>(I) &&
206                I.getOperand(0) == ThisPtr.getPointer();
207       });
208   assert(ThisStore != EntryBB->end() &&
209          "Store of this should be in entry block?");
210   // Adjust "this", if necessary.
211   Builder.SetInsertPoint(&*ThisStore);
212   llvm::Value *AdjustedThisPtr =
213       CGM.getCXXABI().performThisAdjustment(*this, ThisPtr, Thunk.This);
214   AdjustedThisPtr = Builder.CreateBitCast(AdjustedThisPtr,
215                                           ThisStore->getOperand(0)->getType());
216   ThisStore->setOperand(0, AdjustedThisPtr);
217 
218   if (!Thunk.Return.isEmpty()) {
219     // Fix up the returned value, if necessary.
220     for (llvm::BasicBlock &BB : *Fn) {
221       llvm::Instruction *T = BB.getTerminator();
222       if (isa<llvm::ReturnInst>(T)) {
223         RValue RV = RValue::get(T->getOperand(0));
224         T->eraseFromParent();
225         Builder.SetInsertPoint(&BB);
226         RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
227         Builder.CreateRet(RV.getScalarVal());
228         break;
229       }
230     }
231   }
232 
233   return Fn;
234 }
235 
236 void CodeGenFunction::StartThunk(llvm::Function *Fn, GlobalDecl GD,
237                                  const CGFunctionInfo &FnInfo,
238                                  bool IsUnprototyped) {
239   assert(!CurGD.getDecl() && "CurGD was already set!");
240   CurGD = GD;
241   CurFuncIsThunk = true;
242 
243   // Build FunctionArgs.
244   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
245   QualType ThisType = MD->getThisType();
246   QualType ResultType;
247   if (IsUnprototyped)
248     ResultType = CGM.getContext().VoidTy;
249   else if (CGM.getCXXABI().HasThisReturn(GD))
250     ResultType = ThisType;
251   else if (CGM.getCXXABI().hasMostDerivedReturn(GD))
252     ResultType = CGM.getContext().VoidPtrTy;
253   else
254     ResultType = MD->getType()->castAs<FunctionProtoType>()->getReturnType();
255   FunctionArgList FunctionArgs;
256 
257   // Create the implicit 'this' parameter declaration.
258   CGM.getCXXABI().buildThisParam(*this, FunctionArgs);
259 
260   // Add the rest of the parameters, if we have a prototype to work with.
261   if (!IsUnprototyped) {
262     FunctionArgs.append(MD->param_begin(), MD->param_end());
263 
264     if (isa<CXXDestructorDecl>(MD))
265       CGM.getCXXABI().addImplicitStructorParams(*this, ResultType,
266                                                 FunctionArgs);
267   }
268 
269   // Start defining the function.
270   auto NL = ApplyDebugLocation::CreateEmpty(*this);
271   StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs,
272                 MD->getLocation());
273   // Create a scope with an artificial location for the body of this function.
274   auto AL = ApplyDebugLocation::CreateArtificial(*this);
275 
276   // Since we didn't pass a GlobalDecl to StartFunction, do this ourselves.
277   CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
278   CXXThisValue = CXXABIThisValue;
279   CurCodeDecl = MD;
280   CurFuncDecl = MD;
281 }
282 
283 void CodeGenFunction::FinishThunk() {
284   // Clear these to restore the invariants expected by
285   // StartFunction/FinishFunction.
286   CurCodeDecl = nullptr;
287   CurFuncDecl = nullptr;
288 
289   FinishFunction();
290 }
291 
292 void CodeGenFunction::EmitCallAndReturnForThunk(llvm::FunctionCallee Callee,
293                                                 const ThunkInfo *Thunk,
294                                                 bool IsUnprototyped) {
295   assert(isa<CXXMethodDecl>(CurGD.getDecl()) &&
296          "Please use a new CGF for this thunk");
297   const CXXMethodDecl *MD = cast<CXXMethodDecl>(CurGD.getDecl());
298 
299   // Adjust the 'this' pointer if necessary
300   llvm::Value *AdjustedThisPtr =
301     Thunk ? CGM.getCXXABI().performThisAdjustment(
302                           *this, LoadCXXThisAddress(), Thunk->This)
303           : LoadCXXThis();
304 
305   // If perfect forwarding is required a variadic method, a method using
306   // inalloca, or an unprototyped thunk, use musttail. Emit an error if this
307   // thunk requires a return adjustment, since that is impossible with musttail.
308   if (CurFnInfo->usesInAlloca() || CurFnInfo->isVariadic() || IsUnprototyped) {
309     if (Thunk && !Thunk->Return.isEmpty()) {
310       if (IsUnprototyped)
311         CGM.ErrorUnsupported(
312             MD, "return-adjusting thunk with incomplete parameter type");
313       else if (CurFnInfo->isVariadic())
314         llvm_unreachable("shouldn't try to emit musttail return-adjusting "
315                          "thunks for variadic functions");
316       else
317         CGM.ErrorUnsupported(
318             MD, "non-trivial argument copy for return-adjusting thunk");
319     }
320     EmitMustTailThunk(CurGD, AdjustedThisPtr, Callee);
321     return;
322   }
323 
324   // Start building CallArgs.
325   CallArgList CallArgs;
326   QualType ThisType = MD->getThisType();
327   CallArgs.add(RValue::get(AdjustedThisPtr), ThisType);
328 
329   if (isa<CXXDestructorDecl>(MD))
330     CGM.getCXXABI().adjustCallArgsForDestructorThunk(*this, CurGD, CallArgs);
331 
332 #ifndef NDEBUG
333   unsigned PrefixArgs = CallArgs.size() - 1;
334 #endif
335   // Add the rest of the arguments.
336   for (const ParmVarDecl *PD : MD->parameters())
337     EmitDelegateCallArg(CallArgs, PD, SourceLocation());
338 
339   const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
340 
341 #ifndef NDEBUG
342   const CGFunctionInfo &CallFnInfo = CGM.getTypes().arrangeCXXMethodCall(
343       CallArgs, FPT, RequiredArgs::forPrototypePlus(FPT, 1), PrefixArgs);
344   assert(CallFnInfo.getRegParm() == CurFnInfo->getRegParm() &&
345          CallFnInfo.isNoReturn() == CurFnInfo->isNoReturn() &&
346          CallFnInfo.getCallingConvention() == CurFnInfo->getCallingConvention());
347   assert(isa<CXXDestructorDecl>(MD) || // ignore dtor return types
348          similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(),
349                  CurFnInfo->getReturnInfo(), CurFnInfo->getReturnType()));
350   assert(CallFnInfo.arg_size() == CurFnInfo->arg_size());
351   for (unsigned i = 0, e = CurFnInfo->arg_size(); i != e; ++i)
352     assert(similar(CallFnInfo.arg_begin()[i].info,
353                    CallFnInfo.arg_begin()[i].type,
354                    CurFnInfo->arg_begin()[i].info,
355                    CurFnInfo->arg_begin()[i].type));
356 #endif
357 
358   // Determine whether we have a return value slot to use.
359   QualType ResultType = CGM.getCXXABI().HasThisReturn(CurGD)
360                             ? ThisType
361                             : CGM.getCXXABI().hasMostDerivedReturn(CurGD)
362                                   ? CGM.getContext().VoidPtrTy
363                                   : FPT->getReturnType();
364   ReturnValueSlot Slot;
365   if (!ResultType->isVoidType() &&
366       (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect ||
367        hasAggregateEvaluationKind(ResultType)))
368     Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified(),
369                            /*IsUnused=*/false, /*IsExternallyDestructed=*/true);
370 
371   // Now emit our call.
372   llvm::CallBase *CallOrInvoke;
373   RValue RV = EmitCall(*CurFnInfo, CGCallee::forDirect(Callee, CurGD), Slot,
374                        CallArgs, &CallOrInvoke);
375 
376   // Consider return adjustment if we have ThunkInfo.
377   if (Thunk && !Thunk->Return.isEmpty())
378     RV = PerformReturnAdjustment(*this, ResultType, RV, *Thunk);
379   else if (llvm::CallInst* Call = dyn_cast<llvm::CallInst>(CallOrInvoke))
380     Call->setTailCallKind(llvm::CallInst::TCK_Tail);
381 
382   // Emit return.
383   if (!ResultType->isVoidType() && Slot.isNull())
384     CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType);
385 
386   // Disable the final ARC autorelease.
387   AutoreleaseResult = false;
388 
389   FinishThunk();
390 }
391 
392 void CodeGenFunction::EmitMustTailThunk(GlobalDecl GD,
393                                         llvm::Value *AdjustedThisPtr,
394                                         llvm::FunctionCallee Callee) {
395   // Emitting a musttail call thunk doesn't use any of the CGCall.cpp machinery
396   // to translate AST arguments into LLVM IR arguments.  For thunks, we know
397   // that the caller prototype more or less matches the callee prototype with
398   // the exception of 'this'.
399   SmallVector<llvm::Value *, 8> Args;
400   for (llvm::Argument &A : CurFn->args())
401     Args.push_back(&A);
402 
403   // Set the adjusted 'this' pointer.
404   const ABIArgInfo &ThisAI = CurFnInfo->arg_begin()->info;
405   if (ThisAI.isDirect()) {
406     const ABIArgInfo &RetAI = CurFnInfo->getReturnInfo();
407     int ThisArgNo = RetAI.isIndirect() && !RetAI.isSRetAfterThis() ? 1 : 0;
408     llvm::Type *ThisType = Args[ThisArgNo]->getType();
409     if (ThisType != AdjustedThisPtr->getType())
410       AdjustedThisPtr = Builder.CreateBitCast(AdjustedThisPtr, ThisType);
411     Args[ThisArgNo] = AdjustedThisPtr;
412   } else {
413     assert(ThisAI.isInAlloca() && "this is passed directly or inalloca");
414     Address ThisAddr = GetAddrOfLocalVar(CXXABIThisDecl);
415     llvm::Type *ThisType = ThisAddr.getElementType();
416     if (ThisType != AdjustedThisPtr->getType())
417       AdjustedThisPtr = Builder.CreateBitCast(AdjustedThisPtr, ThisType);
418     Builder.CreateStore(AdjustedThisPtr, ThisAddr);
419   }
420 
421   // Emit the musttail call manually.  Even if the prologue pushed cleanups, we
422   // don't actually want to run them.
423   llvm::CallInst *Call = Builder.CreateCall(Callee, Args);
424   Call->setTailCallKind(llvm::CallInst::TCK_MustTail);
425 
426   // Apply the standard set of call attributes.
427   unsigned CallingConv;
428   llvm::AttributeList Attrs;
429   CGM.ConstructAttributeList(Callee.getCallee()->getName(), *CurFnInfo, GD,
430                              Attrs, CallingConv, /*AttrOnCallSite=*/true);
431   Call->setAttributes(Attrs);
432   Call->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
433 
434   if (Call->getType()->isVoidTy())
435     Builder.CreateRetVoid();
436   else
437     Builder.CreateRet(Call);
438 
439   // Finish the function to maintain CodeGenFunction invariants.
440   // FIXME: Don't emit unreachable code.
441   EmitBlock(createBasicBlock());
442 
443   FinishThunk();
444 }
445 
446 void CodeGenFunction::generateThunk(llvm::Function *Fn,
447                                     const CGFunctionInfo &FnInfo, GlobalDecl GD,
448                                     const ThunkInfo &Thunk,
449                                     bool IsUnprototyped) {
450   StartThunk(Fn, GD, FnInfo, IsUnprototyped);
451   // Create a scope with an artificial location for the body of this function.
452   auto AL = ApplyDebugLocation::CreateArtificial(*this);
453 
454   // Get our callee. Use a placeholder type if this method is unprototyped so
455   // that CodeGenModule doesn't try to set attributes.
456   llvm::Type *Ty;
457   if (IsUnprototyped)
458     Ty = llvm::StructType::get(getLLVMContext());
459   else
460     Ty = CGM.getTypes().GetFunctionType(FnInfo);
461 
462   llvm::Constant *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
463 
464   // Fix up the function type for an unprototyped musttail call.
465   if (IsUnprototyped)
466     Callee = llvm::ConstantExpr::getBitCast(Callee, Fn->getType());
467 
468   // Make the call and return the result.
469   EmitCallAndReturnForThunk(llvm::FunctionCallee(Fn->getFunctionType(), Callee),
470                             &Thunk, IsUnprototyped);
471 }
472 
473 static bool shouldEmitVTableThunk(CodeGenModule &CGM, const CXXMethodDecl *MD,
474                                   bool IsUnprototyped, bool ForVTable) {
475   // Always emit thunks in the MS C++ ABI. We cannot rely on other TUs to
476   // provide thunks for us.
477   if (CGM.getTarget().getCXXABI().isMicrosoft())
478     return true;
479 
480   // In the Itanium C++ ABI, vtable thunks are provided by TUs that provide
481   // definitions of the main method. Therefore, emitting thunks with the vtable
482   // is purely an optimization. Emit the thunk if optimizations are enabled and
483   // all of the parameter types are complete.
484   if (ForVTable)
485     return CGM.getCodeGenOpts().OptimizationLevel && !IsUnprototyped;
486 
487   // Always emit thunks along with the method definition.
488   return true;
489 }
490 
491 llvm::Constant *CodeGenVTables::maybeEmitThunk(GlobalDecl GD,
492                                                const ThunkInfo &TI,
493                                                bool ForVTable) {
494   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
495 
496   // First, get a declaration. Compute the mangled name. Don't worry about
497   // getting the function prototype right, since we may only need this
498   // declaration to fill in a vtable slot.
499   SmallString<256> Name;
500   MangleContext &MCtx = CGM.getCXXABI().getMangleContext();
501   llvm::raw_svector_ostream Out(Name);
502   if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD))
503     MCtx.mangleCXXDtorThunk(DD, GD.getDtorType(), TI.This, Out);
504   else
505     MCtx.mangleThunk(MD, TI, Out);
506   llvm::Type *ThunkVTableTy = CGM.getTypes().GetFunctionTypeForVTable(GD);
507   llvm::Constant *Thunk = CGM.GetAddrOfThunk(Name, ThunkVTableTy, GD);
508 
509   // If we don't need to emit a definition, return this declaration as is.
510   bool IsUnprototyped = !CGM.getTypes().isFuncTypeConvertible(
511       MD->getType()->castAs<FunctionType>());
512   if (!shouldEmitVTableThunk(CGM, MD, IsUnprototyped, ForVTable))
513     return Thunk;
514 
515   // Arrange a function prototype appropriate for a function definition. In some
516   // cases in the MS ABI, we may need to build an unprototyped musttail thunk.
517   const CGFunctionInfo &FnInfo =
518       IsUnprototyped ? CGM.getTypes().arrangeUnprototypedMustTailThunk(MD)
519                      : CGM.getTypes().arrangeGlobalDeclaration(GD);
520   llvm::FunctionType *ThunkFnTy = CGM.getTypes().GetFunctionType(FnInfo);
521 
522   // If the type of the underlying GlobalValue is wrong, we'll have to replace
523   // it. It should be a declaration.
524   llvm::Function *ThunkFn = cast<llvm::Function>(Thunk->stripPointerCasts());
525   if (ThunkFn->getFunctionType() != ThunkFnTy) {
526     llvm::GlobalValue *OldThunkFn = ThunkFn;
527 
528     assert(OldThunkFn->isDeclaration() && "Shouldn't replace non-declaration");
529 
530     // Remove the name from the old thunk function and get a new thunk.
531     OldThunkFn->setName(StringRef());
532     ThunkFn = llvm::Function::Create(ThunkFnTy, llvm::Function::ExternalLinkage,
533                                      Name.str(), &CGM.getModule());
534     CGM.SetLLVMFunctionAttributes(MD, FnInfo, ThunkFn);
535 
536     // If needed, replace the old thunk with a bitcast.
537     if (!OldThunkFn->use_empty()) {
538       llvm::Constant *NewPtrForOldDecl =
539           llvm::ConstantExpr::getBitCast(ThunkFn, OldThunkFn->getType());
540       OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
541     }
542 
543     // Remove the old thunk.
544     OldThunkFn->eraseFromParent();
545   }
546 
547   bool ABIHasKeyFunctions = CGM.getTarget().getCXXABI().hasKeyFunctions();
548   bool UseAvailableExternallyLinkage = ForVTable && ABIHasKeyFunctions;
549 
550   if (!ThunkFn->isDeclaration()) {
551     if (!ABIHasKeyFunctions || UseAvailableExternallyLinkage) {
552       // There is already a thunk emitted for this function, do nothing.
553       return ThunkFn;
554     }
555 
556     setThunkProperties(CGM, TI, ThunkFn, ForVTable, GD);
557     return ThunkFn;
558   }
559 
560   // If this will be unprototyped, add the "thunk" attribute so that LLVM knows
561   // that the return type is meaningless. These thunks can be used to call
562   // functions with differing return types, and the caller is required to cast
563   // the prototype appropriately to extract the correct value.
564   if (IsUnprototyped)
565     ThunkFn->addFnAttr("thunk");
566 
567   CGM.SetLLVMFunctionAttributesForDefinition(GD.getDecl(), ThunkFn);
568 
569   // Thunks for variadic methods are special because in general variadic
570   // arguments cannot be perfectly forwarded. In the general case, clang
571   // implements such thunks by cloning the original function body. However, for
572   // thunks with no return adjustment on targets that support musttail, we can
573   // use musttail to perfectly forward the variadic arguments.
574   bool ShouldCloneVarArgs = false;
575   if (!IsUnprototyped && ThunkFn->isVarArg()) {
576     ShouldCloneVarArgs = true;
577     if (TI.Return.isEmpty()) {
578       switch (CGM.getTriple().getArch()) {
579       case llvm::Triple::x86_64:
580       case llvm::Triple::x86:
581       case llvm::Triple::aarch64:
582         ShouldCloneVarArgs = false;
583         break;
584       default:
585         break;
586       }
587     }
588   }
589 
590   if (ShouldCloneVarArgs) {
591     if (UseAvailableExternallyLinkage)
592       return ThunkFn;
593     ThunkFn =
594         CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, TI);
595   } else {
596     // Normal thunk body generation.
597     CodeGenFunction(CGM).generateThunk(ThunkFn, FnInfo, GD, TI, IsUnprototyped);
598   }
599 
600   setThunkProperties(CGM, TI, ThunkFn, ForVTable, GD);
601   return ThunkFn;
602 }
603 
604 void CodeGenVTables::EmitThunks(GlobalDecl GD) {
605   const CXXMethodDecl *MD =
606     cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
607 
608   // We don't need to generate thunks for the base destructor.
609   if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
610     return;
611 
612   const VTableContextBase::ThunkInfoVectorTy *ThunkInfoVector =
613       VTContext->getThunkInfo(GD);
614 
615   if (!ThunkInfoVector)
616     return;
617 
618   for (const ThunkInfo& Thunk : *ThunkInfoVector)
619     maybeEmitThunk(GD, Thunk, /*ForVTable=*/false);
620 }
621 
622 void CodeGenVTables::addRelativeComponent(ConstantArrayBuilder &builder,
623                                           llvm::Constant *component,
624                                           unsigned vtableAddressPoint,
625                                           bool vtableHasLocalLinkage,
626                                           bool isCompleteDtor) const {
627   // No need to get the offset of a nullptr.
628   if (component->isNullValue())
629     return builder.add(llvm::ConstantInt::get(CGM.Int32Ty, 0));
630 
631   auto *globalVal =
632       cast<llvm::GlobalValue>(component->stripPointerCastsAndAliases());
633   llvm::Module &module = CGM.getModule();
634 
635   // We don't want to copy the linkage of the vtable exactly because we still
636   // want the stub/proxy to be emitted for properly calculating the offset.
637   // Examples where there would be no symbol emitted are available_externally
638   // and private linkages.
639   auto stubLinkage = vtableHasLocalLinkage ? llvm::GlobalValue::InternalLinkage
640                                            : llvm::GlobalValue::ExternalLinkage;
641 
642   llvm::Constant *target;
643   if (auto *func = dyn_cast<llvm::Function>(globalVal)) {
644     target = llvm::DSOLocalEquivalent::get(func);
645   } else {
646     llvm::SmallString<16> rttiProxyName(globalVal->getName());
647     rttiProxyName.append(".rtti_proxy");
648 
649     // The RTTI component may not always be emitted in the same linkage unit as
650     // the vtable. As a general case, we can make a dso_local proxy to the RTTI
651     // that points to the actual RTTI struct somewhere. This will result in a
652     // GOTPCREL relocation when taking the relative offset to the proxy.
653     llvm::GlobalVariable *proxy = module.getNamedGlobal(rttiProxyName);
654     if (!proxy) {
655       proxy = new llvm::GlobalVariable(module, globalVal->getType(),
656                                        /*isConstant=*/true, stubLinkage,
657                                        globalVal, rttiProxyName);
658       proxy->setDSOLocal(true);
659       proxy->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
660       if (!proxy->hasLocalLinkage()) {
661         proxy->setVisibility(llvm::GlobalValue::HiddenVisibility);
662         proxy->setComdat(module.getOrInsertComdat(rttiProxyName));
663       }
664     }
665     target = proxy;
666   }
667 
668   builder.addRelativeOffsetToPosition(CGM.Int32Ty, target,
669                                       /*position=*/vtableAddressPoint);
670 }
671 
672 bool CodeGenVTables::useRelativeLayout() const {
673   return CGM.getTarget().getCXXABI().isItaniumFamily() &&
674          CGM.getItaniumVTableContext().isRelativeLayout();
675 }
676 
677 llvm::Type *CodeGenVTables::getVTableComponentType() const {
678   if (useRelativeLayout())
679     return CGM.Int32Ty;
680   return CGM.Int8PtrTy;
681 }
682 
683 static void AddPointerLayoutOffset(const CodeGenModule &CGM,
684                                    ConstantArrayBuilder &builder,
685                                    CharUnits offset) {
686   builder.add(llvm::ConstantExpr::getIntToPtr(
687       llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity()),
688       CGM.Int8PtrTy));
689 }
690 
691 static void AddRelativeLayoutOffset(const CodeGenModule &CGM,
692                                     ConstantArrayBuilder &builder,
693                                     CharUnits offset) {
694   builder.add(llvm::ConstantInt::get(CGM.Int32Ty, offset.getQuantity()));
695 }
696 
697 void CodeGenVTables::addVTableComponent(ConstantArrayBuilder &builder,
698                                         const VTableLayout &layout,
699                                         unsigned componentIndex,
700                                         llvm::Constant *rtti,
701                                         unsigned &nextVTableThunkIndex,
702                                         unsigned vtableAddressPoint,
703                                         bool vtableHasLocalLinkage) {
704   auto &component = layout.vtable_components()[componentIndex];
705 
706   auto addOffsetConstant =
707       useRelativeLayout() ? AddRelativeLayoutOffset : AddPointerLayoutOffset;
708 
709   switch (component.getKind()) {
710   case VTableComponent::CK_VCallOffset:
711     return addOffsetConstant(CGM, builder, component.getVCallOffset());
712 
713   case VTableComponent::CK_VBaseOffset:
714     return addOffsetConstant(CGM, builder, component.getVBaseOffset());
715 
716   case VTableComponent::CK_OffsetToTop:
717     return addOffsetConstant(CGM, builder, component.getOffsetToTop());
718 
719   case VTableComponent::CK_RTTI:
720     if (useRelativeLayout())
721       return addRelativeComponent(builder, rtti, vtableAddressPoint,
722                                   vtableHasLocalLinkage,
723                                   /*isCompleteDtor=*/false);
724     else
725       return builder.add(llvm::ConstantExpr::getBitCast(rtti, CGM.Int8PtrTy));
726 
727   case VTableComponent::CK_FunctionPointer:
728   case VTableComponent::CK_CompleteDtorPointer:
729   case VTableComponent::CK_DeletingDtorPointer: {
730     GlobalDecl GD = component.getGlobalDecl();
731 
732     if (CGM.getLangOpts().CUDA) {
733       // Emit NULL for methods we can't codegen on this
734       // side. Otherwise we'd end up with vtable with unresolved
735       // references.
736       const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
737       // OK on device side: functions w/ __device__ attribute
738       // OK on host side: anything except __device__-only functions.
739       bool CanEmitMethod =
740           CGM.getLangOpts().CUDAIsDevice
741               ? MD->hasAttr<CUDADeviceAttr>()
742               : (MD->hasAttr<CUDAHostAttr>() || !MD->hasAttr<CUDADeviceAttr>());
743       if (!CanEmitMethod)
744         return builder.add(llvm::ConstantExpr::getNullValue(CGM.Int8PtrTy));
745       // Method is acceptable, continue processing as usual.
746     }
747 
748     auto getSpecialVirtualFn = [&](StringRef name) -> llvm::Constant * {
749       // FIXME(PR43094): When merging comdat groups, lld can select a local
750       // symbol as the signature symbol even though it cannot be accessed
751       // outside that symbol's TU. The relative vtables ABI would make
752       // __cxa_pure_virtual and __cxa_deleted_virtual local symbols, and
753       // depending on link order, the comdat groups could resolve to the one
754       // with the local symbol. As a temporary solution, fill these components
755       // with zero. We shouldn't be calling these in the first place anyway.
756       if (useRelativeLayout())
757         return llvm::ConstantPointerNull::get(CGM.Int8PtrTy);
758 
759       // For NVPTX devices in OpenMP emit special functon as null pointers,
760       // otherwise linking ends up with unresolved references.
761       if (CGM.getLangOpts().OpenMP && CGM.getLangOpts().OpenMPIsDevice &&
762           CGM.getTriple().isNVPTX())
763         return llvm::ConstantPointerNull::get(CGM.Int8PtrTy);
764       llvm::FunctionType *fnTy =
765           llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
766       llvm::Constant *fn = cast<llvm::Constant>(
767           CGM.CreateRuntimeFunction(fnTy, name).getCallee());
768       if (auto f = dyn_cast<llvm::Function>(fn))
769         f->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
770       return llvm::ConstantExpr::getBitCast(fn, CGM.Int8PtrTy);
771     };
772 
773     llvm::Constant *fnPtr;
774 
775     // Pure virtual member functions.
776     if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
777       if (!PureVirtualFn)
778         PureVirtualFn =
779             getSpecialVirtualFn(CGM.getCXXABI().GetPureVirtualCallName());
780       fnPtr = PureVirtualFn;
781 
782     // Deleted virtual member functions.
783     } else if (cast<CXXMethodDecl>(GD.getDecl())->isDeleted()) {
784       if (!DeletedVirtualFn)
785         DeletedVirtualFn =
786             getSpecialVirtualFn(CGM.getCXXABI().GetDeletedVirtualCallName());
787       fnPtr = DeletedVirtualFn;
788 
789     // Thunks.
790     } else if (nextVTableThunkIndex < layout.vtable_thunks().size() &&
791                layout.vtable_thunks()[nextVTableThunkIndex].first ==
792                    componentIndex) {
793       auto &thunkInfo = layout.vtable_thunks()[nextVTableThunkIndex].second;
794 
795       nextVTableThunkIndex++;
796       fnPtr = maybeEmitThunk(GD, thunkInfo, /*ForVTable=*/true);
797 
798     // Otherwise we can use the method definition directly.
799     } else {
800       llvm::Type *fnTy = CGM.getTypes().GetFunctionTypeForVTable(GD);
801       fnPtr = CGM.GetAddrOfFunction(GD, fnTy, /*ForVTable=*/true);
802     }
803 
804     if (useRelativeLayout()) {
805       return addRelativeComponent(
806           builder, fnPtr, vtableAddressPoint, vtableHasLocalLinkage,
807           component.getKind() == VTableComponent::CK_CompleteDtorPointer);
808     } else
809       return builder.add(llvm::ConstantExpr::getBitCast(fnPtr, CGM.Int8PtrTy));
810   }
811 
812   case VTableComponent::CK_UnusedFunctionPointer:
813     if (useRelativeLayout())
814       return builder.add(llvm::ConstantExpr::getNullValue(CGM.Int32Ty));
815     else
816       return builder.addNullPointer(CGM.Int8PtrTy);
817   }
818 
819   llvm_unreachable("Unexpected vtable component kind");
820 }
821 
822 llvm::Type *CodeGenVTables::getVTableType(const VTableLayout &layout) {
823   SmallVector<llvm::Type *, 4> tys;
824   llvm::Type *componentType = getVTableComponentType();
825   for (unsigned i = 0, e = layout.getNumVTables(); i != e; ++i)
826     tys.push_back(llvm::ArrayType::get(componentType, layout.getVTableSize(i)));
827 
828   return llvm::StructType::get(CGM.getLLVMContext(), tys);
829 }
830 
831 void CodeGenVTables::createVTableInitializer(ConstantStructBuilder &builder,
832                                              const VTableLayout &layout,
833                                              llvm::Constant *rtti,
834                                              bool vtableHasLocalLinkage) {
835   llvm::Type *componentType = getVTableComponentType();
836 
837   const auto &addressPoints = layout.getAddressPointIndices();
838   unsigned nextVTableThunkIndex = 0;
839   for (unsigned vtableIndex = 0, endIndex = layout.getNumVTables();
840        vtableIndex != endIndex; ++vtableIndex) {
841     auto vtableElem = builder.beginArray(componentType);
842 
843     size_t vtableStart = layout.getVTableOffset(vtableIndex);
844     size_t vtableEnd = vtableStart + layout.getVTableSize(vtableIndex);
845     for (size_t componentIndex = vtableStart; componentIndex < vtableEnd;
846          ++componentIndex) {
847       addVTableComponent(vtableElem, layout, componentIndex, rtti,
848                          nextVTableThunkIndex, addressPoints[vtableIndex],
849                          vtableHasLocalLinkage);
850     }
851     vtableElem.finishAndAddTo(builder);
852   }
853 }
854 
855 llvm::GlobalVariable *CodeGenVTables::GenerateConstructionVTable(
856     const CXXRecordDecl *RD, const BaseSubobject &Base, bool BaseIsVirtual,
857     llvm::GlobalVariable::LinkageTypes Linkage,
858     VTableAddressPointsMapTy &AddressPoints) {
859   if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
860     DI->completeClassData(Base.getBase());
861 
862   std::unique_ptr<VTableLayout> VTLayout(
863       getItaniumVTableContext().createConstructionVTableLayout(
864           Base.getBase(), Base.getBaseOffset(), BaseIsVirtual, RD));
865 
866   // Add the address points.
867   AddressPoints = VTLayout->getAddressPoints();
868 
869   // Get the mangled construction vtable name.
870   SmallString<256> OutName;
871   llvm::raw_svector_ostream Out(OutName);
872   cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
873       .mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(),
874                            Base.getBase(), Out);
875   SmallString<256> Name(OutName);
876 
877   bool UsingRelativeLayout = getItaniumVTableContext().isRelativeLayout();
878   bool VTableAliasExists =
879       UsingRelativeLayout && CGM.getModule().getNamedAlias(Name);
880   if (VTableAliasExists) {
881     // We previously made the vtable hidden and changed its name.
882     Name.append(".local");
883   }
884 
885   llvm::Type *VTType = getVTableType(*VTLayout);
886 
887   // Construction vtable symbols are not part of the Itanium ABI, so we cannot
888   // guarantee that they actually will be available externally. Instead, when
889   // emitting an available_externally VTT, we provide references to an internal
890   // linkage construction vtable. The ABI only requires complete-object vtables
891   // to be the same for all instances of a type, not construction vtables.
892   if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
893     Linkage = llvm::GlobalVariable::InternalLinkage;
894 
895   unsigned Align = CGM.getDataLayout().getABITypeAlignment(VTType);
896 
897   // Create the variable that will hold the construction vtable.
898   llvm::GlobalVariable *VTable =
899       CGM.CreateOrReplaceCXXRuntimeVariable(Name, VTType, Linkage, Align);
900 
901   // V-tables are always unnamed_addr.
902   VTable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
903 
904   llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(
905       CGM.getContext().getTagDeclType(Base.getBase()));
906 
907   // Create and set the initializer.
908   ConstantInitBuilder builder(CGM);
909   auto components = builder.beginStruct();
910   createVTableInitializer(components, *VTLayout, RTTI,
911                           VTable->hasLocalLinkage());
912   components.finishAndSetAsInitializer(VTable);
913 
914   // Set properties only after the initializer has been set to ensure that the
915   // GV is treated as definition and not declaration.
916   assert(!VTable->isDeclaration() && "Shouldn't set properties on declaration");
917   CGM.setGVProperties(VTable, RD);
918 
919   CGM.EmitVTableTypeMetadata(RD, VTable, *VTLayout.get());
920 
921   if (UsingRelativeLayout && !VTable->isDSOLocal())
922     GenerateRelativeVTableAlias(VTable, OutName);
923 
924   return VTable;
925 }
926 
927 // If the VTable is not dso_local, then we will not be able to indicate that
928 // the VTable does not need a relocation and move into rodata. A frequent
929 // time this can occur is for classes that should be made public from a DSO
930 // (like in libc++). For cases like these, we can make the vtable hidden or
931 // private and create a public alias with the same visibility and linkage as
932 // the original vtable type.
933 void CodeGenVTables::GenerateRelativeVTableAlias(llvm::GlobalVariable *VTable,
934                                                  llvm::StringRef AliasNameRef) {
935   assert(getItaniumVTableContext().isRelativeLayout() &&
936          "Can only use this if the relative vtable ABI is used");
937   assert(!VTable->isDSOLocal() && "This should be called only if the vtable is "
938                                   "not guaranteed to be dso_local");
939 
940   // If the vtable is available_externally, we shouldn't (or need to) generate
941   // an alias for it in the first place since the vtable won't actually by
942   // emitted in this compilation unit.
943   if (VTable->hasAvailableExternallyLinkage())
944     return;
945 
946   // Create a new string in the event the alias is already the name of the
947   // vtable. Using the reference directly could lead to use of an inititialized
948   // value in the module's StringMap.
949   llvm::SmallString<256> AliasName(AliasNameRef);
950   VTable->setName(AliasName + ".local");
951 
952   auto Linkage = VTable->getLinkage();
953   assert(llvm::GlobalAlias::isValidLinkage(Linkage) &&
954          "Invalid vtable alias linkage");
955 
956   llvm::GlobalAlias *VTableAlias = CGM.getModule().getNamedAlias(AliasName);
957   if (!VTableAlias) {
958     VTableAlias = llvm::GlobalAlias::create(VTable->getValueType(),
959                                             VTable->getAddressSpace(), Linkage,
960                                             AliasName, &CGM.getModule());
961   } else {
962     assert(VTableAlias->getValueType() == VTable->getValueType());
963     assert(VTableAlias->getLinkage() == Linkage);
964   }
965   VTableAlias->setVisibility(VTable->getVisibility());
966   VTableAlias->setUnnamedAddr(VTable->getUnnamedAddr());
967 
968   // Both of these imply dso_local for the vtable.
969   if (!VTable->hasComdat()) {
970     // If this is in a comdat, then we shouldn't make the linkage private due to
971     // an issue in lld where private symbols can be used as the key symbol when
972     // choosing the prevelant group. This leads to "relocation refers to a
973     // symbol in a discarded section".
974     VTable->setLinkage(llvm::GlobalValue::PrivateLinkage);
975   } else {
976     // We should at least make this hidden since we don't want to expose it.
977     VTable->setVisibility(llvm::GlobalValue::HiddenVisibility);
978   }
979 
980   VTableAlias->setAliasee(VTable);
981 }
982 
983 static bool shouldEmitAvailableExternallyVTable(const CodeGenModule &CGM,
984                                                 const CXXRecordDecl *RD) {
985   return CGM.getCodeGenOpts().OptimizationLevel > 0 &&
986          CGM.getCXXABI().canSpeculativelyEmitVTable(RD);
987 }
988 
989 /// Compute the required linkage of the vtable for the given class.
990 ///
991 /// Note that we only call this at the end of the translation unit.
992 llvm::GlobalVariable::LinkageTypes
993 CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
994   if (!RD->isExternallyVisible())
995     return llvm::GlobalVariable::InternalLinkage;
996 
997   // We're at the end of the translation unit, so the current key
998   // function is fully correct.
999   const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction(RD);
1000   if (keyFunction && !RD->hasAttr<DLLImportAttr>()) {
1001     // If this class has a key function, use that to determine the
1002     // linkage of the vtable.
1003     const FunctionDecl *def = nullptr;
1004     if (keyFunction->hasBody(def))
1005       keyFunction = cast<CXXMethodDecl>(def);
1006 
1007     switch (keyFunction->getTemplateSpecializationKind()) {
1008       case TSK_Undeclared:
1009       case TSK_ExplicitSpecialization:
1010         assert((def || CodeGenOpts.OptimizationLevel > 0 ||
1011                 CodeGenOpts.getDebugInfo() != codegenoptions::NoDebugInfo) &&
1012                "Shouldn't query vtable linkage without key function, "
1013                "optimizations, or debug info");
1014         if (!def && CodeGenOpts.OptimizationLevel > 0)
1015           return llvm::GlobalVariable::AvailableExternallyLinkage;
1016 
1017         if (keyFunction->isInlined())
1018           return !Context.getLangOpts().AppleKext ?
1019                    llvm::GlobalVariable::LinkOnceODRLinkage :
1020                    llvm::Function::InternalLinkage;
1021 
1022         return llvm::GlobalVariable::ExternalLinkage;
1023 
1024       case TSK_ImplicitInstantiation:
1025         return !Context.getLangOpts().AppleKext ?
1026                  llvm::GlobalVariable::LinkOnceODRLinkage :
1027                  llvm::Function::InternalLinkage;
1028 
1029       case TSK_ExplicitInstantiationDefinition:
1030         return !Context.getLangOpts().AppleKext ?
1031                  llvm::GlobalVariable::WeakODRLinkage :
1032                  llvm::Function::InternalLinkage;
1033 
1034       case TSK_ExplicitInstantiationDeclaration:
1035         llvm_unreachable("Should not have been asked to emit this");
1036     }
1037   }
1038 
1039   // -fapple-kext mode does not support weak linkage, so we must use
1040   // internal linkage.
1041   if (Context.getLangOpts().AppleKext)
1042     return llvm::Function::InternalLinkage;
1043 
1044   llvm::GlobalVariable::LinkageTypes DiscardableODRLinkage =
1045       llvm::GlobalValue::LinkOnceODRLinkage;
1046   llvm::GlobalVariable::LinkageTypes NonDiscardableODRLinkage =
1047       llvm::GlobalValue::WeakODRLinkage;
1048   if (RD->hasAttr<DLLExportAttr>()) {
1049     // Cannot discard exported vtables.
1050     DiscardableODRLinkage = NonDiscardableODRLinkage;
1051   } else if (RD->hasAttr<DLLImportAttr>()) {
1052     // Imported vtables are available externally.
1053     DiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage;
1054     NonDiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage;
1055   }
1056 
1057   switch (RD->getTemplateSpecializationKind()) {
1058     case TSK_Undeclared:
1059     case TSK_ExplicitSpecialization:
1060     case TSK_ImplicitInstantiation:
1061       return DiscardableODRLinkage;
1062 
1063     case TSK_ExplicitInstantiationDeclaration:
1064       // Explicit instantiations in MSVC do not provide vtables, so we must emit
1065       // our own.
1066       if (getTarget().getCXXABI().isMicrosoft())
1067         return DiscardableODRLinkage;
1068       return shouldEmitAvailableExternallyVTable(*this, RD)
1069                  ? llvm::GlobalVariable::AvailableExternallyLinkage
1070                  : llvm::GlobalVariable::ExternalLinkage;
1071 
1072     case TSK_ExplicitInstantiationDefinition:
1073       return NonDiscardableODRLinkage;
1074   }
1075 
1076   llvm_unreachable("Invalid TemplateSpecializationKind!");
1077 }
1078 
1079 /// This is a callback from Sema to tell us that a particular vtable is
1080 /// required to be emitted in this translation unit.
1081 ///
1082 /// This is only called for vtables that _must_ be emitted (mainly due to key
1083 /// functions).  For weak vtables, CodeGen tracks when they are needed and
1084 /// emits them as-needed.
1085 void CodeGenModule::EmitVTable(CXXRecordDecl *theClass) {
1086   VTables.GenerateClassData(theClass);
1087 }
1088 
1089 void
1090 CodeGenVTables::GenerateClassData(const CXXRecordDecl *RD) {
1091   if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
1092     DI->completeClassData(RD);
1093 
1094   if (RD->getNumVBases())
1095     CGM.getCXXABI().emitVirtualInheritanceTables(RD);
1096 
1097   CGM.getCXXABI().emitVTableDefinitions(*this, RD);
1098 }
1099 
1100 /// At this point in the translation unit, does it appear that can we
1101 /// rely on the vtable being defined elsewhere in the program?
1102 ///
1103 /// The response is really only definitive when called at the end of
1104 /// the translation unit.
1105 ///
1106 /// The only semantic restriction here is that the object file should
1107 /// not contain a vtable definition when that vtable is defined
1108 /// strongly elsewhere.  Otherwise, we'd just like to avoid emitting
1109 /// vtables when unnecessary.
1110 bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
1111   assert(RD->isDynamicClass() && "Non-dynamic classes have no VTable.");
1112 
1113   // We always synthesize vtables if they are needed in the MS ABI. MSVC doesn't
1114   // emit them even if there is an explicit template instantiation.
1115   if (CGM.getTarget().getCXXABI().isMicrosoft())
1116     return false;
1117 
1118   // If we have an explicit instantiation declaration (and not a
1119   // definition), the vtable is defined elsewhere.
1120   TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
1121   if (TSK == TSK_ExplicitInstantiationDeclaration)
1122     return true;
1123 
1124   // Otherwise, if the class is an instantiated template, the
1125   // vtable must be defined here.
1126   if (TSK == TSK_ImplicitInstantiation ||
1127       TSK == TSK_ExplicitInstantiationDefinition)
1128     return false;
1129 
1130   // Otherwise, if the class doesn't have a key function (possibly
1131   // anymore), the vtable must be defined here.
1132   const CXXMethodDecl *keyFunction = CGM.getContext().getCurrentKeyFunction(RD);
1133   if (!keyFunction)
1134     return false;
1135 
1136   // Otherwise, if we don't have a definition of the key function, the
1137   // vtable must be defined somewhere else.
1138   return !keyFunction->hasBody();
1139 }
1140 
1141 /// Given that we're currently at the end of the translation unit, and
1142 /// we've emitted a reference to the vtable for this class, should
1143 /// we define that vtable?
1144 static bool shouldEmitVTableAtEndOfTranslationUnit(CodeGenModule &CGM,
1145                                                    const CXXRecordDecl *RD) {
1146   // If vtable is internal then it has to be done.
1147   if (!CGM.getVTables().isVTableExternal(RD))
1148     return true;
1149 
1150   // If it's external then maybe we will need it as available_externally.
1151   return shouldEmitAvailableExternallyVTable(CGM, RD);
1152 }
1153 
1154 /// Given that at some point we emitted a reference to one or more
1155 /// vtables, and that we are now at the end of the translation unit,
1156 /// decide whether we should emit them.
1157 void CodeGenModule::EmitDeferredVTables() {
1158 #ifndef NDEBUG
1159   // Remember the size of DeferredVTables, because we're going to assume
1160   // that this entire operation doesn't modify it.
1161   size_t savedSize = DeferredVTables.size();
1162 #endif
1163 
1164   for (const CXXRecordDecl *RD : DeferredVTables)
1165     if (shouldEmitVTableAtEndOfTranslationUnit(*this, RD))
1166       VTables.GenerateClassData(RD);
1167     else if (shouldOpportunisticallyEmitVTables())
1168       OpportunisticVTables.push_back(RD);
1169 
1170   assert(savedSize == DeferredVTables.size() &&
1171          "deferred extra vtables during vtable emission?");
1172   DeferredVTables.clear();
1173 }
1174 
1175 bool CodeGenModule::HasLTOVisibilityPublicStd(const CXXRecordDecl *RD) {
1176   if (!getCodeGenOpts().LTOVisibilityPublicStd)
1177     return false;
1178 
1179   const DeclContext *DC = RD;
1180   while (1) {
1181     auto *D = cast<Decl>(DC);
1182     DC = DC->getParent();
1183     if (isa<TranslationUnitDecl>(DC->getRedeclContext())) {
1184       if (auto *ND = dyn_cast<NamespaceDecl>(D))
1185         if (const IdentifierInfo *II = ND->getIdentifier())
1186           if (II->isStr("std") || II->isStr("stdext"))
1187             return true;
1188       break;
1189     }
1190   }
1191 
1192   return false;
1193 }
1194 
1195 bool CodeGenModule::HasHiddenLTOVisibility(const CXXRecordDecl *RD) {
1196   LinkageInfo LV = RD->getLinkageAndVisibility();
1197   if (!isExternallyVisible(LV.getLinkage()))
1198     return true;
1199 
1200   if (RD->hasAttr<LTOVisibilityPublicAttr>() || RD->hasAttr<UuidAttr>())
1201     return false;
1202 
1203   if (getTriple().isOSBinFormatCOFF()) {
1204     if (RD->hasAttr<DLLExportAttr>() || RD->hasAttr<DLLImportAttr>())
1205       return false;
1206   } else {
1207     if (LV.getVisibility() != HiddenVisibility)
1208       return false;
1209   }
1210 
1211   return !HasLTOVisibilityPublicStd(RD);
1212 }
1213 
1214 llvm::GlobalObject::VCallVisibility CodeGenModule::GetVCallVisibilityLevel(
1215     const CXXRecordDecl *RD, llvm::DenseSet<const CXXRecordDecl *> &Visited) {
1216   // If we have already visited this RD (which means this is a recursive call
1217   // since the initial call should have an empty Visited set), return the max
1218   // visibility. The recursive calls below compute the min between the result
1219   // of the recursive call and the current TypeVis, so returning the max here
1220   // ensures that it will have no effect on the current TypeVis.
1221   if (!Visited.insert(RD).second)
1222     return llvm::GlobalObject::VCallVisibilityTranslationUnit;
1223 
1224   LinkageInfo LV = RD->getLinkageAndVisibility();
1225   llvm::GlobalObject::VCallVisibility TypeVis;
1226   if (!isExternallyVisible(LV.getLinkage()))
1227     TypeVis = llvm::GlobalObject::VCallVisibilityTranslationUnit;
1228   else if (HasHiddenLTOVisibility(RD))
1229     TypeVis = llvm::GlobalObject::VCallVisibilityLinkageUnit;
1230   else
1231     TypeVis = llvm::GlobalObject::VCallVisibilityPublic;
1232 
1233   for (auto B : RD->bases())
1234     if (B.getType()->getAsCXXRecordDecl()->isDynamicClass())
1235       TypeVis = std::min(
1236           TypeVis,
1237           GetVCallVisibilityLevel(B.getType()->getAsCXXRecordDecl(), Visited));
1238 
1239   for (auto B : RD->vbases())
1240     if (B.getType()->getAsCXXRecordDecl()->isDynamicClass())
1241       TypeVis = std::min(
1242           TypeVis,
1243           GetVCallVisibilityLevel(B.getType()->getAsCXXRecordDecl(), Visited));
1244 
1245   return TypeVis;
1246 }
1247 
1248 void CodeGenModule::EmitVTableTypeMetadata(const CXXRecordDecl *RD,
1249                                            llvm::GlobalVariable *VTable,
1250                                            const VTableLayout &VTLayout) {
1251   if (!getCodeGenOpts().LTOUnit)
1252     return;
1253 
1254   CharUnits PointerWidth =
1255       Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
1256 
1257   typedef std::pair<const CXXRecordDecl *, unsigned> AddressPoint;
1258   std::vector<AddressPoint> AddressPoints;
1259   for (auto &&AP : VTLayout.getAddressPoints())
1260     AddressPoints.push_back(std::make_pair(
1261         AP.first.getBase(), VTLayout.getVTableOffset(AP.second.VTableIndex) +
1262                                 AP.second.AddressPointIndex));
1263 
1264   // Sort the address points for determinism.
1265   llvm::sort(AddressPoints, [this](const AddressPoint &AP1,
1266                                    const AddressPoint &AP2) {
1267     if (&AP1 == &AP2)
1268       return false;
1269 
1270     std::string S1;
1271     llvm::raw_string_ostream O1(S1);
1272     getCXXABI().getMangleContext().mangleTypeName(
1273         QualType(AP1.first->getTypeForDecl(), 0), O1);
1274     O1.flush();
1275 
1276     std::string S2;
1277     llvm::raw_string_ostream O2(S2);
1278     getCXXABI().getMangleContext().mangleTypeName(
1279         QualType(AP2.first->getTypeForDecl(), 0), O2);
1280     O2.flush();
1281 
1282     if (S1 < S2)
1283       return true;
1284     if (S1 != S2)
1285       return false;
1286 
1287     return AP1.second < AP2.second;
1288   });
1289 
1290   ArrayRef<VTableComponent> Comps = VTLayout.vtable_components();
1291   for (auto AP : AddressPoints) {
1292     // Create type metadata for the address point.
1293     AddVTableTypeMetadata(VTable, PointerWidth * AP.second, AP.first);
1294 
1295     // The class associated with each address point could also potentially be
1296     // used for indirect calls via a member function pointer, so we need to
1297     // annotate the address of each function pointer with the appropriate member
1298     // function pointer type.
1299     for (unsigned I = 0; I != Comps.size(); ++I) {
1300       if (Comps[I].getKind() != VTableComponent::CK_FunctionPointer)
1301         continue;
1302       llvm::Metadata *MD = CreateMetadataIdentifierForVirtualMemPtrType(
1303           Context.getMemberPointerType(
1304               Comps[I].getFunctionDecl()->getType(),
1305               Context.getRecordType(AP.first).getTypePtr()));
1306       VTable->addTypeMetadata((PointerWidth * I).getQuantity(), MD);
1307     }
1308   }
1309 
1310   if (getCodeGenOpts().VirtualFunctionElimination ||
1311       getCodeGenOpts().WholeProgramVTables) {
1312     llvm::DenseSet<const CXXRecordDecl *> Visited;
1313     llvm::GlobalObject::VCallVisibility TypeVis =
1314         GetVCallVisibilityLevel(RD, Visited);
1315     if (TypeVis != llvm::GlobalObject::VCallVisibilityPublic)
1316       VTable->setVCallVisibilityMetadata(TypeVis);
1317   }
1318 }
1319