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