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