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