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