Coverage Report

Created: 2019-07-24 05:18

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