Coverage Report

Created: 2021-07-27 07:33

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