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