1 //===------- ItaniumCXXABI.cpp - Emit LLVM Code from ASTs for a Module ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This provides C++ code generation targeting the Itanium C++ ABI. The class
11 // in this file generates structures that follow the Itanium C++ ABI, which is
13 // http://www.codesourcery.com/public/cxx-abi/abi.html
14 // http://www.codesourcery.com/public/cxx-abi/abi-eh.html
16 // It also supports the closely-related ARM ABI, documented at:
17 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
19 //===----------------------------------------------------------------------===//
22 #include "CGRecordLayout.h"
23 #include "CGVTables.h"
24 #include "CodeGenFunction.h"
25 #include "CodeGenModule.h"
26 #include "clang/AST/Mangle.h"
27 #include "clang/AST/Type.h"
28 #include "llvm/IR/CallSite.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/Intrinsics.h"
31 #include "llvm/IR/Value.h"
33 using namespace clang;
34 using namespace CodeGen;
37 class ItaniumCXXABI : public CodeGen::CGCXXABI {
38 /// VTables - All the vtables which have been defined.
39 llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> VTables;
42 bool UseARMMethodPtrABI;
43 bool UseARMGuardVarABI;
45 ItaniumMangleContext &getMangleContext() {
46 return cast<ItaniumMangleContext>(CodeGen::CGCXXABI::getMangleContext());
50 ItaniumCXXABI(CodeGen::CodeGenModule &CGM,
51 bool UseARMMethodPtrABI = false,
52 bool UseARMGuardVarABI = false) :
53 CGCXXABI(CGM), UseARMMethodPtrABI(UseARMMethodPtrABI),
54 UseARMGuardVarABI(UseARMGuardVarABI) { }
56 bool classifyReturnType(CGFunctionInfo &FI) const override;
58 RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const override {
59 // Structures with either a non-trivial destructor or a non-trivial
60 // copy constructor are always indirect.
61 // FIXME: Use canCopyArgument() when it is fixed to handle lazily declared
63 if (RD->hasNonTrivialDestructor() || RD->hasNonTrivialCopyConstructor())
68 bool isZeroInitializable(const MemberPointerType *MPT) override;
70 llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT) override;
73 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
76 llvm::Value *MemFnPtr,
77 const MemberPointerType *MPT) override;
80 EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
83 const MemberPointerType *MPT) override;
85 llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
87 llvm::Value *Src) override;
88 llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
89 llvm::Constant *Src) override;
91 llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT) override;
93 llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD) override;
94 llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
95 CharUnits offset) override;
96 llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT) override;
97 llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD,
98 CharUnits ThisAdjustment);
100 llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
101 llvm::Value *L, llvm::Value *R,
102 const MemberPointerType *MPT,
103 bool Inequality) override;
105 llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
107 const MemberPointerType *MPT) override;
109 void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE,
110 llvm::Value *Ptr, QualType ElementType,
111 const CXXDestructorDecl *Dtor) override;
113 void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override;
115 void EmitFundamentalRTTIDescriptor(QualType Type);
116 void EmitFundamentalRTTIDescriptors();
117 llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override;
119 bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override;
120 void EmitBadTypeidCall(CodeGenFunction &CGF) override;
121 llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy,
122 llvm::Value *ThisPtr,
123 llvm::Type *StdTypeInfoPtrTy) override;
125 bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
126 QualType SrcRecordTy) override;
128 llvm::Value *EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value,
129 QualType SrcRecordTy, QualType DestTy,
130 QualType DestRecordTy,
131 llvm::BasicBlock *CastEnd) override;
133 llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, llvm::Value *Value,
134 QualType SrcRecordTy,
135 QualType DestTy) override;
137 bool EmitBadCastCall(CodeGenFunction &CGF) override;
140 GetVirtualBaseClassOffset(CodeGenFunction &CGF, llvm::Value *This,
141 const CXXRecordDecl *ClassDecl,
142 const CXXRecordDecl *BaseClassDecl) override;
144 void EmitCXXConstructors(const CXXConstructorDecl *D) override;
146 void buildStructorSignature(const CXXMethodDecl *MD, StructorType T,
147 SmallVectorImpl<CanQualType> &ArgTys) override;
149 bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
150 CXXDtorType DT) const override {
151 // Itanium does not emit any destructor variant as an inline thunk.
152 // Delegating may occur as an optimization, but all variants are either
153 // emitted with external linkage or as linkonce if they are inline and used.
157 void EmitCXXDestructors(const CXXDestructorDecl *D) override;
159 void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy,
160 FunctionArgList &Params) override;
162 void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override;
164 unsigned addImplicitConstructorArgs(CodeGenFunction &CGF,
165 const CXXConstructorDecl *D,
166 CXXCtorType Type, bool ForVirtualBase,
168 CallArgList &Args) override;
170 void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD,
171 CXXDtorType Type, bool ForVirtualBase,
172 bool Delegating, llvm::Value *This) override;
174 void emitVTableDefinitions(CodeGenVTables &CGVT,
175 const CXXRecordDecl *RD) override;
177 llvm::Value *getVTableAddressPointInStructor(
178 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass,
179 BaseSubobject Base, const CXXRecordDecl *NearestVBase,
180 bool &NeedsVirtualOffset) override;
183 getVTableAddressPointForConstExpr(BaseSubobject Base,
184 const CXXRecordDecl *VTableClass) override;
186 llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
187 CharUnits VPtrOffset) override;
189 llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD,
191 llvm::Type *Ty) override;
193 llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF,
194 const CXXDestructorDecl *Dtor,
195 CXXDtorType DtorType,
197 const CXXMemberCallExpr *CE) override;
199 void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override;
201 void setThunkLinkage(llvm::Function *Thunk, bool ForVTable, GlobalDecl GD,
202 bool ReturnAdjustment) override {
203 // Allow inlining of thunks by emitting them with available_externally
204 // linkage together with vtables when needed.
206 Thunk->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
209 llvm::Value *performThisAdjustment(CodeGenFunction &CGF, llvm::Value *This,
210 const ThisAdjustment &TA) override;
212 llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret,
213 const ReturnAdjustment &RA) override;
215 size_t getSrcArgforCopyCtor(const CXXConstructorDecl *,
216 FunctionArgList &Args) const override {
217 assert(!Args.empty() && "expected the arglist to not be empty!");
218 return Args.size() - 1;
221 StringRef GetPureVirtualCallName() override { return "__cxa_pure_virtual"; }
222 StringRef GetDeletedVirtualCallName() override
223 { return "__cxa_deleted_virtual"; }
225 CharUnits getArrayCookieSizeImpl(QualType elementType) override;
226 llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
228 llvm::Value *NumElements,
229 const CXXNewExpr *expr,
230 QualType ElementType) override;
231 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
232 llvm::Value *allocPtr,
233 CharUnits cookieSize) override;
235 void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
236 llvm::GlobalVariable *DeclPtr,
237 bool PerformInit) override;
238 void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
239 llvm::Constant *dtor, llvm::Constant *addr) override;
241 llvm::Function *getOrCreateThreadLocalWrapper(const VarDecl *VD,
243 void EmitThreadLocalInitFuncs(
245 ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>>
247 ArrayRef<llvm::Function *> CXXThreadLocalInits,
248 ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) override;
250 bool usesThreadWrapperFunction() const override { return true; }
251 LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD,
252 QualType LValType) override;
254 bool NeedsVTTParameter(GlobalDecl GD) override;
256 /**************************** RTTI Uniqueness ******************************/
259 /// Returns true if the ABI requires RTTI type_info objects to be unique
260 /// across a program.
261 virtual bool shouldRTTIBeUnique() const { return true; }
264 /// What sort of unique-RTTI behavior should we use?
265 enum RTTIUniquenessKind {
266 /// We are guaranteeing, or need to guarantee, that the RTTI string
270 /// We are not guaranteeing uniqueness for the RTTI string, so we
271 /// can demote to hidden visibility but must use string comparisons.
274 /// We are not guaranteeing uniqueness for the RTTI string, so we
275 /// have to use string comparisons, but we also have to emit it with
276 /// non-hidden visibility.
280 /// Return the required visibility status for the given type and linkage in
283 classifyRTTIUniqueness(QualType CanTy,
284 llvm::GlobalValue::LinkageTypes Linkage) const;
285 friend class ItaniumRTTIBuilder;
287 void emitCXXStructor(const CXXMethodDecl *MD, StructorType Type) override;
290 class ARMCXXABI : public ItaniumCXXABI {
292 ARMCXXABI(CodeGen::CodeGenModule &CGM) :
293 ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
294 /* UseARMGuardVarABI = */ true) {}
296 bool HasThisReturn(GlobalDecl GD) const override {
297 return (isa<CXXConstructorDecl>(GD.getDecl()) || (
298 isa<CXXDestructorDecl>(GD.getDecl()) &&
299 GD.getDtorType() != Dtor_Deleting));
302 void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV,
303 QualType ResTy) override;
305 CharUnits getArrayCookieSizeImpl(QualType elementType) override;
306 llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
308 llvm::Value *NumElements,
309 const CXXNewExpr *expr,
310 QualType ElementType) override;
311 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, llvm::Value *allocPtr,
312 CharUnits cookieSize) override;
315 class iOS64CXXABI : public ARMCXXABI {
317 iOS64CXXABI(CodeGen::CodeGenModule &CGM) : ARMCXXABI(CGM) {}
319 // ARM64 libraries are prepared for non-unique RTTI.
320 bool shouldRTTIBeUnique() const override { return false; }
324 CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
325 switch (CGM.getTarget().getCXXABI().getKind()) {
326 // For IR-generation purposes, there's no significant difference
327 // between the ARM and iOS ABIs.
328 case TargetCXXABI::GenericARM:
329 case TargetCXXABI::iOS:
330 return new ARMCXXABI(CGM);
332 case TargetCXXABI::iOS64:
333 return new iOS64CXXABI(CGM);
335 // Note that AArch64 uses the generic ItaniumCXXABI class since it doesn't
336 // include the other 32-bit ARM oddities: constructor/destructor return values
337 // and array cookies.
338 case TargetCXXABI::GenericAArch64:
339 return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
340 /* UseARMGuardVarABI = */ true);
342 case TargetCXXABI::GenericItanium:
343 if (CGM.getContext().getTargetInfo().getTriple().getArch()
344 == llvm::Triple::le32) {
345 // For PNaCl, use ARM-style method pointers so that PNaCl code
346 // does not assume anything about the alignment of function
348 return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
349 /* UseARMGuardVarABI = */ false);
351 return new ItaniumCXXABI(CGM);
353 case TargetCXXABI::Microsoft:
354 llvm_unreachable("Microsoft ABI is not Itanium-based");
356 llvm_unreachable("bad ABI kind");
360 ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
361 if (MPT->isMemberDataPointer())
362 return CGM.PtrDiffTy;
363 return llvm::StructType::get(CGM.PtrDiffTy, CGM.PtrDiffTy, nullptr);
366 /// In the Itanium and ARM ABIs, method pointers have the form:
367 /// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
369 /// In the Itanium ABI:
370 /// - method pointers are virtual if (memptr.ptr & 1) is nonzero
371 /// - the this-adjustment is (memptr.adj)
372 /// - the virtual offset is (memptr.ptr - 1)
375 /// - method pointers are virtual if (memptr.adj & 1) is nonzero
376 /// - the this-adjustment is (memptr.adj >> 1)
377 /// - the virtual offset is (memptr.ptr)
378 /// ARM uses 'adj' for the virtual flag because Thumb functions
379 /// may be only single-byte aligned.
381 /// If the member is virtual, the adjusted 'this' pointer points
382 /// to a vtable pointer from which the virtual offset is applied.
384 /// If the member is non-virtual, memptr.ptr is the address of
385 /// the function to call.
386 llvm::Value *ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(
387 CodeGenFunction &CGF, const Expr *E, llvm::Value *&This,
388 llvm::Value *MemFnPtr, const MemberPointerType *MPT) {
389 CGBuilderTy &Builder = CGF.Builder;
391 const FunctionProtoType *FPT =
392 MPT->getPointeeType()->getAs<FunctionProtoType>();
393 const CXXRecordDecl *RD =
394 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
396 llvm::FunctionType *FTy =
397 CGM.getTypes().GetFunctionType(
398 CGM.getTypes().arrangeCXXMethodType(RD, FPT));
400 llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(CGM.PtrDiffTy, 1);
402 llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
403 llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
404 llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
406 // Extract memptr.adj, which is in the second field.
407 llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
409 // Compute the true adjustment.
410 llvm::Value *Adj = RawAdj;
411 if (UseARMMethodPtrABI)
412 Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
414 // Apply the adjustment and cast back to the original struct type
416 llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
417 Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
418 This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
420 // Load the function pointer.
421 llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
423 // If the LSB in the function pointer is 1, the function pointer points to
424 // a virtual function.
425 llvm::Value *IsVirtual;
426 if (UseARMMethodPtrABI)
427 IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
429 IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
430 IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
431 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
433 // In the virtual path, the adjustment left 'This' pointing to the
434 // vtable of the correct base subobject. The "function pointer" is an
435 // offset within the vtable (+1 for the virtual flag on non-ARM).
436 CGF.EmitBlock(FnVirtual);
438 // Cast the adjusted this to a pointer to vtable pointer and load.
439 llvm::Type *VTableTy = Builder.getInt8PtrTy();
440 llvm::Value *VTable = CGF.GetVTablePtr(This, VTableTy);
443 llvm::Value *VTableOffset = FnAsInt;
444 if (!UseARMMethodPtrABI)
445 VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
446 VTable = Builder.CreateGEP(VTable, VTableOffset);
448 // Load the virtual function to call.
449 VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
450 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
451 CGF.EmitBranch(FnEnd);
453 // In the non-virtual path, the function pointer is actually a
455 CGF.EmitBlock(FnNonVirtual);
456 llvm::Value *NonVirtualFn =
457 Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
460 CGF.EmitBlock(FnEnd);
461 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2);
462 Callee->addIncoming(VirtualFn, FnVirtual);
463 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
467 /// Compute an l-value by applying the given pointer-to-member to a
469 llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(
470 CodeGenFunction &CGF, const Expr *E, llvm::Value *Base, llvm::Value *MemPtr,
471 const MemberPointerType *MPT) {
472 assert(MemPtr->getType() == CGM.PtrDiffTy);
474 CGBuilderTy &Builder = CGF.Builder;
476 unsigned AS = Base->getType()->getPointerAddressSpace();
479 Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS));
481 // Apply the offset, which we assume is non-null.
482 llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset");
484 // Cast the address to the appropriate pointer type, adopting the
485 // address space of the base pointer.
487 = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS);
488 return Builder.CreateBitCast(Addr, PType);
491 /// Perform a bitcast, derived-to-base, or base-to-derived member pointer
494 /// Bitcast conversions are always a no-op under Itanium.
496 /// Obligatory offset/adjustment diagram:
497 /// <-- offset --> <-- adjustment -->
498 /// |--------------------------|----------------------|--------------------|
499 /// ^Derived address point ^Base address point ^Member address point
501 /// So when converting a base member pointer to a derived member pointer,
502 /// we add the offset to the adjustment because the address point has
503 /// decreased; and conversely, when converting a derived MP to a base MP
504 /// we subtract the offset from the adjustment because the address point
507 /// The standard forbids (at compile time) conversion to and from
508 /// virtual bases, which is why we don't have to consider them here.
510 /// The standard forbids (at run time) casting a derived MP to a base
511 /// MP when the derived MP does not point to a member of the base.
512 /// This is why -1 is a reasonable choice for null data member
515 ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
518 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
519 E->getCastKind() == CK_BaseToDerivedMemberPointer ||
520 E->getCastKind() == CK_ReinterpretMemberPointer);
522 // Under Itanium, reinterprets don't require any additional processing.
523 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
525 // Use constant emission if we can.
526 if (isa<llvm::Constant>(src))
527 return EmitMemberPointerConversion(E, cast<llvm::Constant>(src));
529 llvm::Constant *adj = getMemberPointerAdjustment(E);
530 if (!adj) return src;
532 CGBuilderTy &Builder = CGF.Builder;
533 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
535 const MemberPointerType *destTy =
536 E->getType()->castAs<MemberPointerType>();
538 // For member data pointers, this is just a matter of adding the
539 // offset if the source is non-null.
540 if (destTy->isMemberDataPointer()) {
543 dst = Builder.CreateNSWSub(src, adj, "adj");
545 dst = Builder.CreateNSWAdd(src, adj, "adj");
548 llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType());
549 llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull");
550 return Builder.CreateSelect(isNull, src, dst);
553 // The this-adjustment is left-shifted by 1 on ARM.
554 if (UseARMMethodPtrABI) {
555 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
557 adj = llvm::ConstantInt::get(adj->getType(), offset);
560 llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj");
563 dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj");
565 dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj");
567 return Builder.CreateInsertValue(src, dstAdj, 1);
571 ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E,
572 llvm::Constant *src) {
573 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
574 E->getCastKind() == CK_BaseToDerivedMemberPointer ||
575 E->getCastKind() == CK_ReinterpretMemberPointer);
577 // Under Itanium, reinterprets don't require any additional processing.
578 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
580 // If the adjustment is trivial, we don't need to do anything.
581 llvm::Constant *adj = getMemberPointerAdjustment(E);
582 if (!adj) return src;
584 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
586 const MemberPointerType *destTy =
587 E->getType()->castAs<MemberPointerType>();
589 // For member data pointers, this is just a matter of adding the
590 // offset if the source is non-null.
591 if (destTy->isMemberDataPointer()) {
592 // null maps to null.
593 if (src->isAllOnesValue()) return src;
596 return llvm::ConstantExpr::getNSWSub(src, adj);
598 return llvm::ConstantExpr::getNSWAdd(src, adj);
601 // The this-adjustment is left-shifted by 1 on ARM.
602 if (UseARMMethodPtrABI) {
603 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
605 adj = llvm::ConstantInt::get(adj->getType(), offset);
608 llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1);
609 llvm::Constant *dstAdj;
611 dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj);
613 dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj);
615 return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1);
619 ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
620 // Itanium C++ ABI 2.3:
621 // A NULL pointer is represented as -1.
622 if (MPT->isMemberDataPointer())
623 return llvm::ConstantInt::get(CGM.PtrDiffTy, -1ULL, /*isSigned=*/true);
625 llvm::Constant *Zero = llvm::ConstantInt::get(CGM.PtrDiffTy, 0);
626 llvm::Constant *Values[2] = { Zero, Zero };
627 return llvm::ConstantStruct::getAnon(Values);
631 ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
633 // Itanium C++ ABI 2.3:
634 // A pointer to data member is an offset from the base address of
635 // the class object containing it, represented as a ptrdiff_t
636 return llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity());
639 llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
640 return BuildMemberPointer(MD, CharUnits::Zero());
643 llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD,
644 CharUnits ThisAdjustment) {
645 assert(MD->isInstance() && "Member function must not be static!");
646 MD = MD->getCanonicalDecl();
648 CodeGenTypes &Types = CGM.getTypes();
650 // Get the function pointer (or index if this is a virtual function).
651 llvm::Constant *MemPtr[2];
652 if (MD->isVirtual()) {
653 uint64_t Index = CGM.getItaniumVTableContext().getMethodVTableIndex(MD);
655 const ASTContext &Context = getContext();
656 CharUnits PointerWidth =
657 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
658 uint64_t VTableOffset = (Index * PointerWidth.getQuantity());
660 if (UseARMMethodPtrABI) {
661 // ARM C++ ABI 3.2.1:
662 // This ABI specifies that adj contains twice the this
663 // adjustment, plus 1 if the member function is virtual. The
664 // least significant bit of adj then makes exactly the same
665 // discrimination as the least significant bit of ptr does for
667 MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset);
668 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
669 2 * ThisAdjustment.getQuantity() + 1);
671 // Itanium C++ ABI 2.3:
672 // For a virtual function, [the pointer field] is 1 plus the
673 // virtual table offset (in bytes) of the function,
674 // represented as a ptrdiff_t.
675 MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset + 1);
676 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
677 ThisAdjustment.getQuantity());
680 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
682 // Check whether the function has a computable LLVM signature.
683 if (Types.isFuncTypeConvertible(FPT)) {
684 // The function has a computable LLVM signature; use the correct type.
685 Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
687 // Use an arbitrary non-function type to tell GetAddrOfFunction that the
688 // function type is incomplete.
691 llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty);
693 MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, CGM.PtrDiffTy);
694 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
695 (UseARMMethodPtrABI ? 2 : 1) *
696 ThisAdjustment.getQuantity());
699 return llvm::ConstantStruct::getAnon(MemPtr);
702 llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP,
704 const MemberPointerType *MPT = MPType->castAs<MemberPointerType>();
705 const ValueDecl *MPD = MP.getMemberPointerDecl();
707 return EmitNullMemberPointer(MPT);
709 CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP);
711 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD))
712 return BuildMemberPointer(MD, ThisAdjustment);
714 CharUnits FieldOffset =
715 getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD));
716 return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset);
719 /// The comparison algorithm is pretty easy: the member pointers are
720 /// the same if they're either bitwise identical *or* both null.
722 /// ARM is different here only because null-ness is more complicated.
724 ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
727 const MemberPointerType *MPT,
729 CGBuilderTy &Builder = CGF.Builder;
731 llvm::ICmpInst::Predicate Eq;
732 llvm::Instruction::BinaryOps And, Or;
734 Eq = llvm::ICmpInst::ICMP_NE;
735 And = llvm::Instruction::Or;
736 Or = llvm::Instruction::And;
738 Eq = llvm::ICmpInst::ICMP_EQ;
739 And = llvm::Instruction::And;
740 Or = llvm::Instruction::Or;
743 // Member data pointers are easy because there's a unique null
744 // value, so it just comes down to bitwise equality.
745 if (MPT->isMemberDataPointer())
746 return Builder.CreateICmp(Eq, L, R);
748 // For member function pointers, the tautologies are more complex.
749 // The Itanium tautology is:
750 // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
751 // The ARM tautology is:
752 // (L == R) <==> (L.ptr == R.ptr &&
753 // (L.adj == R.adj ||
754 // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
755 // The inequality tautologies have exactly the same structure, except
756 // applying De Morgan's laws.
758 llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
759 llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
761 // This condition tests whether L.ptr == R.ptr. This must always be
762 // true for equality to hold.
763 llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
765 // This condition, together with the assumption that L.ptr == R.ptr,
766 // tests whether the pointers are both null. ARM imposes an extra
768 llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
769 llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
771 // This condition tests whether L.adj == R.adj. If this isn't
772 // true, the pointers are unequal unless they're both null.
773 llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
774 llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
775 llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
777 // Null member function pointers on ARM clear the low bit of Adj,
778 // so the zero condition has to check that neither low bit is set.
779 if (UseARMMethodPtrABI) {
780 llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
782 // Compute (l.adj | r.adj) & 1 and test it against zero.
783 llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
784 llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
785 llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
787 EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
790 // Tie together all our conditions.
791 llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
792 Result = Builder.CreateBinOp(And, PtrEq, Result,
793 Inequality ? "memptr.ne" : "memptr.eq");
798 ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
800 const MemberPointerType *MPT) {
801 CGBuilderTy &Builder = CGF.Builder;
803 /// For member data pointers, this is just a check against -1.
804 if (MPT->isMemberDataPointer()) {
805 assert(MemPtr->getType() == CGM.PtrDiffTy);
806 llvm::Value *NegativeOne =
807 llvm::Constant::getAllOnesValue(MemPtr->getType());
808 return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
811 // In Itanium, a member function pointer is not null if 'ptr' is not null.
812 llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
814 llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
815 llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
817 // On ARM, a member function pointer is also non-null if the low bit of 'adj'
818 // (the virtual bit) is set.
819 if (UseARMMethodPtrABI) {
820 llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
821 llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
822 llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
823 llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero,
825 Result = Builder.CreateOr(Result, IsVirtual);
831 bool ItaniumCXXABI::classifyReturnType(CGFunctionInfo &FI) const {
832 const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl();
836 // Return indirectly if we have a non-trivial copy ctor or non-trivial dtor.
837 // FIXME: Use canCopyArgument() when it is fixed to handle lazily declared
839 if (RD->hasNonTrivialDestructor() || RD->hasNonTrivialCopyConstructor()) {
840 FI.getReturnInfo() = ABIArgInfo::getIndirect(0, /*ByVal=*/false);
846 /// The Itanium ABI requires non-zero initialization only for data
847 /// member pointers, for which '0' is a valid offset.
848 bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
849 return MPT->getPointeeType()->isFunctionType();
852 /// The Itanium ABI always places an offset to the complete object
853 /// at entry -2 in the vtable.
854 void ItaniumCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF,
855 const CXXDeleteExpr *DE,
857 QualType ElementType,
858 const CXXDestructorDecl *Dtor) {
859 bool UseGlobalDelete = DE->isGlobalDelete();
860 if (UseGlobalDelete) {
861 // Derive the complete-object pointer, which is what we need
862 // to pass to the deallocation function.
864 // Grab the vtable pointer as an intptr_t*.
865 llvm::Value *VTable = CGF.GetVTablePtr(Ptr, CGF.IntPtrTy->getPointerTo());
867 // Track back to entry -2 and pull out the offset there.
868 llvm::Value *OffsetPtr = CGF.Builder.CreateConstInBoundsGEP1_64(
869 VTable, -2, "complete-offset.ptr");
870 llvm::LoadInst *Offset = CGF.Builder.CreateLoad(OffsetPtr);
871 Offset->setAlignment(CGF.PointerAlignInBytes);
874 llvm::Value *CompletePtr = CGF.Builder.CreateBitCast(Ptr, CGF.Int8PtrTy);
875 CompletePtr = CGF.Builder.CreateInBoundsGEP(CompletePtr, Offset);
877 // If we're supposed to call the global delete, make sure we do so
878 // even if the destructor throws.
879 CGF.pushCallObjectDeleteCleanup(DE->getOperatorDelete(), CompletePtr,
883 // FIXME: Provide a source location here even though there's no
884 // CXXMemberCallExpr for dtor call.
885 CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting;
886 EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, /*CE=*/nullptr);
889 CGF.PopCleanupBlock();
892 void ItaniumCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) {
893 // void __cxa_rethrow();
895 llvm::FunctionType *FTy =
896 llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false);
898 llvm::Constant *Fn = CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow");
901 CGF.EmitNoreturnRuntimeCallOrInvoke(Fn, None);
903 CGF.EmitRuntimeCallOrInvoke(Fn);
906 static llvm::Constant *getItaniumDynamicCastFn(CodeGenFunction &CGF) {
907 // void *__dynamic_cast(const void *sub,
908 // const abi::__class_type_info *src,
909 // const abi::__class_type_info *dst,
910 // std::ptrdiff_t src2dst_offset);
912 llvm::Type *Int8PtrTy = CGF.Int8PtrTy;
913 llvm::Type *PtrDiffTy =
914 CGF.ConvertType(CGF.getContext().getPointerDiffType());
916 llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy };
918 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
920 // Mark the function as nounwind readonly.
921 llvm::Attribute::AttrKind FuncAttrs[] = { llvm::Attribute::NoUnwind,
922 llvm::Attribute::ReadOnly };
923 llvm::AttributeSet Attrs = llvm::AttributeSet::get(
924 CGF.getLLVMContext(), llvm::AttributeSet::FunctionIndex, FuncAttrs);
926 return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast", Attrs);
929 static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) {
930 // void __cxa_bad_cast();
931 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
932 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast");
935 /// \brief Compute the src2dst_offset hint as described in the
936 /// Itanium C++ ABI [2.9.7]
937 static CharUnits computeOffsetHint(ASTContext &Context,
938 const CXXRecordDecl *Src,
939 const CXXRecordDecl *Dst) {
940 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
941 /*DetectVirtual=*/false);
943 // If Dst is not derived from Src we can skip the whole computation below and
944 // return that Src is not a public base of Dst. Record all inheritance paths.
945 if (!Dst->isDerivedFrom(Src, Paths))
946 return CharUnits::fromQuantity(-2ULL);
948 unsigned NumPublicPaths = 0;
951 // Now walk all possible inheritance paths.
952 for (CXXBasePaths::paths_iterator I = Paths.begin(), E = Paths.end(); I != E;
954 if (I->Access != AS_public) // Ignore non-public inheritance.
959 for (CXXBasePath::iterator J = I->begin(), JE = I->end(); J != JE; ++J) {
960 // If the path contains a virtual base class we can't give any hint.
962 if (J->Base->isVirtual())
963 return CharUnits::fromQuantity(-1ULL);
965 if (NumPublicPaths > 1) // Won't use offsets, skip computation.
968 // Accumulate the base class offsets.
969 const ASTRecordLayout &L = Context.getASTRecordLayout(J->Class);
970 Offset += L.getBaseClassOffset(J->Base->getType()->getAsCXXRecordDecl());
974 // -2: Src is not a public base of Dst.
975 if (NumPublicPaths == 0)
976 return CharUnits::fromQuantity(-2ULL);
978 // -3: Src is a multiple public base type but never a virtual base type.
979 if (NumPublicPaths > 1)
980 return CharUnits::fromQuantity(-3ULL);
982 // Otherwise, the Src type is a unique public nonvirtual base type of Dst.
983 // Return the offset of Src from the origin of Dst.
987 static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) {
988 // void __cxa_bad_typeid();
989 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
991 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid");
994 bool ItaniumCXXABI::shouldTypeidBeNullChecked(bool IsDeref,
995 QualType SrcRecordTy) {
999 void ItaniumCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) {
1000 llvm::Value *Fn = getBadTypeidFn(CGF);
1001 CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn();
1002 CGF.Builder.CreateUnreachable();
1005 llvm::Value *ItaniumCXXABI::EmitTypeid(CodeGenFunction &CGF,
1006 QualType SrcRecordTy,
1007 llvm::Value *ThisPtr,
1008 llvm::Type *StdTypeInfoPtrTy) {
1009 llvm::Value *Value =
1010 CGF.GetVTablePtr(ThisPtr, StdTypeInfoPtrTy->getPointerTo());
1012 // Load the type info.
1013 Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL);
1014 return CGF.Builder.CreateLoad(Value);
1017 bool ItaniumCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
1018 QualType SrcRecordTy) {
1022 llvm::Value *ItaniumCXXABI::EmitDynamicCastCall(
1023 CodeGenFunction &CGF, llvm::Value *Value, QualType SrcRecordTy,
1024 QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) {
1025 llvm::Type *PtrDiffLTy =
1026 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1027 llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1029 llvm::Value *SrcRTTI =
1030 CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType());
1031 llvm::Value *DestRTTI =
1032 CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType());
1034 // Compute the offset hint.
1035 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
1036 const CXXRecordDecl *DestDecl = DestRecordTy->getAsCXXRecordDecl();
1037 llvm::Value *OffsetHint = llvm::ConstantInt::get(
1039 computeOffsetHint(CGF.getContext(), SrcDecl, DestDecl).getQuantity());
1041 // Emit the call to __dynamic_cast.
1042 Value = CGF.EmitCastToVoidPtr(Value);
1044 llvm::Value *args[] = {Value, SrcRTTI, DestRTTI, OffsetHint};
1045 Value = CGF.EmitNounwindRuntimeCall(getItaniumDynamicCastFn(CGF), args);
1046 Value = CGF.Builder.CreateBitCast(Value, DestLTy);
1048 /// C++ [expr.dynamic.cast]p9:
1049 /// A failed cast to reference type throws std::bad_cast
1050 if (DestTy->isReferenceType()) {
1051 llvm::BasicBlock *BadCastBlock =
1052 CGF.createBasicBlock("dynamic_cast.bad_cast");
1054 llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value);
1055 CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd);
1057 CGF.EmitBlock(BadCastBlock);
1058 EmitBadCastCall(CGF);
1064 llvm::Value *ItaniumCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF,
1066 QualType SrcRecordTy,
1068 llvm::Type *PtrDiffLTy =
1069 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1070 llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1072 // Get the vtable pointer.
1073 llvm::Value *VTable = CGF.GetVTablePtr(Value, PtrDiffLTy->getPointerTo());
1075 // Get the offset-to-top from the vtable.
1076 llvm::Value *OffsetToTop =
1077 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL);
1078 OffsetToTop = CGF.Builder.CreateLoad(OffsetToTop, "offset.to.top");
1080 // Finally, add the offset to the pointer.
1081 Value = CGF.EmitCastToVoidPtr(Value);
1082 Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop);
1084 return CGF.Builder.CreateBitCast(Value, DestLTy);
1087 bool ItaniumCXXABI::EmitBadCastCall(CodeGenFunction &CGF) {
1088 llvm::Value *Fn = getBadCastFn(CGF);
1089 CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn();
1090 CGF.Builder.CreateUnreachable();
1095 ItaniumCXXABI::GetVirtualBaseClassOffset(CodeGenFunction &CGF,
1097 const CXXRecordDecl *ClassDecl,
1098 const CXXRecordDecl *BaseClassDecl) {
1099 llvm::Value *VTablePtr = CGF.GetVTablePtr(This, CGM.Int8PtrTy);
1100 CharUnits VBaseOffsetOffset =
1101 CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(ClassDecl,
1104 llvm::Value *VBaseOffsetPtr =
1105 CGF.Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(),
1106 "vbase.offset.ptr");
1107 VBaseOffsetPtr = CGF.Builder.CreateBitCast(VBaseOffsetPtr,
1108 CGM.PtrDiffTy->getPointerTo());
1110 llvm::Value *VBaseOffset =
1111 CGF.Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
1116 void ItaniumCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) {
1117 // Just make sure we're in sync with TargetCXXABI.
1118 assert(CGM.getTarget().getCXXABI().hasConstructorVariants());
1120 // The constructor used for constructing this as a base class;
1121 // ignores virtual bases.
1122 CGM.EmitGlobal(GlobalDecl(D, Ctor_Base));
1124 // The constructor used for constructing this as a complete class;
1125 // constructs the virtual bases, then calls the base constructor.
1126 if (!D->getParent()->isAbstract()) {
1127 // We don't need to emit the complete ctor if the class is abstract.
1128 CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete));
1133 ItaniumCXXABI::buildStructorSignature(const CXXMethodDecl *MD, StructorType T,
1134 SmallVectorImpl<CanQualType> &ArgTys) {
1135 ASTContext &Context = getContext();
1137 // All parameters are already in place except VTT, which goes after 'this'.
1138 // These are Clang types, so we don't need to worry about sret yet.
1140 // Check if we need to add a VTT parameter (which has type void **).
1141 if (T == StructorType::Base && MD->getParent()->getNumVBases() != 0)
1142 ArgTys.insert(ArgTys.begin() + 1,
1143 Context.getPointerType(Context.VoidPtrTy));
1146 void ItaniumCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) {
1147 // The destructor used for destructing this as a base class; ignores
1149 CGM.EmitGlobal(GlobalDecl(D, Dtor_Base));
1151 // The destructor used for destructing this as a most-derived class;
1152 // call the base destructor and then destructs any virtual bases.
1153 CGM.EmitGlobal(GlobalDecl(D, Dtor_Complete));
1155 // The destructor in a virtual table is always a 'deleting'
1156 // destructor, which calls the complete destructor and then uses the
1157 // appropriate operator delete.
1159 CGM.EmitGlobal(GlobalDecl(D, Dtor_Deleting));
1162 void ItaniumCXXABI::addImplicitStructorParams(CodeGenFunction &CGF,
1164 FunctionArgList &Params) {
1165 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
1166 assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD));
1168 // Check if we need a VTT parameter as well.
1169 if (NeedsVTTParameter(CGF.CurGD)) {
1170 ASTContext &Context = getContext();
1172 // FIXME: avoid the fake decl
1173 QualType T = Context.getPointerType(Context.VoidPtrTy);
1174 ImplicitParamDecl *VTTDecl
1175 = ImplicitParamDecl::Create(Context, nullptr, MD->getLocation(),
1176 &Context.Idents.get("vtt"), T);
1177 Params.insert(Params.begin() + 1, VTTDecl);
1178 getStructorImplicitParamDecl(CGF) = VTTDecl;
1182 void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
1183 /// Initialize the 'this' slot.
1186 /// Initialize the 'vtt' slot if needed.
1187 if (getStructorImplicitParamDecl(CGF)) {
1188 getStructorImplicitParamValue(CGF) = CGF.Builder.CreateLoad(
1189 CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), "vtt");
1192 /// If this is a function that the ABI specifies returns 'this', initialize
1193 /// the return slot to 'this' at the start of the function.
1195 /// Unlike the setting of return types, this is done within the ABI
1196 /// implementation instead of by clients of CGCXXABI because:
1197 /// 1) getThisValue is currently protected
1198 /// 2) in theory, an ABI could implement 'this' returns some other way;
1199 /// HasThisReturn only specifies a contract, not the implementation
1200 if (HasThisReturn(CGF.CurGD))
1201 CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
1204 unsigned ItaniumCXXABI::addImplicitConstructorArgs(
1205 CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type,
1206 bool ForVirtualBase, bool Delegating, CallArgList &Args) {
1207 if (!NeedsVTTParameter(GlobalDecl(D, Type)))
1210 // Insert the implicit 'vtt' argument as the second argument.
1212 CGF.GetVTTParameter(GlobalDecl(D, Type), ForVirtualBase, Delegating);
1213 QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy);
1214 Args.insert(Args.begin() + 1,
1215 CallArg(RValue::get(VTT), VTTTy, /*needscopy=*/false));
1216 return 1; // Added one arg.
1219 void ItaniumCXXABI::EmitDestructorCall(CodeGenFunction &CGF,
1220 const CXXDestructorDecl *DD,
1221 CXXDtorType Type, bool ForVirtualBase,
1222 bool Delegating, llvm::Value *This) {
1223 GlobalDecl GD(DD, Type);
1224 llvm::Value *VTT = CGF.GetVTTParameter(GD, ForVirtualBase, Delegating);
1225 QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy);
1227 llvm::Value *Callee = nullptr;
1228 if (getContext().getLangOpts().AppleKext)
1229 Callee = CGF.BuildAppleKextVirtualDestructorCall(DD, Type, DD->getParent());
1232 Callee = CGM.getAddrOfCXXStructor(DD, getFromDtorType(Type));
1234 CGF.EmitCXXMemberOrOperatorCall(DD, Callee, ReturnValueSlot(), This, VTT,
1238 void ItaniumCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT,
1239 const CXXRecordDecl *RD) {
1240 llvm::GlobalVariable *VTable = getAddrOfVTable(RD, CharUnits());
1241 if (VTable->hasInitializer())
1244 ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext();
1245 const VTableLayout &VTLayout = VTContext.getVTableLayout(RD);
1246 llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD);
1247 llvm::Constant *RTTI =
1248 CGM.GetAddrOfRTTIDescriptor(CGM.getContext().getTagDeclType(RD));
1250 // Create and set the initializer.
1251 llvm::Constant *Init = CGVT.CreateVTableInitializer(
1252 RD, VTLayout.vtable_component_begin(), VTLayout.getNumVTableComponents(),
1253 VTLayout.vtable_thunk_begin(), VTLayout.getNumVTableThunks(), RTTI);
1254 VTable->setInitializer(Init);
1256 // Set the correct linkage.
1257 VTable->setLinkage(Linkage);
1259 // Set the right visibility.
1260 CGM.setGlobalVisibility(VTable, RD);
1262 // Use pointer alignment for the vtable. Otherwise we would align them based
1263 // on the size of the initializer which doesn't make sense as only single
1265 unsigned PAlign = CGM.getTarget().getPointerAlign(0);
1266 VTable->setAlignment(getContext().toCharUnitsFromBits(PAlign).getQuantity());
1268 // If this is the magic class __cxxabiv1::__fundamental_type_info,
1269 // we will emit the typeinfo for the fundamental types. This is the
1270 // same behaviour as GCC.
1271 const DeclContext *DC = RD->getDeclContext();
1272 if (RD->getIdentifier() &&
1273 RD->getIdentifier()->isStr("__fundamental_type_info") &&
1274 isa<NamespaceDecl>(DC) && cast<NamespaceDecl>(DC)->getIdentifier() &&
1275 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") &&
1276 DC->getParent()->isTranslationUnit())
1277 EmitFundamentalRTTIDescriptors();
1280 llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructor(
1281 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base,
1282 const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) {
1283 bool NeedsVTTParam = CGM.getCXXABI().NeedsVTTParameter(CGF.CurGD);
1284 NeedsVirtualOffset = (NeedsVTTParam && NearestVBase);
1286 llvm::Value *VTableAddressPoint;
1287 if (NeedsVTTParam && (Base.getBase()->getNumVBases() || NearestVBase)) {
1288 // Get the secondary vpointer index.
1289 uint64_t VirtualPointerIndex =
1290 CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base);
1293 llvm::Value *VTT = CGF.LoadCXXVTT();
1294 if (VirtualPointerIndex)
1295 VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex);
1297 // And load the address point from the VTT.
1298 VTableAddressPoint = CGF.Builder.CreateLoad(VTT);
1300 llvm::Constant *VTable =
1301 CGM.getCXXABI().getAddrOfVTable(VTableClass, CharUnits());
1302 uint64_t AddressPoint = CGM.getItaniumVTableContext()
1303 .getVTableLayout(VTableClass)
1304 .getAddressPoint(Base);
1305 VTableAddressPoint =
1306 CGF.Builder.CreateConstInBoundsGEP2_64(VTable, 0, AddressPoint);
1309 return VTableAddressPoint;
1312 llvm::Constant *ItaniumCXXABI::getVTableAddressPointForConstExpr(
1313 BaseSubobject Base, const CXXRecordDecl *VTableClass) {
1314 llvm::Constant *VTable = getAddrOfVTable(VTableClass, CharUnits());
1316 // Find the appropriate vtable within the vtable group.
1317 uint64_t AddressPoint = CGM.getItaniumVTableContext()
1318 .getVTableLayout(VTableClass)
1319 .getAddressPoint(Base);
1320 llvm::Value *Indices[] = {
1321 llvm::ConstantInt::get(CGM.Int64Ty, 0),
1322 llvm::ConstantInt::get(CGM.Int64Ty, AddressPoint)
1325 return llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Indices);
1328 llvm::GlobalVariable *ItaniumCXXABI::getAddrOfVTable(const CXXRecordDecl *RD,
1329 CharUnits VPtrOffset) {
1330 assert(VPtrOffset.isZero() && "Itanium ABI only supports zero vptr offsets");
1332 llvm::GlobalVariable *&VTable = VTables[RD];
1336 // Queue up this v-table for possible deferred emission.
1337 CGM.addDeferredVTable(RD);
1339 SmallString<256> OutName;
1340 llvm::raw_svector_ostream Out(OutName);
1341 getMangleContext().mangleCXXVTable(RD, Out);
1343 StringRef Name = OutName.str();
1345 ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext();
1346 llvm::ArrayType *ArrayType = llvm::ArrayType::get(
1347 CGM.Int8PtrTy, VTContext.getVTableLayout(RD).getNumVTableComponents());
1349 VTable = CGM.CreateOrReplaceCXXRuntimeVariable(
1350 Name, ArrayType, llvm::GlobalValue::ExternalLinkage);
1351 VTable->setUnnamedAddr(true);
1353 if (RD->hasAttr<DLLImportAttr>())
1354 VTable->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
1355 else if (RD->hasAttr<DLLExportAttr>())
1356 VTable->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
1361 llvm::Value *ItaniumCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF,
1365 GD = GD.getCanonicalDecl();
1366 Ty = Ty->getPointerTo()->getPointerTo();
1367 llvm::Value *VTable = CGF.GetVTablePtr(This, Ty);
1369 uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD);
1370 llvm::Value *VFuncPtr =
1371 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn");
1372 return CGF.Builder.CreateLoad(VFuncPtr);
1375 llvm::Value *ItaniumCXXABI::EmitVirtualDestructorCall(
1376 CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType,
1377 llvm::Value *This, const CXXMemberCallExpr *CE) {
1378 assert(CE == nullptr || CE->arg_begin() == CE->arg_end());
1379 assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete);
1381 const CGFunctionInfo *FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration(
1382 Dtor, getFromDtorType(DtorType));
1383 llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo);
1384 llvm::Value *Callee =
1385 getVirtualFunctionPointer(CGF, GlobalDecl(Dtor, DtorType), This, Ty);
1387 CGF.EmitCXXMemberOrOperatorCall(Dtor, Callee, ReturnValueSlot(), This,
1388 /*ImplicitParam=*/nullptr, QualType(), CE);
1392 void ItaniumCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) {
1393 CodeGenVTables &VTables = CGM.getVTables();
1394 llvm::GlobalVariable *VTT = VTables.GetAddrOfVTT(RD);
1395 VTables.EmitVTTDefinition(VTT, CGM.getVTableLinkage(RD), RD);
1398 static llvm::Value *performTypeAdjustment(CodeGenFunction &CGF,
1400 int64_t NonVirtualAdjustment,
1401 int64_t VirtualAdjustment,
1402 bool IsReturnAdjustment) {
1403 if (!NonVirtualAdjustment && !VirtualAdjustment)
1406 llvm::Type *Int8PtrTy = CGF.Int8PtrTy;
1407 llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy);
1409 if (NonVirtualAdjustment && !IsReturnAdjustment) {
1410 // Perform the non-virtual adjustment for a base-to-derived cast.
1411 V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment);
1414 if (VirtualAdjustment) {
1415 llvm::Type *PtrDiffTy =
1416 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1418 // Perform the virtual adjustment.
1419 llvm::Value *VTablePtrPtr =
1420 CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo());
1422 llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr);
1424 llvm::Value *OffsetPtr =
1425 CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
1427 OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo());
1429 // Load the adjustment offset from the vtable.
1430 llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr);
1432 // Adjust our pointer.
1433 V = CGF.Builder.CreateInBoundsGEP(V, Offset);
1436 if (NonVirtualAdjustment && IsReturnAdjustment) {
1437 // Perform the non-virtual adjustment for a derived-to-base cast.
1438 V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment);
1441 // Cast back to the original type.
1442 return CGF.Builder.CreateBitCast(V, Ptr->getType());
1445 llvm::Value *ItaniumCXXABI::performThisAdjustment(CodeGenFunction &CGF,
1447 const ThisAdjustment &TA) {
1448 return performTypeAdjustment(CGF, This, TA.NonVirtual,
1449 TA.Virtual.Itanium.VCallOffsetOffset,
1450 /*IsReturnAdjustment=*/false);
1454 ItaniumCXXABI::performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret,
1455 const ReturnAdjustment &RA) {
1456 return performTypeAdjustment(CGF, Ret, RA.NonVirtual,
1457 RA.Virtual.Itanium.VBaseOffsetOffset,
1458 /*IsReturnAdjustment=*/true);
1461 void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
1462 RValue RV, QualType ResultType) {
1463 if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
1464 return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
1466 // Destructor thunks in the ARM ABI have indeterminate results.
1468 cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType();
1469 RValue Undef = RValue::get(llvm::UndefValue::get(T));
1470 return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
1473 /************************** Array allocation cookies **************************/
1475 CharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) {
1476 // The array cookie is a size_t; pad that up to the element alignment.
1477 // The cookie is actually right-justified in that space.
1478 return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes),
1479 CGM.getContext().getTypeAlignInChars(elementType));
1482 llvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
1483 llvm::Value *NewPtr,
1484 llvm::Value *NumElements,
1485 const CXXNewExpr *expr,
1486 QualType ElementType) {
1487 assert(requiresArrayCookie(expr));
1489 unsigned AS = NewPtr->getType()->getPointerAddressSpace();
1491 ASTContext &Ctx = getContext();
1492 QualType SizeTy = Ctx.getSizeType();
1493 CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy);
1495 // The size of the cookie.
1496 CharUnits CookieSize =
1497 std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
1498 assert(CookieSize == getArrayCookieSizeImpl(ElementType));
1500 // Compute an offset to the cookie.
1501 llvm::Value *CookiePtr = NewPtr;
1502 CharUnits CookieOffset = CookieSize - SizeSize;
1503 if (!CookieOffset.isZero())
1504 CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr,
1505 CookieOffset.getQuantity());
1507 // Write the number of elements into the appropriate slot.
1508 llvm::Type *NumElementsTy = CGF.ConvertType(SizeTy)->getPointerTo(AS);
1509 llvm::Value *NumElementsPtr =
1510 CGF.Builder.CreateBitCast(CookiePtr, NumElementsTy);
1511 llvm::Instruction *SI = CGF.Builder.CreateStore(NumElements, NumElementsPtr);
1512 if (CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) && AS == 0 &&
1513 expr->getOperatorNew()->isReplaceableGlobalAllocationFunction()) {
1514 // The store to the CookiePtr does not need to be instrumented.
1515 CGM.getSanitizerMetadata()->disableSanitizerForInstruction(SI);
1516 llvm::FunctionType *FTy =
1517 llvm::FunctionType::get(CGM.VoidTy, NumElementsTy, false);
1519 CGM.CreateRuntimeFunction(FTy, "__asan_poison_cxx_array_cookie");
1520 CGF.Builder.CreateCall(F, NumElementsPtr);
1523 // Finally, compute a pointer to the actual data buffer by skipping
1524 // over the cookie completely.
1525 return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
1526 CookieSize.getQuantity());
1529 llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
1530 llvm::Value *allocPtr,
1531 CharUnits cookieSize) {
1532 // The element size is right-justified in the cookie.
1533 llvm::Value *numElementsPtr = allocPtr;
1534 CharUnits numElementsOffset =
1535 cookieSize - CharUnits::fromQuantity(CGF.SizeSizeInBytes);
1536 if (!numElementsOffset.isZero())
1538 CGF.Builder.CreateConstInBoundsGEP1_64(numElementsPtr,
1539 numElementsOffset.getQuantity());
1541 unsigned AS = allocPtr->getType()->getPointerAddressSpace();
1543 CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
1544 if (!CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) || AS != 0)
1545 return CGF.Builder.CreateLoad(numElementsPtr);
1546 // In asan mode emit a function call instead of a regular load and let the
1547 // run-time deal with it: if the shadow is properly poisoned return the
1548 // cookie, otherwise return 0 to avoid an infinite loop calling DTORs.
1549 // We can't simply ignore this load using nosanitize metadata because
1550 // the metadata may be lost.
1551 llvm::FunctionType *FTy =
1552 llvm::FunctionType::get(CGF.SizeTy, CGF.SizeTy->getPointerTo(0), false);
1554 CGM.CreateRuntimeFunction(FTy, "__asan_load_cxx_array_cookie");
1555 return CGF.Builder.CreateCall(F, numElementsPtr);
1558 CharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) {
1559 // ARM says that the cookie is always:
1560 // struct array_cookie {
1561 // std::size_t element_size; // element_size != 0
1562 // std::size_t element_count;
1564 // But the base ABI doesn't give anything an alignment greater than
1565 // 8, so we can dismiss this as typical ABI-author blindness to
1566 // actual language complexity and round up to the element alignment.
1567 return std::max(CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes),
1568 CGM.getContext().getTypeAlignInChars(elementType));
1571 llvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
1572 llvm::Value *newPtr,
1573 llvm::Value *numElements,
1574 const CXXNewExpr *expr,
1575 QualType elementType) {
1576 assert(requiresArrayCookie(expr));
1578 // NewPtr is a char*, but we generalize to arbitrary addrspaces.
1579 unsigned AS = newPtr->getType()->getPointerAddressSpace();
1581 // The cookie is always at the start of the buffer.
1582 llvm::Value *cookie = newPtr;
1584 // The first element is the element size.
1585 cookie = CGF.Builder.CreateBitCast(cookie, CGF.SizeTy->getPointerTo(AS));
1586 llvm::Value *elementSize = llvm::ConstantInt::get(CGF.SizeTy,
1587 getContext().getTypeSizeInChars(elementType).getQuantity());
1588 CGF.Builder.CreateStore(elementSize, cookie);
1590 // The second element is the element count.
1591 cookie = CGF.Builder.CreateConstInBoundsGEP1_32(cookie, 1);
1592 CGF.Builder.CreateStore(numElements, cookie);
1594 // Finally, compute a pointer to the actual data buffer by skipping
1595 // over the cookie completely.
1596 CharUnits cookieSize = ARMCXXABI::getArrayCookieSizeImpl(elementType);
1597 return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr,
1598 cookieSize.getQuantity());
1601 llvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
1602 llvm::Value *allocPtr,
1603 CharUnits cookieSize) {
1604 // The number of elements is at offset sizeof(size_t) relative to
1605 // the allocated pointer.
1606 llvm::Value *numElementsPtr
1607 = CGF.Builder.CreateConstInBoundsGEP1_64(allocPtr, CGF.SizeSizeInBytes);
1609 unsigned AS = allocPtr->getType()->getPointerAddressSpace();
1611 CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
1612 return CGF.Builder.CreateLoad(numElementsPtr);
1615 /*********************** Static local initialization **************************/
1617 static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM,
1618 llvm::PointerType *GuardPtrTy) {
1619 // int __cxa_guard_acquire(__guard *guard_object);
1620 llvm::FunctionType *FTy =
1621 llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy),
1622 GuardPtrTy, /*isVarArg=*/false);
1623 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire",
1624 llvm::AttributeSet::get(CGM.getLLVMContext(),
1625 llvm::AttributeSet::FunctionIndex,
1626 llvm::Attribute::NoUnwind));
1629 static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM,
1630 llvm::PointerType *GuardPtrTy) {
1631 // void __cxa_guard_release(__guard *guard_object);
1632 llvm::FunctionType *FTy =
1633 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
1634 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release",
1635 llvm::AttributeSet::get(CGM.getLLVMContext(),
1636 llvm::AttributeSet::FunctionIndex,
1637 llvm::Attribute::NoUnwind));
1640 static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM,
1641 llvm::PointerType *GuardPtrTy) {
1642 // void __cxa_guard_abort(__guard *guard_object);
1643 llvm::FunctionType *FTy =
1644 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
1645 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort",
1646 llvm::AttributeSet::get(CGM.getLLVMContext(),
1647 llvm::AttributeSet::FunctionIndex,
1648 llvm::Attribute::NoUnwind));
1652 struct CallGuardAbort : EHScopeStack::Cleanup {
1653 llvm::GlobalVariable *Guard;
1654 CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
1656 void Emit(CodeGenFunction &CGF, Flags flags) override {
1657 CGF.EmitNounwindRuntimeCall(getGuardAbortFn(CGF.CGM, Guard->getType()),
1663 /// The ARM code here follows the Itanium code closely enough that we
1664 /// just special-case it at particular places.
1665 void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
1667 llvm::GlobalVariable *var,
1668 bool shouldPerformInit) {
1669 CGBuilderTy &Builder = CGF.Builder;
1671 // We only need to use thread-safe statics for local non-TLS variables;
1672 // global initialization is always single-threaded.
1673 bool threadsafe = getContext().getLangOpts().ThreadsafeStatics &&
1674 D.isLocalVarDecl() && !D.getTLSKind();
1676 // If we have a global variable with internal linkage and thread-safe statics
1677 // are disabled, we can just let the guard variable be of type i8.
1678 bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage();
1680 llvm::IntegerType *guardTy;
1681 if (useInt8GuardVariable) {
1682 guardTy = CGF.Int8Ty;
1684 // Guard variables are 64 bits in the generic ABI and size width on ARM
1685 // (i.e. 32-bit on AArch32, 64-bit on AArch64).
1686 guardTy = (UseARMGuardVarABI ? CGF.SizeTy : CGF.Int64Ty);
1688 llvm::PointerType *guardPtrTy = guardTy->getPointerTo();
1690 // Create the guard variable if we don't already have it (as we
1691 // might if we're double-emitting this function body).
1692 llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D);
1694 // Mangle the name for the guard.
1695 SmallString<256> guardName;
1697 llvm::raw_svector_ostream out(guardName);
1698 getMangleContext().mangleStaticGuardVariable(&D, out);
1702 // Create the guard variable with a zero-initializer.
1703 // Just absorb linkage and visibility from the guarded variable.
1704 guard = new llvm::GlobalVariable(CGM.getModule(), guardTy,
1705 false, var->getLinkage(),
1706 llvm::ConstantInt::get(guardTy, 0),
1708 guard->setVisibility(var->getVisibility());
1709 // If the variable is thread-local, so is its guard variable.
1710 guard->setThreadLocalMode(var->getThreadLocalMode());
1712 // The ABI says: It is suggested that it be emitted in the same COMDAT group
1713 // as the associated data object
1714 llvm::Comdat *C = var->getComdat();
1715 if (!D.isLocalVarDecl() && C) {
1716 guard->setComdat(C);
1717 CGF.CurFn->setComdat(C);
1718 } else if (CGM.supportsCOMDAT() && guard->isWeakForLinker()) {
1719 guard->setComdat(CGM.getModule().getOrInsertComdat(guard->getName()));
1722 CGM.setStaticLocalDeclGuardAddress(&D, guard);
1725 // Test whether the variable has completed initialization.
1727 // Itanium C++ ABI 3.3.2:
1728 // The following is pseudo-code showing how these functions can be used:
1729 // if (obj_guard.first_byte == 0) {
1730 // if ( __cxa_guard_acquire (&obj_guard) ) {
1732 // ... initialize the object ...;
1734 // __cxa_guard_abort (&obj_guard);
1737 // ... queue object destructor with __cxa_atexit() ...;
1738 // __cxa_guard_release (&obj_guard);
1742 // Load the first byte of the guard variable.
1743 llvm::LoadInst *LI =
1744 Builder.CreateLoad(Builder.CreateBitCast(guard, CGM.Int8PtrTy));
1745 LI->setAlignment(1);
1748 // An implementation supporting thread-safety on multiprocessor
1749 // systems must also guarantee that references to the initialized
1750 // object do not occur before the load of the initialization flag.
1752 // In LLVM, we do this by marking the load Acquire.
1754 LI->setAtomic(llvm::Acquire);
1756 // For ARM, we should only check the first bit, rather than the entire byte:
1758 // ARM C++ ABI 3.2.3.1:
1759 // To support the potential use of initialization guard variables
1760 // as semaphores that are the target of ARM SWP and LDREX/STREX
1761 // synchronizing instructions we define a static initialization
1762 // guard variable to be a 4-byte aligned, 4-byte word with the
1763 // following inline access protocol.
1764 // #define INITIALIZED 1
1765 // if ((obj_guard & INITIALIZED) != INITIALIZED) {
1766 // if (__cxa_guard_acquire(&obj_guard))
1770 // and similarly for ARM64:
1772 // ARM64 C++ ABI 3.2.2:
1773 // This ABI instead only specifies the value bit 0 of the static guard
1774 // variable; all other bits are platform defined. Bit 0 shall be 0 when the
1775 // variable is not initialized and 1 when it is.
1777 (UseARMGuardVarABI && !useInt8GuardVariable)
1778 ? Builder.CreateAnd(LI, llvm::ConstantInt::get(CGM.Int8Ty, 1))
1780 llvm::Value *isInitialized = Builder.CreateIsNull(V, "guard.uninitialized");
1782 llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check");
1783 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
1785 // Check if the first byte of the guard variable is zero.
1786 Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock);
1788 CGF.EmitBlock(InitCheckBlock);
1790 // Variables used when coping with thread-safe statics and exceptions.
1792 // Call __cxa_guard_acquire.
1794 = CGF.EmitNounwindRuntimeCall(getGuardAcquireFn(CGM, guardPtrTy), guard);
1796 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
1798 Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
1799 InitBlock, EndBlock);
1801 // Call __cxa_guard_abort along the exceptional edge.
1802 CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard);
1804 CGF.EmitBlock(InitBlock);
1807 // Emit the initializer and add a global destructor if appropriate.
1808 CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit);
1811 // Pop the guard-abort cleanup if we pushed one.
1812 CGF.PopCleanupBlock();
1814 // Call __cxa_guard_release. This cannot throw.
1815 CGF.EmitNounwindRuntimeCall(getGuardReleaseFn(CGM, guardPtrTy), guard);
1817 Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guard);
1820 CGF.EmitBlock(EndBlock);
1823 /// Register a global destructor using __cxa_atexit.
1824 static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF,
1825 llvm::Constant *dtor,
1826 llvm::Constant *addr,
1828 const char *Name = "__cxa_atexit";
1830 const llvm::Triple &T = CGF.getTarget().getTriple();
1831 Name = T.isMacOSX() ? "_tlv_atexit" : "__cxa_thread_atexit";
1834 // We're assuming that the destructor function is something we can
1835 // reasonably call with the default CC. Go ahead and cast it to the
1837 llvm::Type *dtorTy =
1838 llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo();
1840 // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d);
1841 llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy };
1842 llvm::FunctionType *atexitTy =
1843 llvm::FunctionType::get(CGF.IntTy, paramTys, false);
1845 // Fetch the actual function.
1846 llvm::Constant *atexit = CGF.CGM.CreateRuntimeFunction(atexitTy, Name);
1847 if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit))
1848 fn->setDoesNotThrow();
1850 // Create a variable that binds the atexit to this shared object.
1851 llvm::Constant *handle =
1852 CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle");
1854 llvm::Value *args[] = {
1855 llvm::ConstantExpr::getBitCast(dtor, dtorTy),
1856 llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy),
1859 CGF.EmitNounwindRuntimeCall(atexit, args);
1862 /// Register a global destructor as best as we know how.
1863 void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF,
1865 llvm::Constant *dtor,
1866 llvm::Constant *addr) {
1867 // Use __cxa_atexit if available.
1868 if (CGM.getCodeGenOpts().CXAAtExit)
1869 return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr, D.getTLSKind());
1872 CGM.ErrorUnsupported(&D, "non-trivial TLS destruction");
1874 // In Apple kexts, we want to add a global destructor entry.
1875 // FIXME: shouldn't this be guarded by some variable?
1876 if (CGM.getLangOpts().AppleKext) {
1877 // Generate a global destructor entry.
1878 return CGM.AddCXXDtorEntry(dtor, addr);
1881 CGF.registerGlobalDtorWithAtExit(D, dtor, addr);
1884 static bool isThreadWrapperReplaceable(const VarDecl *VD,
1885 CodeGen::CodeGenModule &CGM) {
1886 assert(!VD->isStaticLocal() && "static local VarDecls don't need wrappers!");
1887 // OS X prefers to have references to thread local variables to go through
1888 // the thread wrapper instead of directly referencing the backing variable.
1889 return VD->getTLSKind() == VarDecl::TLS_Dynamic &&
1890 CGM.getTarget().getTriple().isMacOSX();
1893 /// Get the appropriate linkage for the wrapper function. This is essentially
1894 /// the weak form of the variable's linkage; every translation unit which needs
1895 /// the wrapper emits a copy, and we want the linker to merge them.
1896 static llvm::GlobalValue::LinkageTypes
1897 getThreadLocalWrapperLinkage(const VarDecl *VD, CodeGen::CodeGenModule &CGM) {
1898 llvm::GlobalValue::LinkageTypes VarLinkage =
1899 CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false);
1901 // For internal linkage variables, we don't need an external or weak wrapper.
1902 if (llvm::GlobalValue::isLocalLinkage(VarLinkage))
1905 // If the thread wrapper is replaceable, give it appropriate linkage.
1906 if (isThreadWrapperReplaceable(VD, CGM)) {
1907 if (llvm::GlobalVariable::isLinkOnceLinkage(VarLinkage) ||
1908 llvm::GlobalVariable::isWeakODRLinkage(VarLinkage))
1909 return llvm::GlobalVariable::WeakAnyLinkage;
1912 return llvm::GlobalValue::WeakODRLinkage;
1916 ItaniumCXXABI::getOrCreateThreadLocalWrapper(const VarDecl *VD,
1918 // Mangle the name for the thread_local wrapper function.
1919 SmallString<256> WrapperName;
1921 llvm::raw_svector_ostream Out(WrapperName);
1922 getMangleContext().mangleItaniumThreadLocalWrapper(VD, Out);
1926 if (llvm::Value *V = CGM.getModule().getNamedValue(WrapperName))
1927 return cast<llvm::Function>(V);
1929 llvm::Type *RetTy = Val->getType();
1930 if (VD->getType()->isReferenceType())
1931 RetTy = RetTy->getPointerElementType();
1933 llvm::FunctionType *FnTy = llvm::FunctionType::get(RetTy, false);
1934 llvm::Function *Wrapper =
1935 llvm::Function::Create(FnTy, getThreadLocalWrapperLinkage(VD, CGM),
1936 WrapperName.str(), &CGM.getModule());
1937 // Always resolve references to the wrapper at link time.
1938 if (!Wrapper->hasLocalLinkage() && !isThreadWrapperReplaceable(VD, CGM))
1939 Wrapper->setVisibility(llvm::GlobalValue::HiddenVisibility);
1943 void ItaniumCXXABI::EmitThreadLocalInitFuncs(
1945 ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>>
1946 CXXThreadLocals, ArrayRef<llvm::Function *> CXXThreadLocalInits,
1947 ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) {
1948 llvm::Function *InitFunc = nullptr;
1949 if (!CXXThreadLocalInits.empty()) {
1950 // Generate a guarded initialization function.
1951 llvm::FunctionType *FTy =
1952 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
1953 InitFunc = CGM.CreateGlobalInitOrDestructFunction(FTy, "__tls_init",
1956 llvm::GlobalVariable *Guard = new llvm::GlobalVariable(
1957 CGM.getModule(), CGM.Int8Ty, /*isConstant=*/false,
1958 llvm::GlobalVariable::InternalLinkage,
1959 llvm::ConstantInt::get(CGM.Int8Ty, 0), "__tls_guard");
1960 Guard->setThreadLocal(true);
1961 CodeGenFunction(CGM)
1962 .GenerateCXXGlobalInitFunc(InitFunc, CXXThreadLocalInits, Guard);
1964 for (unsigned I = 0, N = CXXThreadLocals.size(); I != N; ++I) {
1965 const VarDecl *VD = CXXThreadLocals[I].first;
1966 llvm::GlobalVariable *Var = CXXThreadLocals[I].second;
1968 // Some targets require that all access to thread local variables go through
1969 // the thread wrapper. This means that we cannot attempt to create a thread
1970 // wrapper or a thread helper.
1971 if (isThreadWrapperReplaceable(VD, CGM) && !VD->hasDefinition())
1974 // Mangle the name for the thread_local initialization function.
1975 SmallString<256> InitFnName;
1977 llvm::raw_svector_ostream Out(InitFnName);
1978 getMangleContext().mangleItaniumThreadLocalInit(VD, Out);
1982 // If we have a definition for the variable, emit the initialization
1983 // function as an alias to the global Init function (if any). Otherwise,
1984 // produce a declaration of the initialization function.
1985 llvm::GlobalValue *Init = nullptr;
1986 bool InitIsInitFunc = false;
1987 if (VD->hasDefinition()) {
1988 InitIsInitFunc = true;
1990 Init = llvm::GlobalAlias::create(Var->getLinkage(), InitFnName.str(),
1993 // Emit a weak global function referring to the initialization function.
1994 // This function will not exist if the TU defining the thread_local
1995 // variable in question does not need any dynamic initialization for
1996 // its thread_local variables.
1997 llvm::FunctionType *FnTy = llvm::FunctionType::get(CGM.VoidTy, false);
1998 Init = llvm::Function::Create(
1999 FnTy, llvm::GlobalVariable::ExternalWeakLinkage, InitFnName.str(),
2004 Init->setVisibility(Var->getVisibility());
2006 llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Var);
2007 llvm::LLVMContext &Context = CGM.getModule().getContext();
2008 llvm::BasicBlock *Entry = llvm::BasicBlock::Create(Context, "", Wrapper);
2009 CGBuilderTy Builder(Entry);
2010 if (InitIsInitFunc) {
2012 Builder.CreateCall(Init);
2014 // Don't know whether we have an init function. Call it if it exists.
2015 llvm::Value *Have = Builder.CreateIsNotNull(Init);
2016 llvm::BasicBlock *InitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
2017 llvm::BasicBlock *ExitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
2018 Builder.CreateCondBr(Have, InitBB, ExitBB);
2020 Builder.SetInsertPoint(InitBB);
2021 Builder.CreateCall(Init);
2022 Builder.CreateBr(ExitBB);
2024 Builder.SetInsertPoint(ExitBB);
2027 // For a reference, the result of the wrapper function is a pointer to
2028 // the referenced object.
2029 llvm::Value *Val = Var;
2030 if (VD->getType()->isReferenceType()) {
2031 llvm::LoadInst *LI = Builder.CreateLoad(Val);
2032 LI->setAlignment(CGM.getContext().getDeclAlign(VD).getQuantity());
2035 if (Val->getType() != Wrapper->getReturnType())
2036 Val = Builder.CreatePointerBitCastOrAddrSpaceCast(
2037 Val, Wrapper->getReturnType(), "");
2038 Builder.CreateRet(Val);
2042 LValue ItaniumCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
2044 QualType LValType) {
2045 QualType T = VD->getType();
2046 llvm::Type *Ty = CGF.getTypes().ConvertTypeForMem(T);
2047 llvm::Value *Val = CGF.CGM.GetAddrOfGlobalVar(VD, Ty);
2048 llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Val);
2050 Val = CGF.Builder.CreateCall(Wrapper);
2053 if (VD->getType()->isReferenceType())
2054 LV = CGF.MakeNaturalAlignAddrLValue(Val, LValType);
2056 LV = CGF.MakeAddrLValue(Val, LValType, CGF.getContext().getDeclAlign(VD));
2057 // FIXME: need setObjCGCLValueClass?
2061 /// Return whether the given global decl needs a VTT parameter, which it does
2062 /// if it's a base constructor or destructor with virtual bases.
2063 bool ItaniumCXXABI::NeedsVTTParameter(GlobalDecl GD) {
2064 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2066 // We don't have any virtual bases, just return early.
2067 if (!MD->getParent()->getNumVBases())
2070 // Check if we have a base constructor.
2071 if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base)
2074 // Check if we have a base destructor.
2075 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
2082 class ItaniumRTTIBuilder {
2083 CodeGenModule &CGM; // Per-module state.
2084 llvm::LLVMContext &VMContext;
2085 const ItaniumCXXABI &CXXABI; // Per-module state.
2087 /// Fields - The fields of the RTTI descriptor currently being built.
2088 SmallVector<llvm::Constant *, 16> Fields;
2090 /// GetAddrOfTypeName - Returns the mangled type name of the given type.
2091 llvm::GlobalVariable *
2092 GetAddrOfTypeName(QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage);
2094 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
2095 /// descriptor of the given type.
2096 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
2098 /// BuildVTablePointer - Build the vtable pointer for the given type.
2099 void BuildVTablePointer(const Type *Ty);
2101 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
2102 /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
2103 void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
2105 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
2106 /// classes with bases that do not satisfy the abi::__si_class_type_info
2107 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
2108 void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
2110 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
2111 /// for pointer types.
2112 void BuildPointerTypeInfo(QualType PointeeTy);
2114 /// BuildObjCObjectTypeInfo - Build the appropriate kind of
2115 /// type_info for an object type.
2116 void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty);
2118 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
2119 /// struct, used for member pointer types.
2120 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
2123 ItaniumRTTIBuilder(const ItaniumCXXABI &ABI)
2124 : CGM(ABI.CGM), VMContext(CGM.getModule().getContext()), CXXABI(ABI) {}
2126 // Pointer type info flags.
2128 /// PTI_Const - Type has const qualifier.
2131 /// PTI_Volatile - Type has volatile qualifier.
2134 /// PTI_Restrict - Type has restrict qualifier.
2137 /// PTI_Incomplete - Type is incomplete.
2138 PTI_Incomplete = 0x8,
2140 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
2141 /// (in pointer to member).
2142 PTI_ContainingClassIncomplete = 0x10
2145 // VMI type info flags.
2147 /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
2148 VMI_NonDiamondRepeat = 0x1,
2150 /// VMI_DiamondShaped - Class is diamond shaped.
2151 VMI_DiamondShaped = 0x2
2154 // Base class type info flags.
2156 /// BCTI_Virtual - Base class is virtual.
2159 /// BCTI_Public - Base class is public.
2163 /// BuildTypeInfo - Build the RTTI type info struct for the given type.
2165 /// \param Force - true to force the creation of this RTTI value
2166 llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false);
2170 llvm::GlobalVariable *ItaniumRTTIBuilder::GetAddrOfTypeName(
2171 QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage) {
2172 SmallString<256> OutName;
2173 llvm::raw_svector_ostream Out(OutName);
2174 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
2176 StringRef Name = OutName.str();
2178 // We know that the mangled name of the type starts at index 4 of the
2179 // mangled name of the typename, so we can just index into it in order to
2180 // get the mangled name of the type.
2181 llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext,
2184 llvm::GlobalVariable *GV =
2185 CGM.CreateOrReplaceCXXRuntimeVariable(Name, Init->getType(), Linkage);
2187 GV->setInitializer(Init);
2193 ItaniumRTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
2194 // Mangle the RTTI name.
2195 SmallString<256> OutName;
2196 llvm::raw_svector_ostream Out(OutName);
2197 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
2199 StringRef Name = OutName.str();
2201 // Look for an existing global.
2202 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
2205 // Create a new global variable.
2206 GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy,
2208 llvm::GlobalValue::ExternalLinkage, nullptr,
2210 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
2211 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
2212 if (RD->hasAttr<DLLImportAttr>())
2213 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
2217 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
2220 /// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
2221 /// info for that type is defined in the standard library.
2222 static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
2223 // Itanium C++ ABI 2.9.2:
2224 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
2225 // the run-time support library. Specifically, the run-time support
2226 // library should contain type_info objects for the types X, X* and
2227 // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char,
2228 // unsigned char, signed char, short, unsigned short, int, unsigned int,
2229 // long, unsigned long, long long, unsigned long long, float, double,
2230 // long double, char16_t, char32_t, and the IEEE 754r decimal and
2231 // half-precision floating point types.
2232 switch (Ty->getKind()) {
2233 case BuiltinType::Void:
2234 case BuiltinType::NullPtr:
2235 case BuiltinType::Bool:
2236 case BuiltinType::WChar_S:
2237 case BuiltinType::WChar_U:
2238 case BuiltinType::Char_U:
2239 case BuiltinType::Char_S:
2240 case BuiltinType::UChar:
2241 case BuiltinType::SChar:
2242 case BuiltinType::Short:
2243 case BuiltinType::UShort:
2244 case BuiltinType::Int:
2245 case BuiltinType::UInt:
2246 case BuiltinType::Long:
2247 case BuiltinType::ULong:
2248 case BuiltinType::LongLong:
2249 case BuiltinType::ULongLong:
2250 case BuiltinType::Half:
2251 case BuiltinType::Float:
2252 case BuiltinType::Double:
2253 case BuiltinType::LongDouble:
2254 case BuiltinType::Char16:
2255 case BuiltinType::Char32:
2256 case BuiltinType::Int128:
2257 case BuiltinType::UInt128:
2258 case BuiltinType::OCLImage1d:
2259 case BuiltinType::OCLImage1dArray:
2260 case BuiltinType::OCLImage1dBuffer:
2261 case BuiltinType::OCLImage2d:
2262 case BuiltinType::OCLImage2dArray:
2263 case BuiltinType::OCLImage3d:
2264 case BuiltinType::OCLSampler:
2265 case BuiltinType::OCLEvent:
2268 case BuiltinType::Dependent:
2269 #define BUILTIN_TYPE(Id, SingletonId)
2270 #define PLACEHOLDER_TYPE(Id, SingletonId) \
2271 case BuiltinType::Id:
2272 #include "clang/AST/BuiltinTypes.def"
2273 llvm_unreachable("asking for RRTI for a placeholder type!");
2275 case BuiltinType::ObjCId:
2276 case BuiltinType::ObjCClass:
2277 case BuiltinType::ObjCSel:
2278 llvm_unreachable("FIXME: Objective-C types are unsupported!");
2281 llvm_unreachable("Invalid BuiltinType Kind!");
2284 static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
2285 QualType PointeeTy = PointerTy->getPointeeType();
2286 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
2290 // Check the qualifiers.
2291 Qualifiers Quals = PointeeTy.getQualifiers();
2292 Quals.removeConst();
2297 return TypeInfoIsInStandardLibrary(BuiltinTy);
2300 /// IsStandardLibraryRTTIDescriptor - Returns whether the type
2301 /// information for the given type exists in the standard library.
2302 static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
2303 // Type info for builtin types is defined in the standard library.
2304 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
2305 return TypeInfoIsInStandardLibrary(BuiltinTy);
2307 // Type info for some pointer types to builtin types is defined in the
2308 // standard library.
2309 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
2310 return TypeInfoIsInStandardLibrary(PointerTy);
2315 /// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
2316 /// the given type exists somewhere else, and that we should not emit the type
2317 /// information in this translation unit. Assumes that it is not a
2318 /// standard-library type.
2319 static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM,
2321 ASTContext &Context = CGM.getContext();
2323 // If RTTI is disabled, assume it might be disabled in the
2324 // translation unit that defines any potential key function, too.
2325 if (!Context.getLangOpts().RTTI) return false;
2327 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
2328 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
2329 if (!RD->hasDefinition())
2332 if (!RD->isDynamicClass())
2335 // FIXME: this may need to be reconsidered if the key function
2337 if (CGM.getVTables().isVTableExternal(RD))
2340 if (RD->hasAttr<DLLImportAttr>())
2347 /// IsIncompleteClassType - Returns whether the given record type is incomplete.
2348 static bool IsIncompleteClassType(const RecordType *RecordTy) {
2349 return !RecordTy->getDecl()->isCompleteDefinition();
2352 /// ContainsIncompleteClassType - Returns whether the given type contains an
2353 /// incomplete class type. This is true if
2355 /// * The given type is an incomplete class type.
2356 /// * The given type is a pointer type whose pointee type contains an
2357 /// incomplete class type.
2358 /// * The given type is a member pointer type whose class is an incomplete
2360 /// * The given type is a member pointer type whoise pointee type contains an
2361 /// incomplete class type.
2362 /// is an indirect or direct pointer to an incomplete class type.
2363 static bool ContainsIncompleteClassType(QualType Ty) {
2364 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
2365 if (IsIncompleteClassType(RecordTy))
2369 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
2370 return ContainsIncompleteClassType(PointerTy->getPointeeType());
2372 if (const MemberPointerType *MemberPointerTy =
2373 dyn_cast<MemberPointerType>(Ty)) {
2374 // Check if the class type is incomplete.
2375 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
2376 if (IsIncompleteClassType(ClassType))
2379 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
2385 // CanUseSingleInheritance - Return whether the given record decl has a "single,
2386 // public, non-virtual base at offset zero (i.e. the derived class is dynamic
2387 // iff the base is)", according to Itanium C++ ABI, 2.95p6b.
2388 static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
2389 // Check the number of bases.
2390 if (RD->getNumBases() != 1)
2394 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
2396 // Check that the base is not virtual.
2397 if (Base->isVirtual())
2400 // Check that the base is public.
2401 if (Base->getAccessSpecifier() != AS_public)
2404 // Check that the class is dynamic iff the base is.
2405 const CXXRecordDecl *BaseDecl =
2406 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
2407 if (!BaseDecl->isEmpty() &&
2408 BaseDecl->isDynamicClass() != RD->isDynamicClass())
2414 void ItaniumRTTIBuilder::BuildVTablePointer(const Type *Ty) {
2415 // abi::__class_type_info.
2416 static const char * const ClassTypeInfo =
2417 "_ZTVN10__cxxabiv117__class_type_infoE";
2418 // abi::__si_class_type_info.
2419 static const char * const SIClassTypeInfo =
2420 "_ZTVN10__cxxabiv120__si_class_type_infoE";
2421 // abi::__vmi_class_type_info.
2422 static const char * const VMIClassTypeInfo =
2423 "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
2425 const char *VTableName = nullptr;
2427 switch (Ty->getTypeClass()) {
2428 #define TYPE(Class, Base)
2429 #define ABSTRACT_TYPE(Class, Base)
2430 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
2431 #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2432 #define DEPENDENT_TYPE(Class, Base) case Type::Class:
2433 #include "clang/AST/TypeNodes.def"
2434 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
2436 case Type::LValueReference:
2437 case Type::RValueReference:
2438 llvm_unreachable("References shouldn't get here");
2441 llvm_unreachable("Undeduced auto type shouldn't get here");
2444 // GCC treats vector and complex types as fundamental types.
2446 case Type::ExtVector:
2449 // FIXME: GCC treats block pointers as fundamental types?!
2450 case Type::BlockPointer:
2451 // abi::__fundamental_type_info.
2452 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
2455 case Type::ConstantArray:
2456 case Type::IncompleteArray:
2457 case Type::VariableArray:
2458 // abi::__array_type_info.
2459 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
2462 case Type::FunctionNoProto:
2463 case Type::FunctionProto:
2464 // abi::__function_type_info.
2465 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
2469 // abi::__enum_type_info.
2470 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
2473 case Type::Record: {
2474 const CXXRecordDecl *RD =
2475 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
2477 if (!RD->hasDefinition() || !RD->getNumBases()) {
2478 VTableName = ClassTypeInfo;
2479 } else if (CanUseSingleInheritance(RD)) {
2480 VTableName = SIClassTypeInfo;
2482 VTableName = VMIClassTypeInfo;
2488 case Type::ObjCObject:
2489 // Ignore protocol qualifiers.
2490 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
2492 // Handle id and Class.
2493 if (isa<BuiltinType>(Ty)) {
2494 VTableName = ClassTypeInfo;
2498 assert(isa<ObjCInterfaceType>(Ty));
2501 case Type::ObjCInterface:
2502 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
2503 VTableName = SIClassTypeInfo;
2505 VTableName = ClassTypeInfo;
2509 case Type::ObjCObjectPointer:
2511 // abi::__pointer_type_info.
2512 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
2515 case Type::MemberPointer:
2516 // abi::__pointer_to_member_type_info.
2517 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
2521 llvm::Constant *VTable =
2522 CGM.getModule().getOrInsertGlobal(VTableName, CGM.Int8PtrTy);
2524 llvm::Type *PtrDiffTy =
2525 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
2527 // The vtable address point is 2.
2528 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
2529 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Two);
2530 VTable = llvm::ConstantExpr::getBitCast(VTable, CGM.Int8PtrTy);
2532 Fields.push_back(VTable);
2535 /// \brief Return the linkage that the type info and type info name constants
2536 /// should have for the given type.
2537 static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(CodeGenModule &CGM,
2539 // Itanium C++ ABI 2.9.5p7:
2540 // In addition, it and all of the intermediate abi::__pointer_type_info
2541 // structs in the chain down to the abi::__class_type_info for the
2542 // incomplete class type must be prevented from resolving to the
2543 // corresponding type_info structs for the complete class type, possibly
2544 // by making them local static objects. Finally, a dummy class RTTI is
2545 // generated for the incomplete type that will not resolve to the final
2546 // complete class RTTI (because the latter need not exist), possibly by
2547 // making it a local static object.
2548 if (ContainsIncompleteClassType(Ty))
2549 return llvm::GlobalValue::InternalLinkage;
2551 switch (Ty->getLinkage()) {
2553 case InternalLinkage:
2554 case UniqueExternalLinkage:
2555 return llvm::GlobalValue::InternalLinkage;
2557 case VisibleNoLinkage:
2558 case ExternalLinkage:
2559 if (!CGM.getLangOpts().RTTI) {
2560 // RTTI is not enabled, which means that this type info struct is going
2561 // to be used for exception handling. Give it linkonce_odr linkage.
2562 return llvm::GlobalValue::LinkOnceODRLinkage;
2565 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
2566 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
2567 if (RD->hasAttr<WeakAttr>())
2568 return llvm::GlobalValue::WeakODRLinkage;
2569 if (RD->isDynamicClass())
2570 return CGM.getVTableLinkage(RD);
2573 return llvm::GlobalValue::LinkOnceODRLinkage;
2576 llvm_unreachable("Invalid linkage!");
2579 llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
2580 // We want to operate on the canonical type.
2581 Ty = CGM.getContext().getCanonicalType(Ty);
2583 // Check if we've already emitted an RTTI descriptor for this type.
2584 SmallString<256> OutName;
2585 llvm::raw_svector_ostream Out(OutName);
2586 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
2588 StringRef Name = OutName.str();
2590 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
2591 if (OldGV && !OldGV->isDeclaration()) {
2592 assert(!OldGV->hasAvailableExternallyLinkage() &&
2593 "available_externally typeinfos not yet implemented");
2595 return llvm::ConstantExpr::getBitCast(OldGV, CGM.Int8PtrTy);
2598 // Check if there is already an external RTTI descriptor for this type.
2599 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
2600 if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
2601 return GetAddrOfExternalRTTIDescriptor(Ty);
2603 // Emit the standard library with external linkage.
2604 llvm::GlobalVariable::LinkageTypes Linkage;
2606 Linkage = llvm::GlobalValue::ExternalLinkage;
2608 Linkage = getTypeInfoLinkage(CGM, Ty);
2610 // Add the vtable pointer.
2611 BuildVTablePointer(cast<Type>(Ty));
2614 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
2615 llvm::Constant *TypeNameField;
2617 // If we're supposed to demote the visibility, be sure to set a flag
2618 // to use a string comparison for type_info comparisons.
2619 ItaniumCXXABI::RTTIUniquenessKind RTTIUniqueness =
2620 CXXABI.classifyRTTIUniqueness(Ty, Linkage);
2621 if (RTTIUniqueness != ItaniumCXXABI::RUK_Unique) {
2622 // The flag is the sign bit, which on ARM64 is defined to be clear
2623 // for global pointers. This is very ARM64-specific.
2624 TypeNameField = llvm::ConstantExpr::getPtrToInt(TypeName, CGM.Int64Ty);
2625 llvm::Constant *flag =
2626 llvm::ConstantInt::get(CGM.Int64Ty, ((uint64_t)1) << 63);
2627 TypeNameField = llvm::ConstantExpr::getAdd(TypeNameField, flag);
2629 llvm::ConstantExpr::getIntToPtr(TypeNameField, CGM.Int8PtrTy);
2631 TypeNameField = llvm::ConstantExpr::getBitCast(TypeName, CGM.Int8PtrTy);
2633 Fields.push_back(TypeNameField);
2635 switch (Ty->getTypeClass()) {
2636 #define TYPE(Class, Base)
2637 #define ABSTRACT_TYPE(Class, Base)
2638 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
2639 #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2640 #define DEPENDENT_TYPE(Class, Base) case Type::Class:
2641 #include "clang/AST/TypeNodes.def"
2642 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
2644 // GCC treats vector types as fundamental types.
2647 case Type::ExtVector:
2649 case Type::BlockPointer:
2650 // Itanium C++ ABI 2.9.5p4:
2651 // abi::__fundamental_type_info adds no data members to std::type_info.
2654 case Type::LValueReference:
2655 case Type::RValueReference:
2656 llvm_unreachable("References shouldn't get here");
2659 llvm_unreachable("Undeduced auto type shouldn't get here");
2661 case Type::ConstantArray:
2662 case Type::IncompleteArray:
2663 case Type::VariableArray:
2664 // Itanium C++ ABI 2.9.5p5:
2665 // abi::__array_type_info adds no data members to std::type_info.
2668 case Type::FunctionNoProto:
2669 case Type::FunctionProto:
2670 // Itanium C++ ABI 2.9.5p5:
2671 // abi::__function_type_info adds no data members to std::type_info.
2675 // Itanium C++ ABI 2.9.5p5:
2676 // abi::__enum_type_info adds no data members to std::type_info.
2679 case Type::Record: {
2680 const CXXRecordDecl *RD =
2681 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
2682 if (!RD->hasDefinition() || !RD->getNumBases()) {
2683 // We don't need to emit any fields.
2687 if (CanUseSingleInheritance(RD))
2688 BuildSIClassTypeInfo(RD);
2690 BuildVMIClassTypeInfo(RD);
2695 case Type::ObjCObject:
2696 case Type::ObjCInterface:
2697 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
2700 case Type::ObjCObjectPointer:
2701 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
2705 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
2708 case Type::MemberPointer:
2709 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
2713 // No fields, at least for the moment.
2717 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields);
2719 llvm::GlobalVariable *GV =
2720 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
2721 /*Constant=*/true, Linkage, Init, Name);
2723 // If there's already an old global variable, replace it with the new one.
2725 GV->takeName(OldGV);
2726 llvm::Constant *NewPtr =
2727 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
2728 OldGV->replaceAllUsesWith(NewPtr);
2729 OldGV->eraseFromParent();
2732 // The Itanium ABI specifies that type_info objects must be globally
2733 // unique, with one exception: if the type is an incomplete class
2734 // type or a (possibly indirect) pointer to one. That exception
2735 // affects the general case of comparing type_info objects produced
2736 // by the typeid operator, which is why the comparison operators on
2737 // std::type_info generally use the type_info name pointers instead
2738 // of the object addresses. However, the language's built-in uses
2739 // of RTTI generally require class types to be complete, even when
2740 // manipulating pointers to those class types. This allows the
2741 // implementation of dynamic_cast to rely on address equality tests,
2742 // which is much faster.
2744 // All of this is to say that it's important that both the type_info
2745 // object and the type_info name be uniqued when weakly emitted.
2747 // Give the type_info object and name the formal visibility of the
2749 llvm::GlobalValue::VisibilityTypes llvmVisibility;
2750 if (llvm::GlobalValue::isLocalLinkage(Linkage))
2751 // If the linkage is local, only default visibility makes sense.
2752 llvmVisibility = llvm::GlobalValue::DefaultVisibility;
2753 else if (RTTIUniqueness == ItaniumCXXABI::RUK_NonUniqueHidden)
2754 llvmVisibility = llvm::GlobalValue::HiddenVisibility;
2756 llvmVisibility = CodeGenModule::GetLLVMVisibility(Ty->getVisibility());
2757 TypeName->setVisibility(llvmVisibility);
2758 GV->setVisibility(llvmVisibility);
2760 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
2763 /// ComputeQualifierFlags - Compute the pointer type info flags from the
2764 /// given qualifier.
2765 static unsigned ComputeQualifierFlags(Qualifiers Quals) {
2768 if (Quals.hasConst())
2769 Flags |= ItaniumRTTIBuilder::PTI_Const;
2770 if (Quals.hasVolatile())
2771 Flags |= ItaniumRTTIBuilder::PTI_Volatile;
2772 if (Quals.hasRestrict())
2773 Flags |= ItaniumRTTIBuilder::PTI_Restrict;
2778 /// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
2779 /// for the given Objective-C object type.
2780 void ItaniumRTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
2782 const Type *T = OT->getBaseType().getTypePtr();
2783 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
2785 // The builtin types are abi::__class_type_infos and don't require
2787 if (isa<BuiltinType>(T)) return;
2789 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
2790 ObjCInterfaceDecl *Super = Class->getSuperClass();
2792 // Root classes are also __class_type_info.
2795 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
2797 // Everything else is single inheritance.
2798 llvm::Constant *BaseTypeInfo =
2799 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(SuperTy);
2800 Fields.push_back(BaseTypeInfo);
2803 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
2804 /// inheritance, according to the Itanium C++ ABI, 2.95p6b.
2805 void ItaniumRTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
2806 // Itanium C++ ABI 2.9.5p6b:
2807 // It adds to abi::__class_type_info a single member pointing to the
2808 // type_info structure for the base type,
2809 llvm::Constant *BaseTypeInfo =
2810 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(RD->bases_begin()->getType());
2811 Fields.push_back(BaseTypeInfo);
2815 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
2816 /// a class hierarchy.
2818 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
2819 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
2823 /// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
2824 /// abi::__vmi_class_type_info.
2826 static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
2831 const CXXRecordDecl *BaseDecl =
2832 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
2834 if (Base->isVirtual()) {
2835 // Mark the virtual base as seen.
2836 if (!Bases.VirtualBases.insert(BaseDecl).second) {
2837 // If this virtual base has been seen before, then the class is diamond
2839 Flags |= ItaniumRTTIBuilder::VMI_DiamondShaped;
2841 if (Bases.NonVirtualBases.count(BaseDecl))
2842 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
2845 // Mark the non-virtual base as seen.
2846 if (!Bases.NonVirtualBases.insert(BaseDecl).second) {
2847 // If this non-virtual base has been seen before, then the class has non-
2848 // diamond shaped repeated inheritance.
2849 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
2851 if (Bases.VirtualBases.count(BaseDecl))
2852 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
2857 for (const auto &I : BaseDecl->bases())
2858 Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases);
2863 static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
2868 for (const auto &I : RD->bases())
2869 Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases);
2874 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
2875 /// classes with bases that do not satisfy the abi::__si_class_type_info
2876 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
2877 void ItaniumRTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
2878 llvm::Type *UnsignedIntLTy =
2879 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
2881 // Itanium C++ ABI 2.9.5p6c:
2882 // __flags is a word with flags describing details about the class
2883 // structure, which may be referenced by using the __flags_masks
2884 // enumeration. These flags refer to both direct and indirect bases.
2885 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
2886 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
2888 // Itanium C++ ABI 2.9.5p6c:
2889 // __base_count is a word with the number of direct proper base class
2890 // descriptions that follow.
2891 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
2893 if (!RD->getNumBases())
2896 llvm::Type *LongLTy =
2897 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
2899 // Now add the base class descriptions.
2901 // Itanium C++ ABI 2.9.5p6c:
2902 // __base_info[] is an array of base class descriptions -- one for every
2903 // direct proper base. Each description is of the type:
2905 // struct abi::__base_class_type_info {
2907 // const __class_type_info *__base_type;
2908 // long __offset_flags;
2910 // enum __offset_flags_masks {
2911 // __virtual_mask = 0x1,
2912 // __public_mask = 0x2,
2913 // __offset_shift = 8
2916 for (const auto &Base : RD->bases()) {
2917 // The __base_type member points to the RTTI for the base type.
2918 Fields.push_back(ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(Base.getType()));
2920 const CXXRecordDecl *BaseDecl =
2921 cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
2923 int64_t OffsetFlags = 0;
2925 // All but the lower 8 bits of __offset_flags are a signed offset.
2926 // For a non-virtual base, this is the offset in the object of the base
2927 // subobject. For a virtual base, this is the offset in the virtual table of
2928 // the virtual base offset for the virtual base referenced (negative).
2930 if (Base.isVirtual())
2932 CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(RD, BaseDecl);
2934 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
2935 Offset = Layout.getBaseClassOffset(BaseDecl);
2938 OffsetFlags = uint64_t(Offset.getQuantity()) << 8;
2940 // The low-order byte of __offset_flags contains flags, as given by the
2941 // masks from the enumeration __offset_flags_masks.
2942 if (Base.isVirtual())
2943 OffsetFlags |= BCTI_Virtual;
2944 if (Base.getAccessSpecifier() == AS_public)
2945 OffsetFlags |= BCTI_Public;
2947 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
2951 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
2952 /// used for pointer types.
2953 void ItaniumRTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
2955 QualType UnqualifiedPointeeTy =
2956 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
2958 // Itanium C++ ABI 2.9.5p7:
2959 // __flags is a flag word describing the cv-qualification and other
2960 // attributes of the type pointed to
2961 unsigned Flags = ComputeQualifierFlags(Quals);
2963 // Itanium C++ ABI 2.9.5p7:
2964 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
2965 // incomplete class type, the incomplete target type flag is set.
2966 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
2967 Flags |= PTI_Incomplete;
2969 llvm::Type *UnsignedIntLTy =
2970 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
2971 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
2973 // Itanium C++ ABI 2.9.5p7:
2974 // __pointee is a pointer to the std::type_info derivation for the
2975 // unqualified type being pointed to.
2976 llvm::Constant *PointeeTypeInfo =
2977 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(UnqualifiedPointeeTy);
2978 Fields.push_back(PointeeTypeInfo);
2981 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
2982 /// struct, used for member pointer types.
2984 ItaniumRTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
2985 QualType PointeeTy = Ty->getPointeeType();
2988 QualType UnqualifiedPointeeTy =
2989 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
2991 // Itanium C++ ABI 2.9.5p7:
2992 // __flags is a flag word describing the cv-qualification and other
2993 // attributes of the type pointed to.
2994 unsigned Flags = ComputeQualifierFlags(Quals);
2996 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
2998 // Itanium C++ ABI 2.9.5p7:
2999 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
3000 // incomplete class type, the incomplete target type flag is set.
3001 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
3002 Flags |= PTI_Incomplete;
3004 if (IsIncompleteClassType(ClassType))
3005 Flags |= PTI_ContainingClassIncomplete;
3007 llvm::Type *UnsignedIntLTy =
3008 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
3009 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3011 // Itanium C++ ABI 2.9.5p7:
3012 // __pointee is a pointer to the std::type_info derivation for the
3013 // unqualified type being pointed to.
3014 llvm::Constant *PointeeTypeInfo =
3015 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(UnqualifiedPointeeTy);
3016 Fields.push_back(PointeeTypeInfo);
3018 // Itanium C++ ABI 2.9.5p9:
3019 // __context is a pointer to an abi::__class_type_info corresponding to the
3020 // class type containing the member pointed to
3021 // (e.g., the "A" in "int A::*").
3023 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(QualType(ClassType, 0)));
3026 llvm::Constant *ItaniumCXXABI::getAddrOfRTTIDescriptor(QualType Ty) {
3027 return ItaniumRTTIBuilder(*this).BuildTypeInfo(Ty);
3030 void ItaniumCXXABI::EmitFundamentalRTTIDescriptor(QualType Type) {
3031 QualType PointerType = getContext().getPointerType(Type);
3032 QualType PointerTypeConst = getContext().getPointerType(Type.withConst());
3033 ItaniumRTTIBuilder(*this).BuildTypeInfo(Type, true);
3034 ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerType, true);
3035 ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
3038 void ItaniumCXXABI::EmitFundamentalRTTIDescriptors() {
3039 QualType FundamentalTypes[] = {
3040 getContext().VoidTy, getContext().NullPtrTy,
3041 getContext().BoolTy, getContext().WCharTy,
3042 getContext().CharTy, getContext().UnsignedCharTy,
3043 getContext().SignedCharTy, getContext().ShortTy,
3044 getContext().UnsignedShortTy, getContext().IntTy,
3045 getContext().UnsignedIntTy, getContext().LongTy,
3046 getContext().UnsignedLongTy, getContext().LongLongTy,
3047 getContext().UnsignedLongLongTy, getContext().HalfTy,
3048 getContext().FloatTy, getContext().DoubleTy,
3049 getContext().LongDoubleTy, getContext().Char16Ty,
3050 getContext().Char32Ty,
3052 for (const QualType &FundamentalType : FundamentalTypes)
3053 EmitFundamentalRTTIDescriptor(FundamentalType);
3056 /// What sort of uniqueness rules should we use for the RTTI for the
3058 ItaniumCXXABI::RTTIUniquenessKind ItaniumCXXABI::classifyRTTIUniqueness(
3059 QualType CanTy, llvm::GlobalValue::LinkageTypes Linkage) const {
3060 if (shouldRTTIBeUnique())
3063 // It's only necessary for linkonce_odr or weak_odr linkage.
3064 if (Linkage != llvm::GlobalValue::LinkOnceODRLinkage &&
3065 Linkage != llvm::GlobalValue::WeakODRLinkage)
3068 // It's only necessary with default visibility.
3069 if (CanTy->getVisibility() != DefaultVisibility)
3072 // If we're not required to publish this symbol, hide it.
3073 if (Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
3074 return RUK_NonUniqueHidden;
3076 // If we're required to publish this symbol, as we might be under an
3077 // explicit instantiation, leave it with default visibility but
3078 // enable string-comparisons.
3079 assert(Linkage == llvm::GlobalValue::WeakODRLinkage);
3080 return RUK_NonUniqueVisible;
3083 // Find out how to codegen the complete destructor and constructor
3085 enum class StructorCodegen { Emit, RAUW, Alias, COMDAT };
3087 static StructorCodegen getCodegenToUse(CodeGenModule &CGM,
3088 const CXXMethodDecl *MD) {
3089 if (!CGM.getCodeGenOpts().CXXCtorDtorAliases)
3090 return StructorCodegen::Emit;
3092 // The complete and base structors are not equivalent if there are any virtual
3093 // bases, so emit separate functions.
3094 if (MD->getParent()->getNumVBases())
3095 return StructorCodegen::Emit;
3097 GlobalDecl AliasDecl;
3098 if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) {
3099 AliasDecl = GlobalDecl(DD, Dtor_Complete);
3101 const auto *CD = cast<CXXConstructorDecl>(MD);
3102 AliasDecl = GlobalDecl(CD, Ctor_Complete);
3104 llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl);
3106 if (llvm::GlobalValue::isDiscardableIfUnused(Linkage))
3107 return StructorCodegen::RAUW;
3109 // FIXME: Should we allow available_externally aliases?
3110 if (!llvm::GlobalAlias::isValidLinkage(Linkage))
3111 return StructorCodegen::RAUW;
3113 if (llvm::GlobalValue::isWeakForLinker(Linkage)) {
3114 // Only ELF supports COMDATs with arbitrary names (C5/D5).
3115 if (CGM.getTarget().getTriple().isOSBinFormatELF())
3116 return StructorCodegen::COMDAT;
3117 return StructorCodegen::Emit;
3120 return StructorCodegen::Alias;
3123 static void emitConstructorDestructorAlias(CodeGenModule &CGM,
3124 GlobalDecl AliasDecl,
3125 GlobalDecl TargetDecl) {
3126 llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl);
3128 StringRef MangledName = CGM.getMangledName(AliasDecl);
3129 llvm::GlobalValue *Entry = CGM.GetGlobalValue(MangledName);
3130 if (Entry && !Entry->isDeclaration())
3133 auto *Aliasee = cast<llvm::GlobalValue>(CGM.GetAddrOfGlobal(TargetDecl));
3134 llvm::PointerType *AliasType = Aliasee->getType();
3136 // Create the alias with no name.
3137 auto *Alias = llvm::GlobalAlias::create(
3138 AliasType->getElementType(), 0, Linkage, "", Aliasee, &CGM.getModule());
3140 // Switch any previous uses to the alias.
3142 assert(Entry->getType() == AliasType &&
3143 "declaration exists with different type");
3144 Alias->takeName(Entry);
3145 Entry->replaceAllUsesWith(Alias);
3146 Entry->eraseFromParent();
3148 Alias->setName(MangledName);
3151 // Finally, set up the alias with its proper name and attributes.
3152 CGM.setAliasAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias);
3155 void ItaniumCXXABI::emitCXXStructor(const CXXMethodDecl *MD,
3156 StructorType Type) {
3157 auto *CD = dyn_cast<CXXConstructorDecl>(MD);
3158 const CXXDestructorDecl *DD = CD ? nullptr : cast<CXXDestructorDecl>(MD);
3160 StructorCodegen CGType = getCodegenToUse(CGM, MD);
3162 if (Type == StructorType::Complete) {
3163 GlobalDecl CompleteDecl;
3164 GlobalDecl BaseDecl;
3166 CompleteDecl = GlobalDecl(CD, Ctor_Complete);
3167 BaseDecl = GlobalDecl(CD, Ctor_Base);
3169 CompleteDecl = GlobalDecl(DD, Dtor_Complete);
3170 BaseDecl = GlobalDecl(DD, Dtor_Base);
3173 if (CGType == StructorCodegen::Alias || CGType == StructorCodegen::COMDAT) {
3174 emitConstructorDestructorAlias(CGM, CompleteDecl, BaseDecl);
3178 if (CGType == StructorCodegen::RAUW) {
3179 StringRef MangledName = CGM.getMangledName(CompleteDecl);
3180 auto *Aliasee = cast<llvm::GlobalValue>(CGM.GetAddrOfGlobal(BaseDecl));
3181 CGM.addReplacement(MangledName, Aliasee);
3186 // The base destructor is equivalent to the base destructor of its
3187 // base class if there is exactly one non-virtual base class with a
3188 // non-trivial destructor, there are no fields with a non-trivial
3189 // destructor, and the body of the destructor is trivial.
3190 if (DD && Type == StructorType::Base && CGType != StructorCodegen::COMDAT &&
3191 !CGM.TryEmitBaseDestructorAsAlias(DD))
3194 llvm::Function *Fn = CGM.codegenCXXStructor(MD, Type);
3196 if (CGType == StructorCodegen::COMDAT) {
3197 SmallString<256> Buffer;
3198 llvm::raw_svector_ostream Out(Buffer);
3200 getMangleContext().mangleCXXDtorComdat(DD, Out);
3202 getMangleContext().mangleCXXCtorComdat(CD, Out);
3203 llvm::Comdat *C = CGM.getModule().getOrInsertComdat(Out.str());