1//===--- MicrosoftCXXABI.cpp - Emit LLVM Code from ASTs for a Module ------===//
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 provides C++ code generation targeting the Microsoft Visual C++ ABI.
10// The class in this file generates structures that follow the Microsoft
11// Visual C++ ABI, which is actually not very well documented at all outside
12// of Microsoft.
13//
14//===----------------------------------------------------------------------===//
15
16#include "ABIInfo.h"
17#include "CGCXXABI.h"
18#include "CGCleanup.h"
19#include "CGDebugInfo.h"
20#include "CGVTables.h"
21#include "CodeGenModule.h"
22#include "CodeGenTypes.h"
23#include "TargetInfo.h"
24#include "clang/AST/Attr.h"
25#include "clang/AST/CXXInheritance.h"
26#include "clang/AST/Decl.h"
27#include "clang/AST/DeclCXX.h"
28#include "clang/AST/StmtCXX.h"
29#include "clang/AST/VTableBuilder.h"
30#include "clang/Basic/DiagnosticFrontend.h"
31#include "clang/CodeGen/ConstantInitBuilder.h"
32#include "llvm/ADT/StringExtras.h"
33#include "llvm/ADT/StringSet.h"
34#include "llvm/IR/Intrinsics.h"
35
36using namespace clang;
37using namespace CodeGen;
38
39namespace {
40
41/// Holds all the vbtable globals for a given class.
42struct VBTableGlobals {
43 const VPtrInfoVector *VBTables;
44 SmallVector<llvm::GlobalVariable *, 2> Globals;
45};
46
47class MicrosoftCXXABI : public CGCXXABI {
48public:
49 MicrosoftCXXABI(CodeGenModule &CGM)
50 : CGCXXABI(CGM), BaseClassDescriptorType(nullptr),
51 ClassHierarchyDescriptorType(nullptr),
52 CompleteObjectLocatorType(nullptr), CatchableTypeType(nullptr),
53 ThrowInfoType(nullptr) {
54 assert(!(CGM.getLangOpts().isExplicitDefaultVisibilityExportMapping() ||
55 CGM.getLangOpts().isAllDefaultVisibilityExportMapping()) &&
56 "visibility export mapping option unimplemented in this ABI");
57 }
58
59 bool HasThisReturn(GlobalDecl GD) const override;
60 bool hasMostDerivedReturn(GlobalDecl GD) const override;
61
62 bool classifyReturnType(CGFunctionInfo &FI) const override;
63
64 RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const override;
65
66 bool isSRetParameterAfterThis() const override { return true; }
67
68 bool isThisCompleteObject(GlobalDecl GD) const override {
69 // The Microsoft ABI doesn't use separate complete-object vs.
70 // base-object variants of constructors, but it does of destructors.
71 if (isa<CXXDestructorDecl>(Val: GD.getDecl())) {
72 switch (GD.getDtorType()) {
73 case Dtor_Complete:
74 case Dtor_Deleting:
75 case Dtor_VectorDeleting:
76 return true;
77 case Dtor_Base:
78 return false;
79
80 case Dtor_Comdat: llvm_unreachable("emitting dtor comdat as function?");
81 case Dtor_Unified:
82 llvm_unreachable("unexpected unified dtor type");
83 }
84 llvm_unreachable("bad dtor kind");
85 }
86
87 // No other kinds.
88 return false;
89 }
90
91 size_t getSrcArgforCopyCtor(const CXXConstructorDecl *CD,
92 FunctionArgList &Args) const override {
93 assert(Args.size() >= 2 &&
94 "expected the arglist to have at least two args!");
95 // The 'most_derived' parameter goes second if the ctor is variadic and
96 // has v-bases.
97 if (CD->getParent()->getNumVBases() > 0 &&
98 CD->getType()->castAs<FunctionProtoType>()->isVariadic())
99 return 2;
100 return 1;
101 }
102
103 std::vector<CharUnits> getVBPtrOffsets(const CXXRecordDecl *RD) override {
104 std::vector<CharUnits> VBPtrOffsets;
105 const ASTContext &Context = getContext();
106 const ASTRecordLayout &Layout = Context.getASTRecordLayout(D: RD);
107
108 const VBTableGlobals &VBGlobals = enumerateVBTables(RD);
109 for (const std::unique_ptr<VPtrInfo> &VBT : *VBGlobals.VBTables) {
110 const ASTRecordLayout &SubobjectLayout =
111 Context.getASTRecordLayout(D: VBT->IntroducingObject);
112 CharUnits Offs = VBT->NonVirtualOffset;
113 Offs += SubobjectLayout.getVBPtrOffset();
114 if (VBT->getVBaseWithVPtr())
115 Offs += Layout.getVBaseClassOffset(VBase: VBT->getVBaseWithVPtr());
116 VBPtrOffsets.push_back(x: Offs);
117 }
118 llvm::array_pod_sort(Start: VBPtrOffsets.begin(), End: VBPtrOffsets.end());
119 return VBPtrOffsets;
120 }
121
122 StringRef GetPureVirtualCallName() override { return "_purecall"; }
123 StringRef GetDeletedVirtualCallName() override { return "_purecall"; }
124
125 void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE,
126 Address Ptr, QualType ElementType,
127 const CXXDestructorDecl *Dtor) override;
128
129 void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override;
130 void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) override;
131
132 void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) override;
133
134 llvm::GlobalVariable *getMSCompleteObjectLocator(const CXXRecordDecl *RD,
135 const VPtrInfo &Info);
136
137 llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override;
138 CatchTypeInfo
139 getAddrOfCXXCatchHandlerType(QualType Ty, QualType CatchHandlerType) override;
140
141 /// MSVC needs an extra flag to indicate a catchall.
142 CatchTypeInfo getCatchAllTypeInfo() override {
143 // For -EHa catch(...) must handle HW exception
144 // Adjective = HT_IsStdDotDot (0x40), only catch C++ exceptions
145 if (getContext().getLangOpts().EHAsynch)
146 return CatchTypeInfo{.RTTI: nullptr, .Flags: 0};
147 else
148 return CatchTypeInfo{.RTTI: nullptr, .Flags: 0x40};
149 }
150
151 bool shouldTypeidBeNullChecked(QualType SrcRecordTy) override;
152 void EmitBadTypeidCall(CodeGenFunction &CGF) override;
153 llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy,
154 Address ThisPtr,
155 llvm::Type *StdTypeInfoPtrTy) override;
156
157 bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
158 QualType SrcRecordTy) override;
159
160 bool shouldEmitExactDynamicCast(QualType DestRecordTy) override {
161 // TODO: Add support for exact dynamic_casts.
162 return false;
163 }
164 std::optional<ExactDynamicCastInfo>
165 getExactDynamicCastInfo(QualType SrcRecordTy, QualType DestTy,
166 QualType DestRecordTy) override {
167 llvm_unreachable("unsupported");
168 }
169 llvm::Value *emitExactDynamicCast(CodeGenFunction &CGF, Address Value,
170 QualType SrcRecordTy, QualType DestTy,
171 QualType DestRecordTy,
172 const ExactDynamicCastInfo &CastInfo,
173 llvm::BasicBlock *CastSuccess,
174 llvm::BasicBlock *CastFail) override {
175 llvm_unreachable("unsupported");
176 }
177
178 llvm::Value *emitDynamicCastCall(CodeGenFunction &CGF, Address Value,
179 QualType SrcRecordTy, QualType DestTy,
180 QualType DestRecordTy,
181 llvm::BasicBlock *CastEnd) override;
182
183 llvm::Value *emitDynamicCastToVoid(CodeGenFunction &CGF, Address Value,
184 QualType SrcRecordTy) override;
185
186 bool EmitBadCastCall(CodeGenFunction &CGF) override;
187 bool canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const override {
188 return false;
189 }
190
191 llvm::Value *
192 GetVirtualBaseClassOffset(CodeGenFunction &CGF, Address This,
193 const CXXRecordDecl *ClassDecl,
194 const CXXRecordDecl *BaseClassDecl) override;
195
196 llvm::BasicBlock *
197 EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
198 const CXXRecordDecl *RD) override;
199
200 llvm::BasicBlock *
201 EmitDtorCompleteObjectHandler(CodeGenFunction &CGF);
202
203 void initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF,
204 const CXXRecordDecl *RD) override;
205
206 void EmitCXXConstructors(const CXXConstructorDecl *D) override;
207
208 // Background on MSVC destructors
209 // ==============================
210 //
211 // Both Itanium and MSVC ABIs have destructor variants. The variant names
212 // roughly correspond in the following way:
213 // Itanium Microsoft
214 // Base -> no name, just ~Class
215 // Complete -> vbase destructor
216 // Deleting -> scalar deleting destructor
217 // vector deleting destructor
218 //
219 // The base and complete destructors are the same as in Itanium, although the
220 // complete destructor does not accept a VTT parameter when there are virtual
221 // bases. A separate mechanism involving vtordisps is used to ensure that
222 // virtual methods of destroyed subobjects are not called.
223 //
224 // The deleting destructors accept an i32 bitfield as a second parameter. Bit
225 // 1 indicates if the memory should be deleted. Bit 2 indicates if the this
226 // pointer points to an array. The scalar deleting destructor assumes that
227 // bit 2 is zero, and therefore does not contain a loop.
228 //
229 // For virtual destructors, only one entry is reserved in the vftable, and it
230 // always points to the vector deleting destructor. The vector deleting
231 // destructor is the most general, so it can be used to destroy objects in
232 // place, delete single heap objects, or delete arrays.
233 //
234 // A TU defining a non-inline destructor is only guaranteed to emit a base
235 // destructor, and all of the other variants are emitted on an as-needed basis
236 // in COMDATs. Because a non-base destructor can be emitted in a TU that
237 // lacks a definition for the destructor, non-base destructors must always
238 // delegate to or alias the base destructor.
239
240 AddedStructorArgCounts
241 buildStructorSignature(GlobalDecl GD,
242 SmallVectorImpl<CanQualType> &ArgTys) override;
243
244 /// Non-base dtors should be emitted as delegating thunks in this ABI.
245 bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
246 CXXDtorType DT) const override {
247 return DT != Dtor_Base;
248 }
249
250 void setCXXDestructorDLLStorage(llvm::GlobalValue *GV,
251 const CXXDestructorDecl *Dtor,
252 CXXDtorType DT) const override;
253
254 llvm::GlobalValue::LinkageTypes
255 getCXXDestructorLinkage(GVALinkage Linkage, const CXXDestructorDecl *Dtor,
256 CXXDtorType DT) const override;
257
258 void EmitCXXDestructors(const CXXDestructorDecl *D) override;
259
260 const CXXRecordDecl *getThisArgumentTypeForMethod(GlobalDecl GD) override {
261 auto *MD = cast<CXXMethodDecl>(Val: GD.getDecl());
262
263 if (MD->isVirtual()) {
264 GlobalDecl LookupGD = GD;
265 if (const auto *DD = dyn_cast<CXXDestructorDecl>(Val: MD)) {
266 // Complete dtors take a pointer to the complete object,
267 // thus don't need adjustment.
268 if (GD.getDtorType() == Dtor_Complete)
269 return MD->getParent();
270
271 // There's only Dtor_Deleting in vftable but it shares the this
272 // adjustment with the base one, so look up the deleting one instead.
273 LookupGD = GlobalDecl(
274 DD, CGM.getContext().getTargetInfo().emitVectorDeletingDtors(
275 CGM.getContext().getLangOpts())
276 ? Dtor_VectorDeleting
277 : Dtor_Deleting);
278 }
279 MethodVFTableLocation ML =
280 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD: LookupGD);
281
282 // The vbases might be ordered differently in the final overrider object
283 // and the complete object, so the "this" argument may sometimes point to
284 // memory that has no particular type (e.g. past the complete object).
285 // In this case, we just use a generic pointer type.
286 // FIXME: might want to have a more precise type in the non-virtual
287 // multiple inheritance case.
288 if (ML.VBase || !ML.VFPtrOffset.isZero())
289 return nullptr;
290 }
291 return MD->getParent();
292 }
293
294 Address
295 adjustThisArgumentForVirtualFunctionCall(CodeGenFunction &CGF, GlobalDecl GD,
296 Address This,
297 bool VirtualCall) override;
298
299 void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy,
300 FunctionArgList &Params) override;
301
302 void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override;
303
304 AddedStructorArgs getImplicitConstructorArgs(CodeGenFunction &CGF,
305 const CXXConstructorDecl *D,
306 CXXCtorType Type,
307 bool ForVirtualBase,
308 bool Delegating) override;
309
310 llvm::Value *getCXXDestructorImplicitParam(CodeGenFunction &CGF,
311 const CXXDestructorDecl *DD,
312 CXXDtorType Type,
313 bool ForVirtualBase,
314 bool Delegating) override;
315
316 void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD,
317 CXXDtorType Type, bool ForVirtualBase,
318 bool Delegating, Address This,
319 QualType ThisTy) override;
320
321 void emitVTableTypeMetadata(const VPtrInfo &Info, const CXXRecordDecl *RD,
322 llvm::GlobalVariable *VTable);
323
324 void emitVTableDefinitions(CodeGenVTables &CGVT,
325 const CXXRecordDecl *RD) override;
326
327 bool isVirtualOffsetNeededForVTableField(CodeGenFunction &CGF,
328 CodeGenFunction::VPtr Vptr) override;
329
330 /// Don't initialize vptrs if dynamic class
331 /// is marked with the 'novtable' attribute.
332 bool doStructorsInitializeVPtrs(const CXXRecordDecl *VTableClass) override {
333 return !VTableClass->hasAttr<MSNoVTableAttr>();
334 }
335
336 llvm::Constant *
337 getVTableAddressPoint(BaseSubobject Base,
338 const CXXRecordDecl *VTableClass) override;
339
340 llvm::Value *getVTableAddressPointInStructor(
341 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass,
342 BaseSubobject Base, const CXXRecordDecl *NearestVBase) override;
343
344 llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
345 CharUnits VPtrOffset) override;
346
347 CGCallee getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD,
348 Address This, llvm::Type *Ty,
349 SourceLocation Loc) override;
350
351 llvm::Value *
352 EmitVirtualDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *Dtor,
353 CXXDtorType DtorType, Address This,
354 DeleteOrMemberCallExpr E,
355 llvm::CallBase **CallOrInvoke) override;
356
357 void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF, GlobalDecl GD,
358 CallArgList &CallArgs) override {
359 assert((GD.getDtorType() == Dtor_VectorDeleting ||
360 GD.getDtorType() == Dtor_Deleting) &&
361 "Only vector deleting destructor thunks are available in this ABI");
362 CallArgs.add(rvalue: RValue::get(V: getStructorImplicitParamValue(CGF)),
363 type: getContext().IntTy);
364 }
365
366 void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override;
367
368 llvm::GlobalVariable *
369 getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD,
370 llvm::GlobalVariable::LinkageTypes Linkage);
371
372 llvm::GlobalVariable *
373 getAddrOfVirtualDisplacementMap(const CXXRecordDecl *SrcRD,
374 const CXXRecordDecl *DstRD) {
375 SmallString<256> OutName;
376 llvm::raw_svector_ostream Out(OutName);
377 getMangleContext().mangleCXXVirtualDisplacementMap(SrcRD, DstRD, Out);
378 StringRef MangledName = OutName.str();
379
380 if (auto *VDispMap = CGM.getModule().getNamedGlobal(Name: MangledName))
381 return VDispMap;
382
383 MicrosoftVTableContext &VTContext = CGM.getMicrosoftVTableContext();
384 unsigned NumEntries = 1 + SrcRD->getNumVBases();
385 SmallVector<llvm::Constant *, 4> Map(NumEntries,
386 llvm::PoisonValue::get(T: CGM.IntTy));
387 Map[0] = llvm::ConstantInt::get(Ty: CGM.IntTy, V: 0);
388 bool AnyDifferent = false;
389 for (const auto &I : SrcRD->vbases()) {
390 const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl();
391 if (!DstRD->isVirtuallyDerivedFrom(Base: VBase))
392 continue;
393
394 unsigned SrcVBIndex = VTContext.getVBTableIndex(Derived: SrcRD, VBase);
395 unsigned DstVBIndex = VTContext.getVBTableIndex(Derived: DstRD, VBase);
396 Map[SrcVBIndex] = llvm::ConstantInt::get(Ty: CGM.IntTy, V: DstVBIndex * 4);
397 AnyDifferent |= SrcVBIndex != DstVBIndex;
398 }
399 // This map would be useless, don't use it.
400 if (!AnyDifferent)
401 return nullptr;
402
403 llvm::ArrayType *VDispMapTy = llvm::ArrayType::get(ElementType: CGM.IntTy, NumElements: Map.size());
404 llvm::Constant *Init = llvm::ConstantArray::get(T: VDispMapTy, V: Map);
405 llvm::GlobalValue::LinkageTypes Linkage =
406 SrcRD->isExternallyVisible() && DstRD->isExternallyVisible()
407 ? llvm::GlobalValue::LinkOnceODRLinkage
408 : llvm::GlobalValue::InternalLinkage;
409 auto *VDispMap = new llvm::GlobalVariable(
410 CGM.getModule(), VDispMapTy, /*isConstant=*/true, Linkage,
411 /*Initializer=*/Init, MangledName);
412 return VDispMap;
413 }
414
415 void emitVBTableDefinition(const VPtrInfo &VBT, const CXXRecordDecl *RD,
416 llvm::GlobalVariable *GV) const;
417
418 void setThunkLinkage(llvm::Function *Thunk, bool ForVTable,
419 GlobalDecl GD, bool ReturnAdjustment) override {
420 GVALinkage Linkage =
421 getContext().GetGVALinkageForFunction(FD: cast<FunctionDecl>(Val: GD.getDecl()));
422
423 if (Linkage == GVA_Internal)
424 Thunk->setLinkage(llvm::GlobalValue::InternalLinkage);
425 else if (ReturnAdjustment)
426 Thunk->setLinkage(llvm::GlobalValue::WeakODRLinkage);
427 else
428 Thunk->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage);
429 }
430
431 bool exportThunk() override { return false; }
432
433 llvm::Value *performThisAdjustment(CodeGenFunction &CGF, Address This,
434 const CXXRecordDecl * /*UnadjustedClass*/,
435 const ThunkInfo &TI) override;
436
437 llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, Address Ret,
438 const CXXRecordDecl * /*UnadjustedClass*/,
439 const ReturnAdjustment &RA) override;
440
441 void EmitThreadLocalInitFuncs(
442 CodeGenModule &CGM, ArrayRef<const VarDecl *> CXXThreadLocals,
443 ArrayRef<llvm::Function *> CXXThreadLocalInits,
444 ArrayRef<const VarDecl *> CXXThreadLocalInitVars) override;
445
446 bool usesThreadWrapperFunction(const VarDecl *VD) const override {
447 return getContext().getLangOpts().isCompatibleWithMSVC(
448 MajorVersion: LangOptions::MSVC2019_5) &&
449 CGM.getCodeGenOpts().TlsGuards &&
450 (!isEmittedWithConstantInitializer(VD) || mayNeedDestruction(VD));
451 }
452 LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD,
453 QualType LValType) override;
454
455 void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
456 llvm::GlobalVariable *DeclPtr,
457 bool PerformInit) override;
458 void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
459 llvm::FunctionCallee Dtor,
460 llvm::Constant *Addr) override;
461
462 // ==== Notes on array cookies =========
463 //
464 // MSVC seems to only use cookies when the class has a destructor; a
465 // two-argument usual array deallocation function isn't sufficient.
466 //
467 // For example, this code prints "100" and "1":
468 // struct A {
469 // char x;
470 // void *operator new[](size_t sz) {
471 // printf("%u\n", sz);
472 // return malloc(sz);
473 // }
474 // void operator delete[](void *p, size_t sz) {
475 // printf("%u\n", sz);
476 // free(p);
477 // }
478 // };
479 // int main() {
480 // A *p = new A[100];
481 // delete[] p;
482 // }
483 // Whereas it prints "104" and "104" if you give A a destructor.
484
485 bool requiresArrayCookie(const CXXDeleteExpr *expr,
486 QualType elementType) override;
487 bool requiresArrayCookie(const CXXNewExpr *expr) override;
488 CharUnits getArrayCookieSizeImpl(QualType type) override;
489 Address InitializeArrayCookie(CodeGenFunction &CGF,
490 Address NewPtr,
491 llvm::Value *NumElements,
492 const CXXNewExpr *expr,
493 QualType ElementType) override;
494 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
495 Address allocPtr,
496 CharUnits cookieSize) override;
497
498 friend struct MSRTTIBuilder;
499
500 bool isImageRelative() const {
501 return CGM.getTarget().getPointerWidth(AddrSpace: LangAS::Default) == 64;
502 }
503
504 // 5 routines for constructing the llvm types for MS RTTI structs.
505 llvm::StructType *getTypeDescriptorType(StringRef TypeInfoString) {
506 llvm::SmallString<32> TDTypeName("rtti.TypeDescriptor");
507 TDTypeName += llvm::utostr(X: TypeInfoString.size());
508 llvm::StructType *&TypeDescriptorType =
509 TypeDescriptorTypeMap[TypeInfoString.size()];
510 if (TypeDescriptorType)
511 return TypeDescriptorType;
512 llvm::Type *FieldTypes[] = {
513 CGM.Int8PtrPtrTy,
514 CGM.Int8PtrTy,
515 llvm::ArrayType::get(ElementType: CGM.Int8Ty, NumElements: TypeInfoString.size() + 1)};
516 TypeDescriptorType =
517 llvm::StructType::create(Context&: CGM.getLLVMContext(), Elements: FieldTypes, Name: TDTypeName);
518 return TypeDescriptorType;
519 }
520
521 llvm::Type *getImageRelativeType(llvm::Type *PtrType) {
522 if (!isImageRelative())
523 return PtrType;
524 return CGM.IntTy;
525 }
526
527 llvm::StructType *getBaseClassDescriptorType() {
528 if (BaseClassDescriptorType)
529 return BaseClassDescriptorType;
530 llvm::Type *FieldTypes[] = {
531 getImageRelativeType(PtrType: CGM.Int8PtrTy),
532 CGM.IntTy,
533 CGM.IntTy,
534 CGM.IntTy,
535 CGM.IntTy,
536 CGM.IntTy,
537 getImageRelativeType(PtrType: CGM.DefaultPtrTy),
538 };
539 BaseClassDescriptorType = llvm::StructType::create(
540 Context&: CGM.getLLVMContext(), Elements: FieldTypes, Name: "rtti.BaseClassDescriptor");
541 return BaseClassDescriptorType;
542 }
543
544 llvm::StructType *getClassHierarchyDescriptorType() {
545 if (ClassHierarchyDescriptorType)
546 return ClassHierarchyDescriptorType;
547 // Forward-declare RTTIClassHierarchyDescriptor to break a cycle.
548 llvm::Type *FieldTypes[] = {CGM.IntTy, CGM.IntTy, CGM.IntTy,
549 getImageRelativeType(PtrType: CGM.DefaultPtrTy)};
550 ClassHierarchyDescriptorType =
551 llvm::StructType::create(Elements: FieldTypes, Name: "rtti.ClassHierarchyDescriptor");
552 return ClassHierarchyDescriptorType;
553 }
554
555 llvm::StructType *getCompleteObjectLocatorType() {
556 if (CompleteObjectLocatorType)
557 return CompleteObjectLocatorType;
558 llvm::Type *FieldTypes[] = {
559 CGM.IntTy,
560 CGM.IntTy,
561 CGM.IntTy,
562 getImageRelativeType(PtrType: CGM.Int8PtrTy),
563 getImageRelativeType(PtrType: CGM.DefaultPtrTy),
564 getImageRelativeType(PtrType: CGM.VoidTy),
565 };
566 llvm::ArrayRef<llvm::Type *> FieldTypesRef(FieldTypes);
567 if (!isImageRelative())
568 FieldTypesRef = FieldTypesRef.drop_back();
569 CompleteObjectLocatorType =
570 llvm::StructType::create(Elements: FieldTypesRef, Name: "rtti.CompleteObjectLocator");
571 return CompleteObjectLocatorType;
572 }
573
574 llvm::GlobalVariable *getImageBase() {
575 StringRef Name = "__ImageBase";
576 if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name))
577 return GV;
578
579 auto *GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty,
580 /*isConstant=*/true,
581 llvm::GlobalValue::ExternalLinkage,
582 /*Initializer=*/nullptr, Name);
583 CGM.setDSOLocal(GV);
584 return GV;
585 }
586
587 llvm::Constant *getImageRelativeConstant(llvm::Constant *PtrVal) {
588 if (!isImageRelative())
589 return PtrVal;
590
591 if (PtrVal->isNullValue())
592 return llvm::Constant::getNullValue(Ty: CGM.IntTy);
593
594 llvm::Constant *ImageBaseAsInt =
595 llvm::ConstantExpr::getPtrToInt(C: getImageBase(), Ty: CGM.IntPtrTy);
596 llvm::Constant *PtrValAsInt =
597 llvm::ConstantExpr::getPtrToInt(C: PtrVal, Ty: CGM.IntPtrTy);
598 llvm::Constant *Diff =
599 llvm::ConstantExpr::getSub(C1: PtrValAsInt, C2: ImageBaseAsInt,
600 /*HasNUW=*/true, /*HasNSW=*/true);
601 return llvm::ConstantExpr::getTrunc(C: Diff, Ty: CGM.IntTy);
602 }
603
604private:
605 MicrosoftMangleContext &getMangleContext() {
606 return cast<MicrosoftMangleContext>(Val&: CodeGen::CGCXXABI::getMangleContext());
607 }
608
609 llvm::Constant *getZeroInt() {
610 return llvm::ConstantInt::get(Ty: CGM.IntTy, V: 0);
611 }
612
613 llvm::Constant *getAllOnesInt() {
614 return llvm::Constant::getAllOnesValue(Ty: CGM.IntTy);
615 }
616
617 CharUnits getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD) override;
618
619 void
620 GetNullMemberPointerFields(const MemberPointerType *MPT,
621 llvm::SmallVectorImpl<llvm::Constant *> &fields);
622
623 /// Shared code for virtual base adjustment. Returns the offset from
624 /// the vbptr to the virtual base. Optionally returns the address of the
625 /// vbptr itself.
626 llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF,
627 Address Base,
628 llvm::Value *VBPtrOffset,
629 llvm::Value *VBTableOffset,
630 llvm::Value **VBPtr = nullptr);
631
632 llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF,
633 Address Base,
634 int32_t VBPtrOffset,
635 int32_t VBTableOffset,
636 llvm::Value **VBPtr = nullptr) {
637 assert(VBTableOffset % 4 == 0 && "should be byte offset into table of i32s");
638 llvm::Value *VBPOffset =
639 llvm::ConstantInt::getSigned(Ty: CGM.IntTy, V: VBPtrOffset);
640 llvm::Value *VBTOffset = llvm::ConstantInt::get(Ty: CGM.IntTy, V: VBTableOffset);
641 return GetVBaseOffsetFromVBPtr(CGF, Base, VBPtrOffset: VBPOffset, VBTableOffset: VBTOffset, VBPtr);
642 }
643
644 std::tuple<Address, llvm::Value *, const CXXRecordDecl *>
645 performBaseAdjustment(CodeGenFunction &CGF, Address Value,
646 QualType SrcRecordTy);
647
648 /// Performs a full virtual base adjustment. Used to dereference
649 /// pointers to members of virtual bases.
650 llvm::Value *AdjustVirtualBase(CodeGenFunction &CGF, const Expr *E,
651 const CXXRecordDecl *RD, Address Base,
652 llvm::Value *VirtualBaseAdjustmentOffset,
653 llvm::Value *VBPtrOffset /* optional */);
654
655 /// Emits a full member pointer with the fields common to data and
656 /// function member pointers.
657 llvm::Constant *EmitFullMemberPointer(llvm::Constant *FirstField,
658 bool IsMemberFunction,
659 const CXXRecordDecl *RD,
660 CharUnits NonVirtualBaseAdjustment,
661 unsigned VBTableIndex);
662
663 bool MemberPointerConstantIsNull(const MemberPointerType *MPT,
664 llvm::Constant *MP);
665
666 /// - Initialize all vbptrs of 'this' with RD as the complete type.
667 void EmitVBPtrStores(CodeGenFunction &CGF, const CXXRecordDecl *RD);
668
669 /// Caching wrapper around VBTableBuilder::enumerateVBTables().
670 const VBTableGlobals &enumerateVBTables(const CXXRecordDecl *RD);
671
672 /// Generate a thunk for calling a virtual member function MD.
673 llvm::Function *EmitVirtualMemPtrThunk(const CXXMethodDecl *MD,
674 const MethodVFTableLocation &ML);
675
676 llvm::Constant *EmitMemberDataPointer(const CXXRecordDecl *RD,
677 CharUnits offset);
678
679public:
680 llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT) override;
681
682 bool isZeroInitializable(const MemberPointerType *MPT) override;
683
684 bool isMemberPointerConvertible(const MemberPointerType *MPT) const override {
685 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
686 return RD->hasAttr<MSInheritanceAttr>();
687 }
688
689 llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT) override;
690
691 llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
692 CharUnits offset) override;
693 llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD) override;
694 llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT) override;
695
696 llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
697 llvm::Value *L,
698 llvm::Value *R,
699 const MemberPointerType *MPT,
700 bool Inequality) override;
701
702 llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
703 llvm::Value *MemPtr,
704 const MemberPointerType *MPT) override;
705
706 llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
707 Address Base, llvm::Value *MemPtr,
708 const MemberPointerType *MPT,
709 bool IsInBounds) override;
710
711 llvm::Value *EmitNonNullMemberPointerConversion(
712 const MemberPointerType *SrcTy, const MemberPointerType *DstTy,
713 CastKind CK, CastExpr::path_const_iterator PathBegin,
714 CastExpr::path_const_iterator PathEnd, llvm::Value *Src,
715 CGBuilderTy &Builder);
716
717 llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
718 const CastExpr *E,
719 llvm::Value *Src) override;
720
721 llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
722 llvm::Constant *Src) override;
723
724 llvm::Constant *EmitMemberPointerConversion(
725 const MemberPointerType *SrcTy, const MemberPointerType *DstTy,
726 CastKind CK, CastExpr::path_const_iterator PathBegin,
727 CastExpr::path_const_iterator PathEnd, llvm::Constant *Src);
728
729 CGCallee
730 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, const Expr *E,
731 Address This, llvm::Value *&ThisPtrForCall,
732 llvm::Value *MemPtr,
733 const MemberPointerType *MPT) override;
734
735 void emitCXXStructor(GlobalDecl GD) override;
736
737 llvm::StructType *getCatchableTypeType() {
738 if (CatchableTypeType)
739 return CatchableTypeType;
740 llvm::Type *FieldTypes[] = {
741 CGM.IntTy, // Flags
742 getImageRelativeType(PtrType: CGM.Int8PtrTy), // TypeDescriptor
743 CGM.IntTy, // NonVirtualAdjustment
744 CGM.IntTy, // OffsetToVBPtr
745 CGM.IntTy, // VBTableIndex
746 CGM.IntTy, // Size
747 getImageRelativeType(PtrType: CGM.Int8PtrTy) // CopyCtor
748 };
749 CatchableTypeType = llvm::StructType::create(
750 Context&: CGM.getLLVMContext(), Elements: FieldTypes, Name: "eh.CatchableType");
751 return CatchableTypeType;
752 }
753
754 llvm::StructType *getCatchableTypeArrayType(uint32_t NumEntries) {
755 llvm::StructType *&CatchableTypeArrayType =
756 CatchableTypeArrayTypeMap[NumEntries];
757 if (CatchableTypeArrayType)
758 return CatchableTypeArrayType;
759
760 llvm::SmallString<23> CTATypeName("eh.CatchableTypeArray.");
761 CTATypeName += llvm::utostr(X: NumEntries);
762 llvm::Type *CTType = getImageRelativeType(PtrType: CGM.DefaultPtrTy);
763 llvm::Type *FieldTypes[] = {
764 CGM.IntTy, // NumEntries
765 llvm::ArrayType::get(ElementType: CTType, NumElements: NumEntries) // CatchableTypes
766 };
767 CatchableTypeArrayType =
768 llvm::StructType::create(Context&: CGM.getLLVMContext(), Elements: FieldTypes, Name: CTATypeName);
769 return CatchableTypeArrayType;
770 }
771
772 llvm::StructType *getThrowInfoType() {
773 if (ThrowInfoType)
774 return ThrowInfoType;
775 llvm::Type *FieldTypes[] = {
776 CGM.IntTy, // Flags
777 getImageRelativeType(PtrType: CGM.Int8PtrTy), // CleanupFn
778 getImageRelativeType(PtrType: CGM.Int8PtrTy), // ForwardCompat
779 getImageRelativeType(PtrType: CGM.Int8PtrTy) // CatchableTypeArray
780 };
781 ThrowInfoType = llvm::StructType::create(Context&: CGM.getLLVMContext(), Elements: FieldTypes,
782 Name: "eh.ThrowInfo");
783 return ThrowInfoType;
784 }
785
786 llvm::FunctionCallee getThrowFn() {
787 // _CxxThrowException is passed an exception object and a ThrowInfo object
788 // which describes the exception.
789 llvm::Type *Args[] = {CGM.Int8PtrTy, CGM.DefaultPtrTy};
790 llvm::FunctionType *FTy =
791 llvm::FunctionType::get(Result: CGM.VoidTy, Params: Args, /*isVarArg=*/false);
792 llvm::FunctionCallee Throw =
793 CGM.CreateRuntimeFunction(Ty: FTy, Name: "_CxxThrowException");
794 // _CxxThrowException is stdcall on 32-bit x86 platforms.
795 if (CGM.getTarget().getTriple().getArch() == llvm::Triple::x86) {
796 if (auto *Fn = dyn_cast<llvm::Function>(Val: Throw.getCallee()))
797 Fn->setCallingConv(llvm::CallingConv::X86_StdCall);
798 }
799 return Throw;
800 }
801
802 llvm::Function *getAddrOfCXXCtorClosure(const CXXConstructorDecl *CD,
803 CXXCtorType CT);
804
805 llvm::Constant *getCatchableType(QualType T,
806 uint32_t NVOffset = 0,
807 int32_t VBPtrOffset = -1,
808 uint32_t VBIndex = 0);
809
810 llvm::GlobalVariable *getCatchableTypeArray(QualType T);
811
812 llvm::GlobalVariable *getThrowInfo(QualType T) override;
813
814 std::pair<llvm::Value *, const CXXRecordDecl *>
815 LoadVTablePtr(CodeGenFunction &CGF, Address This,
816 const CXXRecordDecl *RD) override;
817
818 bool
819 isPermittedToBeHomogeneousAggregate(const CXXRecordDecl *RD) const override;
820
821private:
822 typedef std::pair<const CXXRecordDecl *, CharUnits> VFTableIdTy;
823 typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalVariable *> VTablesMapTy;
824 typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalValue *> VFTablesMapTy;
825 /// All the vftables that have been referenced.
826 VFTablesMapTy VFTablesMap;
827 VTablesMapTy VTablesMap;
828
829 /// This set holds the record decls we've deferred vtable emission for.
830 llvm::SmallPtrSet<const CXXRecordDecl *, 4> DeferredVFTables;
831
832
833 /// All the vbtables which have been referenced.
834 llvm::DenseMap<const CXXRecordDecl *, VBTableGlobals> VBTablesMap;
835
836 /// Info on the global variable used to guard initialization of static locals.
837 /// The BitIndex field is only used for externally invisible declarations.
838 struct GuardInfo {
839 GuardInfo() = default;
840 llvm::GlobalVariable *Guard = nullptr;
841 unsigned BitIndex = 0;
842 };
843
844 /// Map from DeclContext to the current guard variable. We assume that the
845 /// AST is visited in source code order.
846 llvm::DenseMap<const DeclContext *, GuardInfo> GuardVariableMap;
847 llvm::DenseMap<const DeclContext *, GuardInfo> ThreadLocalGuardVariableMap;
848 llvm::DenseMap<const DeclContext *, unsigned> ThreadSafeGuardNumMap;
849
850 llvm::DenseMap<size_t, llvm::StructType *> TypeDescriptorTypeMap;
851 llvm::StructType *BaseClassDescriptorType;
852 llvm::StructType *ClassHierarchyDescriptorType;
853 llvm::StructType *CompleteObjectLocatorType;
854
855 llvm::DenseMap<QualType, llvm::GlobalVariable *> CatchableTypeArrays;
856
857 llvm::StructType *CatchableTypeType;
858 llvm::DenseMap<uint32_t, llvm::StructType *> CatchableTypeArrayTypeMap;
859 llvm::StructType *ThrowInfoType;
860};
861
862}
863
864CGCXXABI::RecordArgABI
865MicrosoftCXXABI::getRecordArgABI(const CXXRecordDecl *RD) const {
866 // Use the default C calling convention rules for things that can be passed in
867 // registers, i.e. non-trivially copyable records or records marked with
868 // [[trivial_abi]].
869 if (RD->canPassInRegisters())
870 return RAA_Default;
871
872 switch (CGM.getTarget().getTriple().getArch()) {
873 default:
874 // FIXME: Implement for other architectures.
875 return RAA_Indirect;
876
877 case llvm::Triple::thumb:
878 // Pass things indirectly for now because it is simple.
879 // FIXME: This is incompatible with MSVC for arguments with a dtor and no
880 // copy ctor.
881 return RAA_Indirect;
882
883 case llvm::Triple::x86: {
884 // If the argument has *required* alignment greater than four bytes, pass
885 // it indirectly. Prior to MSVC version 19.14, passing overaligned
886 // arguments was not supported and resulted in a compiler error. In 19.14
887 // and later versions, such arguments are now passed indirectly.
888 TypeInfo Info =
889 getContext().getTypeInfo(T: getContext().getCanonicalTagType(TD: RD));
890 if (Info.isAlignRequired() && Info.Align > 4)
891 return RAA_Indirect;
892
893 // If C++ prohibits us from making a copy, construct the arguments directly
894 // into argument memory.
895 return RAA_DirectInMemory;
896 }
897
898 case llvm::Triple::x86_64:
899 case llvm::Triple::aarch64:
900 return RAA_Indirect;
901 }
902
903 llvm_unreachable("invalid enum");
904}
905
906void MicrosoftCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF,
907 const CXXDeleteExpr *DE,
908 Address Ptr,
909 QualType ElementType,
910 const CXXDestructorDecl *Dtor) {
911 // FIXME: Provide a source location here even though there's no
912 // CXXMemberCallExpr for dtor call.
913 if (!getContext().getTargetInfo().callGlobalDeleteInDeletingDtor(
914 getContext().getLangOpts())) {
915 bool UseGlobalDelete = DE->isGlobalDelete();
916 CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting;
917 llvm::Value *MDThis =
918 EmitVirtualDestructorCall(CGF, Dtor, DtorType, This: Ptr, E: DE,
919 /*CallOrInvoke=*/nullptr);
920 if (UseGlobalDelete)
921 CGF.EmitDeleteCall(DeleteFD: DE->getOperatorDelete(), Ptr: MDThis, DeleteTy: ElementType);
922 } else {
923 EmitVirtualDestructorCall(CGF, Dtor, DtorType: Dtor_Deleting, This: Ptr, E: DE,
924 /*CallOrInvoke=*/nullptr);
925 }
926}
927
928void MicrosoftCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) {
929 llvm::Value *Args[] = {llvm::ConstantPointerNull::get(T: CGM.Int8PtrTy),
930 llvm::ConstantPointerNull::get(T: CGM.DefaultPtrTy)};
931 llvm::FunctionCallee Fn = getThrowFn();
932 if (isNoReturn)
933 CGF.EmitNoreturnRuntimeCallOrInvoke(callee: Fn, args: Args);
934 else
935 CGF.EmitRuntimeCallOrInvoke(callee: Fn, args: Args);
936}
937
938void MicrosoftCXXABI::emitBeginCatch(CodeGenFunction &CGF,
939 const CXXCatchStmt *S) {
940 // In the MS ABI, the runtime handles the copy, and the catch handler is
941 // responsible for destruction.
942 VarDecl *CatchParam = S->getExceptionDecl();
943 llvm::BasicBlock *CatchPadBB = CGF.Builder.GetInsertBlock();
944 llvm::CatchPadInst *CPI =
945 cast<llvm::CatchPadInst>(Val: CatchPadBB->getFirstNonPHIIt());
946 CGF.CurrentFuncletPad = CPI;
947
948 // If this is a catch-all or the catch parameter is unnamed, we don't need to
949 // emit an alloca to the object.
950 if (!CatchParam || !CatchParam->getDeclName()) {
951 CGF.EHStack.pushCleanup<CatchRetScope>(Kind: NormalCleanup, A: CPI);
952 return;
953 }
954
955 CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(var: *CatchParam);
956 CPI->setArgOperand(i: 2, v: var.getObjectAddress(CGF).emitRawPointer(CGF));
957 CGF.EHStack.pushCleanup<CatchRetScope>(Kind: NormalCleanup, A: CPI);
958 CGF.EmitAutoVarCleanups(emission: var);
959}
960
961/// We need to perform a generic polymorphic operation (like a typeid
962/// or a cast), which requires an object with a vfptr. Adjust the
963/// address to point to an object with a vfptr.
964std::tuple<Address, llvm::Value *, const CXXRecordDecl *>
965MicrosoftCXXABI::performBaseAdjustment(CodeGenFunction &CGF, Address Value,
966 QualType SrcRecordTy) {
967 Value = Value.withElementType(ElemTy: CGF.Int8Ty);
968 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
969 const ASTContext &Context = getContext();
970
971 // If the class itself has a vfptr, great. This check implicitly
972 // covers non-virtual base subobjects: a class with its own virtual
973 // functions would be a candidate to be a primary base.
974 if (Context.getASTRecordLayout(D: SrcDecl).hasExtendableVFPtr())
975 return std::make_tuple(args&: Value, args: llvm::ConstantInt::get(Ty: CGF.Int32Ty, V: 0),
976 args&: SrcDecl);
977
978 // Okay, one of the vbases must have a vfptr, or else this isn't
979 // actually a polymorphic class.
980 const CXXRecordDecl *PolymorphicBase = nullptr;
981 for (auto &Base : SrcDecl->vbases()) {
982 const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
983 if (Context.getASTRecordLayout(D: BaseDecl).hasExtendableVFPtr()) {
984 PolymorphicBase = BaseDecl;
985 break;
986 }
987 }
988 assert(PolymorphicBase && "polymorphic class has no apparent vfptr?");
989
990 llvm::Value *Offset =
991 GetVirtualBaseClassOffset(CGF, This: Value, ClassDecl: SrcDecl, BaseClassDecl: PolymorphicBase);
992 llvm::Value *Ptr = CGF.Builder.CreateInBoundsGEP(
993 Ty: Value.getElementType(), Ptr: Value.emitRawPointer(CGF), IdxList: Offset);
994 CharUnits VBaseAlign =
995 CGF.CGM.getVBaseAlignment(DerivedAlign: Value.getAlignment(), Derived: SrcDecl, VBase: PolymorphicBase);
996 return std::make_tuple(args: Address(Ptr, CGF.Int8Ty, VBaseAlign), args&: Offset,
997 args&: PolymorphicBase);
998}
999
1000bool MicrosoftCXXABI::shouldTypeidBeNullChecked(QualType SrcRecordTy) {
1001 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
1002 return !getContext().getASTRecordLayout(D: SrcDecl).hasExtendableVFPtr();
1003}
1004
1005static llvm::CallBase *emitRTtypeidCall(CodeGenFunction &CGF,
1006 llvm::Value *Argument) {
1007 llvm::Type *ArgTypes[] = {CGF.Int8PtrTy};
1008 llvm::FunctionType *FTy =
1009 llvm::FunctionType::get(Result: CGF.Int8PtrTy, Params: ArgTypes, isVarArg: false);
1010 llvm::Value *Args[] = {Argument};
1011 llvm::FunctionCallee Fn = CGF.CGM.CreateRuntimeFunction(Ty: FTy, Name: "__RTtypeid");
1012 return CGF.EmitRuntimeCallOrInvoke(callee: Fn, args: Args);
1013}
1014
1015void MicrosoftCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) {
1016 llvm::CallBase *Call =
1017 emitRTtypeidCall(CGF, Argument: llvm::Constant::getNullValue(Ty: CGM.VoidPtrTy));
1018 Call->setDoesNotReturn();
1019 CGF.Builder.CreateUnreachable();
1020}
1021
1022llvm::Value *MicrosoftCXXABI::EmitTypeid(CodeGenFunction &CGF,
1023 QualType SrcRecordTy,
1024 Address ThisPtr,
1025 llvm::Type *StdTypeInfoPtrTy) {
1026 std::tie(args&: ThisPtr, args: std::ignore, args: std::ignore) =
1027 performBaseAdjustment(CGF, Value: ThisPtr, SrcRecordTy);
1028 llvm::CallBase *Typeid = emitRTtypeidCall(CGF, Argument: ThisPtr.emitRawPointer(CGF));
1029 return CGF.Builder.CreateBitCast(V: Typeid, DestTy: StdTypeInfoPtrTy);
1030}
1031
1032bool MicrosoftCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
1033 QualType SrcRecordTy) {
1034 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
1035 return SrcIsPtr &&
1036 !getContext().getASTRecordLayout(D: SrcDecl).hasExtendableVFPtr();
1037}
1038
1039llvm::Value *MicrosoftCXXABI::emitDynamicCastCall(
1040 CodeGenFunction &CGF, Address This, QualType SrcRecordTy, QualType DestTy,
1041 QualType DestRecordTy, llvm::BasicBlock *CastEnd) {
1042 llvm::Value *SrcRTTI =
1043 CGF.CGM.GetAddrOfRTTIDescriptor(Ty: SrcRecordTy.getUnqualifiedType());
1044 llvm::Value *DestRTTI =
1045 CGF.CGM.GetAddrOfRTTIDescriptor(Ty: DestRecordTy.getUnqualifiedType());
1046
1047 llvm::Value *Offset;
1048 std::tie(args&: This, args&: Offset, args: std::ignore) =
1049 performBaseAdjustment(CGF, Value: This, SrcRecordTy);
1050 llvm::Value *ThisPtr = This.emitRawPointer(CGF);
1051 Offset = CGF.Builder.CreateTrunc(V: Offset, DestTy: CGF.Int32Ty);
1052
1053 // PVOID __RTDynamicCast(
1054 // PVOID inptr,
1055 // LONG VfDelta,
1056 // PVOID SrcType,
1057 // PVOID TargetType,
1058 // BOOL isReference)
1059 llvm::Type *ArgTypes[] = {CGF.Int8PtrTy, CGF.Int32Ty, CGF.Int8PtrTy,
1060 CGF.Int8PtrTy, CGF.Int32Ty};
1061 llvm::FunctionCallee Function = CGF.CGM.CreateRuntimeFunction(
1062 Ty: llvm::FunctionType::get(Result: CGF.Int8PtrTy, Params: ArgTypes, isVarArg: false),
1063 Name: "__RTDynamicCast");
1064 llvm::Value *Args[] = {
1065 ThisPtr, Offset, SrcRTTI, DestRTTI,
1066 llvm::ConstantInt::get(Ty: CGF.Int32Ty, V: DestTy->isReferenceType())};
1067 return CGF.EmitRuntimeCallOrInvoke(callee: Function, args: Args);
1068}
1069
1070llvm::Value *MicrosoftCXXABI::emitDynamicCastToVoid(CodeGenFunction &CGF,
1071 Address Value,
1072 QualType SrcRecordTy) {
1073 std::tie(args&: Value, args: std::ignore, args: std::ignore) =
1074 performBaseAdjustment(CGF, Value, SrcRecordTy);
1075
1076 // PVOID __RTCastToVoid(
1077 // PVOID inptr)
1078 llvm::Type *ArgTypes[] = {CGF.Int8PtrTy};
1079 llvm::FunctionCallee Function = CGF.CGM.CreateRuntimeFunction(
1080 Ty: llvm::FunctionType::get(Result: CGF.Int8PtrTy, Params: ArgTypes, isVarArg: false),
1081 Name: "__RTCastToVoid");
1082 llvm::Value *Args[] = {Value.emitRawPointer(CGF)};
1083 return CGF.EmitRuntimeCall(callee: Function, args: Args);
1084}
1085
1086bool MicrosoftCXXABI::EmitBadCastCall(CodeGenFunction &CGF) {
1087 return false;
1088}
1089
1090llvm::Value *MicrosoftCXXABI::GetVirtualBaseClassOffset(
1091 CodeGenFunction &CGF, Address This, const CXXRecordDecl *ClassDecl,
1092 const CXXRecordDecl *BaseClassDecl) {
1093 const ASTContext &Context = getContext();
1094 int64_t VBPtrChars =
1095 Context.getASTRecordLayout(D: ClassDecl).getVBPtrOffset().getQuantity();
1096 llvm::Value *VBPtrOffset = llvm::ConstantInt::get(Ty: CGM.PtrDiffTy, V: VBPtrChars);
1097 CharUnits IntSize = Context.getTypeSizeInChars(T: Context.IntTy);
1098 CharUnits VBTableChars =
1099 IntSize *
1100 CGM.getMicrosoftVTableContext().getVBTableIndex(Derived: ClassDecl, VBase: BaseClassDecl);
1101 llvm::Value *VBTableOffset =
1102 llvm::ConstantInt::get(Ty: CGM.IntTy, V: VBTableChars.getQuantity());
1103
1104 llvm::Value *VBPtrToNewBase =
1105 GetVBaseOffsetFromVBPtr(CGF, Base: This, VBPtrOffset, VBTableOffset);
1106 VBPtrToNewBase =
1107 CGF.Builder.CreateSExtOrBitCast(V: VBPtrToNewBase, DestTy: CGM.PtrDiffTy);
1108 return CGF.Builder.CreateNSWAdd(LHS: VBPtrOffset, RHS: VBPtrToNewBase);
1109}
1110
1111bool MicrosoftCXXABI::HasThisReturn(GlobalDecl GD) const {
1112 return isa<CXXConstructorDecl>(Val: GD.getDecl());
1113}
1114
1115static bool isDeletingDtor(GlobalDecl GD) {
1116 return isa<CXXDestructorDecl>(Val: GD.getDecl()) &&
1117 (GD.getDtorType() == Dtor_Deleting ||
1118 GD.getDtorType() == Dtor_VectorDeleting);
1119}
1120
1121bool MicrosoftCXXABI::hasMostDerivedReturn(GlobalDecl GD) const {
1122 return isDeletingDtor(GD);
1123}
1124
1125static bool isTrivialForMSVC(const CXXRecordDecl *RD, QualType Ty,
1126 CodeGenModule &CGM) {
1127 // On AArch64, HVAs that can be passed in registers can also be returned
1128 // in registers. (Note this is using the MSVC definition of an HVA; see
1129 // isPermittedToBeHomogeneousAggregate().)
1130 const Type *Base = nullptr;
1131 uint64_t NumElts = 0;
1132 if (CGM.getTarget().getTriple().isAArch64() &&
1133 CGM.getABIInfo().isHomogeneousAggregate(Ty, Base, Members&: NumElts) &&
1134 isa<VectorType>(Val: Base)) {
1135 return true;
1136 }
1137
1138 // We use the C++14 definition of an aggregate, so we also
1139 // check for:
1140 // No private or protected non static data members.
1141 // No base classes
1142 // No virtual functions
1143 // Additionally, we need to ensure that there is a trivial copy assignment
1144 // operator, a trivial destructor, no user-provided constructors and no
1145 // deleted copy assignment operator.
1146
1147 // We need to cover two cases when checking for a deleted copy assignment
1148 // operator.
1149 //
1150 // struct S { int& r; };
1151 // The above will have an implicit copy assignment operator that is deleted
1152 // and there will not be a `CXXMethodDecl` for the copy assignment operator.
1153 // This is handled by the `needsImplicitCopyAssignment()` check below.
1154 //
1155 // struct S { S& operator=(const S&) = delete; int i; };
1156 // The above will not have an implicit copy assignment operator that is
1157 // deleted but there is a deleted `CXXMethodDecl` for the declared copy
1158 // assignment operator. This is handled by the `isDeleted()` check below.
1159
1160 if (RD->hasProtectedFields() || RD->hasPrivateFields())
1161 return false;
1162 if (RD->getNumBases() > 0)
1163 return false;
1164 if (RD->isPolymorphic())
1165 return false;
1166 if (RD->hasNonTrivialCopyAssignment())
1167 return false;
1168 if (RD->needsImplicitCopyAssignment() && !RD->hasSimpleCopyAssignment())
1169 return false;
1170 for (const Decl *D : RD->decls()) {
1171 if (auto *Ctor = dyn_cast<CXXConstructorDecl>(Val: D)) {
1172 if (Ctor->isUserProvided())
1173 return false;
1174 } else if (auto *Template = dyn_cast<FunctionTemplateDecl>(Val: D)) {
1175 if (isa<CXXConstructorDecl>(Val: Template->getTemplatedDecl()))
1176 return false;
1177 } else if (auto *MethodDecl = dyn_cast<CXXMethodDecl>(Val: D)) {
1178 if (MethodDecl->isCopyAssignmentOperator() && MethodDecl->isDeleted())
1179 return false;
1180 }
1181 }
1182 if (RD->hasNonTrivialDestructor())
1183 return false;
1184 return true;
1185}
1186
1187bool MicrosoftCXXABI::classifyReturnType(CGFunctionInfo &FI) const {
1188 const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl();
1189 if (!RD)
1190 return false;
1191
1192 bool isTrivialForABI = RD->canPassInRegisters() &&
1193 isTrivialForMSVC(RD, Ty: FI.getReturnType(), CGM);
1194
1195 // MSVC always returns structs indirectly from C++ instance methods.
1196 bool isIndirectReturn = !isTrivialForABI || FI.isInstanceMethod();
1197
1198 if (isIndirectReturn) {
1199 CharUnits Align = CGM.getContext().getTypeAlignInChars(T: FI.getReturnType());
1200 LangAS SRetAS = CGM.getTargetCodeGenInfo().getSRetAddrSpace(RD);
1201 unsigned AS = CGM.getContext().getTargetAddressSpace(AS: SRetAS);
1202 FI.getReturnInfo() =
1203 ABIArgInfo::getIndirect(Alignment: Align, /*AddrSpace=*/AS, /*ByVal=*/false);
1204
1205 // MSVC always passes `this` before the `sret` parameter.
1206 FI.getReturnInfo().setSRetAfterThis(FI.isInstanceMethod());
1207
1208 // On AArch64, use the `inreg` attribute if the object is considered to not
1209 // be trivially copyable, or if this is an instance method struct return.
1210 FI.getReturnInfo().setInReg(CGM.getTarget().getTriple().isAArch64());
1211
1212 return true;
1213 }
1214
1215 // Otherwise, use the C ABI rules.
1216 return false;
1217}
1218
1219llvm::BasicBlock *
1220MicrosoftCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
1221 const CXXRecordDecl *RD) {
1222 llvm::Value *IsMostDerivedClass = getStructorImplicitParamValue(CGF);
1223 assert(IsMostDerivedClass &&
1224 "ctor for a class with virtual bases must have an implicit parameter");
1225 llvm::Value *IsCompleteObject =
1226 CGF.Builder.CreateIsNotNull(Arg: IsMostDerivedClass, Name: "is_complete_object");
1227
1228 llvm::BasicBlock *CallVbaseCtorsBB = CGF.createBasicBlock(name: "ctor.init_vbases");
1229 llvm::BasicBlock *SkipVbaseCtorsBB = CGF.createBasicBlock(name: "ctor.skip_vbases");
1230 CGF.Builder.CreateCondBr(Cond: IsCompleteObject,
1231 True: CallVbaseCtorsBB, False: SkipVbaseCtorsBB);
1232
1233 CGF.EmitBlock(BB: CallVbaseCtorsBB);
1234
1235 // Fill in the vbtable pointers here.
1236 EmitVBPtrStores(CGF, RD);
1237
1238 // CGF will put the base ctor calls in this basic block for us later.
1239
1240 return SkipVbaseCtorsBB;
1241}
1242
1243llvm::BasicBlock *
1244MicrosoftCXXABI::EmitDtorCompleteObjectHandler(CodeGenFunction &CGF) {
1245 llvm::Value *IsMostDerivedClass = getStructorImplicitParamValue(CGF);
1246 assert(IsMostDerivedClass &&
1247 "ctor for a class with virtual bases must have an implicit parameter");
1248 llvm::Value *IsCompleteObject =
1249 CGF.Builder.CreateIsNotNull(Arg: IsMostDerivedClass, Name: "is_complete_object");
1250
1251 llvm::BasicBlock *CallVbaseDtorsBB = CGF.createBasicBlock(name: "Dtor.dtor_vbases");
1252 llvm::BasicBlock *SkipVbaseDtorsBB = CGF.createBasicBlock(name: "Dtor.skip_vbases");
1253 CGF.Builder.CreateCondBr(Cond: IsCompleteObject,
1254 True: CallVbaseDtorsBB, False: SkipVbaseDtorsBB);
1255
1256 CGF.EmitBlock(BB: CallVbaseDtorsBB);
1257 // CGF will put the base dtor calls in this basic block for us later.
1258
1259 return SkipVbaseDtorsBB;
1260}
1261
1262void MicrosoftCXXABI::initializeHiddenVirtualInheritanceMembers(
1263 CodeGenFunction &CGF, const CXXRecordDecl *RD) {
1264 // In most cases, an override for a vbase virtual method can adjust
1265 // the "this" parameter by applying a constant offset.
1266 // However, this is not enough while a constructor or a destructor of some
1267 // class X is being executed if all the following conditions are met:
1268 // - X has virtual bases, (1)
1269 // - X overrides a virtual method M of a vbase Y, (2)
1270 // - X itself is a vbase of the most derived class.
1271 //
1272 // If (1) and (2) are true, the vtorDisp for vbase Y is a hidden member of X
1273 // which holds the extra amount of "this" adjustment we must do when we use
1274 // the X vftables (i.e. during X ctor or dtor).
1275 // Outside the ctors and dtors, the values of vtorDisps are zero.
1276
1277 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D: RD);
1278 typedef ASTRecordLayout::VBaseOffsetsMapTy VBOffsets;
1279 const VBOffsets &VBaseMap = Layout.getVBaseOffsetsMap();
1280 CGBuilderTy &Builder = CGF.Builder;
1281
1282 llvm::Value *Int8This = nullptr; // Initialize lazily.
1283
1284 for (const CXXBaseSpecifier &S : RD->vbases()) {
1285 const CXXRecordDecl *VBase = S.getType()->getAsCXXRecordDecl();
1286 auto I = VBaseMap.find(Val: VBase);
1287 assert(I != VBaseMap.end());
1288 if (!I->second.hasVtorDisp())
1289 continue;
1290
1291 llvm::Value *VBaseOffset =
1292 GetVirtualBaseClassOffset(CGF, This: getThisAddress(CGF), ClassDecl: RD, BaseClassDecl: VBase);
1293 uint64_t ConstantVBaseOffset = I->second.VBaseOffset.getQuantity();
1294
1295 // vtorDisp_for_vbase = vbptr[vbase_idx] - offsetof(RD, vbase).
1296 llvm::Value *VtorDispValue = Builder.CreateSub(
1297 LHS: VBaseOffset, RHS: llvm::ConstantInt::get(Ty: CGM.PtrDiffTy, V: ConstantVBaseOffset),
1298 Name: "vtordisp.value");
1299 VtorDispValue = Builder.CreateTruncOrBitCast(V: VtorDispValue, DestTy: CGF.Int32Ty);
1300
1301 if (!Int8This)
1302 Int8This = getThisValue(CGF);
1303
1304 llvm::Value *VtorDispPtr =
1305 Builder.CreateInBoundsGEP(Ty: CGF.Int8Ty, Ptr: Int8This, IdxList: VBaseOffset);
1306 // vtorDisp is always the 32-bits before the vbase in the class layout.
1307 VtorDispPtr = Builder.CreateConstGEP1_32(Ty: CGF.Int8Ty, Ptr: VtorDispPtr, Idx0: -4);
1308
1309 Builder.CreateAlignedStore(Val: VtorDispValue, Addr: VtorDispPtr,
1310 Align: CharUnits::fromQuantity(Quantity: 4));
1311 }
1312}
1313
1314static bool hasDefaultCXXMethodCC(ASTContext &Context,
1315 const CXXMethodDecl *MD) {
1316 CallingConv ExpectedCallingConv = Context.getDefaultCallingConvention(
1317 /*IsVariadic=*/false, /*IsCXXMethod=*/true);
1318 CallingConv ActualCallingConv =
1319 MD->getType()->castAs<FunctionProtoType>()->getCallConv();
1320 return ExpectedCallingConv == ActualCallingConv;
1321}
1322
1323void MicrosoftCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) {
1324 // There's only one constructor type in this ABI.
1325 CGM.EmitGlobal(D: GlobalDecl(D, Ctor_Complete));
1326
1327 // Exported default constructors either have a simple call-site where they use
1328 // the typical calling convention and have a single 'this' pointer for an
1329 // argument -or- they get a wrapper function which appropriately thunks to the
1330 // real default constructor. This thunk is the default constructor closure.
1331 if (D->hasAttr<DLLExportAttr>() && D->isDefaultConstructor() &&
1332 D->isDefined()) {
1333 if (!hasDefaultCXXMethodCC(Context&: getContext(), MD: D) || D->getNumParams() != 0) {
1334 llvm::Function *Fn = getAddrOfCXXCtorClosure(CD: D, CT: Ctor_DefaultClosure);
1335 Fn->setLinkage(llvm::GlobalValue::WeakODRLinkage);
1336 CGM.setGVProperties(GV: Fn, D);
1337 }
1338 }
1339}
1340
1341void MicrosoftCXXABI::EmitVBPtrStores(CodeGenFunction &CGF,
1342 const CXXRecordDecl *RD) {
1343 Address This = getThisAddress(CGF);
1344 This = This.withElementType(ElemTy: CGM.Int8Ty);
1345 const ASTContext &Context = getContext();
1346 const ASTRecordLayout &Layout = Context.getASTRecordLayout(D: RD);
1347
1348 const VBTableGlobals &VBGlobals = enumerateVBTables(RD);
1349 for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) {
1350 const std::unique_ptr<VPtrInfo> &VBT = (*VBGlobals.VBTables)[I];
1351 llvm::GlobalVariable *GV = VBGlobals.Globals[I];
1352 const ASTRecordLayout &SubobjectLayout =
1353 Context.getASTRecordLayout(D: VBT->IntroducingObject);
1354 CharUnits Offs = VBT->NonVirtualOffset;
1355 Offs += SubobjectLayout.getVBPtrOffset();
1356 if (VBT->getVBaseWithVPtr())
1357 Offs += Layout.getVBaseClassOffset(VBase: VBT->getVBaseWithVPtr());
1358 Address VBPtr = CGF.Builder.CreateConstInBoundsByteGEP(Addr: This, Offset: Offs);
1359 llvm::Value *GVPtr =
1360 CGF.Builder.CreateConstInBoundsGEP2_32(Ty: GV->getValueType(), Ptr: GV, Idx0: 0, Idx1: 0);
1361 VBPtr = VBPtr.withElementType(ElemTy: GVPtr->getType());
1362 CGF.Builder.CreateStore(Val: GVPtr, Addr: VBPtr);
1363 }
1364}
1365
1366CGCXXABI::AddedStructorArgCounts
1367MicrosoftCXXABI::buildStructorSignature(GlobalDecl GD,
1368 SmallVectorImpl<CanQualType> &ArgTys) {
1369 AddedStructorArgCounts Added;
1370 // TODO: 'for base' flag
1371 if (isa<CXXDestructorDecl>(Val: GD.getDecl()) &&
1372 (GD.getDtorType() == Dtor_Deleting ||
1373 GD.getDtorType() == Dtor_VectorDeleting)) {
1374 // The scalar deleting destructor takes an implicit int parameter.
1375 ArgTys.push_back(Elt: getContext().IntTy);
1376 ++Added.Suffix;
1377 }
1378 auto *CD = dyn_cast<CXXConstructorDecl>(Val: GD.getDecl());
1379 if (!CD)
1380 return Added;
1381
1382 // All parameters are already in place except is_most_derived, which goes
1383 // after 'this' if it's variadic and last if it's not.
1384
1385 const CXXRecordDecl *Class = CD->getParent();
1386 const FunctionProtoType *FPT = CD->getType()->castAs<FunctionProtoType>();
1387 if (Class->getNumVBases()) {
1388 if (FPT->isVariadic()) {
1389 ArgTys.insert(I: ArgTys.begin() + 1, Elt: getContext().IntTy);
1390 ++Added.Prefix;
1391 } else {
1392 ArgTys.push_back(Elt: getContext().IntTy);
1393 ++Added.Suffix;
1394 }
1395 }
1396
1397 return Added;
1398}
1399
1400void MicrosoftCXXABI::setCXXDestructorDLLStorage(llvm::GlobalValue *GV,
1401 const CXXDestructorDecl *Dtor,
1402 CXXDtorType DT) const {
1403 // Deleting destructor variants are never imported or exported. Give them the
1404 // default storage class.
1405 if (DT == Dtor_Deleting || DT == Dtor_VectorDeleting) {
1406 GV->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
1407 } else {
1408 const NamedDecl *ND = Dtor;
1409 CGM.setDLLImportDLLExport(GV, D: ND);
1410 }
1411}
1412
1413llvm::GlobalValue::LinkageTypes MicrosoftCXXABI::getCXXDestructorLinkage(
1414 GVALinkage Linkage, const CXXDestructorDecl *Dtor, CXXDtorType DT) const {
1415 // Internal things are always internal, regardless of attributes. After this,
1416 // we know the thunk is externally visible.
1417 if (Linkage == GVA_Internal)
1418 return llvm::GlobalValue::InternalLinkage;
1419
1420 switch (DT) {
1421 case Dtor_Base:
1422 // The base destructor most closely tracks the user-declared constructor, so
1423 // we delegate back to the normal declarator case.
1424 return CGM.getLLVMLinkageForDeclarator(D: Dtor, Linkage);
1425 case Dtor_Complete:
1426 // The complete destructor is like an inline function, but it may be
1427 // imported and therefore must be exported as well. This requires changing
1428 // the linkage if a DLL attribute is present.
1429 if (Dtor->hasAttr<DLLExportAttr>())
1430 return llvm::GlobalValue::WeakODRLinkage;
1431 if (Dtor->hasAttr<DLLImportAttr>())
1432 return llvm::GlobalValue::AvailableExternallyLinkage;
1433 return llvm::GlobalValue::LinkOnceODRLinkage;
1434 case Dtor_Deleting:
1435 // Deleting destructors are like inline functions. They have vague linkage
1436 // and are emitted everywhere they are used. They are internal if the class
1437 // is internal.
1438 return llvm::GlobalValue::LinkOnceODRLinkage;
1439 case Dtor_Unified:
1440 llvm_unreachable("MS C++ ABI does not support unified dtors");
1441 case Dtor_VectorDeleting:
1442 // Use the weak, non-ODR linkage for vector deleting destructors to block
1443 // inlining. This enables an MS ABI code-size saving optimization that
1444 // allows us to avoid emitting array deletion code when arrays of a given
1445 // type are not allocated within the final linkage unit.
1446 return llvm::GlobalValue::WeakAnyLinkage;
1447 case Dtor_Comdat:
1448 llvm_unreachable("MS C++ ABI does not support comdat dtors");
1449 }
1450 llvm_unreachable("invalid dtor type");
1451}
1452
1453void MicrosoftCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) {
1454 // The TU defining a dtor is only guaranteed to emit a base destructor. All
1455 // other destructor variants are delegating thunks.
1456 CGM.EmitGlobal(D: GlobalDecl(D, Dtor_Base));
1457
1458 // If the class is dllexported, emit the complete (vbase) destructor wherever
1459 // the base dtor is emitted.
1460 // FIXME: To match MSVC, this should only be done when the class is exported
1461 // with -fdllexport-inlines enabled.
1462 if (D->getParent()->getNumVBases() > 0 && D->hasAttr<DLLExportAttr>())
1463 CGM.EmitGlobal(D: GlobalDecl(D, Dtor_Complete));
1464}
1465
1466CharUnits
1467MicrosoftCXXABI::getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD) {
1468 const CXXMethodDecl *MD = cast<CXXMethodDecl>(Val: GD.getDecl());
1469
1470 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(Val: MD)) {
1471 // Complete destructors take a pointer to the complete object as a
1472 // parameter, thus don't need this adjustment.
1473 if (GD.getDtorType() == Dtor_Complete)
1474 return CharUnits();
1475
1476 // There's no Dtor_Base in vftable but it shares the this adjustment with
1477 // the deleting one, so look it up instead.
1478 GD =
1479 GlobalDecl(DD, CGM.getContext().getTargetInfo().emitVectorDeletingDtors(
1480 CGM.getContext().getLangOpts())
1481 ? Dtor_VectorDeleting
1482 : Dtor_Deleting);
1483 }
1484
1485 MethodVFTableLocation ML =
1486 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1487 CharUnits Adjustment = ML.VFPtrOffset;
1488
1489 // Normal virtual instance methods need to adjust from the vfptr that first
1490 // defined the virtual method to the virtual base subobject, but destructors
1491 // do not. The vector deleting destructor thunk applies this adjustment for
1492 // us if necessary.
1493 if (isa<CXXDestructorDecl>(Val: MD))
1494 Adjustment = CharUnits::Zero();
1495
1496 if (ML.VBase) {
1497 const ASTRecordLayout &DerivedLayout =
1498 getContext().getASTRecordLayout(D: MD->getParent());
1499 Adjustment += DerivedLayout.getVBaseClassOffset(VBase: ML.VBase);
1500 }
1501
1502 return Adjustment;
1503}
1504
1505Address MicrosoftCXXABI::adjustThisArgumentForVirtualFunctionCall(
1506 CodeGenFunction &CGF, GlobalDecl GD, Address This,
1507 bool VirtualCall) {
1508 if (!VirtualCall) {
1509 // If the call of a virtual function is not virtual, we just have to
1510 // compensate for the adjustment the virtual function does in its prologue.
1511 CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(GD);
1512 if (Adjustment.isZero())
1513 return This;
1514
1515 This = This.withElementType(ElemTy: CGF.Int8Ty);
1516 assert(Adjustment.isPositive());
1517 return CGF.Builder.CreateConstByteGEP(Addr: This, Offset: Adjustment);
1518 }
1519
1520 const CXXMethodDecl *MD = cast<CXXMethodDecl>(Val: GD.getDecl());
1521
1522 GlobalDecl LookupGD = GD;
1523 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(Val: MD)) {
1524 // Complete dtors take a pointer to the complete object,
1525 // thus don't need adjustment.
1526 if (GD.getDtorType() == Dtor_Complete)
1527 return This;
1528
1529 // There's only Dtor_Deleting in vftable but it shares the this adjustment
1530 // with the base one, so look up the deleting one instead.
1531 LookupGD =
1532 GlobalDecl(DD, CGM.getContext().getTargetInfo().emitVectorDeletingDtors(
1533 CGM.getContext().getLangOpts())
1534 ? Dtor_VectorDeleting
1535 : Dtor_Deleting);
1536 }
1537 MethodVFTableLocation ML =
1538 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD: LookupGD);
1539
1540 CharUnits StaticOffset = ML.VFPtrOffset;
1541
1542 // Base destructors expect 'this' to point to the beginning of the base
1543 // subobject, not the first vfptr that happens to contain the virtual dtor.
1544 // However, we still need to apply the virtual base adjustment.
1545 if (isa<CXXDestructorDecl>(Val: MD) && GD.getDtorType() == Dtor_Base)
1546 StaticOffset = CharUnits::Zero();
1547
1548 Address Result = This;
1549 if (ML.VBase) {
1550 Result = Result.withElementType(ElemTy: CGF.Int8Ty);
1551
1552 const CXXRecordDecl *Derived = MD->getParent();
1553 const CXXRecordDecl *VBase = ML.VBase;
1554 llvm::Value *VBaseOffset =
1555 GetVirtualBaseClassOffset(CGF, This: Result, ClassDecl: Derived, BaseClassDecl: VBase);
1556 llvm::Value *VBasePtr = CGF.Builder.CreateInBoundsGEP(
1557 Ty: Result.getElementType(), Ptr: Result.emitRawPointer(CGF), IdxList: VBaseOffset);
1558 CharUnits VBaseAlign =
1559 CGF.CGM.getVBaseAlignment(DerivedAlign: Result.getAlignment(), Derived, VBase);
1560 Result = Address(VBasePtr, CGF.Int8Ty, VBaseAlign);
1561 }
1562 if (!StaticOffset.isZero()) {
1563 assert(StaticOffset.isPositive());
1564 Result = Result.withElementType(ElemTy: CGF.Int8Ty);
1565 if (ML.VBase) {
1566 // Non-virtual adjustment might result in a pointer outside the allocated
1567 // object, e.g. if the final overrider class is laid out after the virtual
1568 // base that declares a method in the most derived class.
1569 // FIXME: Update the code that emits this adjustment in thunks prologues.
1570 Result = CGF.Builder.CreateConstByteGEP(Addr: Result, Offset: StaticOffset);
1571 } else {
1572 Result = CGF.Builder.CreateConstInBoundsByteGEP(Addr: Result, Offset: StaticOffset);
1573 }
1574 }
1575 return Result;
1576}
1577
1578void MicrosoftCXXABI::addImplicitStructorParams(CodeGenFunction &CGF,
1579 QualType &ResTy,
1580 FunctionArgList &Params) {
1581 ASTContext &Context = getContext();
1582 const CXXMethodDecl *MD = cast<CXXMethodDecl>(Val: CGF.CurGD.getDecl());
1583 assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD));
1584 if (isa<CXXConstructorDecl>(Val: MD) && MD->getParent()->getNumVBases()) {
1585 auto *IsMostDerived = ImplicitParamDecl::Create(
1586 C&: Context, /*DC=*/nullptr, IdLoc: CGF.CurGD.getDecl()->getLocation(),
1587 Id: &Context.Idents.get(Name: "is_most_derived"), T: Context.IntTy,
1588 ParamKind: ImplicitParamKind::Other);
1589 // The 'most_derived' parameter goes second if the ctor is variadic and last
1590 // if it's not. Dtors can't be variadic.
1591 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
1592 if (FPT->isVariadic())
1593 Params.insert(I: Params.begin() + 1, Elt: IsMostDerived);
1594 else
1595 Params.push_back(Elt: IsMostDerived);
1596 getStructorImplicitParamDecl(CGF) = IsMostDerived;
1597 } else if (isDeletingDtor(GD: CGF.CurGD)) {
1598 auto *ShouldDelete = ImplicitParamDecl::Create(
1599 C&: Context, /*DC=*/nullptr, IdLoc: CGF.CurGD.getDecl()->getLocation(),
1600 Id: &Context.Idents.get(Name: "should_call_delete"), T: Context.IntTy,
1601 ParamKind: ImplicitParamKind::Other);
1602 Params.push_back(Elt: ShouldDelete);
1603 getStructorImplicitParamDecl(CGF) = ShouldDelete;
1604 }
1605}
1606
1607void MicrosoftCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
1608 // Naked functions have no prolog.
1609 if (CGF.CurFuncDecl && CGF.CurFuncDecl->hasAttr<NakedAttr>())
1610 return;
1611
1612 // Overridden virtual methods of non-primary bases need to adjust the incoming
1613 // 'this' pointer in the prologue. In this hierarchy, C::b will subtract
1614 // sizeof(void*) to adjust from B* to C*:
1615 // struct A { virtual void a(); };
1616 // struct B { virtual void b(); };
1617 // struct C : A, B { virtual void b(); };
1618 //
1619 // Leave the value stored in the 'this' alloca unadjusted, so that the
1620 // debugger sees the unadjusted value. Microsoft debuggers require this, and
1621 // will apply the ThisAdjustment in the method type information.
1622 // FIXME: Do something better for DWARF debuggers, which won't expect this,
1623 // without making our codegen depend on debug info settings.
1624 llvm::Value *This = loadIncomingCXXThis(CGF);
1625 const CXXMethodDecl *MD = cast<CXXMethodDecl>(Val: CGF.CurGD.getDecl());
1626 if (!CGF.CurFuncIsThunk && MD->isVirtual()) {
1627 CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(GD: CGF.CurGD);
1628 if (!Adjustment.isZero()) {
1629 assert(Adjustment.isPositive());
1630 This = CGF.Builder.CreateConstInBoundsGEP1_32(Ty: CGF.Int8Ty, Ptr: This,
1631 Idx0: -Adjustment.getQuantity());
1632 }
1633 }
1634 setCXXABIThisValue(CGF, ThisPtr: This);
1635
1636 // If this is a function that the ABI specifies returns 'this', initialize
1637 // the return slot to 'this' at the start of the function.
1638 //
1639 // Unlike the setting of return types, this is done within the ABI
1640 // implementation instead of by clients of CGCXXABI because:
1641 // 1) getThisValue is currently protected
1642 // 2) in theory, an ABI could implement 'this' returns some other way;
1643 // HasThisReturn only specifies a contract, not the implementation
1644 if (HasThisReturn(GD: CGF.CurGD) || hasMostDerivedReturn(GD: CGF.CurGD))
1645 CGF.Builder.CreateStore(Val: getThisValue(CGF), Addr: CGF.ReturnValue);
1646
1647 if (isa<CXXConstructorDecl>(Val: MD) && MD->getParent()->getNumVBases()) {
1648 assert(getStructorImplicitParamDecl(CGF) &&
1649 "no implicit parameter for a constructor with virtual bases?");
1650 getStructorImplicitParamValue(CGF)
1651 = CGF.Builder.CreateLoad(
1652 Addr: CGF.GetAddrOfLocalVar(VD: getStructorImplicitParamDecl(CGF)),
1653 Name: "is_most_derived");
1654 }
1655
1656 if (isDeletingDtor(GD: CGF.CurGD)) {
1657 assert(getStructorImplicitParamDecl(CGF) &&
1658 "no implicit parameter for a deleting destructor?");
1659 getStructorImplicitParamValue(CGF)
1660 = CGF.Builder.CreateLoad(
1661 Addr: CGF.GetAddrOfLocalVar(VD: getStructorImplicitParamDecl(CGF)),
1662 Name: "should_call_delete");
1663 }
1664}
1665
1666CGCXXABI::AddedStructorArgs MicrosoftCXXABI::getImplicitConstructorArgs(
1667 CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type,
1668 bool ForVirtualBase, bool Delegating) {
1669 assert(Type == Ctor_Complete || Type == Ctor_Base);
1670
1671 // Check if we need a 'most_derived' parameter.
1672 if (!D->getParent()->getNumVBases())
1673 return AddedStructorArgs{};
1674
1675 // Add the 'most_derived' argument second if we are variadic or last if not.
1676 const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>();
1677 llvm::Value *MostDerivedArg;
1678 if (Delegating) {
1679 MostDerivedArg = getStructorImplicitParamValue(CGF);
1680 } else {
1681 MostDerivedArg = llvm::ConstantInt::get(Ty: CGM.Int32Ty, V: Type == Ctor_Complete);
1682 }
1683 if (FPT->isVariadic()) {
1684 return AddedStructorArgs::prefix(Args: {{.Value: MostDerivedArg, .Type: getContext().IntTy}});
1685 }
1686 return AddedStructorArgs::suffix(Args: {{.Value: MostDerivedArg, .Type: getContext().IntTy}});
1687}
1688
1689llvm::Value *MicrosoftCXXABI::getCXXDestructorImplicitParam(
1690 CodeGenFunction &CGF, const CXXDestructorDecl *DD, CXXDtorType Type,
1691 bool ForVirtualBase, bool Delegating) {
1692 return nullptr;
1693}
1694
1695void MicrosoftCXXABI::EmitDestructorCall(CodeGenFunction &CGF,
1696 const CXXDestructorDecl *DD,
1697 CXXDtorType Type, bool ForVirtualBase,
1698 bool Delegating, Address This,
1699 QualType ThisTy) {
1700 // Use the base destructor variant in place of the complete destructor variant
1701 // if the class has no virtual bases. This effectively implements some of the
1702 // -mconstructor-aliases optimization, but as part of the MS C++ ABI.
1703 if (Type == Dtor_Complete && DD->getParent()->getNumVBases() == 0)
1704 Type = Dtor_Base;
1705
1706 GlobalDecl GD(DD, Type);
1707 CGCallee Callee = CGCallee::forDirect(functionPtr: CGM.getAddrOfCXXStructor(GD), abstractInfo: GD);
1708
1709 if (DD->isVirtual()) {
1710 assert(Type != CXXDtorType::Dtor_Deleting &&
1711 "The deleting destructor should only be called via a virtual call");
1712 This = adjustThisArgumentForVirtualFunctionCall(CGF, GD: GlobalDecl(DD, Type),
1713 This, VirtualCall: false);
1714 }
1715
1716 llvm::BasicBlock *BaseDtorEndBB = nullptr;
1717 if (ForVirtualBase && isa<CXXConstructorDecl>(Val: CGF.CurCodeDecl)) {
1718 BaseDtorEndBB = EmitDtorCompleteObjectHandler(CGF);
1719 }
1720
1721 llvm::Value *Implicit =
1722 getCXXDestructorImplicitParam(CGF, DD, Type, ForVirtualBase,
1723 Delegating); // = nullptr
1724 CGF.EmitCXXDestructorCall(Dtor: GD, Callee, This: CGF.getAsNaturalPointerTo(Addr: This, PointeeType: ThisTy),
1725 ThisTy,
1726 /*ImplicitParam=*/Implicit,
1727 /*ImplicitParamTy=*/QualType(), /*E=*/nullptr);
1728 if (BaseDtorEndBB) {
1729 // Complete object handler should continue to be the remaining
1730 CGF.Builder.CreateBr(Dest: BaseDtorEndBB);
1731 CGF.EmitBlock(BB: BaseDtorEndBB);
1732 }
1733}
1734
1735void MicrosoftCXXABI::emitVTableTypeMetadata(const VPtrInfo &Info,
1736 const CXXRecordDecl *RD,
1737 llvm::GlobalVariable *VTable) {
1738 // Emit type metadata on vtables with LTO or IR instrumentation.
1739 // In IR instrumentation, the type metadata could be used to find out vtable
1740 // definitions (for type profiling) among all global variables.
1741 if (!CGM.getCodeGenOpts().LTOUnit &&
1742 !CGM.getCodeGenOpts().hasProfileIRInstr())
1743 return;
1744
1745 // TODO: Should VirtualFunctionElimination also be supported here?
1746 // See similar handling in CodeGenModule::EmitVTableTypeMetadata.
1747 if (CGM.getCodeGenOpts().WholeProgramVTables) {
1748 llvm::DenseSet<const CXXRecordDecl *> Visited;
1749 llvm::GlobalObject::VCallVisibility TypeVis =
1750 CGM.GetVCallVisibilityLevel(RD, Visited);
1751 if (TypeVis != llvm::GlobalObject::VCallVisibilityPublic)
1752 VTable->setVCallVisibilityMetadata(TypeVis);
1753 }
1754
1755 // The location of the first virtual function pointer in the virtual table,
1756 // aka the "address point" on Itanium. This is at offset 0 if RTTI is
1757 // disabled, or sizeof(void*) if RTTI is enabled.
1758 CharUnits AddressPoint =
1759 getContext().getLangOpts().RTTIData
1760 ? getContext().toCharUnitsFromBits(
1761 BitSize: getContext().getTargetInfo().getPointerWidth(AddrSpace: LangAS::Default))
1762 : CharUnits::Zero();
1763
1764 if (Info.PathToIntroducingObject.empty()) {
1765 CGM.AddVTableTypeMetadata(VTable, Offset: AddressPoint, RD);
1766 return;
1767 }
1768
1769 // Add a bitset entry for the least derived base belonging to this vftable.
1770 CGM.AddVTableTypeMetadata(VTable, Offset: AddressPoint,
1771 RD: Info.PathToIntroducingObject.back());
1772
1773 // Add a bitset entry for each derived class that is laid out at the same
1774 // offset as the least derived base.
1775 for (unsigned I = Info.PathToIntroducingObject.size() - 1; I != 0; --I) {
1776 const CXXRecordDecl *DerivedRD = Info.PathToIntroducingObject[I - 1];
1777 const CXXRecordDecl *BaseRD = Info.PathToIntroducingObject[I];
1778
1779 const ASTRecordLayout &Layout =
1780 getContext().getASTRecordLayout(D: DerivedRD);
1781 CharUnits Offset;
1782 auto VBI = Layout.getVBaseOffsetsMap().find(Val: BaseRD);
1783 if (VBI == Layout.getVBaseOffsetsMap().end())
1784 Offset = Layout.getBaseClassOffset(Base: BaseRD);
1785 else
1786 Offset = VBI->second.VBaseOffset;
1787 if (!Offset.isZero())
1788 return;
1789 CGM.AddVTableTypeMetadata(VTable, Offset: AddressPoint, RD: DerivedRD);
1790 }
1791
1792 // Finally do the same for the most derived class.
1793 if (Info.FullOffsetInMDC.isZero())
1794 CGM.AddVTableTypeMetadata(VTable, Offset: AddressPoint, RD);
1795}
1796
1797void MicrosoftCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT,
1798 const CXXRecordDecl *RD) {
1799 MicrosoftVTableContext &VFTContext = CGM.getMicrosoftVTableContext();
1800 const VPtrInfoVector &VFPtrs = VFTContext.getVFPtrOffsets(RD);
1801
1802 for (const std::unique_ptr<VPtrInfo>& Info : VFPtrs) {
1803 llvm::GlobalVariable *VTable = getAddrOfVTable(RD, VPtrOffset: Info->FullOffsetInMDC);
1804 if (VTable->hasInitializer())
1805 continue;
1806
1807 const VTableLayout &VTLayout =
1808 VFTContext.getVFTableLayout(RD, VFPtrOffset: Info->FullOffsetInMDC);
1809
1810 llvm::Constant *RTTI = nullptr;
1811 if (any_of(Range: VTLayout.vtable_components(),
1812 P: [](const VTableComponent &VTC) { return VTC.isRTTIKind(); }))
1813 RTTI = getMSCompleteObjectLocator(RD, Info: *Info);
1814
1815 ConstantInitBuilder builder(CGM);
1816 auto components = builder.beginStruct();
1817 CGVT.createVTableInitializer(builder&: components, layout: VTLayout, rtti: RTTI,
1818 vtableHasLocalLinkage: VTable->hasLocalLinkage());
1819 components.finishAndSetAsInitializer(global: VTable);
1820
1821 emitVTableTypeMetadata(Info: *Info, RD, VTable);
1822 }
1823}
1824
1825bool MicrosoftCXXABI::isVirtualOffsetNeededForVTableField(
1826 CodeGenFunction &CGF, CodeGenFunction::VPtr Vptr) {
1827 return Vptr.NearestVBase != nullptr;
1828}
1829
1830llvm::Value *MicrosoftCXXABI::getVTableAddressPointInStructor(
1831 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base,
1832 const CXXRecordDecl *NearestVBase) {
1833 llvm::Constant *VTableAddressPoint = getVTableAddressPoint(Base, VTableClass);
1834 if (!VTableAddressPoint) {
1835 assert(Base.getBase()->getNumVBases() &&
1836 !getContext().getASTRecordLayout(Base.getBase()).hasOwnVFPtr());
1837 }
1838 return VTableAddressPoint;
1839}
1840
1841static void mangleVFTableName(MicrosoftMangleContext &MangleContext,
1842 const CXXRecordDecl *RD, const VPtrInfo &VFPtr,
1843 SmallString<256> &Name) {
1844 llvm::raw_svector_ostream Out(Name);
1845 MangleContext.mangleCXXVFTable(Derived: RD, BasePath: VFPtr.MangledPath, Out);
1846}
1847
1848llvm::Constant *
1849MicrosoftCXXABI::getVTableAddressPoint(BaseSubobject Base,
1850 const CXXRecordDecl *VTableClass) {
1851 (void)getAddrOfVTable(RD: VTableClass, VPtrOffset: Base.getBaseOffset());
1852 VFTableIdTy ID(VTableClass, Base.getBaseOffset());
1853 return VFTablesMap[ID];
1854}
1855
1856llvm::GlobalVariable *MicrosoftCXXABI::getAddrOfVTable(const CXXRecordDecl *RD,
1857 CharUnits VPtrOffset) {
1858 // getAddrOfVTable may return 0 if asked to get an address of a vtable which
1859 // shouldn't be used in the given record type. We want to cache this result in
1860 // VFTablesMap, thus a simple zero check is not sufficient.
1861
1862 VFTableIdTy ID(RD, VPtrOffset);
1863 auto [I, Inserted] = VTablesMap.try_emplace(Key: ID);
1864 if (!Inserted)
1865 return I->second;
1866
1867 llvm::GlobalVariable *&VTable = I->second;
1868
1869 MicrosoftVTableContext &VTContext = CGM.getMicrosoftVTableContext();
1870 const VPtrInfoVector &VFPtrs = VTContext.getVFPtrOffsets(RD);
1871
1872 if (DeferredVFTables.insert(Ptr: RD).second) {
1873 // We haven't processed this record type before.
1874 // Queue up this vtable for possible deferred emission.
1875 CGM.addDeferredVTable(RD);
1876
1877#ifndef NDEBUG
1878 // Create all the vftables at once in order to make sure each vftable has
1879 // a unique mangled name.
1880 llvm::StringSet<> ObservedMangledNames;
1881 for (const auto &VFPtr : VFPtrs) {
1882 SmallString<256> Name;
1883 mangleVFTableName(getMangleContext(), RD, *VFPtr, Name);
1884 if (!ObservedMangledNames.insert(Name.str()).second)
1885 llvm_unreachable("Already saw this mangling before?");
1886 }
1887#endif
1888 }
1889
1890 const std::unique_ptr<VPtrInfo> *VFPtrI =
1891 llvm::find_if(Range: VFPtrs, P: [&](const std::unique_ptr<VPtrInfo> &VPI) {
1892 return VPI->FullOffsetInMDC == VPtrOffset;
1893 });
1894 if (VFPtrI == VFPtrs.end()) {
1895 VFTablesMap[ID] = nullptr;
1896 return nullptr;
1897 }
1898 const std::unique_ptr<VPtrInfo> &VFPtr = *VFPtrI;
1899
1900 SmallString<256> VFTableName;
1901 mangleVFTableName(MangleContext&: getMangleContext(), RD, VFPtr: *VFPtr, Name&: VFTableName);
1902
1903 // Classes marked __declspec(dllimport) need vftables generated on the
1904 // import-side in order to support features like constexpr. No other
1905 // translation unit relies on the emission of the local vftable, translation
1906 // units are expected to generate them as needed.
1907 //
1908 // Because of this unique behavior, we maintain this logic here instead of
1909 // getVTableLinkage.
1910 llvm::GlobalValue::LinkageTypes VFTableLinkage =
1911 RD->hasAttr<DLLImportAttr>() ? llvm::GlobalValue::LinkOnceODRLinkage
1912 : CGM.getVTableLinkage(RD);
1913 bool VFTableComesFromAnotherTU =
1914 llvm::GlobalValue::isAvailableExternallyLinkage(Linkage: VFTableLinkage) ||
1915 llvm::GlobalValue::isExternalLinkage(Linkage: VFTableLinkage);
1916 bool VTableAliasIsRequred =
1917 !VFTableComesFromAnotherTU && getContext().getLangOpts().RTTIData;
1918
1919 if (llvm::GlobalValue *VFTable =
1920 CGM.getModule().getNamedGlobal(Name: VFTableName)) {
1921 VFTablesMap[ID] = VFTable;
1922 VTable = VTableAliasIsRequred
1923 ? cast<llvm::GlobalVariable>(
1924 Val: cast<llvm::GlobalAlias>(Val: VFTable)->getAliaseeObject())
1925 : cast<llvm::GlobalVariable>(Val: VFTable);
1926 return VTable;
1927 }
1928
1929 const VTableLayout &VTLayout =
1930 VTContext.getVFTableLayout(RD, VFPtrOffset: VFPtr->FullOffsetInMDC);
1931 llvm::GlobalValue::LinkageTypes VTableLinkage =
1932 VTableAliasIsRequred ? llvm::GlobalValue::PrivateLinkage : VFTableLinkage;
1933
1934 StringRef VTableName = VTableAliasIsRequred ? StringRef() : VFTableName.str();
1935
1936 llvm::Type *VTableType = CGM.getVTables().getVTableType(layout: VTLayout);
1937
1938 // Create a backing variable for the contents of VTable. The VTable may
1939 // or may not include space for a pointer to RTTI data.
1940 llvm::GlobalValue *VFTable;
1941 VTable = new llvm::GlobalVariable(CGM.getModule(), VTableType,
1942 /*isConstant=*/true, VTableLinkage,
1943 /*Initializer=*/nullptr, VTableName);
1944 if (!CGM.shouldEmitRTTI())
1945 VTable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1946
1947 llvm::Comdat *C = nullptr;
1948 if (!VFTableComesFromAnotherTU &&
1949 llvm::GlobalValue::isWeakForLinker(Linkage: VFTableLinkage))
1950 C = CGM.getModule().getOrInsertComdat(Name: VFTableName.str());
1951
1952 // Only insert a pointer into the VFTable for RTTI data if we are not
1953 // importing it. We never reference the RTTI data directly so there is no
1954 // need to make room for it.
1955 if (VTableAliasIsRequred) {
1956 llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(Ty: CGM.Int32Ty, V: 0),
1957 llvm::ConstantInt::get(Ty: CGM.Int32Ty, V: 0),
1958 llvm::ConstantInt::get(Ty: CGM.Int32Ty, V: 1)};
1959 // Create a GEP which points just after the first entry in the VFTable,
1960 // this should be the location of the first virtual method.
1961 llvm::Constant *VTableGEP = llvm::ConstantExpr::getInBoundsGetElementPtr(
1962 Ty: VTable->getValueType(), C: VTable, IdxList: GEPIndices);
1963 if (llvm::GlobalValue::isWeakForLinker(Linkage: VFTableLinkage)) {
1964 VFTableLinkage = llvm::GlobalValue::ExternalLinkage;
1965 if (C)
1966 C->setSelectionKind(llvm::Comdat::Largest);
1967 }
1968 VFTable = llvm::GlobalAlias::create(Ty: CGM.Int8PtrTy,
1969 /*AddressSpace=*/0, Linkage: VFTableLinkage,
1970 Name: VFTableName.str(), Aliasee: VTableGEP,
1971 Parent: &CGM.getModule());
1972 if (!CGM.shouldEmitRTTI())
1973 VFTable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1974 } else {
1975 // We don't need a GlobalAlias to be a symbol for the VTable if we won't
1976 // be referencing any RTTI data.
1977 // The GlobalVariable will end up being an appropriate definition of the
1978 // VFTable.
1979 VFTable = VTable;
1980 }
1981 if (C)
1982 VTable->setComdat(C);
1983
1984 if (RD->hasAttr<DLLExportAttr>())
1985 VFTable->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
1986
1987 VFTablesMap[ID] = VFTable;
1988 return VTable;
1989}
1990
1991CGCallee MicrosoftCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF,
1992 GlobalDecl GD,
1993 Address This,
1994 llvm::Type *Ty,
1995 SourceLocation Loc) {
1996 CGBuilderTy &Builder = CGF.Builder;
1997
1998 Ty = CGF.DefaultPtrTy;
1999 Address VPtr =
2000 adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, VirtualCall: true);
2001
2002 auto *MethodDecl = cast<CXXMethodDecl>(Val: GD.getDecl());
2003 llvm::Value *VTable =
2004 CGF.GetVTablePtr(This: VPtr, VTableTy: CGF.DefaultPtrTy, VTableClass: MethodDecl->getParent());
2005
2006 MicrosoftVTableContext &VFTContext = CGM.getMicrosoftVTableContext();
2007 MethodVFTableLocation ML = VFTContext.getMethodVFTableLocation(GD);
2008
2009 // Compute the identity of the most derived class whose virtual table is
2010 // located at the MethodVFTableLocation ML.
2011 auto getObjectWithVPtr = [&] {
2012 return llvm::find_if(Range: VFTContext.getVFPtrOffsets(
2013 RD: ML.VBase ? ML.VBase : MethodDecl->getParent()),
2014 P: [&](const std::unique_ptr<VPtrInfo> &Info) {
2015 return Info->FullOffsetInMDC == ML.VFPtrOffset;
2016 })
2017 ->get()
2018 ->ObjectWithVPtr;
2019 };
2020
2021 llvm::Value *VFunc;
2022 if (CGF.ShouldEmitVTableTypeCheckedLoad(RD: MethodDecl->getParent())) {
2023 VFunc = CGF.EmitVTableTypeCheckedLoad(
2024 RD: getObjectWithVPtr(), VTable, VTableTy: Ty,
2025 VTableByteOffset: ML.Index *
2026 CGM.getContext().getTargetInfo().getPointerWidth(AddrSpace: LangAS::Default) /
2027 8);
2028 } else {
2029 if (CGM.getCodeGenOpts().PrepareForLTO)
2030 CGF.EmitTypeMetadataCodeForVCall(RD: getObjectWithVPtr(), VTable, Loc);
2031
2032 llvm::Value *VFuncPtr =
2033 Builder.CreateConstInBoundsGEP1_64(Ty, Ptr: VTable, Idx0: ML.Index, Name: "vfn");
2034 VFunc = Builder.CreateAlignedLoad(Ty, Addr: VFuncPtr, Align: CGF.getPointerAlign());
2035 }
2036
2037 CGCallee Callee(GD, VFunc);
2038 return Callee;
2039}
2040
2041llvm::Value *MicrosoftCXXABI::EmitVirtualDestructorCall(
2042 CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType,
2043 Address This, DeleteOrMemberCallExpr E, llvm::CallBase **CallOrInvoke) {
2044 auto *CE = dyn_cast<const CXXMemberCallExpr *>(Val&: E);
2045 auto *D = dyn_cast<const CXXDeleteExpr *>(Val&: E);
2046 assert((CE != nullptr) ^ (D != nullptr));
2047 assert(CE == nullptr || CE->arg_begin() == CE->arg_end());
2048 assert(DtorType == Dtor_VectorDeleting || DtorType == Dtor_Complete ||
2049 DtorType == Dtor_Deleting);
2050
2051 // We have only one destructor in the vftable but can get both behaviors
2052 // by passing an implicit int parameter.
2053 ASTContext &Context = getContext();
2054 bool VectorDeletingDtorsEnabled =
2055 Context.getTargetInfo().emitVectorDeletingDtors(Context.getLangOpts());
2056 GlobalDecl GD(Dtor, VectorDeletingDtorsEnabled ? Dtor_VectorDeleting
2057 : Dtor_Deleting);
2058 const CGFunctionInfo *FInfo =
2059 &CGM.getTypes().arrangeCXXStructorDeclaration(GD);
2060 llvm::FunctionType *Ty = CGF.CGM.getTypes().GetFunctionType(Info: *FInfo);
2061 CGCallee Callee = CGCallee::forVirtual(CE, MD: GD, Addr: This, FTy: Ty);
2062
2063 bool IsDeleting = DtorType == Dtor_Deleting;
2064 bool IsArrayDelete = D && D->isArrayForm() && VectorDeletingDtorsEnabled;
2065 bool IsGlobalDelete = D && D->isGlobalDelete() &&
2066 Context.getTargetInfo().callGlobalDeleteInDeletingDtor(
2067 Context.getLangOpts());
2068 llvm::Value *ImplicitParam =
2069 CGF.Builder.getInt32(C: (IsDeleting ? 1 : 0) | (IsGlobalDelete ? 4 : 0) |
2070 (IsArrayDelete ? 2 : 0));
2071
2072 QualType ThisTy;
2073 if (CE) {
2074 ThisTy = CE->getObjectType();
2075 } else {
2076 ThisTy = D->getDestroyedType();
2077 }
2078
2079 while (const ArrayType *ATy = Context.getAsArrayType(T: ThisTy))
2080 ThisTy = ATy->getElementType();
2081
2082 This = adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, VirtualCall: true);
2083 RValue RV =
2084 CGF.EmitCXXDestructorCall(Dtor: GD, Callee, This: This.emitRawPointer(CGF), ThisTy,
2085 ImplicitParam, ImplicitParamTy: Context.IntTy, E: CE, CallOrInvoke);
2086 return RV.getScalarVal();
2087}
2088
2089const VBTableGlobals &
2090MicrosoftCXXABI::enumerateVBTables(const CXXRecordDecl *RD) {
2091 // At this layer, we can key the cache off of a single class, which is much
2092 // easier than caching each vbtable individually.
2093 auto [Entry, Added] = VBTablesMap.try_emplace(Key: RD);
2094 VBTableGlobals &VBGlobals = Entry->second;
2095 if (!Added)
2096 return VBGlobals;
2097
2098 MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext();
2099 VBGlobals.VBTables = &Context.enumerateVBTables(RD);
2100
2101 // Cache the globals for all vbtables so we don't have to recompute the
2102 // mangled names.
2103 llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD);
2104 for (VPtrInfoVector::const_iterator I = VBGlobals.VBTables->begin(),
2105 E = VBGlobals.VBTables->end();
2106 I != E; ++I) {
2107 VBGlobals.Globals.push_back(Elt: getAddrOfVBTable(VBT: **I, RD, Linkage));
2108 }
2109
2110 return VBGlobals;
2111}
2112
2113llvm::Function *
2114MicrosoftCXXABI::EmitVirtualMemPtrThunk(const CXXMethodDecl *MD,
2115 const MethodVFTableLocation &ML) {
2116 assert(!isa<CXXConstructorDecl>(MD) && !isa<CXXDestructorDecl>(MD) &&
2117 "can't form pointers to ctors or virtual dtors");
2118
2119 // Calculate the mangled name.
2120 SmallString<256> ThunkName;
2121 llvm::raw_svector_ostream Out(ThunkName);
2122 getMangleContext().mangleVirtualMemPtrThunk(MD, ML, Out);
2123
2124 // If the thunk has been generated previously, just return it.
2125 if (llvm::GlobalValue *GV = CGM.getModule().getNamedValue(Name: ThunkName))
2126 return cast<llvm::Function>(Val: GV);
2127
2128 // Create the llvm::Function.
2129 const CGFunctionInfo &FnInfo =
2130 CGM.getTypes().arrangeUnprototypedMustTailThunk(MD);
2131 llvm::FunctionType *ThunkTy = CGM.getTypes().GetFunctionType(Info: FnInfo);
2132 llvm::Function *ThunkFn =
2133 llvm::Function::Create(Ty: ThunkTy, Linkage: llvm::Function::ExternalLinkage,
2134 N: ThunkName.str(), M: &CGM.getModule());
2135 assert(ThunkFn->getName() == ThunkName && "name was uniqued!");
2136
2137 ThunkFn->setLinkage(MD->isExternallyVisible()
2138 ? llvm::GlobalValue::LinkOnceODRLinkage
2139 : llvm::GlobalValue::InternalLinkage);
2140 if (MD->isExternallyVisible())
2141 ThunkFn->setComdat(CGM.getModule().getOrInsertComdat(Name: ThunkFn->getName()));
2142
2143 CGM.SetLLVMFunctionAttributes(GD: MD, Info: FnInfo, F: ThunkFn, /*IsThunk=*/false);
2144 CGM.SetLLVMFunctionAttributesForDefinition(D: MD, F: ThunkFn);
2145
2146 // Add the "thunk" attribute so that LLVM knows that the return type is
2147 // meaningless. These thunks can be used to call functions with differing
2148 // return types, and the caller is required to cast the prototype
2149 // appropriately to extract the correct value.
2150 ThunkFn->addFnAttr(Kind: "thunk");
2151
2152 // These thunks can be compared, so they are not unnamed.
2153 ThunkFn->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::None);
2154
2155 // Start codegen.
2156 CodeGenFunction CGF(CGM);
2157 CGF.CurGD = GlobalDecl(MD);
2158 CGF.CurFuncIsThunk = true;
2159
2160 // Build FunctionArgs, but only include the implicit 'this' parameter
2161 // declaration.
2162 FunctionArgList FunctionArgs;
2163 buildThisParam(CGF, Params&: FunctionArgs);
2164
2165 // Start defining the function.
2166 CGF.StartFunction(GD: GlobalDecl(), RetTy: FnInfo.getReturnType(), Fn: ThunkFn, FnInfo,
2167 Args: FunctionArgs, Loc: MD->getLocation(), StartLoc: SourceLocation());
2168
2169 ApplyDebugLocation AL(CGF, MD->getLocation());
2170 setCXXABIThisValue(CGF, ThisPtr: loadIncomingCXXThis(CGF));
2171
2172 // Load the vfptr and then callee from the vftable. The callee should have
2173 // adjusted 'this' so that the vfptr is at offset zero.
2174 llvm::Type *ThunkPtrTy = CGF.DefaultPtrTy;
2175 llvm::Value *VTable =
2176 CGF.GetVTablePtr(This: getThisAddress(CGF), VTableTy: CGF.DefaultPtrTy, VTableClass: MD->getParent());
2177
2178 llvm::Value *VFuncPtr = CGF.Builder.CreateConstInBoundsGEP1_64(
2179 Ty: ThunkPtrTy, Ptr: VTable, Idx0: ML.Index, Name: "vfn");
2180 llvm::Value *Callee =
2181 CGF.Builder.CreateAlignedLoad(Ty: ThunkPtrTy, Addr: VFuncPtr, Align: CGF.getPointerAlign());
2182
2183 CGF.EmitMustTailThunk(GD: MD, AdjustedThisPtr: getThisValue(CGF), Callee: {ThunkTy, Callee});
2184
2185 return ThunkFn;
2186}
2187
2188void MicrosoftCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) {
2189 const VBTableGlobals &VBGlobals = enumerateVBTables(RD);
2190 for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) {
2191 const std::unique_ptr<VPtrInfo>& VBT = (*VBGlobals.VBTables)[I];
2192 llvm::GlobalVariable *GV = VBGlobals.Globals[I];
2193 if (GV->isDeclaration())
2194 emitVBTableDefinition(VBT: *VBT, RD, GV);
2195 }
2196}
2197
2198llvm::GlobalVariable *
2199MicrosoftCXXABI::getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD,
2200 llvm::GlobalVariable::LinkageTypes Linkage) {
2201 SmallString<256> OutName;
2202 llvm::raw_svector_ostream Out(OutName);
2203 getMangleContext().mangleCXXVBTable(Derived: RD, BasePath: VBT.MangledPath, Out);
2204 StringRef Name = OutName.str();
2205
2206 llvm::ArrayType *VBTableType =
2207 llvm::ArrayType::get(ElementType: CGM.IntTy, NumElements: 1 + VBT.ObjectWithVPtr->getNumVBases());
2208
2209 assert(!CGM.getModule().getNamedGlobal(Name) &&
2210 "vbtable with this name already exists: mangling bug?");
2211 CharUnits Alignment =
2212 CGM.getContext().getTypeAlignInChars(T: CGM.getContext().IntTy);
2213 llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable(
2214 Name, Ty: VBTableType, Linkage, Alignment: Alignment.getAsAlign());
2215 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2216
2217 if (RD->hasAttr<DLLImportAttr>())
2218 GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
2219 else if (RD->hasAttr<DLLExportAttr>())
2220 GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
2221
2222 if (!GV->hasExternalLinkage())
2223 emitVBTableDefinition(VBT, RD, GV);
2224
2225 return GV;
2226}
2227
2228void MicrosoftCXXABI::emitVBTableDefinition(const VPtrInfo &VBT,
2229 const CXXRecordDecl *RD,
2230 llvm::GlobalVariable *GV) const {
2231 const CXXRecordDecl *ObjectWithVPtr = VBT.ObjectWithVPtr;
2232
2233 assert(RD->getNumVBases() && ObjectWithVPtr->getNumVBases() &&
2234 "should only emit vbtables for classes with vbtables");
2235
2236 const ASTRecordLayout &BaseLayout =
2237 getContext().getASTRecordLayout(D: VBT.IntroducingObject);
2238 const ASTRecordLayout &DerivedLayout = getContext().getASTRecordLayout(D: RD);
2239
2240 SmallVector<llvm::Constant *, 4> Offsets(1 + ObjectWithVPtr->getNumVBases(),
2241 nullptr);
2242
2243 // The offset from ObjectWithVPtr's vbptr to itself always leads.
2244 CharUnits VBPtrOffset = BaseLayout.getVBPtrOffset();
2245 Offsets[0] =
2246 llvm::ConstantInt::getSigned(Ty: CGM.IntTy, V: -VBPtrOffset.getQuantity());
2247
2248 MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext();
2249 for (const auto &I : ObjectWithVPtr->vbases()) {
2250 const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl();
2251 CharUnits Offset = DerivedLayout.getVBaseClassOffset(VBase);
2252 assert(!Offset.isNegative());
2253
2254 // Make it relative to the subobject vbptr.
2255 CharUnits CompleteVBPtrOffset = VBT.NonVirtualOffset + VBPtrOffset;
2256 if (VBT.getVBaseWithVPtr())
2257 CompleteVBPtrOffset +=
2258 DerivedLayout.getVBaseClassOffset(VBase: VBT.getVBaseWithVPtr());
2259 Offset -= CompleteVBPtrOffset;
2260
2261 unsigned VBIndex = Context.getVBTableIndex(Derived: ObjectWithVPtr, VBase);
2262 assert(Offsets[VBIndex] == nullptr && "The same vbindex seen twice?");
2263 Offsets[VBIndex] =
2264 llvm::ConstantInt::getSigned(Ty: CGM.IntTy, V: Offset.getQuantity());
2265 }
2266
2267 assert(Offsets.size() ==
2268 cast<llvm::ArrayType>(GV->getValueType())->getNumElements());
2269 llvm::ArrayType *VBTableType =
2270 llvm::ArrayType::get(ElementType: CGM.IntTy, NumElements: Offsets.size());
2271 llvm::Constant *Init = llvm::ConstantArray::get(T: VBTableType, V: Offsets);
2272 GV->setInitializer(Init);
2273
2274 if (RD->hasAttr<DLLImportAttr>())
2275 GV->setLinkage(llvm::GlobalVariable::AvailableExternallyLinkage);
2276}
2277
2278llvm::Value *MicrosoftCXXABI::performThisAdjustment(
2279 CodeGenFunction &CGF, Address This,
2280 const CXXRecordDecl * /*UnadjustedClass*/, const ThunkInfo &TI) {
2281 const ThisAdjustment &TA = TI.This;
2282 if (TA.isEmpty())
2283 return This.emitRawPointer(CGF);
2284
2285 This = This.withElementType(ElemTy: CGF.Int8Ty);
2286
2287 llvm::Value *V;
2288 if (TA.Virtual.isEmpty()) {
2289 V = This.emitRawPointer(CGF);
2290 } else {
2291 assert(TA.Virtual.Microsoft.VtordispOffset < 0);
2292 // Adjust the this argument based on the vtordisp value.
2293 Address VtorDispPtr =
2294 CGF.Builder.CreateConstInBoundsByteGEP(Addr: This,
2295 Offset: CharUnits::fromQuantity(Quantity: TA.Virtual.Microsoft.VtordispOffset));
2296 VtorDispPtr = VtorDispPtr.withElementType(ElemTy: CGF.Int32Ty);
2297 llvm::Value *VtorDisp = CGF.Builder.CreateLoad(Addr: VtorDispPtr, Name: "vtordisp");
2298 V = CGF.Builder.CreateGEP(Ty: This.getElementType(), Ptr: This.emitRawPointer(CGF),
2299 IdxList: CGF.Builder.CreateNeg(V: VtorDisp));
2300
2301 // Unfortunately, having applied the vtordisp means that we no
2302 // longer really have a known alignment for the vbptr step.
2303 // We'll assume the vbptr is pointer-aligned.
2304
2305 if (TA.Virtual.Microsoft.VBPtrOffset) {
2306 // If the final overrider is defined in a virtual base other than the one
2307 // that holds the vfptr, we have to use a vtordispex thunk which looks up
2308 // the vbtable of the derived class.
2309 assert(TA.Virtual.Microsoft.VBPtrOffset > 0);
2310 assert(TA.Virtual.Microsoft.VBOffsetOffset >= 0);
2311 llvm::Value *VBPtr;
2312 llvm::Value *VBaseOffset = GetVBaseOffsetFromVBPtr(
2313 CGF, Base: Address(V, CGF.Int8Ty, CGF.getPointerAlign()),
2314 VBPtrOffset: -TA.Virtual.Microsoft.VBPtrOffset,
2315 VBTableOffset: TA.Virtual.Microsoft.VBOffsetOffset, VBPtr: &VBPtr);
2316 V = CGF.Builder.CreateInBoundsGEP(Ty: CGF.Int8Ty, Ptr: VBPtr, IdxList: VBaseOffset);
2317 }
2318 }
2319
2320 if (TA.NonVirtual) {
2321 // Non-virtual adjustment might result in a pointer outside the allocated
2322 // object, e.g. if the final overrider class is laid out after the virtual
2323 // base that declares a method in the most derived class.
2324 V = CGF.Builder.CreateConstGEP1_32(Ty: CGF.Int8Ty, Ptr: V, Idx0: TA.NonVirtual);
2325 }
2326
2327 // Don't need to bitcast back, the call CodeGen will handle this.
2328 return V;
2329}
2330
2331llvm::Value *MicrosoftCXXABI::performReturnAdjustment(
2332 CodeGenFunction &CGF, Address Ret,
2333 const CXXRecordDecl * /*UnadjustedClass*/, const ReturnAdjustment &RA) {
2334
2335 if (RA.isEmpty())
2336 return Ret.emitRawPointer(CGF);
2337
2338 Ret = Ret.withElementType(ElemTy: CGF.Int8Ty);
2339
2340 llvm::Value *V = Ret.emitRawPointer(CGF);
2341 if (RA.Virtual.Microsoft.VBIndex) {
2342 assert(RA.Virtual.Microsoft.VBIndex > 0);
2343 int32_t IntSize = CGF.getIntSize().getQuantity();
2344 llvm::Value *VBPtr;
2345 llvm::Value *VBaseOffset =
2346 GetVBaseOffsetFromVBPtr(CGF, Base: Ret, VBPtrOffset: RA.Virtual.Microsoft.VBPtrOffset,
2347 VBTableOffset: IntSize * RA.Virtual.Microsoft.VBIndex, VBPtr: &VBPtr);
2348 V = CGF.Builder.CreateInBoundsGEP(Ty: CGF.Int8Ty, Ptr: VBPtr, IdxList: VBaseOffset);
2349 }
2350
2351 if (RA.NonVirtual)
2352 V = CGF.Builder.CreateConstInBoundsGEP1_32(Ty: CGF.Int8Ty, Ptr: V, Idx0: RA.NonVirtual);
2353
2354 return V;
2355}
2356
2357bool MicrosoftCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr,
2358 QualType elementType) {
2359 // Microsoft seems to completely ignore the possibility of a
2360 // two-argument usual deallocation function.
2361 return elementType.isDestructedType();
2362}
2363
2364bool MicrosoftCXXABI::requiresArrayCookie(const CXXNewExpr *expr) {
2365 // Microsoft seems to completely ignore the possibility of a
2366 // two-argument usual deallocation function.
2367 return expr->getAllocatedType().isDestructedType();
2368}
2369
2370CharUnits MicrosoftCXXABI::getArrayCookieSizeImpl(QualType type) {
2371 // The array cookie is always a size_t; we then pad that out to the
2372 // alignment of the element type.
2373 ASTContext &Ctx = getContext();
2374 return std::max(a: Ctx.getTypeSizeInChars(T: Ctx.getSizeType()),
2375 b: Ctx.getTypeAlignInChars(T: type));
2376}
2377
2378llvm::Value *MicrosoftCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
2379 Address allocPtr,
2380 CharUnits cookieSize) {
2381 Address numElementsPtr = allocPtr.withElementType(ElemTy: CGF.SizeTy);
2382 return CGF.Builder.CreateLoad(Addr: numElementsPtr);
2383}
2384
2385Address MicrosoftCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
2386 Address newPtr,
2387 llvm::Value *numElements,
2388 const CXXNewExpr *expr,
2389 QualType elementType) {
2390 assert(requiresArrayCookie(expr));
2391
2392 // The size of the cookie.
2393 CharUnits cookieSize = getArrayCookieSizeImpl(type: elementType);
2394
2395 // Compute an offset to the cookie.
2396 Address cookiePtr = newPtr;
2397
2398 // Write the number of elements into the appropriate slot.
2399 Address numElementsPtr = cookiePtr.withElementType(ElemTy: CGF.SizeTy);
2400 CGF.Builder.CreateStore(Val: numElements, Addr: numElementsPtr);
2401
2402 // Finally, compute a pointer to the actual data buffer by skipping
2403 // over the cookie completely.
2404 return CGF.Builder.CreateConstInBoundsByteGEP(Addr: newPtr, Offset: cookieSize);
2405}
2406
2407static void emitGlobalDtorWithTLRegDtor(CodeGenFunction &CGF, const VarDecl &VD,
2408 llvm::FunctionCallee Dtor,
2409 llvm::Constant *Addr) {
2410 // Create a function which calls the destructor.
2411 llvm::Constant *DtorStub = CGF.createAtExitStub(VD, Dtor, Addr);
2412
2413 // extern "C" int __tlregdtor(void (*f)(void));
2414 llvm::FunctionType *TLRegDtorTy = llvm::FunctionType::get(
2415 Result: CGF.IntTy, Params: DtorStub->getType(), /*isVarArg=*/false);
2416
2417 llvm::FunctionCallee TLRegDtor = CGF.CGM.CreateRuntimeFunction(
2418 Ty: TLRegDtorTy, Name: "__tlregdtor", ExtraAttrs: llvm::AttributeList(), /*Local=*/true);
2419 if (llvm::Function *TLRegDtorFn =
2420 dyn_cast<llvm::Function>(Val: TLRegDtor.getCallee()))
2421 TLRegDtorFn->setDoesNotThrow();
2422
2423 CGF.EmitNounwindRuntimeCall(callee: TLRegDtor, args: DtorStub);
2424}
2425
2426void MicrosoftCXXABI::registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
2427 llvm::FunctionCallee Dtor,
2428 llvm::Constant *Addr) {
2429 if (D.isNoDestroy(CGM.getContext()))
2430 return;
2431
2432 if (D.getTLSKind())
2433 return emitGlobalDtorWithTLRegDtor(CGF, VD: D, Dtor, Addr);
2434
2435 // HLSL doesn't support atexit.
2436 if (CGM.getLangOpts().HLSL)
2437 return CGM.AddCXXDtorEntry(DtorFn: Dtor, Object: Addr);
2438
2439 // The default behavior is to use atexit.
2440 CGF.registerGlobalDtorWithAtExit(D, fn: Dtor, addr: Addr);
2441}
2442
2443void MicrosoftCXXABI::EmitThreadLocalInitFuncs(
2444 CodeGenModule &CGM, ArrayRef<const VarDecl *> CXXThreadLocals,
2445 ArrayRef<llvm::Function *> CXXThreadLocalInits,
2446 ArrayRef<const VarDecl *> CXXThreadLocalInitVars) {
2447 if (CXXThreadLocalInits.empty())
2448 return;
2449
2450 CGM.AppendLinkerOptions(Opts: CGM.getTarget().getTriple().getArch() ==
2451 llvm::Triple::x86
2452 ? "/include:___dyn_tls_init@12"
2453 : "/include:__dyn_tls_init");
2454
2455 // This will create a GV in the .CRT$XDU section. It will point to our
2456 // initialization function. The CRT will call all of these function
2457 // pointers at start-up time and, eventually, at thread-creation time.
2458 auto AddToXDU = [&CGM](llvm::Function *InitFunc) {
2459 llvm::GlobalVariable *InitFuncPtr = new llvm::GlobalVariable(
2460 CGM.getModule(), InitFunc->getType(), /*isConstant=*/true,
2461 llvm::GlobalVariable::InternalLinkage, InitFunc,
2462 Twine(InitFunc->getName(), "$initializer$"));
2463 InitFuncPtr->setSection(".CRT$XDU");
2464 // This variable has discardable linkage, we have to add it to @llvm.used to
2465 // ensure it won't get discarded.
2466 CGM.addUsedGlobal(GV: InitFuncPtr);
2467 return InitFuncPtr;
2468 };
2469
2470 std::vector<llvm::Function *> NonComdatInits;
2471 for (size_t I = 0, E = CXXThreadLocalInitVars.size(); I != E; ++I) {
2472 llvm::GlobalVariable *GV = cast<llvm::GlobalVariable>(
2473 Val: CGM.GetGlobalValue(Ref: CGM.getMangledName(GD: CXXThreadLocalInitVars[I])));
2474 llvm::Function *F = CXXThreadLocalInits[I];
2475
2476 // If the GV is already in a comdat group, then we have to join it.
2477 if (llvm::Comdat *C = GV->getComdat())
2478 AddToXDU(F)->setComdat(C);
2479 else
2480 NonComdatInits.push_back(x: F);
2481 }
2482
2483 if (!NonComdatInits.empty()) {
2484 llvm::FunctionType *FTy =
2485 llvm::FunctionType::get(Result: CGM.VoidTy, /*isVarArg=*/false);
2486 llvm::Function *InitFunc = CGM.CreateGlobalInitOrCleanUpFunction(
2487 ty: FTy, name: "__tls_init", FI: CGM.getTypes().arrangeNullaryFunction(),
2488 Loc: SourceLocation(), /*TLS=*/true);
2489 CodeGenFunction(CGM).GenerateCXXGlobalInitFunc(Fn: InitFunc, CXXThreadLocals: NonComdatInits);
2490
2491 AddToXDU(InitFunc);
2492 }
2493}
2494
2495static llvm::GlobalValue *getTlsGuardVar(CodeGenModule &CGM) {
2496 // __tls_guard comes from the MSVC runtime and reflects
2497 // whether TLS has been initialized for a particular thread.
2498 // It is set from within __dyn_tls_init by the runtime.
2499 // Every library and executable has its own variable.
2500 llvm::Type *VTy = llvm::Type::getInt8Ty(C&: CGM.getLLVMContext());
2501 llvm::Constant *TlsGuardConstant =
2502 CGM.CreateRuntimeVariable(Ty: VTy, Name: "__tls_guard");
2503 llvm::GlobalValue *TlsGuard = cast<llvm::GlobalValue>(Val: TlsGuardConstant);
2504
2505 TlsGuard->setThreadLocal(true);
2506
2507 return TlsGuard;
2508}
2509
2510static llvm::FunctionCallee getDynTlsOnDemandInitFn(CodeGenModule &CGM) {
2511 // __dyn_tls_on_demand_init comes from the MSVC runtime and triggers
2512 // dynamic TLS initialization by calling __dyn_tls_init internally.
2513 llvm::FunctionType *FTy =
2514 llvm::FunctionType::get(Result: llvm::Type::getVoidTy(C&: CGM.getLLVMContext()), Params: {},
2515 /*isVarArg=*/false);
2516 return CGM.CreateRuntimeFunction(
2517 Ty: FTy, Name: "__dyn_tls_on_demand_init",
2518 ExtraAttrs: llvm::AttributeList::get(C&: CGM.getLLVMContext(),
2519 Index: llvm::AttributeList::FunctionIndex,
2520 Kinds: llvm::Attribute::NoUnwind),
2521 /*Local=*/true);
2522}
2523
2524static void emitTlsGuardCheck(CodeGenFunction &CGF, llvm::GlobalValue *TlsGuard,
2525 llvm::BasicBlock *DynInitBB,
2526 llvm::BasicBlock *ContinueBB) {
2527 llvm::LoadInst *TlsGuardValue =
2528 CGF.Builder.CreateLoad(Addr: Address(TlsGuard, CGF.Int8Ty, CharUnits::One()));
2529 llvm::Value *CmpResult =
2530 CGF.Builder.CreateICmpEQ(LHS: TlsGuardValue, RHS: CGF.Builder.getInt8(C: 0));
2531 CGF.Builder.CreateCondBr(Cond: CmpResult, True: DynInitBB, False: ContinueBB);
2532}
2533
2534static void emitDynamicTlsInitializationCall(CodeGenFunction &CGF,
2535 llvm::GlobalValue *TlsGuard,
2536 llvm::BasicBlock *ContinueBB) {
2537 llvm::FunctionCallee Initializer = getDynTlsOnDemandInitFn(CGM&: CGF.CGM);
2538 llvm::Function *InitializerFunction =
2539 cast<llvm::Function>(Val: Initializer.getCallee());
2540 llvm::CallInst *CallVal = CGF.Builder.CreateCall(Callee: InitializerFunction);
2541 CallVal->setCallingConv(InitializerFunction->getCallingConv());
2542
2543 CGF.Builder.CreateBr(Dest: ContinueBB);
2544}
2545
2546static void emitDynamicTlsInitialization(CodeGenFunction &CGF) {
2547 llvm::BasicBlock *DynInitBB =
2548 CGF.createBasicBlock(name: "dyntls.dyn_init", parent: CGF.CurFn);
2549 llvm::BasicBlock *ContinueBB =
2550 CGF.createBasicBlock(name: "dyntls.continue", parent: CGF.CurFn);
2551
2552 llvm::GlobalValue *TlsGuard = getTlsGuardVar(CGM&: CGF.CGM);
2553
2554 emitTlsGuardCheck(CGF, TlsGuard, DynInitBB, ContinueBB);
2555 CGF.Builder.SetInsertPoint(DynInitBB);
2556 emitDynamicTlsInitializationCall(CGF, TlsGuard, ContinueBB);
2557 CGF.Builder.SetInsertPoint(ContinueBB);
2558}
2559
2560LValue MicrosoftCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
2561 const VarDecl *VD,
2562 QualType LValType) {
2563 // Dynamic TLS initialization works by checking the state of a
2564 // guard variable (__tls_guard) to see whether TLS initialization
2565 // for a thread has happend yet.
2566 // If not, the initialization is triggered on-demand
2567 // by calling __dyn_tls_on_demand_init.
2568 emitDynamicTlsInitialization(CGF);
2569
2570 // Emit the variable just like any regular global variable.
2571
2572 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(D: VD);
2573 llvm::Type *RealVarTy = CGF.getTypes().ConvertTypeForMem(T: VD->getType());
2574
2575 CharUnits Alignment = CGF.getContext().getDeclAlign(D: VD);
2576 Address Addr(V, RealVarTy, Alignment);
2577
2578 LValue LV = VD->getType()->isReferenceType()
2579 ? CGF.EmitLoadOfReferenceLValue(RefAddr: Addr, RefTy: VD->getType(),
2580 Source: AlignmentSource::Decl)
2581 : CGF.MakeAddrLValue(Addr, T: LValType, Source: AlignmentSource::Decl);
2582 return LV;
2583}
2584
2585static ConstantAddress getInitThreadEpochPtr(CodeGenModule &CGM) {
2586 StringRef VarName("_Init_thread_epoch");
2587 CharUnits Align = CGM.getIntAlign();
2588 if (auto *GV = CGM.getModule().getNamedGlobal(Name: VarName))
2589 return ConstantAddress(GV, GV->getValueType(), Align);
2590 auto *GV = new llvm::GlobalVariable(
2591 CGM.getModule(), CGM.IntTy,
2592 /*isConstant=*/false, llvm::GlobalVariable::ExternalLinkage,
2593 /*Initializer=*/nullptr, VarName,
2594 /*InsertBefore=*/nullptr, llvm::GlobalVariable::GeneralDynamicTLSModel);
2595 GV->setAlignment(Align.getAsAlign());
2596 return ConstantAddress(GV, GV->getValueType(), Align);
2597}
2598
2599static llvm::FunctionCallee getInitThreadHeaderFn(CodeGenModule &CGM) {
2600 llvm::FunctionType *FTy =
2601 llvm::FunctionType::get(Result: llvm::Type::getVoidTy(C&: CGM.getLLVMContext()),
2602 Params: CGM.DefaultPtrTy, /*isVarArg=*/false);
2603 return CGM.CreateRuntimeFunction(
2604 Ty: FTy, Name: "_Init_thread_header",
2605 ExtraAttrs: llvm::AttributeList::get(C&: CGM.getLLVMContext(),
2606 Index: llvm::AttributeList::FunctionIndex,
2607 Kinds: llvm::Attribute::NoUnwind),
2608 /*Local=*/true);
2609}
2610
2611static llvm::FunctionCallee getInitThreadFooterFn(CodeGenModule &CGM) {
2612 llvm::FunctionType *FTy =
2613 llvm::FunctionType::get(Result: llvm::Type::getVoidTy(C&: CGM.getLLVMContext()),
2614 Params: CGM.DefaultPtrTy, /*isVarArg=*/false);
2615 return CGM.CreateRuntimeFunction(
2616 Ty: FTy, Name: "_Init_thread_footer",
2617 ExtraAttrs: llvm::AttributeList::get(C&: CGM.getLLVMContext(),
2618 Index: llvm::AttributeList::FunctionIndex,
2619 Kinds: llvm::Attribute::NoUnwind),
2620 /*Local=*/true);
2621}
2622
2623static llvm::FunctionCallee getInitThreadAbortFn(CodeGenModule &CGM) {
2624 llvm::FunctionType *FTy =
2625 llvm::FunctionType::get(Result: llvm::Type::getVoidTy(C&: CGM.getLLVMContext()),
2626 Params: CGM.DefaultPtrTy, /*isVarArg=*/false);
2627 return CGM.CreateRuntimeFunction(
2628 Ty: FTy, Name: "_Init_thread_abort",
2629 ExtraAttrs: llvm::AttributeList::get(C&: CGM.getLLVMContext(),
2630 Index: llvm::AttributeList::FunctionIndex,
2631 Kinds: llvm::Attribute::NoUnwind),
2632 /*Local=*/true);
2633}
2634
2635namespace {
2636struct ResetGuardBit final : EHScopeStack::Cleanup {
2637 Address Guard;
2638 unsigned GuardNum;
2639 ResetGuardBit(Address Guard, unsigned GuardNum)
2640 : Guard(Guard), GuardNum(GuardNum) {}
2641
2642 void Emit(CodeGenFunction &CGF, Flags flags) override {
2643 // Reset the bit in the mask so that the static variable may be
2644 // reinitialized.
2645 CGBuilderTy &Builder = CGF.Builder;
2646 llvm::LoadInst *LI = Builder.CreateLoad(Addr: Guard);
2647 llvm::ConstantInt *Mask =
2648 llvm::ConstantInt::getSigned(Ty: CGF.IntTy, V: ~(1ULL << GuardNum));
2649 Builder.CreateStore(Val: Builder.CreateAnd(LHS: LI, RHS: Mask), Addr: Guard);
2650 }
2651};
2652
2653struct CallInitThreadAbort final : EHScopeStack::Cleanup {
2654 llvm::Value *Guard;
2655 CallInitThreadAbort(RawAddress Guard) : Guard(Guard.getPointer()) {}
2656
2657 void Emit(CodeGenFunction &CGF, Flags flags) override {
2658 // Calling _Init_thread_abort will reset the guard's state.
2659 CGF.EmitNounwindRuntimeCall(callee: getInitThreadAbortFn(CGM&: CGF.CGM), args: Guard);
2660 }
2661};
2662}
2663
2664void MicrosoftCXXABI::EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
2665 llvm::GlobalVariable *GV,
2666 bool PerformInit) {
2667 // MSVC only uses guards for static locals.
2668 if (!D.isStaticLocal()) {
2669 assert(GV->hasWeakLinkage() || GV->hasLinkOnceLinkage());
2670 // GlobalOpt is allowed to discard the initializer, so use linkonce_odr.
2671 llvm::Function *F = CGF.CurFn;
2672 F->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage);
2673 F->setComdat(CGM.getModule().getOrInsertComdat(Name: F->getName()));
2674 CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit);
2675 return;
2676 }
2677
2678 bool ThreadlocalStatic = D.getTLSKind();
2679 bool ThreadsafeStatic = getContext().getLangOpts().ThreadsafeStatics;
2680
2681 // Thread-safe static variables which aren't thread-specific have a
2682 // per-variable guard.
2683 bool HasPerVariableGuard = ThreadsafeStatic && !ThreadlocalStatic;
2684
2685 CGBuilderTy &Builder = CGF.Builder;
2686 llvm::IntegerType *GuardTy = CGF.Int32Ty;
2687 llvm::ConstantInt *Zero = llvm::ConstantInt::get(Ty: GuardTy, V: 0);
2688 CharUnits GuardAlign = CharUnits::fromQuantity(Quantity: 4);
2689
2690 // Get the guard variable for this function if we have one already.
2691 GuardInfo *GI = nullptr;
2692 if (ThreadlocalStatic)
2693 GI = &ThreadLocalGuardVariableMap[D.getDeclContext()];
2694 else if (!ThreadsafeStatic)
2695 GI = &GuardVariableMap[D.getDeclContext()];
2696
2697 llvm::GlobalVariable *GuardVar = GI ? GI->Guard : nullptr;
2698 unsigned GuardNum;
2699 if (D.isExternallyVisible()) {
2700 // Externally visible variables have to be numbered in Sema to properly
2701 // handle unreachable VarDecls.
2702 GuardNum = getContext().getStaticLocalNumber(VD: &D);
2703 assert(GuardNum > 0);
2704 GuardNum--;
2705 } else if (HasPerVariableGuard) {
2706 GuardNum = ThreadSafeGuardNumMap[D.getDeclContext()]++;
2707 } else {
2708 // Non-externally visible variables are numbered here in CodeGen.
2709 GuardNum = GI->BitIndex++;
2710 }
2711
2712 if (!HasPerVariableGuard && GuardNum >= 32) {
2713 if (D.isExternallyVisible())
2714 ErrorUnsupportedABI(CGF, S: "more than 32 guarded initializations");
2715 GuardNum %= 32;
2716 GuardVar = nullptr;
2717 }
2718
2719 if (!GuardVar) {
2720 // Mangle the name for the guard.
2721 SmallString<256> GuardName;
2722 {
2723 llvm::raw_svector_ostream Out(GuardName);
2724 if (HasPerVariableGuard)
2725 getMangleContext().mangleThreadSafeStaticGuardVariable(VD: &D, GuardNum,
2726 Out);
2727 else
2728 getMangleContext().mangleStaticGuardVariable(D: &D, Out);
2729 }
2730
2731 // Create the guard variable with a zero-initializer. Just absorb linkage,
2732 // visibility and dll storage class from the guarded variable.
2733 GuardVar =
2734 new llvm::GlobalVariable(CGM.getModule(), GuardTy, /*isConstant=*/false,
2735 GV->getLinkage(), Zero, GuardName.str());
2736 GuardVar->setVisibility(GV->getVisibility());
2737 GuardVar->setDLLStorageClass(GV->getDLLStorageClass());
2738 GuardVar->setAlignment(GuardAlign.getAsAlign());
2739 if (GuardVar->isWeakForLinker())
2740 GuardVar->setComdat(
2741 CGM.getModule().getOrInsertComdat(Name: GuardVar->getName()));
2742 if (D.getTLSKind())
2743 CGM.setTLSMode(GV: GuardVar, D);
2744 if (GI && !HasPerVariableGuard)
2745 GI->Guard = GuardVar;
2746 }
2747
2748 ConstantAddress GuardAddr(GuardVar, GuardTy, GuardAlign);
2749
2750 assert(GuardVar->getLinkage() == GV->getLinkage() &&
2751 "static local from the same function had different linkage");
2752
2753 if (!HasPerVariableGuard) {
2754 // Pseudo code for the test:
2755 // if (!(GuardVar & MyGuardBit)) {
2756 // GuardVar |= MyGuardBit;
2757 // ... initialize the object ...;
2758 // }
2759
2760 // Test our bit from the guard variable.
2761 llvm::ConstantInt *Bit = llvm::ConstantInt::get(Ty: GuardTy, V: 1ULL << GuardNum);
2762 llvm::LoadInst *LI = Builder.CreateLoad(Addr: GuardAddr);
2763 llvm::Value *NeedsInit =
2764 Builder.CreateICmpEQ(LHS: Builder.CreateAnd(LHS: LI, RHS: Bit), RHS: Zero);
2765 llvm::BasicBlock *InitBlock = CGF.createBasicBlock(name: "init");
2766 llvm::BasicBlock *EndBlock = CGF.createBasicBlock(name: "init.end");
2767 CGF.EmitCXXGuardedInitBranch(NeedsInit, InitBlock, NoInitBlock: EndBlock,
2768 Kind: CodeGenFunction::GuardKind::VariableGuard, D: &D);
2769
2770 // Set our bit in the guard variable and emit the initializer and add a global
2771 // destructor if appropriate.
2772 CGF.EmitBlock(BB: InitBlock);
2773 Builder.CreateStore(Val: Builder.CreateOr(LHS: LI, RHS: Bit), Addr: GuardAddr);
2774 CGF.EHStack.pushCleanup<ResetGuardBit>(Kind: EHCleanup, A: GuardAddr, A: GuardNum);
2775 CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit);
2776 CGF.PopCleanupBlock();
2777 Builder.CreateBr(Dest: EndBlock);
2778
2779 // Continue.
2780 CGF.EmitBlock(BB: EndBlock);
2781 } else {
2782 // Pseudo code for the test:
2783 // if (TSS > _Init_thread_epoch) {
2784 // _Init_thread_header(&TSS);
2785 // if (TSS == -1) {
2786 // ... initialize the object ...;
2787 // _Init_thread_footer(&TSS);
2788 // }
2789 // }
2790 //
2791 // The algorithm is almost identical to what can be found in the appendix
2792 // found in N2325.
2793
2794 // This BasicBLock determines whether or not we have any work to do.
2795 llvm::LoadInst *FirstGuardLoad = Builder.CreateLoad(Addr: GuardAddr);
2796 FirstGuardLoad->setOrdering(llvm::AtomicOrdering::Unordered);
2797 llvm::LoadInst *InitThreadEpoch =
2798 Builder.CreateLoad(Addr: getInitThreadEpochPtr(CGM));
2799 llvm::Value *IsUninitialized =
2800 Builder.CreateICmpSGT(LHS: FirstGuardLoad, RHS: InitThreadEpoch);
2801 llvm::BasicBlock *AttemptInitBlock = CGF.createBasicBlock(name: "init.attempt");
2802 llvm::BasicBlock *EndBlock = CGF.createBasicBlock(name: "init.end");
2803 CGF.EmitCXXGuardedInitBranch(NeedsInit: IsUninitialized, InitBlock: AttemptInitBlock, NoInitBlock: EndBlock,
2804 Kind: CodeGenFunction::GuardKind::VariableGuard, D: &D);
2805
2806 // This BasicBlock attempts to determine whether or not this thread is
2807 // responsible for doing the initialization.
2808 CGF.EmitBlock(BB: AttemptInitBlock);
2809 CGF.EmitNounwindRuntimeCall(callee: getInitThreadHeaderFn(CGM),
2810 args: GuardAddr.getPointer());
2811 llvm::LoadInst *SecondGuardLoad = Builder.CreateLoad(Addr: GuardAddr);
2812 SecondGuardLoad->setOrdering(llvm::AtomicOrdering::Unordered);
2813 llvm::Value *ShouldDoInit =
2814 Builder.CreateICmpEQ(LHS: SecondGuardLoad, RHS: getAllOnesInt());
2815 llvm::BasicBlock *InitBlock = CGF.createBasicBlock(name: "init");
2816 Builder.CreateCondBr(Cond: ShouldDoInit, True: InitBlock, False: EndBlock);
2817
2818 // Ok, we ended up getting selected as the initializing thread.
2819 CGF.EmitBlock(BB: InitBlock);
2820 CGF.EHStack.pushCleanup<CallInitThreadAbort>(Kind: EHCleanup, A: GuardAddr);
2821 CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit);
2822 CGF.PopCleanupBlock();
2823 CGF.EmitNounwindRuntimeCall(callee: getInitThreadFooterFn(CGM),
2824 args: GuardAddr.getPointer());
2825 Builder.CreateBr(Dest: EndBlock);
2826
2827 CGF.EmitBlock(BB: EndBlock);
2828 }
2829}
2830
2831bool MicrosoftCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
2832 // Null-ness for function memptrs only depends on the first field, which is
2833 // the function pointer. The rest don't matter, so we can zero initialize.
2834 if (MPT->isMemberFunctionPointer())
2835 return true;
2836
2837 // The virtual base adjustment field is always -1 for null, so if we have one
2838 // we can't zero initialize. The field offset is sometimes also -1 if 0 is a
2839 // valid field offset.
2840 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
2841 MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
2842 return (!inheritanceModelHasVBTableOffsetField(Inheritance) &&
2843 RD->nullFieldOffsetIsZero());
2844}
2845
2846llvm::Type *
2847MicrosoftCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
2848 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
2849 MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
2850 llvm::SmallVector<llvm::Type *, 4> fields;
2851 if (MPT->isMemberFunctionPointer())
2852 fields.push_back(Elt: CGM.VoidPtrTy); // FunctionPointerOrVirtualThunk
2853 else
2854 fields.push_back(Elt: CGM.IntTy); // FieldOffset
2855
2856 if (inheritanceModelHasNVOffsetField(IsMemberFunction: MPT->isMemberFunctionPointer(),
2857 Inheritance))
2858 fields.push_back(Elt: CGM.IntTy);
2859 if (inheritanceModelHasVBPtrOffsetField(Inheritance))
2860 fields.push_back(Elt: CGM.IntTy);
2861 if (inheritanceModelHasVBTableOffsetField(Inheritance))
2862 fields.push_back(Elt: CGM.IntTy); // VirtualBaseAdjustmentOffset
2863
2864 if (fields.size() == 1)
2865 return fields[0];
2866 return llvm::StructType::get(Context&: CGM.getLLVMContext(), Elements: fields);
2867}
2868
2869void MicrosoftCXXABI::
2870GetNullMemberPointerFields(const MemberPointerType *MPT,
2871 llvm::SmallVectorImpl<llvm::Constant *> &fields) {
2872 assert(fields.empty());
2873 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
2874 MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
2875 if (MPT->isMemberFunctionPointer()) {
2876 // FunctionPointerOrVirtualThunk
2877 fields.push_back(Elt: llvm::Constant::getNullValue(Ty: CGM.VoidPtrTy));
2878 } else {
2879 if (RD->nullFieldOffsetIsZero())
2880 fields.push_back(Elt: getZeroInt()); // FieldOffset
2881 else
2882 fields.push_back(Elt: getAllOnesInt()); // FieldOffset
2883 }
2884
2885 if (inheritanceModelHasNVOffsetField(IsMemberFunction: MPT->isMemberFunctionPointer(),
2886 Inheritance))
2887 fields.push_back(Elt: getZeroInt());
2888 if (inheritanceModelHasVBPtrOffsetField(Inheritance))
2889 fields.push_back(Elt: getZeroInt());
2890 if (inheritanceModelHasVBTableOffsetField(Inheritance))
2891 fields.push_back(Elt: getAllOnesInt());
2892}
2893
2894llvm::Constant *
2895MicrosoftCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
2896 llvm::SmallVector<llvm::Constant *, 4> fields;
2897 GetNullMemberPointerFields(MPT, fields);
2898 if (fields.size() == 1)
2899 return fields[0];
2900 llvm::Constant *Res = llvm::ConstantStruct::getAnon(V: fields);
2901 assert(Res->getType() == ConvertMemberPointerType(MPT));
2902 return Res;
2903}
2904
2905llvm::Constant *
2906MicrosoftCXXABI::EmitFullMemberPointer(llvm::Constant *FirstField,
2907 bool IsMemberFunction,
2908 const CXXRecordDecl *RD,
2909 CharUnits NonVirtualBaseAdjustment,
2910 unsigned VBTableIndex) {
2911 MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
2912
2913 // Single inheritance class member pointer are represented as scalars instead
2914 // of aggregates.
2915 if (inheritanceModelHasOnlyOneField(IsMemberFunction, Inheritance))
2916 return FirstField;
2917
2918 llvm::SmallVector<llvm::Constant *, 4> fields;
2919 fields.push_back(Elt: FirstField);
2920
2921 if (inheritanceModelHasNVOffsetField(IsMemberFunction, Inheritance))
2922 fields.push_back(Elt: llvm::ConstantInt::getSigned(
2923 Ty: CGM.IntTy, V: NonVirtualBaseAdjustment.getQuantity()));
2924
2925 if (inheritanceModelHasVBPtrOffsetField(Inheritance)) {
2926 CharUnits Offs = CharUnits::Zero();
2927 if (VBTableIndex)
2928 Offs = getContext().getASTRecordLayout(D: RD).getVBPtrOffset();
2929 fields.push_back(Elt: llvm::ConstantInt::get(Ty: CGM.IntTy, V: Offs.getQuantity()));
2930 }
2931
2932 // The rest of the fields are adjusted by conversions to a more derived class.
2933 if (inheritanceModelHasVBTableOffsetField(Inheritance))
2934 fields.push_back(Elt: llvm::ConstantInt::get(Ty: CGM.IntTy, V: VBTableIndex));
2935
2936 return llvm::ConstantStruct::getAnon(V: fields);
2937}
2938
2939llvm::Constant *
2940MicrosoftCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
2941 CharUnits offset) {
2942 return EmitMemberDataPointer(RD: MPT->getMostRecentCXXRecordDecl(), offset);
2943}
2944
2945llvm::Constant *MicrosoftCXXABI::EmitMemberDataPointer(const CXXRecordDecl *RD,
2946 CharUnits offset) {
2947 if (RD->getMSInheritanceModel() ==
2948 MSInheritanceModel::Virtual)
2949 offset -= getContext().getOffsetOfBaseWithVBPtr(RD);
2950 llvm::Constant *FirstField =
2951 llvm::ConstantInt::get(Ty: CGM.IntTy, V: offset.getQuantity());
2952 return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/false, RD,
2953 NonVirtualBaseAdjustment: CharUnits::Zero(), /*VBTableIndex=*/0);
2954}
2955
2956llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const APValue &MP,
2957 QualType MPType) {
2958 const MemberPointerType *DstTy = MPType->castAs<MemberPointerType>();
2959 const ValueDecl *MPD = MP.getMemberPointerDecl();
2960 if (!MPD)
2961 return EmitNullMemberPointer(MPT: DstTy);
2962
2963 ASTContext &Ctx = getContext();
2964 ArrayRef<const CXXRecordDecl *> MemberPointerPath = MP.getMemberPointerPath();
2965
2966 llvm::Constant *C;
2967 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: MPD)) {
2968 C = EmitMemberFunctionPointer(MD);
2969 } else {
2970 // For a pointer to data member, start off with the offset of the field in
2971 // the class in which it was declared, and convert from there if necessary.
2972 // For indirect field decls, get the outermost anonymous field and use the
2973 // parent class.
2974 Ctx.recordMemberDataPointerEvaluation(VD: MPD);
2975 CharUnits FieldOffset = Ctx.toCharUnitsFromBits(BitSize: Ctx.getFieldOffset(FD: MPD));
2976 const FieldDecl *FD = dyn_cast<FieldDecl>(Val: MPD);
2977 if (!FD)
2978 FD = cast<FieldDecl>(Val: *cast<IndirectFieldDecl>(Val: MPD)->chain_begin());
2979 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Val: FD->getParent());
2980 RD = RD->getMostRecentDecl();
2981 C = EmitMemberDataPointer(RD, offset: FieldOffset);
2982 }
2983
2984 if (!MemberPointerPath.empty()) {
2985 const CXXRecordDecl *SrcRD = cast<CXXRecordDecl>(Val: MPD->getDeclContext());
2986 const MemberPointerType *SrcTy =
2987 Ctx.getMemberPointerType(T: DstTy->getPointeeType(),
2988 /*Qualifier=*/std::nullopt, Cls: SrcRD)
2989 ->castAs<MemberPointerType>();
2990
2991 bool DerivedMember = MP.isMemberPointerToDerivedMember();
2992 SmallVector<const CXXBaseSpecifier *, 4> DerivedToBasePath;
2993 const CXXRecordDecl *PrevRD = SrcRD;
2994 for (const CXXRecordDecl *PathElem : MemberPointerPath) {
2995 const CXXRecordDecl *Base = nullptr;
2996 const CXXRecordDecl *Derived = nullptr;
2997 if (DerivedMember) {
2998 Base = PathElem;
2999 Derived = PrevRD;
3000 } else {
3001 Base = PrevRD;
3002 Derived = PathElem;
3003 }
3004 for (const CXXBaseSpecifier &BS : Derived->bases())
3005 if (BS.getType()->getAsCXXRecordDecl()->getCanonicalDecl() ==
3006 Base->getCanonicalDecl())
3007 DerivedToBasePath.push_back(Elt: &BS);
3008 PrevRD = PathElem;
3009 }
3010 assert(DerivedToBasePath.size() == MemberPointerPath.size());
3011
3012 CastKind CK = DerivedMember ? CK_DerivedToBaseMemberPointer
3013 : CK_BaseToDerivedMemberPointer;
3014 C = EmitMemberPointerConversion(SrcTy, DstTy, CK, PathBegin: DerivedToBasePath.begin(),
3015 PathEnd: DerivedToBasePath.end(), Src: C);
3016 }
3017 return C;
3018}
3019
3020llvm::Constant *
3021MicrosoftCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) {
3022 assert(MD->isInstance() && "Member function must not be static!");
3023
3024 CharUnits NonVirtualBaseAdjustment = CharUnits::Zero();
3025 const CXXRecordDecl *RD = MD->getParent()->getMostRecentDecl();
3026 CodeGenTypes &Types = CGM.getTypes();
3027
3028 unsigned VBTableIndex = 0;
3029 llvm::Constant *FirstField;
3030 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
3031 if (!MD->isVirtual()) {
3032 llvm::Type *Ty;
3033 // Check whether the function has a computable LLVM signature.
3034 if (Types.isFuncTypeConvertible(FT: FPT)) {
3035 // The function has a computable LLVM signature; use the correct type.
3036 Ty = Types.GetFunctionType(Info: Types.arrangeCXXMethodDeclaration(MD));
3037 } else {
3038 // Use an arbitrary non-function type to tell GetAddrOfFunction that the
3039 // function type is incomplete.
3040 Ty = CGM.PtrDiffTy;
3041 }
3042 FirstField = CGM.GetAddrOfFunction(GD: MD, Ty);
3043 } else {
3044 auto &VTableContext = CGM.getMicrosoftVTableContext();
3045 MethodVFTableLocation ML = VTableContext.getMethodVFTableLocation(GD: MD);
3046 FirstField = EmitVirtualMemPtrThunk(MD, ML);
3047 // Include the vfptr adjustment if the method is in a non-primary vftable.
3048 NonVirtualBaseAdjustment += ML.VFPtrOffset;
3049 if (ML.VBase)
3050 VBTableIndex = VTableContext.getVBTableIndex(Derived: RD, VBase: ML.VBase) * 4;
3051 }
3052
3053 if (VBTableIndex == 0 &&
3054 RD->getMSInheritanceModel() ==
3055 MSInheritanceModel::Virtual)
3056 NonVirtualBaseAdjustment -= getContext().getOffsetOfBaseWithVBPtr(RD);
3057
3058 // The rest of the fields are common with data member pointers.
3059 return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/true, RD,
3060 NonVirtualBaseAdjustment, VBTableIndex);
3061}
3062
3063/// Member pointers are the same if they're either bitwise identical *or* both
3064/// null. Null-ness for function members is determined by the first field,
3065/// while for data member pointers we must compare all fields.
3066llvm::Value *
3067MicrosoftCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
3068 llvm::Value *L,
3069 llvm::Value *R,
3070 const MemberPointerType *MPT,
3071 bool Inequality) {
3072 CGBuilderTy &Builder = CGF.Builder;
3073
3074 // Handle != comparisons by switching the sense of all boolean operations.
3075 llvm::ICmpInst::Predicate Eq;
3076 llvm::Instruction::BinaryOps And, Or;
3077 if (Inequality) {
3078 Eq = llvm::ICmpInst::ICMP_NE;
3079 And = llvm::Instruction::Or;
3080 Or = llvm::Instruction::And;
3081 } else {
3082 Eq = llvm::ICmpInst::ICMP_EQ;
3083 And = llvm::Instruction::And;
3084 Or = llvm::Instruction::Or;
3085 }
3086
3087 // If this is a single field member pointer (single inheritance), this is a
3088 // single icmp.
3089 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
3090 MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
3091 if (inheritanceModelHasOnlyOneField(IsMemberFunction: MPT->isMemberFunctionPointer(),
3092 Inheritance))
3093 return Builder.CreateICmp(P: Eq, LHS: L, RHS: R);
3094
3095 // Compare the first field.
3096 llvm::Value *L0 = Builder.CreateExtractValue(Agg: L, Idxs: 0, Name: "lhs.0");
3097 llvm::Value *R0 = Builder.CreateExtractValue(Agg: R, Idxs: 0, Name: "rhs.0");
3098 llvm::Value *Cmp0 = Builder.CreateICmp(P: Eq, LHS: L0, RHS: R0, Name: "memptr.cmp.first");
3099
3100 // Compare everything other than the first field.
3101 llvm::Value *Res = nullptr;
3102 llvm::StructType *LType = cast<llvm::StructType>(Val: L->getType());
3103 for (unsigned I = 1, E = LType->getNumElements(); I != E; ++I) {
3104 llvm::Value *LF = Builder.CreateExtractValue(Agg: L, Idxs: I);
3105 llvm::Value *RF = Builder.CreateExtractValue(Agg: R, Idxs: I);
3106 llvm::Value *Cmp = Builder.CreateICmp(P: Eq, LHS: LF, RHS: RF, Name: "memptr.cmp.rest");
3107 if (Res)
3108 Res = Builder.CreateBinOp(Opc: And, LHS: Res, RHS: Cmp);
3109 else
3110 Res = Cmp;
3111 }
3112
3113 // Check if the first field is 0 if this is a function pointer.
3114 if (MPT->isMemberFunctionPointer()) {
3115 // (l1 == r1 && ...) || l0 == 0
3116 llvm::Value *Zero = llvm::Constant::getNullValue(Ty: L0->getType());
3117 llvm::Value *IsZero = Builder.CreateICmp(P: Eq, LHS: L0, RHS: Zero, Name: "memptr.cmp.iszero");
3118 Res = Builder.CreateBinOp(Opc: Or, LHS: Res, RHS: IsZero);
3119 }
3120
3121 // Combine the comparison of the first field, which must always be true for
3122 // this comparison to succeeed.
3123 return Builder.CreateBinOp(Opc: And, LHS: Res, RHS: Cmp0, Name: "memptr.cmp");
3124}
3125
3126llvm::Value *
3127MicrosoftCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
3128 llvm::Value *MemPtr,
3129 const MemberPointerType *MPT) {
3130 CGBuilderTy &Builder = CGF.Builder;
3131 llvm::SmallVector<llvm::Constant *, 4> fields;
3132 // We only need one field for member functions.
3133 if (MPT->isMemberFunctionPointer())
3134 fields.push_back(Elt: llvm::Constant::getNullValue(Ty: CGM.VoidPtrTy));
3135 else
3136 GetNullMemberPointerFields(MPT, fields);
3137 assert(!fields.empty());
3138 llvm::Value *FirstField = MemPtr;
3139 if (MemPtr->getType()->isStructTy())
3140 FirstField = Builder.CreateExtractValue(Agg: MemPtr, Idxs: 0);
3141 llvm::Value *Res = Builder.CreateICmpNE(LHS: FirstField, RHS: fields[0], Name: "memptr.cmp0");
3142
3143 // For function member pointers, we only need to test the function pointer
3144 // field. The other fields if any can be garbage.
3145 if (MPT->isMemberFunctionPointer())
3146 return Res;
3147
3148 // Otherwise, emit a series of compares and combine the results.
3149 for (int I = 1, E = fields.size(); I < E; ++I) {
3150 llvm::Value *Field = Builder.CreateExtractValue(Agg: MemPtr, Idxs: I);
3151 llvm::Value *Next = Builder.CreateICmpNE(LHS: Field, RHS: fields[I], Name: "memptr.cmp");
3152 Res = Builder.CreateOr(LHS: Res, RHS: Next, Name: "memptr.tobool");
3153 }
3154 return Res;
3155}
3156
3157bool MicrosoftCXXABI::MemberPointerConstantIsNull(const MemberPointerType *MPT,
3158 llvm::Constant *Val) {
3159 // Function pointers are null if the pointer in the first field is null.
3160 if (MPT->isMemberFunctionPointer()) {
3161 llvm::Constant *FirstField = Val->getType()->isStructTy() ?
3162 Val->getAggregateElement(Elt: 0U) : Val;
3163 return FirstField->isNullValue();
3164 }
3165
3166 // If it's not a function pointer and it's zero initializable, we can easily
3167 // check zero.
3168 if (isZeroInitializable(MPT) && Val->isNullValue())
3169 return true;
3170
3171 // Otherwise, break down all the fields for comparison. Hopefully these
3172 // little Constants are reused, while a big null struct might not be.
3173 llvm::SmallVector<llvm::Constant *, 4> Fields;
3174 GetNullMemberPointerFields(MPT, fields&: Fields);
3175 if (Fields.size() == 1) {
3176 assert(Val->getType()->isIntegerTy());
3177 return Val == Fields[0];
3178 }
3179
3180 unsigned I, E;
3181 for (I = 0, E = Fields.size(); I != E; ++I) {
3182 if (Val->getAggregateElement(Elt: I) != Fields[I])
3183 break;
3184 }
3185 return I == E;
3186}
3187
3188llvm::Value *
3189MicrosoftCXXABI::GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF,
3190 Address This,
3191 llvm::Value *VBPtrOffset,
3192 llvm::Value *VBTableOffset,
3193 llvm::Value **VBPtrOut) {
3194 CGBuilderTy &Builder = CGF.Builder;
3195 // Load the vbtable pointer from the vbptr in the instance.
3196 llvm::Value *VBPtr = Builder.CreateInBoundsGEP(
3197 Ty: CGM.Int8Ty, Ptr: This.emitRawPointer(CGF), IdxList: VBPtrOffset, Name: "vbptr");
3198 if (VBPtrOut)
3199 *VBPtrOut = VBPtr;
3200
3201 CharUnits VBPtrAlign;
3202 if (auto CI = dyn_cast<llvm::ConstantInt>(Val: VBPtrOffset)) {
3203 VBPtrAlign = This.getAlignment().alignmentAtOffset(
3204 offset: CharUnits::fromQuantity(Quantity: CI->getSExtValue()));
3205 } else {
3206 VBPtrAlign = CGF.getPointerAlign();
3207 }
3208
3209 llvm::Value *VBTable =
3210 Builder.CreateAlignedLoad(Ty: CGM.DefaultPtrTy, Addr: VBPtr, Align: VBPtrAlign, Name: "vbtable");
3211
3212 // Translate from byte offset to table index. It improves analyzability.
3213 llvm::Value *VBTableIndex = Builder.CreateAShr(
3214 LHS: VBTableOffset, RHS: llvm::ConstantInt::get(Ty: VBTableOffset->getType(), V: 2),
3215 Name: "vbtindex", /*isExact=*/true);
3216
3217 // Load an i32 offset from the vb-table.
3218 llvm::Value *VBaseOffs =
3219 Builder.CreateInBoundsGEP(Ty: CGM.Int32Ty, Ptr: VBTable, IdxList: VBTableIndex);
3220 return Builder.CreateAlignedLoad(Ty: CGM.Int32Ty, Addr: VBaseOffs,
3221 Align: CharUnits::fromQuantity(Quantity: 4), Name: "vbase_offs");
3222}
3223
3224// Returns an adjusted base cast to i8*, since we do more address arithmetic on
3225// it.
3226llvm::Value *MicrosoftCXXABI::AdjustVirtualBase(
3227 CodeGenFunction &CGF, const Expr *E, const CXXRecordDecl *RD,
3228 Address Base, llvm::Value *VBTableOffset, llvm::Value *VBPtrOffset) {
3229 CGBuilderTy &Builder = CGF.Builder;
3230 Base = Base.withElementType(ElemTy: CGM.Int8Ty);
3231 llvm::BasicBlock *OriginalBB = nullptr;
3232 llvm::BasicBlock *SkipAdjustBB = nullptr;
3233 llvm::BasicBlock *VBaseAdjustBB = nullptr;
3234
3235 // In the unspecified inheritance model, there might not be a vbtable at all,
3236 // in which case we need to skip the virtual base lookup. If there is a
3237 // vbtable, the first entry is a no-op entry that gives back the original
3238 // base, so look for a virtual base adjustment offset of zero.
3239 if (VBPtrOffset) {
3240 OriginalBB = Builder.GetInsertBlock();
3241 VBaseAdjustBB = CGF.createBasicBlock(name: "memptr.vadjust");
3242 SkipAdjustBB = CGF.createBasicBlock(name: "memptr.skip_vadjust");
3243 llvm::Value *IsVirtual =
3244 Builder.CreateICmpNE(LHS: VBTableOffset, RHS: getZeroInt(),
3245 Name: "memptr.is_vbase");
3246 Builder.CreateCondBr(Cond: IsVirtual, True: VBaseAdjustBB, False: SkipAdjustBB);
3247 CGF.EmitBlock(BB: VBaseAdjustBB);
3248 }
3249
3250 // If we weren't given a dynamic vbptr offset, RD should be complete and we'll
3251 // know the vbptr offset.
3252 if (!VBPtrOffset) {
3253 CharUnits offs = CharUnits::Zero();
3254 if (!RD->hasDefinition()) {
3255 DiagnosticsEngine &Diags = CGF.CGM.getDiags();
3256 Diags.Report(Loc: E->getExprLoc(), DiagID: diag::err_member_ptr_requires_complete_type)
3257 << RD << E->getSourceRange();
3258 } else if (RD->getNumVBases())
3259 offs = getContext().getASTRecordLayout(D: RD).getVBPtrOffset();
3260 VBPtrOffset = llvm::ConstantInt::get(Ty: CGM.IntTy, V: offs.getQuantity());
3261 }
3262 llvm::Value *VBPtr = nullptr;
3263 llvm::Value *VBaseOffs =
3264 GetVBaseOffsetFromVBPtr(CGF, This: Base, VBPtrOffset, VBTableOffset, VBPtrOut: &VBPtr);
3265 llvm::Value *AdjustedBase =
3266 Builder.CreateInBoundsGEP(Ty: CGM.Int8Ty, Ptr: VBPtr, IdxList: VBaseOffs);
3267
3268 // Merge control flow with the case where we didn't have to adjust.
3269 if (VBaseAdjustBB) {
3270 Builder.CreateBr(Dest: SkipAdjustBB);
3271 CGF.EmitBlock(BB: SkipAdjustBB);
3272 llvm::PHINode *Phi = Builder.CreatePHI(Ty: CGM.Int8PtrTy, NumReservedValues: 2, Name: "memptr.base");
3273 Phi->addIncoming(V: Base.emitRawPointer(CGF), BB: OriginalBB);
3274 Phi->addIncoming(V: AdjustedBase, BB: VBaseAdjustBB);
3275 return Phi;
3276 }
3277 return AdjustedBase;
3278}
3279
3280llvm::Value *MicrosoftCXXABI::EmitMemberDataPointerAddress(
3281 CodeGenFunction &CGF, const Expr *E, Address Base, llvm::Value *MemPtr,
3282 const MemberPointerType *MPT, bool IsInBounds) {
3283 assert(MPT->isMemberDataPointer());
3284 CGBuilderTy &Builder = CGF.Builder;
3285 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
3286 MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
3287
3288 // Extract the fields we need, regardless of model. We'll apply them if we
3289 // have them.
3290 llvm::Value *FieldOffset = MemPtr;
3291 llvm::Value *VirtualBaseAdjustmentOffset = nullptr;
3292 llvm::Value *VBPtrOffset = nullptr;
3293 if (MemPtr->getType()->isStructTy()) {
3294 // We need to extract values.
3295 unsigned I = 0;
3296 FieldOffset = Builder.CreateExtractValue(Agg: MemPtr, Idxs: I++);
3297 if (inheritanceModelHasVBPtrOffsetField(Inheritance))
3298 VBPtrOffset = Builder.CreateExtractValue(Agg: MemPtr, Idxs: I++);
3299 if (inheritanceModelHasVBTableOffsetField(Inheritance))
3300 VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(Agg: MemPtr, Idxs: I++);
3301 }
3302
3303 llvm::Value *Addr;
3304 if (VirtualBaseAdjustmentOffset) {
3305 Addr = AdjustVirtualBase(CGF, E, RD, Base, VBTableOffset: VirtualBaseAdjustmentOffset,
3306 VBPtrOffset);
3307 } else {
3308 Addr = Base.emitRawPointer(CGF);
3309 }
3310
3311 // Apply the offset.
3312 return Builder.CreateGEP(Ty: CGF.Int8Ty, Ptr: Addr, IdxList: FieldOffset, Name: "memptr.offset",
3313 NW: IsInBounds ? llvm::GEPNoWrapFlags::inBounds()
3314 : llvm::GEPNoWrapFlags::none());
3315}
3316
3317llvm::Value *
3318MicrosoftCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
3319 const CastExpr *E,
3320 llvm::Value *Src) {
3321 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
3322 E->getCastKind() == CK_BaseToDerivedMemberPointer ||
3323 E->getCastKind() == CK_ReinterpretMemberPointer);
3324
3325 // Use constant emission if we can.
3326 if (isa<llvm::Constant>(Val: Src))
3327 return EmitMemberPointerConversion(E, Src: cast<llvm::Constant>(Val: Src));
3328
3329 // We may be adding or dropping fields from the member pointer, so we need
3330 // both types and the inheritance models of both records.
3331 const MemberPointerType *SrcTy =
3332 E->getSubExpr()->getType()->castAs<MemberPointerType>();
3333 const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>();
3334 bool IsFunc = SrcTy->isMemberFunctionPointer();
3335
3336 // If the classes use the same null representation, reinterpret_cast is a nop.
3337 bool IsReinterpret = E->getCastKind() == CK_ReinterpretMemberPointer;
3338 if (IsReinterpret && IsFunc)
3339 return Src;
3340
3341 CXXRecordDecl *SrcRD = SrcTy->getMostRecentCXXRecordDecl();
3342 CXXRecordDecl *DstRD = DstTy->getMostRecentCXXRecordDecl();
3343 if (IsReinterpret &&
3344 SrcRD->nullFieldOffsetIsZero() == DstRD->nullFieldOffsetIsZero())
3345 return Src;
3346
3347 CGBuilderTy &Builder = CGF.Builder;
3348
3349 // Branch past the conversion if Src is null.
3350 llvm::Value *IsNotNull = EmitMemberPointerIsNotNull(CGF, MemPtr: Src, MPT: SrcTy);
3351 llvm::Constant *DstNull = EmitNullMemberPointer(MPT: DstTy);
3352
3353 // C++ 5.2.10p9: The null member pointer value is converted to the null member
3354 // pointer value of the destination type.
3355 if (IsReinterpret) {
3356 // For reinterpret casts, sema ensures that src and dst are both functions
3357 // or data and have the same size, which means the LLVM types should match.
3358 assert(Src->getType() == DstNull->getType());
3359 return Builder.CreateSelect(C: IsNotNull, True: Src, False: DstNull);
3360 }
3361
3362 llvm::BasicBlock *OriginalBB = Builder.GetInsertBlock();
3363 llvm::BasicBlock *ConvertBB = CGF.createBasicBlock(name: "memptr.convert");
3364 llvm::BasicBlock *ContinueBB = CGF.createBasicBlock(name: "memptr.converted");
3365 Builder.CreateCondBr(Cond: IsNotNull, True: ConvertBB, False: ContinueBB);
3366 CGF.EmitBlock(BB: ConvertBB);
3367
3368 llvm::Value *Dst = EmitNonNullMemberPointerConversion(
3369 SrcTy, DstTy, CK: E->getCastKind(), PathBegin: E->path_begin(), PathEnd: E->path_end(), Src,
3370 Builder);
3371
3372 Builder.CreateBr(Dest: ContinueBB);
3373
3374 // In the continuation, choose between DstNull and Dst.
3375 CGF.EmitBlock(BB: ContinueBB);
3376 llvm::PHINode *Phi = Builder.CreatePHI(Ty: DstNull->getType(), NumReservedValues: 2, Name: "memptr.converted");
3377 Phi->addIncoming(V: DstNull, BB: OriginalBB);
3378 Phi->addIncoming(V: Dst, BB: ConvertBB);
3379 return Phi;
3380}
3381
3382llvm::Value *MicrosoftCXXABI::EmitNonNullMemberPointerConversion(
3383 const MemberPointerType *SrcTy, const MemberPointerType *DstTy, CastKind CK,
3384 CastExpr::path_const_iterator PathBegin,
3385 CastExpr::path_const_iterator PathEnd, llvm::Value *Src,
3386 CGBuilderTy &Builder) {
3387 const CXXRecordDecl *SrcRD = SrcTy->getMostRecentCXXRecordDecl();
3388 const CXXRecordDecl *DstRD = DstTy->getMostRecentCXXRecordDecl();
3389 MSInheritanceModel SrcInheritance = SrcRD->getMSInheritanceModel();
3390 MSInheritanceModel DstInheritance = DstRD->getMSInheritanceModel();
3391 bool IsFunc = SrcTy->isMemberFunctionPointer();
3392 bool IsConstant = isa<llvm::Constant>(Val: Src);
3393
3394 // Decompose src.
3395 llvm::Value *FirstField = Src;
3396 llvm::Value *NonVirtualBaseAdjustment = getZeroInt();
3397 llvm::Value *VirtualBaseAdjustmentOffset = getZeroInt();
3398 llvm::Value *VBPtrOffset = getZeroInt();
3399 if (!inheritanceModelHasOnlyOneField(IsMemberFunction: IsFunc, Inheritance: SrcInheritance)) {
3400 // We need to extract values.
3401 unsigned I = 0;
3402 FirstField = Builder.CreateExtractValue(Agg: Src, Idxs: I++);
3403 if (inheritanceModelHasNVOffsetField(IsMemberFunction: IsFunc, Inheritance: SrcInheritance))
3404 NonVirtualBaseAdjustment = Builder.CreateExtractValue(Agg: Src, Idxs: I++);
3405 if (inheritanceModelHasVBPtrOffsetField(Inheritance: SrcInheritance))
3406 VBPtrOffset = Builder.CreateExtractValue(Agg: Src, Idxs: I++);
3407 if (inheritanceModelHasVBTableOffsetField(Inheritance: SrcInheritance))
3408 VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(Agg: Src, Idxs: I++);
3409 }
3410
3411 bool IsDerivedToBase = (CK == CK_DerivedToBaseMemberPointer);
3412 const MemberPointerType *DerivedTy = IsDerivedToBase ? SrcTy : DstTy;
3413 const CXXRecordDecl *DerivedClass = DerivedTy->getMostRecentCXXRecordDecl();
3414
3415 // For data pointers, we adjust the field offset directly. For functions, we
3416 // have a separate field.
3417 llvm::Value *&NVAdjustField = IsFunc ? NonVirtualBaseAdjustment : FirstField;
3418
3419 // The virtual inheritance model has a quirk: the virtual base table is always
3420 // referenced when dereferencing a member pointer even if the member pointer
3421 // is non-virtual. This is accounted for by adjusting the non-virtual offset
3422 // to point backwards to the top of the MDC from the first VBase. Undo this
3423 // adjustment to normalize the member pointer.
3424 llvm::Value *SrcVBIndexEqZero =
3425 Builder.CreateICmpEQ(LHS: VirtualBaseAdjustmentOffset, RHS: getZeroInt());
3426 if (SrcInheritance == MSInheritanceModel::Virtual) {
3427 if (int64_t SrcOffsetToFirstVBase =
3428 getContext().getOffsetOfBaseWithVBPtr(RD: SrcRD).getQuantity()) {
3429 llvm::Value *UndoSrcAdjustment = Builder.CreateSelect(
3430 C: SrcVBIndexEqZero,
3431 True: llvm::ConstantInt::get(Ty: CGM.IntTy, V: SrcOffsetToFirstVBase),
3432 False: getZeroInt());
3433 NVAdjustField = Builder.CreateNSWAdd(LHS: NVAdjustField, RHS: UndoSrcAdjustment);
3434 }
3435 }
3436
3437 // A non-zero vbindex implies that we are dealing with a source member in a
3438 // floating virtual base in addition to some non-virtual offset. If the
3439 // vbindex is zero, we are dealing with a source that exists in a non-virtual,
3440 // fixed, base. The difference between these two cases is that the vbindex +
3441 // nvoffset *always* point to the member regardless of what context they are
3442 // evaluated in so long as the vbindex is adjusted. A member inside a fixed
3443 // base requires explicit nv adjustment.
3444 llvm::Constant *BaseClassOffset = llvm::ConstantInt::get(
3445 Ty: CGM.IntTy,
3446 V: CGM.computeNonVirtualBaseClassOffset(DerivedClass, Start: PathBegin, End: PathEnd)
3447 .getQuantity());
3448
3449 llvm::Value *NVDisp;
3450 if (IsDerivedToBase)
3451 NVDisp = Builder.CreateNSWSub(LHS: NVAdjustField, RHS: BaseClassOffset, Name: "adj");
3452 else
3453 NVDisp = Builder.CreateNSWAdd(LHS: NVAdjustField, RHS: BaseClassOffset, Name: "adj");
3454
3455 NVAdjustField = Builder.CreateSelect(C: SrcVBIndexEqZero, True: NVDisp, False: getZeroInt());
3456
3457 // Update the vbindex to an appropriate value in the destination because
3458 // SrcRD's vbtable might not be a strict prefix of the one in DstRD.
3459 llvm::Value *DstVBIndexEqZero = SrcVBIndexEqZero;
3460 if (inheritanceModelHasVBTableOffsetField(Inheritance: DstInheritance) &&
3461 inheritanceModelHasVBTableOffsetField(Inheritance: SrcInheritance)) {
3462 if (llvm::GlobalVariable *VDispMap =
3463 getAddrOfVirtualDisplacementMap(SrcRD, DstRD)) {
3464 llvm::Value *VBIndex = Builder.CreateExactUDiv(
3465 LHS: VirtualBaseAdjustmentOffset, RHS: llvm::ConstantInt::get(Ty: CGM.IntTy, V: 4));
3466 if (IsConstant) {
3467 llvm::Constant *Mapping = VDispMap->getInitializer();
3468 VirtualBaseAdjustmentOffset =
3469 Mapping->getAggregateElement(Elt: cast<llvm::Constant>(Val: VBIndex));
3470 } else {
3471 llvm::Value *Idxs[] = {getZeroInt(), VBIndex};
3472 VirtualBaseAdjustmentOffset = Builder.CreateAlignedLoad(
3473 Ty: CGM.IntTy, Addr: Builder.CreateInBoundsGEP(Ty: VDispMap->getValueType(),
3474 Ptr: VDispMap, IdxList: Idxs),
3475 Align: CharUnits::fromQuantity(Quantity: 4));
3476 }
3477
3478 DstVBIndexEqZero =
3479 Builder.CreateICmpEQ(LHS: VirtualBaseAdjustmentOffset, RHS: getZeroInt());
3480 }
3481 }
3482
3483 // Set the VBPtrOffset to zero if the vbindex is zero. Otherwise, initialize
3484 // it to the offset of the vbptr.
3485 if (inheritanceModelHasVBPtrOffsetField(Inheritance: DstInheritance)) {
3486 llvm::Value *DstVBPtrOffset = llvm::ConstantInt::getSigned(
3487 Ty: CGM.IntTy,
3488 V: getContext().getASTRecordLayout(D: DstRD).getVBPtrOffset().getQuantity());
3489 VBPtrOffset =
3490 Builder.CreateSelect(C: DstVBIndexEqZero, True: getZeroInt(), False: DstVBPtrOffset);
3491 }
3492
3493 // Likewise, apply a similar adjustment so that dereferencing the member
3494 // pointer correctly accounts for the distance between the start of the first
3495 // virtual base and the top of the MDC.
3496 if (DstInheritance == MSInheritanceModel::Virtual) {
3497 if (int64_t DstOffsetToFirstVBase =
3498 getContext().getOffsetOfBaseWithVBPtr(RD: DstRD).getQuantity()) {
3499 llvm::Value *DoDstAdjustment = Builder.CreateSelect(
3500 C: DstVBIndexEqZero,
3501 True: llvm::ConstantInt::get(Ty: CGM.IntTy, V: DstOffsetToFirstVBase),
3502 False: getZeroInt());
3503 NVAdjustField = Builder.CreateNSWSub(LHS: NVAdjustField, RHS: DoDstAdjustment);
3504 }
3505 }
3506
3507 // Recompose dst from the null struct and the adjusted fields from src.
3508 llvm::Value *Dst;
3509 if (inheritanceModelHasOnlyOneField(IsMemberFunction: IsFunc, Inheritance: DstInheritance)) {
3510 Dst = FirstField;
3511 } else {
3512 Dst = llvm::PoisonValue::get(T: ConvertMemberPointerType(MPT: DstTy));
3513 unsigned Idx = 0;
3514 Dst = Builder.CreateInsertValue(Agg: Dst, Val: FirstField, Idxs: Idx++);
3515 if (inheritanceModelHasNVOffsetField(IsMemberFunction: IsFunc, Inheritance: DstInheritance))
3516 Dst = Builder.CreateInsertValue(Agg: Dst, Val: NonVirtualBaseAdjustment, Idxs: Idx++);
3517 if (inheritanceModelHasVBPtrOffsetField(Inheritance: DstInheritance))
3518 Dst = Builder.CreateInsertValue(Agg: Dst, Val: VBPtrOffset, Idxs: Idx++);
3519 if (inheritanceModelHasVBTableOffsetField(Inheritance: DstInheritance))
3520 Dst = Builder.CreateInsertValue(Agg: Dst, Val: VirtualBaseAdjustmentOffset, Idxs: Idx++);
3521 }
3522 return Dst;
3523}
3524
3525llvm::Constant *
3526MicrosoftCXXABI::EmitMemberPointerConversion(const CastExpr *E,
3527 llvm::Constant *Src) {
3528 const MemberPointerType *SrcTy =
3529 E->getSubExpr()->getType()->castAs<MemberPointerType>();
3530 const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>();
3531
3532 CastKind CK = E->getCastKind();
3533
3534 return EmitMemberPointerConversion(SrcTy, DstTy, CK, PathBegin: E->path_begin(),
3535 PathEnd: E->path_end(), Src);
3536}
3537
3538llvm::Constant *MicrosoftCXXABI::EmitMemberPointerConversion(
3539 const MemberPointerType *SrcTy, const MemberPointerType *DstTy, CastKind CK,
3540 CastExpr::path_const_iterator PathBegin,
3541 CastExpr::path_const_iterator PathEnd, llvm::Constant *Src) {
3542 assert(CK == CK_DerivedToBaseMemberPointer ||
3543 CK == CK_BaseToDerivedMemberPointer ||
3544 CK == CK_ReinterpretMemberPointer);
3545 // If src is null, emit a new null for dst. We can't return src because dst
3546 // might have a new representation.
3547 if (MemberPointerConstantIsNull(MPT: SrcTy, Val: Src))
3548 return EmitNullMemberPointer(MPT: DstTy);
3549
3550 // We don't need to do anything for reinterpret_casts of non-null member
3551 // pointers. We should only get here when the two type representations have
3552 // the same size.
3553 if (CK == CK_ReinterpretMemberPointer)
3554 return Src;
3555
3556 CGBuilderTy Builder(CGM, CGM.getLLVMContext());
3557 auto *Dst = cast<llvm::Constant>(Val: EmitNonNullMemberPointerConversion(
3558 SrcTy, DstTy, CK, PathBegin, PathEnd, Src, Builder));
3559
3560 return Dst;
3561}
3562
3563CGCallee MicrosoftCXXABI::EmitLoadOfMemberFunctionPointer(
3564 CodeGenFunction &CGF, const Expr *E, Address This,
3565 llvm::Value *&ThisPtrForCall, llvm::Value *MemPtr,
3566 const MemberPointerType *MPT) {
3567 assert(MPT->isMemberFunctionPointer());
3568 const FunctionProtoType *FPT =
3569 MPT->getPointeeType()->castAs<FunctionProtoType>();
3570 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
3571 CGBuilderTy &Builder = CGF.Builder;
3572
3573 MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
3574
3575 // Extract the fields we need, regardless of model. We'll apply them if we
3576 // have them.
3577 llvm::Value *FunctionPointer = MemPtr;
3578 llvm::Value *NonVirtualBaseAdjustment = nullptr;
3579 llvm::Value *VirtualBaseAdjustmentOffset = nullptr;
3580 llvm::Value *VBPtrOffset = nullptr;
3581 if (MemPtr->getType()->isStructTy()) {
3582 // We need to extract values.
3583 unsigned I = 0;
3584 FunctionPointer = Builder.CreateExtractValue(Agg: MemPtr, Idxs: I++);
3585 if (inheritanceModelHasNVOffsetField(IsMemberFunction: MPT, Inheritance))
3586 NonVirtualBaseAdjustment = Builder.CreateExtractValue(Agg: MemPtr, Idxs: I++);
3587 if (inheritanceModelHasVBPtrOffsetField(Inheritance))
3588 VBPtrOffset = Builder.CreateExtractValue(Agg: MemPtr, Idxs: I++);
3589 if (inheritanceModelHasVBTableOffsetField(Inheritance))
3590 VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(Agg: MemPtr, Idxs: I++);
3591 }
3592
3593 if (VirtualBaseAdjustmentOffset) {
3594 ThisPtrForCall = AdjustVirtualBase(CGF, E, RD, Base: This,
3595 VBTableOffset: VirtualBaseAdjustmentOffset, VBPtrOffset);
3596 } else {
3597 ThisPtrForCall = This.emitRawPointer(CGF);
3598 }
3599
3600 if (NonVirtualBaseAdjustment)
3601 ThisPtrForCall = Builder.CreateInBoundsGEP(Ty: CGF.Int8Ty, Ptr: ThisPtrForCall,
3602 IdxList: NonVirtualBaseAdjustment);
3603
3604 CGCallee Callee(FPT, FunctionPointer);
3605 return Callee;
3606}
3607
3608CGCXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) {
3609 return new MicrosoftCXXABI(CGM);
3610}
3611
3612// MS RTTI Overview:
3613// The run time type information emitted by cl.exe contains 5 distinct types of
3614// structures. Many of them reference each other.
3615//
3616// TypeInfo: Static classes that are returned by typeid.
3617//
3618// CompleteObjectLocator: Referenced by vftables. They contain information
3619// required for dynamic casting, including OffsetFromTop. They also contain
3620// a reference to the TypeInfo for the type and a reference to the
3621// CompleteHierarchyDescriptor for the type.
3622//
3623// ClassHierarchyDescriptor: Contains information about a class hierarchy.
3624// Used during dynamic_cast to walk a class hierarchy. References a base
3625// class array and the size of said array.
3626//
3627// BaseClassArray: Contains a list of classes in a hierarchy. BaseClassArray is
3628// somewhat of a misnomer because the most derived class is also in the list
3629// as well as multiple copies of virtual bases (if they occur multiple times
3630// in the hierarchy.) The BaseClassArray contains one BaseClassDescriptor for
3631// every path in the hierarchy, in pre-order depth first order. Note, we do
3632// not declare a specific llvm type for BaseClassArray, it's merely an array
3633// of BaseClassDescriptor pointers.
3634//
3635// BaseClassDescriptor: Contains information about a class in a class hierarchy.
3636// BaseClassDescriptor is also somewhat of a misnomer for the same reason that
3637// BaseClassArray is. It contains information about a class within a
3638// hierarchy such as: is this base is ambiguous and what is its offset in the
3639// vbtable. The names of the BaseClassDescriptors have all of their fields
3640// mangled into them so they can be aggressively deduplicated by the linker.
3641
3642static llvm::GlobalVariable *getTypeInfoVTable(CodeGenModule &CGM) {
3643 StringRef MangledName("??_7type_info@@6B@");
3644 if (auto VTable = CGM.getModule().getNamedGlobal(Name: MangledName))
3645 return VTable;
3646 return new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy,
3647 /*isConstant=*/true,
3648 llvm::GlobalVariable::ExternalLinkage,
3649 /*Initializer=*/nullptr, MangledName);
3650}
3651
3652namespace {
3653
3654/// A Helper struct that stores information about a class in a class
3655/// hierarchy. The information stored in these structs struct is used during
3656/// the generation of ClassHierarchyDescriptors and BaseClassDescriptors.
3657// During RTTI creation, MSRTTIClasses are stored in a contiguous array with
3658// implicit depth first pre-order tree connectivity. getFirstChild and
3659// getNextSibling allow us to walk the tree efficiently.
3660struct MSRTTIClass {
3661 enum {
3662 IsPrivateOnPath = 1 | 8,
3663 IsAmbiguous = 2,
3664 IsPrivate = 4,
3665 IsVirtual = 16,
3666 HasHierarchyDescriptor = 64
3667 };
3668 MSRTTIClass(const CXXRecordDecl *RD) : RD(RD) {}
3669 uint32_t initialize(const MSRTTIClass *Parent,
3670 const CXXBaseSpecifier *Specifier);
3671
3672 MSRTTIClass *getFirstChild() { return this + 1; }
3673 static MSRTTIClass *getNextChild(MSRTTIClass *Child) {
3674 return Child + 1 + Child->NumBases;
3675 }
3676
3677 const CXXRecordDecl *RD, *VirtualRoot;
3678 uint32_t Flags, NumBases, OffsetInVBase;
3679};
3680
3681/// Recursively initialize the base class array.
3682uint32_t MSRTTIClass::initialize(const MSRTTIClass *Parent,
3683 const CXXBaseSpecifier *Specifier) {
3684 Flags = HasHierarchyDescriptor;
3685 if (!Parent) {
3686 VirtualRoot = nullptr;
3687 OffsetInVBase = 0;
3688 } else {
3689 if (Specifier->getAccessSpecifier() != AS_public)
3690 Flags |= IsPrivate | IsPrivateOnPath;
3691 if (Specifier->isVirtual()) {
3692 Flags |= IsVirtual;
3693 VirtualRoot = RD;
3694 OffsetInVBase = 0;
3695 } else {
3696 if (Parent->Flags & IsPrivateOnPath)
3697 Flags |= IsPrivateOnPath;
3698 VirtualRoot = Parent->VirtualRoot;
3699 OffsetInVBase = Parent->OffsetInVBase + RD->getASTContext()
3700 .getASTRecordLayout(D: Parent->RD).getBaseClassOffset(Base: RD).getQuantity();
3701 }
3702 }
3703 NumBases = 0;
3704 MSRTTIClass *Child = getFirstChild();
3705 for (const CXXBaseSpecifier &Base : RD->bases()) {
3706 NumBases += Child->initialize(Parent: this, Specifier: &Base) + 1;
3707 Child = getNextChild(Child);
3708 }
3709 return NumBases;
3710}
3711
3712static llvm::GlobalValue::LinkageTypes getLinkageForRTTI(QualType Ty) {
3713 switch (Ty->getLinkage()) {
3714 case Linkage::Invalid:
3715 llvm_unreachable("Linkage hasn't been computed!");
3716
3717 case Linkage::None:
3718 case Linkage::Internal:
3719 case Linkage::UniqueExternal:
3720 return llvm::GlobalValue::InternalLinkage;
3721
3722 case Linkage::VisibleNone:
3723 case Linkage::Module:
3724 case Linkage::External:
3725 return llvm::GlobalValue::LinkOnceODRLinkage;
3726 }
3727 llvm_unreachable("Invalid linkage!");
3728}
3729
3730/// An ephemeral helper class for building MS RTTI types. It caches some
3731/// calls to the module and information about the most derived class in a
3732/// hierarchy.
3733struct MSRTTIBuilder {
3734 enum {
3735 HasBranchingHierarchy = 1,
3736 HasVirtualBranchingHierarchy = 2,
3737 HasAmbiguousBases = 4
3738 };
3739
3740 MSRTTIBuilder(MicrosoftCXXABI &ABI, const CXXRecordDecl *RD)
3741 : CGM(ABI.CGM), Context(CGM.getContext()),
3742 VMContext(CGM.getLLVMContext()), Module(CGM.getModule()), RD(RD),
3743 Linkage(getLinkageForRTTI(Ty: CGM.getContext().getCanonicalTagType(TD: RD))),
3744 ABI(ABI) {}
3745
3746 llvm::GlobalVariable *getBaseClassDescriptor(const MSRTTIClass &Classes);
3747 llvm::GlobalVariable *
3748 getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes);
3749 llvm::GlobalVariable *getClassHierarchyDescriptor();
3750 llvm::GlobalVariable *getCompleteObjectLocator(const VPtrInfo &Info);
3751
3752 CodeGenModule &CGM;
3753 ASTContext &Context;
3754 llvm::LLVMContext &VMContext;
3755 llvm::Module &Module;
3756 const CXXRecordDecl *RD;
3757 llvm::GlobalVariable::LinkageTypes Linkage;
3758 MicrosoftCXXABI &ABI;
3759};
3760
3761} // namespace
3762
3763/// Recursively serializes a class hierarchy in pre-order depth first
3764/// order.
3765static void serializeClassHierarchy(SmallVectorImpl<MSRTTIClass> &Classes,
3766 const CXXRecordDecl *RD) {
3767 Classes.push_back(Elt: MSRTTIClass(RD));
3768 for (const CXXBaseSpecifier &Base : RD->bases())
3769 serializeClassHierarchy(Classes, RD: Base.getType()->getAsCXXRecordDecl());
3770}
3771
3772/// Find ambiguity among base classes.
3773static void
3774detectAmbiguousBases(SmallVectorImpl<MSRTTIClass> &Classes) {
3775 llvm::SmallPtrSet<const CXXRecordDecl *, 8> VirtualBases;
3776 llvm::SmallPtrSet<const CXXRecordDecl *, 8> UniqueBases;
3777 llvm::SmallPtrSet<const CXXRecordDecl *, 8> AmbiguousBases;
3778 for (MSRTTIClass *Class = &Classes.front(); Class <= &Classes.back();) {
3779 if ((Class->Flags & MSRTTIClass::IsVirtual) &&
3780 !VirtualBases.insert(Ptr: Class->RD).second) {
3781 Class = MSRTTIClass::getNextChild(Child: Class);
3782 continue;
3783 }
3784 if (!UniqueBases.insert(Ptr: Class->RD).second)
3785 AmbiguousBases.insert(Ptr: Class->RD);
3786 Class++;
3787 }
3788 if (AmbiguousBases.empty())
3789 return;
3790 for (MSRTTIClass &Class : Classes)
3791 if (AmbiguousBases.count(Ptr: Class.RD))
3792 Class.Flags |= MSRTTIClass::IsAmbiguous;
3793}
3794
3795llvm::GlobalVariable *MSRTTIBuilder::getClassHierarchyDescriptor() {
3796 SmallString<256> MangledName;
3797 {
3798 llvm::raw_svector_ostream Out(MangledName);
3799 ABI.getMangleContext().mangleCXXRTTIClassHierarchyDescriptor(Derived: RD, Out);
3800 }
3801
3802 // Check to see if we've already declared this ClassHierarchyDescriptor.
3803 if (auto CHD = Module.getNamedGlobal(Name: MangledName))
3804 return CHD;
3805
3806 // Serialize the class hierarchy and initialize the CHD Fields.
3807 SmallVector<MSRTTIClass, 8> Classes;
3808 serializeClassHierarchy(Classes, RD);
3809 Classes.front().initialize(/*Parent=*/nullptr, /*Specifier=*/nullptr);
3810 detectAmbiguousBases(Classes);
3811 int Flags = 0;
3812 for (const MSRTTIClass &Class : Classes) {
3813 if (Class.RD->getNumBases() > 1)
3814 Flags |= HasBranchingHierarchy;
3815 // Note: cl.exe does not calculate "HasAmbiguousBases" correctly. We
3816 // believe the field isn't actually used.
3817 if (Class.Flags & MSRTTIClass::IsAmbiguous)
3818 Flags |= HasAmbiguousBases;
3819 }
3820 if ((Flags & HasBranchingHierarchy) && RD->getNumVBases() != 0)
3821 Flags |= HasVirtualBranchingHierarchy;
3822 // These gep indices are used to get the address of the first element of the
3823 // base class array.
3824 llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(Ty: CGM.IntTy, V: 0),
3825 llvm::ConstantInt::get(Ty: CGM.IntTy, V: 0)};
3826
3827 // Forward-declare the class hierarchy descriptor
3828 auto Type = ABI.getClassHierarchyDescriptorType();
3829 auto CHD = new llvm::GlobalVariable(Module, Type, /*isConstant=*/true, Linkage,
3830 /*Initializer=*/nullptr,
3831 MangledName);
3832 if (CHD->isWeakForLinker())
3833 CHD->setComdat(CGM.getModule().getOrInsertComdat(Name: CHD->getName()));
3834
3835 auto *Bases = getBaseClassArray(Classes);
3836
3837 // Initialize the base class ClassHierarchyDescriptor.
3838 llvm::Constant *Fields[] = {
3839 llvm::ConstantInt::get(Ty: CGM.IntTy, V: 0), // reserved by the runtime
3840 llvm::ConstantInt::get(Ty: CGM.IntTy, V: Flags),
3841 llvm::ConstantInt::get(Ty: CGM.IntTy, V: Classes.size()),
3842 ABI.getImageRelativeConstant(PtrVal: llvm::ConstantExpr::getInBoundsGetElementPtr(
3843 Ty: Bases->getValueType(), C: Bases,
3844 IdxList: llvm::ArrayRef<llvm::Value *>(GEPIndices))),
3845 };
3846 CHD->setInitializer(llvm::ConstantStruct::get(T: Type, V: Fields));
3847 return CHD;
3848}
3849
3850llvm::GlobalVariable *
3851MSRTTIBuilder::getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes) {
3852 SmallString<256> MangledName;
3853 {
3854 llvm::raw_svector_ostream Out(MangledName);
3855 ABI.getMangleContext().mangleCXXRTTIBaseClassArray(Derived: RD, Out);
3856 }
3857
3858 // Forward-declare the base class array.
3859 // cl.exe pads the base class array with 1 (in 32 bit mode) or 4 (in 64 bit
3860 // mode) bytes of padding. We provide a pointer sized amount of padding by
3861 // adding +1 to Classes.size(). The sections have pointer alignment and are
3862 // marked pick-any so it shouldn't matter.
3863 llvm::Type *PtrType = ABI.getImageRelativeType(PtrType: CGM.DefaultPtrTy);
3864 auto *ArrType = llvm::ArrayType::get(ElementType: PtrType, NumElements: Classes.size() + 1);
3865 auto *BCA =
3866 new llvm::GlobalVariable(Module, ArrType,
3867 /*isConstant=*/true, Linkage,
3868 /*Initializer=*/nullptr, MangledName);
3869 if (BCA->isWeakForLinker())
3870 BCA->setComdat(CGM.getModule().getOrInsertComdat(Name: BCA->getName()));
3871
3872 // Initialize the BaseClassArray.
3873 SmallVector<llvm::Constant *, 8> BaseClassArrayData;
3874 for (MSRTTIClass &Class : Classes)
3875 BaseClassArrayData.push_back(
3876 Elt: ABI.getImageRelativeConstant(PtrVal: getBaseClassDescriptor(Classes: Class)));
3877 BaseClassArrayData.push_back(Elt: llvm::Constant::getNullValue(Ty: PtrType));
3878 BCA->setInitializer(llvm::ConstantArray::get(T: ArrType, V: BaseClassArrayData));
3879 return BCA;
3880}
3881
3882llvm::GlobalVariable *
3883MSRTTIBuilder::getBaseClassDescriptor(const MSRTTIClass &Class) {
3884 // Compute the fields for the BaseClassDescriptor. They are computed up front
3885 // because they are mangled into the name of the object.
3886 uint32_t OffsetInVBTable = 0;
3887 int32_t VBPtrOffset = -1;
3888 if (Class.VirtualRoot) {
3889 auto &VTableContext = CGM.getMicrosoftVTableContext();
3890 OffsetInVBTable = VTableContext.getVBTableIndex(Derived: RD, VBase: Class.VirtualRoot) * 4;
3891 VBPtrOffset = Context.getASTRecordLayout(D: RD).getVBPtrOffset().getQuantity();
3892 }
3893
3894 SmallString<256> MangledName;
3895 {
3896 llvm::raw_svector_ostream Out(MangledName);
3897 ABI.getMangleContext().mangleCXXRTTIBaseClassDescriptor(
3898 Derived: Class.RD, NVOffset: Class.OffsetInVBase, VBPtrOffset, VBTableOffset: OffsetInVBTable,
3899 Flags: Class.Flags, Out);
3900 }
3901
3902 // Check to see if we've already declared this object.
3903 if (auto BCD = Module.getNamedGlobal(Name: MangledName))
3904 return BCD;
3905
3906 // Forward-declare the base class descriptor.
3907 auto Type = ABI.getBaseClassDescriptorType();
3908 auto BCD =
3909 new llvm::GlobalVariable(Module, Type, /*isConstant=*/true, Linkage,
3910 /*Initializer=*/nullptr, MangledName);
3911 if (BCD->isWeakForLinker())
3912 BCD->setComdat(CGM.getModule().getOrInsertComdat(Name: BCD->getName()));
3913
3914 // Initialize the BaseClassDescriptor.
3915 llvm::Constant *Fields[] = {
3916 ABI.getImageRelativeConstant(
3917 PtrVal: ABI.getAddrOfRTTIDescriptor(Ty: Context.getCanonicalTagType(TD: Class.RD))),
3918 llvm::ConstantInt::get(Ty: CGM.IntTy, V: Class.NumBases),
3919 llvm::ConstantInt::get(Ty: CGM.IntTy, V: Class.OffsetInVBase),
3920 llvm::ConstantInt::getSigned(Ty: CGM.IntTy, V: VBPtrOffset),
3921 llvm::ConstantInt::get(Ty: CGM.IntTy, V: OffsetInVBTable),
3922 llvm::ConstantInt::get(Ty: CGM.IntTy, V: Class.Flags),
3923 ABI.getImageRelativeConstant(
3924 PtrVal: MSRTTIBuilder(ABI, Class.RD).getClassHierarchyDescriptor()),
3925 };
3926 BCD->setInitializer(llvm::ConstantStruct::get(T: Type, V: Fields));
3927 return BCD;
3928}
3929
3930llvm::GlobalVariable *
3931MSRTTIBuilder::getCompleteObjectLocator(const VPtrInfo &Info) {
3932 SmallString<256> MangledName;
3933 {
3934 llvm::raw_svector_ostream Out(MangledName);
3935 ABI.getMangleContext().mangleCXXRTTICompleteObjectLocator(Derived: RD, BasePath: Info.MangledPath, Out);
3936 }
3937
3938 // Check to see if we've already computed this complete object locator.
3939 if (auto COL = Module.getNamedGlobal(Name: MangledName))
3940 return COL;
3941
3942 // Compute the fields of the complete object locator.
3943 int OffsetToTop = Info.FullOffsetInMDC.getQuantity();
3944 int VFPtrOffset = 0;
3945 // The offset includes the vtordisp if one exists.
3946 if (const CXXRecordDecl *VBase = Info.getVBaseWithVPtr())
3947 if (Context.getASTRecordLayout(D: RD)
3948 .getVBaseOffsetsMap()
3949 .find(Val: VBase)
3950 ->second.hasVtorDisp())
3951 VFPtrOffset = Info.NonVirtualOffset.getQuantity() + 4;
3952
3953 // Forward-declare the complete object locator.
3954 llvm::StructType *Type = ABI.getCompleteObjectLocatorType();
3955 auto COL = new llvm::GlobalVariable(Module, Type, /*isConstant=*/true, Linkage,
3956 /*Initializer=*/nullptr, MangledName);
3957
3958 // Initialize the CompleteObjectLocator.
3959 llvm::Constant *Fields[] = {
3960 llvm::ConstantInt::get(Ty: CGM.IntTy, V: ABI.isImageRelative()),
3961 llvm::ConstantInt::get(Ty: CGM.IntTy, V: OffsetToTop),
3962 llvm::ConstantInt::get(Ty: CGM.IntTy, V: VFPtrOffset),
3963 ABI.getImageRelativeConstant(
3964 PtrVal: CGM.GetAddrOfRTTIDescriptor(Ty: Context.getCanonicalTagType(TD: RD))),
3965 ABI.getImageRelativeConstant(PtrVal: getClassHierarchyDescriptor()),
3966 ABI.getImageRelativeConstant(PtrVal: COL),
3967 };
3968 llvm::ArrayRef<llvm::Constant *> FieldsRef(Fields);
3969 if (!ABI.isImageRelative())
3970 FieldsRef = FieldsRef.drop_back();
3971 COL->setInitializer(llvm::ConstantStruct::get(T: Type, V: FieldsRef));
3972 if (COL->isWeakForLinker())
3973 COL->setComdat(CGM.getModule().getOrInsertComdat(Name: COL->getName()));
3974 return COL;
3975}
3976
3977static QualType decomposeTypeForEH(ASTContext &Context, QualType T,
3978 bool &IsConst, bool &IsVolatile,
3979 bool &IsUnaligned) {
3980 T = Context.getExceptionObjectType(T);
3981
3982 // C++14 [except.handle]p3:
3983 // A handler is a match for an exception object of type E if [...]
3984 // - the handler is of type cv T or const T& where T is a pointer type and
3985 // E is a pointer type that can be converted to T by [...]
3986 // - a qualification conversion
3987 IsConst = false;
3988 IsVolatile = false;
3989 IsUnaligned = false;
3990 QualType PointeeType = T->getPointeeType();
3991 if (!PointeeType.isNull()) {
3992 IsConst = PointeeType.isConstQualified();
3993 IsVolatile = PointeeType.isVolatileQualified();
3994 IsUnaligned = PointeeType.getQualifiers().hasUnaligned();
3995 }
3996
3997 // Member pointer types like "const int A::*" are represented by having RTTI
3998 // for "int A::*" and separately storing the const qualifier.
3999 if (const auto *MPTy = T->getAs<MemberPointerType>())
4000 T = Context.getMemberPointerType(T: PointeeType.getUnqualifiedType(),
4001 Qualifier: MPTy->getQualifier(),
4002 Cls: MPTy->getMostRecentCXXRecordDecl());
4003
4004 // Pointer types like "const int * const *" are represented by having RTTI
4005 // for "const int **" and separately storing the const qualifier.
4006 if (T->isPointerType())
4007 T = Context.getPointerType(T: PointeeType.getUnqualifiedType());
4008
4009 return T;
4010}
4011
4012CatchTypeInfo
4013MicrosoftCXXABI::getAddrOfCXXCatchHandlerType(QualType Type,
4014 QualType CatchHandlerType) {
4015 // TypeDescriptors for exceptions never have qualified pointer types,
4016 // qualifiers are stored separately in order to support qualification
4017 // conversions.
4018 bool IsConst, IsVolatile, IsUnaligned;
4019 Type =
4020 decomposeTypeForEH(Context&: getContext(), T: Type, IsConst, IsVolatile, IsUnaligned);
4021
4022 bool IsReference = CatchHandlerType->isReferenceType();
4023
4024 uint32_t Flags = 0;
4025 if (IsConst)
4026 Flags |= 1;
4027 if (IsVolatile)
4028 Flags |= 2;
4029 if (IsUnaligned)
4030 Flags |= 4;
4031 if (IsReference)
4032 Flags |= 8;
4033
4034 return CatchTypeInfo{.RTTI: getAddrOfRTTIDescriptor(Ty: Type)->stripPointerCasts(),
4035 .Flags: Flags};
4036}
4037
4038/// Gets a TypeDescriptor. Returns a llvm::Constant * rather than a
4039/// llvm::GlobalVariable * because different type descriptors have different
4040/// types, and need to be abstracted. They are abstracting by casting the
4041/// address to an Int8PtrTy.
4042llvm::Constant *MicrosoftCXXABI::getAddrOfRTTIDescriptor(QualType Type) {
4043 SmallString<256> MangledName;
4044 {
4045 llvm::raw_svector_ostream Out(MangledName);
4046 getMangleContext().mangleCXXRTTI(T: Type, Out);
4047 }
4048
4049 // Check to see if we've already declared this TypeDescriptor.
4050 if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name: MangledName))
4051 return GV;
4052
4053 // Note for the future: If we would ever like to do deferred emission of
4054 // RTTI, check if emitting vtables opportunistically need any adjustment.
4055
4056 // Compute the fields for the TypeDescriptor.
4057 SmallString<256> TypeInfoString;
4058 {
4059 llvm::raw_svector_ostream Out(TypeInfoString);
4060 getMangleContext().mangleCXXRTTIName(T: Type, Out);
4061 }
4062
4063 // Declare and initialize the TypeDescriptor.
4064 llvm::Constant *Fields[] = {
4065 getTypeInfoVTable(CGM), // VFPtr
4066 llvm::ConstantPointerNull::get(T: CGM.Int8PtrTy), // Runtime data
4067 llvm::ConstantDataArray::getString(Context&: CGM.getLLVMContext(), Initializer: TypeInfoString)};
4068 llvm::StructType *TypeDescriptorType =
4069 getTypeDescriptorType(TypeInfoString);
4070 auto *Var = new llvm::GlobalVariable(
4071 CGM.getModule(), TypeDescriptorType, /*isConstant=*/false,
4072 getLinkageForRTTI(Ty: Type),
4073 llvm::ConstantStruct::get(T: TypeDescriptorType, V: Fields),
4074 MangledName);
4075 if (Var->isWeakForLinker())
4076 Var->setComdat(CGM.getModule().getOrInsertComdat(Name: Var->getName()));
4077 return Var;
4078}
4079
4080/// Gets or a creates a Microsoft CompleteObjectLocator.
4081llvm::GlobalVariable *
4082MicrosoftCXXABI::getMSCompleteObjectLocator(const CXXRecordDecl *RD,
4083 const VPtrInfo &Info) {
4084 return MSRTTIBuilder(*this, RD).getCompleteObjectLocator(Info);
4085}
4086
4087void MicrosoftCXXABI::emitCXXStructor(GlobalDecl GD) {
4088 if (auto *ctor = dyn_cast<CXXConstructorDecl>(Val: GD.getDecl())) {
4089 // There are no constructor variants, always emit the complete destructor.
4090 llvm::Function *Fn =
4091 CGM.codegenCXXStructor(GD: GD.getWithCtorType(Type: Ctor_Complete));
4092 CGM.maybeSetTrivialComdat(D: *ctor, GO&: *Fn);
4093 return;
4094 }
4095
4096 auto *dtor = cast<CXXDestructorDecl>(Val: GD.getDecl());
4097
4098 // Emit the base destructor if the base and complete (vbase) destructors are
4099 // equivalent. This effectively implements -mconstructor-aliases as part of
4100 // the ABI.
4101 if (GD.getDtorType() == Dtor_Complete &&
4102 dtor->getParent()->getNumVBases() == 0)
4103 GD = GD.getWithDtorType(Type: Dtor_Base);
4104
4105 // The base destructor is equivalent to the base destructor of its
4106 // base class if there is exactly one non-virtual base class with a
4107 // non-trivial destructor, there are no fields with a non-trivial
4108 // destructor, and the body of the destructor is trivial.
4109 if (GD.getDtorType() == Dtor_Base && !CGM.TryEmitBaseDestructorAsAlias(D: dtor))
4110 return;
4111
4112 if (GD.getDtorType() == Dtor_VectorDeleting &&
4113 !CGM.classNeedsVectorDestructor(RD: dtor->getParent())) {
4114 // Create GlobalDecl object with the correct type for the scalar
4115 // deleting destructor.
4116 GlobalDecl ScalarDtorGD(dtor, Dtor_Deleting);
4117
4118 // Emit an alias from the vector deleting destructor to the scalar deleting
4119 // destructor.
4120 CGM.EmitDefinitionAsAlias(Alias: GD, Target: ScalarDtorGD);
4121 return;
4122 }
4123
4124 llvm::Function *Fn = CGM.codegenCXXStructor(GD);
4125 if (Fn->isWeakForLinker())
4126 Fn->setComdat(CGM.getModule().getOrInsertComdat(Name: Fn->getName()));
4127}
4128
4129llvm::Function *
4130MicrosoftCXXABI::getAddrOfCXXCtorClosure(const CXXConstructorDecl *CD,
4131 CXXCtorType CT) {
4132 assert(CT == Ctor_CopyingClosure || CT == Ctor_DefaultClosure);
4133
4134 // Calculate the mangled name.
4135 SmallString<256> ThunkName;
4136 llvm::raw_svector_ostream Out(ThunkName);
4137 getMangleContext().mangleName(GD: GlobalDecl(CD, CT), Out);
4138
4139 // If the thunk has been generated previously, just return it.
4140 if (llvm::GlobalValue *GV = CGM.getModule().getNamedValue(Name: ThunkName))
4141 return cast<llvm::Function>(Val: GV);
4142
4143 // Create the llvm::Function.
4144 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeMSCtorClosure(CD, CT);
4145 llvm::FunctionType *ThunkTy = CGM.getTypes().GetFunctionType(Info: FnInfo);
4146 const CXXRecordDecl *RD = CD->getParent();
4147 CanQualType RecordTy = getContext().getCanonicalTagType(TD: RD);
4148 llvm::Function *ThunkFn = llvm::Function::Create(
4149 Ty: ThunkTy, Linkage: getLinkageForRTTI(Ty: RecordTy), N: ThunkName.str(), M: &CGM.getModule());
4150 ThunkFn->setCallingConv(static_cast<llvm::CallingConv::ID>(
4151 FnInfo.getEffectiveCallingConvention()));
4152 if (ThunkFn->isWeakForLinker())
4153 ThunkFn->setComdat(CGM.getModule().getOrInsertComdat(Name: ThunkFn->getName()));
4154 bool IsCopy = CT == Ctor_CopyingClosure;
4155
4156 // Start codegen.
4157 CodeGenFunction CGF(CGM);
4158 CGF.CurGD = GlobalDecl(CD, Ctor_Complete);
4159
4160 // Build FunctionArgs.
4161 FunctionArgList FunctionArgs;
4162
4163 // A constructor always starts with a 'this' pointer as its first argument.
4164 buildThisParam(CGF, Params&: FunctionArgs);
4165
4166 // Following the 'this' pointer is a reference to the source object that we
4167 // are copying from.
4168 auto *SrcParam = ImplicitParamDecl::Create(
4169 C&: getContext(), /*DC=*/nullptr, IdLoc: SourceLocation(),
4170 Id: &getContext().Idents.get(Name: "src"),
4171 T: getContext().getLValueReferenceType(T: RecordTy,
4172 /*SpelledAsLValue=*/true),
4173 ParamKind: ImplicitParamKind::Other);
4174 if (IsCopy)
4175 FunctionArgs.push_back(Elt: SrcParam);
4176
4177 // Constructors for classes which utilize virtual bases have an additional
4178 // parameter which indicates whether or not it is being delegated to by a more
4179 // derived constructor.
4180 auto *IsMostDerived =
4181 ImplicitParamDecl::Create(C&: getContext(), /*DC=*/nullptr, IdLoc: SourceLocation(),
4182 Id: &getContext().Idents.get(Name: "is_most_derived"),
4183 T: getContext().IntTy, ParamKind: ImplicitParamKind::Other);
4184 // Only add the parameter to the list if the class has virtual bases.
4185 if (RD->getNumVBases() > 0)
4186 FunctionArgs.push_back(Elt: IsMostDerived);
4187
4188 // Start defining the function.
4189 auto NL = ApplyDebugLocation::CreateEmpty(CGF);
4190 CGF.StartFunction(GD: GlobalDecl(), RetTy: FnInfo.getReturnType(), Fn: ThunkFn, FnInfo,
4191 Args: FunctionArgs, Loc: CD->getLocation(), StartLoc: SourceLocation());
4192 // Create a scope with an artificial location for the body of this function.
4193 auto AL = ApplyDebugLocation::CreateArtificial(CGF);
4194 setCXXABIThisValue(CGF, ThisPtr: loadIncomingCXXThis(CGF));
4195 llvm::Value *This = getThisValue(CGF);
4196
4197 llvm::Value *SrcVal =
4198 IsCopy ? CGF.Builder.CreateLoad(Addr: CGF.GetAddrOfLocalVar(VD: SrcParam), Name: "src")
4199 : nullptr;
4200
4201 CallArgList Args;
4202
4203 // Push the this ptr.
4204 Args.add(rvalue: RValue::get(V: This), type: CD->getThisType());
4205
4206 // Push the src ptr.
4207 if (SrcVal)
4208 Args.add(rvalue: RValue::get(V: SrcVal), type: SrcParam->getType());
4209
4210 // Add the rest of the default arguments.
4211 SmallVector<const Stmt *, 4> ArgVec;
4212 ArrayRef<ParmVarDecl *> params = CD->parameters().drop_front(N: IsCopy ? 1 : 0);
4213 for (const ParmVarDecl *PD : params) {
4214 assert(PD->hasDefaultArg() && "ctor closure lacks default args");
4215 ArgVec.push_back(Elt: PD->getDefaultArg());
4216 }
4217
4218 CodeGenFunction::RunCleanupsScope Cleanups(CGF);
4219
4220 const auto *FPT = CD->getType()->castAs<FunctionProtoType>();
4221 CGF.EmitCallArgs(Args, Prototype: FPT, ArgRange: llvm::ArrayRef(ArgVec), AC: CD, ParamsToSkip: IsCopy ? 1 : 0);
4222
4223 // Insert any ABI-specific implicit constructor arguments.
4224 AddedStructorArgCounts ExtraArgs =
4225 addImplicitConstructorArgs(CGF, D: CD, Type: Ctor_Complete,
4226 /*ForVirtualBase=*/false,
4227 /*Delegating=*/false, Args);
4228 // Call the destructor with our arguments.
4229 llvm::Constant *CalleePtr =
4230 CGM.getAddrOfCXXStructor(GD: GlobalDecl(CD, Ctor_Complete));
4231 CGCallee Callee =
4232 CGCallee::forDirect(functionPtr: CalleePtr, abstractInfo: GlobalDecl(CD, Ctor_Complete));
4233 const CGFunctionInfo &CalleeInfo = CGM.getTypes().arrangeCXXConstructorCall(
4234 Args, D: CD, CtorKind: Ctor_Complete, ExtraPrefixArgs: ExtraArgs.Prefix, ExtraSuffixArgs: ExtraArgs.Suffix);
4235 CGF.EmitCall(CallInfo: CalleeInfo, Callee, ReturnValue: ReturnValueSlot(), Args);
4236
4237 Cleanups.ForceCleanup();
4238
4239 // Emit the ret instruction, remove any temporary instructions created for the
4240 // aid of CodeGen.
4241 CGF.FinishFunction(EndLoc: SourceLocation());
4242
4243 return ThunkFn;
4244}
4245
4246llvm::Constant *MicrosoftCXXABI::getCatchableType(QualType T,
4247 uint32_t NVOffset,
4248 int32_t VBPtrOffset,
4249 uint32_t VBIndex) {
4250 assert(!T->isReferenceType());
4251
4252 CXXRecordDecl *RD = T->getAsCXXRecordDecl();
4253 const CXXConstructorDecl *CD =
4254 RD ? CGM.getContext().getCopyConstructorForExceptionObject(RD) : nullptr;
4255 CXXCtorType CT = Ctor_Complete;
4256 if (CD)
4257 if (!hasDefaultCXXMethodCC(Context&: getContext(), MD: CD) || CD->getNumParams() != 1)
4258 CT = Ctor_CopyingClosure;
4259
4260 uint32_t Size = getContext().getTypeSizeInChars(T).getQuantity();
4261 SmallString<256> MangledName;
4262 {
4263 llvm::raw_svector_ostream Out(MangledName);
4264 getMangleContext().mangleCXXCatchableType(T, CD, CT, Size, NVOffset,
4265 VBPtrOffset, VBIndex, Out);
4266 }
4267 if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name: MangledName))
4268 return getImageRelativeConstant(PtrVal: GV);
4269
4270 // The TypeDescriptor is used by the runtime to determine if a catch handler
4271 // is appropriate for the exception object.
4272 llvm::Constant *TD = getImageRelativeConstant(PtrVal: getAddrOfRTTIDescriptor(Type: T));
4273
4274 // The runtime is responsible for calling the copy constructor if the
4275 // exception is caught by value.
4276 llvm::Constant *CopyCtor;
4277 if (CD) {
4278 if (CT == Ctor_CopyingClosure)
4279 CopyCtor = getAddrOfCXXCtorClosure(CD, CT: Ctor_CopyingClosure);
4280 else
4281 CopyCtor = CGM.getAddrOfCXXStructor(GD: GlobalDecl(CD, Ctor_Complete));
4282 } else {
4283 CopyCtor = llvm::Constant::getNullValue(Ty: CGM.Int8PtrTy);
4284 }
4285 CopyCtor = getImageRelativeConstant(PtrVal: CopyCtor);
4286
4287 bool IsScalar = !RD;
4288 bool HasVirtualBases = false;
4289 bool IsStdBadAlloc = false; // std::bad_alloc is special for some reason.
4290 QualType PointeeType = T;
4291 if (T->isPointerType())
4292 PointeeType = T->getPointeeType();
4293 if (const CXXRecordDecl *RD = PointeeType->getAsCXXRecordDecl()) {
4294 HasVirtualBases = RD->getNumVBases() > 0;
4295 if (IdentifierInfo *II = RD->getIdentifier())
4296 IsStdBadAlloc = II->isStr(Str: "bad_alloc") && RD->isInStdNamespace();
4297 }
4298
4299 // Encode the relevant CatchableType properties into the Flags bitfield.
4300 // FIXME: Figure out how bits 2 or 8 can get set.
4301 uint32_t Flags = 0;
4302 if (IsScalar)
4303 Flags |= 1;
4304 if (HasVirtualBases)
4305 Flags |= 4;
4306 if (IsStdBadAlloc)
4307 Flags |= 16;
4308
4309 llvm::Constant *Fields[] = {
4310 llvm::ConstantInt::get(Ty: CGM.IntTy, V: Flags), // Flags
4311 TD, // TypeDescriptor
4312 llvm::ConstantInt::get(Ty: CGM.IntTy, V: NVOffset), // NonVirtualAdjustment
4313 llvm::ConstantInt::getSigned(Ty: CGM.IntTy, V: VBPtrOffset), // OffsetToVBPtr
4314 llvm::ConstantInt::get(Ty: CGM.IntTy, V: VBIndex), // VBTableIndex
4315 llvm::ConstantInt::get(Ty: CGM.IntTy, V: Size), // Size
4316 CopyCtor // CopyCtor
4317 };
4318 llvm::StructType *CTType = getCatchableTypeType();
4319 auto *GV = new llvm::GlobalVariable(
4320 CGM.getModule(), CTType, /*isConstant=*/true, getLinkageForRTTI(Ty: T),
4321 llvm::ConstantStruct::get(T: CTType, V: Fields), MangledName);
4322 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
4323 GV->setSection(".xdata");
4324 if (GV->isWeakForLinker())
4325 GV->setComdat(CGM.getModule().getOrInsertComdat(Name: GV->getName()));
4326 return getImageRelativeConstant(PtrVal: GV);
4327}
4328
4329llvm::GlobalVariable *MicrosoftCXXABI::getCatchableTypeArray(QualType T) {
4330 assert(!T->isReferenceType());
4331
4332 // See if we've already generated a CatchableTypeArray for this type before.
4333 llvm::GlobalVariable *&CTA = CatchableTypeArrays[T];
4334 if (CTA)
4335 return CTA;
4336
4337 // Ensure that we don't have duplicate entries in our CatchableTypeArray by
4338 // using a SmallSetVector. Duplicates may arise due to virtual bases
4339 // occurring more than once in the hierarchy.
4340 llvm::SmallSetVector<llvm::Constant *, 2> CatchableTypes;
4341
4342 // C++14 [except.handle]p3:
4343 // A handler is a match for an exception object of type E if [...]
4344 // - the handler is of type cv T or cv T& and T is an unambiguous public
4345 // base class of E, or
4346 // - the handler is of type cv T or const T& where T is a pointer type and
4347 // E is a pointer type that can be converted to T by [...]
4348 // - a standard pointer conversion (4.10) not involving conversions to
4349 // pointers to private or protected or ambiguous classes
4350 const CXXRecordDecl *MostDerivedClass = nullptr;
4351 bool IsPointer = T->isPointerType();
4352 if (IsPointer)
4353 MostDerivedClass = T->getPointeeType()->getAsCXXRecordDecl();
4354 else
4355 MostDerivedClass = T->getAsCXXRecordDecl();
4356
4357 // Collect all the unambiguous public bases of the MostDerivedClass.
4358 if (MostDerivedClass) {
4359 const ASTContext &Context = getContext();
4360 const ASTRecordLayout &MostDerivedLayout =
4361 Context.getASTRecordLayout(D: MostDerivedClass);
4362 MicrosoftVTableContext &VTableContext = CGM.getMicrosoftVTableContext();
4363 SmallVector<MSRTTIClass, 8> Classes;
4364 serializeClassHierarchy(Classes, RD: MostDerivedClass);
4365 Classes.front().initialize(/*Parent=*/nullptr, /*Specifier=*/nullptr);
4366 detectAmbiguousBases(Classes);
4367 for (const MSRTTIClass &Class : Classes) {
4368 // Skip any ambiguous or private bases.
4369 if (Class.Flags &
4370 (MSRTTIClass::IsPrivateOnPath | MSRTTIClass::IsAmbiguous))
4371 continue;
4372 // Write down how to convert from a derived pointer to a base pointer.
4373 uint32_t OffsetInVBTable = 0;
4374 int32_t VBPtrOffset = -1;
4375 if (Class.VirtualRoot) {
4376 OffsetInVBTable =
4377 VTableContext.getVBTableIndex(Derived: MostDerivedClass, VBase: Class.VirtualRoot)*4;
4378 VBPtrOffset = MostDerivedLayout.getVBPtrOffset().getQuantity();
4379 }
4380
4381 // Turn our record back into a pointer if the exception object is a
4382 // pointer.
4383 CanQualType RTTITy = Context.getCanonicalTagType(TD: Class.RD);
4384 if (IsPointer)
4385 RTTITy = Context.getPointerType(T: RTTITy);
4386 CatchableTypes.insert(X: getCatchableType(T: RTTITy, NVOffset: Class.OffsetInVBase,
4387 VBPtrOffset, VBIndex: OffsetInVBTable));
4388 }
4389 }
4390
4391 // C++14 [except.handle]p3:
4392 // A handler is a match for an exception object of type E if
4393 // - The handler is of type cv T or cv T& and E and T are the same type
4394 // (ignoring the top-level cv-qualifiers)
4395 CatchableTypes.insert(X: getCatchableType(T));
4396
4397 // C++14 [except.handle]p3:
4398 // A handler is a match for an exception object of type E if
4399 // - the handler is of type cv T or const T& where T is a pointer type and
4400 // E is a pointer type that can be converted to T by [...]
4401 // - a standard pointer conversion (4.10) not involving conversions to
4402 // pointers to private or protected or ambiguous classes
4403 //
4404 // C++14 [conv.ptr]p2:
4405 // A prvalue of type "pointer to cv T," where T is an object type, can be
4406 // converted to a prvalue of type "pointer to cv void".
4407 if (IsPointer && T->getPointeeType()->isObjectType())
4408 CatchableTypes.insert(X: getCatchableType(T: getContext().VoidPtrTy));
4409
4410 // C++14 [except.handle]p3:
4411 // A handler is a match for an exception object of type E if [...]
4412 // - the handler is of type cv T or const T& where T is a pointer or
4413 // pointer to member type and E is std::nullptr_t.
4414 //
4415 // We cannot possibly list all possible pointer types here, making this
4416 // implementation incompatible with the standard. However, MSVC includes an
4417 // entry for pointer-to-void in this case. Let's do the same.
4418 if (T->isNullPtrType())
4419 CatchableTypes.insert(X: getCatchableType(T: getContext().VoidPtrTy));
4420
4421 uint32_t NumEntries = CatchableTypes.size();
4422 llvm::Type *CTType = getImageRelativeType(PtrType: CGM.DefaultPtrTy);
4423 llvm::ArrayType *AT = llvm::ArrayType::get(ElementType: CTType, NumElements: NumEntries);
4424 llvm::StructType *CTAType = getCatchableTypeArrayType(NumEntries);
4425 llvm::Constant *Fields[] = {
4426 llvm::ConstantInt::get(Ty: CGM.IntTy, V: NumEntries), // NumEntries
4427 llvm::ConstantArray::get(
4428 T: AT, V: llvm::ArrayRef(CatchableTypes.begin(),
4429 CatchableTypes.end())) // CatchableTypes
4430 };
4431 SmallString<256> MangledName;
4432 {
4433 llvm::raw_svector_ostream Out(MangledName);
4434 getMangleContext().mangleCXXCatchableTypeArray(T, NumEntries, Out);
4435 }
4436 CTA = new llvm::GlobalVariable(
4437 CGM.getModule(), CTAType, /*isConstant=*/true, getLinkageForRTTI(Ty: T),
4438 llvm::ConstantStruct::get(T: CTAType, V: Fields), MangledName);
4439 CTA->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
4440 CTA->setSection(".xdata");
4441 if (CTA->isWeakForLinker())
4442 CTA->setComdat(CGM.getModule().getOrInsertComdat(Name: CTA->getName()));
4443 return CTA;
4444}
4445
4446llvm::GlobalVariable *MicrosoftCXXABI::getThrowInfo(QualType T) {
4447 bool IsConst, IsVolatile, IsUnaligned;
4448 T = decomposeTypeForEH(Context&: getContext(), T, IsConst, IsVolatile, IsUnaligned);
4449
4450 // The CatchableTypeArray enumerates the various (CV-unqualified) types that
4451 // the exception object may be caught as.
4452 llvm::GlobalVariable *CTA = getCatchableTypeArray(T);
4453 // The first field in a CatchableTypeArray is the number of CatchableTypes.
4454 // This is used as a component of the mangled name which means that we need to
4455 // know what it is in order to see if we have previously generated the
4456 // ThrowInfo.
4457 uint32_t NumEntries =
4458 cast<llvm::ConstantInt>(Val: CTA->getInitializer()->getAggregateElement(Elt: 0U))
4459 ->getLimitedValue();
4460
4461 SmallString<256> MangledName;
4462 {
4463 llvm::raw_svector_ostream Out(MangledName);
4464 getMangleContext().mangleCXXThrowInfo(T, IsConst, IsVolatile, IsUnaligned,
4465 NumEntries, Out);
4466 }
4467
4468 // Reuse a previously generated ThrowInfo if we have generated an appropriate
4469 // one before.
4470 if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name: MangledName))
4471 return GV;
4472
4473 // The RTTI TypeDescriptor uses an unqualified type but catch clauses must
4474 // be at least as CV qualified. Encode this requirement into the Flags
4475 // bitfield.
4476 uint32_t Flags = 0;
4477 if (IsConst)
4478 Flags |= 1;
4479 if (IsVolatile)
4480 Flags |= 2;
4481 if (IsUnaligned)
4482 Flags |= 4;
4483
4484 // The cleanup-function (a destructor) must be called when the exception
4485 // object's lifetime ends.
4486 llvm::Constant *CleanupFn = llvm::Constant::getNullValue(Ty: CGM.Int8PtrTy);
4487 if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
4488 if (CXXDestructorDecl *DtorD = RD->getDestructor())
4489 if (!DtorD->isTrivial())
4490 CleanupFn = CGM.getAddrOfCXXStructor(GD: GlobalDecl(DtorD, Dtor_Complete));
4491 // This is unused as far as we can tell, initialize it to null.
4492 llvm::Constant *ForwardCompat =
4493 getImageRelativeConstant(PtrVal: llvm::Constant::getNullValue(Ty: CGM.Int8PtrTy));
4494 llvm::Constant *PointerToCatchableTypes = getImageRelativeConstant(PtrVal: CTA);
4495 llvm::StructType *TIType = getThrowInfoType();
4496 llvm::Constant *Fields[] = {
4497 llvm::ConstantInt::get(Ty: CGM.IntTy, V: Flags), // Flags
4498 getImageRelativeConstant(PtrVal: CleanupFn), // CleanupFn
4499 ForwardCompat, // ForwardCompat
4500 PointerToCatchableTypes // CatchableTypeArray
4501 };
4502 auto *GV = new llvm::GlobalVariable(
4503 CGM.getModule(), TIType, /*isConstant=*/true, getLinkageForRTTI(Ty: T),
4504 llvm::ConstantStruct::get(T: TIType, V: Fields), MangledName.str());
4505 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
4506 GV->setSection(".xdata");
4507 if (GV->isWeakForLinker())
4508 GV->setComdat(CGM.getModule().getOrInsertComdat(Name: GV->getName()));
4509 return GV;
4510}
4511
4512void MicrosoftCXXABI::emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) {
4513 const Expr *SubExpr = E->getSubExpr();
4514 assert(SubExpr && "SubExpr cannot be null");
4515 QualType ThrowType = SubExpr->getType();
4516 // The exception object lives on the stack and it's address is passed to the
4517 // runtime function.
4518 Address AI = CGF.CreateMemTempWithoutCast(T: ThrowType);
4519 CGF.EmitAnyExprToMem(E: SubExpr, Location: AI, Quals: ThrowType.getQualifiers(),
4520 /*IsInit=*/IsInitializer: true);
4521
4522 // The so-called ThrowInfo is used to describe how the exception object may be
4523 // caught.
4524 llvm::GlobalVariable *TI = getThrowInfo(T: ThrowType);
4525
4526 // Call into the runtime to throw the exception.
4527 llvm::Value *Args[] = {AI.emitRawPointer(CGF), TI};
4528 CGF.EmitNoreturnRuntimeCallOrInvoke(callee: getThrowFn(), args: Args);
4529}
4530
4531std::pair<llvm::Value *, const CXXRecordDecl *>
4532MicrosoftCXXABI::LoadVTablePtr(CodeGenFunction &CGF, Address This,
4533 const CXXRecordDecl *RD) {
4534 CanQualType T = CGF.getContext().getCanonicalTagType(TD: RD);
4535 std::tie(args&: This, args: std::ignore, args&: RD) = performBaseAdjustment(CGF, Value: This, SrcRecordTy: T);
4536 return {CGF.GetVTablePtr(This, VTableTy: CGM.Int8PtrTy, VTableClass: RD), RD};
4537}
4538
4539bool MicrosoftCXXABI::isPermittedToBeHomogeneousAggregate(
4540 const CXXRecordDecl *RD) const {
4541 // All aggregates are permitted to be HFA on non-ARM platforms, which mostly
4542 // affects vectorcall on x64/x86.
4543 if (!CGM.getTarget().getTriple().isAArch64())
4544 return true;
4545 // MSVC Windows on Arm64 has its own rules for determining if a type is HFA
4546 // that are inconsistent with the AAPCS64 ABI. The following are our best
4547 // determination of those rules so far, based on observation of MSVC's
4548 // behavior.
4549 if (RD->isEmpty())
4550 return false;
4551 if (RD->isPolymorphic())
4552 return false;
4553 if (RD->hasNonTrivialCopyAssignment())
4554 return false;
4555 if (RD->hasNonTrivialDestructor())
4556 return false;
4557 if (RD->hasNonTrivialDefaultConstructor())
4558 return false;
4559 // These two are somewhat redundant given the caller
4560 // (ABIInfo::isHomogeneousAggregate) checks the bases and fields, but that
4561 // caller doesn't consider empty bases/fields to be non-homogenous, but it
4562 // looks like Microsoft's AArch64 ABI does care about these empty types &
4563 // anything containing/derived from one is non-homogeneous.
4564 // Instead we could add another CXXABI entry point to query this property and
4565 // have ABIInfo::isHomogeneousAggregate use that property.
4566 // I don't think any other of the features listed above could be true of a
4567 // base/field while not true of the outer struct. For example, if you have a
4568 // base/field that has an non-trivial copy assignment/dtor/default ctor, then
4569 // the outer struct's corresponding operation must be non-trivial.
4570 for (const CXXBaseSpecifier &B : RD->bases()) {
4571 if (const CXXRecordDecl *FRD = B.getType()->getAsCXXRecordDecl()) {
4572 if (!isPermittedToBeHomogeneousAggregate(RD: FRD))
4573 return false;
4574 }
4575 }
4576 // empty fields seem to be caught by the ABIInfo::isHomogeneousAggregate
4577 // checking for padding - but maybe there are ways to end up with an empty
4578 // field without padding? Not that I know of, so don't check fields here &
4579 // rely on the padding check.
4580 return true;
4581}
4582