Coverage Report

Created: 2022-07-16 07:03

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