1207619Srdivacky//===--- CGVTables.cpp - Emit LLVM Code for C++ vtables -------------------===//
2207619Srdivacky//
3207619Srdivacky//                     The LLVM Compiler Infrastructure
4207619Srdivacky//
5207619Srdivacky// This file is distributed under the University of Illinois Open Source
6207619Srdivacky// License. See LICENSE.TXT for details.
7207619Srdivacky//
8207619Srdivacky//===----------------------------------------------------------------------===//
9207619Srdivacky//
10207619Srdivacky// This contains code dealing with C++ code generation of virtual tables.
11207619Srdivacky//
12207619Srdivacky//===----------------------------------------------------------------------===//
13207619Srdivacky
14207619Srdivacky#include "CodeGenFunction.h"
15212904Sdim#include "CGCXXABI.h"
16249423Sdim#include "CodeGenModule.h"
17207619Srdivacky#include "clang/AST/CXXInheritance.h"
18207619Srdivacky#include "clang/AST/RecordLayout.h"
19261991Sdim#include "clang/CodeGen/CGFunctionInfo.h"
20212904Sdim#include "clang/Frontend/CodeGenOptions.h"
21207619Srdivacky#include "llvm/ADT/DenseSet.h"
22207619Srdivacky#include "llvm/ADT/SetVector.h"
23207619Srdivacky#include "llvm/Support/Compiler.h"
24207619Srdivacky#include "llvm/Support/Format.h"
25223017Sdim#include "llvm/Transforms/Utils/Cloning.h"
26207619Srdivacky#include <algorithm>
27207619Srdivacky#include <cstdio>
28207619Srdivacky
29207619Srdivackyusing namespace clang;
30207619Srdivackyusing namespace CodeGen;
31207619Srdivacky
32226633SdimCodeGenVTables::CodeGenVTables(CodeGenModule &CGM)
33276479Sdim    : CGM(CGM), VTContext(CGM.getContext().getVTableContext()) {}
34207619Srdivacky
35207619Srdivackyllvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
36207619Srdivacky                                              const ThunkInfo &Thunk) {
37207619Srdivacky  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
38207619Srdivacky
39207619Srdivacky  // Compute the mangled name.
40234353Sdim  SmallString<256> Name;
41218893Sdim  llvm::raw_svector_ostream Out(Name);
42207619Srdivacky  if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
43212904Sdim    getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(),
44218893Sdim                                                      Thunk.This, Out);
45207619Srdivacky  else
46218893Sdim    getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out);
47218893Sdim
48226633Sdim  llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD);
49276479Sdim  return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true,
50280031Sdim                                 /*DontDefer=*/true, /*IsThunk=*/true);
51207619Srdivacky}
52207619Srdivacky
53212904Sdimstatic void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD,
54212904Sdim                               const ThunkInfo &Thunk, llvm::Function *Fn) {
55212904Sdim  CGM.setGlobalVisibility(Fn, MD);
56212904Sdim}
57212904Sdim
58288943Sdimstatic void setThunkProperties(CodeGenModule &CGM, const ThunkInfo &Thunk,
59288943Sdim                               llvm::Function *ThunkFn, bool ForVTable,
60288943Sdim                               GlobalDecl GD) {
61288943Sdim  CGM.setFunctionLinkage(GD, ThunkFn);
62288943Sdim  CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable, GD,
63288943Sdim                                  !Thunk.Return.isEmpty());
64288943Sdim
65288943Sdim  // Set the right visibility.
66288943Sdim  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
67288943Sdim  setThunkVisibility(CGM, MD, Thunk, ThunkFn);
68288943Sdim
69288943Sdim  if (CGM.supportsCOMDAT() && ThunkFn->isWeakForLinker())
70288943Sdim    ThunkFn->setComdat(CGM.getModule().getOrInsertComdat(ThunkFn->getName()));
71288943Sdim}
72288943Sdim
73221345Sdim#ifndef NDEBUG
74221345Sdimstatic bool similar(const ABIArgInfo &infoL, CanQualType typeL,
75221345Sdim                    const ABIArgInfo &infoR, CanQualType typeR) {
76221345Sdim  return (infoL.getKind() == infoR.getKind() &&
77221345Sdim          (typeL == typeR ||
78221345Sdim           (isa<PointerType>(typeL) && isa<PointerType>(typeR)) ||
79221345Sdim           (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR))));
80221345Sdim}
81221345Sdim#endif
82221345Sdim
83223017Sdimstatic RValue PerformReturnAdjustment(CodeGenFunction &CGF,
84223017Sdim                                      QualType ResultType, RValue RV,
85223017Sdim                                      const ThunkInfo &Thunk) {
86223017Sdim  // Emit the return adjustment.
87223017Sdim  bool NullCheckValue = !ResultType->isReferenceType();
88276479Sdim
89276479Sdim  llvm::BasicBlock *AdjustNull = nullptr;
90276479Sdim  llvm::BasicBlock *AdjustNotNull = nullptr;
91276479Sdim  llvm::BasicBlock *AdjustEnd = nullptr;
92276479Sdim
93223017Sdim  llvm::Value *ReturnValue = RV.getScalarVal();
94223017Sdim
95223017Sdim  if (NullCheckValue) {
96223017Sdim    AdjustNull = CGF.createBasicBlock("adjust.null");
97223017Sdim    AdjustNotNull = CGF.createBasicBlock("adjust.notnull");
98223017Sdim    AdjustEnd = CGF.createBasicBlock("adjust.end");
99223017Sdim
100223017Sdim    llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue);
101223017Sdim    CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
102223017Sdim    CGF.EmitBlock(AdjustNotNull);
103223017Sdim  }
104261991Sdim
105296417Sdim  auto ClassDecl = ResultType->getPointeeType()->getAsCXXRecordDecl();
106296417Sdim  auto ClassAlign = CGF.CGM.getClassPointerAlignment(ClassDecl);
107296417Sdim  ReturnValue = CGF.CGM.getCXXABI().performReturnAdjustment(CGF,
108296417Sdim                                            Address(ReturnValue, ClassAlign),
109296417Sdim                                            Thunk.Return);
110261991Sdim
111223017Sdim  if (NullCheckValue) {
112223017Sdim    CGF.Builder.CreateBr(AdjustEnd);
113223017Sdim    CGF.EmitBlock(AdjustNull);
114223017Sdim    CGF.Builder.CreateBr(AdjustEnd);
115223017Sdim    CGF.EmitBlock(AdjustEnd);
116223017Sdim
117223017Sdim    llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2);
118223017Sdim    PHI->addIncoming(ReturnValue, AdjustNotNull);
119223017Sdim    PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
120223017Sdim                     AdjustNull);
121223017Sdim    ReturnValue = PHI;
122223017Sdim  }
123223017Sdim
124223017Sdim  return RValue::get(ReturnValue);
125223017Sdim}
126223017Sdim
127223017Sdim// This function does roughly the same thing as GenerateThunk, but in a
128223017Sdim// very different way, so that va_start and va_end work correctly.
129223017Sdim// FIXME: This function assumes "this" is the first non-sret LLVM argument of
130223017Sdim//        a function, and that there is an alloca built in the entry block
131223017Sdim//        for all accesses to "this".
132223017Sdim// FIXME: This function assumes there is only one "ret" statement per function.
133223017Sdim// FIXME: Cloning isn't correct in the presence of indirect goto!
134223017Sdim// FIXME: This implementation of thunks bloats codesize by duplicating the
135223017Sdim//        function definition.  There are alternatives:
136223017Sdim//        1. Add some sort of stub support to LLVM for cases where we can
137223017Sdim//           do a this adjustment, then a sibcall.
138223017Sdim//        2. We could transform the definition to take a va_list instead of an
139223017Sdim//           actual variable argument list, then have the thunks (including a
140223017Sdim//           no-op thunk for the regular definition) call va_start/va_end.
141223017Sdim//           There's a bit of per-call overhead for this solution, but it's
142223017Sdim//           better for codesize if the definition is long.
143288943Sdimllvm::Function *
144288943SdimCodeGenFunction::GenerateVarArgsThunk(llvm::Function *Fn,
145223017Sdim                                      const CGFunctionInfo &FnInfo,
146223017Sdim                                      GlobalDecl GD, const ThunkInfo &Thunk) {
147223017Sdim  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
148223017Sdim  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
149276479Sdim  QualType ResultType = FPT->getReturnType();
150223017Sdim
151223017Sdim  // Get the original function
152234353Sdim  assert(FnInfo.isVariadic());
153234353Sdim  llvm::Type *Ty = CGM.getTypes().GetFunctionType(FnInfo);
154223017Sdim  llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
155223017Sdim  llvm::Function *BaseFn = cast<llvm::Function>(Callee);
156223017Sdim
157223017Sdim  // Clone to thunk.
158243830Sdim  llvm::ValueToValueMapTy VMap;
159243830Sdim  llvm::Function *NewFn = llvm::CloneFunction(BaseFn, VMap,
160243830Sdim                                              /*ModuleLevelChanges=*/false);
161223017Sdim  CGM.getModule().getFunctionList().push_back(NewFn);
162223017Sdim  Fn->replaceAllUsesWith(NewFn);
163223017Sdim  NewFn->takeName(Fn);
164223017Sdim  Fn->eraseFromParent();
165223017Sdim  Fn = NewFn;
166223017Sdim
167223017Sdim  // "Initialize" CGF (minimally).
168223017Sdim  CurFn = Fn;
169223017Sdim
170223017Sdim  // Get the "this" value
171223017Sdim  llvm::Function::arg_iterator AI = Fn->arg_begin();
172223017Sdim  if (CGM.ReturnTypeUsesSRet(FnInfo))
173223017Sdim    ++AI;
174223017Sdim
175223017Sdim  // Find the first store of "this", which will be to the alloca associated
176223017Sdim  // with "this".
177296417Sdim  Address ThisPtr(&*AI, CGM.getClassPointerAlignment(MD->getParent()));
178296417Sdim  llvm::BasicBlock *EntryBB = &Fn->front();
179296417Sdim  llvm::BasicBlock::iterator ThisStore =
180280031Sdim      std::find_if(EntryBB->begin(), EntryBB->end(), [&](llvm::Instruction &I) {
181296417Sdim        return isa<llvm::StoreInst>(I) &&
182296417Sdim               I.getOperand(0) == ThisPtr.getPointer();
183296417Sdim      });
184296417Sdim  assert(ThisStore != EntryBB->end() &&
185296417Sdim         "Store of this should be in entry block?");
186223017Sdim  // Adjust "this", if necessary.
187296417Sdim  Builder.SetInsertPoint(&*ThisStore);
188261991Sdim  llvm::Value *AdjustedThisPtr =
189261991Sdim      CGM.getCXXABI().performThisAdjustment(*this, ThisPtr, Thunk.This);
190223017Sdim  ThisStore->setOperand(0, AdjustedThisPtr);
191223017Sdim
192223017Sdim  if (!Thunk.Return.isEmpty()) {
193223017Sdim    // Fix up the returned value, if necessary.
194296417Sdim    for (llvm::BasicBlock &BB : *Fn) {
195296417Sdim      llvm::Instruction *T = BB.getTerminator();
196223017Sdim      if (isa<llvm::ReturnInst>(T)) {
197223017Sdim        RValue RV = RValue::get(T->getOperand(0));
198223017Sdim        T->eraseFromParent();
199296417Sdim        Builder.SetInsertPoint(&BB);
200223017Sdim        RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
201223017Sdim        Builder.CreateRet(RV.getScalarVal());
202223017Sdim        break;
203223017Sdim      }
204223017Sdim    }
205223017Sdim  }
206288943Sdim
207288943Sdim  return Fn;
208223017Sdim}
209223017Sdim
210261991Sdimvoid CodeGenFunction::StartThunk(llvm::Function *Fn, GlobalDecl GD,
211261991Sdim                                 const CGFunctionInfo &FnInfo) {
212261991Sdim  assert(!CurGD.getDecl() && "CurGD was already set!");
213261991Sdim  CurGD = GD;
214280031Sdim  CurFuncIsThunk = true;
215261991Sdim
216261991Sdim  // Build FunctionArgs.
217207619Srdivacky  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
218261991Sdim  QualType ThisType = MD->getThisType(getContext());
219207619Srdivacky  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
220280031Sdim  QualType ResultType = CGM.getCXXABI().HasThisReturn(GD)
221280031Sdim                            ? ThisType
222280031Sdim                            : CGM.getCXXABI().hasMostDerivedReturn(GD)
223280031Sdim                                  ? CGM.getContext().VoidPtrTy
224280031Sdim                                  : FPT->getReturnType();
225207619Srdivacky  FunctionArgList FunctionArgs;
226207619Srdivacky
227207619Srdivacky  // Create the implicit 'this' parameter declaration.
228276479Sdim  CGM.getCXXABI().buildThisParam(*this, FunctionArgs);
229207619Srdivacky
230207619Srdivacky  // Add the rest of the parameters.
231280031Sdim  FunctionArgs.append(MD->param_begin(), MD->param_end());
232243830Sdim
233276479Sdim  if (isa<CXXDestructorDecl>(MD))
234276479Sdim    CGM.getCXXABI().addImplicitStructorParams(*this, ResultType, FunctionArgs);
235276479Sdim
236261991Sdim  // Start defining the function.
237221345Sdim  StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs,
238280031Sdim                MD->getLocation(), MD->getLocation());
239207619Srdivacky
240261991Sdim  // Since we didn't pass a GlobalDecl to StartFunction, do this ourselves.
241212904Sdim  CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
242234353Sdim  CXXThisValue = CXXABIThisValue;
243296417Sdim  CurCodeDecl = MD;
244296417Sdim  CurFuncDecl = MD;
245261991Sdim}
246212904Sdim
247296417Sdimvoid CodeGenFunction::FinishThunk() {
248296417Sdim  // Clear these to restore the invariants expected by
249296417Sdim  // StartFunction/FinishFunction.
250296417Sdim  CurCodeDecl = nullptr;
251296417Sdim  CurFuncDecl = nullptr;
252296417Sdim
253296417Sdim  FinishFunction();
254296417Sdim}
255296417Sdim
256280031Sdimvoid CodeGenFunction::EmitCallAndReturnForThunk(llvm::Value *Callee,
257261991Sdim                                                const ThunkInfo *Thunk) {
258261991Sdim  assert(isa<CXXMethodDecl>(CurGD.getDecl()) &&
259261991Sdim         "Please use a new CGF for this thunk");
260280031Sdim  const CXXMethodDecl *MD = cast<CXXMethodDecl>(CurGD.getDecl());
261261991Sdim
262261991Sdim  // Adjust the 'this' pointer if necessary
263296417Sdim  llvm::Value *AdjustedThisPtr =
264296417Sdim    Thunk ? CGM.getCXXABI().performThisAdjustment(
265296417Sdim                          *this, LoadCXXThisAddress(), Thunk->This)
266296417Sdim          : LoadCXXThis();
267261991Sdim
268280031Sdim  if (CurFnInfo->usesInAlloca()) {
269280031Sdim    // We don't handle return adjusting thunks, because they require us to call
270280031Sdim    // the copy constructor.  For now, fall through and pretend the return
271280031Sdim    // adjustment was empty so we don't crash.
272280031Sdim    if (Thunk && !Thunk->Return.isEmpty()) {
273280031Sdim      CGM.ErrorUnsupported(
274280031Sdim          MD, "non-trivial argument copy for return-adjusting thunk");
275280031Sdim    }
276280031Sdim    EmitMustTailThunk(MD, AdjustedThisPtr, Callee);
277280031Sdim    return;
278280031Sdim  }
279280031Sdim
280261991Sdim  // Start building CallArgs.
281207619Srdivacky  CallArgList CallArgs;
282261991Sdim  QualType ThisType = MD->getThisType(getContext());
283221345Sdim  CallArgs.add(RValue::get(AdjustedThisPtr), ThisType);
284207619Srdivacky
285261991Sdim  if (isa<CXXDestructorDecl>(MD))
286280031Sdim    CGM.getCXXABI().adjustCallArgsForDestructorThunk(*this, CurGD, CallArgs);
287261991Sdim
288261991Sdim  // Add the rest of the arguments.
289280031Sdim  for (const ParmVarDecl *PD : MD->params())
290280031Sdim    EmitDelegateCallArg(CallArgs, PD, PD->getLocStart());
291207619Srdivacky
292261991Sdim  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
293207619Srdivacky
294221345Sdim#ifndef NDEBUG
295239462Sdim  const CGFunctionInfo &CallFnInfo =
296239462Sdim    CGM.getTypes().arrangeCXXMethodCall(CallArgs, FPT,
297234353Sdim                                       RequiredArgs::forPrototypePlus(FPT, 1));
298261991Sdim  assert(CallFnInfo.getRegParm() == CurFnInfo->getRegParm() &&
299261991Sdim         CallFnInfo.isNoReturn() == CurFnInfo->isNoReturn() &&
300261991Sdim         CallFnInfo.getCallingConvention() == CurFnInfo->getCallingConvention());
301239462Sdim  assert(isa<CXXDestructorDecl>(MD) || // ignore dtor return types
302239462Sdim         similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(),
303261991Sdim                 CurFnInfo->getReturnInfo(), CurFnInfo->getReturnType()));
304261991Sdim  assert(CallFnInfo.arg_size() == CurFnInfo->arg_size());
305261991Sdim  for (unsigned i = 0, e = CurFnInfo->arg_size(); i != e; ++i)
306221345Sdim    assert(similar(CallFnInfo.arg_begin()[i].info,
307221345Sdim                   CallFnInfo.arg_begin()[i].type,
308261991Sdim                   CurFnInfo->arg_begin()[i].info,
309261991Sdim                   CurFnInfo->arg_begin()[i].type));
310221345Sdim#endif
311261991Sdim
312208600Srdivacky  // Determine whether we have a return value slot to use.
313280031Sdim  QualType ResultType = CGM.getCXXABI().HasThisReturn(CurGD)
314280031Sdim                            ? ThisType
315280031Sdim                            : CGM.getCXXABI().hasMostDerivedReturn(CurGD)
316280031Sdim                                  ? CGM.getContext().VoidPtrTy
317280031Sdim                                  : FPT->getReturnType();
318208600Srdivacky  ReturnValueSlot Slot;
319208600Srdivacky  if (!ResultType->isVoidType() &&
320261991Sdim      CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
321249423Sdim      !hasScalarEvaluationKind(CurFnInfo->getReturnType()))
322208600Srdivacky    Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified());
323208600Srdivacky
324207619Srdivacky  // Now emit our call.
325280031Sdim  llvm::Instruction *CallOrInvoke;
326280031Sdim  RValue RV = EmitCall(*CurFnInfo, Callee, Slot, CallArgs, MD, &CallOrInvoke);
327280031Sdim
328261991Sdim  // Consider return adjustment if we have ThunkInfo.
329261991Sdim  if (Thunk && !Thunk->Return.isEmpty())
330261991Sdim    RV = PerformReturnAdjustment(*this, ResultType, RV, *Thunk);
331296417Sdim  else if (llvm::CallInst* Call = dyn_cast<llvm::CallInst>(CallOrInvoke))
332296417Sdim    Call->setTailCallKind(llvm::CallInst::TCK_Tail);
333207619Srdivacky
334261991Sdim  // Emit return.
335208600Srdivacky  if (!ResultType->isVoidType() && Slot.isNull())
336218893Sdim    CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType);
337207619Srdivacky
338239462Sdim  // Disable the final ARC autorelease.
339239462Sdim  AutoreleaseResult = false;
340239462Sdim
341296417Sdim  FinishThunk();
342261991Sdim}
343207619Srdivacky
344280031Sdimvoid CodeGenFunction::EmitMustTailThunk(const CXXMethodDecl *MD,
345280031Sdim                                        llvm::Value *AdjustedThisPtr,
346280031Sdim                                        llvm::Value *Callee) {
347280031Sdim  // Emitting a musttail call thunk doesn't use any of the CGCall.cpp machinery
348280031Sdim  // to translate AST arguments into LLVM IR arguments.  For thunks, we know
349280031Sdim  // that the caller prototype more or less matches the callee prototype with
350280031Sdim  // the exception of 'this'.
351280031Sdim  SmallVector<llvm::Value *, 8> Args;
352280031Sdim  for (llvm::Argument &A : CurFn->args())
353280031Sdim    Args.push_back(&A);
354280031Sdim
355280031Sdim  // Set the adjusted 'this' pointer.
356280031Sdim  const ABIArgInfo &ThisAI = CurFnInfo->arg_begin()->info;
357280031Sdim  if (ThisAI.isDirect()) {
358280031Sdim    const ABIArgInfo &RetAI = CurFnInfo->getReturnInfo();
359280031Sdim    int ThisArgNo = RetAI.isIndirect() && !RetAI.isSRetAfterThis() ? 1 : 0;
360280031Sdim    llvm::Type *ThisType = Args[ThisArgNo]->getType();
361280031Sdim    if (ThisType != AdjustedThisPtr->getType())
362280031Sdim      AdjustedThisPtr = Builder.CreateBitCast(AdjustedThisPtr, ThisType);
363280031Sdim    Args[ThisArgNo] = AdjustedThisPtr;
364280031Sdim  } else {
365280031Sdim    assert(ThisAI.isInAlloca() && "this is passed directly or inalloca");
366296417Sdim    Address ThisAddr = GetAddrOfLocalVar(CXXABIThisDecl);
367296417Sdim    llvm::Type *ThisType = ThisAddr.getElementType();
368280031Sdim    if (ThisType != AdjustedThisPtr->getType())
369280031Sdim      AdjustedThisPtr = Builder.CreateBitCast(AdjustedThisPtr, ThisType);
370280031Sdim    Builder.CreateStore(AdjustedThisPtr, ThisAddr);
371280031Sdim  }
372280031Sdim
373280031Sdim  // Emit the musttail call manually.  Even if the prologue pushed cleanups, we
374280031Sdim  // don't actually want to run them.
375280031Sdim  llvm::CallInst *Call = Builder.CreateCall(Callee, Args);
376280031Sdim  Call->setTailCallKind(llvm::CallInst::TCK_MustTail);
377280031Sdim
378280031Sdim  // Apply the standard set of call attributes.
379280031Sdim  unsigned CallingConv;
380280031Sdim  CodeGen::AttributeListType AttributeList;
381296417Sdim  CGM.ConstructAttributeList(Callee->getName(), *CurFnInfo, MD, AttributeList,
382296417Sdim                             CallingConv, /*AttrOnCallSite=*/true);
383280031Sdim  llvm::AttributeSet Attrs =
384280031Sdim      llvm::AttributeSet::get(getLLVMContext(), AttributeList);
385280031Sdim  Call->setAttributes(Attrs);
386280031Sdim  Call->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
387280031Sdim
388280031Sdim  if (Call->getType()->isVoidTy())
389280031Sdim    Builder.CreateRetVoid();
390280031Sdim  else
391280031Sdim    Builder.CreateRet(Call);
392280031Sdim
393280031Sdim  // Finish the function to maintain CodeGenFunction invariants.
394280031Sdim  // FIXME: Don't emit unreachable code.
395280031Sdim  EmitBlock(createBasicBlock());
396280031Sdim  FinishFunction();
397280031Sdim}
398280031Sdim
399288943Sdimvoid CodeGenFunction::generateThunk(llvm::Function *Fn,
400261991Sdim                                    const CGFunctionInfo &FnInfo,
401261991Sdim                                    GlobalDecl GD, const ThunkInfo &Thunk) {
402261991Sdim  StartThunk(Fn, GD, FnInfo);
403261991Sdim
404261991Sdim  // Get our callee.
405261991Sdim  llvm::Type *Ty =
406261991Sdim    CGM.getTypes().GetFunctionType(CGM.getTypes().arrangeGlobalDeclaration(GD));
407261991Sdim  llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
408261991Sdim
409261991Sdim  // Make the call and return the result.
410280031Sdim  EmitCallAndReturnForThunk(Callee, &Thunk);
411207619Srdivacky}
412207619Srdivacky
413261991Sdimvoid CodeGenVTables::emitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
414261991Sdim                               bool ForVTable) {
415234353Sdim  const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeGlobalDeclaration(GD);
416221345Sdim
417221345Sdim  // FIXME: re-use FnInfo in this computation.
418276479Sdim  llvm::Constant *C = CGM.GetAddrOfThunk(GD, Thunk);
419276479Sdim  llvm::GlobalValue *Entry;
420276479Sdim
421207619Srdivacky  // Strip off a bitcast if we got one back.
422276479Sdim  if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(C)) {
423207619Srdivacky    assert(CE->getOpcode() == llvm::Instruction::BitCast);
424276479Sdim    Entry = cast<llvm::GlobalValue>(CE->getOperand(0));
425276479Sdim  } else {
426276479Sdim    Entry = cast<llvm::GlobalValue>(C);
427207619Srdivacky  }
428276479Sdim
429207619Srdivacky  // There's already a declaration with the same name, check if it has the same
430207619Srdivacky  // type or if we need to replace it.
431276479Sdim  if (Entry->getType()->getElementType() !=
432212904Sdim      CGM.getTypes().GetFunctionTypeForVTable(GD)) {
433276479Sdim    llvm::GlobalValue *OldThunkFn = Entry;
434276479Sdim
435207619Srdivacky    // If the types mismatch then we have to rewrite the definition.
436207619Srdivacky    assert(OldThunkFn->isDeclaration() &&
437207619Srdivacky           "Shouldn't replace non-declaration");
438207619Srdivacky
439207619Srdivacky    // Remove the name from the old thunk function and get a new thunk.
440226633Sdim    OldThunkFn->setName(StringRef());
441276479Sdim    Entry = cast<llvm::GlobalValue>(CGM.GetAddrOfThunk(GD, Thunk));
442207619Srdivacky
443207619Srdivacky    // If needed, replace the old thunk with a bitcast.
444207619Srdivacky    if (!OldThunkFn->use_empty()) {
445207619Srdivacky      llvm::Constant *NewPtrForOldDecl =
446207619Srdivacky        llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType());
447207619Srdivacky      OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
448207619Srdivacky    }
449207619Srdivacky
450207619Srdivacky    // Remove the old thunk.
451207619Srdivacky    OldThunkFn->eraseFromParent();
452207619Srdivacky  }
453207619Srdivacky
454218893Sdim  llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
455261991Sdim  bool ABIHasKeyFunctions = CGM.getTarget().getCXXABI().hasKeyFunctions();
456261991Sdim  bool UseAvailableExternallyLinkage = ForVTable && ABIHasKeyFunctions;
457218893Sdim
458218893Sdim  if (!ThunkFn->isDeclaration()) {
459261991Sdim    if (!ABIHasKeyFunctions || UseAvailableExternallyLinkage) {
460218893Sdim      // There is already a thunk emitted for this function, do nothing.
461218893Sdim      return;
462218893Sdim    }
463218893Sdim
464288943Sdim    setThunkProperties(CGM, Thunk, ThunkFn, ForVTable, GD);
465218893Sdim    return;
466218893Sdim  }
467218893Sdim
468243830Sdim  CGM.SetLLVMFunctionAttributesForDefinition(GD.getDecl(), ThunkFn);
469243830Sdim
470223017Sdim  if (ThunkFn->isVarArg()) {
471223017Sdim    // Varargs thunks are special; we can't just generate a call because
472223017Sdim    // we can't copy the varargs.  Our implementation is rather
473223017Sdim    // expensive/sucky at the moment, so don't generate the thunk unless
474223017Sdim    // we have to.
475223017Sdim    // FIXME: Do something better here; GenerateVarArgsThunk is extremely ugly.
476288943Sdim    if (UseAvailableExternallyLinkage)
477288943Sdim      return;
478288943Sdim    ThunkFn =
479288943Sdim        CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk);
480223017Sdim  } else {
481223017Sdim    // Normal thunk body generation.
482288943Sdim    CodeGenFunction(CGM).generateThunk(ThunkFn, FnInfo, GD, Thunk);
483223017Sdim  }
484288943Sdim
485288943Sdim  setThunkProperties(CGM, Thunk, ThunkFn, ForVTable, GD);
486207619Srdivacky}
487207619Srdivacky
488261991Sdimvoid CodeGenVTables::maybeEmitThunkForVTable(GlobalDecl GD,
489261991Sdim                                             const ThunkInfo &Thunk) {
490261991Sdim  // If the ABI has key functions, only the TU with the key function should emit
491261991Sdim  // the thunk. However, we can allow inlining of thunks if we emit them with
492261991Sdim  // available_externally linkage together with vtables when optimizations are
493261991Sdim  // enabled.
494261991Sdim  if (CGM.getTarget().getCXXABI().hasKeyFunctions() &&
495261991Sdim      !CGM.getCodeGenOpts().OptimizationLevel)
496218893Sdim    return;
497218893Sdim
498218893Sdim  // We can't emit thunks for member functions with incomplete types.
499218893Sdim  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
500224145Sdim  if (!CGM.getTypes().isFuncTypeConvertible(
501261991Sdim           MD->getType()->castAs<FunctionType>()))
502218893Sdim    return;
503218893Sdim
504261991Sdim  emitThunk(GD, Thunk, /*ForVTable=*/true);
505218893Sdim}
506218893Sdim
507207619Srdivackyvoid CodeGenVTables::EmitThunks(GlobalDecl GD)
508207619Srdivacky{
509207619Srdivacky  const CXXMethodDecl *MD =
510207619Srdivacky    cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
511207619Srdivacky
512207619Srdivacky  // We don't need to generate thunks for the base destructor.
513207619Srdivacky  if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
514207619Srdivacky    return;
515207619Srdivacky
516276479Sdim  const VTableContextBase::ThunkInfoVectorTy *ThunkInfoVector =
517276479Sdim      VTContext->getThunkInfo(GD);
518261991Sdim
519226633Sdim  if (!ThunkInfoVector)
520207619Srdivacky    return;
521207619Srdivacky
522296417Sdim  for (const ThunkInfo& Thunk : *ThunkInfoVector)
523296417Sdim    emitThunk(GD, Thunk, /*ForVTable=*/false);
524207619Srdivacky}
525207619Srdivacky
526276479Sdimllvm::Constant *CodeGenVTables::CreateVTableInitializer(
527276479Sdim    const CXXRecordDecl *RD, const VTableComponent *Components,
528276479Sdim    unsigned NumComponents, const VTableLayout::VTableThunkTy *VTableThunks,
529276479Sdim    unsigned NumVTableThunks, llvm::Constant *RTTI) {
530226633Sdim  SmallVector<llvm::Constant *, 64> Inits;
531207619Srdivacky
532234353Sdim  llvm::Type *Int8PtrTy = CGM.Int8PtrTy;
533207619Srdivacky
534226633Sdim  llvm::Type *PtrDiffTy =
535207619Srdivacky    CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
536207619Srdivacky
537207619Srdivacky  unsigned NextVTableThunkIndex = 0;
538207619Srdivacky
539276479Sdim  llvm::Constant *PureVirtualFn = nullptr, *DeletedVirtualFn = nullptr;
540276479Sdim
541207619Srdivacky  for (unsigned I = 0; I != NumComponents; ++I) {
542226633Sdim    VTableComponent Component = Components[I];
543207619Srdivacky
544276479Sdim    llvm::Constant *Init = nullptr;
545207619Srdivacky
546207619Srdivacky    switch (Component.getKind()) {
547207619Srdivacky    case VTableComponent::CK_VCallOffset:
548221345Sdim      Init = llvm::ConstantInt::get(PtrDiffTy,
549221345Sdim                                    Component.getVCallOffset().getQuantity());
550207619Srdivacky      Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
551207619Srdivacky      break;
552207619Srdivacky    case VTableComponent::CK_VBaseOffset:
553221345Sdim      Init = llvm::ConstantInt::get(PtrDiffTy,
554221345Sdim                                    Component.getVBaseOffset().getQuantity());
555207619Srdivacky      Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
556207619Srdivacky      break;
557207619Srdivacky    case VTableComponent::CK_OffsetToTop:
558221345Sdim      Init = llvm::ConstantInt::get(PtrDiffTy,
559221345Sdim                                    Component.getOffsetToTop().getQuantity());
560207619Srdivacky      Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
561207619Srdivacky      break;
562207619Srdivacky    case VTableComponent::CK_RTTI:
563207619Srdivacky      Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
564207619Srdivacky      break;
565207619Srdivacky    case VTableComponent::CK_FunctionPointer:
566207619Srdivacky    case VTableComponent::CK_CompleteDtorPointer:
567207619Srdivacky    case VTableComponent::CK_DeletingDtorPointer: {
568207619Srdivacky      GlobalDecl GD;
569207619Srdivacky
570207619Srdivacky      // Get the right global decl.
571207619Srdivacky      switch (Component.getKind()) {
572207619Srdivacky      default:
573207619Srdivacky        llvm_unreachable("Unexpected vtable component kind");
574207619Srdivacky      case VTableComponent::CK_FunctionPointer:
575207619Srdivacky        GD = Component.getFunctionDecl();
576207619Srdivacky        break;
577207619Srdivacky      case VTableComponent::CK_CompleteDtorPointer:
578207619Srdivacky        GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
579207619Srdivacky        break;
580207619Srdivacky      case VTableComponent::CK_DeletingDtorPointer:
581207619Srdivacky        GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
582207619Srdivacky        break;
583207619Srdivacky      }
584207619Srdivacky
585296417Sdim      if (CGM.getLangOpts().CUDA) {
586296417Sdim        // Emit NULL for methods we can't codegen on this
587296417Sdim        // side. Otherwise we'd end up with vtable with unresolved
588296417Sdim        // references.
589296417Sdim        const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
590296417Sdim        // OK on device side: functions w/ __device__ attribute
591296417Sdim        // OK on host side: anything except __device__-only functions.
592296417Sdim        bool CanEmitMethod = CGM.getLangOpts().CUDAIsDevice
593296417Sdim                                 ? MD->hasAttr<CUDADeviceAttr>()
594296417Sdim                                 : (MD->hasAttr<CUDAHostAttr>() ||
595296417Sdim                                    !MD->hasAttr<CUDADeviceAttr>());
596296417Sdim        if (!CanEmitMethod) {
597296417Sdim          Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
598296417Sdim          break;
599296417Sdim        }
600296417Sdim        // Method is acceptable, continue processing as usual.
601296417Sdim      }
602296417Sdim
603207619Srdivacky      if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
604207619Srdivacky        // We have a pure virtual member function.
605207619Srdivacky        if (!PureVirtualFn) {
606243830Sdim          llvm::FunctionType *Ty =
607243830Sdim            llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
608243830Sdim          StringRef PureCallName = CGM.getCXXABI().GetPureVirtualCallName();
609243830Sdim          PureVirtualFn = CGM.CreateRuntimeFunction(Ty, PureCallName);
610243830Sdim          PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn,
611239462Sdim                                                         CGM.Int8PtrTy);
612207619Srdivacky        }
613207619Srdivacky        Init = PureVirtualFn;
614243830Sdim      } else if (cast<CXXMethodDecl>(GD.getDecl())->isDeleted()) {
615243830Sdim        if (!DeletedVirtualFn) {
616243830Sdim          llvm::FunctionType *Ty =
617243830Sdim            llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
618243830Sdim          StringRef DeletedCallName =
619243830Sdim            CGM.getCXXABI().GetDeletedVirtualCallName();
620243830Sdim          DeletedVirtualFn = CGM.CreateRuntimeFunction(Ty, DeletedCallName);
621243830Sdim          DeletedVirtualFn = llvm::ConstantExpr::getBitCast(DeletedVirtualFn,
622243830Sdim                                                         CGM.Int8PtrTy);
623243830Sdim        }
624243830Sdim        Init = DeletedVirtualFn;
625207619Srdivacky      } else {
626207619Srdivacky        // Check if we should use a thunk.
627226633Sdim        if (NextVTableThunkIndex < NumVTableThunks &&
628207619Srdivacky            VTableThunks[NextVTableThunkIndex].first == I) {
629207619Srdivacky          const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
630207619Srdivacky
631261991Sdim          maybeEmitThunkForVTable(GD, Thunk);
632207619Srdivacky          Init = CGM.GetAddrOfThunk(GD, Thunk);
633218893Sdim
634207619Srdivacky          NextVTableThunkIndex++;
635207619Srdivacky        } else {
636226633Sdim          llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD);
637207619Srdivacky
638218893Sdim          Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
639207619Srdivacky        }
640207619Srdivacky
641207619Srdivacky        Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
642207619Srdivacky      }
643207619Srdivacky      break;
644207619Srdivacky    }
645207619Srdivacky
646207619Srdivacky    case VTableComponent::CK_UnusedFunctionPointer:
647207619Srdivacky      Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
648207619Srdivacky      break;
649207619Srdivacky    };
650207619Srdivacky
651207619Srdivacky    Inits.push_back(Init);
652207619Srdivacky  }
653207619Srdivacky
654207619Srdivacky  llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
655224145Sdim  return llvm::ConstantArray::get(ArrayType, Inits);
656207619Srdivacky}
657207619Srdivacky
658207619Srdivackyllvm::GlobalVariable *
659207619SrdivackyCodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD,
660207619Srdivacky                                      const BaseSubobject &Base,
661207619Srdivacky                                      bool BaseIsVirtual,
662221345Sdim                                   llvm::GlobalVariable::LinkageTypes Linkage,
663207619Srdivacky                                      VTableAddressPointsMapTy& AddressPoints) {
664261991Sdim  if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
665261991Sdim    DI->completeClassData(Base.getBase());
666261991Sdim
667276479Sdim  std::unique_ptr<VTableLayout> VTLayout(
668276479Sdim      getItaniumVTableContext().createConstructionVTableLayout(
669261991Sdim          Base.getBase(), Base.getBaseOffset(), BaseIsVirtual, RD));
670207619Srdivacky
671207619Srdivacky  // Add the address points.
672226633Sdim  AddressPoints = VTLayout->getAddressPoints();
673207619Srdivacky
674207619Srdivacky  // Get the mangled construction vtable name.
675234353Sdim  SmallString<256> OutName;
676218893Sdim  llvm::raw_svector_ostream Out(OutName);
677261991Sdim  cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
678261991Sdim      .mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(),
679261991Sdim                           Base.getBase(), Out);
680226633Sdim  StringRef Name = OutName.str();
681207619Srdivacky
682207619Srdivacky  llvm::ArrayType *ArrayType =
683234353Sdim    llvm::ArrayType::get(CGM.Int8PtrTy, VTLayout->getNumVTableComponents());
684207619Srdivacky
685249423Sdim  // Construction vtable symbols are not part of the Itanium ABI, so we cannot
686249423Sdim  // guarantee that they actually will be available externally. Instead, when
687249423Sdim  // emitting an available_externally VTT, we provide references to an internal
688249423Sdim  // linkage construction vtable. The ABI only requires complete-object vtables
689249423Sdim  // to be the same for all instances of a type, not construction vtables.
690249423Sdim  if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
691249423Sdim    Linkage = llvm::GlobalVariable::InternalLinkage;
692249423Sdim
693207619Srdivacky  // Create the variable that will hold the construction vtable.
694207619Srdivacky  llvm::GlobalVariable *VTable =
695221345Sdim    CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, Linkage);
696276479Sdim  CGM.setGlobalVisibility(VTable, RD);
697207619Srdivacky
698221345Sdim  // V-tables are always unnamed_addr.
699221345Sdim  VTable->setUnnamedAddr(true);
700221345Sdim
701276479Sdim  llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(
702276479Sdim      CGM.getContext().getTagDeclType(Base.getBase()));
703276479Sdim
704207619Srdivacky  // Create and set the initializer.
705276479Sdim  llvm::Constant *Init = CreateVTableInitializer(
706276479Sdim      Base.getBase(), VTLayout->vtable_component_begin(),
707276479Sdim      VTLayout->getNumVTableComponents(), VTLayout->vtable_thunk_begin(),
708276479Sdim      VTLayout->getNumVTableThunks(), RTTI);
709207619Srdivacky  VTable->setInitializer(Init);
710207619Srdivacky
711288943Sdim  CGM.EmitVTableBitSetEntries(VTable, *VTLayout.get());
712288943Sdim
713207619Srdivacky  return VTable;
714207619Srdivacky}
715207619Srdivacky
716296417Sdimstatic bool shouldEmitAvailableExternallyVTable(const CodeGenModule &CGM,
717296417Sdim                                                const CXXRecordDecl *RD) {
718296417Sdim  return CGM.getCodeGenOpts().OptimizationLevel > 0 &&
719296417Sdim         CGM.getCXXABI().canSpeculativelyEmitVTable(RD);
720296417Sdim}
721296417Sdim
722249423Sdim/// Compute the required linkage of the v-table for the given class.
723249423Sdim///
724249423Sdim/// Note that we only call this at the end of the translation unit.
725249423Sdimllvm::GlobalVariable::LinkageTypes
726249423SdimCodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
727261991Sdim  if (!RD->isExternallyVisible())
728249423Sdim    return llvm::GlobalVariable::InternalLinkage;
729249423Sdim
730249423Sdim  // We're at the end of the translation unit, so the current key
731249423Sdim  // function is fully correct.
732280031Sdim  const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction(RD);
733280031Sdim  if (keyFunction && !RD->hasAttr<DLLImportAttr>()) {
734249423Sdim    // If this class has a key function, use that to determine the
735249423Sdim    // linkage of the vtable.
736276479Sdim    const FunctionDecl *def = nullptr;
737249423Sdim    if (keyFunction->hasBody(def))
738249423Sdim      keyFunction = cast<CXXMethodDecl>(def);
739249423Sdim
740249423Sdim    switch (keyFunction->getTemplateSpecializationKind()) {
741249423Sdim      case TSK_Undeclared:
742249423Sdim      case TSK_ExplicitSpecialization:
743296417Sdim        assert((def || CodeGenOpts.OptimizationLevel > 0) &&
744296417Sdim               "Shouldn't query vtable linkage without key function or "
745296417Sdim               "optimizations");
746296417Sdim        if (!def && CodeGenOpts.OptimizationLevel > 0)
747296417Sdim          return llvm::GlobalVariable::AvailableExternallyLinkage;
748296417Sdim
749249423Sdim        if (keyFunction->isInlined())
750249423Sdim          return !Context.getLangOpts().AppleKext ?
751249423Sdim                   llvm::GlobalVariable::LinkOnceODRLinkage :
752249423Sdim                   llvm::Function::InternalLinkage;
753249423Sdim
754249423Sdim        return llvm::GlobalVariable::ExternalLinkage;
755288943Sdim
756249423Sdim      case TSK_ImplicitInstantiation:
757249423Sdim        return !Context.getLangOpts().AppleKext ?
758249423Sdim                 llvm::GlobalVariable::LinkOnceODRLinkage :
759249423Sdim                 llvm::Function::InternalLinkage;
760249423Sdim
761249423Sdim      case TSK_ExplicitInstantiationDefinition:
762249423Sdim        return !Context.getLangOpts().AppleKext ?
763249423Sdim                 llvm::GlobalVariable::WeakODRLinkage :
764249423Sdim                 llvm::Function::InternalLinkage;
765249423Sdim
766249423Sdim      case TSK_ExplicitInstantiationDeclaration:
767261991Sdim        llvm_unreachable("Should not have been asked to emit this");
768249423Sdim    }
769249423Sdim  }
770249423Sdim
771249423Sdim  // -fapple-kext mode does not support weak linkage, so we must use
772249423Sdim  // internal linkage.
773249423Sdim  if (Context.getLangOpts().AppleKext)
774249423Sdim    return llvm::Function::InternalLinkage;
775276479Sdim
776276479Sdim  llvm::GlobalVariable::LinkageTypes DiscardableODRLinkage =
777276479Sdim      llvm::GlobalValue::LinkOnceODRLinkage;
778276479Sdim  llvm::GlobalVariable::LinkageTypes NonDiscardableODRLinkage =
779276479Sdim      llvm::GlobalValue::WeakODRLinkage;
780276479Sdim  if (RD->hasAttr<DLLExportAttr>()) {
781276479Sdim    // Cannot discard exported vtables.
782276479Sdim    DiscardableODRLinkage = NonDiscardableODRLinkage;
783276479Sdim  } else if (RD->hasAttr<DLLImportAttr>()) {
784276479Sdim    // Imported vtables are available externally.
785276479Sdim    DiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage;
786276479Sdim    NonDiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage;
787276479Sdim  }
788276479Sdim
789249423Sdim  switch (RD->getTemplateSpecializationKind()) {
790296417Sdim    case TSK_Undeclared:
791296417Sdim    case TSK_ExplicitSpecialization:
792296417Sdim    case TSK_ImplicitInstantiation:
793296417Sdim      return DiscardableODRLinkage;
794249423Sdim
795296417Sdim    case TSK_ExplicitInstantiationDeclaration:
796296417Sdim      return shouldEmitAvailableExternallyVTable(*this, RD)
797296417Sdim                 ? llvm::GlobalVariable::AvailableExternallyLinkage
798296417Sdim                 : llvm::GlobalVariable::ExternalLinkage;
799249423Sdim
800296417Sdim    case TSK_ExplicitInstantiationDefinition:
801296417Sdim      return NonDiscardableODRLinkage;
802249423Sdim  }
803249423Sdim
804249423Sdim  llvm_unreachable("Invalid TemplateSpecializationKind!");
805249423Sdim}
806249423Sdim
807288943Sdim/// This is a callback from Sema to tell us that that a particular v-table is
808288943Sdim/// required to be emitted in this translation unit.
809249423Sdim///
810288943Sdim/// This is only called for vtables that _must_ be emitted (mainly due to key
811288943Sdim/// functions).  For weak vtables, CodeGen tracks when they are needed and
812288943Sdim/// emits them as-needed.
813288943Sdimvoid CodeGenModule::EmitVTable(CXXRecordDecl *theClass) {
814249423Sdim  VTables.GenerateClassData(theClass);
815249423Sdim}
816249423Sdim
817207619Srdivackyvoid
818249423SdimCodeGenVTables::GenerateClassData(const CXXRecordDecl *RD) {
819261991Sdim  if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
820261991Sdim    DI->completeClassData(RD);
821207619Srdivacky
822261991Sdim  if (RD->getNumVBases())
823261991Sdim    CGM.getCXXABI().emitVirtualInheritanceTables(RD);
824207619Srdivacky
825261991Sdim  CGM.getCXXABI().emitVTableDefinitions(*this, RD);
826207619Srdivacky}
827249423Sdim
828249423Sdim/// At this point in the translation unit, does it appear that can we
829249423Sdim/// rely on the vtable being defined elsewhere in the program?
830249423Sdim///
831249423Sdim/// The response is really only definitive when called at the end of
832249423Sdim/// the translation unit.
833249423Sdim///
834249423Sdim/// The only semantic restriction here is that the object file should
835249423Sdim/// not contain a v-table definition when that v-table is defined
836249423Sdim/// strongly elsewhere.  Otherwise, we'd just like to avoid emitting
837249423Sdim/// v-tables when unnecessary.
838249423Sdimbool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
839276479Sdim  assert(RD->isDynamicClass() && "Non-dynamic classes have no VTable.");
840249423Sdim
841249423Sdim  // If we have an explicit instantiation declaration (and not a
842249423Sdim  // definition), the v-table is defined elsewhere.
843249423Sdim  TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
844249423Sdim  if (TSK == TSK_ExplicitInstantiationDeclaration)
845249423Sdim    return true;
846249423Sdim
847249423Sdim  // Otherwise, if the class is an instantiated template, the
848249423Sdim  // v-table must be defined here.
849249423Sdim  if (TSK == TSK_ImplicitInstantiation ||
850249423Sdim      TSK == TSK_ExplicitInstantiationDefinition)
851249423Sdim    return false;
852249423Sdim
853249423Sdim  // Otherwise, if the class doesn't have a key function (possibly
854249423Sdim  // anymore), the v-table must be defined here.
855249423Sdim  const CXXMethodDecl *keyFunction = CGM.getContext().getCurrentKeyFunction(RD);
856249423Sdim  if (!keyFunction)
857249423Sdim    return false;
858249423Sdim
859249423Sdim  // Otherwise, if we don't have a definition of the key function, the
860249423Sdim  // v-table must be defined somewhere else.
861249423Sdim  return !keyFunction->hasBody();
862249423Sdim}
863249423Sdim
864249423Sdim/// Given that we're currently at the end of the translation unit, and
865249423Sdim/// we've emitted a reference to the v-table for this class, should
866249423Sdim/// we define that v-table?
867249423Sdimstatic bool shouldEmitVTableAtEndOfTranslationUnit(CodeGenModule &CGM,
868249423Sdim                                                   const CXXRecordDecl *RD) {
869296417Sdim  // If vtable is internal then it has to be done.
870296417Sdim  if (!CGM.getVTables().isVTableExternal(RD))
871296417Sdim    return true;
872296417Sdim
873296417Sdim  // If it's external then maybe we will need it as available_externally.
874296417Sdim  return shouldEmitAvailableExternallyVTable(CGM, RD);
875249423Sdim}
876249423Sdim
877249423Sdim/// Given that at some point we emitted a reference to one or more
878249423Sdim/// v-tables, and that we are now at the end of the translation unit,
879249423Sdim/// decide whether we should emit them.
880249423Sdimvoid CodeGenModule::EmitDeferredVTables() {
881249423Sdim#ifndef NDEBUG
882249423Sdim  // Remember the size of DeferredVTables, because we're going to assume
883249423Sdim  // that this entire operation doesn't modify it.
884249423Sdim  size_t savedSize = DeferredVTables.size();
885249423Sdim#endif
886249423Sdim
887296417Sdim  for (const CXXRecordDecl *RD : DeferredVTables)
888249423Sdim    if (shouldEmitVTableAtEndOfTranslationUnit(*this, RD))
889249423Sdim      VTables.GenerateClassData(RD);
890249423Sdim
891249423Sdim  assert(savedSize == DeferredVTables.size() &&
892249423Sdim         "deferred extra v-tables during v-table emission?");
893249423Sdim  DeferredVTables.clear();
894249423Sdim}
895288943Sdim
896288943Sdimbool CodeGenModule::IsCFIBlacklistedRecord(const CXXRecordDecl *RD) {
897296417Sdim  if (RD->hasAttr<UuidAttr>() &&
898296417Sdim      getContext().getSanitizerBlacklist().isBlacklistedType("attr:uuid"))
899296417Sdim    return true;
900296417Sdim
901296417Sdim  return getContext().getSanitizerBlacklist().isBlacklistedType(
902296417Sdim      RD->getQualifiedNameAsString());
903288943Sdim}
904288943Sdim
905288943Sdimvoid CodeGenModule::EmitVTableBitSetEntries(llvm::GlobalVariable *VTable,
906288943Sdim                                            const VTableLayout &VTLayout) {
907288943Sdim  if (!LangOpts.Sanitize.has(SanitizerKind::CFIVCall) &&
908288943Sdim      !LangOpts.Sanitize.has(SanitizerKind::CFINVCall) &&
909288943Sdim      !LangOpts.Sanitize.has(SanitizerKind::CFIDerivedCast) &&
910288943Sdim      !LangOpts.Sanitize.has(SanitizerKind::CFIUnrelatedCast))
911288943Sdim    return;
912288943Sdim
913288943Sdim  CharUnits PointerWidth =
914288943Sdim      Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
915288943Sdim
916296417Sdim  typedef std::pair<const CXXRecordDecl *, unsigned> BSEntry;
917296417Sdim  std::vector<BSEntry> BitsetEntries;
918288943Sdim  // Create a bit set entry for each address point.
919288943Sdim  for (auto &&AP : VTLayout.getAddressPoints()) {
920288943Sdim    if (IsCFIBlacklistedRecord(AP.first.getBase()))
921288943Sdim      continue;
922288943Sdim
923296417Sdim    BitsetEntries.push_back(std::make_pair(AP.first.getBase(), AP.second));
924288943Sdim  }
925288943Sdim
926288943Sdim  // Sort the bit set entries for determinism.
927296417Sdim  std::sort(BitsetEntries.begin(), BitsetEntries.end(),
928296417Sdim            [this](const BSEntry &E1, const BSEntry &E2) {
929296417Sdim    if (&E1 == &E2)
930288943Sdim      return false;
931288943Sdim
932296417Sdim    std::string S1;
933296417Sdim    llvm::raw_string_ostream O1(S1);
934296417Sdim    getCXXABI().getMangleContext().mangleTypeName(
935296417Sdim        QualType(E1.first->getTypeForDecl(), 0), O1);
936296417Sdim    O1.flush();
937296417Sdim
938296417Sdim    std::string S2;
939296417Sdim    llvm::raw_string_ostream O2(S2);
940296417Sdim    getCXXABI().getMangleContext().mangleTypeName(
941296417Sdim        QualType(E2.first->getTypeForDecl(), 0), O2);
942296417Sdim    O2.flush();
943296417Sdim
944288943Sdim    if (S1 < S2)
945288943Sdim      return true;
946288943Sdim    if (S1 != S2)
947288943Sdim      return false;
948288943Sdim
949296417Sdim    return E1.second < E2.second;
950288943Sdim  });
951288943Sdim
952288943Sdim  llvm::NamedMDNode *BitsetsMD =
953288943Sdim      getModule().getOrInsertNamedMetadata("llvm.bitsets");
954288943Sdim  for (auto BitsetEntry : BitsetEntries)
955296417Sdim    CreateVTableBitSetEntry(BitsetsMD, VTable,
956296417Sdim                            PointerWidth * BitsetEntry.second,
957296417Sdim                            BitsetEntry.first);
958288943Sdim}
959