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