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