1//===--- CGCXX.cpp - Emit LLVM Code for declarations ----------------------===//
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.
10//
11//===----------------------------------------------------------------------===//
12
13// We might split this into multiple files if it gets too unwieldy
14
15#include "CGCXXABI.h"
16#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/Attr.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclCXX.h"
22#include "clang/AST/DeclObjC.h"
23#include "clang/AST/Mangle.h"
24#include "clang/AST/RecordLayout.h"
25#include "clang/Basic/CodeGenOptions.h"
26using namespace clang;
27using namespace CodeGen;
28
29
30/// Try to emit a base destructor as an alias to its primary
31/// base-class destructor.
32bool CodeGenModule::TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) {
33 if (!getCodeGenOpts().CXXCtorDtorAliases)
34 return true;
35
36 // Producing an alias to a base class ctor/dtor can degrade debug quality
37 // as the debugger cannot tell them apart.
38 if (getCodeGenOpts().OptimizationLevel == 0)
39 return true;
40
41 // Disable this optimization for ARM64EC. FIXME: This probably should work,
42 // but getting the symbol table correct is complicated.
43 if (getTarget().getTriple().isWindowsArm64EC())
44 return true;
45
46 // If sanitizing memory to check for use-after-dtor, do not emit as
47 // an alias, unless this class owns no members.
48 if (getCodeGenOpts().SanitizeMemoryUseAfterDtor &&
49 !D->getParent()->field_empty())
50 return true;
51
52 // If the destructor doesn't have a trivial body, we have to emit it
53 // separately.
54 if (!D->hasTrivialBody())
55 return true;
56
57 const CXXRecordDecl *Class = D->getParent();
58
59 // We are going to instrument this destructor, so give up even if it is
60 // currently empty.
61 if (Class->mayInsertExtraPadding())
62 return true;
63
64 // If we need to manipulate a VTT parameter, give up.
65 if (Class->getNumVBases()) {
66 // Extra Credit: passing extra parameters is perfectly safe
67 // in many calling conventions, so only bail out if the ctor's
68 // calling convention is nonstandard.
69 return true;
70 }
71
72 // If any field has a non-trivial destructor, we have to emit the
73 // destructor separately.
74 for (const auto *I : Class->fields())
75 if (I->getType().isDestructedType())
76 return true;
77
78 // Try to find a unique base class with a non-trivial destructor.
79 const CXXRecordDecl *UniqueBase = nullptr;
80 for (const auto &I : Class->bases()) {
81
82 // We're in the base destructor, so skip virtual bases.
83 if (I.isVirtual()) continue;
84
85 // Skip base classes with trivial destructors.
86 const auto *Base = I.getType()->castAsCXXRecordDecl();
87 if (Base->hasTrivialDestructor()) continue;
88
89 // If we've already found a base class with a non-trivial
90 // destructor, give up.
91 if (UniqueBase) return true;
92 UniqueBase = Base;
93 }
94
95 // If we didn't find any bases with a non-trivial destructor, then
96 // the base destructor is actually effectively trivial, which can
97 // happen if it was needlessly user-defined or if there are virtual
98 // bases with non-trivial destructors.
99 if (!UniqueBase)
100 return true;
101
102 // If the base is at a non-zero offset, give up.
103 const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(D: Class);
104 if (!ClassLayout.getBaseClassOffset(Base: UniqueBase).isZero())
105 return true;
106
107 // Give up if the calling conventions don't match. We could update the call,
108 // but it is probably not worth it.
109 const CXXDestructorDecl *BaseD = UniqueBase->getDestructor();
110 if (BaseD->getType()->castAs<FunctionType>()->getCallConv() !=
111 D->getType()->castAs<FunctionType>()->getCallConv())
112 return true;
113
114 GlobalDecl AliasDecl(D, Dtor_Base);
115 GlobalDecl TargetDecl(BaseD, Dtor_Base);
116
117 // The alias will use the linkage of the referent. If we can't
118 // support aliases with that linkage, fail.
119 llvm::GlobalValue::LinkageTypes Linkage = getFunctionLinkage(GD: AliasDecl);
120
121 // We can't use an alias if the linkage is not valid for one.
122 if (!llvm::GlobalAlias::isValidLinkage(L: Linkage))
123 return true;
124
125 llvm::GlobalValue::LinkageTypes TargetLinkage =
126 getFunctionLinkage(GD: TargetDecl);
127
128 // Check if we have it already.
129 StringRef MangledName = getMangledName(GD: AliasDecl);
130 llvm::GlobalValue *Entry = GetGlobalValue(Ref: MangledName);
131 if (Entry && !Entry->isDeclaration())
132 return false;
133 if (Replacements.count(Key: MangledName))
134 return false;
135
136 llvm::Type *AliasValueType = getTypes().GetFunctionType(GD: AliasDecl);
137
138 // Find the referent.
139 auto *Aliasee = cast<llvm::GlobalValue>(Val: GetAddrOfGlobal(GD: TargetDecl));
140
141 // Instead of creating as alias to a linkonce_odr, replace all of the uses
142 // of the aliasee.
143 if (llvm::GlobalValue::isDiscardableIfUnused(Linkage) &&
144 !(TargetLinkage == llvm::GlobalValue::AvailableExternallyLinkage &&
145 TargetDecl.getDecl()->hasAttr<AlwaysInlineAttr>())) {
146 // FIXME: An extern template instantiation will create functions with
147 // linkage "AvailableExternally". In libc++, some classes also define
148 // members with attribute "AlwaysInline" and expect no reference to
149 // be generated. It is desirable to reenable this optimisation after
150 // corresponding LLVM changes.
151 addReplacement(Name: MangledName, C: Aliasee);
152 return false;
153 }
154
155 // If we have a weak, non-discardable alias (weak, weak_odr), like an extern
156 // template instantiation or a dllexported class, avoid forming it on COFF.
157 // A COFF weak external alias cannot satisfy a normal undefined symbol
158 // reference from another TU. The other TU must also mark the referenced
159 // symbol as weak, which we cannot rely on.
160 if (llvm::GlobalValue::isWeakForLinker(Linkage) &&
161 getTriple().isOSBinFormatCOFF()) {
162 return true;
163 }
164
165 // If we don't have a definition for the destructor yet or the definition is
166 // avaialable_externally, don't emit an alias. We can't emit aliases to
167 // declarations; that's just not how aliases work.
168 if (Aliasee->isDeclarationForLinker())
169 return true;
170
171 // Don't create an alias to a linker weak symbol. This avoids producing
172 // different COMDATs in different TUs. Another option would be to
173 // output the alias both for weak_odr and linkonce_odr, but that
174 // requires explicit comdat support in the IL.
175 if (llvm::GlobalValue::isWeakForLinker(Linkage: TargetLinkage))
176 return true;
177 // Create the alias with no name.
178 auto *Alias = llvm::GlobalAlias::create(Ty: AliasValueType, AddressSpace: 0, Linkage, Name: "",
179 Aliasee, Parent: &getModule());
180
181 // Destructors are always unnamed_addr.
182 Alias->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
183
184 // Switch any previous uses to the alias.
185 if (Entry) {
186 assert(Entry->getValueType() == AliasValueType &&
187 Entry->getAddressSpace() == Alias->getAddressSpace() &&
188 "declaration exists with different type");
189 Alias->takeName(V: Entry);
190 Entry->replaceAllUsesWith(V: Alias);
191 Entry->eraseFromParent();
192 } else {
193 Alias->setName(MangledName);
194 }
195
196 // Finally, set up the alias with its proper name and attributes.
197 SetCommonAttributes(GD: AliasDecl, GV: Alias);
198
199 return false;
200}
201
202/// Emit a definition as a global alias for another definition, unconditionally.
203void CodeGenModule::EmitDefinitionAsAlias(GlobalDecl AliasDecl,
204 GlobalDecl TargetDecl) {
205
206 llvm::Type *AliasValueType = getTypes().GetFunctionType(GD: AliasDecl);
207
208 StringRef MangledName = getMangledName(GD: AliasDecl);
209 llvm::GlobalValue *Entry = GetGlobalValue(Ref: MangledName);
210 if (Entry && !Entry->isDeclaration())
211 return;
212 auto *Aliasee = cast<llvm::GlobalValue>(Val: GetAddrOfGlobal(GD: TargetDecl));
213
214 // Determine the linkage type for the alias.
215 llvm::GlobalValue::LinkageTypes Linkage = getFunctionLinkage(GD: AliasDecl);
216
217 // Create the alias with no name.
218 auto *Alias = llvm::GlobalAlias::create(Ty: AliasValueType, AddressSpace: 0, Linkage, Name: "",
219 Aliasee, Parent: &getModule());
220 // Destructors are always unnamed_addr.
221 Alias->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
222
223 if (Entry) {
224 assert(Entry->getValueType() == AliasValueType &&
225 Entry->getAddressSpace() == Alias->getAddressSpace() &&
226 "declaration exists with different type");
227 Alias->takeName(V: Entry);
228 Entry->replaceAllUsesWith(V: Alias);
229 Entry->eraseFromParent();
230 } else {
231 Alias->setName(MangledName);
232 }
233
234 // Set any additional necessary attributes for the alias.
235 SetCommonAttributes(GD: AliasDecl, GV: Alias);
236}
237
238llvm::Function *CodeGenModule::codegenCXXStructor(GlobalDecl GD) {
239 const CGFunctionInfo &FnInfo = getTypes().arrangeCXXStructorDeclaration(GD);
240 auto *Fn = cast<llvm::Function>(
241 Val: getAddrOfCXXStructor(GD, FnInfo: &FnInfo, /*FnType=*/nullptr,
242 /*DontDefer=*/true, IsForDefinition: ForDefinition));
243
244 setFunctionLinkage(GD, F: Fn);
245
246 CodeGenFunction(*this).GenerateCode(GD, Fn, FnInfo);
247 setNonAliasAttributes(GD, GO: Fn);
248 SetLLVMFunctionAttributesForDefinition(D: cast<CXXMethodDecl>(Val: GD.getDecl()), F: Fn);
249 return Fn;
250}
251
252llvm::FunctionCallee CodeGenModule::getAddrAndTypeOfCXXStructor(
253 GlobalDecl GD, const CGFunctionInfo *FnInfo, llvm::FunctionType *FnType,
254 bool DontDefer, ForDefinition_t IsForDefinition) {
255 auto *MD = cast<CXXMethodDecl>(Val: GD.getDecl());
256
257 if (isa<CXXDestructorDecl>(Val: MD)) {
258 // Always alias equivalent complete destructors to base destructors in the
259 // MS ABI.
260 if (getTarget().getCXXABI().isMicrosoft() &&
261 GD.getDtorType() == Dtor_Complete &&
262 MD->getParent()->getNumVBases() == 0)
263 GD = GD.getWithDtorType(Type: Dtor_Base);
264 }
265
266 if (!FnType) {
267 if (!FnInfo)
268 FnInfo = &getTypes().arrangeCXXStructorDeclaration(GD);
269 FnType = getTypes().GetFunctionType(Info: *FnInfo);
270 }
271
272 llvm::Constant *Ptr = GetOrCreateLLVMFunction(
273 MangledName: getMangledName(GD), Ty: FnType, D: GD, /*ForVTable=*/false, DontDefer,
274 /*IsThunk=*/false, /*ExtraAttrs=*/llvm::AttributeList(), IsForDefinition);
275 return {FnType, Ptr};
276}
277
278static CGCallee BuildAppleKextVirtualCall(CodeGenFunction &CGF,
279 GlobalDecl GD,
280 llvm::Type *Ty,
281 const CXXRecordDecl *RD) {
282 assert(!CGF.CGM.getTarget().getCXXABI().isMicrosoft() &&
283 "No kext in Microsoft ABI");
284 CodeGenModule &CGM = CGF.CGM;
285 llvm::Value *VTable = CGM.getCXXABI().getAddrOfVTable(RD, VPtrOffset: CharUnits());
286 Ty = llvm::PointerType::getUnqual(C&: CGM.getLLVMContext());
287 assert(VTable && "BuildVirtualCall = kext vtbl pointer is null");
288 uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD);
289 const VTableLayout &VTLayout = CGM.getItaniumVTableContext().getVTableLayout(RD);
290 VTableLayout::AddressPointLocation AddressPoint =
291 VTLayout.getAddressPoint(Base: BaseSubobject(RD, CharUnits::Zero()));
292 VTableIndex += VTLayout.getVTableOffset(i: AddressPoint.VTableIndex) +
293 AddressPoint.AddressPointIndex;
294 llvm::Value *VFuncPtr =
295 CGF.Builder.CreateConstInBoundsGEP1_64(Ty, Ptr: VTable, Idx0: VTableIndex, Name: "vfnkxt");
296 llvm::Value *VFunc = CGF.Builder.CreateAlignedLoad(
297 Ty, Ptr: VFuncPtr, Align: llvm::Align(CGF.PointerAlignInBytes));
298
299 CGPointerAuthInfo PointerAuth;
300 if (auto &Schema =
301 CGM.getCodeGenOpts().PointerAuth.CXXVirtualFunctionPointers) {
302 GlobalDecl OrigMD =
303 CGM.getItaniumVTableContext().findOriginalMethod(GD: GD.getCanonicalDecl());
304 PointerAuth = CGF.EmitPointerAuthInfo(Schema, StorageAddress: VFuncPtr, SchemaDecl: OrigMD, SchemaType: QualType());
305 }
306
307 CGCallee Callee(GD, VFunc, PointerAuth);
308 return Callee;
309}
310
311/// BuildAppleKextVirtualCall - This routine is to support gcc's kext ABI making
312/// indirect call to virtual functions. It makes the call through indexing
313/// into the vtable.
314CGCallee CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
315 NestedNameSpecifier Qual,
316 llvm::Type *Ty) {
317 const CXXRecordDecl *RD = Qual.getAsRecordDecl();
318 assert(RD && "BuildAppleKextVirtualCall - Qual must be record");
319 if (const auto *DD = dyn_cast<CXXDestructorDecl>(Val: MD))
320 return BuildAppleKextVirtualDestructorCall(DD, Type: Dtor_Complete, RD);
321
322 return ::BuildAppleKextVirtualCall(CGF&: *this, GD: MD, Ty, RD);
323}
324
325/// BuildVirtualCall - This routine makes indirect vtable call for
326/// call to virtual destructors. It returns 0 if it could not do it.
327CGCallee
328CodeGenFunction::BuildAppleKextVirtualDestructorCall(
329 const CXXDestructorDecl *DD,
330 CXXDtorType Type,
331 const CXXRecordDecl *RD) {
332 assert(DD->isVirtual() && Type != Dtor_Base);
333 // Compute the function type we're calling.
334 const CGFunctionInfo &FInfo = CGM.getTypes().arrangeCXXStructorDeclaration(
335 GD: GlobalDecl(DD, Dtor_Complete));
336 llvm::Type *Ty = CGM.getTypes().GetFunctionType(Info: FInfo);
337 return ::BuildAppleKextVirtualCall(CGF&: *this, GD: GlobalDecl(DD, Type), Ty, RD);
338}
339