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