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