clang 24.0.0git
MicrosoftCXXABI.cpp
Go to the documentation of this file.
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"
26#include "clang/AST/Decl.h"
27#include "clang/AST/DeclCXX.h"
28#include "clang/AST/StmtCXX.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) {
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.
72 switch (GD.getDtorType()) {
73 case Dtor_Complete:
74 case Dtor_Deleting:
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(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(VBT->IntroducingObject);
112 CharUnits Offs = VBT->NonVirtualOffset;
113 Offs += SubobjectLayout.getVBPtrOffset();
114 if (VBT->getVBaseWithVPtr())
115 Offs += Layout.getVBaseClassOffset(VBT->getVBaseWithVPtr());
116 VBPtrOffsets.push_back(Offs);
117 }
118 llvm::array_pod_sort(VBPtrOffsets.begin(), 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{nullptr, 0};
147 else
148 return CatchTypeInfo{nullptr, 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>(GD.getDecl());
262
263 if (MD->isVirtual()) {
264 GlobalDecl LookupGD = GD;
265 if (const auto *DD = dyn_cast<CXXDestructorDecl>(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(
275 CGM.getContext().getLangOpts())
277 : Dtor_Deleting);
278 }
279 MethodVFTableLocation ML =
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,
307 bool ForVirtualBase,
308 bool Delegating) override;
309
310 llvm::Value *getCXXDestructorImplicitParam(CodeGenFunction &CGF,
311 const CXXDestructorDecl *DD,
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::get(getStructorImplicitParamValue(CGF)),
363 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(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(CGM.IntTy));
387 Map[0] = llvm::ConstantInt::get(CGM.IntTy, 0);
388 bool AnyDifferent = false;
389 for (const auto &I : SrcRD->vbases()) {
390 const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl();
391 if (!DstRD->isVirtuallyDerivedFrom(VBase))
392 continue;
393
394 unsigned SrcVBIndex = VTContext.getVBTableIndex(SrcRD, VBase);
395 unsigned DstVBIndex = VTContext.getVBTableIndex(DstRD, VBase);
396 Map[SrcVBIndex] = llvm::ConstantInt::get(CGM.IntTy, 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(CGM.IntTy, Map.size());
404 llvm::Constant *Init = llvm::ConstantArray::get(VDispMapTy, 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 {
421 getContext().GetGVALinkageForFunction(cast<FunctionDecl>(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 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(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(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(CGM.Int8Ty, TypeInfoString.size() + 1)};
524 TypeDescriptorType =
525 llvm::StructType::create(CGM.getLLVMContext(), FieldTypes, 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(CGM.Int8PtrTy),
540 CGM.IntTy,
541 CGM.IntTy,
542 CGM.IntTy,
543 CGM.IntTy,
544 CGM.IntTy,
545 getImageRelativeType(CGM.DefaultPtrTy),
546 };
547 BaseClassDescriptorType = llvm::StructType::create(
548 CGM.getLLVMContext(), FieldTypes, "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(CGM.DefaultPtrTy)};
558 ClassHierarchyDescriptorType =
559 llvm::StructType::create(FieldTypes, "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(CGM.Int8PtrTy),
571 getImageRelativeType(CGM.DefaultPtrTy),
572 getImageRelativeType(CGM.VoidTy),
573 };
574 llvm::ArrayRef<llvm::Type *> FieldTypesRef(FieldTypes);
575 if (!isRTTIImageRelative())
576 FieldTypesRef = FieldTypesRef.drop_back();
577 CompleteObjectLocatorType =
578 llvm::StructType::create(FieldTypesRef, "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(getImageBase(), CGM.IntPtrTy);
616 llvm::Constant *PtrValAsInt =
617 llvm::ConstantExpr::getPtrToInt(PtrVal, CGM.IntPtrTy);
618 llvm::Constant *Diff =
619 llvm::ConstantExpr::getSub(PtrValAsInt, 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(Diff, CGM.IntTy);
625 }
626
627private:
628 MicrosoftMangleContext &getMangleContext() {
630 }
631
632 llvm::Constant *getZeroInt() {
633 return llvm::ConstantInt::get(CGM.IntTy, 0);
634 }
635
636 llvm::Constant *getAllOnesInt() {
637 return llvm::Constant::getAllOnesValue(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(CGM.IntTy, VBPtrOffset);
663 llvm::Value *VBTOffset = llvm::ConstantInt::get(CGM.IntTy, VBTableOffset);
664 return GetVBaseOffsetFromVBPtr(CGF, Base, VBPOffset, 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,
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,
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(CGM.Int8PtrTy), // TypeDescriptor
766 CGM.IntTy, // NonVirtualAdjustment
767 CGM.IntTy, // OffsetToVBPtr
768 CGM.IntTy, // VBTableIndex
769 CGM.IntTy, // Size
770 getThrowInfoRelativeType(CGM.Int8PtrTy) // CopyCtor
771 };
772 CatchableTypeType = llvm::StructType::create(
773 CGM.getLLVMContext(), FieldTypes, "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(NumEntries);
785 llvm::Type *CTType = getThrowInfoRelativeType(CGM.DefaultPtrTy);
786 llvm::Type *FieldTypes[] = {
787 CGM.IntTy, // NumEntries
788 llvm::ArrayType::get(CTType, NumEntries) // CatchableTypes
789 };
790 CatchableTypeArrayType =
791 llvm::StructType::create(CGM.getLLVMContext(), FieldTypes, 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(CGM.Int8PtrTy), // CleanupFn
801 getThrowInfoRelativeType(CGM.Int8PtrTy), // ForwardCompat
802 getThrowInfoRelativeType(CGM.Int8PtrTy) // CatchableTypeArray
803 };
804 ThrowInfoType = llvm::StructType::create(CGM.getLLVMContext(), FieldTypes,
805 "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(CGM.VoidTy, Args, /*isVarArg=*/false);
815 llvm::FunctionCallee Throw =
816 CGM.CreateRuntimeFunction(FTy, "_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>(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
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(getContext().getCanonicalTagType(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, Ptr, DE,
942 /*CallOrInvoke=*/nullptr);
943 if (UseGlobalDelete)
944 CGF.EmitDeleteCall(DE->getOperatorDelete(), MDThis, ElementType);
945 } else {
946 EmitVirtualDestructorCall(CGF, Dtor, Dtor_Deleting, Ptr, DE,
947 /*CallOrInvoke=*/nullptr);
948 }
949}
950
951void MicrosoftCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) {
952 llvm::Value *Args[] = {llvm::ConstantPointerNull::get(CGM.Int8PtrTy),
953 llvm::ConstantPointerNull::get(CGM.DefaultPtrTy)};
954 llvm::FunctionCallee Fn = getThrowFn();
955 if (isNoReturn)
957 else
958 CGF.EmitRuntimeCallOrInvoke(Fn, 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>(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>(NormalCleanup, CPI);
975 return;
976 }
977
978 CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam);
979 CPI->setArgOperand(2, var.getObjectAddress(CGF).emitRawPointer(CGF));
980 CGF.EHStack.pushCleanup<CatchRetScope>(NormalCleanup, CPI);
981 CGF.EmitAutoVarCleanups(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(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(SrcDecl).hasExtendableVFPtr())
998 return std::make_tuple(Value, llvm::ConstantInt::get(CGF.Int32Ty, 0),
999 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(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, Value, SrcDecl, PolymorphicBase);
1015 llvm::Value *Ptr = CGF.Builder.CreateInBoundsGEP(
1016 Value.getElementType(), Value.emitRawPointer(CGF), Offset);
1017 CharUnits VBaseAlign =
1018 CGF.CGM.getVBaseAlignment(Value.getAlignment(), SrcDecl, PolymorphicBase);
1019 return std::make_tuple(Address(Ptr, CGF.Int8Ty, VBaseAlign), Offset,
1020 PolymorphicBase);
1021}
1022
1023bool MicrosoftCXXABI::shouldTypeidBeNullChecked(QualType SrcRecordTy) {
1024 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
1025 return !getContext().getASTRecordLayout(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(CGF.Int8PtrTy, ArgTypes, false);
1033 llvm::Value *Args[] = {Argument};
1034 llvm::FunctionCallee Fn = CGF.CGM.CreateRuntimeFunction(FTy, "__RTtypeid");
1035 return CGF.EmitRuntimeCallOrInvoke(Fn, Args);
1036}
1037
1038void MicrosoftCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) {
1039 llvm::CallBase *Call =
1040 emitRTtypeidCall(CGF, llvm::Constant::getNullValue(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(ThisPtr, std::ignore, std::ignore) =
1050 performBaseAdjustment(CGF, ThisPtr, SrcRecordTy);
1051 llvm::CallBase *Typeid = emitRTtypeidCall(CGF, ThisPtr.emitRawPointer(CGF));
1052 return CGF.Builder.CreateBitCast(Typeid, StdTypeInfoPtrTy);
1053}
1054
1055bool MicrosoftCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
1056 QualType SrcRecordTy) {
1057 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
1058 return SrcIsPtr &&
1059 !getContext().getASTRecordLayout(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 =
1067 llvm::Value *DestRTTI =
1069
1070 llvm::Value *Offset;
1071 std::tie(This, Offset, std::ignore) =
1072 performBaseAdjustment(CGF, This, SrcRecordTy);
1073 llvm::Value *ThisPtr = This.emitRawPointer(CGF);
1074 Offset = CGF.Builder.CreateTrunc(Offset, 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 llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false),
1086 "__RTDynamicCast");
1087 llvm::Value *Args[] = {
1088 ThisPtr, Offset, SrcRTTI, DestRTTI,
1089 llvm::ConstantInt::get(CGF.Int32Ty, DestTy->isReferenceType())};
1090 return CGF.EmitRuntimeCallOrInvoke(Function, Args);
1091}
1092
1093llvm::Value *MicrosoftCXXABI::emitDynamicCastToVoid(CodeGenFunction &CGF,
1094 Address Value,
1095 QualType SrcRecordTy) {
1096 std::tie(Value, std::ignore, 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 llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false),
1104 "__RTCastToVoid");
1105 llvm::Value *Args[] = {Value.emitRawPointer(CGF)};
1106 return CGF.EmitRuntimeCall(Function, 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(ClassDecl).getVBPtrOffset().getQuantity();
1119 llvm::Value *VBPtrOffset = llvm::ConstantInt::get(CGM.PtrDiffTy, VBPtrChars);
1120 CharUnits IntSize = Context.getTypeSizeInChars(Context.IntTy);
1121 CharUnits VBTableChars =
1122 IntSize *
1123 CGM.getMicrosoftVTableContext().getVBTableIndex(ClassDecl, BaseClassDecl);
1124 llvm::Value *VBTableOffset =
1125 llvm::ConstantInt::get(CGM.IntTy, VBTableChars.getQuantity());
1126
1127 llvm::Value *VBPtrToNewBase =
1128 GetVBaseOffsetFromVBPtr(CGF, This, VBPtrOffset, VBTableOffset);
1129 VBPtrToNewBase =
1130 CGF.Builder.CreateSExtOrBitCast(VBPtrToNewBase, CGM.PtrDiffTy);
1131 return CGF.Builder.CreateNSWAdd(VBPtrOffset, VBPtrToNewBase);
1132}
1133
1134bool MicrosoftCXXABI::HasThisReturn(GlobalDecl GD) const {
1135 return isa<CXXConstructorDecl>(GD.getDecl());
1136}
1137
1139 return isa<CXXDestructorDecl>(GD.getDecl()) &&
1140 (GD.getDtorType() == Dtor_Deleting ||
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, NumElts) &&
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;
1190 return false;
1192 return false;
1193 for (const Decl *D : RD->decls()) {
1194 auto *Ctor = dyn_cast<CXXConstructorDecl>(D);
1195 if (!Ctor) {
1196 if (auto *Template = dyn_cast<FunctionTemplateDecl>(D))
1197 Ctor = dyn_cast<CXXConstructorDecl>(Template->getTemplatedDecl());
1198 }
1199 if (Ctor && Ctor->isUserProvided()) {
1200 return false;
1201 } else if (auto *MethodDecl = dyn_cast<CXXMethodDecl>(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, 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(FI.getReturnType());
1224 LangAS SRetAS = CGM.getTargetCodeGenInfo().getSRetAddrSpace(RD);
1225 unsigned AS = CGM.getContext().getTargetAddressSpace(SRetAS);
1226 FI.getReturnInfo() =
1227 ABIArgInfo::getIndirect(Align, /*AddrSpace=*/AS, /*ByVal=*/false);
1228
1229 // MSVC always passes `this` before the `sret` parameter.
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(IsMostDerivedClass, "is_complete_object");
1251
1252 llvm::BasicBlock *CallVbaseCtorsBB = CGF.createBasicBlock("ctor.init_vbases");
1253 llvm::BasicBlock *SkipVbaseCtorsBB = CGF.createBasicBlock("ctor.skip_vbases");
1254 CGF.Builder.CreateCondBr(IsCompleteObject,
1255 CallVbaseCtorsBB, SkipVbaseCtorsBB);
1256
1257 CGF.EmitBlock(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(IsMostDerivedClass, "is_complete_object");
1274
1275 llvm::BasicBlock *CallVbaseDtorsBB = CGF.createBasicBlock("Dtor.dtor_vbases");
1276 llvm::BasicBlock *SkipVbaseDtorsBB = CGF.createBasicBlock("Dtor.skip_vbases");
1277 CGF.Builder.CreateCondBr(IsCompleteObject,
1278 CallVbaseDtorsBB, SkipVbaseDtorsBB);
1279
1280 CGF.EmitBlock(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(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(VBase);
1311 assert(I != VBaseMap.end());
1312 if (!I->second.hasVtorDisp())
1313 continue;
1314
1315 llvm::Value *VBaseOffset =
1316 GetVirtualBaseClassOffset(CGF, getThisAddress(CGF), RD, 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 VBaseOffset, llvm::ConstantInt::get(CGM.PtrDiffTy, ConstantVBaseOffset),
1322 "vtordisp.value");
1323 VtorDispValue = Builder.CreateTruncOrBitCast(VtorDispValue, CGF.Int32Ty);
1324
1325 if (!Int8This)
1326 Int8This = getThisValue(CGF);
1327
1328 llvm::Value *VtorDispPtr =
1329 Builder.CreateInBoundsGEP(CGF.Int8Ty, Int8This, VBaseOffset);
1330 // vtorDisp is always the 32-bits before the vbase in the class layout.
1331 VtorDispPtr = Builder.CreateConstGEP1_32(CGF.Int8Ty, VtorDispPtr, -4);
1332
1333 Builder.CreateAlignedStore(VtorDispValue, VtorDispPtr,
1335 }
1336}
1337
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(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(getContext(), D) || D->getNumParams() != 0) {
1358 llvm::Function *Fn = getAddrOfCXXCtorClosure(D, Ctor_DefaultClosure);
1359 Fn->setLinkage(llvm::GlobalValue::WeakODRLinkage);
1360 CGM.setGVProperties(Fn, D);
1361 }
1362 }
1363}
1364
1365void MicrosoftCXXABI::EmitVBPtrStores(CodeGenFunction &CGF,
1366 const CXXRecordDecl *RD) {
1367 Address This = getThisAddress(CGF);
1368 This = This.withElementType(CGM.Int8Ty);
1369 const ASTContext &Context = getContext();
1370 const ASTRecordLayout &Layout = Context.getASTRecordLayout(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(VBT->IntroducingObject);
1378 CharUnits Offs = VBT->NonVirtualOffset;
1379 Offs += SubobjectLayout.getVBPtrOffset();
1380 if (VBT->getVBaseWithVPtr())
1381 Offs += Layout.getVBaseClassOffset(VBT->getVBaseWithVPtr());
1382 Address VBPtr = CGF.Builder.CreateConstInBoundsByteGEP(This, Offs);
1383 llvm::Value *GVPtr =
1384 CGF.Builder.CreateConstInBoundsGEP2_32(GV->getValueType(), GV, 0, 0);
1385 VBPtr = VBPtr.withElementType(GVPtr->getType());
1386 CGF.Builder.CreateStore(GVPtr, 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>(GD.getDecl()) &&
1396 (GD.getDtorType() == Dtor_Deleting ||
1398 // The scalar deleting destructor takes an implicit int parameter.
1399 ArgTys.push_back(getContext().IntTy);
1400 ++Added.Suffix;
1401 }
1402 auto *CD = dyn_cast<CXXConstructorDecl>(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(ArgTys.begin() + 1, getContext().IntTy);
1414 ++Added.Prefix;
1415 } else {
1416 ArgTys.push_back(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, 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(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");
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(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(GlobalDecl(D, Dtor_Complete));
1488}
1489
1490CharUnits
1491MicrosoftCXXABI::getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD) {
1492 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1493
1494 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(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())
1506 : Dtor_Deleting);
1507 }
1508
1509 MethodVFTableLocation ML =
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>(MD))
1518 Adjustment = CharUnits::Zero();
1519
1520 if (ML.VBase) {
1521 const ASTRecordLayout &DerivedLayout =
1522 getContext().getASTRecordLayout(MD->getParent());
1523 Adjustment += DerivedLayout.getVBaseClassOffset(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(CGF.Int8Ty);
1540 assert(Adjustment.isPositive());
1541 return CGF.Builder.CreateConstByteGEP(This, Adjustment);
1542 }
1543
1544 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1545
1546 GlobalDecl LookupGD = GD;
1547 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(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())
1559 : Dtor_Deleting);
1560 }
1561 MethodVFTableLocation ML =
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>(MD) && GD.getDtorType() == Dtor_Base)
1570 StaticOffset = CharUnits::Zero();
1571
1573 if (ML.VBase) {
1574 Result = Result.withElementType(CGF.Int8Ty);
1575
1576 const CXXRecordDecl *Derived = MD->getParent();
1577 const CXXRecordDecl *VBase = ML.VBase;
1578 llvm::Value *VBaseOffset =
1579 GetVirtualBaseClassOffset(CGF, Result, Derived, VBase);
1580 llvm::Value *VBasePtr = CGF.Builder.CreateInBoundsGEP(
1581 Result.getElementType(), Result.emitRawPointer(CGF), VBaseOffset);
1582 CharUnits VBaseAlign =
1583 CGF.CGM.getVBaseAlignment(Result.getAlignment(), Derived, VBase);
1584 Result = Address(VBasePtr, CGF.Int8Ty, VBaseAlign);
1585 }
1586 if (!StaticOffset.isZero()) {
1587 assert(StaticOffset.isPositive());
1588 Result = Result.withElementType(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(Result, StaticOffset);
1595 } else {
1596 Result = CGF.Builder.CreateConstInBoundsByteGEP(Result, 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>(CGF.CurGD.getDecl());
1608 if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) {
1609 auto *IsMostDerived = ImplicitParamDecl::Create(
1610 Context, /*DC=*/nullptr, CGF.CurGD.getDecl()->getLocation(),
1611 &Context.Idents.get("is_most_derived"), Context.IntTy,
1612 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(Params.begin() + 1, IsMostDerived);
1618 else
1619 Params.push_back(IsMostDerived);
1620 getStructorImplicitParamDecl(CGF) = IsMostDerived;
1621 } else if (isDeletingDtor(CGF.CurGD)) {
1622 auto *ShouldDelete = ImplicitParamDecl::Create(
1623 Context, /*DC=*/nullptr, CGF.CurGD.getDecl()->getLocation(),
1624 &Context.Idents.get("should_call_delete"), Context.IntTy,
1625 ImplicitParamKind::Other);
1626 Params.push_back(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>(CGF.CurGD.getDecl());
1650 if (!CGF.CurFuncIsThunk && MD->isVirtual()) {
1651 CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(CGF.CurGD);
1652 if (!Adjustment.isZero()) {
1653 assert(Adjustment.isPositive());
1654 This = CGF.Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, This,
1655 -Adjustment.getQuantity());
1656 }
1657 }
1658 setCXXABIThisValue(CGF, 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(CGF.CurGD) || hasMostDerivedReturn(CGF.CurGD))
1669 CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
1670
1671 if (isa<CXXConstructorDecl>(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 CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)),
1677 "is_most_derived");
1678 }
1679
1680 if (isDeletingDtor(CGF.CurGD)) {
1681 assert(getStructorImplicitParamDecl(CGF) &&
1682 "no implicit parameter for a deleting destructor?");
1683 getStructorImplicitParamValue(CGF)
1684 = CGF.Builder.CreateLoad(
1685 CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)),
1686 "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(CGM.Int32Ty, Type == Ctor_Complete);
1706 }
1707 if (FPT->isVariadic()) {
1708 return AddedStructorArgs::prefix({{MostDerivedArg, getContext().IntTy}});
1709 }
1710 return AddedStructorArgs::suffix({{MostDerivedArg, 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(CGM.getAddrOfCXXStructor(GD), 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, GlobalDecl(DD, Type),
1737 This, false);
1738 }
1739
1740 llvm::BasicBlock *BaseDtorEndBB = nullptr;
1741 if (ForVirtualBase && isa<CXXConstructorDecl>(CGF.CurCodeDecl)) {
1742 BaseDtorEndBB = EmitDtorCompleteObjectHandler(CGF);
1743 }
1744
1745 llvm::Value *Implicit =
1746 getCXXDestructorImplicitParam(CGF, DD, Type, ForVirtualBase,
1747 Delegating); // = nullptr
1748 CGF.EmitCXXDestructorCall(GD, Callee, CGF.getAsNaturalPointerTo(This, 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(BaseDtorEndBB);
1755 CGF.EmitBlock(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 &&
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 getContext().getTargetInfo().getPointerWidth(LangAS::Default))
1786 : CharUnits::Zero();
1787
1788 if (Info.PathToIntroducingObject.empty()) {
1789 CGM.AddVTableTypeMetadata(VTable, AddressPoint, RD);
1790 return;
1791 }
1792
1793 // Add a bitset entry for the least derived base belonging to this vftable.
1794 CGM.AddVTableTypeMetadata(VTable, AddressPoint,
1795 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(DerivedRD);
1805 CharUnits Offset;
1806 auto VBI = Layout.getVBaseOffsetsMap().find(BaseRD);
1807 if (VBI == Layout.getVBaseOffsetsMap().end())
1808 Offset = Layout.getBaseClassOffset(BaseRD);
1809 else
1810 Offset = VBI->second.VBaseOffset;
1811 if (!Offset.isZero())
1812 return;
1813 CGM.AddVTableTypeMetadata(VTable, AddressPoint, DerivedRD);
1814 }
1815
1816 // Finally do the same for the most derived class.
1817 if (Info.FullOffsetInMDC.isZero())
1818 CGM.AddVTableTypeMetadata(VTable, 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, Info->FullOffsetInMDC);
1828 if (VTable->hasInitializer())
1829 continue;
1830
1831 const VTableLayout &VTLayout =
1832 VFTContext.getVFTableLayout(RD, Info->FullOffsetInMDC);
1833
1834 llvm::Constant *RTTI = nullptr;
1835 if (any_of(VTLayout.vtable_components(),
1836 [](const VTableComponent &VTC) { return VTC.isRTTIKind(); }))
1837 RTTI = getMSCompleteObjectLocator(RD, *Info);
1838
1839 ConstantInitBuilder builder(CGM);
1840 auto components = builder.beginStruct();
1841 CGVT.createVTableInitializer(components, VTLayout, RTTI,
1842 VTable->hasLocalLinkage());
1843 components.finishAndSetAsInitializer(VTable);
1844
1845 emitVTableTypeMetadata(*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
1866 const CXXRecordDecl *RD, const VPtrInfo &VFPtr,
1867 SmallString<256> &Name) {
1868 llvm::raw_svector_ostream Out(Name);
1869 MangleContext.mangleCXXVFTable(RD, VFPtr.MangledPath, Out);
1870}
1871
1872llvm::Constant *
1873MicrosoftCXXABI::getVTableAddressPoint(BaseSubobject Base,
1874 const CXXRecordDecl *VTableClass) {
1875 (void)getAddrOfVTable(VTableClass, 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(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(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(VFPtrs, [&](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(getMangleContext(), RD, *VFPtr, 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(VFTableLinkage) ||
1939 llvm::GlobalValue::isExternalLinkage(VFTableLinkage);
1940 bool VTableAliasIsRequred =
1941 !VFTableComesFromAnotherTU && getContext().getLangOpts().RTTIData;
1942
1943 if (llvm::GlobalValue *VFTable =
1944 CGM.getModule().getNamedGlobal(VFTableName)) {
1945 VFTablesMap[ID] = VFTable;
1946 VTable = VTableAliasIsRequred
1948 cast<llvm::GlobalAlias>(VFTable)->getAliaseeObject())
1949 : cast<llvm::GlobalVariable>(VFTable);
1950 return VTable;
1951 }
1952
1953 const VTableLayout &VTLayout =
1954 VTContext.getVFTableLayout(RD, 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(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(VFTableLinkage))
1974 C = CGM.getModule().getOrInsertComdat(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(CGM.Int32Ty, 0),
1981 llvm::ConstantInt::get(CGM.Int32Ty, 0),
1982 llvm::ConstantInt::get(CGM.Int32Ty, 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 CGM.getDataLayout(), VTable->getValueType(), VTable, GEPIndices,
1987 llvm::GEPNoWrapFlags::inBounds());
1988 if (llvm::GlobalValue::isWeakForLinker(VFTableLinkage)) {
1989 VFTableLinkage = llvm::GlobalValue::ExternalLinkage;
1990 if (C)
1991 C->setSelectionKind(llvm::Comdat::Largest);
1992 }
1993 VFTable = llvm::GlobalAlias::create(CGM.Int8PtrTy,
1994 /*AddressSpace=*/0, VFTableLinkage,
1995 VFTableName.str(), VTableGEP,
1996 &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, true);
2026
2027 auto *MethodDecl = cast<CXXMethodDecl>(GD.getDecl());
2028 llvm::Value *VTable =
2029 CGF.GetVTablePtr(VPtr, CGF.DefaultPtrTy, 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(VFTContext.getVFPtrOffsets(
2039 ML.VBase ? ML.VBase : MethodDecl->getParent()),
2040 [&](const std::unique_ptr<VPtrInfo> &Info) {
2041 return Info->FullOffsetInMDC == ML.VFPtrOffset;
2042 })
2043 ->get()
2044 ->ObjectWithVPtr;
2045 if (CGF.ShouldEmitVTableTypeCheckedLoad(ObjectWithVPtr)) {
2046 llvm::Value *VFunc = CGF.EmitVTableTypeCheckedLoad(
2047 ObjectWithVPtr, VTable, Ty,
2048 ML.Index *
2050 LangAS::Default) /
2051 8,
2052 Loc);
2053 return CGCallee(GD, VFunc);
2054 }
2055
2056 CGF.EmitTypeMetadataCodeForVCall(ObjectWithVPtr, VTable, Loc);
2057 }
2058
2059 llvm::Value *VFuncPtr =
2060 Builder.CreateConstInBoundsGEP1_64(Ty, VTable, ML.Index, "vfn");
2061 llvm::Value *VFunc =
2062 Builder.CreateAlignedLoad(Ty, VFuncPtr, 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 *>(E);
2070 auto *D = dyn_cast<const CXXDeleteExpr *>(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 =
2081 GlobalDecl GD(Dtor, VectorDeletingDtorsEnabled ? Dtor_VectorDeleting
2082 : Dtor_Deleting);
2083 const CGFunctionInfo *FInfo =
2085 llvm::FunctionType *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo);
2086 CGCallee Callee = CGCallee::forVirtual(CE, GD, This, Ty);
2087
2088 bool IsDeleting = DtorType == Dtor_Deleting;
2089 bool IsArrayDelete = D && D->isArrayForm() && VectorDeletingDtorsEnabled;
2090 bool IsGlobalDelete = D && D->isGlobalDelete() &&
2092 Context.getLangOpts());
2093 llvm::Value *ImplicitParam =
2094 CGF.Builder.getInt32((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(ThisTy))
2105 ThisTy = ATy->getElementType();
2106
2107 This = adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, true);
2108 RValue RV =
2109 CGF.EmitCXXDestructorCall(GD, Callee, This.emitRawPointer(CGF), ThisTy,
2110 ImplicitParam, Context.IntTy, 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(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(getAddrOfVBTable(**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(ThunkName))
2151 return cast<llvm::Function>(GV);
2152
2153 // Create the llvm::Function.
2154 const CGFunctionInfo &FnInfo =
2156 llvm::FunctionType *ThunkTy = CGM.getTypes().GetFunctionType(FnInfo);
2157 llvm::Function *ThunkFn =
2158 llvm::Function::Create(ThunkTy, llvm::Function::ExternalLinkage,
2159 ThunkName.str(), &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(ThunkFn->getName()));
2167
2168 CGM.SetLLVMFunctionAttributes(MD, FnInfo, ThunkFn, /*IsThunk=*/false);
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("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, FunctionArgs);
2189
2190 // Start defining the function.
2191 CGF.StartFunction(GlobalDecl(), FnInfo.getReturnType(), ThunkFn, FnInfo,
2192 FunctionArgs, MD->getLocation(), SourceLocation());
2193
2194 ApplyDebugLocation AL(CGF, MD->getLocation());
2195 setCXXABIThisValue(CGF, 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(getThisAddress(CGF), CGF.DefaultPtrTy, MD->getParent());
2202
2203 llvm::Value *VFuncPtr = CGF.Builder.CreateConstInBoundsGEP1_64(
2204 ThunkPtrTy, VTable, ML.Index, "vfn");
2205 llvm::Value *Callee =
2206 CGF.Builder.CreateAlignedLoad(ThunkPtrTy, VFuncPtr, CGF.getPointerAlign());
2207
2208 CGF.EmitMustTailThunk(MD, getThisValue(CGF), {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, 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(RD, VBT.MangledPath, Out);
2229 StringRef Name = OutName.str();
2230
2231 llvm::ArrayType *VBTableType =
2232 llvm::ArrayType::get(CGM.IntTy, 1 + VBT.ObjectWithVPtr->getNumVBases());
2233
2234 assert(!CGM.getModule().getNamedGlobal(Name) &&
2235 "vbtable with this name already exists: mangling bug?");
2236 CharUnits Alignment =
2238 llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable(
2239 Name, VBTableType, Linkage, 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(VBT.IntroducingObject);
2263 const ASTRecordLayout &DerivedLayout = getContext().getASTRecordLayout(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(CGM.IntTy, -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(VBT.getVBaseWithVPtr());
2284 Offset -= CompleteVBPtrOffset;
2285
2286 unsigned VBIndex = Context.getVBTableIndex(ObjectWithVPtr, VBase);
2287 assert(Offsets[VBIndex] == nullptr && "The same vbindex seen twice?");
2288 Offsets[VBIndex] =
2289 llvm::ConstantInt::getSigned(CGM.IntTy, Offset.getQuantity());
2290 }
2291
2292 assert(Offsets.size() ==
2293 cast<llvm::ArrayType>(GV->getValueType())->getNumElements());
2294 llvm::ArrayType *VBTableType =
2295 llvm::ArrayType::get(CGM.IntTy, Offsets.size());
2296 llvm::Constant *Init = llvm::ConstantArray::get(VBTableType, 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(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 =
2321 VtorDispPtr = VtorDispPtr.withElementType(CGF.Int32Ty);
2322 llvm::Value *VtorDisp = CGF.Builder.CreateLoad(VtorDispPtr, "vtordisp");
2323 V = CGF.Builder.CreateGEP(This.getElementType(), This.emitRawPointer(CGF),
2324 CGF.Builder.CreateNeg(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, Address(V, CGF.Int8Ty, CGF.getPointerAlign()),
2340 TA.Virtual.Microsoft.VBOffsetOffset, &VBPtr);
2341 V = CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, VBPtr, 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(CGF.Int8Ty, V, 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(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, Ret, RA.Virtual.Microsoft.VBPtrOffset,
2372 IntSize * RA.Virtual.Microsoft.VBIndex, &VBPtr);
2373 V = CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, VBPtr, VBaseOffset);
2374 }
2375
2376 if (RA.NonVirtual)
2377 V = CGF.Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, V, 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(Ctx.getTypeSizeInChars(Ctx.getSizeType()),
2401}
2402
2403llvm::Value *MicrosoftCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
2404 Address allocPtr,
2405 CharUnits cookieSize) {
2406 Address numElementsPtr = allocPtr.withElementType(CGF.SizeTy);
2407 return CGF.Builder.CreateLoad(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(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(CGF.SizeTy);
2425 CGF.Builder.CreateStore(numElements, numElementsPtr);
2426
2427 // Finally, compute a pointer to the actual data buffer by skipping
2428 // over the cookie completely.
2429 return CGF.Builder.CreateConstInBoundsByteGEP(newPtr, cookieSize);
2430}
2431
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 CGF.IntTy, DtorStub->getType(), /*isVarArg=*/false);
2441
2442 llvm::FunctionCallee TLRegDtor = CGF.CGM.CreateRuntimeFunction(
2443 TLRegDtorTy, "__tlregdtor", llvm::AttributeList(), /*Local=*/true);
2444 if (llvm::Function *TLRegDtorFn =
2445 dyn_cast<llvm::Function>(TLRegDtor.getCallee()))
2446 TLRegDtorFn->setDoesNotThrow();
2447
2448 CGF.EmitNounwindRuntimeCall(TLRegDtor, 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, D, Dtor, Addr);
2459
2460 // HLSL doesn't support atexit.
2461 if (CGM.getLangOpts().HLSL)
2462 return CGM.AddCXXDtorEntry(Dtor, 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(D, Dtor, Addr);
2468 llvm::FunctionType *ExitFnTy =
2469 llvm::FunctionType::get(CGM.IntTy, dtorStub->getType(), false);
2470 llvm::Constant *ExitFn = CGM.GetAddrOfFunction(FD, ExitFnTy);
2471 CGF.EmitNounwindRuntimeCall(llvm::FunctionCallee(ExitFnTy, ExitFn),
2472 dtorStub);
2473 return;
2474 }
2475 }
2476
2477 // The default behavior is to use atexit.
2478 CGF.registerGlobalDtorWithAtExit(D, Dtor, 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(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(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 CGM.GetGlobalValue(CGM.getMangledName(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(F);
2519 }
2520
2521 if (!NonComdatInits.empty()) {
2522 llvm::FunctionType *FTy =
2523 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
2524 llvm::Function *InitFunc = CGM.CreateGlobalInitOrCleanUpFunction(
2525 FTy, "__tls_init", CGM.getTypes().arrangeNullaryFunction(),
2526 SourceLocation(), /*TLS=*/true);
2527 CodeGenFunction(CGM).GenerateCXXGlobalInitFunc(InitFunc, 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(CGM.getLLVMContext());
2539 llvm::Constant *TlsGuardConstant =
2540 CGM.CreateRuntimeVariable(VTy, "__tls_guard");
2541 llvm::GlobalValue *TlsGuard = cast<llvm::GlobalValue>(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(llvm::Type::getVoidTy(CGM.getLLVMContext()), {},
2553 /*isVarArg=*/false);
2554 return CGM.CreateRuntimeFunction(
2555 FTy, "__dyn_tls_on_demand_init",
2556 llvm::AttributeList::get(CGM.getLLVMContext(),
2557 llvm::AttributeList::FunctionIndex,
2558 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(Address(TlsGuard, CGF.Int8Ty, CharUnits::One()));
2567 llvm::Value *CmpResult =
2568 CGF.Builder.CreateICmpEQ(TlsGuardValue, CGF.Builder.getInt8(0));
2569 CGF.Builder.CreateCondBr(CmpResult, DynInitBB, ContinueBB);
2570}
2571
2573 llvm::GlobalValue *TlsGuard,
2574 llvm::BasicBlock *ContinueBB) {
2575 llvm::FunctionCallee Initializer = getDynTlsOnDemandInitFn(CGF.CGM);
2576 llvm::Function *InitializerFunction =
2577 cast<llvm::Function>(Initializer.getCallee());
2578 llvm::CallInst *CallVal = CGF.Builder.CreateCall(InitializerFunction);
2579 CallVal->setCallingConv(InitializerFunction->getCallingConv());
2580
2581 CGF.Builder.CreateBr(ContinueBB);
2582}
2583
2585 llvm::BasicBlock *DynInitBB =
2586 CGF.createBasicBlock("dyntls.dyn_init", CGF.CurFn);
2587 llvm::BasicBlock *ContinueBB =
2588 CGF.createBasicBlock("dyntls.continue", CGF.CurFn);
2589
2590 llvm::GlobalValue *TlsGuard = getTlsGuardVar(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.
2607
2608 // Emit the variable just like any regular global variable.
2609
2610 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
2611 llvm::Type *RealVarTy = CGF.getTypes().ConvertTypeForMem(VD->getType());
2612
2613 CharUnits Alignment = CGF.getContext().getDeclAlign(VD);
2614 Address Addr(V, RealVarTy, Alignment);
2615
2616 LValue LV = VD->getType()->isReferenceType()
2618 AlignmentSource::Decl)
2619 : CGF.MakeAddrLValue(Addr, LValType, AlignmentSource::Decl);
2620 return LV;
2621}
2622
2624 StringRef VarName("_Init_thread_epoch");
2625 CharUnits Align = CGM.getIntAlign();
2626 if (auto *GV = CGM.getModule().getNamedGlobal(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(llvm::Type::getVoidTy(CGM.getLLVMContext()),
2640 CGM.DefaultPtrTy, /*isVarArg=*/false);
2641 return CGM.CreateRuntimeFunction(
2642 FTy, "_Init_thread_header",
2643 llvm::AttributeList::get(CGM.getLLVMContext(),
2644 llvm::AttributeList::FunctionIndex,
2645 llvm::Attribute::NoUnwind),
2646 /*Local=*/true);
2647}
2648
2649static llvm::FunctionCallee getInitThreadFooterFn(CodeGenModule &CGM) {
2650 llvm::FunctionType *FTy =
2651 llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()),
2652 CGM.DefaultPtrTy, /*isVarArg=*/false);
2653 return CGM.CreateRuntimeFunction(
2654 FTy, "_Init_thread_footer",
2655 llvm::AttributeList::get(CGM.getLLVMContext(),
2656 llvm::AttributeList::FunctionIndex,
2657 llvm::Attribute::NoUnwind),
2658 /*Local=*/true);
2659}
2660
2661static llvm::FunctionCallee getInitThreadAbortFn(CodeGenModule &CGM) {
2662 llvm::FunctionType *FTy =
2663 llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()),
2664 CGM.DefaultPtrTy, /*isVarArg=*/false);
2665 return CGM.CreateRuntimeFunction(
2666 FTy, "_Init_thread_abort",
2667 llvm::AttributeList::get(CGM.getLLVMContext(),
2668 llvm::AttributeList::FunctionIndex,
2669 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(Guard);
2685 llvm::ConstantInt *Mask =
2686 llvm::ConstantInt::getSigned(CGF.IntTy, ~(1ULL << GuardNum));
2687 Builder.CreateStore(Builder.CreateAnd(LI, Mask), 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.
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(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(GuardTy, 0);
2726 CharUnits GuardAlign = CharUnits::fromQuantity(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(&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, "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(&D, GuardNum,
2764 Out);
2765 else
2766 getMangleContext().mangleStaticGuardVariable(&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(GuardVar->getName()));
2780 if (D.getTLSKind())
2781 CGM.setTLSMode(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(GuardTy, 1ULL << GuardNum);
2800 llvm::LoadInst *LI = Builder.CreateLoad(GuardAddr);
2801 llvm::Value *NeedsInit =
2802 Builder.CreateICmpEQ(Builder.CreateAnd(LI, Bit), Zero);
2803 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
2804 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
2805 CGF.EmitCXXGuardedInitBranch(NeedsInit, InitBlock, EndBlock,
2806 CodeGenFunction::GuardKind::VariableGuard, &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(InitBlock);
2811 Builder.CreateStore(Builder.CreateOr(LI, Bit), GuardAddr);
2812 CGF.EHStack.pushCleanup<ResetGuardBit>(EHCleanup, GuardAddr, GuardNum);
2813 CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit);
2814 CGF.PopCleanupBlock();
2815 Builder.CreateBr(EndBlock);
2816
2817 // Continue.
2818 CGF.EmitBlock(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(GuardAddr);
2834 FirstGuardLoad->setOrdering(llvm::AtomicOrdering::Unordered);
2835 llvm::LoadInst *InitThreadEpoch =
2836 Builder.CreateLoad(getInitThreadEpochPtr(CGM));
2837 llvm::Value *IsUninitialized =
2838 Builder.CreateICmpSGT(FirstGuardLoad, InitThreadEpoch);
2839 llvm::BasicBlock *AttemptInitBlock = CGF.createBasicBlock("init.attempt");
2840 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
2841 CGF.EmitCXXGuardedInitBranch(IsUninitialized, AttemptInitBlock, EndBlock,
2842 CodeGenFunction::GuardKind::VariableGuard, &D);
2843
2844 // This BasicBlock attempts to determine whether or not this thread is
2845 // responsible for doing the initialization.
2846 CGF.EmitBlock(AttemptInitBlock);
2848 GuardAddr.getPointer());
2849 llvm::LoadInst *SecondGuardLoad = Builder.CreateLoad(GuardAddr);
2850 SecondGuardLoad->setOrdering(llvm::AtomicOrdering::Unordered);
2851 llvm::Value *ShouldDoInit =
2852 Builder.CreateICmpEQ(SecondGuardLoad, getAllOnesInt());
2853 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
2854 Builder.CreateCondBr(ShouldDoInit, InitBlock, EndBlock);
2855
2856 // Ok, we ended up getting selected as the initializing thread.
2857 CGF.EmitBlock(InitBlock);
2858 CGF.EHStack.pushCleanup<CallInitThreadAbort>(EHCleanup, GuardAddr);
2859 CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit);
2860 CGF.PopCleanupBlock();
2862 GuardAddr.getPointer());
2863 Builder.CreateBr(EndBlock);
2864
2865 CGF.EmitBlock(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(CGM.VoidPtrTy); // FunctionPointerOrVirtualThunk
2891 else
2892 fields.push_back(CGM.IntTy); // FieldOffset
2893
2895 Inheritance))
2896 fields.push_back(CGM.IntTy);
2897 if (inheritanceModelHasVBPtrOffsetField(Inheritance))
2898 fields.push_back(CGM.IntTy);
2900 fields.push_back(CGM.IntTy); // VirtualBaseAdjustmentOffset
2901
2902 if (fields.size() == 1)
2903 return fields[0];
2904 return llvm::StructType::get(CGM.getLLVMContext(), 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(llvm::Constant::getNullValue(CGM.VoidPtrTy));
2916 } else {
2917 if (RD->nullFieldOffsetIsZero())
2918 fields.push_back(getZeroInt()); // FieldOffset
2919 else
2920 fields.push_back(getAllOnesInt()); // FieldOffset
2921 }
2922
2924 Inheritance))
2925 fields.push_back(getZeroInt());
2926 if (inheritanceModelHasVBPtrOffsetField(Inheritance))
2927 fields.push_back(getZeroInt());
2929 fields.push_back(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(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(FirstField);
2958
2959 if (inheritanceModelHasNVOffsetField(IsMemberFunction, Inheritance))
2960 fields.push_back(llvm::ConstantInt::getSigned(
2961 CGM.IntTy, NonVirtualBaseAdjustment.getQuantity()));
2962
2963 if (inheritanceModelHasVBPtrOffsetField(Inheritance)) {
2964 CharUnits Offs = CharUnits::Zero();
2965 if (VBTableIndex)
2966 Offs = getContext().getASTRecordLayout(RD).getVBPtrOffset();
2967 fields.push_back(llvm::ConstantInt::get(CGM.IntTy, Offs.getQuantity()));
2968 }
2969
2970 // The rest of the fields are adjusted by conversions to a more derived class.
2972 fields.push_back(llvm::ConstantInt::get(CGM.IntTy, VBTableIndex));
2973
2974 return llvm::ConstantStruct::getAnon(fields);
2975}
2976
2977llvm::Constant *
2978MicrosoftCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
2979 CharUnits offset) {
2980 return EmitMemberDataPointer(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(CGM.IntTy, offset.getQuantity());
2990 return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/false, RD,
2991 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(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>(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.
3013 CharUnits FieldOffset = Ctx.toCharUnitsFromBits(Ctx.getFieldOffset(MPD));
3014 const FieldDecl *FD = dyn_cast<FieldDecl>(MPD);
3015 if (!FD)
3016 FD = cast<FieldDecl>(*cast<IndirectFieldDecl>(MPD)->chain_begin());
3017 const CXXRecordDecl *RD = cast<CXXRecordDecl>(FD->getParent());
3018 RD = RD->getMostRecentDecl();
3019 C = EmitMemberDataPointer(RD, FieldOffset);
3020 }
3021
3022 if (!MemberPointerPath.empty()) {
3023 const CXXRecordDecl *SrcRD = cast<CXXRecordDecl>(MPD->getDeclContext());
3024 const MemberPointerType *SrcTy =
3026 /*Qualifier=*/std::nullopt, 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(&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, DerivedToBasePath.begin(),
3053 DerivedToBasePath.end(), 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(FPT)) {
3073 // The function has a computable LLVM signature; use the correct type.
3074 Ty = Types.GetFunctionType(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(MD, Ty);
3081 } else {
3082 auto &VTableContext = CGM.getMicrosoftVTableContext();
3083 MethodVFTableLocation ML = VTableContext.getMethodVFTableLocation(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(RD, 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();
3130 Inheritance))
3131 return Builder.CreateICmp(Eq, L, R);
3132
3133 // Compare the first field.
3134 llvm::Value *L0 = Builder.CreateExtractValue(L, 0, "lhs.0");
3135 llvm::Value *R0 = Builder.CreateExtractValue(R, 0, "rhs.0");
3136 llvm::Value *Cmp0 = Builder.CreateICmp(Eq, L0, R0, "memptr.cmp.first");
3137
3138 // Compare everything other than the first field.
3139 llvm::Value *Res = nullptr;
3140 llvm::StructType *LType = cast<llvm::StructType>(L->getType());
3141 for (unsigned I = 1, E = LType->getNumElements(); I != E; ++I) {
3142 llvm::Value *LF = Builder.CreateExtractValue(L, I);
3143 llvm::Value *RF = Builder.CreateExtractValue(R, I);
3144 llvm::Value *Cmp = Builder.CreateICmp(Eq, LF, RF, "memptr.cmp.rest");
3145 if (Res)
3146 Res = Builder.CreateBinOp(And, Res, 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(L0->getType());
3155 llvm::Value *IsZero = Builder.CreateICmp(Eq, L0, Zero, "memptr.cmp.iszero");
3156 Res = Builder.CreateBinOp(Or, Res, 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(And, Res, Cmp0, "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(llvm::Constant::getNullValue(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(MemPtr, 0);
3179 llvm::Value *Res = Builder.CreateICmpNE(FirstField, fields[0], "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(MemPtr, I);
3189 llvm::Value *Next = Builder.CreateICmpNE(Field, fields[I], "memptr.cmp");
3190 Res = Builder.CreateOr(Res, Next, "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(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);
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(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 CGM.Int8Ty, This.emitRawPointer(CGF), VBPtrOffset, "vbptr");
3236 if (VBPtrOut)
3237 *VBPtrOut = VBPtr;
3238
3239 CharUnits VBPtrAlign;
3240 if (auto CI = dyn_cast<llvm::ConstantInt>(VBPtrOffset)) {
3241 VBPtrAlign = This.getAlignment().alignmentAtOffset(
3242 CharUnits::fromQuantity(CI->getSExtValue()));
3243 } else {
3244 VBPtrAlign = CGF.getPointerAlign();
3245 }
3246
3247 llvm::Value *VBTable =
3248 Builder.CreateAlignedLoad(CGM.DefaultPtrTy, VBPtr, VBPtrAlign, "vbtable");
3249
3250 // Translate from byte offset to table index. It improves analyzability.
3251 llvm::Value *VBTableIndex = Builder.CreateAShr(
3252 VBTableOffset, llvm::ConstantInt::get(VBTableOffset->getType(), 2),
3253 "vbtindex", /*isExact=*/true);
3254
3255 // Load an i32 offset from the vb-table.
3256 llvm::Value *VBaseOffs =
3257 Builder.CreateInBoundsGEP(CGM.Int32Ty, VBTable, VBTableIndex);
3258 return Builder.CreateAlignedLoad(CGM.Int32Ty, VBaseOffs,
3259 CharUnits::fromQuantity(4), "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(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("memptr.vadjust");
3280 SkipAdjustBB = CGF.createBasicBlock("memptr.skip_vadjust");
3281 llvm::Value *IsVirtual =
3282 Builder.CreateICmpNE(VBTableOffset, getZeroInt(),
3283 "memptr.is_vbase");
3284 Builder.CreateCondBr(IsVirtual, VBaseAdjustBB, SkipAdjustBB);
3285 CGF.EmitBlock(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(E->getExprLoc(), diag::err_member_ptr_requires_complete_type)
3295 << RD << E->getSourceRange();
3296 } else if (RD->getNumVBases())
3297 offs = getContext().getASTRecordLayout(RD).getVBPtrOffset();
3298 VBPtrOffset = llvm::ConstantInt::get(CGM.IntTy, offs.getQuantity());
3299 }
3300 llvm::Value *VBPtr = nullptr;
3301 llvm::Value *VBaseOffs =
3302 GetVBaseOffsetFromVBPtr(CGF, Base, VBPtrOffset, VBTableOffset, &VBPtr);
3303 llvm::Value *AdjustedBase =
3304 Builder.CreateInBoundsGEP(CGM.Int8Ty, VBPtr, VBaseOffs);
3305
3306 // Merge control flow with the case where we didn't have to adjust.
3307 if (VBaseAdjustBB) {
3308 Builder.CreateBr(SkipAdjustBB);
3309 CGF.EmitBlock(SkipAdjustBB);
3310 llvm::PHINode *Phi = Builder.CreatePHI(CGM.Int8PtrTy, 2, "memptr.base");
3311 Phi->addIncoming(Base.emitRawPointer(CGF), OriginalBB);
3312 Phi->addIncoming(AdjustedBase, 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(MemPtr, I++);
3335 if (inheritanceModelHasVBPtrOffsetField(Inheritance))
3336 VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++);
3338 VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++);
3339 }
3340
3341 llvm::Value *Addr;
3342 if (VirtualBaseAdjustmentOffset) {
3343 Addr = AdjustVirtualBase(CGF, E, RD, Base, VirtualBaseAdjustmentOffset,
3344 VBPtrOffset);
3345 } else {
3346 Addr = Base.emitRawPointer(CGF);
3347 }
3348
3349 // Apply the offset.
3350 return Builder.CreateGEP(CGF.Int8Ty, Addr, FieldOffset, "memptr.offset",
3351 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>(Src))
3365 return EmitMemberPointerConversion(E, cast<llvm::Constant>(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, Src, SrcTy);
3389 llvm::Constant *DstNull = EmitNullMemberPointer(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(IsNotNull, Src, DstNull);
3398 }
3399
3400 llvm::BasicBlock *OriginalBB = Builder.GetInsertBlock();
3401 llvm::BasicBlock *ConvertBB = CGF.createBasicBlock("memptr.convert");
3402 llvm::BasicBlock *ContinueBB = CGF.createBasicBlock("memptr.converted");
3403 Builder.CreateCondBr(IsNotNull, ConvertBB, ContinueBB);
3404 CGF.EmitBlock(ConvertBB);
3405
3406 llvm::Value *Dst = EmitNonNullMemberPointerConversion(
3407 SrcTy, DstTy, E->getCastKind(), E->path_begin(), E->path_end(), Src,
3408 Builder);
3409
3410 Builder.CreateBr(ContinueBB);
3411
3412 // In the continuation, choose between DstNull and Dst.
3413 CGF.EmitBlock(ContinueBB);
3414 llvm::PHINode *Phi = Builder.CreatePHI(DstNull->getType(), 2, "memptr.converted");
3415 Phi->addIncoming(DstNull, OriginalBB);
3416 Phi->addIncoming(Dst, ConvertBB);
3417 return Phi;
3418}
3419
3420llvm::Value *MicrosoftCXXABI::EmitNonNullMemberPointerConversion(
3421 const MemberPointerType *SrcTy, const MemberPointerType *DstTy, CastKind CK,
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>(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(IsFunc, SrcInheritance)) {
3438 // We need to extract values.
3439 unsigned I = 0;
3440 FirstField = Builder.CreateExtractValue(Src, I++);
3441 if (inheritanceModelHasNVOffsetField(IsFunc, SrcInheritance))
3442 NonVirtualBaseAdjustment = Builder.CreateExtractValue(Src, I++);
3443 if (inheritanceModelHasVBPtrOffsetField(SrcInheritance))
3444 VBPtrOffset = Builder.CreateExtractValue(Src, I++);
3445 if (inheritanceModelHasVBTableOffsetField(SrcInheritance))
3446 VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(Src, 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(VirtualBaseAdjustmentOffset, getZeroInt());
3464 if (SrcInheritance == MSInheritanceModel::Virtual) {
3465 if (int64_t SrcOffsetToFirstVBase =
3466 getContext().getOffsetOfBaseWithVBPtr(SrcRD).getQuantity()) {
3467 llvm::Value *UndoSrcAdjustment = Builder.CreateSelect(
3468 SrcVBIndexEqZero,
3469 llvm::ConstantInt::get(CGM.IntTy, SrcOffsetToFirstVBase),
3470 getZeroInt());
3471 NVAdjustField = Builder.CreateNSWAdd(NVAdjustField, 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 CGM.IntTy,
3484 CGM.computeNonVirtualBaseClassOffset(DerivedClass, PathBegin, PathEnd)
3485 .getQuantity());
3486
3487 llvm::Value *NVDisp;
3488 if (IsDerivedToBase)
3489 NVDisp = Builder.CreateNSWSub(NVAdjustField, BaseClassOffset, "adj");
3490 else
3491 NVDisp = Builder.CreateNSWAdd(NVAdjustField, BaseClassOffset, "adj");
3492
3493 NVAdjustField = Builder.CreateSelect(SrcVBIndexEqZero, NVDisp, 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(DstInheritance) &&
3499 inheritanceModelHasVBTableOffsetField(SrcInheritance)) {
3500 if (llvm::GlobalVariable *VDispMap =
3501 getAddrOfVirtualDisplacementMap(SrcRD, DstRD)) {
3502 llvm::Value *VBIndex = Builder.CreateExactUDiv(
3503 VirtualBaseAdjustmentOffset, llvm::ConstantInt::get(CGM.IntTy, 4));
3504 if (IsConstant) {
3505 llvm::Constant *Mapping = VDispMap->getInitializer();
3506 VirtualBaseAdjustmentOffset =
3507 Mapping->getAggregateElement(cast<llvm::Constant>(VBIndex));
3508 } else {
3509 llvm::Value *Idxs[] = {getZeroInt(), VBIndex};
3510 VirtualBaseAdjustmentOffset = Builder.CreateAlignedLoad(
3511 CGM.IntTy, Builder.CreateInBoundsGEP(VDispMap->getValueType(),
3512 VDispMap, Idxs),
3514 }
3515
3516 DstVBIndexEqZero =
3517 Builder.CreateICmpEQ(VirtualBaseAdjustmentOffset, 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(DstInheritance)) {
3524 llvm::Value *DstVBPtrOffset = llvm::ConstantInt::getSigned(
3525 CGM.IntTy,
3526 getContext().getASTRecordLayout(DstRD).getVBPtrOffset().getQuantity());
3527 VBPtrOffset =
3528 Builder.CreateSelect(DstVBIndexEqZero, getZeroInt(), 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(DstRD).getQuantity()) {
3537 llvm::Value *DoDstAdjustment = Builder.CreateSelect(
3538 DstVBIndexEqZero,
3539 llvm::ConstantInt::get(CGM.IntTy, DstOffsetToFirstVBase),
3540 getZeroInt());
3541 NVAdjustField = Builder.CreateNSWSub(NVAdjustField, DoDstAdjustment);
3542 }
3543 }
3544
3545 // Recompose dst from the null struct and the adjusted fields from src.
3546 llvm::Value *Dst;
3547 if (inheritanceModelHasOnlyOneField(IsFunc, DstInheritance)) {
3548 Dst = FirstField;
3549 } else {
3550 Dst = llvm::PoisonValue::get(ConvertMemberPointerType(DstTy));
3551 unsigned Idx = 0;
3552 Dst = Builder.CreateInsertValue(Dst, FirstField, Idx++);
3553 if (inheritanceModelHasNVOffsetField(IsFunc, DstInheritance))
3554 Dst = Builder.CreateInsertValue(Dst, NonVirtualBaseAdjustment, Idx++);
3555 if (inheritanceModelHasVBPtrOffsetField(DstInheritance))
3556 Dst = Builder.CreateInsertValue(Dst, VBPtrOffset, Idx++);
3557 if (inheritanceModelHasVBTableOffsetField(DstInheritance))
3558 Dst = Builder.CreateInsertValue(Dst, VirtualBaseAdjustmentOffset, 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, E->path_begin(),
3573 E->path_end(), Src);
3574}
3575
3576llvm::Constant *MicrosoftCXXABI::EmitMemberPointerConversion(
3577 const MemberPointerType *SrcTy, const MemberPointerType *DstTy, CastKind CK,
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(SrcTy, Src))
3586 return EmitNullMemberPointer(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>(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(MemPtr, I++);
3623 if (inheritanceModelHasNVOffsetField(MPT, Inheritance))
3624 NonVirtualBaseAdjustment = Builder.CreateExtractValue(MemPtr, I++);
3625 if (inheritanceModelHasVBPtrOffsetField(Inheritance))
3626 VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++);
3628 VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++);
3629 }
3630
3631 if (VirtualBaseAdjustmentOffset) {
3632 ThisPtrForCall = AdjustVirtualBase(CGF, E, RD, This,
3633 VirtualBaseAdjustmentOffset, VBPtrOffset);
3634 } else {
3635 ThisPtrForCall = This.emitRawPointer(CGF);
3636 }
3637
3638 if (NonVirtualBaseAdjustment)
3639 ThisPtrForCall = Builder.CreateInBoundsGEP(CGF.Int8Ty, ThisPtrForCall,
3640 NonVirtualBaseAdjustment);
3641
3642 CGCallee Callee(FPT, FunctionPointer);
3643 return Callee;
3644}
3645
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(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(Parent->RD).getBaseClassOffset(RD).getQuantity();
3739 }
3740 }
3741 NumBases = 0;
3742 MSRTTIClass *Child = getFirstChild();
3743 for (const CXXBaseSpecifier &Base : RD->bases()) {
3744 NumBases += Child->initialize(this, &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(CGM.getContext().getCanonicalTagType(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.
3804 const CXXRecordDecl *RD) {
3805 Classes.push_back(MSRTTIClass(RD));
3806 for (const CXXBaseSpecifier &Base : RD->bases())
3807 serializeClassHierarchy(Classes, Base.getType()->getAsCXXRecordDecl());
3808}
3809
3810/// Find ambiguity among base classes.
3811static void
3816 for (MSRTTIClass *Class = &Classes.front(); Class <= &Classes.back();) {
3817 if ((Class->Flags & MSRTTIClass::IsVirtual) &&
3818 !VirtualBases.insert(Class->RD).second) {
3819 Class = MSRTTIClass::getNextChild(Class);
3820 continue;
3821 }
3822 if (!UniqueBases.insert(Class->RD).second)
3823 AmbiguousBases.insert(Class->RD);
3824 Class++;
3825 }
3826 if (AmbiguousBases.empty())
3827 return;
3828 for (MSRTTIClass &Class : Classes)
3829 if (AmbiguousBases.count(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(RD, Out);
3838 }
3839
3840 // Check to see if we've already declared this ClassHierarchyDescriptor.
3841 if (auto CHD = Module.getNamedGlobal(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(CHD->getName()));
3868
3869 auto *Bases = getBaseClassArray(Classes);
3870
3871 // Initialize the base class ClassHierarchyDescriptor.
3872 llvm::Constant *Fields[] = {
3873 llvm::ConstantInt::get(CGM.IntTy, 0), // reserved by the runtime
3874 llvm::ConstantInt::get(CGM.IntTy, Flags),
3875 llvm::ConstantInt::get(CGM.IntTy, Classes.size()),
3876 ABI.getImageRelativeConstant(Bases)
3877 };
3878 CHD->setInitializer(llvm::ConstantStruct::get(Type, 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(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(CGM.DefaultPtrTy);
3896 auto *ArrType = llvm::ArrayType::get(PtrType, 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(BCA->getName()));
3903
3904 // Initialize the BaseClassArray.
3905 SmallVector<llvm::Constant *, 8> BaseClassArrayData;
3906 for (MSRTTIClass &Class : Classes)
3907 BaseClassArrayData.push_back(
3908 ABI.getImageRelativeConstant(getBaseClassDescriptor(Class)));
3909 BaseClassArrayData.push_back(llvm::Constant::getNullValue(PtrType));
3910 BCA->setInitializer(llvm::ConstantArray::get(ArrType, 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(RD, Class.VirtualRoot) * 4;
3923 VBPtrOffset = Context.getASTRecordLayout(RD).getVBPtrOffset().getQuantity();
3924 }
3925
3926 SmallString<256> MangledName;
3927 {
3928 llvm::raw_svector_ostream Out(MangledName);
3929 ABI.getMangleContext().mangleCXXRTTIBaseClassDescriptor(
3930 Class.RD, Class.OffsetInVBase, VBPtrOffset, OffsetInVBTable,
3931 Class.Flags, Out);
3932 }
3933
3934 // Check to see if we've already declared this object.
3935 if (auto BCD = Module.getNamedGlobal(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(BCD->getName()));
3945
3946 // Initialize the BaseClassDescriptor.
3947 llvm::Constant *Fields[] = {
3948 ABI.getImageRelativeConstant(
3949 ABI.getAddrOfRTTIDescriptor(Context.getCanonicalTagType(Class.RD))),
3950 llvm::ConstantInt::get(CGM.IntTy, Class.NumBases),
3951 llvm::ConstantInt::get(CGM.IntTy, Class.OffsetInVBase),
3952 llvm::ConstantInt::getSigned(CGM.IntTy, VBPtrOffset),
3953 llvm::ConstantInt::get(CGM.IntTy, OffsetInVBTable),
3954 llvm::ConstantInt::get(CGM.IntTy, Class.Flags),
3955 ABI.getImageRelativeConstant(
3956 MSRTTIBuilder(ABI, Class.RD).getClassHierarchyDescriptor()),
3957 };
3958 BCD->setInitializer(llvm::ConstantStruct::get(Type, 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(RD, Info.MangledPath, Out);
3968 }
3969
3970 // Check to see if we've already computed this complete object locator.
3971 if (auto COL = Module.getNamedGlobal(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(RD)
3981 .find(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(CGM.IntTy, ABI.isRTTIImageRelative()),
3993 llvm::ConstantInt::get(CGM.IntTy, OffsetToTop),
3994 llvm::ConstantInt::get(CGM.IntTy, VFPtrOffset),
3995 ABI.getImageRelativeConstant(
3997 ABI.getImageRelativeConstant(getClassHierarchyDescriptor()),
3998 ABI.getImageRelativeConstant(COL),
3999 };
4000 llvm::ArrayRef<llvm::Constant *> FieldsRef(Fields);
4001 if (!ABI.isRTTIImageRelative())
4002 FieldsRef = FieldsRef.drop_back();
4003 COL->setInitializer(llvm::ConstantStruct::get(Type, FieldsRef));
4004 if (COL->isWeakForLinker())
4005 COL->setComdat(CGM.getModule().getOrInsertComdat(COL->getName()));
4006 return COL;
4007}
4008
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(PointeeType.getUnqualifiedType(),
4033 MPTy->getQualifier(),
4034 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(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(getContext(), 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{getAddrOfRTTIDescriptor(Type)->stripPointerCasts(),
4067 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(Type, Out);
4079 }
4080
4081 // Check to see if we've already declared this TypeDescriptor.
4082 if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(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(Type, Out);
4093 }
4094
4095 // Declare and initialize the TypeDescriptor.
4096 llvm::Constant *Fields[] = {
4097 getTypeInfoVTable(CGM), // VFPtr
4098 llvm::ConstantPointerNull::get(CGM.Int8PtrTy), // Runtime data
4099 llvm::ConstantDataArray::getString(CGM.getLLVMContext(), TypeInfoString)};
4100 llvm::StructType *TypeDescriptorType =
4101 getTypeDescriptorType(TypeInfoString);
4102 auto *Var = new llvm::GlobalVariable(
4103 CGM.getModule(), TypeDescriptorType, /*isConstant=*/false,
4104 getLinkageForRTTI(Type),
4105 llvm::ConstantStruct::get(TypeDescriptorType, Fields),
4106 MangledName);
4107 if (Var->isWeakForLinker())
4108 Var->setComdat(CGM.getModule().getOrInsertComdat(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>(GD.getDecl())) {
4121 // There are no constructor variants, always emit the complete destructor.
4122 llvm::Function *Fn =
4124 CGM.maybeSetTrivialComdat(*ctor, *Fn);
4125 return;
4126 }
4127
4128 auto *dtor = cast<CXXDestructorDecl>(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(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(dtor))
4142 return;
4143
4144 if (GD.getDtorType() == Dtor_VectorDeleting &&
4145 !CGM.classNeedsVectorDestructor(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(GD, ScalarDtorGD);
4153 return;
4154 }
4155
4156 llvm::Function *Fn = CGM.codegenCXXStructor(GD);
4157 if (Fn->isWeakForLinker())
4158 Fn->setComdat(CGM.getModule().getOrInsertComdat(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(GlobalDecl(CD, CT), Out);
4170
4171 // If the thunk has been generated previously, just return it.
4172 if (llvm::GlobalValue *GV = CGM.getModule().getNamedValue(ThunkName))
4173 return cast<llvm::Function>(GV);
4174
4175 // Create the llvm::Function.
4176 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeMSCtorClosure(CD, CT);
4177 llvm::FunctionType *ThunkTy = CGM.getTypes().GetFunctionType(FnInfo);
4178 const CXXRecordDecl *RD = CD->getParent();
4179 CanQualType RecordTy = getContext().getCanonicalTagType(RD);
4180 llvm::Function *ThunkFn = llvm::Function::Create(
4181 ThunkTy, getLinkageForRTTI(RecordTy), ThunkName.str(), &CGM.getModule());
4182 ThunkFn->setCallingConv(static_cast<llvm::CallingConv::ID>(
4184 if (ThunkFn->isWeakForLinker())
4185 ThunkFn->setComdat(CGM.getModule().getOrInsertComdat(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, 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 getContext(), /*DC=*/nullptr, SourceLocation(),
4202 &getContext().Idents.get("src"),
4203 getContext().getLValueReferenceType(RecordTy,
4204 /*SpelledAsLValue=*/true),
4205 ImplicitParamKind::Other);
4206 if (IsCopy)
4207 FunctionArgs.push_back(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(getContext(), /*DC=*/nullptr, SourceLocation(),
4214 &getContext().Idents.get("is_most_derived"),
4215 getContext().IntTy, 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(IsMostDerived);
4219
4220 // Start defining the function.
4221 auto NL = ApplyDebugLocation::CreateEmpty(CGF);
4222 CGF.StartFunction(GlobalDecl(), FnInfo.getReturnType(), ThunkFn, FnInfo,
4223 FunctionArgs, CD->getLocation(), SourceLocation());
4224 // Create a scope with an artificial location for the body of this function.
4226 setCXXABIThisValue(CGF, loadIncomingCXXThis(CGF));
4227 llvm::Value *This = getThisValue(CGF);
4228
4229 llvm::Value *SrcVal =
4230 IsCopy ? CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(SrcParam), "src")
4231 : nullptr;
4232
4233 CallArgList Args;
4234
4235 // Push the this ptr.
4236 Args.add(RValue::get(This), CD->getThisType());
4237
4238 // Push the src ptr.
4239 if (SrcVal)
4240 Args.add(RValue::get(SrcVal), 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(IsCopy ? 1 : 0))
4246 ArgVec.push_back(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, FPT, ArrayRef(ArgVec), CD, IsCopy ? 1 : 0);
4253
4254 // Insert any ABI-specific implicit constructor arguments.
4255 AddedStructorArgCounts ExtraArgs =
4256 addImplicitConstructorArgs(CGF, CD, Ctor_Complete,
4257 /*ForVirtualBase=*/false,
4258 /*Delegating=*/false, Args);
4259 // Call the destructor with our arguments.
4260 llvm::Constant *CalleePtr =
4261 CGM.getAddrOfCXXStructor(GlobalDecl(CD, Ctor_Complete));
4262 CGCallee Callee =
4263 CGCallee::forDirect(CalleePtr, 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, CD, Ctor_Complete, ExtraArgs.Prefix, ExtraArgs.Suffix,
4269 /*ABIInfoFD=*/nullptr);
4270 CGF.EmitCall(CalleeInfo, Callee, 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(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;
4291 if (CD)
4292 if (!hasDefaultCXXMethodCC(getContext(), CD) || CD->getNumParams() != 1)
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(MangledName))
4303 return getThrowInfoRelativeConstant(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(getAddrOfRTTIDescriptor(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, Ctor_CopyingClosure);
4315 else
4316 CopyCtor = CGM.getAddrOfCXXStructor(GlobalDecl(CD, Ctor_Complete));
4317 CopyCtor = getThrowInfoRelativeConstant(CopyCtor);
4318 } else {
4319 CopyCtor =
4320 llvm::Constant::getNullValue(getThrowInfoRelativeType(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("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(CGM.IntTy, Flags), // Flags
4347 TD, // TypeDescriptor
4348 llvm::ConstantInt::get(CGM.IntTy, NVOffset), // NonVirtualAdjustment
4349 llvm::ConstantInt::getSigned(CGM.IntTy, VBPtrOffset), // OffsetToVBPtr
4350 llvm::ConstantInt::get(CGM.IntTy, VBIndex), // VBTableIndex
4351 llvm::ConstantInt::get(CGM.IntTy, Size), // Size
4352 CopyCtor // CopyCtor
4353 };
4354 llvm::StructType *CTType = getCatchableTypeType();
4355 auto *GV = new llvm::GlobalVariable(
4356 CGM.getModule(), CTType, /*isConstant=*/true, getLinkageForRTTI(T),
4357 llvm::ConstantStruct::get(CTType, Fields), MangledName);
4358 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
4359 GV->setSection(".xdata");
4360 if (GV->isWeakForLinker())
4361 GV->setComdat(CGM.getModule().getOrInsertComdat(GV->getName()));
4362 return getThrowInfoRelativeConstant(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(MostDerivedClass);
4398 MicrosoftVTableContext &VTableContext = CGM.getMicrosoftVTableContext();
4399 SmallVector<MSRTTIClass, 8> Classes;
4400 serializeClassHierarchy(Classes, 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(MostDerivedClass, 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(Class.RD);
4420 if (IsPointer)
4421 RTTITy = Context.getPointerType(RTTITy);
4422 CatchableTypes.insert(getCatchableType(RTTITy, Class.OffsetInVBase,
4423 VBPtrOffset, 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(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(getCatchableType(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(getCatchableType(getContext().VoidPtrTy));
4456
4457 uint32_t NumEntries = CatchableTypes.size();
4458 llvm::Type *CTType = getThrowInfoRelativeType(CGM.DefaultPtrTy);
4459 llvm::ArrayType *AT = llvm::ArrayType::get(CTType, NumEntries);
4460 llvm::StructType *CTAType = getCatchableTypeArrayType(NumEntries);
4461 llvm::Constant *Fields[] = {
4462 llvm::ConstantInt::get(CGM.IntTy, NumEntries), // NumEntries
4463 llvm::ConstantArray::get(
4464 AT, 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(T),
4474 llvm::ConstantStruct::get(CTAType, Fields), MangledName);
4475 CTA->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
4476 CTA->setSection(".xdata");
4477 if (CTA->isWeakForLinker())
4478 CTA->setComdat(CGM.getModule().getOrInsertComdat(CTA->getName()));
4479 return CTA;
4480}
4481
4482llvm::GlobalVariable *MicrosoftCXXABI::getThrowInfo(QualType T) {
4483 bool IsConst, IsVolatile, IsUnaligned;
4484 T = decomposeTypeForEH(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>(CTA->getInitializer()->getAggregateElement(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(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(getThrowInfoRelativeType(CGM.Int8PtrTy));
4524 if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
4525 if (CXXDestructorDecl *DtorD = RD->getDestructor())
4526 if (!DtorD->isTrivial())
4527 CleanupFn = getThrowInfoRelativeConstant(
4528 CGM.getAddrOfCXXStructor(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(getThrowInfoRelativeType(CGM.Int8PtrTy));
4532 llvm::Constant *PointerToCatchableTypes = getThrowInfoRelativeConstant(CTA);
4533 llvm::StructType *TIType = getThrowInfoType();
4534 llvm::Constant *Fields[] = {
4535 llvm::ConstantInt::get(CGM.IntTy, 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(T),
4542 llvm::ConstantStruct::get(TIType, Fields), MangledName.str());
4543 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
4544 GV->setSection(".xdata");
4545 if (GV->isWeakForLinker())
4546 GV->setComdat(CGM.getModule().getOrInsertComdat(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(ThrowType);
4557 CGF.EmitAnyExprToMem(SubExpr, AI, ThrowType.getQualifiers(),
4558 /*IsInit=*/true);
4559
4560 // The so-called ThrowInfo is used to describe how the exception object may be
4561 // caught.
4562 llvm::GlobalVariable *TI = getThrowInfo(ThrowType);
4563
4564 // Call into the runtime to throw the exception.
4565 llvm::Value *Args[] = {AI.emitRawPointer(CGF), TI};
4567}
4568
4569std::pair<llvm::Value *, const CXXRecordDecl *>
4570MicrosoftCXXABI::LoadVTablePtr(CodeGenFunction &CGF, Address This,
4571 const CXXRecordDecl *RD) {
4573 std::tie(This, std::ignore, RD) = performBaseAdjustment(CGF, This, T);
4574 return {CGF.GetVTablePtr(This, CGM.Int8PtrTy, 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;
4592 return false;
4593 if (RD->hasNonTrivialDestructor())
4594 return false;
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(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}
#define V(N, I)
static Address emitDynamicCastToVoid(CIRGenFunction &cgf, mlir::Location loc, QualType srcRecordTy, Address src)
static mlir::Value emitExactDynamicCast(CIRGenItaniumCXXABI &abi, CIRGenFunction &cgf, mlir::Location loc, QualType srcRecordTy, QualType destRecordTy, cir::PointerType destCIRTy, bool isRefCast, Address src)
static RValue performReturnAdjustment(CIRGenFunction &cgf, QualType resultType, RValue rv, const ThunkInfo &thunk)
static llvm::FunctionCallee getThrowFn(CodeGenModule &CGM)
static void emitTlsGuardCheck(CodeGenFunction &CGF, llvm::GlobalValue *TlsGuard, llvm::BasicBlock *DynInitBB, llvm::BasicBlock *ContinueBB)
static llvm::GlobalVariable * getTypeInfoVTable(CodeGenModule &CGM)
static bool hasDefaultCXXMethodCC(ASTContext &Context, const CXXMethodDecl *MD)
static bool isTrivialForMSVC(const CXXRecordDecl *RD, QualType Ty, CodeGenModule &CGM)
static llvm::FunctionCallee getInitThreadAbortFn(CodeGenModule &CGM)
static llvm::FunctionCallee getInitThreadHeaderFn(CodeGenModule &CGM)
static llvm::GlobalValue * getTlsGuardVar(CodeGenModule &CGM)
static QualType decomposeTypeForEH(ASTContext &Context, QualType T, bool &IsConst, bool &IsVolatile, bool &IsUnaligned)
static llvm::CallBase * emitRTtypeidCall(CodeGenFunction &CGF, llvm::Value *Argument)
static llvm::FunctionCallee getDynTlsOnDemandInitFn(CodeGenModule &CGM)
static void emitDynamicTlsInitializationCall(CodeGenFunction &CGF, llvm::GlobalValue *TlsGuard, llvm::BasicBlock *ContinueBB)
static void detectAmbiguousBases(SmallVectorImpl< MSRTTIClass > &Classes)
Find ambiguity among base classes.
static void emitDynamicTlsInitialization(CodeGenFunction &CGF)
static void serializeClassHierarchy(SmallVectorImpl< MSRTTIClass > &Classes, const CXXRecordDecl *RD)
Recursively serializes a class hierarchy in pre-order depth first order.
static llvm::FunctionCallee getInitThreadFooterFn(CodeGenModule &CGM)
static bool isDeletingDtor(GlobalDecl GD)
static void emitGlobalDtorWithTLRegDtor(CodeGenFunction &CGF, const VarDecl &VD, llvm::FunctionCallee Dtor, llvm::Constant *Addr)
static ConstantAddress getInitThreadEpochPtr(CodeGenModule &CGM)
static void mangleVFTableName(MicrosoftMangleContext &MangleContext, const CXXRecordDecl *RD, const VPtrInfo &VFPtr, SmallString< 256 > &Name)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
FormatToken * Next
The next token in the unwrapped line.
Result
Implement __builtin_bit_cast and related operations.
bool isMemberPointerToDerivedMember() const
Definition APValue.cpp:1227
const ValueDecl * getMemberPointerDecl() const
Definition APValue.cpp:1220
ArrayRef< const CXXRecordDecl * > getMemberPointerPath() const
Definition APValue.cpp:1234
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
uint64_t getFieldOffset(const ValueDecl *FD) const
Get the offset of a FieldDecl or IndirectFieldDecl, in bits.
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D,...
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
IdentifierTable & Idents
Definition ASTContext.h:855
const LangOptions & getLangOpts() const
CanQualType IntTy
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
const ArrayType * getAsArrayType(QualType T) const
Type Query functions.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
QualType getMemberPointerType(QualType T, NestedNameSpecifier Qualifier, const CXXRecordDecl *Cls) const
Return the uniqued reference to the type for a member pointer to the specified type in the specified ...
const CXXConstructorDecl * getCopyConstructorForExceptionObject(CXXRecordDecl *RD)
QualType getSizeType() const
Return the unique type for "size_t" (C99 7.17), defined in <stddef.h>.
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:974
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
CanQualType getCanonicalTagType(const TagDecl *TD) const
unsigned getTargetAddressSpace(LangAS AS) const
void recordMemberDataPointerEvaluation(const ValueDecl *VD)
llvm::DenseMap< const CXXRecordDecl *, VBaseInfo > VBaseOffsetsMapTy
CharUnits getVBPtrOffset() const
getVBPtrOffset - Get the offset for virtual base table pointer.
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const
getVBaseClassOffset - Get the offset, in chars, for the given base class.
const VBaseOffsetsMapTy & getVBaseOffsetsMap() const
bool hasExtendableVFPtr() const
hasVFPtr - Does this class have a virtual function table pointer that can be extended by a derived cl...
Represents a base class of a C++ class.
Definition DeclCXX.h:146
VarDecl * getExceptionDecl() const
Definition StmtCXX.h:50
bool isDefaultConstructor() const
Whether this constructor is a default constructor (C++ [class.ctor]p5), which can be used to default-...
Definition DeclCXX.cpp:3049
ArrayRef< CXXDefaultArgExpr * > getCtorClosureDefaultArgs() const
Definition DeclCXX.cpp:3139
FunctionDecl * getOperatorDelete() const
Definition ExprCXX.h:2670
bool isGlobalDelete() const
Definition ExprCXX.h:2656
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2153
bool isVirtual() const
Definition DeclCXX.h:2208
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition DeclCXX.h:2296
QualType getThisType() const
Return the type of the this pointer.
Definition DeclCXX.cpp:2859
bool isInstance() const
Definition DeclCXX.h:2180
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
bool hasNonTrivialCopyAssignment() const
Determine whether this class has a non-trivial copy assignment operator (C++ [class....
Definition DeclCXX.h:1350
CXXRecordDecl * getMostRecentDecl()
Definition DeclCXX.h:540
bool hasPrivateFields() const
Definition DeclCXX.h:1201
base_class_range bases()
Definition DeclCXX.h:609
bool hasProtectedFields() const
Definition DeclCXX.h:1205
bool hasNonTrivialDestructor() const
Determine whether this class has a non-trivial destructor (C++ [class.dtor]p3)
Definition DeclCXX.h:1392
bool isPolymorphic() const
Whether this class is polymorphic (C++ [class.virtual]), which means that the class contains or inher...
Definition DeclCXX.h:1224
unsigned getNumBases() const
Retrieves the number of base classes of this class.
Definition DeclCXX.h:603
base_class_range vbases()
Definition DeclCXX.h:626
MSInheritanceModel getMSInheritanceModel() const
Returns the inheritance model used for this record.
bool nullFieldOffsetIsZero() const
In the Microsoft C++ ABI, use zero for the field offset of a null data member pointer if we can guara...
bool hasDefinition() const
Definition DeclCXX.h:562
bool isEmpty() const
Determine whether this is an empty class in the sense of (C++11 [meta.unary.prop]).
Definition DeclCXX.h:1196
CXXDestructorDecl * getDestructor() const
Returns the destructor decl for this class.
Definition DeclCXX.cpp:2129
bool hasNonTrivialDefaultConstructor() const
Determine whether this class has a non-trivial default constructor (C++11 [class.ctor]p5).
Definition DeclCXX.h:1263
bool isVirtuallyDerivedFrom(const CXXRecordDecl *Base) const
Determine whether this class is virtually derived from the class Base.
bool needsImplicitCopyAssignment() const
Determine whether this class needs an implicit copy assignment operator to be lazily declared.
Definition DeclCXX.h:933
bool hasSimpleCopyAssignment() const
true if we know for sure that this class has a single, accessible, unambiguous copy assignment operat...
Definition DeclCXX.h:742
unsigned getNumVBases() const
Retrieves the number of virtual base classes of this class.
Definition DeclCXX.h:624
const Expr * getSubExpr() const
Definition ExprCXX.h:1233
path_iterator path_begin()
Definition Expr.h:3815
CastKind getCastKind() const
Definition Expr.h:3789
path_iterator path_end()
Definition Expr.h:3816
const CXXBaseSpecifier *const * path_const_iterator
Definition Expr.h:3812
Expr * getSubExpr()
Definition Expr.h:3795
This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
bool isNegative() const
Test whether the quantity is less than zero.
Definition CharUnits.h:110
bool isPositive() const
Test whether the quantity is greater than zero.
Definition CharUnits.h:107
bool isZero() const
Test whether the quantity equals zero.
Definition CharUnits.h:101
llvm::Align getAsAlign() const
Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit bytes.
Definition CharUnits.h:159
QuantityType getQuantity() const
Get the raw integer representation of this quantity.
Definition CharUnits.h:155
static CharUnits One()
Construct a CharUnits quantity of one.
Definition CharUnits.h:55
static CharUnits fromQuantity(QuantityType Quantity)
Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:58
static CharUnits Zero()
Construct a CharUnits quantity of zero.
Definition CharUnits.h:52
bool hasProfileIRInstr() const
Check if IR level profile instrumentation is on.
static ABIArgInfo getIndirect(CharUnits Alignment, unsigned AddrSpace, bool ByVal=true, bool Realign=false, llvm::Type *Padding=nullptr)
void setSRetAfterThis(bool AfterThis)
bool isHomogeneousAggregate(QualType Ty, const Type *&Base, uint64_t &Members) const
isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous aggregate.
Definition ABIInfo.cpp:66
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
Definition Address.h:128
llvm::Value * emitRawPointer(CodeGenFunction &CGF) const
Return the pointer contained in this class after authenticating it and adding offset to it if necessa...
Definition Address.h:253
Address withElementType(llvm::Type *ElemTy) const
Return address with different element type, but same pointer and alignment.
Definition Address.h:276
static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF)
Apply TemporaryLocation if it is valid.
static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF)
Set the IRBuilder to not attach debug locations.
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
Definition CGBuilder.h:156
Address CreateConstInBoundsByteGEP(Address Addr, CharUnits Offset, const llvm::Twine &Name="")
Given a pointer to i8, adjust it by a given constant offset.
Definition CGBuilder.h:325
Address CreateConstInBoundsGEP2_32(Address Addr, unsigned Idx0, unsigned Idx1, const llvm::Twine &Name="")
Definition CGBuilder.h:345
Address CreateGEP(CodeGenFunction &CGF, Address Addr, llvm::Value *Index, const llvm::Twine &Name="")
Definition CGBuilder.h:312
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition CGBuilder.h:128
Address CreateConstByteGEP(Address Addr, CharUnits Offset, const llvm::Twine &Name="")
Definition CGBuilder.h:335
llvm::LoadInst * CreateAlignedLoad(llvm::Type *Ty, llvm::Value *Addr, CharUnits Align, const llvm::Twine &Name="")
Definition CGBuilder.h:148
Address CreateInBoundsGEP(Address Addr, ArrayRef< llvm::Value * > IdxList, llvm::Type *ElementType, CharUnits Align, const Twine &Name="")
Definition CGBuilder.h:366
Implements C++ ABI-specific code generation functions.
Definition CGCXXABI.h:43
RecordArgABI
Specify how one should pass an argument of a record type.
Definition CGCXXABI.h:150
MangleContext & getMangleContext()
Gets the mangle context.
Definition CGCXXABI.h:113
static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr, llvm::FunctionType *FTy)
Definition CGCall.h:150
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition CGCall.h:140
CanQualType getReturnType() const
unsigned getEffectiveCallingConvention() const
getEffectiveCallingConvention - Return the actual calling convention to use, which may depend on the ...
void add(RValue rvalue, QualType type)
Definition CGCall.h:305
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
llvm::Value * GetVTablePtr(Address This, llvm::Type *VTableTy, const CXXRecordDecl *VTableClass, VTableAuthMode AuthMode=VTableAuthMode::Authenticate)
GetVTablePtr - Return the Value of the vtable pointer member pointed to by This.
Definition CGClass.cpp:2727
LValue EmitLoadOfReferenceLValue(LValue RefLVal)
Definition CGExpr.cpp:3444
GlobalDecl CurGD
CurGD - The GlobalDecl for the current function being compiled.
llvm::Constant * createAtExitStub(const VarDecl &VD, llvm::FunctionCallee Dtor, llvm::Constant *Addr)
Create a stub function, suitable for being passed to atexit, which passes the given address to the gi...
void EmitMustTailThunk(GlobalDecl GD, llvm::Value *AdjustedThisPtr, llvm::FunctionCallee Callee)
Emit a musttail call for a thunk with a potentially adjusted this pointer.
void EmitNoreturnRuntimeCallOrInvoke(llvm::FunctionCallee callee, ArrayRef< llvm::Value * > args)
Emits a call or invoke to the given noreturn runtime function.
Definition CGCall.cpp:5539
llvm::CallBase * EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee, ArrayRef< llvm::Value * > args, const Twine &name="")
Emits a call or invoke instruction to the given runtime function.
Definition CGCall.cpp:5566
bool CurFuncIsThunk
In C++, whether we are code generating a thunk.
void registerGlobalDtorWithAtExit(const VarDecl &D, llvm::FunctionCallee fn, llvm::Constant *addr)
Call atexit() with a function that passes the given argument to the given function.
llvm::Value * getAsNaturalPointerTo(Address Addr, QualType PointeeType)
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
AutoVarEmission EmitAutoVarAlloca(const VarDecl &var)
EmitAutoVarAlloca - Emit the alloca and debug information for a local variable.
Definition CGDecl.cpp:1497
void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type, bool ForVirtualBase, bool Delegating, Address This, QualType ThisTy)
Definition CGClass.cpp:2550
const Decl * CurCodeDecl
CurCodeDecl - This is the inner-most code context, which includes blocks.
void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::GlobalVariable *GV, bool PerformInit)
EmitCXXGlobalVarDeclInit - Create the initializer for a C++ variable with global storage.
void StartFunction(GlobalDecl GD, QualType RetTy, llvm::Function *Fn, const CGFunctionInfo &FnInfo, const FunctionArgList &Args, SourceLocation Loc=SourceLocation(), SourceLocation StartLoc=SourceLocation())
Emit code for the start of a function.
llvm::Value * EmitVTableTypeCheckedLoad(const CXXRecordDecl *RD, llvm::Value *VTable, llvm::Type *VTableTy, uint64_t VTableByteOffset, SourceLocation Loc)
Emit a type checked load from the given vtable.
Definition CGClass.cpp:3006
void EmitCXXGuardedInitBranch(llvm::Value *NeedsInit, llvm::BasicBlock *InitBlock, llvm::BasicBlock *NoInitBlock, GuardKind Kind, const VarDecl *D)
Emit a branch to select whether or not to perform guarded initialization.
RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, llvm::CallBase **CallOrInvoke, bool IsMustTail, SourceLocation Loc, bool IsVirtualFunctionPointerThunk=false)
EmitCall - Generate a call of the given function, expecting the given result type,...
Definition CGCall.cpp:5744
RawAddress CreateMemTempWithoutCast(QualType T, const Twine &Name="tmp")
CreateMemTemp - Create a temporary memory object of the given type, with appropriate alignmen without...
Definition CGExpr.cpp:236
void EmitTypeMetadataCodeForVCall(const CXXRecordDecl *RD, llvm::Value *VTable, SourceLocation Loc)
If whole-program virtual table optimization is enabled, emit an assumption that VTable is a member of...
Definition CGClass.cpp:2799
llvm::CallInst * EmitNounwindRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
const Decl * CurFuncDecl
CurFuncDecl - Holds the Decl for the current outermost non-closure context.
void EmitAutoVarCleanups(const AutoVarEmission &emission)
Definition CGDecl.cpp:2287
void EmitAnyExprToMem(const Expr *E, Address Location, Qualifiers Quals, bool IsInitializer)
EmitAnyExprToMem - Emits the code necessary to evaluate an arbitrary expression into the given memory...
Definition CGExpr.cpp:314
llvm::CallInst * EmitRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
CodeGenTypes & getTypes() const
void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr, QualType DeleteTy, llvm::Value *NumElements=nullptr, CharUnits CookieSize=CharUnits(), llvm::Constant *CalleeOverride=nullptr)
void EmitCallArgs(CallArgList &Args, PrototypeWrapper Prototype, llvm::iterator_range< CallExpr::const_arg_iterator > ArgRange, AbstractCallee AC=AbstractCallee(), unsigned ParamsToSkip=0, EvaluationOrder Order=EvaluationOrder::Default)
EmitCallArgs - Emit call arguments for a function.
Definition CGCall.cpp:5108
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
void FinishFunction(SourceLocation EndLoc=SourceLocation())
FinishFunction - Complete IR generation of the current function.
Address GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
llvm::Instruction * CurrentFuncletPad
bool ShouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *RD)
Returns whether we should perform a type checked load when loading a virtual function for virtual cal...
Definition CGClass.cpp:2993
void PopCleanupBlock(bool FallThroughIsBranchThrough=false, bool ForDeactivation=false)
PopCleanupBlock - Will pop the cleanup entry on the stack and process all branch fixups.
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition CGStmt.cpp:658
This class organizes the cross-function state that is used while generating LLVM code.
void setGVProperties(llvm::GlobalValue *GV, GlobalDecl GD) const
Set visibility, dllimport/dllexport and dso_local.
void AddCXXDtorEntry(llvm::FunctionCallee DtorFn, llvm::Constant *Object)
Add a destructor and object to add to the C++ global destructor function.
void AddVTableTypeMetadata(llvm::GlobalVariable *VTable, CharUnits Offset, const CXXRecordDecl *RD)
Create and attach type metadata for the given vtable.
void setDSOLocal(llvm::GlobalValue *GV) const
llvm::GlobalObject::VCallVisibility GetVCallVisibilityLevel(const CXXRecordDecl *RD, llvm::DenseSet< const CXXRecordDecl * > &Visited)
Returns the vcall visibility of the given type.
llvm::Module & getModule() const
llvm::FunctionCallee CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeList ExtraAttrs=llvm::AttributeList(), bool Local=false, bool AssumeConvergent=false)
Create or return a runtime function declaration with the specified type and name.
CodeGenVTables & getVTables()
bool classNeedsVectorDestructor(const CXXRecordDecl *RD)
Check that class need vector deleting destructor body.
llvm::Constant * GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH=false)
Get the address of the RTTI descriptor for the given type.
llvm::Constant * GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty=nullptr, bool ForVTable=false, bool DontDefer=false, ForDefinition_t IsForDefinition=NotForDefinition)
Return the address of the given function.
DiagnosticsEngine & getDiags() const
llvm::Constant * getAddrOfCXXStructor(GlobalDecl GD, const CGFunctionInfo *FnInfo=nullptr, llvm::FunctionType *FnType=nullptr, bool DontDefer=false, ForDefinition_t IsForDefinition=NotForDefinition)
Return the address of the constructor/destructor of the given type.
llvm::GlobalValue::LinkageTypes getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage)
Returns LLVM linkage for a declarator.
const LangOptions & getLangOpts() const
const TargetInfo & getTarget() const
bool shouldEmitRTTI(bool ForEH=false)
void EmitGlobal(GlobalDecl D)
Emit code for a single global function or var decl.
void addUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.used metadata.
void AppendLinkerOptions(StringRef Opts)
Appends Opts to the "llvm.linker.options" metadata value.
bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D)
Try to emit a base destructor as an alias to its primary base-class destructor.
Definition CGCXX.cpp:34
const llvm::DataLayout & getDataLayout() const
CharUnits computeNonVirtualBaseClassOffset(const CXXRecordDecl *DerivedClass, CastExpr::path_const_iterator Start, CastExpr::path_const_iterator End)
Definition CGClass.cpp:170
CharUnits getVBaseAlignment(CharUnits DerivedAlign, const CXXRecordDecl *Derived, const CXXRecordDecl *VBase)
Returns the assumed alignment of a virtual base of a class.
Definition CGClass.cpp:78
void EmitDefinitionAsAlias(GlobalDecl Alias, GlobalDecl Target)
Emit a definition as a global alias for another definition, unconditionally.
Definition CGCXX.cpp:207
llvm::Function * codegenCXXStructor(GlobalDecl GD)
Definition CGCXX.cpp:268
llvm::Constant * CreateRuntimeVariable(llvm::Type *Ty, StringRef Name)
Create a new runtime global variable with the specified type and name.
void setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const
Set the TLS mode for the given LLVM GlobalValue for the thread-local variable declaration D.
ASTContext & getContext() const
llvm::Constant * GetAddrOfGlobalVar(const VarDecl *D, llvm::Type *Ty=nullptr, ForDefinition_t IsForDefinition=NotForDefinition)
Return the llvm::Constant for the address of the given global variable.
MicrosoftVTableContext & getMicrosoftVTableContext()
const TargetCodeGenInfo & getTargetCodeGenInfo()
const CodeGenOptions & getCodeGenOpts() const
StringRef getMangledName(GlobalDecl GD)
llvm::GlobalVariable * CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage, llvm::Align Alignment)
Will return a global variable of the given type.
llvm::LLVMContext & getLLVMContext()
llvm::GlobalValue * GetGlobalValue(StringRef Ref)
void maybeSetTrivialComdat(const Decl &D, llvm::GlobalObject &GO)
void setDLLImportDLLExport(llvm::GlobalValue *GV, GlobalDecl D) const
llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD)
Return the appropriate linkage for the vtable, VTT, and type information of the given class.
void SetLLVMFunctionAttributes(GlobalDecl GD, const CGFunctionInfo &Info, llvm::Function *F, bool IsThunk)
Set the LLVM function attributes (sext, zext, etc).
void addDeferredVTable(const CXXRecordDecl *RD)
void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F)
Set the LLVM function attributes which only apply to a function definition.
llvm::Function * CreateGlobalInitOrCleanUpFunction(llvm::FunctionType *ty, const Twine &name, const CGFunctionInfo &FI, SourceLocation Loc=SourceLocation(), bool TLS=false, llvm::GlobalVariable::LinkageTypes Linkage=llvm::GlobalVariable::InternalLinkage)
const CGFunctionInfo & arrangeCXXConstructorCall(const CallArgList &Args, const CXXConstructorDecl *D, CXXCtorType CtorKind, unsigned ExtraPrefixArgs, unsigned ExtraSuffixArgs, const FunctionDecl *ABIInfoFD, bool PassProtoArgs=true)
Arrange a call to a C++ method, passing the given arguments.
Definition CGCall.cpp:504
const CGFunctionInfo & arrangeCXXMethodDeclaration(const CXXMethodDecl *MD)
C++ methods have some special rules and also have implicit parameters.
Definition CGCall.cpp:387
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition CGCall.cpp:2098
bool isFuncTypeConvertible(const FunctionType *FT)
isFuncTypeConvertible - Utility to check whether a function type can be converted to an LLVM type (i....
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
const CGFunctionInfo & arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD)
Arrange a thunk that takes 'this' as the first parameter followed by varargs.
Definition CGCall.cpp:656
const CGFunctionInfo & arrangeCXXStructorDeclaration(GlobalDecl GD)
Definition CGCall.cpp:421
const CGFunctionInfo & arrangeMSCtorClosure(const CXXConstructorDecl *CD, CXXCtorType CT)
Definition CGCall.cpp:665
const CGFunctionInfo & arrangeNullaryFunction()
A nullary function is a freestanding function of type 'void ()'.
Definition CGCall.cpp:824
void createVTableInitializer(ConstantStructBuilder &builder, const VTableLayout &layout, llvm::Constant *rtti, bool vtableHasLocalLinkage)
Add vtable components for the given vtable layout to the given global initializer.
A specialization of Address that requires the address to be an LLVM Constant.
Definition Address.h:296
static RValue get(llvm::Value *V)
Definition CGValue.h:99
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
Definition CGValue.h:72
virtual LangAS getSRetAddrSpace(const CXXRecordDecl *RD) const
Get the address space for an indirect (sret) return of the given type.
Definition TargetInfo.h:354
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition DeclBase.h:2106
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition DeclBase.h:2403
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
bool isInStdNamespace() const
Definition DeclBase.cpp:453
T * getAttr() const
Definition DeclBase.h:582
SourceLocation getLocation() const
Definition DeclBase.h:448
DeclContext * getDeclContext()
Definition DeclBase.h:457
bool hasAttr() const
Definition DeclBase.h:586
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:283
QualType getType() const
Definition Expr.h:146
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition Decl.h:3577
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition Decl.cpp:3900
bool isDefined(const FunctionDecl *&Definition, bool CheckForPendingFriendDefinition=false) const
Returns true if the function has a definition that does not need to be instantiated.
Definition Decl.cpp:3252
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5548
bool isVariadic() const
Whether this function prototype is variadic.
Definition TypeBase.h:5952
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:60
GlobalDecl getWithCtorType(CXXCtorType Type)
Definition GlobalDecl.h:176
GlobalDecl getWithDtorType(CXXDtorType Type)
Definition GlobalDecl.h:183
CXXDtorType getDtorType() const
Definition GlobalDecl.h:122
const Decl * getDecl() const
Definition GlobalDecl.h:115
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
static ImplicitParamDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, ImplicitParamKind ParamKind)
Create implicit parameter.
Definition Decl.cpp:5710
bool isExplicitDefaultVisibilityExportMapping() const
bool isAllDefaultVisibilityExportMapping() const
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
Definition Mangle.h:56
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition TypeBase.h:3751
CXXRecordDecl * getMostRecentCXXRecordDecl() const
Note: this can trigger extra deserialization when external AST sources are used.
Definition Type.cpp:5847
QualType getPointeeType() const
Definition TypeBase.h:3769
bool isMemberFunctionPointer() const
Returns true if the member type (i.e.
Definition TypeBase.h:3773
bool isMemberDataPointer() const
Returns true if the member type (i.e.
Definition TypeBase.h:3779
unsigned getVBTableIndex(const CXXRecordDecl *Derived, const CXXRecordDecl *VBase)
Returns the index of VBase in the vbtable of Derived.
MethodVFTableLocation getMethodVFTableLocation(GlobalDecl GD)
const VPtrInfoVector & getVFPtrOffsets(const CXXRecordDecl *RD)
const VTableLayout & getVFTableLayout(const CXXRecordDecl *RD, CharUnits VFPtrOffset)
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition Decl.h:297
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition Decl.h:342
bool isExternallyVisible() const
Definition Decl.h:435
A (possibly-)qualified type.
Definition TypeBase.h:938
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition TypeBase.h:8662
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1005
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition TypeBase.h:8618
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition TypeBase.h:8672
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition TypeBase.h:8651
DestructionKind isDestructedType() const
Returns a nonzero value if objects of this type require non-trivial work to clean up after.
Definition TypeBase.h:1561
bool hasUnaligned() const
Definition TypeBase.h:512
bool canPassInRegisters() const
Determine whether this class can be passed in registers.
Definition Decl.h:4643
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:343
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
virtual bool emitVectorDeletingDtors(const LangOptions &) const
Controls whether to emit MSVC vector deleting destructors.
uint64_t getPointerWidth(LangAS AddrSpace) const
Return the width of pointers on this target, for the specified address space.
Definition TargetInfo.h:497
virtual bool callGlobalDeleteInDeletingDtor(const LangOptions &) const
Controls whether global operator delete is called by the deleting destructor or at the point where de...
The base class of the type hierarchy.
Definition TypeBase.h:1879
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
bool isPointerType() const
Definition TypeBase.h:8815
const T * castAs() const
Member-template castAs<specific type>.
Definition TypeBase.h:9498
bool isReferenceType() const
Definition TypeBase.h:8839
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:885
bool isObjectType() const
Determine whether this type is an object type.
Definition TypeBase.h:2574
Linkage getLinkage() const
Determine the linkage of this type.
Definition Type.cpp:5202
bool isNullPtrType() const
Definition TypeBase.h:9241
ArrayRef< VTableComponent > vtable_components() const
QualType getType() const
Definition Decl.h:725
Represents a variable declaration or definition.
Definition Decl.h:934
TLSKind getTLSKind() const
Definition Decl.cpp:2149
bool isNoDestroy(const ASTContext &) const
Is destruction of this variable entirely suppressed?
Definition Decl.cpp:2828
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
Definition Decl.h:1216
llvm::Value * getCXXDestructorImplicitParam(CodeGenModule &CGM, llvm::BasicBlock *InsertBlock, llvm::BasicBlock::iterator InsertPoint, const CXXDestructorDecl *D, CXXDtorType Type, bool ForVirtualBase, bool Delegating)
CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, CGCXXABI &CXXABI)
bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI, const ABIInfo &Info)
@ NormalCleanup
Denotes a cleanup that should run when a scope is exited using normal control flow (falling off the e...
@ EHCleanup
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
CGCXXABI * CreateMicrosoftCXXABI(CodeGenModule &CGM)
Creates a Microsoft-family ABI.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
constexpr Variable var(Literal L)
Returns the variable of L.
Definition CNFFormula.h:64
PRESERVE_NONE bool Ret(InterpState &S)
Definition Interp.h:276
bool This(InterpState &S, CodePtr OpPC)
Definition Interp.h:3186
@ Address
A pointer to a ValueDecl.
Definition Primitives.h:28
Top level wrappers for InstallAPI frontend operations.
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
CXXCtorType
C++ constructor types.
Definition ABI.h:24
@ Ctor_Base
Base object ctor.
Definition ABI.h:26
@ Ctor_DefaultClosure
Default closure variant of a ctor.
Definition ABI.h:29
@ Ctor_CopyingClosure
Copying closure variant of a ctor.
Definition ABI.h:28
@ Ctor_Complete
Complete object ctor.
Definition ABI.h:25
bool isa(CodeGen::Address addr)
Definition Address.h:330
GVALinkage
A more specific kind of linkage than enum Linkage.
Definition Linkage.h:72
@ GVA_Internal
Definition Linkage.h:73
bool inheritanceModelHasNVOffsetField(bool IsMemberFunction, MSInheritanceModel Inheritance)
@ AS_public
Definition Specifiers.h:128
bool inheritanceModelHasOnlyOneField(bool IsMemberFunction, MSInheritanceModel Inheritance)
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
bool inheritanceModelHasVBPtrOffsetField(MSInheritanceModel Inheritance)
bool inheritanceModelHasVBTableOffsetField(MSInheritanceModel Inheritance)
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
Definition Linkage.h:24
@ Module
Module linkage, which indicates that the entity can be referred to from other translation units withi...
Definition Linkage.h:54
const FunctionProtoType * T
@ Template
We are parsing a template declaration.
Definition Parser.h:81
SmallVector< std::unique_ptr< VPtrInfo >, 2 > VPtrInfoVector
CXXDtorType
C++ destructor types.
Definition ABI.h:34
@ Dtor_VectorDeleting
Vector deleting dtor.
Definition ABI.h:40
@ Dtor_Comdat
The COMDAT used for dtors.
Definition ABI.h:38
@ Dtor_Unified
GCC-style unified dtor.
Definition ABI.h:39
@ Dtor_Base
Base object dtor.
Definition ABI.h:37
@ Dtor_Complete
Complete object dtor.
Definition ABI.h:36
@ Dtor_Deleting
Deleting dtor.
Definition ABI.h:35
LangAS
Defines the address space values used by the address space qualifier of QualType.
CastKind
CastKind - The kind of operation required for a conversion.
MSInheritanceModel
Assigned inheritance model for a class in the MS C++ ABI.
Definition Specifiers.h:414
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition Specifiers.h:282
U cast(CodeGen::Address addr)
Definition Address.h:327
@ Class
The "class" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:6157
@ Implicit
An implicit conversion.
Definition Sema.h:434
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t int64_t
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat int32_t
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
const CXXRecordDecl * VBase
If nonnull, holds the last vbase which contains the vfptr that the method definition is adjusted to.
CharUnits VFPtrOffset
This is the offset of the vfptr from the start of the last vbase, or the complete type if there are n...
uint64_t Index
Method's index in the vftable.
bool isEmpty() const
Definition Thunk.h:70
union clang::ReturnAdjustment::VirtualAdjustment Virtual
int64_t NonVirtual
The non-virtual adjustment from the derived object to its nearest virtual base.
Definition Thunk.h:30
union clang::ThisAdjustment::VirtualAdjustment Virtual
bool isEmpty() const
Definition Thunk.h:137
int64_t NonVirtual
The non-virtual adjustment from the derived object to its nearest virtual base.
Definition Thunk.h:95
ThisAdjustment This
The this pointer adjustment.
Definition Thunk.h:159
bool isAlignRequired()
Definition ASTContext.h:201
Holds information about the inheritance path to a virtual base or function table pointer.
CharUnits NonVirtualOffset
IntroducingObject is at this offset from its containing complete object or virtual base.
CharUnits FullOffsetInMDC
Static offset from the top of the most derived class to this vfptr, including any virtual base offset...
const CXXRecordDecl * getVBaseWithVPtr() const
The vptr is stored inside the non-virtual component of this virtual base.
const CXXRecordDecl * IntroducingObject
This is the class that introduced the vptr by declaring new virtual methods or virtual bases.
BasePath MangledPath
The bases from the inheritance path that got used to mangle the vbtable name.
BasePath PathToIntroducingObject
This holds the base classes path from the complete type to the first base with the given vfptr offset...
const CXXRecordDecl * ObjectWithVPtr
This is the most derived class that has this vptr at offset zero.
uint32_t VBPtrOffset
The offset (in bytes) of the vbptr, relative to the beginning of the derived class.
Definition Thunk.h:46
struct clang::ReturnAdjustment::VirtualAdjustment::@167242364125115315334113104006210160165266060257 Microsoft
uint32_t VBIndex
Index of the virtual base in the vbtable.
Definition Thunk.h:49
int32_t VtordispOffset
The offset of the vtordisp (in bytes), relative to the ECX.
Definition Thunk.h:109
struct clang::ThisAdjustment::VirtualAdjustment::@312251255113040203233347230177110330127151157305 Microsoft
int32_t VBOffsetOffset
The offset (in bytes) of the vbase offset in the vbtable.
Definition Thunk.h:116
int32_t VBPtrOffset
The offset of the vbptr of the derived class (in bytes), relative to the ECX after vtordisp adjustmen...
Definition Thunk.h:113