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