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