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