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