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