clang  3.9.0
CGVTables.cpp
Go to the documentation of this file.
1 //===--- CGVTables.cpp - Emit LLVM Code for C++ vtables -------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This contains code dealing with C++ code generation of virtual tables.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenFunction.h"
15 #include "CGCXXABI.h"
16 #include "CodeGenModule.h"
18 #include "clang/AST/RecordLayout.h"
21 #include "llvm/ADT/DenseSet.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/Format.h"
25 #include "llvm/Transforms/Utils/Cloning.h"
26 #include <algorithm>
27 #include <cstdio>
28 
29 using namespace clang;
30 using namespace CodeGen;
31 
33  : CGM(CGM), VTContext(CGM.getContext().getVTableContext()) {}
34 
36  const ThunkInfo &Thunk) {
37  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
38 
39  // Compute the mangled name.
41  llvm::raw_svector_ostream Out(Name);
42  if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
44  Thunk.This, Out);
45  else
46  getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out);
47 
49  return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true,
50  /*DontDefer=*/true, /*IsThunk=*/true);
51 }
52 
53 static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD,
54  const ThunkInfo &Thunk, llvm::Function *Fn) {
55  CGM.setGlobalVisibility(Fn, MD);
56 }
57 
58 static void setThunkProperties(CodeGenModule &CGM, const ThunkInfo &Thunk,
59  llvm::Function *ThunkFn, bool ForVTable,
60  GlobalDecl GD) {
61  CGM.setFunctionLinkage(GD, ThunkFn);
62  CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable, GD,
63  !Thunk.Return.isEmpty());
64 
65  // Set the right visibility.
66  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
67  setThunkVisibility(CGM, MD, Thunk, ThunkFn);
68 
69  if (CGM.supportsCOMDAT() && ThunkFn->isWeakForLinker())
70  ThunkFn->setComdat(CGM.getModule().getOrInsertComdat(ThunkFn->getName()));
71 }
72 
73 #ifndef NDEBUG
74 static bool similar(const ABIArgInfo &infoL, CanQualType typeL,
75  const ABIArgInfo &infoR, CanQualType typeR) {
76  return (infoL.getKind() == infoR.getKind() &&
77  (typeL == typeR ||
78  (isa<PointerType>(typeL) && isa<PointerType>(typeR)) ||
79  (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR))));
80 }
81 #endif
82 
84  QualType ResultType, RValue RV,
85  const ThunkInfo &Thunk) {
86  // Emit the return adjustment.
87  bool NullCheckValue = !ResultType->isReferenceType();
88 
89  llvm::BasicBlock *AdjustNull = nullptr;
90  llvm::BasicBlock *AdjustNotNull = nullptr;
91  llvm::BasicBlock *AdjustEnd = nullptr;
92 
93  llvm::Value *ReturnValue = RV.getScalarVal();
94 
95  if (NullCheckValue) {
96  AdjustNull = CGF.createBasicBlock("adjust.null");
97  AdjustNotNull = CGF.createBasicBlock("adjust.notnull");
98  AdjustEnd = CGF.createBasicBlock("adjust.end");
99 
100  llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue);
101  CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
102  CGF.EmitBlock(AdjustNotNull);
103  }
104 
105  auto ClassDecl = ResultType->getPointeeType()->getAsCXXRecordDecl();
106  auto ClassAlign = CGF.CGM.getClassPointerAlignment(ClassDecl);
107  ReturnValue = CGF.CGM.getCXXABI().performReturnAdjustment(CGF,
108  Address(ReturnValue, ClassAlign),
109  Thunk.Return);
110 
111  if (NullCheckValue) {
112  CGF.Builder.CreateBr(AdjustEnd);
113  CGF.EmitBlock(AdjustNull);
114  CGF.Builder.CreateBr(AdjustEnd);
115  CGF.EmitBlock(AdjustEnd);
116 
117  llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2);
118  PHI->addIncoming(ReturnValue, AdjustNotNull);
119  PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
120  AdjustNull);
121  ReturnValue = PHI;
122  }
123 
124  return RValue::get(ReturnValue);
125 }
126 
127 // This function does roughly the same thing as GenerateThunk, but in a
128 // very different way, so that va_start and va_end work correctly.
129 // FIXME: This function assumes "this" is the first non-sret LLVM argument of
130 // a function, and that there is an alloca built in the entry block
131 // for all accesses to "this".
132 // FIXME: This function assumes there is only one "ret" statement per function.
133 // FIXME: Cloning isn't correct in the presence of indirect goto!
134 // FIXME: This implementation of thunks bloats codesize by duplicating the
135 // function definition. There are alternatives:
136 // 1. Add some sort of stub support to LLVM for cases where we can
137 // do a this adjustment, then a sibcall.
138 // 2. We could transform the definition to take a va_list instead of an
139 // actual variable argument list, then have the thunks (including a
140 // no-op thunk for the regular definition) call va_start/va_end.
141 // There's a bit of per-call overhead for this solution, but it's
142 // better for codesize if the definition is long.
143 llvm::Function *
145  const CGFunctionInfo &FnInfo,
146  GlobalDecl GD, const ThunkInfo &Thunk) {
147  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
148  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
149  QualType ResultType = FPT->getReturnType();
150 
151  // Get the original function
152  assert(FnInfo.isVariadic());
153  llvm::Type *Ty = CGM.getTypes().GetFunctionType(FnInfo);
154  llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
155  llvm::Function *BaseFn = cast<llvm::Function>(Callee);
156 
157  // Clone to thunk.
158  llvm::ValueToValueMapTy VMap;
159  llvm::Function *NewFn = llvm::CloneFunction(BaseFn, VMap);
160  Fn->replaceAllUsesWith(NewFn);
161  NewFn->takeName(Fn);
162  Fn->eraseFromParent();
163  Fn = NewFn;
164 
165  // "Initialize" CGF (minimally).
166  CurFn = Fn;
167 
168  // Get the "this" value
169  llvm::Function::arg_iterator AI = Fn->arg_begin();
170  if (CGM.ReturnTypeUsesSRet(FnInfo))
171  ++AI;
172 
173  // Find the first store of "this", which will be to the alloca associated
174  // with "this".
175  Address ThisPtr(&*AI, CGM.getClassPointerAlignment(MD->getParent()));
176  llvm::BasicBlock *EntryBB = &Fn->front();
177  llvm::BasicBlock::iterator ThisStore =
178  std::find_if(EntryBB->begin(), EntryBB->end(), [&](llvm::Instruction &I) {
179  return isa<llvm::StoreInst>(I) &&
180  I.getOperand(0) == ThisPtr.getPointer();
181  });
182  assert(ThisStore != EntryBB->end() &&
183  "Store of this should be in entry block?");
184  // Adjust "this", if necessary.
185  Builder.SetInsertPoint(&*ThisStore);
186  llvm::Value *AdjustedThisPtr =
187  CGM.getCXXABI().performThisAdjustment(*this, ThisPtr, Thunk.This);
188  ThisStore->setOperand(0, AdjustedThisPtr);
189 
190  if (!Thunk.Return.isEmpty()) {
191  // Fix up the returned value, if necessary.
192  for (llvm::BasicBlock &BB : *Fn) {
193  llvm::Instruction *T = BB.getTerminator();
194  if (isa<llvm::ReturnInst>(T)) {
195  RValue RV = RValue::get(T->getOperand(0));
196  T->eraseFromParent();
197  Builder.SetInsertPoint(&BB);
198  RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
199  Builder.CreateRet(RV.getScalarVal());
200  break;
201  }
202  }
203  }
204 
205  return Fn;
206 }
207 
208 void CodeGenFunction::StartThunk(llvm::Function *Fn, GlobalDecl GD,
209  const CGFunctionInfo &FnInfo) {
210  assert(!CurGD.getDecl() && "CurGD was already set!");
211  CurGD = GD;
212  CurFuncIsThunk = true;
213 
214  // Build FunctionArgs.
215  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
216  QualType ThisType = MD->getThisType(getContext());
217  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
218  QualType ResultType = CGM.getCXXABI().HasThisReturn(GD)
219  ? ThisType
222  : FPT->getReturnType();
223  FunctionArgList FunctionArgs;
224 
225  // Create the implicit 'this' parameter declaration.
226  CGM.getCXXABI().buildThisParam(*this, FunctionArgs);
227 
228  // Add the rest of the parameters.
229  FunctionArgs.append(MD->param_begin(), MD->param_end());
230 
231  if (isa<CXXDestructorDecl>(MD))
232  CGM.getCXXABI().addImplicitStructorParams(*this, ResultType, FunctionArgs);
233 
234  // Start defining the function.
235  StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs,
236  MD->getLocation(), MD->getLocation());
237 
238  // Since we didn't pass a GlobalDecl to StartFunction, do this ourselves.
240  CXXThisValue = CXXABIThisValue;
241  CurCodeDecl = MD;
242  CurFuncDecl = MD;
243 }
244 
246  // Clear these to restore the invariants expected by
247  // StartFunction/FinishFunction.
248  CurCodeDecl = nullptr;
249  CurFuncDecl = nullptr;
250 
251  FinishFunction();
252 }
253 
255  const ThunkInfo *Thunk) {
256  assert(isa<CXXMethodDecl>(CurGD.getDecl()) &&
257  "Please use a new CGF for this thunk");
258  const CXXMethodDecl *MD = cast<CXXMethodDecl>(CurGD.getDecl());
259 
260  // Adjust the 'this' pointer if necessary
261  llvm::Value *AdjustedThisPtr =
263  *this, LoadCXXThisAddress(), Thunk->This)
264  : LoadCXXThis();
265 
266  if (CurFnInfo->usesInAlloca()) {
267  // We don't handle return adjusting thunks, because they require us to call
268  // the copy constructor. For now, fall through and pretend the return
269  // adjustment was empty so we don't crash.
270  if (Thunk && !Thunk->Return.isEmpty()) {
272  MD, "non-trivial argument copy for return-adjusting thunk");
273  }
274  EmitMustTailThunk(MD, AdjustedThisPtr, Callee);
275  return;
276  }
277 
278  // Start building CallArgs.
279  CallArgList CallArgs;
280  QualType ThisType = MD->getThisType(getContext());
281  CallArgs.add(RValue::get(AdjustedThisPtr), ThisType);
282 
283  if (isa<CXXDestructorDecl>(MD))
285 
286  // Add the rest of the arguments.
287  for (const ParmVarDecl *PD : MD->parameters())
288  EmitDelegateCallArg(CallArgs, PD, PD->getLocStart());
289 
290  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
291 
292 #ifndef NDEBUG
293  const CGFunctionInfo &CallFnInfo = CGM.getTypes().arrangeCXXMethodCall(
294  CallArgs, FPT, RequiredArgs::forPrototypePlus(FPT, 1, MD));
295  assert(CallFnInfo.getRegParm() == CurFnInfo->getRegParm() &&
296  CallFnInfo.isNoReturn() == CurFnInfo->isNoReturn() &&
297  CallFnInfo.getCallingConvention() == CurFnInfo->getCallingConvention());
298  assert(isa<CXXDestructorDecl>(MD) || // ignore dtor return types
299  similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(),
301  assert(CallFnInfo.arg_size() == CurFnInfo->arg_size());
302  for (unsigned i = 0, e = CurFnInfo->arg_size(); i != e; ++i)
303  assert(similar(CallFnInfo.arg_begin()[i].info,
304  CallFnInfo.arg_begin()[i].type,
305  CurFnInfo->arg_begin()[i].info,
306  CurFnInfo->arg_begin()[i].type));
307 #endif
308 
309  // Determine whether we have a return value slot to use.
310  QualType ResultType = CGM.getCXXABI().HasThisReturn(CurGD)
311  ? ThisType
314  : FPT->getReturnType();
315  ReturnValueSlot Slot;
316  if (!ResultType->isVoidType() &&
319  Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified());
320 
321  // Now emit our call.
322  llvm::Instruction *CallOrInvoke;
323  RValue RV = EmitCall(*CurFnInfo, Callee, Slot, CallArgs, MD, &CallOrInvoke);
324 
325  // Consider return adjustment if we have ThunkInfo.
326  if (Thunk && !Thunk->Return.isEmpty())
327  RV = PerformReturnAdjustment(*this, ResultType, RV, *Thunk);
328  else if (llvm::CallInst* Call = dyn_cast<llvm::CallInst>(CallOrInvoke))
329  Call->setTailCallKind(llvm::CallInst::TCK_Tail);
330 
331  // Emit return.
332  if (!ResultType->isVoidType() && Slot.isNull())
333  CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType);
334 
335  // Disable the final ARC autorelease.
336  AutoreleaseResult = false;
337 
338  FinishThunk();
339 }
340 
342  llvm::Value *AdjustedThisPtr,
343  llvm::Value *Callee) {
344  // Emitting a musttail call thunk doesn't use any of the CGCall.cpp machinery
345  // to translate AST arguments into LLVM IR arguments. For thunks, we know
346  // that the caller prototype more or less matches the callee prototype with
347  // the exception of 'this'.
349  for (llvm::Argument &A : CurFn->args())
350  Args.push_back(&A);
351 
352  // Set the adjusted 'this' pointer.
353  const ABIArgInfo &ThisAI = CurFnInfo->arg_begin()->info;
354  if (ThisAI.isDirect()) {
355  const ABIArgInfo &RetAI = CurFnInfo->getReturnInfo();
356  int ThisArgNo = RetAI.isIndirect() && !RetAI.isSRetAfterThis() ? 1 : 0;
357  llvm::Type *ThisType = Args[ThisArgNo]->getType();
358  if (ThisType != AdjustedThisPtr->getType())
359  AdjustedThisPtr = Builder.CreateBitCast(AdjustedThisPtr, ThisType);
360  Args[ThisArgNo] = AdjustedThisPtr;
361  } else {
362  assert(ThisAI.isInAlloca() && "this is passed directly or inalloca");
363  Address ThisAddr = GetAddrOfLocalVar(CXXABIThisDecl);
364  llvm::Type *ThisType = ThisAddr.getElementType();
365  if (ThisType != AdjustedThisPtr->getType())
366  AdjustedThisPtr = Builder.CreateBitCast(AdjustedThisPtr, ThisType);
367  Builder.CreateStore(AdjustedThisPtr, ThisAddr);
368  }
369 
370  // Emit the musttail call manually. Even if the prologue pushed cleanups, we
371  // don't actually want to run them.
372  llvm::CallInst *Call = Builder.CreateCall(Callee, Args);
373  Call->setTailCallKind(llvm::CallInst::TCK_MustTail);
374 
375  // Apply the standard set of call attributes.
376  unsigned CallingConv;
378  CGM.ConstructAttributeList(Callee->getName(), *CurFnInfo, MD, AttributeList,
379  CallingConv, /*AttrOnCallSite=*/true);
380  llvm::AttributeSet Attrs =
381  llvm::AttributeSet::get(getLLVMContext(), AttributeList);
382  Call->setAttributes(Attrs);
383  Call->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
384 
385  if (Call->getType()->isVoidTy())
386  Builder.CreateRetVoid();
387  else
388  Builder.CreateRet(Call);
389 
390  // Finish the function to maintain CodeGenFunction invariants.
391  // FIXME: Don't emit unreachable code.
393  FinishFunction();
394 }
395 
396 void CodeGenFunction::generateThunk(llvm::Function *Fn,
397  const CGFunctionInfo &FnInfo,
398  GlobalDecl GD, const ThunkInfo &Thunk) {
399  StartThunk(Fn, GD, FnInfo);
400 
401  // Get our callee.
402  llvm::Type *Ty =
404  llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
405 
406  // Make the call and return the result.
407  EmitCallAndReturnForThunk(Callee, &Thunk);
408 }
409 
410 void CodeGenVTables::emitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
411  bool ForVTable) {
412  const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeGlobalDeclaration(GD);
413 
414  // FIXME: re-use FnInfo in this computation.
415  llvm::Constant *C = CGM.GetAddrOfThunk(GD, Thunk);
416  llvm::GlobalValue *Entry;
417 
418  // Strip off a bitcast if we got one back.
419  if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(C)) {
420  assert(CE->getOpcode() == llvm::Instruction::BitCast);
421  Entry = cast<llvm::GlobalValue>(CE->getOperand(0));
422  } else {
423  Entry = cast<llvm::GlobalValue>(C);
424  }
425 
426  // There's already a declaration with the same name, check if it has the same
427  // type or if we need to replace it.
428  if (Entry->getType()->getElementType() !=
430  llvm::GlobalValue *OldThunkFn = Entry;
431 
432  // If the types mismatch then we have to rewrite the definition.
433  assert(OldThunkFn->isDeclaration() &&
434  "Shouldn't replace non-declaration");
435 
436  // Remove the name from the old thunk function and get a new thunk.
437  OldThunkFn->setName(StringRef());
438  Entry = cast<llvm::GlobalValue>(CGM.GetAddrOfThunk(GD, Thunk));
439 
440  // If needed, replace the old thunk with a bitcast.
441  if (!OldThunkFn->use_empty()) {
442  llvm::Constant *NewPtrForOldDecl =
443  llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType());
444  OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
445  }
446 
447  // Remove the old thunk.
448  OldThunkFn->eraseFromParent();
449  }
450 
451  llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
452  bool ABIHasKeyFunctions = CGM.getTarget().getCXXABI().hasKeyFunctions();
453  bool UseAvailableExternallyLinkage = ForVTable && ABIHasKeyFunctions;
454 
455  if (!ThunkFn->isDeclaration()) {
456  if (!ABIHasKeyFunctions || UseAvailableExternallyLinkage) {
457  // There is already a thunk emitted for this function, do nothing.
458  return;
459  }
460 
461  setThunkProperties(CGM, Thunk, ThunkFn, ForVTable, GD);
462  return;
463  }
464 
466 
467  if (ThunkFn->isVarArg()) {
468  // Varargs thunks are special; we can't just generate a call because
469  // we can't copy the varargs. Our implementation is rather
470  // expensive/sucky at the moment, so don't generate the thunk unless
471  // we have to.
472  // FIXME: Do something better here; GenerateVarArgsThunk is extremely ugly.
473  if (UseAvailableExternallyLinkage)
474  return;
475  ThunkFn =
476  CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk);
477  } else {
478  // Normal thunk body generation.
479  CodeGenFunction(CGM).generateThunk(ThunkFn, FnInfo, GD, Thunk);
480  }
481 
482  setThunkProperties(CGM, Thunk, ThunkFn, ForVTable, GD);
483 }
484 
485 void CodeGenVTables::maybeEmitThunkForVTable(GlobalDecl GD,
486  const ThunkInfo &Thunk) {
487  // If the ABI has key functions, only the TU with the key function should emit
488  // the thunk. However, we can allow inlining of thunks if we emit them with
489  // available_externally linkage together with vtables when optimizations are
490  // enabled.
491  if (CGM.getTarget().getCXXABI().hasKeyFunctions() &&
492  !CGM.getCodeGenOpts().OptimizationLevel)
493  return;
494 
495  // We can't emit thunks for member functions with incomplete types.
496  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
497  if (!CGM.getTypes().isFuncTypeConvertible(
498  MD->getType()->castAs<FunctionType>()))
499  return;
500 
501  emitThunk(GD, Thunk, /*ForVTable=*/true);
502 }
503 
505 {
506  const CXXMethodDecl *MD =
507  cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
508 
509  // We don't need to generate thunks for the base destructor.
510  if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
511  return;
512 
513  const VTableContextBase::ThunkInfoVectorTy *ThunkInfoVector =
514  VTContext->getThunkInfo(GD);
515 
516  if (!ThunkInfoVector)
517  return;
518 
519  for (const ThunkInfo& Thunk : *ThunkInfoVector)
520  emitThunk(GD, Thunk, /*ForVTable=*/false);
521 }
522 
524  const CXXRecordDecl *RD, const VTableComponent *Components,
525  unsigned NumComponents, const VTableLayout::VTableThunkTy *VTableThunks,
526  unsigned NumVTableThunks, llvm::Constant *RTTI) {
528 
529  llvm::Type *Int8PtrTy = CGM.Int8PtrTy;
530 
531  llvm::Type *PtrDiffTy =
533 
534  unsigned NextVTableThunkIndex = 0;
535 
536  llvm::Constant *PureVirtualFn = nullptr, *DeletedVirtualFn = nullptr;
537 
538  for (unsigned I = 0; I != NumComponents; ++I) {
539  VTableComponent Component = Components[I];
540 
541  llvm::Constant *Init = nullptr;
542 
543  switch (Component.getKind()) {
545  Init = llvm::ConstantInt::get(PtrDiffTy,
546  Component.getVCallOffset().getQuantity());
547  Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
548  break;
550  Init = llvm::ConstantInt::get(PtrDiffTy,
551  Component.getVBaseOffset().getQuantity());
552  Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
553  break;
555  Init = llvm::ConstantInt::get(PtrDiffTy,
556  Component.getOffsetToTop().getQuantity());
557  Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
558  break;
560  Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
561  break;
565  GlobalDecl GD;
566 
567  // Get the right global decl.
568  switch (Component.getKind()) {
569  default:
570  llvm_unreachable("Unexpected vtable component kind");
572  GD = Component.getFunctionDecl();
573  break;
575  GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
576  break;
578  GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
579  break;
580  }
581 
582  if (CGM.getLangOpts().CUDA) {
583  // Emit NULL for methods we can't codegen on this
584  // side. Otherwise we'd end up with vtable with unresolved
585  // references.
586  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
587  // OK on device side: functions w/ __device__ attribute
588  // OK on host side: anything except __device__-only functions.
589  bool CanEmitMethod = CGM.getLangOpts().CUDAIsDevice
590  ? MD->hasAttr<CUDADeviceAttr>()
591  : (MD->hasAttr<CUDAHostAttr>() ||
592  !MD->hasAttr<CUDADeviceAttr>());
593  if (!CanEmitMethod) {
594  Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
595  break;
596  }
597  // Method is acceptable, continue processing as usual.
598  }
599 
600  if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
601  // We have a pure virtual member function.
602  if (!PureVirtualFn) {
603  llvm::FunctionType *Ty =
604  llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
605  StringRef PureCallName = CGM.getCXXABI().GetPureVirtualCallName();
606  PureVirtualFn = CGM.CreateRuntimeFunction(Ty, PureCallName);
607  if (auto *F = dyn_cast<llvm::Function>(PureVirtualFn))
608  F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
609  PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn,
610  CGM.Int8PtrTy);
611  }
612  Init = PureVirtualFn;
613  } else if (cast<CXXMethodDecl>(GD.getDecl())->isDeleted()) {
614  if (!DeletedVirtualFn) {
615  llvm::FunctionType *Ty =
616  llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
617  StringRef DeletedCallName =
619  DeletedVirtualFn = CGM.CreateRuntimeFunction(Ty, DeletedCallName);
620  if (auto *F = dyn_cast<llvm::Function>(DeletedVirtualFn))
621  F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
622  DeletedVirtualFn = llvm::ConstantExpr::getBitCast(DeletedVirtualFn,
623  CGM.Int8PtrTy);
624  }
625  Init = DeletedVirtualFn;
626  } else {
627  // Check if we should use a thunk.
628  if (NextVTableThunkIndex < NumVTableThunks &&
629  VTableThunks[NextVTableThunkIndex].first == I) {
630  const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
631 
632  maybeEmitThunkForVTable(GD, Thunk);
633  Init = CGM.GetAddrOfThunk(GD, Thunk);
634 
635  NextVTableThunkIndex++;
636  } else {
638 
639  Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
640  }
641 
642  Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
643  }
644  break;
645  }
646 
648  Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
649  break;
650  };
651 
652  Inits.push_back(Init);
653  }
654 
655  llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
656  return llvm::ConstantArray::get(ArrayType, Inits);
657 }
658 
659 llvm::GlobalVariable *
661  const BaseSubobject &Base,
662  bool BaseIsVirtual,
663  llvm::GlobalVariable::LinkageTypes Linkage,
664  VTableAddressPointsMapTy& AddressPoints) {
665  if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
666  DI->completeClassData(Base.getBase());
667 
668  std::unique_ptr<VTableLayout> VTLayout(
669  getItaniumVTableContext().createConstructionVTableLayout(
670  Base.getBase(), Base.getBaseOffset(), BaseIsVirtual, RD));
671 
672  // Add the address points.
673  AddressPoints = VTLayout->getAddressPoints();
674 
675  // Get the mangled construction vtable name.
676  SmallString<256> OutName;
677  llvm::raw_svector_ostream Out(OutName);
678  cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
679  .mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(),
680  Base.getBase(), Out);
681  StringRef Name = OutName.str();
682 
683  llvm::ArrayType *ArrayType =
684  llvm::ArrayType::get(CGM.Int8PtrTy, VTLayout->getNumVTableComponents());
685 
686  // Construction vtable symbols are not part of the Itanium ABI, so we cannot
687  // guarantee that they actually will be available externally. Instead, when
688  // emitting an available_externally VTT, we provide references to an internal
689  // linkage construction vtable. The ABI only requires complete-object vtables
690  // to be the same for all instances of a type, not construction vtables.
691  if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
693 
694  // Create the variable that will hold the construction vtable.
695  llvm::GlobalVariable *VTable =
696  CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, Linkage);
697  CGM.setGlobalVisibility(VTable, RD);
698 
699  // V-tables are always unnamed_addr.
700  VTable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
701 
702  llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(
703  CGM.getContext().getTagDeclType(Base.getBase()));
704 
705  // Create and set the initializer.
706  llvm::Constant *Init = CreateVTableInitializer(
707  Base.getBase(), VTLayout->vtable_component_begin(),
708  VTLayout->getNumVTableComponents(), VTLayout->vtable_thunk_begin(),
709  VTLayout->getNumVTableThunks(), RTTI);
710  VTable->setInitializer(Init);
711 
712  CGM.EmitVTableTypeMetadata(VTable, *VTLayout.get());
713 
714  return VTable;
715 }
716 
718  const CXXRecordDecl *RD) {
719  return CGM.getCodeGenOpts().OptimizationLevel > 0 &&
721 }
722 
723 /// Compute the required linkage of the vtable for the given class.
724 ///
725 /// Note that we only call this at the end of the translation unit.
726 llvm::GlobalVariable::LinkageTypes
728  if (!RD->isExternallyVisible())
730 
731  // We're at the end of the translation unit, so the current key
732  // function is fully correct.
733  const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction(RD);
734  if (keyFunction && !RD->hasAttr<DLLImportAttr>()) {
735  // If this class has a key function, use that to determine the
736  // linkage of the vtable.
737  const FunctionDecl *def = nullptr;
738  if (keyFunction->hasBody(def))
739  keyFunction = cast<CXXMethodDecl>(def);
740 
741  switch (keyFunction->getTemplateSpecializationKind()) {
742  case TSK_Undeclared:
744  assert((def || CodeGenOpts.OptimizationLevel > 0) &&
745  "Shouldn't query vtable linkage without key function or "
746  "optimizations");
747  if (!def && CodeGenOpts.OptimizationLevel > 0)
748  return llvm::GlobalVariable::AvailableExternallyLinkage;
749 
750  if (keyFunction->isInlined())
751  return !Context.getLangOpts().AppleKext ?
752  llvm::GlobalVariable::LinkOnceODRLinkage :
754 
756 
758  return !Context.getLangOpts().AppleKext ?
759  llvm::GlobalVariable::LinkOnceODRLinkage :
761 
763  return !Context.getLangOpts().AppleKext ?
764  llvm::GlobalVariable::WeakODRLinkage :
766 
768  llvm_unreachable("Should not have been asked to emit this");
769  }
770  }
771 
772  // -fapple-kext mode does not support weak linkage, so we must use
773  // internal linkage.
774  if (Context.getLangOpts().AppleKext)
776 
777  llvm::GlobalVariable::LinkageTypes DiscardableODRLinkage =
778  llvm::GlobalValue::LinkOnceODRLinkage;
779  llvm::GlobalVariable::LinkageTypes NonDiscardableODRLinkage =
780  llvm::GlobalValue::WeakODRLinkage;
781  if (RD->hasAttr<DLLExportAttr>()) {
782  // Cannot discard exported vtables.
783  DiscardableODRLinkage = NonDiscardableODRLinkage;
784  } else if (RD->hasAttr<DLLImportAttr>()) {
785  // Imported vtables are available externally.
786  DiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage;
787  NonDiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage;
788  }
789 
790  switch (RD->getTemplateSpecializationKind()) {
791  case TSK_Undeclared:
794  return DiscardableODRLinkage;
795 
797  // Explicit instantiations in MSVC do not provide vtables, so we must emit
798  // our own.
799  if (getTarget().getCXXABI().isMicrosoft())
800  return DiscardableODRLinkage;
801  return shouldEmitAvailableExternallyVTable(*this, RD)
802  ? llvm::GlobalVariable::AvailableExternallyLinkage
804 
806  return NonDiscardableODRLinkage;
807  }
808 
809  llvm_unreachable("Invalid TemplateSpecializationKind!");
810 }
811 
812 /// This is a callback from Sema to tell us that that a particular vtable is
813 /// required to be emitted in this translation unit.
814 ///
815 /// This is only called for vtables that _must_ be emitted (mainly due to key
816 /// functions). For weak vtables, CodeGen tracks when they are needed and
817 /// emits them as-needed.
819  VTables.GenerateClassData(theClass);
820 }
821 
822 void
824  if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
825  DI->completeClassData(RD);
826 
827  if (RD->getNumVBases())
829 
830  CGM.getCXXABI().emitVTableDefinitions(*this, RD);
831 }
832 
833 /// At this point in the translation unit, does it appear that can we
834 /// rely on the vtable being defined elsewhere in the program?
835 ///
836 /// The response is really only definitive when called at the end of
837 /// the translation unit.
838 ///
839 /// The only semantic restriction here is that the object file should
840 /// not contain a vtable definition when that vtable is defined
841 /// strongly elsewhere. Otherwise, we'd just like to avoid emitting
842 /// vtables when unnecessary.
844  assert(RD->isDynamicClass() && "Non-dynamic classes have no VTable.");
845 
846  // We always synthesize vtables if they are needed in the MS ABI. MSVC doesn't
847  // emit them even if there is an explicit template instantiation.
848  if (CGM.getTarget().getCXXABI().isMicrosoft())
849  return false;
850 
851  // If we have an explicit instantiation declaration (and not a
852  // definition), the vtable is defined elsewhere.
855  return true;
856 
857  // Otherwise, if the class is an instantiated template, the
858  // vtable must be defined here.
859  if (TSK == TSK_ImplicitInstantiation ||
861  return false;
862 
863  // Otherwise, if the class doesn't have a key function (possibly
864  // anymore), the vtable must be defined here.
865  const CXXMethodDecl *keyFunction = CGM.getContext().getCurrentKeyFunction(RD);
866  if (!keyFunction)
867  return false;
868 
869  // Otherwise, if we don't have a definition of the key function, the
870  // vtable must be defined somewhere else.
871  return !keyFunction->hasBody();
872 }
873 
874 /// Given that we're currently at the end of the translation unit, and
875 /// we've emitted a reference to the vtable for this class, should
876 /// we define that vtable?
878  const CXXRecordDecl *RD) {
879  // If vtable is internal then it has to be done.
880  if (!CGM.getVTables().isVTableExternal(RD))
881  return true;
882 
883  // If it's external then maybe we will need it as available_externally.
884  return shouldEmitAvailableExternallyVTable(CGM, RD);
885 }
886 
887 /// Given that at some point we emitted a reference to one or more
888 /// vtables, and that we are now at the end of the translation unit,
889 /// decide whether we should emit them.
890 void CodeGenModule::EmitDeferredVTables() {
891 #ifndef NDEBUG
892  // Remember the size of DeferredVTables, because we're going to assume
893  // that this entire operation doesn't modify it.
894  size_t savedSize = DeferredVTables.size();
895 #endif
896 
897  for (const CXXRecordDecl *RD : DeferredVTables)
899  VTables.GenerateClassData(RD);
900 
901  assert(savedSize == DeferredVTables.size() &&
902  "deferred extra vtables during vtable emission?");
903  DeferredVTables.clear();
904 }
905 
908  if (!isExternallyVisible(LV.getLinkage()))
909  return true;
910 
911  if (RD->hasAttr<LTOVisibilityPublicAttr>() || RD->hasAttr<UuidAttr>())
912  return false;
913 
914  if (getTriple().isOSBinFormatCOFF()) {
915  if (RD->hasAttr<DLLExportAttr>() || RD->hasAttr<DLLImportAttr>())
916  return false;
917  } else {
918  if (LV.getVisibility() != HiddenVisibility)
919  return false;
920  }
921 
922  if (getCodeGenOpts().LTOVisibilityPublicStd) {
923  const DeclContext *DC = RD;
924  while (1) {
925  auto *D = cast<Decl>(DC);
926  DC = DC->getParent();
927  if (isa<TranslationUnitDecl>(DC->getRedeclContext())) {
928  if (auto *ND = dyn_cast<NamespaceDecl>(D))
929  if (const IdentifierInfo *II = ND->getIdentifier())
930  if (II->isStr("std") || II->isStr("stdext"))
931  return false;
932  break;
933  }
934  }
935  }
936 
937  return true;
938 }
939 
940 void CodeGenModule::EmitVTableTypeMetadata(llvm::GlobalVariable *VTable,
941  const VTableLayout &VTLayout) {
942  if (!getCodeGenOpts().PrepareForLTO)
943  return;
944 
945  CharUnits PointerWidth =
946  Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
947 
948  typedef std::pair<const CXXRecordDecl *, unsigned> BSEntry;
949  std::vector<BSEntry> BitsetEntries;
950  // Create a bit set entry for each address point.
951  for (auto &&AP : VTLayout.getAddressPoints())
952  BitsetEntries.push_back(std::make_pair(AP.first.getBase(), AP.second));
953 
954  // Sort the bit set entries for determinism.
955  std::sort(BitsetEntries.begin(), BitsetEntries.end(),
956  [this](const BSEntry &E1, const BSEntry &E2) {
957  if (&E1 == &E2)
958  return false;
959 
960  std::string S1;
961  llvm::raw_string_ostream O1(S1);
963  QualType(E1.first->getTypeForDecl(), 0), O1);
964  O1.flush();
965 
966  std::string S2;
967  llvm::raw_string_ostream O2(S2);
969  QualType(E2.first->getTypeForDecl(), 0), O2);
970  O2.flush();
971 
972  if (S1 < S2)
973  return true;
974  if (S1 != S2)
975  return false;
976 
977  return E1.second < E2.second;
978  });
979 
980  for (auto BitsetEntry : BitsetEntries)
981  AddVTableTypeMetadata(VTable, PointerWidth * BitsetEntry.second,
982  BitsetEntry.first);
983 }
ReturnValueSlot - Contains the address where the return value of a function can be stored...
Definition: CGCall.h:151
static const Decl * getCanonicalDecl(const Decl *D)
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1561
External linkage, which indicates that the entity can be referred to from other translation units...
Definition: Linkage.h:50
CanQualType VoidPtrTy
Definition: ASTContext.h:908
A (possibly-)qualified type.
Definition: Type.h:598
bool ReturnTypeUsesSRet(const CGFunctionInfo &FI)
Return true iff the given type uses 'sret' when used as a return type.
Definition: CGCall.cpp:1423
CanQualType getReturnType() const
CharUnits getOffsetToTop() const
virtual void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResultType)
Definition: CGCXXABI.cpp:188
llvm::Module & getModule() const
virtual void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy, FunctionArgList &Params)=0
Insert any ABI-specific implicit parameters into the parameter list for a function.
CharUnits getClassPointerAlignment(const CXXRecordDecl *CD)
Returns the assumed alignment of an opaque pointer to the given class.
Definition: CGClass.cpp:36
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:2879
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:179
virtual const ThunkInfoVectorTy * getThunkInfo(GlobalDecl GD)
virtual llvm::Value * performThisAdjustment(CodeGenFunction &CGF, Address This, const ThisAdjustment &TA)=0
const Decl * CurCodeDecl
CurCodeDecl - This is the inner-most code context, which includes blocks.
llvm::Value * LoadCXXThis()
LoadCXXThis - Load the value of 'this'.
void setFunctionLinkage(GlobalDecl GD, llvm::Function *F)
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:2456
virtual StringRef GetDeletedVirtualCallName()=0
Gets the deleted virtual member call name.
llvm::Function * GenerateVarArgsThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo, GlobalDecl GD, const ThunkInfo &Thunk)
Definition: CGVTables.cpp:144
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:906
Address GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
Objects with "hidden" visibility are not seen by the dynamic linker.
Definition: Visibility.h:35
CGDebugInfo * getModuleDebugInfo()
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition: CGDebugInfo.h:52
QualType getThisType(ASTContext &C) const
Returns the type of the this pointer.
Definition: DeclCXX.cpp:1672
ParmVarDecl - Represents a parameter to a function.
Definition: Decl.h:1377
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have...
Definition: Linkage.h:25
bool isVoidType() const
Definition: Type.h:5680
virtual bool hasMostDerivedReturn(GlobalDecl GD) const
Definition: CGCXXABI.h:107
Visibility getVisibility() const
Definition: Visibility.h:80
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body (definition).
Definition: Decl.cpp:2454
One of these records is kept for each identifier that is lexed.
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
Definition: Type.h:4549
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...
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
static bool shouldEmitAvailableExternallyVTable(const CodeGenModule &CGM, const CXXRecordDecl *RD)
Definition: CGVTables.cpp:717
static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD, const ThunkInfo &Thunk, llvm::Function *Fn)
Definition: CGVTables.cpp:53
bool isReferenceType() const
Definition: Type.h:5491
RValue EmitCall(const CGFunctionInfo &FnInfo, llvm::Value *Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, CGCalleeInfo CalleeInfo=CGCalleeInfo(), llvm::Instruction **callOrInvoke=nullptr)
EmitCall - Generate a call of the given function, expecting the given result type, and using the given argument list which specifies both the LLVM arguments and the types they were derived from.
Definition: CGCall.cpp:3507
The this pointer adjustment as well as an optional return adjustment for a thunk. ...
Definition: ABI.h:179
const Decl * getDecl() const
Definition: GlobalDecl.h:62
Linkage getLinkage() const
Definition: Visibility.h:79
virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF)=0
Emit the ABI-specific prolog for the function.
const CXXMethodDecl * getFunctionDecl() const
static bool hasScalarEvaluationKind(QualType T)
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:588
virtual void emitVTableDefinitions(CodeGenVTables &CGVT, const CXXRecordDecl *RD)=0
Emits the VTable definitions required for the given record type.
const LangOptions & getLangOpts() const
Definition: ASTContext.h:604
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
CharUnits getVCallOffset() const
QualType getReturnType() const
Definition: Type.h:3009
const CXXRecordDecl * getParent() const
Returns the parent of this method declaration, which is the class in which this method is defined...
Definition: DeclCXX.h:1838
Deleting dtor.
Definition: ABI.h:35
ABIArgInfo - Helper class to encapsulate information about how a specific C type should be passed to ...
unsigned getCallingConvention() const
getCallingConvention - Return the user specified calling convention, which has been translated into a...
virtual bool canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const =0
Determine whether it's possible to emit a vtable for RD, even though we do not know that the vtable h...
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:71
GlobalDecl CurGD
CurGD - The GlobalDecl for the current function being compiled.
void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const
Set the visibility for the given LLVM GlobalValue.
detail::InMemoryDirectory::const_iterator I
QualType getType() const
Definition: Decl.h:599
const CGFunctionInfo & arrangeGlobalDeclaration(GlobalDecl GD)
Definition: CGCall.cpp:456
param_iterator param_begin()
Definition: Decl.h:2000
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3073
const TargetInfo & getTarget() const
RValue - This trivial value class is used to represent the result of an expression that is evaluated...
Definition: CGValue.h:38
void EmitDelegateCallArg(CallArgList &args, const VarDecl *param, SourceLocation loc)
EmitDelegateCallArg - We are performing a delegate call; that is, the current function is delegating ...
Definition: CGCall.cpp:2881
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:843
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
Definition: Type.cpp:415
const CXXRecordDecl * getBase() const
getBase - Returns the base class declaration.
Definition: BaseSubobject.h:40
static void setThunkProperties(CodeGenModule &CGM, const ThunkInfo &Thunk, llvm::Function *ThunkFn, bool ForVTable, GlobalDecl GD)
Definition: CGVTables.cpp:58
llvm::Value * getPointer() const
Definition: Address.h:38
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
Definition: TargetCXXABI.h:155
static bool similar(const ABIArgInfo &infoL, CanQualType typeL, const ABIArgInfo &infoR, CanQualType typeR)
Definition: CGVTables.cpp:74
CXXDtorType getDtorType() const
Definition: GlobalDecl.h:69
CGCXXABI & getCXXABI() const
bool usesInAlloca() const
Return true if this function uses inalloca arguments.
Inits[]
Definition: OpenMPClause.h:312
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2414
virtual StringRef GetPureVirtualCallName()=0
Gets the pure virtual member call function.
ASTContext & getContext() const
void add(RValue rvalue, QualType type, bool needscopy=false)
Definition: CGCall.h:81
bool isInlined() const
Determine whether this function should be inlined, because it is either marked "inline" or "constexpr...
Definition: Decl.h:2083
llvm::LLVMContext & getLLVMContext()
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
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:235
Base object dtor.
Definition: ABI.h:37
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:1214
virtual bool HasThisReturn(GlobalDecl GD) const
Returns true if the given constructor or destructor is one of the kinds that the ABI says returns 'th...
Definition: CGCXXABI.h:105
bool isExternallyVisible(Linkage L)
Definition: Linkage.h:72
bool isExternallyVisible() const
Definition: Decl.h:348
This template specialization was implicitly instantiated from a template.
Definition: Specifiers.h:147
virtual void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF, GlobalDecl GD, CallArgList &CallArgs)
Definition: CGCXXABI.h:418
void AddVTableTypeMetadata(llvm::GlobalVariable *VTable, CharUnits Offset, const CXXRecordDecl *RD)
Create and attach type metadata for the given vtable.
void generateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo, GlobalDecl GD, const ThunkInfo &Thunk)
Generate a thunk for the given method.
Definition: CGVTables.cpp:396
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:231
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:29
llvm::GlobalVariable * CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage)
Will return a global variable of the given type.
llvm::Constant * GetAddrOfThunk(GlobalDecl GD, const ThunkInfo &Thunk)
Get the address of the thunk for the given global decl.
Definition: CGVTables.cpp:35
The l-value was considered opaque, so the alignment was determined from a type.
virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, const ThisAdjustment &ThisAdjustment, raw_ostream &)=0
Address CreateBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Definition: CGBuilder.h:160
llvm::Constant * CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeSet ExtraAttrs=llvm::AttributeSet())
Create a new runtime function with the specified type and name.
ASTContext & getContext() const
const TemplateArgument * iterator
Definition: Type.h:4233
void EmitCallAndReturnForThunk(llvm::Value *Callee, const ThunkInfo *Thunk)
Definition: CGVTables.cpp:254
Represents a single component in a vtable.
Definition: VTableBuilder.h:31
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1736
ArrayRef< ParmVarDecl * > parameters() const
Definition: Decl.h:1989
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:32
const CXXDestructorDecl * getDestructorDecl() const
const CodeGenOptions & getCodeGenOpts() const
An aligned address.
Definition: Address.h:25
LinkageInfo getLinkageAndVisibility() const
Determines the linkage and visibility of this entity.
Definition: Decl.cpp:1036
void StartFunction(GlobalDecl GD, QualType RetTy, llvm::Function *Fn, const CGFunctionInfo &FnInfo, const FunctionArgList &Args, SourceLocation Loc=SourceLocation(), SourceLocation StartLoc=SourceLocation())
Emit code for the start of a function.
const LangOptions & getLangOpts() const
void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F)
Set the LLVM function attributes which only apply to a function definition.
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:5849
virtual void emitVirtualInheritanceTables(const CXXRecordDecl *RD)=0
Emit any tables needed to implement virtual inheritance.
MangleContext & getMangleContext()
Gets the mangle context.
Definition: CGCXXABI.h:95
This template specialization was instantiated from a template due to an explicit instantiation defini...
Definition: Specifiers.h:159
This template specialization was formed from a template-id but has not yet been declared, defined, or instantiated.
Definition: Specifiers.h:144
Complete object dtor.
Definition: ABI.h:36
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition: Type.h:5329
const CGFunctionInfo & arrangeCXXMethodCall(const CallArgList &args, const FunctionProtoType *type, RequiredArgs required)
Arrange a call to a C++ method, passing the given arguments.
Definition: CGCall.cpp:618
const CGFunctionInfo * CurFnInfo
bool isDynamicClass() const
Definition: DeclCXX.h:698
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:660
void FinishFunction(SourceLocation EndLoc=SourceLocation())
FinishFunction - Complete IR generation of the current function.
virtual void mangleTypeName(QualType T, raw_ostream &)=0
Generates a unique string for an externally visible type for use with TBAA or type uniquing...
FunctionArgList - Type for representing both the decl and type of parameters to a function...
Definition: CGCall.h:146
TemplateSpecializationKind getTemplateSpecializationKind() const
Determine whether this particular class is a specialization or instantiation of a class template or m...
Definition: DeclCXX.cpp:1310
void ErrorUnsupported(const Stmt *S, const char *Type)
Print out an error that codegen doesn'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.
ThisAdjustment This
The this pointer adjustment.
Definition: ABI.h:181
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1135
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
Definition: CGValue.h:58
void EmitVTableTypeMetadata(llvm::GlobalVariable *VTable, const VTableLayout &VTLayout)
Emit type metadata for the given vtable using the given layout.
Definition: CGVTables.cpp:940
This template specialization was instantiated from a template due to an explicit instantiation declar...
Definition: Specifiers.h:155
CharUnits getVBaseOffset() const
void buildThisParam(CodeGenFunction &CGF, FunctionArgList &Params)
Build a parameter variable suitable for 'this'.
Definition: CGCXXABI.cpp:156
TemplateSpecializationKind
Describes the kind of template specialization that a particular template specialization declaration r...
Definition: Specifiers.h:141
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
Definition: CGBuilder.h:113
const llvm::Triple & getTriple() const
void EmitThunks(GlobalDecl GD)
EmitThunks - Emit the associated thunks for the given global decl.
Definition: CGVTables.cpp:504
param_iterator param_end()
Definition: Decl.h:2001
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:727
QualType getPointerDiffType() const
Return the unique type for "ptrdiff_t" (C99 7.17) defined in <stddef.h>.
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:5818
This template specialization was declared or defined by an explicit specialization (C++ [temp...
Definition: Specifiers.h:151
TemplateSpecializationKind getTemplateSpecializationKind() const
Determine what kind of template instantiation this function represents.
Definition: Decl.cpp:3273
static bool shouldEmitVTableAtEndOfTranslationUnit(CodeGenModule &CGM, const CXXRecordDecl *RD)
Given that we're currently at the end of the translation unit, and we've emitted a reference to the v...
Definition: CGVTables.cpp:877
void GenerateClassData(const CXXRecordDecl *RD)
GenerateClassData - Generate all the class data required to be generated upon definition of a KeyFunc...
Definition: CGVTables.cpp:823
ReturnAdjustment Return
The return adjustment.
Definition: ABI.h:184
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1528
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
Definition: Linkage.h:33
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition: CGStmt.cpp:397
QualType getTagDeclType(const TagDecl *Decl) const
Return the unique reference to the type for the specified TagDecl (struct/union/class/enum) decl...
void StartThunk(llvm::Function *Fn, GlobalDecl GD, const CGFunctionInfo &FnInfo)
Definition: CGVTables.cpp:208
uint64_t getPointerWidth(unsigned AddrSpace) const
Return the width of pointers on this target, for the specified address space.
const Decl * CurFuncDecl
CurFuncDecl - Holds the Decl for the current outermost non-closure context.
llvm::Type * GetFunctionTypeForVTable(GlobalDecl GD)
GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable, given a CXXMethodDecl...
Definition: CGCall.cpp:1593
static RValue PerformReturnAdjustment(CodeGenFunction &CGF, QualType ResultType, RValue RV, const ThunkInfo &Thunk)
Definition: CGVTables.cpp:83
const CXXMethodDecl * getCurrentKeyFunction(const CXXRecordDecl *RD)
Get our current best idea for the key function of the given record decl, or NULL if there isn't one...
Represents a C++ struct/union/class.
Definition: DeclCXX.h:263
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
virtual void mangleThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk, raw_ostream &)=0
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
llvm::Constant * CreateVTableInitializer(const CXXRecordDecl *RD, const VTableComponent *Components, unsigned NumComponents, const VTableLayout::VTableThunkTy *VTableThunks, unsigned NumVTableThunks, llvm::Constant *RTTI)
CreateVTableInitializer - Create a vtable initializer for the given record decl.
Definition: CGVTables.cpp:523
std::pair< uint64_t, ThunkInfo > VTableThunkTy
bool AutoreleaseResult
In ARC, whether we should autorelease the return value.
const_arg_iterator arg_begin() const
A pointer to the deleting destructor.
Definition: VTableBuilder.h:44
static RValue get(llvm::Value *V)
Definition: CGValue.h:85
static RequiredArgs forPrototypePlus(const FunctionProtoType *prototype, unsigned additional, const FunctionDecl *FD)
Compute the arguments required by the given formal prototype, given that there may be some additional...
virtual llvm::Value * performReturnAdjustment(CodeGenFunction &CGF, Address Ret, const ReturnAdjustment &RA)=0
Kind getKind() const
Get the kind of this vtable component.
const AddressPointsMapTy & getAddressPoints() const
CodeGenVTables & getVTables()
CharUnits getBaseOffset() const
getBaseOffset - Returns the base class offset.
Definition: BaseSubobject.h:43
unsigned getNumVBases() const
Retrieves the number of virtual base classes of this class.
Definition: DeclCXX.h:733
bool CurFuncIsThunk
In C++, whether we are code generating a thunk.
void ConstructAttributeList(StringRef Name, const CGFunctionInfo &Info, CGCalleeInfo CalleeInfo, AttributeListType &PAL, unsigned &CallingConv, bool AttrOnCallSite)
Get the LLVM attributes and calling convention to use for a particular function type.
Definition: CGCall.cpp:1620
llvm::Constant * GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty=nullptr, bool ForVTable=false, bool DontDefer=false, bool IsForDefinition=false)
Return the address of the given function.
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:56
A pointer to the complete destructor.
Definition: VTableBuilder.h:41
bool isEmpty() const
Definition: ABI.h:87
virtual void setThunkLinkage(llvm::Function *Thunk, bool ForVTable, GlobalDecl GD, bool ReturnAdjustment)=0
void EmitVTable(CXXRecordDecl *Class)
This is a callback from Sema to tell us that that a particular vtable is required to be emitted in th...
Definition: CGVTables.cpp:818
AttributeList - Represents a syntactic attribute.
Definition: AttributeList.h:94
void EmitMustTailThunk(const CXXMethodDecl *MD, llvm::Value *AdjustedThisPtr, llvm::Value *Callee)
Emit a musttail call for a thunk with a potentially adjusted this pointer.
Definition: CGVTables.cpp:341
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1466