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