Coverage Report

Created: 2020-02-25 14:32

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