Coverage Report

Created: 2019-07-24 05:18

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