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