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