blob: 8f9b16470b6428c779a02d0d6bd090cb2f2efc49 [file] [log] [blame]
Charles Davis4e786dd2010-05-25 19:52:27 +00001//===------- ItaniumCXXABI.cpp - Emit LLVM Code from ASTs for a Module ----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Charles Davis4e786dd2010-05-25 19:52:27 +00006//
7//===----------------------------------------------------------------------===//
8//
Chris Lattner57540c52011-04-15 05:22:18 +00009// This provides C++ code generation targeting the Itanium C++ ABI. The class
Charles Davis4e786dd2010-05-25 19:52:27 +000010// in this file generates structures that follow the Itanium C++ ABI, which is
11// documented at:
12// http://www.codesourcery.com/public/cxx-abi/abi.html
13// http://www.codesourcery.com/public/cxx-abi/abi-eh.html
John McCall86353412010-08-21 22:46:04 +000014//
15// It also supports the closely-related ARM ABI, documented at:
16// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
17//
Charles Davis4e786dd2010-05-25 19:52:27 +000018//===----------------------------------------------------------------------===//
19
20#include "CGCXXABI.h"
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000021#include "CGCleanup.h"
John McCall7a9aac22010-08-23 01:21:21 +000022#include "CGRecordLayout.h"
Charles Davisa325a6e2012-06-23 23:44:00 +000023#include "CGVTables.h"
John McCall475999d2010-08-22 00:05:51 +000024#include "CodeGenFunction.h"
Charles Davis4e786dd2010-05-25 19:52:27 +000025#include "CodeGenModule.h"
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000026#include "TargetInfo.h"
John McCall5ad74072017-03-02 20:04:19 +000027#include "clang/CodeGen/ConstantInitBuilder.h"
Craig Topperc9ee1d02012-09-15 18:47:51 +000028#include "clang/AST/Mangle.h"
29#include "clang/AST/Type.h"
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000030#include "clang/AST/StmtCXX.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000031#include "llvm/IR/DataLayout.h"
Thomas Andersonb6d87cf2018-07-24 00:43:47 +000032#include "llvm/IR/GlobalValue.h"
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000033#include "llvm/IR/Instructions.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000034#include "llvm/IR/Intrinsics.h"
35#include "llvm/IR/Value.h"
Akira Hatanaka617e2612018-04-17 18:41:52 +000036#include "llvm/Support/ScopedPrinter.h"
Charles Davis4e786dd2010-05-25 19:52:27 +000037
38using namespace clang;
John McCall475999d2010-08-22 00:05:51 +000039using namespace CodeGen;
Charles Davis4e786dd2010-05-25 19:52:27 +000040
41namespace {
Charles Davis53c59df2010-08-16 03:33:14 +000042class ItaniumCXXABI : public CodeGen::CGCXXABI {
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +000043 /// VTables - All the vtables which have been defined.
44 llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> VTables;
45
Richard Smith00223822019-09-12 20:00:24 +000046 /// All the thread wrapper functions that have been used.
47 llvm::SmallVector<std::pair<const VarDecl *, llvm::Function *>, 8>
48 ThreadWrappers;
49
John McCall475999d2010-08-22 00:05:51 +000050protected:
Mark Seabornedf0d382013-07-24 16:25:13 +000051 bool UseARMMethodPtrABI;
52 bool UseARMGuardVarABI;
John McCalld23b27e2016-09-16 02:40:45 +000053 bool Use32BitVTableOffsetABI;
John McCall7a9aac22010-08-23 01:21:21 +000054
Timur Iskhodzhanov67455222013-10-03 06:26:13 +000055 ItaniumMangleContext &getMangleContext() {
56 return cast<ItaniumMangleContext>(CodeGen::CGCXXABI::getMangleContext());
57 }
58
Charles Davis4e786dd2010-05-25 19:52:27 +000059public:
Mark Seabornedf0d382013-07-24 16:25:13 +000060 ItaniumCXXABI(CodeGen::CodeGenModule &CGM,
61 bool UseARMMethodPtrABI = false,
62 bool UseARMGuardVarABI = false) :
63 CGCXXABI(CGM), UseARMMethodPtrABI(UseARMMethodPtrABI),
John McCalld23b27e2016-09-16 02:40:45 +000064 UseARMGuardVarABI(UseARMGuardVarABI),
Richard Smithb17d6fa2016-12-01 03:04:07 +000065 Use32BitVTableOffsetABI(false) { }
John McCall475999d2010-08-22 00:05:51 +000066
Reid Kleckner40ca9132014-05-13 22:05:45 +000067 bool classifyReturnType(CGFunctionInfo &FI) const override;
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000068
Craig Topper4f12f102014-03-12 06:41:41 +000069 RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const override {
Richard Smith96cd6712017-08-16 01:49:53 +000070 // If C++ prohibits us from making a copy, pass by address.
Reid Kleckneradb41982019-04-30 22:23:20 +000071 if (!RD->canPassInRegisters())
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000072 return RAA_Indirect;
73 return RAA_Default;
74 }
75
John McCall7f416cc2015-09-08 08:05:57 +000076 bool isThisCompleteObject(GlobalDecl GD) const override {
77 // The Itanium ABI has separate complete-object vs. base-object
78 // variants of both constructors and destructors.
79 if (isa<CXXDestructorDecl>(GD.getDecl())) {
80 switch (GD.getDtorType()) {
81 case Dtor_Complete:
82 case Dtor_Deleting:
83 return true;
84
85 case Dtor_Base:
86 return false;
87
88 case Dtor_Comdat:
89 llvm_unreachable("emitting dtor comdat as function?");
90 }
91 llvm_unreachable("bad dtor kind");
92 }
93 if (isa<CXXConstructorDecl>(GD.getDecl())) {
94 switch (GD.getCtorType()) {
95 case Ctor_Complete:
96 return true;
97
98 case Ctor_Base:
99 return false;
100
101 case Ctor_CopyingClosure:
102 case Ctor_DefaultClosure:
103 llvm_unreachable("closure ctors in Itanium ABI?");
104
105 case Ctor_Comdat:
106 llvm_unreachable("emitting ctor comdat as function?");
107 }
108 llvm_unreachable("bad dtor kind");
109 }
110
111 // No other kinds.
112 return false;
113 }
114
Craig Topper4f12f102014-03-12 06:41:41 +0000115 bool isZeroInitializable(const MemberPointerType *MPT) override;
John McCall84fa5102010-08-22 04:16:24 +0000116
Craig Topper4f12f102014-03-12 06:41:41 +0000117 llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT) override;
John McCall7a9aac22010-08-23 01:21:21 +0000118
John McCallb92ab1a2016-10-26 23:46:34 +0000119 CGCallee
Craig Topper4f12f102014-03-12 06:41:41 +0000120 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
121 const Expr *E,
John McCall7f416cc2015-09-08 08:05:57 +0000122 Address This,
123 llvm::Value *&ThisPtrForCall,
Craig Topper4f12f102014-03-12 06:41:41 +0000124 llvm::Value *MemFnPtr,
125 const MemberPointerType *MPT) override;
John McCalla8bbb822010-08-22 03:04:22 +0000126
Craig Topper4f12f102014-03-12 06:41:41 +0000127 llvm::Value *
128 EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
John McCall7f416cc2015-09-08 08:05:57 +0000129 Address Base,
Craig Topper4f12f102014-03-12 06:41:41 +0000130 llvm::Value *MemPtr,
131 const MemberPointerType *MPT) override;
John McCallc134eb52010-08-31 21:07:20 +0000132
John McCall7a9aac22010-08-23 01:21:21 +0000133 llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
134 const CastExpr *E,
Craig Topper4f12f102014-03-12 06:41:41 +0000135 llvm::Value *Src) override;
John McCallc62bb392012-02-15 01:22:51 +0000136 llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
Craig Topper4f12f102014-03-12 06:41:41 +0000137 llvm::Constant *Src) override;
John McCall84fa5102010-08-22 04:16:24 +0000138
Craig Topper4f12f102014-03-12 06:41:41 +0000139 llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT) override;
John McCall84fa5102010-08-22 04:16:24 +0000140
David Majnemere2be95b2015-06-23 07:31:01 +0000141 llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD) override;
John McCallf3a88602011-02-03 08:15:49 +0000142 llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
Craig Topper4f12f102014-03-12 06:41:41 +0000143 CharUnits offset) override;
144 llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT) override;
Richard Smithdafff942012-01-14 04:30:29 +0000145 llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD,
146 CharUnits ThisAdjustment);
John McCall1c456c82010-08-22 06:43:33 +0000147
John McCall7a9aac22010-08-23 01:21:21 +0000148 llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +0000149 llvm::Value *L, llvm::Value *R,
John McCall7a9aac22010-08-23 01:21:21 +0000150 const MemberPointerType *MPT,
Craig Topper4f12f102014-03-12 06:41:41 +0000151 bool Inequality) override;
John McCall131d97d2010-08-22 08:30:07 +0000152
John McCall7a9aac22010-08-23 01:21:21 +0000153 llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +0000154 llvm::Value *Addr,
155 const MemberPointerType *MPT) override;
John McCall5d865c322010-08-31 07:33:07 +0000156
David Majnemer08681372014-11-01 07:37:17 +0000157 void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE,
John McCall7f416cc2015-09-08 08:05:57 +0000158 Address Ptr, QualType ElementType,
David Majnemer0c0b6d92014-10-31 20:09:12 +0000159 const CXXDestructorDecl *Dtor) override;
John McCall82fb8922012-09-25 10:10:39 +0000160
David Majnemer442d0a22014-11-25 07:20:20 +0000161 void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override;
David Majnemer7c237072015-03-05 00:46:22 +0000162 void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) override;
David Majnemer442d0a22014-11-25 07:20:20 +0000163
Reid Klecknerfff8e7f2015-03-03 19:21:04 +0000164 void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) override;
165
166 llvm::CallInst *
167 emitTerminateForUnexpectedException(CodeGenFunction &CGF,
168 llvm::Value *Exn) override;
169
Thomas Andersonb6d87cf2018-07-24 00:43:47 +0000170 void EmitFundamentalRTTIDescriptors(const CXXRecordDecl *RD);
David Majnemer443250f2015-03-17 20:35:00 +0000171 llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override;
Reid Kleckner10aa7702015-09-16 20:15:55 +0000172 CatchTypeInfo
David Majnemer37b417f2015-03-29 21:55:10 +0000173 getAddrOfCXXCatchHandlerType(QualType Ty,
174 QualType CatchHandlerType) override {
Reid Kleckner10aa7702015-09-16 20:15:55 +0000175 return CatchTypeInfo{getAddrOfRTTIDescriptor(Ty), 0};
David Majnemer443250f2015-03-17 20:35:00 +0000176 }
David Majnemere2cb8d12014-07-07 06:20:47 +0000177
David Majnemer1162d252014-06-22 19:05:33 +0000178 bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override;
179 void EmitBadTypeidCall(CodeGenFunction &CGF) override;
180 llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy,
John McCall7f416cc2015-09-08 08:05:57 +0000181 Address ThisPtr,
David Majnemer1162d252014-06-22 19:05:33 +0000182 llvm::Type *StdTypeInfoPtrTy) override;
183
184 bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
185 QualType SrcRecordTy) override;
186
John McCall7f416cc2015-09-08 08:05:57 +0000187 llvm::Value *EmitDynamicCastCall(CodeGenFunction &CGF, Address Value,
David Majnemer1162d252014-06-22 19:05:33 +0000188 QualType SrcRecordTy, QualType DestTy,
189 QualType DestRecordTy,
190 llvm::BasicBlock *CastEnd) override;
191
John McCall7f416cc2015-09-08 08:05:57 +0000192 llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, Address Value,
David Majnemer1162d252014-06-22 19:05:33 +0000193 QualType SrcRecordTy,
194 QualType DestTy) override;
195
196 bool EmitBadCastCall(CodeGenFunction &CGF) override;
197
Craig Topper4f12f102014-03-12 06:41:41 +0000198 llvm::Value *
John McCall7f416cc2015-09-08 08:05:57 +0000199 GetVirtualBaseClassOffset(CodeGenFunction &CGF, Address This,
Craig Topper4f12f102014-03-12 06:41:41 +0000200 const CXXRecordDecl *ClassDecl,
201 const CXXRecordDecl *BaseClassDecl) override;
Reid Klecknerd8cbeec2013-05-29 18:02:47 +0000202
Craig Topper4f12f102014-03-12 06:41:41 +0000203 void EmitCXXConstructors(const CXXConstructorDecl *D) override;
Timur Iskhodzhanov40f2fa92013-08-04 17:30:04 +0000204
George Burgess IVf203dbf2017-02-22 20:28:02 +0000205 AddedStructorArgs
Peter Collingbourned1c5b282019-03-22 23:05:10 +0000206 buildStructorSignature(GlobalDecl GD,
George Burgess IVf203dbf2017-02-22 20:28:02 +0000207 SmallVectorImpl<CanQualType> &ArgTys) override;
John McCall5d865c322010-08-31 07:33:07 +0000208
Reid Klecknere7de47e2013-07-22 13:51:44 +0000209 bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
Craig Topper4f12f102014-03-12 06:41:41 +0000210 CXXDtorType DT) const override {
Reid Klecknere7de47e2013-07-22 13:51:44 +0000211 // Itanium does not emit any destructor variant as an inline thunk.
212 // Delegating may occur as an optimization, but all variants are either
213 // emitted with external linkage or as linkonce if they are inline and used.
214 return false;
215 }
216
Craig Topper4f12f102014-03-12 06:41:41 +0000217 void EmitCXXDestructors(const CXXDestructorDecl *D) override;
Reid Klecknere7de47e2013-07-22 13:51:44 +0000218
Reid Kleckner89077a12013-12-17 19:46:40 +0000219 void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy,
Craig Topper4f12f102014-03-12 06:41:41 +0000220 FunctionArgList &Params) override;
John McCall5d865c322010-08-31 07:33:07 +0000221
Craig Topper4f12f102014-03-12 06:41:41 +0000222 void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override;
John McCall8ed55a52010-09-02 09:58:18 +0000223
George Burgess IVf203dbf2017-02-22 20:28:02 +0000224 AddedStructorArgs
225 addImplicitConstructorArgs(CodeGenFunction &CGF, const CXXConstructorDecl *D,
226 CXXCtorType Type, bool ForVirtualBase,
227 bool Delegating, CallArgList &Args) override;
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000228
Reid Kleckner6fe771a2013-12-13 00:53:54 +0000229 void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD,
230 CXXDtorType Type, bool ForVirtualBase,
Marco Antognini88559632019-07-22 09:39:13 +0000231 bool Delegating, Address This,
232 QualType ThisTy) override;
Reid Kleckner6fe771a2013-12-13 00:53:54 +0000233
Craig Topper4f12f102014-03-12 06:41:41 +0000234 void emitVTableDefinitions(CodeGenVTables &CGVT,
235 const CXXRecordDecl *RD) override;
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000236
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000237 bool isVirtualOffsetNeededForVTableField(CodeGenFunction &CGF,
238 CodeGenFunction::VPtr Vptr) override;
239
240 bool doStructorsInitializeVPtrs(const CXXRecordDecl *VTableClass) override {
241 return true;
242 }
243
244 llvm::Constant *
245 getVTableAddressPoint(BaseSubobject Base,
246 const CXXRecordDecl *VTableClass) override;
247
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000248 llvm::Value *getVTableAddressPointInStructor(
249 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass,
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000250 BaseSubobject Base, const CXXRecordDecl *NearestVBase) override;
251
252 llvm::Value *getVTableAddressPointInStructorWithVTT(
253 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass,
254 BaseSubobject Base, const CXXRecordDecl *NearestVBase);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000255
256 llvm::Constant *
257 getVTableAddressPointForConstExpr(BaseSubobject Base,
Craig Topper4f12f102014-03-12 06:41:41 +0000258 const CXXRecordDecl *VTableClass) override;
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000259
260 llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
Craig Topper4f12f102014-03-12 06:41:41 +0000261 CharUnits VPtrOffset) override;
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000262
John McCall9831b842018-02-06 18:52:44 +0000263 CGCallee getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD,
264 Address This, llvm::Type *Ty,
265 SourceLocation Loc) override;
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000266
David Majnemer0c0b6d92014-10-31 20:09:12 +0000267 llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF,
268 const CXXDestructorDecl *Dtor,
Marco Antognini88559632019-07-22 09:39:13 +0000269 CXXDtorType DtorType, Address This,
270 DeleteOrMemberCallExpr E) override;
Timur Iskhodzhanovd6197112013-02-15 14:45:22 +0000271
Craig Topper4f12f102014-03-12 06:41:41 +0000272 void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override;
Reid Kleckner7810af02013-06-19 15:20:38 +0000273
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000274 bool canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const override;
Richard Smithc195c252018-11-27 19:33:49 +0000275 bool canSpeculativelyEmitVTableAsBaseClass(const CXXRecordDecl *RD) const;
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000276
Hans Wennborgc94391d2014-06-06 20:04:01 +0000277 void setThunkLinkage(llvm::Function *Thunk, bool ForVTable, GlobalDecl GD,
278 bool ReturnAdjustment) override {
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +0000279 // Allow inlining of thunks by emitting them with available_externally
280 // linkage together with vtables when needed.
Peter Collingbourne8fabc1b2015-07-01 02:10:26 +0000281 if (ForVTable && !Thunk->hasLocalLinkage())
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +0000282 Thunk->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
Rafael Espindolab7350042018-03-01 00:35:47 +0000283 CGM.setGVProperties(Thunk, GD);
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +0000284 }
285
Rafael Espindolab7350042018-03-01 00:35:47 +0000286 bool exportThunk() override { return true; }
287
John McCall7f416cc2015-09-08 08:05:57 +0000288 llvm::Value *performThisAdjustment(CodeGenFunction &CGF, Address This,
Craig Topper4f12f102014-03-12 06:41:41 +0000289 const ThisAdjustment &TA) override;
Timur Iskhodzhanov02014322013-10-30 11:55:43 +0000290
John McCall7f416cc2015-09-08 08:05:57 +0000291 llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, Address Ret,
Craig Topper4f12f102014-03-12 06:41:41 +0000292 const ReturnAdjustment &RA) override;
Timur Iskhodzhanov02014322013-10-30 11:55:43 +0000293
David Majnemer196ac332014-09-11 23:05:02 +0000294 size_t getSrcArgforCopyCtor(const CXXConstructorDecl *,
295 FunctionArgList &Args) const override {
296 assert(!Args.empty() && "expected the arglist to not be empty!");
297 return Args.size() - 1;
298 }
299
Craig Topper4f12f102014-03-12 06:41:41 +0000300 StringRef GetPureVirtualCallName() override { return "__cxa_pure_virtual"; }
301 StringRef GetDeletedVirtualCallName() override
302 { return "__cxa_deleted_virtual"; }
Joao Matos2ce88ef2012-07-17 17:10:11 +0000303
Craig Topper4f12f102014-03-12 06:41:41 +0000304 CharUnits getArrayCookieSizeImpl(QualType elementType) override;
John McCall7f416cc2015-09-08 08:05:57 +0000305 Address InitializeArrayCookie(CodeGenFunction &CGF,
306 Address NewPtr,
307 llvm::Value *NumElements,
308 const CXXNewExpr *expr,
309 QualType ElementType) override;
John McCallb91cd662012-05-01 05:23:51 +0000310 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +0000311 Address allocPtr,
Craig Topper4f12f102014-03-12 06:41:41 +0000312 CharUnits cookieSize) override;
John McCall68ff0372010-09-08 01:44:27 +0000313
John McCallcdf7ef52010-11-06 09:44:32 +0000314 void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
Craig Topper4f12f102014-03-12 06:41:41 +0000315 llvm::GlobalVariable *DeclPtr,
316 bool PerformInit) override;
Richard Smithdbf74ba2013-04-14 23:01:42 +0000317 void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
James Y Knightf7321542019-02-07 01:14:17 +0000318 llvm::FunctionCallee dtor,
319 llvm::Constant *addr) override;
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000320
321 llvm::Function *getOrCreateThreadLocalWrapper(const VarDecl *VD,
Alexander Musmanf94c3182014-09-26 06:28:25 +0000322 llvm::Value *Val);
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000323 void EmitThreadLocalInitFuncs(
David Majnemerb3341ea2014-10-05 05:05:40 +0000324 CodeGenModule &CGM,
Richard Smith5a99c492015-12-01 01:10:48 +0000325 ArrayRef<const VarDecl *> CXXThreadLocals,
David Majnemerb3341ea2014-10-05 05:05:40 +0000326 ArrayRef<llvm::Function *> CXXThreadLocalInits,
Richard Smith5a99c492015-12-01 01:10:48 +0000327 ArrayRef<const VarDecl *> CXXThreadLocalInitVars) override;
David Majnemerb3341ea2014-10-05 05:05:40 +0000328
Richard Smith00223822019-09-12 20:00:24 +0000329 /// Determine whether we will definitely emit this variable with a constant
330 /// initializer, either because the language semantics demand it or because
331 /// we know that the initializer is a constant.
332 bool isEmittedWithConstantInitializer(const VarDecl *VD) const {
333 VD = VD->getMostRecentDecl();
334 if (VD->hasAttr<ConstInitAttr>())
335 return true;
336
337 // All later checks examine the initializer specified on the variable. If
338 // the variable is weak, such examination would not be correct.
339 if (VD->isWeak() || VD->hasAttr<SelectAnyAttr>())
340 return false;
341
342 const VarDecl *InitDecl = VD->getInitializingDeclaration();
343 if (!InitDecl)
344 return false;
345
346 // If there's no initializer to run, this is constant initialization.
347 if (!InitDecl->hasInit())
348 return true;
349
350 // If we have the only definition, we don't need a thread wrapper if we
351 // will emit the value as a constant.
352 if (isUniqueGVALinkage(getContext().GetGVALinkageForVariable(VD)))
Richard Smith2b4fa532019-09-29 05:08:46 +0000353 return !VD->needsDestruction(getContext()) && InitDecl->evaluateValue();
Richard Smith00223822019-09-12 20:00:24 +0000354
355 // Otherwise, we need a thread wrapper unless we know that every
356 // translation unit will emit the value as a constant. We rely on
357 // ICE-ness not varying between translation units, which isn't actually
358 // guaranteed by the standard but is necessary for sanity.
359 return InitDecl->isInitKnownICE() && InitDecl->isInitICE();
360 }
361
362 bool usesThreadWrapperFunction(const VarDecl *VD) const override {
Richard Smith8ac5c742019-10-01 01:23:23 +0000363 return !isEmittedWithConstantInitializer(VD) ||
364 VD->needsDestruction(getContext());
Richard Smith00223822019-09-12 20:00:24 +0000365 }
Richard Smith0f383742014-03-26 22:48:22 +0000366 LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD,
367 QualType LValType) override;
Peter Collingbourne66f82e62013-06-28 20:45:28 +0000368
Craig Topper4f12f102014-03-12 06:41:41 +0000369 bool NeedsVTTParameter(GlobalDecl GD) override;
David Majnemere2cb8d12014-07-07 06:20:47 +0000370
371 /**************************** RTTI Uniqueness ******************************/
372
373protected:
374 /// Returns true if the ABI requires RTTI type_info objects to be unique
375 /// across a program.
376 virtual bool shouldRTTIBeUnique() const { return true; }
377
378public:
379 /// What sort of unique-RTTI behavior should we use?
380 enum RTTIUniquenessKind {
381 /// We are guaranteeing, or need to guarantee, that the RTTI string
382 /// is unique.
383 RUK_Unique,
384
385 /// We are not guaranteeing uniqueness for the RTTI string, so we
386 /// can demote to hidden visibility but must use string comparisons.
387 RUK_NonUniqueHidden,
388
389 /// We are not guaranteeing uniqueness for the RTTI string, so we
390 /// have to use string comparisons, but we also have to emit it with
391 /// non-hidden visibility.
392 RUK_NonUniqueVisible
393 };
394
395 /// Return the required visibility status for the given type and linkage in
396 /// the current ABI.
397 RTTIUniquenessKind
398 classifyRTTIUniqueness(QualType CanTy,
399 llvm::GlobalValue::LinkageTypes Linkage) const;
400 friend class ItaniumRTTIBuilder;
Rafael Espindola91f68b42014-09-15 19:20:10 +0000401
Peter Collingbourned1c5b282019-03-22 23:05:10 +0000402 void emitCXXStructor(GlobalDecl GD) override;
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000403
Peter Collingbourne60108802017-12-13 21:53:04 +0000404 std::pair<llvm::Value *, const CXXRecordDecl *>
405 LoadVTablePtr(CodeGenFunction &CGF, Address This,
406 const CXXRecordDecl *RD) override;
407
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000408 private:
Piotr Padlewskid3b1cbd2017-06-01 08:04:05 +0000409 bool hasAnyUnusedVirtualInlineFunction(const CXXRecordDecl *RD) const {
410 const auto &VtableLayout =
411 CGM.getItaniumVTableContext().getVTableLayout(RD);
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000412
Piotr Padlewskid3b1cbd2017-06-01 08:04:05 +0000413 for (const auto &VtableComponent : VtableLayout.vtable_components()) {
414 // Skip empty slot.
415 if (!VtableComponent.isUsedFunctionPointerKind())
416 continue;
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000417
Piotr Padlewskid3b1cbd2017-06-01 08:04:05 +0000418 const CXXMethodDecl *Method = VtableComponent.getFunctionDecl();
419 if (!Method->getCanonicalDecl()->isInlined())
420 continue;
421
422 StringRef Name = CGM.getMangledName(VtableComponent.getGlobalDecl());
423 auto *Entry = CGM.GetGlobalValue(Name);
424 // This checks if virtual inline function has already been emitted.
425 // Note that it is possible that this inline function would be emitted
426 // after trying to emit vtable speculatively. Because of this we do
427 // an extra pass after emitting all deferred vtables to find and emit
428 // these vtables opportunistically.
429 if (!Entry || Entry->isDeclaration())
430 return true;
431 }
432 return false;
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000433 }
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000434
435 bool isVTableHidden(const CXXRecordDecl *RD) const {
436 const auto &VtableLayout =
437 CGM.getItaniumVTableContext().getVTableLayout(RD);
438
439 for (const auto &VtableComponent : VtableLayout.vtable_components()) {
440 if (VtableComponent.isRTTIKind()) {
441 const CXXRecordDecl *RTTIDecl = VtableComponent.getRTTIDecl();
442 if (RTTIDecl->getVisibility() == Visibility::HiddenVisibility)
443 return true;
444 } else if (VtableComponent.isUsedFunctionPointerKind()) {
445 const CXXMethodDecl *Method = VtableComponent.getFunctionDecl();
446 if (Method->getVisibility() == Visibility::HiddenVisibility &&
447 !Method->isDefined())
448 return true;
449 }
450 }
451 return false;
452 }
Charles Davis4e786dd2010-05-25 19:52:27 +0000453};
John McCall86353412010-08-21 22:46:04 +0000454
455class ARMCXXABI : public ItaniumCXXABI {
456public:
Mark Seabornedf0d382013-07-24 16:25:13 +0000457 ARMCXXABI(CodeGen::CodeGenModule &CGM) :
Sam Clegga5ee6392019-07-19 00:30:23 +0000458 ItaniumCXXABI(CGM, /*UseARMMethodPtrABI=*/true,
459 /*UseARMGuardVarABI=*/true) {}
John McCall5d865c322010-08-31 07:33:07 +0000460
Craig Topper4f12f102014-03-12 06:41:41 +0000461 bool HasThisReturn(GlobalDecl GD) const override {
Stephen Lin9dc6eef2013-06-30 20:40:16 +0000462 return (isa<CXXConstructorDecl>(GD.getDecl()) || (
463 isa<CXXDestructorDecl>(GD.getDecl()) &&
464 GD.getDtorType() != Dtor_Deleting));
465 }
John McCall5d865c322010-08-31 07:33:07 +0000466
Craig Topper4f12f102014-03-12 06:41:41 +0000467 void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV,
468 QualType ResTy) override;
John McCall5d865c322010-08-31 07:33:07 +0000469
Craig Topper4f12f102014-03-12 06:41:41 +0000470 CharUnits getArrayCookieSizeImpl(QualType elementType) override;
John McCall7f416cc2015-09-08 08:05:57 +0000471 Address InitializeArrayCookie(CodeGenFunction &CGF,
472 Address NewPtr,
473 llvm::Value *NumElements,
474 const CXXNewExpr *expr,
475 QualType ElementType) override;
476 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, Address allocPtr,
Craig Topper4f12f102014-03-12 06:41:41 +0000477 CharUnits cookieSize) override;
John McCall86353412010-08-21 22:46:04 +0000478};
Tim Northovera2ee4332014-03-29 15:09:45 +0000479
480class iOS64CXXABI : public ARMCXXABI {
481public:
John McCalld23b27e2016-09-16 02:40:45 +0000482 iOS64CXXABI(CodeGen::CodeGenModule &CGM) : ARMCXXABI(CGM) {
483 Use32BitVTableOffsetABI = true;
484 }
Tim Northover65f582f2014-03-30 17:32:48 +0000485
486 // ARM64 libraries are prepared for non-unique RTTI.
David Majnemere2cb8d12014-07-07 06:20:47 +0000487 bool shouldRTTIBeUnique() const override { return false; }
Tim Northovera2ee4332014-03-29 15:09:45 +0000488};
Dan Gohmanc2853072015-09-03 22:51:53 +0000489
490class WebAssemblyCXXABI final : public ItaniumCXXABI {
491public:
492 explicit WebAssemblyCXXABI(CodeGen::CodeGenModule &CGM)
493 : ItaniumCXXABI(CGM, /*UseARMMethodPtrABI=*/true,
494 /*UseARMGuardVarABI=*/true) {}
Heejin Ahnc6479192018-05-31 22:18:13 +0000495 void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) override;
Dan Gohmanc2853072015-09-03 22:51:53 +0000496
497private:
498 bool HasThisReturn(GlobalDecl GD) const override {
499 return isa<CXXConstructorDecl>(GD.getDecl()) ||
500 (isa<CXXDestructorDecl>(GD.getDecl()) &&
501 GD.getDtorType() != Dtor_Deleting);
502 }
Derek Schuff8179be42016-05-10 17:44:55 +0000503 bool canCallMismatchedFunctionType() const override { return false; }
Dan Gohmanc2853072015-09-03 22:51:53 +0000504};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000505}
Charles Davis4e786dd2010-05-25 19:52:27 +0000506
Charles Davis53c59df2010-08-16 03:33:14 +0000507CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
John McCallc8e01702013-04-16 22:48:15 +0000508 switch (CGM.getTarget().getCXXABI().getKind()) {
John McCall57625922013-01-25 23:36:14 +0000509 // For IR-generation purposes, there's no significant difference
510 // between the ARM and iOS ABIs.
511 case TargetCXXABI::GenericARM:
512 case TargetCXXABI::iOS:
Tim Northover756447a2015-10-30 16:30:36 +0000513 case TargetCXXABI::WatchOS:
John McCall57625922013-01-25 23:36:14 +0000514 return new ARMCXXABI(CGM);
Charles Davis4e786dd2010-05-25 19:52:27 +0000515
Tim Northovera2ee4332014-03-29 15:09:45 +0000516 case TargetCXXABI::iOS64:
517 return new iOS64CXXABI(CGM);
518
Tim Northover9bb857a2013-01-31 12:13:10 +0000519 // Note that AArch64 uses the generic ItaniumCXXABI class since it doesn't
520 // include the other 32-bit ARM oddities: constructor/destructor return values
521 // and array cookies.
522 case TargetCXXABI::GenericAArch64:
Sam Clegga5ee6392019-07-19 00:30:23 +0000523 return new ItaniumCXXABI(CGM, /*UseARMMethodPtrABI=*/true,
524 /*UseARMGuardVarABI=*/true);
Tim Northover9bb857a2013-01-31 12:13:10 +0000525
Zoran Jovanovic26a12162015-02-18 15:21:35 +0000526 case TargetCXXABI::GenericMIPS:
Sam Clegga5ee6392019-07-19 00:30:23 +0000527 return new ItaniumCXXABI(CGM, /*UseARMMethodPtrABI=*/true);
Zoran Jovanovic26a12162015-02-18 15:21:35 +0000528
Dan Gohmanc2853072015-09-03 22:51:53 +0000529 case TargetCXXABI::WebAssembly:
530 return new WebAssemblyCXXABI(CGM);
531
John McCall57625922013-01-25 23:36:14 +0000532 case TargetCXXABI::GenericItanium:
Mark Seabornedf0d382013-07-24 16:25:13 +0000533 if (CGM.getContext().getTargetInfo().getTriple().getArch()
534 == llvm::Triple::le32) {
535 // For PNaCl, use ARM-style method pointers so that PNaCl code
536 // does not assume anything about the alignment of function
537 // pointers.
Sam Clegga5ee6392019-07-19 00:30:23 +0000538 return new ItaniumCXXABI(CGM, /*UseARMMethodPtrABI=*/true);
Mark Seabornedf0d382013-07-24 16:25:13 +0000539 }
John McCall57625922013-01-25 23:36:14 +0000540 return new ItaniumCXXABI(CGM);
541
542 case TargetCXXABI::Microsoft:
543 llvm_unreachable("Microsoft ABI is not Itanium-based");
544 }
545 llvm_unreachable("bad ABI kind");
John McCall86353412010-08-21 22:46:04 +0000546}
547
Chris Lattnera5f58b02011-07-09 17:41:47 +0000548llvm::Type *
John McCall7a9aac22010-08-23 01:21:21 +0000549ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
550 if (MPT->isMemberDataPointer())
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000551 return CGM.PtrDiffTy;
Serge Guelton1d993272017-05-09 19:31:30 +0000552 return llvm::StructType::get(CGM.PtrDiffTy, CGM.PtrDiffTy);
John McCall1c456c82010-08-22 06:43:33 +0000553}
554
John McCalld9c6c0b2010-08-22 00:59:17 +0000555/// In the Itanium and ARM ABIs, method pointers have the form:
556/// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
557///
558/// In the Itanium ABI:
559/// - method pointers are virtual if (memptr.ptr & 1) is nonzero
560/// - the this-adjustment is (memptr.adj)
561/// - the virtual offset is (memptr.ptr - 1)
562///
563/// In the ARM ABI:
564/// - method pointers are virtual if (memptr.adj & 1) is nonzero
565/// - the this-adjustment is (memptr.adj >> 1)
566/// - the virtual offset is (memptr.ptr)
567/// ARM uses 'adj' for the virtual flag because Thumb functions
568/// may be only single-byte aligned.
569///
570/// If the member is virtual, the adjusted 'this' pointer points
571/// to a vtable pointer from which the virtual offset is applied.
572///
573/// If the member is non-virtual, memptr.ptr is the address of
574/// the function to call.
John McCallb92ab1a2016-10-26 23:46:34 +0000575CGCallee ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(
John McCall7f416cc2015-09-08 08:05:57 +0000576 CodeGenFunction &CGF, const Expr *E, Address ThisAddr,
577 llvm::Value *&ThisPtrForCall,
David Majnemer2b0d66d2014-02-20 23:22:07 +0000578 llvm::Value *MemFnPtr, const MemberPointerType *MPT) {
John McCall475999d2010-08-22 00:05:51 +0000579 CGBuilderTy &Builder = CGF.Builder;
580
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000581 const FunctionProtoType *FPT =
John McCall475999d2010-08-22 00:05:51 +0000582 MPT->getPointeeType()->getAs<FunctionProtoType>();
Simon Pilgrimf2805472019-10-02 20:45:16 +0000583 auto *RD =
584 cast<CXXRecordDecl>(MPT->getClass()->castAs<RecordType>()->getDecl());
John McCall475999d2010-08-22 00:05:51 +0000585
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000586 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(
587 CGM.getTypes().arrangeCXXMethodType(RD, FPT, /*FD=*/nullptr));
John McCall475999d2010-08-22 00:05:51 +0000588
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000589 llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(CGM.PtrDiffTy, 1);
John McCall475999d2010-08-22 00:05:51 +0000590
John McCalld9c6c0b2010-08-22 00:59:17 +0000591 llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
592 llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
593 llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
594
John McCalla1dee5302010-08-22 10:59:02 +0000595 // Extract memptr.adj, which is in the second field.
596 llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
John McCalld9c6c0b2010-08-22 00:59:17 +0000597
598 // Compute the true adjustment.
599 llvm::Value *Adj = RawAdj;
Mark Seabornedf0d382013-07-24 16:25:13 +0000600 if (UseARMMethodPtrABI)
John McCalld9c6c0b2010-08-22 00:59:17 +0000601 Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
John McCall475999d2010-08-22 00:05:51 +0000602
603 // Apply the adjustment and cast back to the original struct type
604 // for consistency.
John McCall7f416cc2015-09-08 08:05:57 +0000605 llvm::Value *This = ThisAddr.getPointer();
John McCalld9c6c0b2010-08-22 00:59:17 +0000606 llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
607 Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
608 This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
John McCall7f416cc2015-09-08 08:05:57 +0000609 ThisPtrForCall = This;
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000610
John McCall475999d2010-08-22 00:05:51 +0000611 // Load the function pointer.
John McCalla1dee5302010-08-22 10:59:02 +0000612 llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000613
John McCall475999d2010-08-22 00:05:51 +0000614 // If the LSB in the function pointer is 1, the function pointer points to
615 // a virtual function.
John McCalld9c6c0b2010-08-22 00:59:17 +0000616 llvm::Value *IsVirtual;
Mark Seabornedf0d382013-07-24 16:25:13 +0000617 if (UseARMMethodPtrABI)
John McCalld9c6c0b2010-08-22 00:59:17 +0000618 IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
619 else
620 IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
621 IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
John McCall475999d2010-08-22 00:05:51 +0000622 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
623
624 // In the virtual path, the adjustment left 'This' pointing to the
625 // vtable of the correct base subobject. The "function pointer" is an
John McCalld9c6c0b2010-08-22 00:59:17 +0000626 // offset within the vtable (+1 for the virtual flag on non-ARM).
John McCall475999d2010-08-22 00:05:51 +0000627 CGF.EmitBlock(FnVirtual);
628
629 // Cast the adjusted this to a pointer to vtable pointer and load.
Chris Lattner2192fe52011-07-18 04:24:23 +0000630 llvm::Type *VTableTy = Builder.getInt8PtrTy();
John McCall7f416cc2015-09-08 08:05:57 +0000631 CharUnits VTablePtrAlign =
632 CGF.CGM.getDynamicOffsetAlignment(ThisAddr.getAlignment(), RD,
633 CGF.getPointerAlign());
634 llvm::Value *VTable =
Piotr Padlewski4b1ac722015-09-15 21:46:55 +0000635 CGF.GetVTablePtr(Address(This, VTablePtrAlign), VTableTy, RD);
John McCall475999d2010-08-22 00:05:51 +0000636
637 // Apply the offset.
John McCalld23b27e2016-09-16 02:40:45 +0000638 // On ARM64, to reserve extra space in virtual member function pointers,
639 // we only pay attention to the low 32 bits of the offset.
John McCalld9c6c0b2010-08-22 00:59:17 +0000640 llvm::Value *VTableOffset = FnAsInt;
Mark Seabornedf0d382013-07-24 16:25:13 +0000641 if (!UseARMMethodPtrABI)
642 VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
John McCalld23b27e2016-09-16 02:40:45 +0000643 if (Use32BitVTableOffsetABI) {
644 VTableOffset = Builder.CreateTrunc(VTableOffset, CGF.Int32Ty);
645 VTableOffset = Builder.CreateZExt(VTableOffset, CGM.PtrDiffTy);
646 }
Peter Collingbournee44acad2018-06-26 02:15:47 +0000647
648 // Check the address of the function pointer if CFI on member function
649 // pointers is enabled.
650 llvm::Constant *CheckSourceLocation;
651 llvm::Constant *CheckTypeDesc;
652 bool ShouldEmitCFICheck = CGF.SanOpts.has(SanitizerKind::CFIMFCall) &&
653 CGM.HasHiddenLTOVisibility(RD);
Oliver Stannard9f6a8732019-10-11 11:59:55 +0000654 bool ShouldEmitVFEInfo = CGM.getCodeGenOpts().VirtualFunctionElimination &&
655 CGM.HasHiddenLTOVisibility(RD);
656 llvm::Value *VirtualFn = nullptr;
657
658 {
Peter Collingbournee44acad2018-06-26 02:15:47 +0000659 CodeGenFunction::SanitizerScope SanScope(&CGF);
Oliver Stannard9f6a8732019-10-11 11:59:55 +0000660 llvm::Value *TypeId = nullptr;
661 llvm::Value *CheckResult = nullptr;
Peter Collingbournee44acad2018-06-26 02:15:47 +0000662
Oliver Stannard9f6a8732019-10-11 11:59:55 +0000663 if (ShouldEmitCFICheck || ShouldEmitVFEInfo) {
664 // If doing CFI or VFE, we will need the metadata node to check against.
665 llvm::Metadata *MD =
666 CGM.CreateMetadataIdentifierForVirtualMemPtrType(QualType(MPT, 0));
667 TypeId = llvm::MetadataAsValue::get(CGF.getLLVMContext(), MD);
Peter Collingbournee44acad2018-06-26 02:15:47 +0000668 }
669
Oliver Stannard9f6a8732019-10-11 11:59:55 +0000670 llvm::Value *VFPAddr = Builder.CreateGEP(VTable, VTableOffset);
John McCall475999d2010-08-22 00:05:51 +0000671
Oliver Stannard9f6a8732019-10-11 11:59:55 +0000672 if (ShouldEmitVFEInfo) {
673 // If doing VFE, load from the vtable with a type.checked.load intrinsic
674 // call. Note that we use the GEP to calculate the address to load from
675 // and pass 0 as the offset to the intrinsic. This is because every
676 // vtable slot of the correct type is marked with matching metadata, and
677 // we know that the load must be from one of these slots.
678 llvm::Value *CheckedLoad = Builder.CreateCall(
679 CGM.getIntrinsic(llvm::Intrinsic::type_checked_load),
680 {VFPAddr, llvm::ConstantInt::get(CGM.Int32Ty, 0), TypeId});
681 CheckResult = Builder.CreateExtractValue(CheckedLoad, 1);
682 VirtualFn = Builder.CreateExtractValue(CheckedLoad, 0);
683 VirtualFn = Builder.CreateBitCast(VirtualFn, FTy->getPointerTo(),
684 "memptr.virtualfn");
685 } else {
686 // When not doing VFE, emit a normal load, as it allows more
687 // optimisations than type.checked.load.
688 if (ShouldEmitCFICheck) {
689 CheckResult = Builder.CreateCall(
690 CGM.getIntrinsic(llvm::Intrinsic::type_test),
691 {Builder.CreateBitCast(VFPAddr, CGF.Int8PtrTy), TypeId});
692 }
693 VFPAddr =
694 Builder.CreateBitCast(VFPAddr, FTy->getPointerTo()->getPointerTo());
695 VirtualFn = Builder.CreateAlignedLoad(VFPAddr, CGF.getPointerAlign(),
696 "memptr.virtualfn");
697 }
698 assert(VirtualFn && "Virtual fuction pointer not created!");
699 assert((!ShouldEmitCFICheck || !ShouldEmitVFEInfo || CheckResult) &&
700 "Check result required but not created!");
701
702 if (ShouldEmitCFICheck) {
703 // If doing CFI, emit the check.
704 CheckSourceLocation = CGF.EmitCheckSourceLocation(E->getBeginLoc());
705 CheckTypeDesc = CGF.EmitCheckTypeDescriptor(QualType(MPT, 0));
706 llvm::Constant *StaticData[] = {
707 llvm::ConstantInt::get(CGF.Int8Ty, CodeGenFunction::CFITCK_VMFCall),
708 CheckSourceLocation,
709 CheckTypeDesc,
710 };
711
712 if (CGM.getCodeGenOpts().SanitizeTrap.has(SanitizerKind::CFIMFCall)) {
713 CGF.EmitTrapCheck(CheckResult);
714 } else {
715 llvm::Value *AllVtables = llvm::MetadataAsValue::get(
716 CGM.getLLVMContext(),
717 llvm::MDString::get(CGM.getLLVMContext(), "all-vtables"));
718 llvm::Value *ValidVtable = Builder.CreateCall(
719 CGM.getIntrinsic(llvm::Intrinsic::type_test), {VTable, AllVtables});
720 CGF.EmitCheck(std::make_pair(CheckResult, SanitizerKind::CFIMFCall),
721 SanitizerHandler::CFICheckFail, StaticData,
722 {VTable, ValidVtable});
723 }
724
725 FnVirtual = Builder.GetInsertBlock();
726 }
727 } // End of sanitizer scope
728
John McCall475999d2010-08-22 00:05:51 +0000729 CGF.EmitBranch(FnEnd);
730
731 // In the non-virtual path, the function pointer is actually a
732 // function pointer.
733 CGF.EmitBlock(FnNonVirtual);
734 llvm::Value *NonVirtualFn =
John McCalld9c6c0b2010-08-22 00:59:17 +0000735 Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000736
Peter Collingbournee44acad2018-06-26 02:15:47 +0000737 // Check the function pointer if CFI on member function pointers is enabled.
738 if (ShouldEmitCFICheck) {
739 CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl();
740 if (RD->hasDefinition()) {
741 CodeGenFunction::SanitizerScope SanScope(&CGF);
742
743 llvm::Constant *StaticData[] = {
744 llvm::ConstantInt::get(CGF.Int8Ty, CodeGenFunction::CFITCK_NVMFCall),
745 CheckSourceLocation,
746 CheckTypeDesc,
747 };
748
749 llvm::Value *Bit = Builder.getFalse();
750 llvm::Value *CastedNonVirtualFn =
751 Builder.CreateBitCast(NonVirtualFn, CGF.Int8PtrTy);
752 for (const CXXRecordDecl *Base : CGM.getMostBaseClasses(RD)) {
753 llvm::Metadata *MD = CGM.CreateMetadataIdentifierForType(
754 getContext().getMemberPointerType(
755 MPT->getPointeeType(),
756 getContext().getRecordType(Base).getTypePtr()));
757 llvm::Value *TypeId =
758 llvm::MetadataAsValue::get(CGF.getLLVMContext(), MD);
759
760 llvm::Value *TypeTest =
761 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::type_test),
762 {CastedNonVirtualFn, TypeId});
763 Bit = Builder.CreateOr(Bit, TypeTest);
764 }
765
766 CGF.EmitCheck(std::make_pair(Bit, SanitizerKind::CFIMFCall),
767 SanitizerHandler::CFICheckFail, StaticData,
768 {CastedNonVirtualFn, llvm::UndefValue::get(CGF.IntPtrTy)});
769
770 FnNonVirtual = Builder.GetInsertBlock();
771 }
772 }
773
John McCall475999d2010-08-22 00:05:51 +0000774 // We're done.
775 CGF.EmitBlock(FnEnd);
John McCallb92ab1a2016-10-26 23:46:34 +0000776 llvm::PHINode *CalleePtr = Builder.CreatePHI(FTy->getPointerTo(), 2);
777 CalleePtr->addIncoming(VirtualFn, FnVirtual);
778 CalleePtr->addIncoming(NonVirtualFn, FnNonVirtual);
779
780 CGCallee Callee(FPT, CalleePtr);
John McCall475999d2010-08-22 00:05:51 +0000781 return Callee;
782}
John McCalla8bbb822010-08-22 03:04:22 +0000783
John McCallc134eb52010-08-31 21:07:20 +0000784/// Compute an l-value by applying the given pointer-to-member to a
785/// base object.
David Majnemer2b0d66d2014-02-20 23:22:07 +0000786llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(
John McCall7f416cc2015-09-08 08:05:57 +0000787 CodeGenFunction &CGF, const Expr *E, Address Base, llvm::Value *MemPtr,
David Majnemer2b0d66d2014-02-20 23:22:07 +0000788 const MemberPointerType *MPT) {
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000789 assert(MemPtr->getType() == CGM.PtrDiffTy);
John McCallc134eb52010-08-31 21:07:20 +0000790
791 CGBuilderTy &Builder = CGF.Builder;
792
John McCallc134eb52010-08-31 21:07:20 +0000793 // Cast to char*.
John McCall7f416cc2015-09-08 08:05:57 +0000794 Base = Builder.CreateElementBitCast(Base, CGF.Int8Ty);
John McCallc134eb52010-08-31 21:07:20 +0000795
796 // Apply the offset, which we assume is non-null.
John McCall7f416cc2015-09-08 08:05:57 +0000797 llvm::Value *Addr =
798 Builder.CreateInBoundsGEP(Base.getPointer(), MemPtr, "memptr.offset");
John McCallc134eb52010-08-31 21:07:20 +0000799
800 // Cast the address to the appropriate pointer type, adopting the
801 // address space of the base pointer.
John McCall7f416cc2015-09-08 08:05:57 +0000802 llvm::Type *PType = CGF.ConvertTypeForMem(MPT->getPointeeType())
803 ->getPointerTo(Base.getAddressSpace());
John McCallc134eb52010-08-31 21:07:20 +0000804 return Builder.CreateBitCast(Addr, PType);
805}
806
John McCallc62bb392012-02-15 01:22:51 +0000807/// Perform a bitcast, derived-to-base, or base-to-derived member pointer
808/// conversion.
809///
810/// Bitcast conversions are always a no-op under Itanium.
John McCall7a9aac22010-08-23 01:21:21 +0000811///
812/// Obligatory offset/adjustment diagram:
813/// <-- offset --> <-- adjustment -->
814/// |--------------------------|----------------------|--------------------|
815/// ^Derived address point ^Base address point ^Member address point
816///
817/// So when converting a base member pointer to a derived member pointer,
818/// we add the offset to the adjustment because the address point has
819/// decreased; and conversely, when converting a derived MP to a base MP
820/// we subtract the offset from the adjustment because the address point
821/// has increased.
822///
823/// The standard forbids (at compile time) conversion to and from
824/// virtual bases, which is why we don't have to consider them here.
825///
826/// The standard forbids (at run time) casting a derived MP to a base
827/// MP when the derived MP does not point to a member of the base.
828/// This is why -1 is a reasonable choice for null data member
829/// pointers.
John McCalla1dee5302010-08-22 10:59:02 +0000830llvm::Value *
John McCall7a9aac22010-08-23 01:21:21 +0000831ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
832 const CastExpr *E,
John McCallc62bb392012-02-15 01:22:51 +0000833 llvm::Value *src) {
John McCalle3027922010-08-25 11:45:40 +0000834 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
John McCallc62bb392012-02-15 01:22:51 +0000835 E->getCastKind() == CK_BaseToDerivedMemberPointer ||
836 E->getCastKind() == CK_ReinterpretMemberPointer);
837
838 // Under Itanium, reinterprets don't require any additional processing.
839 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
840
841 // Use constant emission if we can.
842 if (isa<llvm::Constant>(src))
843 return EmitMemberPointerConversion(E, cast<llvm::Constant>(src));
844
845 llvm::Constant *adj = getMemberPointerAdjustment(E);
846 if (!adj) return src;
John McCalla8bbb822010-08-22 03:04:22 +0000847
848 CGBuilderTy &Builder = CGF.Builder;
John McCallc62bb392012-02-15 01:22:51 +0000849 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
John McCalla8bbb822010-08-22 03:04:22 +0000850
John McCallc62bb392012-02-15 01:22:51 +0000851 const MemberPointerType *destTy =
852 E->getType()->castAs<MemberPointerType>();
John McCall1c456c82010-08-22 06:43:33 +0000853
John McCall7a9aac22010-08-23 01:21:21 +0000854 // For member data pointers, this is just a matter of adding the
855 // offset if the source is non-null.
John McCallc62bb392012-02-15 01:22:51 +0000856 if (destTy->isMemberDataPointer()) {
857 llvm::Value *dst;
858 if (isDerivedToBase)
859 dst = Builder.CreateNSWSub(src, adj, "adj");
John McCall7a9aac22010-08-23 01:21:21 +0000860 else
John McCallc62bb392012-02-15 01:22:51 +0000861 dst = Builder.CreateNSWAdd(src, adj, "adj");
John McCall7a9aac22010-08-23 01:21:21 +0000862
863 // Null check.
John McCallc62bb392012-02-15 01:22:51 +0000864 llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType());
865 llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull");
866 return Builder.CreateSelect(isNull, src, dst);
John McCall7a9aac22010-08-23 01:21:21 +0000867 }
868
John McCalla1dee5302010-08-22 10:59:02 +0000869 // The this-adjustment is left-shifted by 1 on ARM.
Mark Seabornedf0d382013-07-24 16:25:13 +0000870 if (UseARMMethodPtrABI) {
John McCallc62bb392012-02-15 01:22:51 +0000871 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
872 offset <<= 1;
873 adj = llvm::ConstantInt::get(adj->getType(), offset);
John McCalla1dee5302010-08-22 10:59:02 +0000874 }
875
John McCallc62bb392012-02-15 01:22:51 +0000876 llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj");
877 llvm::Value *dstAdj;
878 if (isDerivedToBase)
879 dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj");
John McCalla1dee5302010-08-22 10:59:02 +0000880 else
John McCallc62bb392012-02-15 01:22:51 +0000881 dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj");
John McCalla1dee5302010-08-22 10:59:02 +0000882
John McCallc62bb392012-02-15 01:22:51 +0000883 return Builder.CreateInsertValue(src, dstAdj, 1);
884}
885
886llvm::Constant *
887ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E,
888 llvm::Constant *src) {
889 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
890 E->getCastKind() == CK_BaseToDerivedMemberPointer ||
891 E->getCastKind() == CK_ReinterpretMemberPointer);
892
893 // Under Itanium, reinterprets don't require any additional processing.
894 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
895
896 // If the adjustment is trivial, we don't need to do anything.
897 llvm::Constant *adj = getMemberPointerAdjustment(E);
898 if (!adj) return src;
899
900 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
901
902 const MemberPointerType *destTy =
903 E->getType()->castAs<MemberPointerType>();
904
905 // For member data pointers, this is just a matter of adding the
906 // offset if the source is non-null.
907 if (destTy->isMemberDataPointer()) {
908 // null maps to null.
909 if (src->isAllOnesValue()) return src;
910
911 if (isDerivedToBase)
912 return llvm::ConstantExpr::getNSWSub(src, adj);
913 else
914 return llvm::ConstantExpr::getNSWAdd(src, adj);
915 }
916
917 // The this-adjustment is left-shifted by 1 on ARM.
Mark Seabornedf0d382013-07-24 16:25:13 +0000918 if (UseARMMethodPtrABI) {
John McCallc62bb392012-02-15 01:22:51 +0000919 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
920 offset <<= 1;
921 adj = llvm::ConstantInt::get(adj->getType(), offset);
922 }
923
924 llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1);
925 llvm::Constant *dstAdj;
926 if (isDerivedToBase)
927 dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj);
928 else
929 dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj);
930
931 return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1);
John McCalla8bbb822010-08-22 03:04:22 +0000932}
John McCall84fa5102010-08-22 04:16:24 +0000933
934llvm::Constant *
John McCall7a9aac22010-08-23 01:21:21 +0000935ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
John McCall7a9aac22010-08-23 01:21:21 +0000936 // Itanium C++ ABI 2.3:
937 // A NULL pointer is represented as -1.
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000938 if (MPT->isMemberDataPointer())
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000939 return llvm::ConstantInt::get(CGM.PtrDiffTy, -1ULL, /*isSigned=*/true);
John McCalla1dee5302010-08-22 10:59:02 +0000940
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000941 llvm::Constant *Zero = llvm::ConstantInt::get(CGM.PtrDiffTy, 0);
John McCalla1dee5302010-08-22 10:59:02 +0000942 llvm::Constant *Values[2] = { Zero, Zero };
Chris Lattnere64d7ba2011-06-20 04:01:35 +0000943 return llvm::ConstantStruct::getAnon(Values);
John McCall84fa5102010-08-22 04:16:24 +0000944}
945
John McCallf3a88602011-02-03 08:15:49 +0000946llvm::Constant *
947ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
948 CharUnits offset) {
John McCall7a9aac22010-08-23 01:21:21 +0000949 // Itanium C++ ABI 2.3:
950 // A pointer to data member is an offset from the base address of
951 // the class object containing it, represented as a ptrdiff_t
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000952 return llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity());
John McCall7a9aac22010-08-23 01:21:21 +0000953}
954
David Majnemere2be95b2015-06-23 07:31:01 +0000955llvm::Constant *
956ItaniumCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) {
Richard Smithdafff942012-01-14 04:30:29 +0000957 return BuildMemberPointer(MD, CharUnits::Zero());
958}
959
960llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD,
961 CharUnits ThisAdjustment) {
John McCalla1dee5302010-08-22 10:59:02 +0000962 assert(MD->isInstance() && "Member function must not be static!");
John McCalla1dee5302010-08-22 10:59:02 +0000963
964 CodeGenTypes &Types = CGM.getTypes();
John McCalla1dee5302010-08-22 10:59:02 +0000965
966 // Get the function pointer (or index if this is a virtual function).
967 llvm::Constant *MemPtr[2];
968 if (MD->isVirtual()) {
Timur Iskhodzhanov58776632013-11-05 15:54:58 +0000969 uint64_t Index = CGM.getItaniumVTableContext().getMethodVTableIndex(MD);
John McCalla1dee5302010-08-22 10:59:02 +0000970
Ken Dyckdf016282011-04-09 01:30:02 +0000971 const ASTContext &Context = getContext();
972 CharUnits PointerWidth =
Douglas Gregore8bbc122011-09-02 00:18:52 +0000973 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Ken Dyckdf016282011-04-09 01:30:02 +0000974 uint64_t VTableOffset = (Index * PointerWidth.getQuantity());
John McCalla1dee5302010-08-22 10:59:02 +0000975
Mark Seabornedf0d382013-07-24 16:25:13 +0000976 if (UseARMMethodPtrABI) {
John McCalla1dee5302010-08-22 10:59:02 +0000977 // ARM C++ ABI 3.2.1:
978 // This ABI specifies that adj contains twice the this
979 // adjustment, plus 1 if the member function is virtual. The
980 // least significant bit of adj then makes exactly the same
981 // discrimination as the least significant bit of ptr does for
982 // Itanium.
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000983 MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset);
984 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
Richard Smithdafff942012-01-14 04:30:29 +0000985 2 * ThisAdjustment.getQuantity() + 1);
John McCalla1dee5302010-08-22 10:59:02 +0000986 } else {
987 // Itanium C++ ABI 2.3:
988 // For a virtual function, [the pointer field] is 1 plus the
989 // virtual table offset (in bytes) of the function,
990 // represented as a ptrdiff_t.
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000991 MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset + 1);
992 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
Richard Smithdafff942012-01-14 04:30:29 +0000993 ThisAdjustment.getQuantity());
John McCalla1dee5302010-08-22 10:59:02 +0000994 }
995 } else {
John McCall2979fe02011-04-12 00:42:48 +0000996 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
Chris Lattner2192fe52011-07-18 04:24:23 +0000997 llvm::Type *Ty;
John McCall2979fe02011-04-12 00:42:48 +0000998 // Check whether the function has a computable LLVM signature.
Chris Lattner8806e322011-07-10 00:18:59 +0000999 if (Types.isFuncTypeConvertible(FPT)) {
John McCall2979fe02011-04-12 00:42:48 +00001000 // The function has a computable LLVM signature; use the correct type.
John McCalla729c622012-02-17 03:33:10 +00001001 Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
John McCalla1dee5302010-08-22 10:59:02 +00001002 } else {
John McCall2979fe02011-04-12 00:42:48 +00001003 // Use an arbitrary non-function type to tell GetAddrOfFunction that the
1004 // function type is incomplete.
Reid Kleckner9cffbc12013-03-22 16:13:10 +00001005 Ty = CGM.PtrDiffTy;
John McCalla1dee5302010-08-22 10:59:02 +00001006 }
John McCall2979fe02011-04-12 00:42:48 +00001007 llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty);
John McCalla1dee5302010-08-22 10:59:02 +00001008
Reid Kleckner9cffbc12013-03-22 16:13:10 +00001009 MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, CGM.PtrDiffTy);
Mark Seabornedf0d382013-07-24 16:25:13 +00001010 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
1011 (UseARMMethodPtrABI ? 2 : 1) *
Richard Smithdafff942012-01-14 04:30:29 +00001012 ThisAdjustment.getQuantity());
John McCalla1dee5302010-08-22 10:59:02 +00001013 }
Jake Ehrlichc451cf22017-11-11 01:15:41 +00001014
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001015 return llvm::ConstantStruct::getAnon(MemPtr);
John McCall1c456c82010-08-22 06:43:33 +00001016}
1017
Richard Smithdafff942012-01-14 04:30:29 +00001018llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP,
1019 QualType MPType) {
1020 const MemberPointerType *MPT = MPType->castAs<MemberPointerType>();
1021 const ValueDecl *MPD = MP.getMemberPointerDecl();
1022 if (!MPD)
1023 return EmitNullMemberPointer(MPT);
1024
Reid Kleckner452abac2013-05-09 21:01:17 +00001025 CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP);
Richard Smithdafff942012-01-14 04:30:29 +00001026
1027 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD))
1028 return BuildMemberPointer(MD, ThisAdjustment);
1029
1030 CharUnits FieldOffset =
1031 getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD));
1032 return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset);
1033}
1034
John McCall131d97d2010-08-22 08:30:07 +00001035/// The comparison algorithm is pretty easy: the member pointers are
1036/// the same if they're either bitwise identical *or* both null.
1037///
1038/// ARM is different here only because null-ness is more complicated.
1039llvm::Value *
John McCall7a9aac22010-08-23 01:21:21 +00001040ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
1041 llvm::Value *L,
1042 llvm::Value *R,
1043 const MemberPointerType *MPT,
1044 bool Inequality) {
John McCall131d97d2010-08-22 08:30:07 +00001045 CGBuilderTy &Builder = CGF.Builder;
1046
John McCall131d97d2010-08-22 08:30:07 +00001047 llvm::ICmpInst::Predicate Eq;
1048 llvm::Instruction::BinaryOps And, Or;
1049 if (Inequality) {
1050 Eq = llvm::ICmpInst::ICMP_NE;
1051 And = llvm::Instruction::Or;
1052 Or = llvm::Instruction::And;
1053 } else {
1054 Eq = llvm::ICmpInst::ICMP_EQ;
1055 And = llvm::Instruction::And;
1056 Or = llvm::Instruction::Or;
1057 }
1058
John McCall7a9aac22010-08-23 01:21:21 +00001059 // Member data pointers are easy because there's a unique null
1060 // value, so it just comes down to bitwise equality.
1061 if (MPT->isMemberDataPointer())
1062 return Builder.CreateICmp(Eq, L, R);
1063
1064 // For member function pointers, the tautologies are more complex.
1065 // The Itanium tautology is:
John McCall61a14882010-08-23 06:56:36 +00001066 // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
John McCall7a9aac22010-08-23 01:21:21 +00001067 // The ARM tautology is:
John McCall61a14882010-08-23 06:56:36 +00001068 // (L == R) <==> (L.ptr == R.ptr &&
1069 // (L.adj == R.adj ||
1070 // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
John McCall7a9aac22010-08-23 01:21:21 +00001071 // The inequality tautologies have exactly the same structure, except
1072 // applying De Morgan's laws.
Jake Ehrlichc451cf22017-11-11 01:15:41 +00001073
John McCall7a9aac22010-08-23 01:21:21 +00001074 llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
1075 llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
1076
John McCall131d97d2010-08-22 08:30:07 +00001077 // This condition tests whether L.ptr == R.ptr. This must always be
1078 // true for equality to hold.
1079 llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
1080
1081 // This condition, together with the assumption that L.ptr == R.ptr,
1082 // tests whether the pointers are both null. ARM imposes an extra
1083 // condition.
1084 llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
1085 llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
1086
1087 // This condition tests whether L.adj == R.adj. If this isn't
1088 // true, the pointers are unequal unless they're both null.
John McCalla1dee5302010-08-22 10:59:02 +00001089 llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
1090 llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
John McCall131d97d2010-08-22 08:30:07 +00001091 llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
1092
1093 // Null member function pointers on ARM clear the low bit of Adj,
1094 // so the zero condition has to check that neither low bit is set.
Mark Seabornedf0d382013-07-24 16:25:13 +00001095 if (UseARMMethodPtrABI) {
John McCall131d97d2010-08-22 08:30:07 +00001096 llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
1097
1098 // Compute (l.adj | r.adj) & 1 and test it against zero.
1099 llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
1100 llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
1101 llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
1102 "cmp.or.adj");
1103 EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
1104 }
1105
1106 // Tie together all our conditions.
1107 llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
1108 Result = Builder.CreateBinOp(And, PtrEq, Result,
1109 Inequality ? "memptr.ne" : "memptr.eq");
1110 return Result;
1111}
1112
1113llvm::Value *
John McCall7a9aac22010-08-23 01:21:21 +00001114ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
1115 llvm::Value *MemPtr,
1116 const MemberPointerType *MPT) {
John McCall131d97d2010-08-22 08:30:07 +00001117 CGBuilderTy &Builder = CGF.Builder;
John McCall7a9aac22010-08-23 01:21:21 +00001118
1119 /// For member data pointers, this is just a check against -1.
1120 if (MPT->isMemberDataPointer()) {
Reid Kleckner9cffbc12013-03-22 16:13:10 +00001121 assert(MemPtr->getType() == CGM.PtrDiffTy);
John McCall7a9aac22010-08-23 01:21:21 +00001122 llvm::Value *NegativeOne =
1123 llvm::Constant::getAllOnesValue(MemPtr->getType());
1124 return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
1125 }
Jake Ehrlichc451cf22017-11-11 01:15:41 +00001126
Daniel Dunbar914bc412011-04-19 23:10:47 +00001127 // In Itanium, a member function pointer is not null if 'ptr' is not null.
John McCalla1dee5302010-08-22 10:59:02 +00001128 llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
John McCall131d97d2010-08-22 08:30:07 +00001129
1130 llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
1131 llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
1132
Daniel Dunbar914bc412011-04-19 23:10:47 +00001133 // On ARM, a member function pointer is also non-null if the low bit of 'adj'
1134 // (the virtual bit) is set.
Mark Seabornedf0d382013-07-24 16:25:13 +00001135 if (UseARMMethodPtrABI) {
John McCall131d97d2010-08-22 08:30:07 +00001136 llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
John McCalla1dee5302010-08-22 10:59:02 +00001137 llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
John McCall131d97d2010-08-22 08:30:07 +00001138 llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
Daniel Dunbar914bc412011-04-19 23:10:47 +00001139 llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero,
1140 "memptr.isvirtual");
1141 Result = Builder.CreateOr(Result, IsVirtual);
John McCall131d97d2010-08-22 08:30:07 +00001142 }
1143
1144 return Result;
1145}
John McCall1c456c82010-08-22 06:43:33 +00001146
Reid Kleckner40ca9132014-05-13 22:05:45 +00001147bool ItaniumCXXABI::classifyReturnType(CGFunctionInfo &FI) const {
1148 const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl();
1149 if (!RD)
1150 return false;
1151
Richard Smith96cd6712017-08-16 01:49:53 +00001152 // If C++ prohibits us from making a copy, return by address.
Reid Kleckneradb41982019-04-30 22:23:20 +00001153 if (!RD->canPassInRegisters()) {
John McCall7f416cc2015-09-08 08:05:57 +00001154 auto Align = CGM.getContext().getTypeAlignInChars(FI.getReturnType());
1155 FI.getReturnInfo() = ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
Reid Kleckner40ca9132014-05-13 22:05:45 +00001156 return true;
1157 }
Reid Kleckner40ca9132014-05-13 22:05:45 +00001158 return false;
1159}
1160
John McCall614dbdc2010-08-22 21:01:12 +00001161/// The Itanium ABI requires non-zero initialization only for data
1162/// member pointers, for which '0' is a valid offset.
1163bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
David Majnemer5fd33e02015-04-24 01:25:08 +00001164 return MPT->isMemberFunctionPointer();
John McCall84fa5102010-08-22 04:16:24 +00001165}
John McCall5d865c322010-08-31 07:33:07 +00001166
John McCall82fb8922012-09-25 10:10:39 +00001167/// The Itanium ABI always places an offset to the complete object
1168/// at entry -2 in the vtable.
David Majnemer08681372014-11-01 07:37:17 +00001169void ItaniumCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF,
1170 const CXXDeleteExpr *DE,
John McCall7f416cc2015-09-08 08:05:57 +00001171 Address Ptr,
David Majnemer08681372014-11-01 07:37:17 +00001172 QualType ElementType,
1173 const CXXDestructorDecl *Dtor) {
1174 bool UseGlobalDelete = DE->isGlobalDelete();
David Majnemer0c0b6d92014-10-31 20:09:12 +00001175 if (UseGlobalDelete) {
1176 // Derive the complete-object pointer, which is what we need
1177 // to pass to the deallocation function.
John McCall82fb8922012-09-25 10:10:39 +00001178
David Majnemer0c0b6d92014-10-31 20:09:12 +00001179 // Grab the vtable pointer as an intptr_t*.
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001180 auto *ClassDecl =
Simon Pilgrimf2805472019-10-02 20:45:16 +00001181 cast<CXXRecordDecl>(ElementType->castAs<RecordType>()->getDecl());
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001182 llvm::Value *VTable =
1183 CGF.GetVTablePtr(Ptr, CGF.IntPtrTy->getPointerTo(), ClassDecl);
John McCall82fb8922012-09-25 10:10:39 +00001184
David Majnemer0c0b6d92014-10-31 20:09:12 +00001185 // Track back to entry -2 and pull out the offset there.
1186 llvm::Value *OffsetPtr = CGF.Builder.CreateConstInBoundsGEP1_64(
1187 VTable, -2, "complete-offset.ptr");
John McCall7f416cc2015-09-08 08:05:57 +00001188 llvm::Value *Offset =
1189 CGF.Builder.CreateAlignedLoad(OffsetPtr, CGF.getPointerAlign());
David Majnemer0c0b6d92014-10-31 20:09:12 +00001190
1191 // Apply the offset.
John McCall7f416cc2015-09-08 08:05:57 +00001192 llvm::Value *CompletePtr =
1193 CGF.Builder.CreateBitCast(Ptr.getPointer(), CGF.Int8PtrTy);
David Majnemer0c0b6d92014-10-31 20:09:12 +00001194 CompletePtr = CGF.Builder.CreateInBoundsGEP(CompletePtr, Offset);
1195
1196 // If we're supposed to call the global delete, make sure we do so
1197 // even if the destructor throws.
David Majnemer08681372014-11-01 07:37:17 +00001198 CGF.pushCallObjectDeleteCleanup(DE->getOperatorDelete(), CompletePtr,
1199 ElementType);
David Majnemer0c0b6d92014-10-31 20:09:12 +00001200 }
1201
1202 // FIXME: Provide a source location here even though there's no
1203 // CXXMemberCallExpr for dtor call.
1204 CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting;
Marco Antognini88559632019-07-22 09:39:13 +00001205 EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, DE);
David Majnemer0c0b6d92014-10-31 20:09:12 +00001206
1207 if (UseGlobalDelete)
1208 CGF.PopCleanupBlock();
John McCall82fb8922012-09-25 10:10:39 +00001209}
1210
David Majnemer442d0a22014-11-25 07:20:20 +00001211void ItaniumCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) {
1212 // void __cxa_rethrow();
1213
1214 llvm::FunctionType *FTy =
Rui Ueyama49a3ad22019-07-16 04:46:31 +00001215 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
David Majnemer442d0a22014-11-25 07:20:20 +00001216
James Y Knight9871db02019-02-05 16:42:33 +00001217 llvm::FunctionCallee Fn = CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow");
David Majnemer442d0a22014-11-25 07:20:20 +00001218
1219 if (isNoReturn)
1220 CGF.EmitNoreturnRuntimeCallOrInvoke(Fn, None);
1221 else
1222 CGF.EmitRuntimeCallOrInvoke(Fn);
1223}
1224
James Y Knight9871db02019-02-05 16:42:33 +00001225static llvm::FunctionCallee getAllocateExceptionFn(CodeGenModule &CGM) {
David Majnemer7c237072015-03-05 00:46:22 +00001226 // void *__cxa_allocate_exception(size_t thrown_size);
1227
1228 llvm::FunctionType *FTy =
Rui Ueyama49a3ad22019-07-16 04:46:31 +00001229 llvm::FunctionType::get(CGM.Int8PtrTy, CGM.SizeTy, /*isVarArg=*/false);
David Majnemer7c237072015-03-05 00:46:22 +00001230
1231 return CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception");
1232}
1233
James Y Knight9871db02019-02-05 16:42:33 +00001234static llvm::FunctionCallee getThrowFn(CodeGenModule &CGM) {
David Majnemer7c237072015-03-05 00:46:22 +00001235 // void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
1236 // void (*dest) (void *));
1237
1238 llvm::Type *Args[3] = { CGM.Int8PtrTy, CGM.Int8PtrTy, CGM.Int8PtrTy };
1239 llvm::FunctionType *FTy =
Rui Ueyama49a3ad22019-07-16 04:46:31 +00001240 llvm::FunctionType::get(CGM.VoidTy, Args, /*isVarArg=*/false);
David Majnemer7c237072015-03-05 00:46:22 +00001241
1242 return CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
1243}
1244
1245void ItaniumCXXABI::emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) {
1246 QualType ThrowType = E->getSubExpr()->getType();
1247 // Now allocate the exception object.
1248 llvm::Type *SizeTy = CGF.ConvertType(getContext().getSizeType());
1249 uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity();
1250
James Y Knight9871db02019-02-05 16:42:33 +00001251 llvm::FunctionCallee AllocExceptionFn = getAllocateExceptionFn(CGM);
David Majnemer7c237072015-03-05 00:46:22 +00001252 llvm::CallInst *ExceptionPtr = CGF.EmitNounwindRuntimeCall(
1253 AllocExceptionFn, llvm::ConstantInt::get(SizeTy, TypeSize), "exception");
1254
Akira Hatanakac39a2432019-05-10 02:16:37 +00001255 CharUnits ExnAlign = CGF.getContext().getExnObjectAlignment();
John McCall7f416cc2015-09-08 08:05:57 +00001256 CGF.EmitAnyExprToExn(E->getSubExpr(), Address(ExceptionPtr, ExnAlign));
David Majnemer7c237072015-03-05 00:46:22 +00001257
1258 // Now throw the exception.
1259 llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType,
1260 /*ForEH=*/true);
1261
1262 // The address of the destructor. If the exception type has a
1263 // trivial destructor (or isn't a record), we just pass null.
1264 llvm::Constant *Dtor = nullptr;
1265 if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) {
1266 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
1267 if (!Record->hasTrivialDestructor()) {
1268 CXXDestructorDecl *DtorD = Record->getDestructor();
Peter Collingbourned1c5b282019-03-22 23:05:10 +00001269 Dtor = CGM.getAddrOfCXXStructor(GlobalDecl(DtorD, Dtor_Complete));
David Majnemer7c237072015-03-05 00:46:22 +00001270 Dtor = llvm::ConstantExpr::getBitCast(Dtor, CGM.Int8PtrTy);
1271 }
1272 }
1273 if (!Dtor) Dtor = llvm::Constant::getNullValue(CGM.Int8PtrTy);
1274
1275 llvm::Value *args[] = { ExceptionPtr, TypeInfo, Dtor };
1276 CGF.EmitNoreturnRuntimeCallOrInvoke(getThrowFn(CGM), args);
1277}
1278
James Y Knight9871db02019-02-05 16:42:33 +00001279static llvm::FunctionCallee getItaniumDynamicCastFn(CodeGenFunction &CGF) {
David Majnemer1162d252014-06-22 19:05:33 +00001280 // void *__dynamic_cast(const void *sub,
1281 // const abi::__class_type_info *src,
1282 // const abi::__class_type_info *dst,
1283 // std::ptrdiff_t src2dst_offset);
Jake Ehrlichc451cf22017-11-11 01:15:41 +00001284
David Majnemer1162d252014-06-22 19:05:33 +00001285 llvm::Type *Int8PtrTy = CGF.Int8PtrTy;
Jake Ehrlichc451cf22017-11-11 01:15:41 +00001286 llvm::Type *PtrDiffTy =
David Majnemer1162d252014-06-22 19:05:33 +00001287 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1288
1289 llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy };
1290
1291 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
1292
1293 // Mark the function as nounwind readonly.
1294 llvm::Attribute::AttrKind FuncAttrs[] = { llvm::Attribute::NoUnwind,
1295 llvm::Attribute::ReadOnly };
Reid Klecknerde864822017-03-21 16:57:30 +00001296 llvm::AttributeList Attrs = llvm::AttributeList::get(
1297 CGF.getLLVMContext(), llvm::AttributeList::FunctionIndex, FuncAttrs);
David Majnemer1162d252014-06-22 19:05:33 +00001298
1299 return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast", Attrs);
1300}
1301
James Y Knight9871db02019-02-05 16:42:33 +00001302static llvm::FunctionCallee getBadCastFn(CodeGenFunction &CGF) {
David Majnemer1162d252014-06-22 19:05:33 +00001303 // void __cxa_bad_cast();
1304 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
1305 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast");
1306}
1307
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001308/// Compute the src2dst_offset hint as described in the
David Majnemer1162d252014-06-22 19:05:33 +00001309/// Itanium C++ ABI [2.9.7]
1310static CharUnits computeOffsetHint(ASTContext &Context,
1311 const CXXRecordDecl *Src,
1312 const CXXRecordDecl *Dst) {
1313 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
1314 /*DetectVirtual=*/false);
1315
1316 // If Dst is not derived from Src we can skip the whole computation below and
1317 // return that Src is not a public base of Dst. Record all inheritance paths.
1318 if (!Dst->isDerivedFrom(Src, Paths))
1319 return CharUnits::fromQuantity(-2ULL);
1320
1321 unsigned NumPublicPaths = 0;
1322 CharUnits Offset;
1323
1324 // Now walk all possible inheritance paths.
Piotr Padlewski44b4ce82015-07-28 16:10:58 +00001325 for (const CXXBasePath &Path : Paths) {
1326 if (Path.Access != AS_public) // Ignore non-public inheritance.
David Majnemer1162d252014-06-22 19:05:33 +00001327 continue;
1328
1329 ++NumPublicPaths;
1330
Piotr Padlewski44b4ce82015-07-28 16:10:58 +00001331 for (const CXXBasePathElement &PathElement : Path) {
David Majnemer1162d252014-06-22 19:05:33 +00001332 // If the path contains a virtual base class we can't give any hint.
1333 // -1: no hint.
Piotr Padlewski44b4ce82015-07-28 16:10:58 +00001334 if (PathElement.Base->isVirtual())
David Majnemer1162d252014-06-22 19:05:33 +00001335 return CharUnits::fromQuantity(-1ULL);
1336
1337 if (NumPublicPaths > 1) // Won't use offsets, skip computation.
1338 continue;
1339
1340 // Accumulate the base class offsets.
Piotr Padlewski44b4ce82015-07-28 16:10:58 +00001341 const ASTRecordLayout &L = Context.getASTRecordLayout(PathElement.Class);
1342 Offset += L.getBaseClassOffset(
1343 PathElement.Base->getType()->getAsCXXRecordDecl());
David Majnemer1162d252014-06-22 19:05:33 +00001344 }
1345 }
1346
1347 // -2: Src is not a public base of Dst.
1348 if (NumPublicPaths == 0)
1349 return CharUnits::fromQuantity(-2ULL);
1350
1351 // -3: Src is a multiple public base type but never a virtual base type.
1352 if (NumPublicPaths > 1)
1353 return CharUnits::fromQuantity(-3ULL);
1354
1355 // Otherwise, the Src type is a unique public nonvirtual base type of Dst.
1356 // Return the offset of Src from the origin of Dst.
1357 return Offset;
1358}
1359
James Y Knight9871db02019-02-05 16:42:33 +00001360static llvm::FunctionCallee getBadTypeidFn(CodeGenFunction &CGF) {
David Majnemer1162d252014-06-22 19:05:33 +00001361 // void __cxa_bad_typeid();
1362 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
1363
1364 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid");
1365}
1366
1367bool ItaniumCXXABI::shouldTypeidBeNullChecked(bool IsDeref,
1368 QualType SrcRecordTy) {
1369 return IsDeref;
1370}
1371
1372void ItaniumCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) {
James Y Knight9871db02019-02-05 16:42:33 +00001373 llvm::FunctionCallee Fn = getBadTypeidFn(CGF);
James Y Knight3933add2019-01-30 02:54:28 +00001374 llvm::CallBase *Call = CGF.EmitRuntimeCallOrInvoke(Fn);
1375 Call->setDoesNotReturn();
David Majnemer1162d252014-06-22 19:05:33 +00001376 CGF.Builder.CreateUnreachable();
1377}
1378
1379llvm::Value *ItaniumCXXABI::EmitTypeid(CodeGenFunction &CGF,
1380 QualType SrcRecordTy,
John McCall7f416cc2015-09-08 08:05:57 +00001381 Address ThisPtr,
David Majnemer1162d252014-06-22 19:05:33 +00001382 llvm::Type *StdTypeInfoPtrTy) {
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001383 auto *ClassDecl =
Simon Pilgrimf2805472019-10-02 20:45:16 +00001384 cast<CXXRecordDecl>(SrcRecordTy->castAs<RecordType>()->getDecl());
David Majnemer1162d252014-06-22 19:05:33 +00001385 llvm::Value *Value =
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001386 CGF.GetVTablePtr(ThisPtr, StdTypeInfoPtrTy->getPointerTo(), ClassDecl);
David Majnemer1162d252014-06-22 19:05:33 +00001387
1388 // Load the type info.
1389 Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL);
John McCall7f416cc2015-09-08 08:05:57 +00001390 return CGF.Builder.CreateAlignedLoad(Value, CGF.getPointerAlign());
David Majnemer1162d252014-06-22 19:05:33 +00001391}
1392
1393bool ItaniumCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
1394 QualType SrcRecordTy) {
1395 return SrcIsPtr;
1396}
1397
1398llvm::Value *ItaniumCXXABI::EmitDynamicCastCall(
John McCall7f416cc2015-09-08 08:05:57 +00001399 CodeGenFunction &CGF, Address ThisAddr, QualType SrcRecordTy,
David Majnemer1162d252014-06-22 19:05:33 +00001400 QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) {
1401 llvm::Type *PtrDiffLTy =
1402 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1403 llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1404
1405 llvm::Value *SrcRTTI =
1406 CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType());
1407 llvm::Value *DestRTTI =
1408 CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType());
1409
1410 // Compute the offset hint.
1411 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
1412 const CXXRecordDecl *DestDecl = DestRecordTy->getAsCXXRecordDecl();
1413 llvm::Value *OffsetHint = llvm::ConstantInt::get(
1414 PtrDiffLTy,
1415 computeOffsetHint(CGF.getContext(), SrcDecl, DestDecl).getQuantity());
1416
1417 // Emit the call to __dynamic_cast.
John McCall7f416cc2015-09-08 08:05:57 +00001418 llvm::Value *Value = ThisAddr.getPointer();
David Majnemer1162d252014-06-22 19:05:33 +00001419 Value = CGF.EmitCastToVoidPtr(Value);
1420
1421 llvm::Value *args[] = {Value, SrcRTTI, DestRTTI, OffsetHint};
1422 Value = CGF.EmitNounwindRuntimeCall(getItaniumDynamicCastFn(CGF), args);
1423 Value = CGF.Builder.CreateBitCast(Value, DestLTy);
1424
1425 /// C++ [expr.dynamic.cast]p9:
1426 /// A failed cast to reference type throws std::bad_cast
1427 if (DestTy->isReferenceType()) {
1428 llvm::BasicBlock *BadCastBlock =
1429 CGF.createBasicBlock("dynamic_cast.bad_cast");
1430
1431 llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value);
1432 CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd);
1433
1434 CGF.EmitBlock(BadCastBlock);
1435 EmitBadCastCall(CGF);
1436 }
1437
1438 return Value;
1439}
1440
1441llvm::Value *ItaniumCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00001442 Address ThisAddr,
David Majnemer1162d252014-06-22 19:05:33 +00001443 QualType SrcRecordTy,
1444 QualType DestTy) {
1445 llvm::Type *PtrDiffLTy =
1446 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1447 llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1448
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001449 auto *ClassDecl =
Simon Pilgrimf2805472019-10-02 20:45:16 +00001450 cast<CXXRecordDecl>(SrcRecordTy->castAs<RecordType>()->getDecl());
David Majnemer1162d252014-06-22 19:05:33 +00001451 // Get the vtable pointer.
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001452 llvm::Value *VTable = CGF.GetVTablePtr(ThisAddr, PtrDiffLTy->getPointerTo(),
1453 ClassDecl);
David Majnemer1162d252014-06-22 19:05:33 +00001454
1455 // Get the offset-to-top from the vtable.
1456 llvm::Value *OffsetToTop =
1457 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL);
John McCall7f416cc2015-09-08 08:05:57 +00001458 OffsetToTop =
1459 CGF.Builder.CreateAlignedLoad(OffsetToTop, CGF.getPointerAlign(),
1460 "offset.to.top");
David Majnemer1162d252014-06-22 19:05:33 +00001461
1462 // Finally, add the offset to the pointer.
John McCall7f416cc2015-09-08 08:05:57 +00001463 llvm::Value *Value = ThisAddr.getPointer();
David Majnemer1162d252014-06-22 19:05:33 +00001464 Value = CGF.EmitCastToVoidPtr(Value);
1465 Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop);
1466
1467 return CGF.Builder.CreateBitCast(Value, DestLTy);
1468}
1469
1470bool ItaniumCXXABI::EmitBadCastCall(CodeGenFunction &CGF) {
James Y Knight9871db02019-02-05 16:42:33 +00001471 llvm::FunctionCallee Fn = getBadCastFn(CGF);
James Y Knight3933add2019-01-30 02:54:28 +00001472 llvm::CallBase *Call = CGF.EmitRuntimeCallOrInvoke(Fn);
1473 Call->setDoesNotReturn();
David Majnemer1162d252014-06-22 19:05:33 +00001474 CGF.Builder.CreateUnreachable();
1475 return true;
1476}
1477
Reid Klecknerd8cbeec2013-05-29 18:02:47 +00001478llvm::Value *
1479ItaniumCXXABI::GetVirtualBaseClassOffset(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00001480 Address This,
Reid Klecknerd8cbeec2013-05-29 18:02:47 +00001481 const CXXRecordDecl *ClassDecl,
1482 const CXXRecordDecl *BaseClassDecl) {
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001483 llvm::Value *VTablePtr = CGF.GetVTablePtr(This, CGM.Int8PtrTy, ClassDecl);
Reid Klecknerd8cbeec2013-05-29 18:02:47 +00001484 CharUnits VBaseOffsetOffset =
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001485 CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(ClassDecl,
1486 BaseClassDecl);
Reid Klecknerd8cbeec2013-05-29 18:02:47 +00001487
1488 llvm::Value *VBaseOffsetPtr =
1489 CGF.Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(),
1490 "vbase.offset.ptr");
1491 VBaseOffsetPtr = CGF.Builder.CreateBitCast(VBaseOffsetPtr,
1492 CGM.PtrDiffTy->getPointerTo());
1493
1494 llvm::Value *VBaseOffset =
John McCall7f416cc2015-09-08 08:05:57 +00001495 CGF.Builder.CreateAlignedLoad(VBaseOffsetPtr, CGF.getPointerAlign(),
1496 "vbase.offset");
Reid Klecknerd8cbeec2013-05-29 18:02:47 +00001497
1498 return VBaseOffset;
1499}
1500
Timur Iskhodzhanov40f2fa92013-08-04 17:30:04 +00001501void ItaniumCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) {
1502 // Just make sure we're in sync with TargetCXXABI.
1503 assert(CGM.getTarget().getCXXABI().hasConstructorVariants());
1504
Rafael Espindolac3cde362013-12-09 14:51:17 +00001505 // The constructor used for constructing this as a base class;
1506 // ignores virtual bases.
1507 CGM.EmitGlobal(GlobalDecl(D, Ctor_Base));
1508
Timur Iskhodzhanov40f2fa92013-08-04 17:30:04 +00001509 // The constructor used for constructing this as a complete class;
Nico Weber4c2ffb22015-01-07 05:25:05 +00001510 // constructs the virtual bases, then calls the base constructor.
Timur Iskhodzhanov40f2fa92013-08-04 17:30:04 +00001511 if (!D->getParent()->isAbstract()) {
1512 // We don't need to emit the complete ctor if the class is abstract.
1513 CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete));
1514 }
Timur Iskhodzhanov40f2fa92013-08-04 17:30:04 +00001515}
1516
George Burgess IVf203dbf2017-02-22 20:28:02 +00001517CGCXXABI::AddedStructorArgs
Peter Collingbourned1c5b282019-03-22 23:05:10 +00001518ItaniumCXXABI::buildStructorSignature(GlobalDecl GD,
Rafael Espindola8d2a19b2014-09-08 16:01:27 +00001519 SmallVectorImpl<CanQualType> &ArgTys) {
John McCall9bca9232010-09-02 10:25:57 +00001520 ASTContext &Context = getContext();
John McCall5d865c322010-08-31 07:33:07 +00001521
Rafael Espindola8d2a19b2014-09-08 16:01:27 +00001522 // All parameters are already in place except VTT, which goes after 'this'.
1523 // These are Clang types, so we don't need to worry about sret yet.
John McCall5d865c322010-08-31 07:33:07 +00001524
1525 // Check if we need to add a VTT parameter (which has type void **).
Peter Collingbourned1c5b282019-03-22 23:05:10 +00001526 if ((isa<CXXConstructorDecl>(GD.getDecl()) ? GD.getCtorType() == Ctor_Base
1527 : GD.getDtorType() == Dtor_Base) &&
1528 cast<CXXMethodDecl>(GD.getDecl())->getParent()->getNumVBases() != 0) {
Rafael Espindola8d2a19b2014-09-08 16:01:27 +00001529 ArgTys.insert(ArgTys.begin() + 1,
1530 Context.getPointerType(Context.VoidPtrTy));
George Burgess IVf203dbf2017-02-22 20:28:02 +00001531 return AddedStructorArgs::prefix(1);
1532 }
1533 return AddedStructorArgs{};
John McCall5d865c322010-08-31 07:33:07 +00001534}
1535
Reid Klecknere7de47e2013-07-22 13:51:44 +00001536void ItaniumCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) {
Rafael Espindolac3cde362013-12-09 14:51:17 +00001537 // The destructor used for destructing this as a base class; ignores
1538 // virtual bases.
1539 CGM.EmitGlobal(GlobalDecl(D, Dtor_Base));
Reid Klecknere7de47e2013-07-22 13:51:44 +00001540
1541 // The destructor used for destructing this as a most-derived class;
1542 // call the base destructor and then destructs any virtual bases.
1543 CGM.EmitGlobal(GlobalDecl(D, Dtor_Complete));
1544
Rafael Espindolac3cde362013-12-09 14:51:17 +00001545 // The destructor in a virtual table is always a 'deleting'
1546 // destructor, which calls the complete destructor and then uses the
1547 // appropriate operator delete.
1548 if (D->isVirtual())
1549 CGM.EmitGlobal(GlobalDecl(D, Dtor_Deleting));
Reid Klecknere7de47e2013-07-22 13:51:44 +00001550}
1551
Reid Kleckner89077a12013-12-17 19:46:40 +00001552void ItaniumCXXABI::addImplicitStructorParams(CodeGenFunction &CGF,
1553 QualType &ResTy,
1554 FunctionArgList &Params) {
John McCall5d865c322010-08-31 07:33:07 +00001555 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
Reid Kleckner89077a12013-12-17 19:46:40 +00001556 assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD));
John McCall5d865c322010-08-31 07:33:07 +00001557
1558 // Check if we need a VTT parameter as well.
Peter Collingbourne66f82e62013-06-28 20:45:28 +00001559 if (NeedsVTTParameter(CGF.CurGD)) {
John McCall9bca9232010-09-02 10:25:57 +00001560 ASTContext &Context = getContext();
John McCall5d865c322010-08-31 07:33:07 +00001561
1562 // FIXME: avoid the fake decl
1563 QualType T = Context.getPointerType(Context.VoidPtrTy);
Alexey Bataev56223232017-06-09 13:40:18 +00001564 auto *VTTDecl = ImplicitParamDecl::Create(
1565 Context, /*DC=*/nullptr, MD->getLocation(), &Context.Idents.get("vtt"),
1566 T, ImplicitParamDecl::CXXVTT);
Reid Kleckner89077a12013-12-17 19:46:40 +00001567 Params.insert(Params.begin() + 1, VTTDecl);
Reid Kleckner2af6d732013-12-13 00:09:59 +00001568 getStructorImplicitParamDecl(CGF) = VTTDecl;
John McCall5d865c322010-08-31 07:33:07 +00001569 }
1570}
1571
John McCall5d865c322010-08-31 07:33:07 +00001572void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
Justin Lebared4f1722016-07-27 22:04:24 +00001573 // Naked functions have no prolog.
1574 if (CGF.CurFuncDecl && CGF.CurFuncDecl->hasAttr<NakedAttr>())
1575 return;
1576
Reid Kleckner06239e42017-11-16 19:09:36 +00001577 /// Initialize the 'this' slot. In the Itanium C++ ABI, no prologue
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001578 /// adjustments are required, because they are all handled by thunks.
Reid Kleckner06239e42017-11-16 19:09:36 +00001579 setCXXABIThisValue(CGF, loadIncomingCXXThis(CGF));
John McCall5d865c322010-08-31 07:33:07 +00001580
1581 /// Initialize the 'vtt' slot if needed.
Reid Kleckner2af6d732013-12-13 00:09:59 +00001582 if (getStructorImplicitParamDecl(CGF)) {
1583 getStructorImplicitParamValue(CGF) = CGF.Builder.CreateLoad(
1584 CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), "vtt");
John McCall5d865c322010-08-31 07:33:07 +00001585 }
John McCall5d865c322010-08-31 07:33:07 +00001586
Stephen Lin9dc6eef2013-06-30 20:40:16 +00001587 /// If this is a function that the ABI specifies returns 'this', initialize
1588 /// the return slot to 'this' at the start of the function.
1589 ///
1590 /// Unlike the setting of return types, this is done within the ABI
1591 /// implementation instead of by clients of CGCXXABI because:
1592 /// 1) getThisValue is currently protected
1593 /// 2) in theory, an ABI could implement 'this' returns some other way;
1594 /// HasThisReturn only specifies a contract, not the implementation
John McCall5d865c322010-08-31 07:33:07 +00001595 if (HasThisReturn(CGF.CurGD))
Eli Friedman9fbeba02012-02-11 02:57:39 +00001596 CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
John McCall5d865c322010-08-31 07:33:07 +00001597}
1598
George Burgess IVf203dbf2017-02-22 20:28:02 +00001599CGCXXABI::AddedStructorArgs ItaniumCXXABI::addImplicitConstructorArgs(
Reid Kleckner89077a12013-12-17 19:46:40 +00001600 CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type,
1601 bool ForVirtualBase, bool Delegating, CallArgList &Args) {
1602 if (!NeedsVTTParameter(GlobalDecl(D, Type)))
George Burgess IVf203dbf2017-02-22 20:28:02 +00001603 return AddedStructorArgs{};
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001604
Reid Kleckner89077a12013-12-17 19:46:40 +00001605 // Insert the implicit 'vtt' argument as the second argument.
1606 llvm::Value *VTT =
1607 CGF.GetVTTParameter(GlobalDecl(D, Type), ForVirtualBase, Delegating);
1608 QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy);
Yaxun Liu5b330e82018-03-15 15:25:19 +00001609 Args.insert(Args.begin() + 1, CallArg(RValue::get(VTT), VTTTy));
George Burgess IVf203dbf2017-02-22 20:28:02 +00001610 return AddedStructorArgs::prefix(1); // Added one arg.
Reid Kleckner6fe771a2013-12-13 00:53:54 +00001611}
1612
1613void ItaniumCXXABI::EmitDestructorCall(CodeGenFunction &CGF,
1614 const CXXDestructorDecl *DD,
1615 CXXDtorType Type, bool ForVirtualBase,
Marco Antognini88559632019-07-22 09:39:13 +00001616 bool Delegating, Address This,
1617 QualType ThisTy) {
Reid Kleckner6fe771a2013-12-13 00:53:54 +00001618 GlobalDecl GD(DD, Type);
1619 llvm::Value *VTT = CGF.GetVTTParameter(GD, ForVirtualBase, Delegating);
1620 QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy);
1621
John McCallb92ab1a2016-10-26 23:46:34 +00001622 CGCallee Callee;
1623 if (getContext().getLangOpts().AppleKext &&
1624 Type != Dtor_Base && DD->isVirtual())
Reid Kleckner6fe771a2013-12-13 00:53:54 +00001625 Callee = CGF.BuildAppleKextVirtualDestructorCall(DD, Type, DD->getParent());
John McCallb92ab1a2016-10-26 23:46:34 +00001626 else
Peter Collingbourned1c5b282019-03-22 23:05:10 +00001627 Callee = CGCallee::forDirect(CGM.getAddrOfCXXStructor(GD), GD);
Reid Kleckner6fe771a2013-12-13 00:53:54 +00001628
Marco Antognini88559632019-07-22 09:39:13 +00001629 CGF.EmitCXXDestructorCall(GD, Callee, This.getPointer(), ThisTy, VTT, VTTTy,
1630 nullptr);
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001631}
1632
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001633void ItaniumCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT,
1634 const CXXRecordDecl *RD) {
1635 llvm::GlobalVariable *VTable = getAddrOfVTable(RD, CharUnits());
1636 if (VTable->hasInitializer())
1637 return;
1638
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001639 ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext();
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001640 const VTableLayout &VTLayout = VTContext.getVTableLayout(RD);
1641 llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD);
David Majnemerd905da42014-07-01 20:30:31 +00001642 llvm::Constant *RTTI =
1643 CGM.GetAddrOfRTTIDescriptor(CGM.getContext().getTagDeclType(RD));
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001644
1645 // Create and set the initializer.
John McCall9c6cb762016-11-28 22:18:33 +00001646 ConstantInitBuilder Builder(CGM);
Peter Collingbourne2849c4e2016-12-13 20:40:39 +00001647 auto Components = Builder.beginStruct();
John McCall9c6cb762016-11-28 22:18:33 +00001648 CGVT.createVTableInitializer(Components, VTLayout, RTTI);
1649 Components.finishAndSetAsInitializer(VTable);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001650
1651 // Set the correct linkage.
1652 VTable->setLinkage(Linkage);
1653
NAKAMURA Takumic7da6da2015-05-09 21:10:07 +00001654 if (CGM.supportsCOMDAT() && VTable->isWeakForLinker())
1655 VTable->setComdat(CGM.getModule().getOrInsertComdat(VTable->getName()));
Rafael Espindolacb92c192015-01-15 23:18:01 +00001656
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001657 // Set the right visibility.
Rafael Espindola699f5d62018-02-07 22:15:33 +00001658 CGM.setGVProperties(VTable, RD);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001659
1660 // If this is the magic class __cxxabiv1::__fundamental_type_info,
1661 // we will emit the typeinfo for the fundamental types. This is the
1662 // same behaviour as GCC.
1663 const DeclContext *DC = RD->getDeclContext();
1664 if (RD->getIdentifier() &&
1665 RD->getIdentifier()->isStr("__fundamental_type_info") &&
1666 isa<NamespaceDecl>(DC) && cast<NamespaceDecl>(DC)->getIdentifier() &&
1667 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") &&
1668 DC->getParent()->isTranslationUnit())
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00001669 EmitFundamentalRTTIDescriptors(RD);
Peter Collingbournea4ccff32015-02-20 20:30:56 +00001670
Evgeniy Stepanov93987df2016-01-23 01:20:18 +00001671 if (!VTable->isDeclarationForLinker())
Oliver Stannard9f6a8732019-10-11 11:59:55 +00001672 CGM.EmitVTableTypeMetadata(RD, VTable, VTLayout);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001673}
1674
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00001675bool ItaniumCXXABI::isVirtualOffsetNeededForVTableField(
1676 CodeGenFunction &CGF, CodeGenFunction::VPtr Vptr) {
1677 if (Vptr.NearestVBase == nullptr)
1678 return false;
1679 return NeedsVTTParameter(CGF.CurGD);
Piotr Padlewski255652e2015-09-09 22:20:28 +00001680}
1681
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00001682llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructor(
1683 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base,
1684 const CXXRecordDecl *NearestVBase) {
1685
1686 if ((Base.getBase()->getNumVBases() || NearestVBase != nullptr) &&
1687 NeedsVTTParameter(CGF.CurGD)) {
1688 return getVTableAddressPointInStructorWithVTT(CGF, VTableClass, Base,
1689 NearestVBase);
1690 }
1691 return getVTableAddressPoint(Base, VTableClass);
1692}
1693
1694llvm::Constant *
1695ItaniumCXXABI::getVTableAddressPoint(BaseSubobject Base,
1696 const CXXRecordDecl *VTableClass) {
1697 llvm::GlobalValue *VTable = getAddrOfVTable(VTableClass, CharUnits());
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001698
Peter Collingbourne2849c4e2016-12-13 20:40:39 +00001699 // Find the appropriate vtable within the vtable group, and the address point
1700 // within that vtable.
1701 VTableLayout::AddressPointLocation AddressPoint =
1702 CGM.getItaniumVTableContext()
1703 .getVTableLayout(VTableClass)
1704 .getAddressPoint(Base);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001705 llvm::Value *Indices[] = {
Peter Collingbourne4e6a5402016-03-14 19:07:10 +00001706 llvm::ConstantInt::get(CGM.Int32Ty, 0),
Peter Collingbourne2849c4e2016-12-13 20:40:39 +00001707 llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.VTableIndex),
1708 llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.AddressPointIndex),
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001709 };
1710
Peter Collingbourne25a2b702016-12-13 20:50:44 +00001711 return llvm::ConstantExpr::getGetElementPtr(VTable->getValueType(), VTable,
1712 Indices, /*InBounds=*/true,
1713 /*InRangeIndex=*/1);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001714}
1715
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00001716llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructorWithVTT(
1717 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base,
1718 const CXXRecordDecl *NearestVBase) {
1719 assert((Base.getBase()->getNumVBases() || NearestVBase != nullptr) &&
1720 NeedsVTTParameter(CGF.CurGD) && "This class doesn't have VTT");
1721
1722 // Get the secondary vpointer index.
1723 uint64_t VirtualPointerIndex =
1724 CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base);
1725
1726 /// Load the VTT.
1727 llvm::Value *VTT = CGF.LoadCXXVTT();
1728 if (VirtualPointerIndex)
1729 VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex);
1730
1731 // And load the address point from the VTT.
1732 return CGF.Builder.CreateAlignedLoad(VTT, CGF.getPointerAlign());
1733}
1734
1735llvm::Constant *ItaniumCXXABI::getVTableAddressPointForConstExpr(
1736 BaseSubobject Base, const CXXRecordDecl *VTableClass) {
1737 return getVTableAddressPoint(Base, VTableClass);
1738}
1739
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001740llvm::GlobalVariable *ItaniumCXXABI::getAddrOfVTable(const CXXRecordDecl *RD,
1741 CharUnits VPtrOffset) {
1742 assert(VPtrOffset.isZero() && "Itanium ABI only supports zero vptr offsets");
1743
1744 llvm::GlobalVariable *&VTable = VTables[RD];
1745 if (VTable)
1746 return VTable;
1747
Eric Christopherd160c502016-01-29 01:35:53 +00001748 // Queue up this vtable for possible deferred emission.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001749 CGM.addDeferredVTable(RD);
1750
Yaron Kerene46f7ed2015-07-29 14:21:47 +00001751 SmallString<256> Name;
1752 llvm::raw_svector_ostream Out(Name);
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00001753 getMangleContext().mangleCXXVTable(RD, Out);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001754
Peter Collingbourne2849c4e2016-12-13 20:40:39 +00001755 const VTableLayout &VTLayout =
1756 CGM.getItaniumVTableContext().getVTableLayout(RD);
1757 llvm::Type *VTableType = CGM.getVTables().getVTableType(VTLayout);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001758
David Greenbe0c5b62018-09-12 14:09:06 +00001759 // Use pointer alignment for the vtable. Otherwise we would align them based
1760 // on the size of the initializer which doesn't make sense as only single
1761 // values are read.
1762 unsigned PAlign = CGM.getTarget().getPointerAlign(0);
1763
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001764 VTable = CGM.CreateOrReplaceCXXRuntimeVariable(
David Greenbe0c5b62018-09-12 14:09:06 +00001765 Name, VTableType, llvm::GlobalValue::ExternalLinkage,
1766 getContext().toCharUnitsFromBits(PAlign).getQuantity());
Peter Collingbournebcf909d2016-06-14 21:02:05 +00001767 VTable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
Hans Wennborgda24e9c2014-06-02 23:13:03 +00001768
Rafael Espindola922f2aa2018-02-23 19:30:48 +00001769 CGM.setGVProperties(VTable, RD);
1770
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001771 return VTable;
1772}
1773
John McCall9831b842018-02-06 18:52:44 +00001774CGCallee ItaniumCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF,
1775 GlobalDecl GD,
1776 Address This,
1777 llvm::Type *Ty,
1778 SourceLocation Loc) {
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +00001779 Ty = Ty->getPointerTo()->getPointerTo();
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001780 auto *MethodDecl = cast<CXXMethodDecl>(GD.getDecl());
1781 llvm::Value *VTable = CGF.GetVTablePtr(This, Ty, MethodDecl->getParent());
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +00001782
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001783 uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD);
John McCall9831b842018-02-06 18:52:44 +00001784 llvm::Value *VFunc;
1785 if (CGF.ShouldEmitVTableTypeCheckedLoad(MethodDecl->getParent())) {
1786 VFunc = CGF.EmitVTableTypeCheckedLoad(
Peter Collingbourne0ca03632016-06-25 00:24:06 +00001787 MethodDecl->getParent(), VTable,
1788 VTableIndex * CGM.getContext().getTargetInfo().getPointerWidth(0) / 8);
John McCall9831b842018-02-06 18:52:44 +00001789 } else {
1790 CGF.EmitTypeMetadataCodeForVCall(MethodDecl->getParent(), VTable, Loc);
Peter Collingbourne0ca03632016-06-25 00:24:06 +00001791
John McCall9831b842018-02-06 18:52:44 +00001792 llvm::Value *VFuncPtr =
1793 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn");
1794 auto *VFuncLoad =
1795 CGF.Builder.CreateAlignedLoad(VFuncPtr, CGF.getPointerAlign());
Piotr Padlewski77cc9622016-10-29 15:28:30 +00001796
John McCall9831b842018-02-06 18:52:44 +00001797 // Add !invariant.load md to virtual function load to indicate that
1798 // function didn't change inside vtable.
1799 // It's safe to add it without -fstrict-vtable-pointers, but it would not
1800 // help in devirtualization because it will only matter if we will have 2
1801 // the same virtual function loads from the same vtable load, which won't
1802 // happen without enabled devirtualization with -fstrict-vtable-pointers.
1803 if (CGM.getCodeGenOpts().OptimizationLevel > 0 &&
1804 CGM.getCodeGenOpts().StrictVTablePointers)
1805 VFuncLoad->setMetadata(
1806 llvm::LLVMContext::MD_invariant_load,
1807 llvm::MDNode::get(CGM.getLLVMContext(),
1808 llvm::ArrayRef<llvm::Metadata *>()));
1809 VFunc = VFuncLoad;
1810 }
John McCallb92ab1a2016-10-26 23:46:34 +00001811
Erich Keanede6480a32018-11-13 15:48:08 +00001812 CGCallee Callee(GD, VFunc);
John McCall9831b842018-02-06 18:52:44 +00001813 return Callee;
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +00001814}
1815
David Majnemer0c0b6d92014-10-31 20:09:12 +00001816llvm::Value *ItaniumCXXABI::EmitVirtualDestructorCall(
1817 CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType,
Marco Antognini88559632019-07-22 09:39:13 +00001818 Address This, DeleteOrMemberCallExpr E) {
1819 auto *CE = E.dyn_cast<const CXXMemberCallExpr *>();
1820 auto *D = E.dyn_cast<const CXXDeleteExpr *>();
1821 assert((CE != nullptr) ^ (D != nullptr));
Alexey Samsonova5bf76b2014-08-25 20:17:35 +00001822 assert(CE == nullptr || CE->arg_begin() == CE->arg_end());
Timur Iskhodzhanovd6197112013-02-15 14:45:22 +00001823 assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete);
1824
Peter Collingbourned1c5b282019-03-22 23:05:10 +00001825 GlobalDecl GD(Dtor, DtorType);
1826 const CGFunctionInfo *FInfo =
1827 &CGM.getTypes().arrangeCXXStructorDeclaration(GD);
George Burgess IV00f70bd2018-03-01 05:43:23 +00001828 llvm::FunctionType *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo);
Peter Collingbourned1c5b282019-03-22 23:05:10 +00001829 CGCallee Callee = CGCallee::forVirtual(CE, GD, This, Ty);
Timur Iskhodzhanovd6197112013-02-15 14:45:22 +00001830
Marco Antognini88559632019-07-22 09:39:13 +00001831 QualType ThisTy;
1832 if (CE) {
1833 ThisTy = CE->getObjectType();
1834 } else {
1835 ThisTy = D->getDestroyedType();
1836 }
1837
1838 CGF.EmitCXXDestructorCall(GD, Callee, This.getPointer(), ThisTy, nullptr,
1839 QualType(), nullptr);
David Majnemer0c0b6d92014-10-31 20:09:12 +00001840 return nullptr;
Timur Iskhodzhanovd6197112013-02-15 14:45:22 +00001841}
1842
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001843void ItaniumCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) {
Reid Kleckner7810af02013-06-19 15:20:38 +00001844 CodeGenVTables &VTables = CGM.getVTables();
1845 llvm::GlobalVariable *VTT = VTables.GetAddrOfVTT(RD);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001846 VTables.EmitVTTDefinition(VTT, CGM.getVTableLinkage(RD), RD);
Reid Kleckner7810af02013-06-19 15:20:38 +00001847}
1848
Richard Smithc195c252018-11-27 19:33:49 +00001849bool ItaniumCXXABI::canSpeculativelyEmitVTableAsBaseClass(
1850 const CXXRecordDecl *RD) const {
Piotr Padlewskia68a7872015-07-24 04:04:49 +00001851 // We don't emit available_externally vtables if we are in -fapple-kext mode
1852 // because kext mode does not permit devirtualization.
1853 if (CGM.getLangOpts().AppleKext)
1854 return false;
1855
Piotr Padlewskie368de32018-06-13 13:55:42 +00001856 // If the vtable is hidden then it is not safe to emit an available_externally
1857 // copy of vtable.
1858 if (isVTableHidden(RD))
1859 return false;
1860
1861 if (CGM.getCodeGenOpts().ForceEmitVTables)
1862 return true;
1863
1864 // If we don't have any not emitted inline virtual function then we are safe
1865 // to emit an available_externally copy of vtable.
Piotr Padlewskia68a7872015-07-24 04:04:49 +00001866 // FIXME we can still emit a copy of the vtable if we
1867 // can emit definition of the inline functions.
Richard Smithc195c252018-11-27 19:33:49 +00001868 if (hasAnyUnusedVirtualInlineFunction(RD))
1869 return false;
1870
1871 // For a class with virtual bases, we must also be able to speculatively
1872 // emit the VTT, because CodeGen doesn't have separate notions of "can emit
1873 // the vtable" and "can emit the VTT". For a base subobject, this means we
1874 // need to be able to emit non-virtual base vtables.
1875 if (RD->getNumVBases()) {
1876 for (const auto &B : RD->bases()) {
1877 auto *BRD = B.getType()->getAsCXXRecordDecl();
1878 assert(BRD && "no class for base specifier");
1879 if (B.isVirtual() || !BRD->isDynamicClass())
1880 continue;
1881 if (!canSpeculativelyEmitVTableAsBaseClass(BRD))
1882 return false;
1883 }
1884 }
1885
1886 return true;
1887}
1888
1889bool ItaniumCXXABI::canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const {
1890 if (!canSpeculativelyEmitVTableAsBaseClass(RD))
1891 return false;
1892
1893 // For a complete-object vtable (or more specifically, for the VTT), we need
1894 // to be able to speculatively emit the vtables of all dynamic virtual bases.
1895 for (const auto &B : RD->vbases()) {
1896 auto *BRD = B.getType()->getAsCXXRecordDecl();
1897 assert(BRD && "no class for base specifier");
1898 if (!BRD->isDynamicClass())
1899 continue;
1900 if (!canSpeculativelyEmitVTableAsBaseClass(BRD))
1901 return false;
1902 }
1903
1904 return true;
Piotr Padlewskia68a7872015-07-24 04:04:49 +00001905}
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001906static llvm::Value *performTypeAdjustment(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00001907 Address InitialPtr,
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001908 int64_t NonVirtualAdjustment,
1909 int64_t VirtualAdjustment,
1910 bool IsReturnAdjustment) {
1911 if (!NonVirtualAdjustment && !VirtualAdjustment)
John McCall7f416cc2015-09-08 08:05:57 +00001912 return InitialPtr.getPointer();
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001913
John McCall7f416cc2015-09-08 08:05:57 +00001914 Address V = CGF.Builder.CreateElementBitCast(InitialPtr, CGF.Int8Ty);
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001915
John McCall7f416cc2015-09-08 08:05:57 +00001916 // In a base-to-derived cast, the non-virtual adjustment is applied first.
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001917 if (NonVirtualAdjustment && !IsReturnAdjustment) {
John McCall7f416cc2015-09-08 08:05:57 +00001918 V = CGF.Builder.CreateConstInBoundsByteGEP(V,
1919 CharUnits::fromQuantity(NonVirtualAdjustment));
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001920 }
1921
John McCall7f416cc2015-09-08 08:05:57 +00001922 // Perform the virtual adjustment if we have one.
1923 llvm::Value *ResultPtr;
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001924 if (VirtualAdjustment) {
1925 llvm::Type *PtrDiffTy =
1926 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1927
John McCall7f416cc2015-09-08 08:05:57 +00001928 Address VTablePtrPtr = CGF.Builder.CreateElementBitCast(V, CGF.Int8PtrTy);
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001929 llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr);
1930
1931 llvm::Value *OffsetPtr =
1932 CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
1933
1934 OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo());
1935
1936 // Load the adjustment offset from the vtable.
John McCall7f416cc2015-09-08 08:05:57 +00001937 llvm::Value *Offset =
1938 CGF.Builder.CreateAlignedLoad(OffsetPtr, CGF.getPointerAlign());
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001939
1940 // Adjust our pointer.
John McCall7f416cc2015-09-08 08:05:57 +00001941 ResultPtr = CGF.Builder.CreateInBoundsGEP(V.getPointer(), Offset);
1942 } else {
1943 ResultPtr = V.getPointer();
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001944 }
1945
John McCall7f416cc2015-09-08 08:05:57 +00001946 // In a derived-to-base conversion, the non-virtual adjustment is
1947 // applied second.
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001948 if (NonVirtualAdjustment && IsReturnAdjustment) {
John McCall7f416cc2015-09-08 08:05:57 +00001949 ResultPtr = CGF.Builder.CreateConstInBoundsGEP1_64(ResultPtr,
1950 NonVirtualAdjustment);
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001951 }
1952
1953 // Cast back to the original type.
John McCall7f416cc2015-09-08 08:05:57 +00001954 return CGF.Builder.CreateBitCast(ResultPtr, InitialPtr.getType());
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001955}
1956
1957llvm::Value *ItaniumCXXABI::performThisAdjustment(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00001958 Address This,
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001959 const ThisAdjustment &TA) {
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00001960 return performTypeAdjustment(CGF, This, TA.NonVirtual,
1961 TA.Virtual.Itanium.VCallOffsetOffset,
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001962 /*IsReturnAdjustment=*/false);
1963}
1964
1965llvm::Value *
John McCall7f416cc2015-09-08 08:05:57 +00001966ItaniumCXXABI::performReturnAdjustment(CodeGenFunction &CGF, Address Ret,
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001967 const ReturnAdjustment &RA) {
1968 return performTypeAdjustment(CGF, Ret, RA.NonVirtual,
1969 RA.Virtual.Itanium.VBaseOffsetOffset,
1970 /*IsReturnAdjustment=*/true);
1971}
1972
John McCall5d865c322010-08-31 07:33:07 +00001973void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
1974 RValue RV, QualType ResultType) {
1975 if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
1976 return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
1977
1978 // Destructor thunks in the ARM ABI have indeterminate results.
John McCall7f416cc2015-09-08 08:05:57 +00001979 llvm::Type *T = CGF.ReturnValue.getElementType();
John McCall5d865c322010-08-31 07:33:07 +00001980 RValue Undef = RValue::get(llvm::UndefValue::get(T));
1981 return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
1982}
John McCall8ed55a52010-09-02 09:58:18 +00001983
1984/************************** Array allocation cookies **************************/
1985
John McCallb91cd662012-05-01 05:23:51 +00001986CharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) {
1987 // The array cookie is a size_t; pad that up to the element alignment.
1988 // The cookie is actually right-justified in that space.
1989 return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes),
1990 CGM.getContext().getTypeAlignInChars(elementType));
John McCall8ed55a52010-09-02 09:58:18 +00001991}
1992
John McCall7f416cc2015-09-08 08:05:57 +00001993Address ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
1994 Address NewPtr,
1995 llvm::Value *NumElements,
1996 const CXXNewExpr *expr,
1997 QualType ElementType) {
John McCallb91cd662012-05-01 05:23:51 +00001998 assert(requiresArrayCookie(expr));
John McCall8ed55a52010-09-02 09:58:18 +00001999
John McCall7f416cc2015-09-08 08:05:57 +00002000 unsigned AS = NewPtr.getAddressSpace();
John McCall8ed55a52010-09-02 09:58:18 +00002001
John McCall9bca9232010-09-02 10:25:57 +00002002 ASTContext &Ctx = getContext();
John McCall7f416cc2015-09-08 08:05:57 +00002003 CharUnits SizeSize = CGF.getSizeSize();
John McCall8ed55a52010-09-02 09:58:18 +00002004
2005 // The size of the cookie.
2006 CharUnits CookieSize =
2007 std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
John McCallb91cd662012-05-01 05:23:51 +00002008 assert(CookieSize == getArrayCookieSizeImpl(ElementType));
John McCall8ed55a52010-09-02 09:58:18 +00002009
2010 // Compute an offset to the cookie.
John McCall7f416cc2015-09-08 08:05:57 +00002011 Address CookiePtr = NewPtr;
John McCall8ed55a52010-09-02 09:58:18 +00002012 CharUnits CookieOffset = CookieSize - SizeSize;
2013 if (!CookieOffset.isZero())
John McCall7f416cc2015-09-08 08:05:57 +00002014 CookiePtr = CGF.Builder.CreateConstInBoundsByteGEP(CookiePtr, CookieOffset);
John McCall8ed55a52010-09-02 09:58:18 +00002015
2016 // Write the number of elements into the appropriate slot.
John McCall7f416cc2015-09-08 08:05:57 +00002017 Address NumElementsPtr =
2018 CGF.Builder.CreateElementBitCast(CookiePtr, CGF.SizeTy);
Kostya Serebryany4ee69042014-08-26 02:29:59 +00002019 llvm::Instruction *SI = CGF.Builder.CreateStore(NumElements, NumElementsPtr);
John McCall7f416cc2015-09-08 08:05:57 +00002020
2021 // Handle the array cookie specially in ASan.
Filipe Cabecinhas6f83fa92018-01-02 13:46:12 +00002022 if (CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) && AS == 0 &&
Filipe Cabecinhas4ba58172018-02-12 11:49:02 +00002023 (expr->getOperatorNew()->isReplaceableGlobalAllocationFunction() ||
Filipe Cabecinhas0eb50082018-11-02 17:29:04 +00002024 CGM.getCodeGenOpts().SanitizeAddressPoisonCustomArrayCookie)) {
Kostya Serebryany4a9187a2014-08-29 01:01:32 +00002025 // The store to the CookiePtr does not need to be instrumented.
Kostya Serebryany4ee69042014-08-26 02:29:59 +00002026 CGM.getSanitizerMetadata()->disableSanitizerForInstruction(SI);
2027 llvm::FunctionType *FTy =
John McCall7f416cc2015-09-08 08:05:57 +00002028 llvm::FunctionType::get(CGM.VoidTy, NumElementsPtr.getType(), false);
James Y Knight9871db02019-02-05 16:42:33 +00002029 llvm::FunctionCallee F =
Kostya Serebryany4ee69042014-08-26 02:29:59 +00002030 CGM.CreateRuntimeFunction(FTy, "__asan_poison_cxx_array_cookie");
John McCall7f416cc2015-09-08 08:05:57 +00002031 CGF.Builder.CreateCall(F, NumElementsPtr.getPointer());
Kostya Serebryany4ee69042014-08-26 02:29:59 +00002032 }
John McCall8ed55a52010-09-02 09:58:18 +00002033
2034 // Finally, compute a pointer to the actual data buffer by skipping
2035 // over the cookie completely.
John McCall7f416cc2015-09-08 08:05:57 +00002036 return CGF.Builder.CreateConstInBoundsByteGEP(NewPtr, CookieSize);
John McCall8ed55a52010-09-02 09:58:18 +00002037}
2038
John McCallb91cd662012-05-01 05:23:51 +00002039llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00002040 Address allocPtr,
John McCallb91cd662012-05-01 05:23:51 +00002041 CharUnits cookieSize) {
2042 // The element size is right-justified in the cookie.
John McCall7f416cc2015-09-08 08:05:57 +00002043 Address numElementsPtr = allocPtr;
2044 CharUnits numElementsOffset = cookieSize - CGF.getSizeSize();
John McCallb91cd662012-05-01 05:23:51 +00002045 if (!numElementsOffset.isZero())
2046 numElementsPtr =
John McCall7f416cc2015-09-08 08:05:57 +00002047 CGF.Builder.CreateConstInBoundsByteGEP(numElementsPtr, numElementsOffset);
John McCall8ed55a52010-09-02 09:58:18 +00002048
John McCall7f416cc2015-09-08 08:05:57 +00002049 unsigned AS = allocPtr.getAddressSpace();
2050 numElementsPtr = CGF.Builder.CreateElementBitCast(numElementsPtr, CGF.SizeTy);
Alexey Samsonovedf99a92014-11-07 22:29:38 +00002051 if (!CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) || AS != 0)
Kostya Serebryany4a9187a2014-08-29 01:01:32 +00002052 return CGF.Builder.CreateLoad(numElementsPtr);
2053 // In asan mode emit a function call instead of a regular load and let the
2054 // run-time deal with it: if the shadow is properly poisoned return the
2055 // cookie, otherwise return 0 to avoid an infinite loop calling DTORs.
2056 // We can't simply ignore this load using nosanitize metadata because
2057 // the metadata may be lost.
2058 llvm::FunctionType *FTy =
2059 llvm::FunctionType::get(CGF.SizeTy, CGF.SizeTy->getPointerTo(0), false);
James Y Knight9871db02019-02-05 16:42:33 +00002060 llvm::FunctionCallee F =
Kostya Serebryany4a9187a2014-08-29 01:01:32 +00002061 CGM.CreateRuntimeFunction(FTy, "__asan_load_cxx_array_cookie");
John McCall7f416cc2015-09-08 08:05:57 +00002062 return CGF.Builder.CreateCall(F, numElementsPtr.getPointer());
John McCall8ed55a52010-09-02 09:58:18 +00002063}
2064
John McCallb91cd662012-05-01 05:23:51 +00002065CharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) {
John McCallc19c7062013-01-25 23:36:19 +00002066 // ARM says that the cookie is always:
John McCall8ed55a52010-09-02 09:58:18 +00002067 // struct array_cookie {
2068 // std::size_t element_size; // element_size != 0
2069 // std::size_t element_count;
2070 // };
John McCallc19c7062013-01-25 23:36:19 +00002071 // But the base ABI doesn't give anything an alignment greater than
2072 // 8, so we can dismiss this as typical ABI-author blindness to
2073 // actual language complexity and round up to the element alignment.
2074 return std::max(CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes),
2075 CGM.getContext().getTypeAlignInChars(elementType));
John McCall8ed55a52010-09-02 09:58:18 +00002076}
2077
John McCall7f416cc2015-09-08 08:05:57 +00002078Address ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
2079 Address newPtr,
2080 llvm::Value *numElements,
2081 const CXXNewExpr *expr,
2082 QualType elementType) {
John McCallb91cd662012-05-01 05:23:51 +00002083 assert(requiresArrayCookie(expr));
John McCall8ed55a52010-09-02 09:58:18 +00002084
John McCall8ed55a52010-09-02 09:58:18 +00002085 // The cookie is always at the start of the buffer.
John McCall7f416cc2015-09-08 08:05:57 +00002086 Address cookie = newPtr;
John McCall8ed55a52010-09-02 09:58:18 +00002087
2088 // The first element is the element size.
John McCall7f416cc2015-09-08 08:05:57 +00002089 cookie = CGF.Builder.CreateElementBitCast(cookie, CGF.SizeTy);
John McCallc19c7062013-01-25 23:36:19 +00002090 llvm::Value *elementSize = llvm::ConstantInt::get(CGF.SizeTy,
2091 getContext().getTypeSizeInChars(elementType).getQuantity());
2092 CGF.Builder.CreateStore(elementSize, cookie);
John McCall8ed55a52010-09-02 09:58:18 +00002093
2094 // The second element is the element count.
James Y Knight751fe282019-02-09 22:22:28 +00002095 cookie = CGF.Builder.CreateConstInBoundsGEP(cookie, 1);
John McCallc19c7062013-01-25 23:36:19 +00002096 CGF.Builder.CreateStore(numElements, cookie);
John McCall8ed55a52010-09-02 09:58:18 +00002097
2098 // Finally, compute a pointer to the actual data buffer by skipping
2099 // over the cookie completely.
John McCallc19c7062013-01-25 23:36:19 +00002100 CharUnits cookieSize = ARMCXXABI::getArrayCookieSizeImpl(elementType);
John McCall7f416cc2015-09-08 08:05:57 +00002101 return CGF.Builder.CreateConstInBoundsByteGEP(newPtr, cookieSize);
John McCall8ed55a52010-09-02 09:58:18 +00002102}
2103
John McCallb91cd662012-05-01 05:23:51 +00002104llvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00002105 Address allocPtr,
John McCallb91cd662012-05-01 05:23:51 +00002106 CharUnits cookieSize) {
2107 // The number of elements is at offset sizeof(size_t) relative to
2108 // the allocated pointer.
John McCall7f416cc2015-09-08 08:05:57 +00002109 Address numElementsPtr
2110 = CGF.Builder.CreateConstInBoundsByteGEP(allocPtr, CGF.getSizeSize());
John McCall8ed55a52010-09-02 09:58:18 +00002111
John McCall7f416cc2015-09-08 08:05:57 +00002112 numElementsPtr = CGF.Builder.CreateElementBitCast(numElementsPtr, CGF.SizeTy);
John McCallb91cd662012-05-01 05:23:51 +00002113 return CGF.Builder.CreateLoad(numElementsPtr);
John McCall8ed55a52010-09-02 09:58:18 +00002114}
2115
John McCall68ff0372010-09-08 01:44:27 +00002116/*********************** Static local initialization **************************/
2117
James Y Knight9871db02019-02-05 16:42:33 +00002118static llvm::FunctionCallee getGuardAcquireFn(CodeGenModule &CGM,
2119 llvm::PointerType *GuardPtrTy) {
John McCall68ff0372010-09-08 01:44:27 +00002120 // int __cxa_guard_acquire(__guard *guard_object);
Chris Lattner2192fe52011-07-18 04:24:23 +00002121 llvm::FunctionType *FTy =
John McCall68ff0372010-09-08 01:44:27 +00002122 llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy),
Jay Foad5709f7c2011-07-29 13:56:53 +00002123 GuardPtrTy, /*isVarArg=*/false);
Reid Klecknerde864822017-03-21 16:57:30 +00002124 return CGM.CreateRuntimeFunction(
2125 FTy, "__cxa_guard_acquire",
2126 llvm::AttributeList::get(CGM.getLLVMContext(),
2127 llvm::AttributeList::FunctionIndex,
2128 llvm::Attribute::NoUnwind));
John McCall68ff0372010-09-08 01:44:27 +00002129}
2130
James Y Knight9871db02019-02-05 16:42:33 +00002131static llvm::FunctionCallee getGuardReleaseFn(CodeGenModule &CGM,
2132 llvm::PointerType *GuardPtrTy) {
John McCall68ff0372010-09-08 01:44:27 +00002133 // void __cxa_guard_release(__guard *guard_object);
Chris Lattner2192fe52011-07-18 04:24:23 +00002134 llvm::FunctionType *FTy =
Chris Lattnerece04092012-02-07 00:39:47 +00002135 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
Reid Klecknerde864822017-03-21 16:57:30 +00002136 return CGM.CreateRuntimeFunction(
2137 FTy, "__cxa_guard_release",
2138 llvm::AttributeList::get(CGM.getLLVMContext(),
2139 llvm::AttributeList::FunctionIndex,
2140 llvm::Attribute::NoUnwind));
John McCall68ff0372010-09-08 01:44:27 +00002141}
2142
James Y Knight9871db02019-02-05 16:42:33 +00002143static llvm::FunctionCallee getGuardAbortFn(CodeGenModule &CGM,
2144 llvm::PointerType *GuardPtrTy) {
John McCall68ff0372010-09-08 01:44:27 +00002145 // void __cxa_guard_abort(__guard *guard_object);
Chris Lattner2192fe52011-07-18 04:24:23 +00002146 llvm::FunctionType *FTy =
Chris Lattnerece04092012-02-07 00:39:47 +00002147 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
Reid Klecknerde864822017-03-21 16:57:30 +00002148 return CGM.CreateRuntimeFunction(
2149 FTy, "__cxa_guard_abort",
2150 llvm::AttributeList::get(CGM.getLLVMContext(),
2151 llvm::AttributeList::FunctionIndex,
2152 llvm::Attribute::NoUnwind));
John McCall68ff0372010-09-08 01:44:27 +00002153}
2154
2155namespace {
David Blaikie7e70d682015-08-18 22:40:54 +00002156 struct CallGuardAbort final : EHScopeStack::Cleanup {
John McCall68ff0372010-09-08 01:44:27 +00002157 llvm::GlobalVariable *Guard;
Chandler Carruth84537952012-03-30 19:44:53 +00002158 CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
John McCall68ff0372010-09-08 01:44:27 +00002159
Craig Topper4f12f102014-03-12 06:41:41 +00002160 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCall882987f2013-02-28 19:01:20 +00002161 CGF.EmitNounwindRuntimeCall(getGuardAbortFn(CGF.CGM, Guard->getType()),
2162 Guard);
John McCall68ff0372010-09-08 01:44:27 +00002163 }
2164 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00002165}
John McCall68ff0372010-09-08 01:44:27 +00002166
2167/// The ARM code here follows the Itanium code closely enough that we
2168/// just special-case it at particular places.
John McCallcdf7ef52010-11-06 09:44:32 +00002169void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
2170 const VarDecl &D,
John McCallb88a5662012-03-30 21:00:39 +00002171 llvm::GlobalVariable *var,
2172 bool shouldPerformInit) {
John McCall68ff0372010-09-08 01:44:27 +00002173 CGBuilderTy &Builder = CGF.Builder;
John McCallcdf7ef52010-11-06 09:44:32 +00002174
Richard Smith62f19e72016-06-25 00:15:56 +00002175 // Inline variables that weren't instantiated from variable templates have
2176 // partially-ordered initialization within their translation unit.
2177 bool NonTemplateInline =
2178 D.isInline() &&
2179 !isTemplateInstantiation(D.getTemplateSpecializationKind());
2180
2181 // We only need to use thread-safe statics for local non-TLS variables and
2182 // inline variables; other global initialization is always single-threaded
2183 // or (through lazy dynamic loading in multiple threads) unsequenced.
Richard Smithdbf74ba2013-04-14 23:01:42 +00002184 bool threadsafe = getContext().getLangOpts().ThreadsafeStatics &&
Richard Smith62f19e72016-06-25 00:15:56 +00002185 (D.isLocalVarDecl() || NonTemplateInline) &&
2186 !D.getTLSKind();
Anders Carlssonc5d3ba12011-04-27 04:37:08 +00002187
Anders Carlssonc5d3ba12011-04-27 04:37:08 +00002188 // If we have a global variable with internal linkage and thread-safe statics
2189 // are disabled, we can just let the guard variable be of type i8.
John McCallb88a5662012-03-30 21:00:39 +00002190 bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage();
2191
2192 llvm::IntegerType *guardTy;
John McCall7f416cc2015-09-08 08:05:57 +00002193 CharUnits guardAlignment;
John McCall5aa52592011-06-17 07:33:57 +00002194 if (useInt8GuardVariable) {
John McCallb88a5662012-03-30 21:00:39 +00002195 guardTy = CGF.Int8Ty;
John McCall7f416cc2015-09-08 08:05:57 +00002196 guardAlignment = CharUnits::One();
John McCall5aa52592011-06-17 07:33:57 +00002197 } else {
Tim Northover9bb857a2013-01-31 12:13:10 +00002198 // Guard variables are 64 bits in the generic ABI and size width on ARM
2199 // (i.e. 32-bit on AArch32, 64-bit on AArch64).
John McCall7f416cc2015-09-08 08:05:57 +00002200 if (UseARMGuardVarABI) {
2201 guardTy = CGF.SizeTy;
2202 guardAlignment = CGF.getSizeAlign();
2203 } else {
2204 guardTy = CGF.Int64Ty;
2205 guardAlignment = CharUnits::fromQuantity(
2206 CGM.getDataLayout().getABITypeAlignment(guardTy));
2207 }
Anders Carlssonc5d3ba12011-04-27 04:37:08 +00002208 }
John McCallb88a5662012-03-30 21:00:39 +00002209 llvm::PointerType *guardPtrTy = guardTy->getPointerTo();
John McCall68ff0372010-09-08 01:44:27 +00002210
John McCallb88a5662012-03-30 21:00:39 +00002211 // Create the guard variable if we don't already have it (as we
2212 // might if we're double-emitting this function body).
2213 llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D);
2214 if (!guard) {
2215 // Mangle the name for the guard.
2216 SmallString<256> guardName;
2217 {
2218 llvm::raw_svector_ostream out(guardName);
Reid Klecknerd8110b62013-09-10 20:14:30 +00002219 getMangleContext().mangleStaticGuardVariable(&D, out);
John McCallb88a5662012-03-30 21:00:39 +00002220 }
John McCall8e7cb6d2010-11-02 21:04:24 +00002221
John McCallb88a5662012-03-30 21:00:39 +00002222 // Create the guard variable with a zero-initializer.
2223 // Just absorb linkage and visibility from the guarded variable.
2224 guard = new llvm::GlobalVariable(CGM.getModule(), guardTy,
2225 false, var->getLinkage(),
2226 llvm::ConstantInt::get(guardTy, 0),
2227 guardName.str());
Rafael Espindola699f5d62018-02-07 22:15:33 +00002228 guard->setDSOLocal(var->isDSOLocal());
John McCallb88a5662012-03-30 21:00:39 +00002229 guard->setVisibility(var->getVisibility());
Richard Smithdbf74ba2013-04-14 23:01:42 +00002230 // If the variable is thread-local, so is its guard variable.
2231 guard->setThreadLocalMode(var->getThreadLocalMode());
Guillaume Chateletc79099e2019-10-03 13:00:29 +00002232 guard->setAlignment(guardAlignment.getAsAlign());
John McCallb88a5662012-03-30 21:00:39 +00002233
Yaron Keren5bfa1082015-09-03 20:33:29 +00002234 // The ABI says: "It is suggested that it be emitted in the same COMDAT
2235 // group as the associated data object." In practice, this doesn't work for
Dan Gohman839f2152017-01-17 21:46:38 +00002236 // non-ELF and non-Wasm object formats, so only do it for ELF and Wasm.
Rafael Espindola0d4fb982015-01-12 22:13:53 +00002237 llvm::Comdat *C = var->getComdat();
Yaron Keren5bfa1082015-09-03 20:33:29 +00002238 if (!D.isLocalVarDecl() && C &&
Dan Gohman839f2152017-01-17 21:46:38 +00002239 (CGM.getTarget().getTriple().isOSBinFormatELF() ||
2240 CGM.getTarget().getTriple().isOSBinFormatWasm())) {
Rafael Espindola2ae4b632014-09-19 19:43:18 +00002241 guard->setComdat(C);
Richard Smith62f19e72016-06-25 00:15:56 +00002242 // An inline variable's guard function is run from the per-TU
2243 // initialization function, not via a dedicated global ctor function, so
2244 // we can't put it in a comdat.
2245 if (!NonTemplateInline)
2246 CGF.CurFn->setComdat(C);
NAKAMURA Takumic7da6da2015-05-09 21:10:07 +00002247 } else if (CGM.supportsCOMDAT() && guard->isWeakForLinker()) {
2248 guard->setComdat(CGM.getModule().getOrInsertComdat(guard->getName()));
Rafael Espindola2ae4b632014-09-19 19:43:18 +00002249 }
2250
John McCallb88a5662012-03-30 21:00:39 +00002251 CGM.setStaticLocalDeclGuardAddress(&D, guard);
2252 }
John McCall87590e62012-03-30 07:09:50 +00002253
John McCall7f416cc2015-09-08 08:05:57 +00002254 Address guardAddr = Address(guard, guardAlignment);
2255
John McCall68ff0372010-09-08 01:44:27 +00002256 // Test whether the variable has completed initialization.
Justin Bogner0cbb6d82014-04-23 01:50:10 +00002257 //
John McCall68ff0372010-09-08 01:44:27 +00002258 // Itanium C++ ABI 3.3.2:
2259 // The following is pseudo-code showing how these functions can be used:
2260 // if (obj_guard.first_byte == 0) {
2261 // if ( __cxa_guard_acquire (&obj_guard) ) {
2262 // try {
2263 // ... initialize the object ...;
2264 // } catch (...) {
2265 // __cxa_guard_abort (&obj_guard);
2266 // throw;
2267 // }
2268 // ... queue object destructor with __cxa_atexit() ...;
2269 // __cxa_guard_release (&obj_guard);
2270 // }
2271 // }
Tim Northovera2ee4332014-03-29 15:09:45 +00002272
Justin Bogner0cbb6d82014-04-23 01:50:10 +00002273 // Load the first byte of the guard variable.
2274 llvm::LoadInst *LI =
John McCall7f416cc2015-09-08 08:05:57 +00002275 Builder.CreateLoad(Builder.CreateElementBitCast(guardAddr, CGM.Int8Ty));
John McCall68ff0372010-09-08 01:44:27 +00002276
Justin Bogner0cbb6d82014-04-23 01:50:10 +00002277 // Itanium ABI:
2278 // An implementation supporting thread-safety on multiprocessor
2279 // systems must also guarantee that references to the initialized
2280 // object do not occur before the load of the initialization flag.
2281 //
2282 // In LLVM, we do this by marking the load Acquire.
2283 if (threadsafe)
JF Bastien92f4ef12016-04-06 17:26:42 +00002284 LI->setAtomic(llvm::AtomicOrdering::Acquire);
Eli Friedman84d28122011-09-13 22:21:56 +00002285
Justin Bogner0cbb6d82014-04-23 01:50:10 +00002286 // For ARM, we should only check the first bit, rather than the entire byte:
2287 //
2288 // ARM C++ ABI 3.2.3.1:
2289 // To support the potential use of initialization guard variables
2290 // as semaphores that are the target of ARM SWP and LDREX/STREX
2291 // synchronizing instructions we define a static initialization
2292 // guard variable to be a 4-byte aligned, 4-byte word with the
2293 // following inline access protocol.
2294 // #define INITIALIZED 1
2295 // if ((obj_guard & INITIALIZED) != INITIALIZED) {
2296 // if (__cxa_guard_acquire(&obj_guard))
2297 // ...
2298 // }
2299 //
2300 // and similarly for ARM64:
2301 //
2302 // ARM64 C++ ABI 3.2.2:
2303 // This ABI instead only specifies the value bit 0 of the static guard
2304 // variable; all other bits are platform defined. Bit 0 shall be 0 when the
2305 // variable is not initialized and 1 when it is.
2306 llvm::Value *V =
2307 (UseARMGuardVarABI && !useInt8GuardVariable)
2308 ? Builder.CreateAnd(LI, llvm::ConstantInt::get(CGM.Int8Ty, 1))
2309 : LI;
Richard Smithae8d62c2017-07-26 22:01:09 +00002310 llvm::Value *NeedsInit = Builder.CreateIsNull(V, "guard.uninitialized");
John McCall68ff0372010-09-08 01:44:27 +00002311
2312 llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check");
2313 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
2314
2315 // Check if the first byte of the guard variable is zero.
Richard Smithae8d62c2017-07-26 22:01:09 +00002316 CGF.EmitCXXGuardedInitBranch(NeedsInit, InitCheckBlock, EndBlock,
2317 CodeGenFunction::GuardKind::VariableGuard, &D);
John McCall68ff0372010-09-08 01:44:27 +00002318
2319 CGF.EmitBlock(InitCheckBlock);
2320
2321 // Variables used when coping with thread-safe statics and exceptions.
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002322 if (threadsafe) {
John McCall68ff0372010-09-08 01:44:27 +00002323 // Call __cxa_guard_acquire.
2324 llvm::Value *V
John McCall882987f2013-02-28 19:01:20 +00002325 = CGF.EmitNounwindRuntimeCall(getGuardAcquireFn(CGM, guardPtrTy), guard);
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002326
John McCall68ff0372010-09-08 01:44:27 +00002327 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002328
John McCall68ff0372010-09-08 01:44:27 +00002329 Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
2330 InitBlock, EndBlock);
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002331
John McCall68ff0372010-09-08 01:44:27 +00002332 // Call __cxa_guard_abort along the exceptional edge.
John McCallb88a5662012-03-30 21:00:39 +00002333 CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard);
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002334
John McCall68ff0372010-09-08 01:44:27 +00002335 CGF.EmitBlock(InitBlock);
2336 }
2337
2338 // Emit the initializer and add a global destructor if appropriate.
John McCallb88a5662012-03-30 21:00:39 +00002339 CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit);
John McCall68ff0372010-09-08 01:44:27 +00002340
John McCall5aa52592011-06-17 07:33:57 +00002341 if (threadsafe) {
John McCall68ff0372010-09-08 01:44:27 +00002342 // Pop the guard-abort cleanup if we pushed one.
2343 CGF.PopCleanupBlock();
2344
2345 // Call __cxa_guard_release. This cannot throw.
John McCall7f416cc2015-09-08 08:05:57 +00002346 CGF.EmitNounwindRuntimeCall(getGuardReleaseFn(CGM, guardPtrTy),
2347 guardAddr.getPointer());
John McCall68ff0372010-09-08 01:44:27 +00002348 } else {
John McCall7f416cc2015-09-08 08:05:57 +00002349 Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guardAddr);
John McCall68ff0372010-09-08 01:44:27 +00002350 }
2351
2352 CGF.EmitBlock(EndBlock);
2353}
John McCallc84ed6a2012-05-01 06:13:13 +00002354
2355/// Register a global destructor using __cxa_atexit.
2356static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF,
James Y Knightf7321542019-02-07 01:14:17 +00002357 llvm::FunctionCallee dtor,
2358 llvm::Constant *addr, bool TLS) {
Erich Keane34ec6922019-06-13 18:20:19 +00002359 assert((TLS || CGF.getTypes().getCodeGenOpts().CXAAtExit) &&
2360 "__cxa_atexit is disabled");
Bill Wendling95cae882013-05-02 19:18:03 +00002361 const char *Name = "__cxa_atexit";
2362 if (TLS) {
2363 const llvm::Triple &T = CGF.getTarget().getTriple();
Manman Renf93fff22015-11-11 23:08:18 +00002364 Name = T.isOSDarwin() ? "_tlv_atexit" : "__cxa_thread_atexit";
Bill Wendling95cae882013-05-02 19:18:03 +00002365 }
Richard Smithdbf74ba2013-04-14 23:01:42 +00002366
John McCallc84ed6a2012-05-01 06:13:13 +00002367 // We're assuming that the destructor function is something we can
2368 // reasonably call with the default CC. Go ahead and cast it to the
2369 // right prototype.
2370 llvm::Type *dtorTy =
2371 llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo();
2372
Anastasia Stulova960ff082019-07-15 11:58:10 +00002373 // Preserve address space of addr.
2374 auto AddrAS = addr ? addr->getType()->getPointerAddressSpace() : 0;
2375 auto AddrInt8PtrTy =
2376 AddrAS ? CGF.Int8Ty->getPointerTo(AddrAS) : CGF.Int8PtrTy;
2377
2378 // Create a variable that binds the atexit to this shared object.
2379 llvm::Constant *handle =
2380 CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle");
2381 auto *GV = cast<llvm::GlobalValue>(handle->stripPointerCasts());
2382 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
2383
John McCallc84ed6a2012-05-01 06:13:13 +00002384 // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d);
Anastasia Stulova960ff082019-07-15 11:58:10 +00002385 llvm::Type *paramTys[] = {dtorTy, AddrInt8PtrTy, handle->getType()};
John McCallc84ed6a2012-05-01 06:13:13 +00002386 llvm::FunctionType *atexitTy =
2387 llvm::FunctionType::get(CGF.IntTy, paramTys, false);
2388
2389 // Fetch the actual function.
James Y Knight9871db02019-02-05 16:42:33 +00002390 llvm::FunctionCallee atexit = CGF.CGM.CreateRuntimeFunction(atexitTy, Name);
2391 if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit.getCallee()))
John McCallc84ed6a2012-05-01 06:13:13 +00002392 fn->setDoesNotThrow();
2393
Akira Hatanaka617e2612018-04-17 18:41:52 +00002394 if (!addr)
2395 // addr is null when we are trying to register a dtor annotated with
2396 // __attribute__((destructor)) in a constructor function. Using null here is
2397 // okay because this argument is just passed back to the destructor
2398 // function.
2399 addr = llvm::Constant::getNullValue(CGF.Int8PtrTy);
2400
James Y Knightf7321542019-02-07 01:14:17 +00002401 llvm::Value *args[] = {llvm::ConstantExpr::getBitCast(
2402 cast<llvm::Constant>(dtor.getCallee()), dtorTy),
Anastasia Stulova960ff082019-07-15 11:58:10 +00002403 llvm::ConstantExpr::getBitCast(addr, AddrInt8PtrTy),
James Y Knightf7321542019-02-07 01:14:17 +00002404 handle};
John McCall882987f2013-02-28 19:01:20 +00002405 CGF.EmitNounwindRuntimeCall(atexit, args);
John McCallc84ed6a2012-05-01 06:13:13 +00002406}
2407
Akira Hatanaka617e2612018-04-17 18:41:52 +00002408void CodeGenModule::registerGlobalDtorsWithAtExit() {
2409 for (const auto I : DtorsUsingAtExit) {
2410 int Priority = I.first;
2411 const llvm::TinyPtrVector<llvm::Function *> &Dtors = I.second;
2412
2413 // Create a function that registers destructors that have the same priority.
2414 //
2415 // Since constructor functions are run in non-descending order of their
2416 // priorities, destructors are registered in non-descending order of their
2417 // priorities, and since destructor functions are run in the reverse order
2418 // of their registration, destructor functions are run in non-ascending
2419 // order of their priorities.
2420 CodeGenFunction CGF(*this);
2421 std::string GlobalInitFnName =
2422 std::string("__GLOBAL_init_") + llvm::to_string(Priority);
2423 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
2424 llvm::Function *GlobalInitFn = CreateGlobalInitOrDestructFunction(
2425 FTy, GlobalInitFnName, getTypes().arrangeNullaryFunction(),
2426 SourceLocation());
2427 ASTContext &Ctx = getContext();
Jonas Devlieghere64a26302018-11-11 00:56:15 +00002428 QualType ReturnTy = Ctx.VoidTy;
2429 QualType FunctionTy = Ctx.getFunctionType(ReturnTy, llvm::None, {});
Akira Hatanaka617e2612018-04-17 18:41:52 +00002430 FunctionDecl *FD = FunctionDecl::Create(
2431 Ctx, Ctx.getTranslationUnitDecl(), SourceLocation(), SourceLocation(),
Jonas Devlieghere64a26302018-11-11 00:56:15 +00002432 &Ctx.Idents.get(GlobalInitFnName), FunctionTy, nullptr, SC_Static,
Akira Hatanaka617e2612018-04-17 18:41:52 +00002433 false, false);
Jonas Devlieghere64a26302018-11-11 00:56:15 +00002434 CGF.StartFunction(GlobalDecl(FD), ReturnTy, GlobalInitFn,
Akira Hatanaka617e2612018-04-17 18:41:52 +00002435 getTypes().arrangeNullaryFunction(), FunctionArgList(),
2436 SourceLocation(), SourceLocation());
2437
2438 for (auto *Dtor : Dtors) {
2439 // Register the destructor function calling __cxa_atexit if it is
2440 // available. Otherwise fall back on calling atexit.
2441 if (getCodeGenOpts().CXAAtExit)
2442 emitGlobalDtorWithCXAAtExit(CGF, Dtor, nullptr, false);
2443 else
2444 CGF.registerGlobalDtorWithAtExit(Dtor);
2445 }
2446
2447 CGF.FinishFunction();
2448 AddGlobalCtor(GlobalInitFn, Priority, nullptr);
2449 }
2450}
2451
John McCallc84ed6a2012-05-01 06:13:13 +00002452/// Register a global destructor as best as we know how.
James Y Knightf7321542019-02-07 01:14:17 +00002453void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
2454 llvm::FunctionCallee dtor,
John McCallc84ed6a2012-05-01 06:13:13 +00002455 llvm::Constant *addr) {
Erik Pilkington5a559e62018-08-21 17:24:06 +00002456 if (D.isNoDestroy(CGM.getContext()))
2457 return;
2458
Erich Keane34ec6922019-06-13 18:20:19 +00002459 // emitGlobalDtorWithCXAAtExit will emit a call to either __cxa_thread_atexit
2460 // or __cxa_atexit depending on whether this VarDecl is a thread-local storage
2461 // or not. CXAAtExit controls only __cxa_atexit, so use it if it is enabled.
2462 // We can always use __cxa_thread_atexit.
2463 if (CGM.getCodeGenOpts().CXAAtExit || D.getTLSKind())
Richard Smithdbf74ba2013-04-14 23:01:42 +00002464 return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr, D.getTLSKind());
2465
John McCallc84ed6a2012-05-01 06:13:13 +00002466 // In Apple kexts, we want to add a global destructor entry.
2467 // FIXME: shouldn't this be guarded by some variable?
Richard Smith9c6890a2012-11-01 22:30:59 +00002468 if (CGM.getLangOpts().AppleKext) {
John McCallc84ed6a2012-05-01 06:13:13 +00002469 // Generate a global destructor entry.
2470 return CGM.AddCXXDtorEntry(dtor, addr);
2471 }
2472
David Blaikieebe87e12013-08-27 23:57:18 +00002473 CGF.registerGlobalDtorWithAtExit(D, dtor, addr);
John McCallc84ed6a2012-05-01 06:13:13 +00002474}
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002475
David Majnemer9b21c332014-07-11 20:28:10 +00002476static bool isThreadWrapperReplaceable(const VarDecl *VD,
2477 CodeGen::CodeGenModule &CGM) {
2478 assert(!VD->isStaticLocal() && "static local VarDecls don't need wrappers!");
Manman Renf93fff22015-11-11 23:08:18 +00002479 // Darwin prefers to have references to thread local variables to go through
David Majnemer9b21c332014-07-11 20:28:10 +00002480 // the thread wrapper instead of directly referencing the backing variable.
2481 return VD->getTLSKind() == VarDecl::TLS_Dynamic &&
Manman Renf93fff22015-11-11 23:08:18 +00002482 CGM.getTarget().getTriple().isOSDarwin();
David Majnemer9b21c332014-07-11 20:28:10 +00002483}
2484
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002485/// Get the appropriate linkage for the wrapper function. This is essentially
David Majnemer4632e1e2014-06-27 16:56:27 +00002486/// the weak form of the variable's linkage; every translation unit which needs
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002487/// the wrapper emits a copy, and we want the linker to merge them.
David Majnemer35ab3282014-06-11 04:08:55 +00002488static llvm::GlobalValue::LinkageTypes
2489getThreadLocalWrapperLinkage(const VarDecl *VD, CodeGen::CodeGenModule &CGM) {
2490 llvm::GlobalValue::LinkageTypes VarLinkage =
Rui Ueyama49a3ad22019-07-16 04:46:31 +00002491 CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false);
David Majnemer35ab3282014-06-11 04:08:55 +00002492
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002493 // For internal linkage variables, we don't need an external or weak wrapper.
2494 if (llvm::GlobalValue::isLocalLinkage(VarLinkage))
2495 return VarLinkage;
David Majnemer35ab3282014-06-11 04:08:55 +00002496
David Majnemer9b21c332014-07-11 20:28:10 +00002497 // If the thread wrapper is replaceable, give it appropriate linkage.
Manman Ren68150262015-11-11 22:42:31 +00002498 if (isThreadWrapperReplaceable(VD, CGM))
2499 if (!llvm::GlobalVariable::isLinkOnceLinkage(VarLinkage) &&
2500 !llvm::GlobalVariable::isWeakODRLinkage(VarLinkage))
2501 return VarLinkage;
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002502 return llvm::GlobalValue::WeakODRLinkage;
2503}
2504
2505llvm::Function *
2506ItaniumCXXABI::getOrCreateThreadLocalWrapper(const VarDecl *VD,
Alexander Musmanf94c3182014-09-26 06:28:25 +00002507 llvm::Value *Val) {
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002508 // Mangle the name for the thread_local wrapper function.
2509 SmallString<256> WrapperName;
2510 {
2511 llvm::raw_svector_ostream Out(WrapperName);
2512 getMangleContext().mangleItaniumThreadLocalWrapper(VD, Out);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002513 }
2514
Akira Hatanaka26907f92016-01-15 03:34:06 +00002515 // FIXME: If VD is a definition, we should regenerate the function attributes
2516 // before returning.
Alexander Musmanf94c3182014-09-26 06:28:25 +00002517 if (llvm::Value *V = CGM.getModule().getNamedValue(WrapperName))
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002518 return cast<llvm::Function>(V);
2519
Akira Hatanaka26907f92016-01-15 03:34:06 +00002520 QualType RetQT = VD->getType();
2521 if (RetQT->isReferenceType())
2522 RetQT = RetQT.getNonReferenceType();
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002523
John McCallc56a8b32016-03-11 04:30:31 +00002524 const CGFunctionInfo &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(
2525 getContext().getPointerType(RetQT), FunctionArgList());
Akira Hatanaka26907f92016-01-15 03:34:06 +00002526
2527 llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FI);
David Majnemer35ab3282014-06-11 04:08:55 +00002528 llvm::Function *Wrapper =
2529 llvm::Function::Create(FnTy, getThreadLocalWrapperLinkage(VD, CGM),
2530 WrapperName.str(), &CGM.getModule());
Akira Hatanaka26907f92016-01-15 03:34:06 +00002531
Erich Keanede6480a32018-11-13 15:48:08 +00002532 CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, Wrapper);
Akira Hatanaka26907f92016-01-15 03:34:06 +00002533
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002534 // Always resolve references to the wrapper at link time.
Vlad Tsyrklevichc93390b2019-01-17 17:53:45 +00002535 if (!Wrapper->hasLocalLinkage())
2536 if (!isThreadWrapperReplaceable(VD, CGM) ||
2537 llvm::GlobalVariable::isLinkOnceLinkage(Wrapper->getLinkage()) ||
2538 llvm::GlobalVariable::isWeakODRLinkage(Wrapper->getLinkage()) ||
2539 VD->getVisibility() == HiddenVisibility)
2540 Wrapper->setVisibility(llvm::GlobalValue::HiddenVisibility);
Manman Renb0b3af72015-12-17 00:42:36 +00002541
2542 if (isThreadWrapperReplaceable(VD, CGM)) {
2543 Wrapper->setCallingConv(llvm::CallingConv::CXX_FAST_TLS);
2544 Wrapper->addFnAttr(llvm::Attribute::NoUnwind);
2545 }
Richard Smith00223822019-09-12 20:00:24 +00002546
2547 ThreadWrappers.push_back({VD, Wrapper});
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002548 return Wrapper;
2549}
2550
2551void ItaniumCXXABI::EmitThreadLocalInitFuncs(
Richard Smith5a99c492015-12-01 01:10:48 +00002552 CodeGenModule &CGM, ArrayRef<const VarDecl *> CXXThreadLocals,
2553 ArrayRef<llvm::Function *> CXXThreadLocalInits,
2554 ArrayRef<const VarDecl *> CXXThreadLocalInitVars) {
David Majnemerb3341ea2014-10-05 05:05:40 +00002555 llvm::Function *InitFunc = nullptr;
Richard Smithfbe23692017-01-13 00:43:31 +00002556
2557 // Separate initializers into those with ordered (or partially-ordered)
2558 // initialization and those with unordered initialization.
2559 llvm::SmallVector<llvm::Function *, 8> OrderedInits;
2560 llvm::SmallDenseMap<const VarDecl *, llvm::Function *> UnorderedInits;
2561 for (unsigned I = 0; I != CXXThreadLocalInits.size(); ++I) {
2562 if (isTemplateInstantiation(
2563 CXXThreadLocalInitVars[I]->getTemplateSpecializationKind()))
2564 UnorderedInits[CXXThreadLocalInitVars[I]->getCanonicalDecl()] =
2565 CXXThreadLocalInits[I];
2566 else
2567 OrderedInits.push_back(CXXThreadLocalInits[I]);
2568 }
2569
2570 if (!OrderedInits.empty()) {
David Majnemerb3341ea2014-10-05 05:05:40 +00002571 // Generate a guarded initialization function.
2572 llvm::FunctionType *FTy =
2573 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
Akira Hatanaka7791f1a42015-10-31 01:28:07 +00002574 const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction();
2575 InitFunc = CGM.CreateGlobalInitOrDestructFunction(FTy, "__tls_init", FI,
Alexey Samsonov1444bb92014-10-17 00:20:19 +00002576 SourceLocation(),
David Majnemerb3341ea2014-10-05 05:05:40 +00002577 /*TLS=*/true);
2578 llvm::GlobalVariable *Guard = new llvm::GlobalVariable(
2579 CGM.getModule(), CGM.Int8Ty, /*isConstant=*/false,
2580 llvm::GlobalVariable::InternalLinkage,
2581 llvm::ConstantInt::get(CGM.Int8Ty, 0), "__tls_guard");
2582 Guard->setThreadLocal(true);
John McCall7f416cc2015-09-08 08:05:57 +00002583
2584 CharUnits GuardAlign = CharUnits::One();
Guillaume Chateletc79099e2019-10-03 13:00:29 +00002585 Guard->setAlignment(GuardAlign.getAsAlign());
John McCall7f416cc2015-09-08 08:05:57 +00002586
Richard Smith3ad06362018-10-31 20:39:26 +00002587 CodeGenFunction(CGM).GenerateCXXGlobalInitFunc(
2588 InitFunc, OrderedInits, ConstantAddress(Guard, GuardAlign));
Manman Ren5e5d0462016-03-18 23:35:21 +00002589 // On Darwin platforms, use CXX_FAST_TLS calling convention.
2590 if (CGM.getTarget().getTriple().isOSDarwin()) {
2591 InitFunc->setCallingConv(llvm::CallingConv::CXX_FAST_TLS);
2592 InitFunc->addFnAttr(llvm::Attribute::NoUnwind);
2593 }
David Majnemerb3341ea2014-10-05 05:05:40 +00002594 }
Richard Smithfbe23692017-01-13 00:43:31 +00002595
Richard Smith00223822019-09-12 20:00:24 +00002596 // Create declarations for thread wrappers for all thread-local variables
2597 // with non-discardable definitions in this translation unit.
Richard Smith5a99c492015-12-01 01:10:48 +00002598 for (const VarDecl *VD : CXXThreadLocals) {
Richard Smith00223822019-09-12 20:00:24 +00002599 if (VD->hasDefinition() &&
2600 !isDiscardableGVALinkage(getContext().GetGVALinkageForVariable(VD))) {
2601 llvm::GlobalValue *GV = CGM.GetGlobalValue(CGM.getMangledName(VD));
2602 getOrCreateThreadLocalWrapper(VD, GV);
2603 }
2604 }
2605
2606 // Emit all referenced thread wrappers.
2607 for (auto VDAndWrapper : ThreadWrappers) {
2608 const VarDecl *VD = VDAndWrapper.first;
Richard Smith5a99c492015-12-01 01:10:48 +00002609 llvm::GlobalVariable *Var =
2610 cast<llvm::GlobalVariable>(CGM.GetGlobalValue(CGM.getMangledName(VD)));
Richard Smith00223822019-09-12 20:00:24 +00002611 llvm::Function *Wrapper = VDAndWrapper.second;
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002612
David Majnemer9b21c332014-07-11 20:28:10 +00002613 // Some targets require that all access to thread local variables go through
2614 // the thread wrapper. This means that we cannot attempt to create a thread
2615 // wrapper or a thread helper.
Richard Smith00223822019-09-12 20:00:24 +00002616 if (!VD->hasDefinition()) {
2617 if (isThreadWrapperReplaceable(VD, CGM)) {
2618 Wrapper->setLinkage(llvm::Function::ExternalLinkage);
2619 continue;
2620 }
2621
2622 // If this isn't a TU in which this variable is defined, the thread
2623 // wrapper is discardable.
2624 if (Wrapper->getLinkage() == llvm::Function::WeakODRLinkage)
2625 Wrapper->setLinkage(llvm::Function::LinkOnceODRLinkage);
Richard Smithfbe23692017-01-13 00:43:31 +00002626 }
David Majnemer9b21c332014-07-11 20:28:10 +00002627
Richard Smith00223822019-09-12 20:00:24 +00002628 CGM.SetLLVMFunctionAttributesForDefinition(nullptr, Wrapper);
2629
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002630 // Mangle the name for the thread_local initialization function.
2631 SmallString<256> InitFnName;
2632 {
2633 llvm::raw_svector_ostream Out(InitFnName);
2634 getMangleContext().mangleItaniumThreadLocalInit(VD, Out);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002635 }
2636
James Y Knightf7321542019-02-07 01:14:17 +00002637 llvm::FunctionType *InitFnTy = llvm::FunctionType::get(CGM.VoidTy, false);
2638
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002639 // If we have a definition for the variable, emit the initialization
2640 // function as an alias to the global Init function (if any). Otherwise,
2641 // produce a declaration of the initialization function.
Craig Topper8a13c412014-05-21 05:09:00 +00002642 llvm::GlobalValue *Init = nullptr;
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002643 bool InitIsInitFunc = false;
Richard Smith00223822019-09-12 20:00:24 +00002644 bool HasConstantInitialization = false;
Richard Smith8ac5c742019-10-01 01:23:23 +00002645 if (!usesThreadWrapperFunction(VD)) {
Richard Smith00223822019-09-12 20:00:24 +00002646 HasConstantInitialization = true;
2647 } else if (VD->hasDefinition()) {
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002648 InitIsInitFunc = true;
Richard Smithfbe23692017-01-13 00:43:31 +00002649 llvm::Function *InitFuncToUse = InitFunc;
2650 if (isTemplateInstantiation(VD->getTemplateSpecializationKind()))
2651 InitFuncToUse = UnorderedInits.lookup(VD->getCanonicalDecl());
2652 if (InitFuncToUse)
Rafael Espindola234405b2014-05-17 21:30:14 +00002653 Init = llvm::GlobalAlias::create(Var->getLinkage(), InitFnName.str(),
Richard Smithfbe23692017-01-13 00:43:31 +00002654 InitFuncToUse);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002655 } else {
2656 // Emit a weak global function referring to the initialization function.
2657 // This function will not exist if the TU defining the thread_local
2658 // variable in question does not need any dynamic initialization for
2659 // its thread_local variables.
James Y Knightf7321542019-02-07 01:14:17 +00002660 Init = llvm::Function::Create(InitFnTy,
Richard Smithfbe23692017-01-13 00:43:31 +00002661 llvm::GlobalVariable::ExternalWeakLinkage,
2662 InitFnName.str(), &CGM.getModule());
John McCallc56a8b32016-03-11 04:30:31 +00002663 const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction();
Erich Keanede6480a32018-11-13 15:48:08 +00002664 CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI,
2665 cast<llvm::Function>(Init));
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002666 }
2667
Rafael Espindolaabdb3222018-03-07 23:18:06 +00002668 if (Init) {
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002669 Init->setVisibility(Var->getVisibility());
Rafael Espindolaabdb3222018-03-07 23:18:06 +00002670 Init->setDSOLocal(Var->isDSOLocal());
2671 }
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002672
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002673 llvm::LLVMContext &Context = CGM.getModule().getContext();
2674 llvm::BasicBlock *Entry = llvm::BasicBlock::Create(Context, "", Wrapper);
John McCall7f416cc2015-09-08 08:05:57 +00002675 CGBuilderTy Builder(CGM, Entry);
Richard Smith00223822019-09-12 20:00:24 +00002676 if (HasConstantInitialization) {
2677 // No dynamic initialization to invoke.
2678 } else if (InitIsInitFunc) {
Manman Ren5e5d0462016-03-18 23:35:21 +00002679 if (Init) {
James Y Knightf7321542019-02-07 01:14:17 +00002680 llvm::CallInst *CallVal = Builder.CreateCall(InitFnTy, Init);
Akira Hatanaka1da9dbb2018-05-29 18:28:49 +00002681 if (isThreadWrapperReplaceable(VD, CGM)) {
Manman Ren5e5d0462016-03-18 23:35:21 +00002682 CallVal->setCallingConv(llvm::CallingConv::CXX_FAST_TLS);
Akira Hatanaka1da9dbb2018-05-29 18:28:49 +00002683 llvm::Function *Fn =
2684 cast<llvm::Function>(cast<llvm::GlobalAlias>(Init)->getAliasee());
2685 Fn->setCallingConv(llvm::CallingConv::CXX_FAST_TLS);
2686 }
Manman Ren5e5d0462016-03-18 23:35:21 +00002687 }
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002688 } else {
2689 // Don't know whether we have an init function. Call it if it exists.
2690 llvm::Value *Have = Builder.CreateIsNotNull(Init);
2691 llvm::BasicBlock *InitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
2692 llvm::BasicBlock *ExitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
2693 Builder.CreateCondBr(Have, InitBB, ExitBB);
2694
2695 Builder.SetInsertPoint(InitBB);
James Y Knightf7321542019-02-07 01:14:17 +00002696 Builder.CreateCall(InitFnTy, Init);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002697 Builder.CreateBr(ExitBB);
2698
2699 Builder.SetInsertPoint(ExitBB);
2700 }
2701
2702 // For a reference, the result of the wrapper function is a pointer to
2703 // the referenced object.
2704 llvm::Value *Val = Var;
2705 if (VD->getType()->isReferenceType()) {
John McCall7f416cc2015-09-08 08:05:57 +00002706 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2707 Val = Builder.CreateAlignedLoad(Val, Align);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002708 }
Alexander Musmanf94c3182014-09-26 06:28:25 +00002709 if (Val->getType() != Wrapper->getReturnType())
2710 Val = Builder.CreatePointerBitCastOrAddrSpaceCast(
2711 Val, Wrapper->getReturnType(), "");
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002712 Builder.CreateRet(Val);
2713 }
2714}
2715
Richard Smith0f383742014-03-26 22:48:22 +00002716LValue ItaniumCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
2717 const VarDecl *VD,
2718 QualType LValType) {
Richard Smith5a99c492015-12-01 01:10:48 +00002719 llvm::Value *Val = CGF.CGM.GetAddrOfGlobalVar(VD);
Alexander Musmanf94c3182014-09-26 06:28:25 +00002720 llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Val);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002721
Manman Renb0b3af72015-12-17 00:42:36 +00002722 llvm::CallInst *CallVal = CGF.Builder.CreateCall(Wrapper);
Saleem Abdulrasool4a7130a2016-08-01 21:31:24 +00002723 CallVal->setCallingConv(Wrapper->getCallingConv());
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002724
2725 LValue LV;
2726 if (VD->getType()->isReferenceType())
Manman Renb0b3af72015-12-17 00:42:36 +00002727 LV = CGF.MakeNaturalAlignAddrLValue(CallVal, LValType);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002728 else
Manman Renb0b3af72015-12-17 00:42:36 +00002729 LV = CGF.MakeAddrLValue(CallVal, LValType,
2730 CGF.getContext().getDeclAlign(VD));
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002731 // FIXME: need setObjCGCLValueClass?
2732 return LV;
2733}
Peter Collingbourne66f82e62013-06-28 20:45:28 +00002734
2735/// Return whether the given global decl needs a VTT parameter, which it does
2736/// if it's a base constructor or destructor with virtual bases.
2737bool ItaniumCXXABI::NeedsVTTParameter(GlobalDecl GD) {
2738 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002739
Peter Collingbourne66f82e62013-06-28 20:45:28 +00002740 // We don't have any virtual bases, just return early.
2741 if (!MD->getParent()->getNumVBases())
2742 return false;
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002743
Peter Collingbourne66f82e62013-06-28 20:45:28 +00002744 // Check if we have a base constructor.
2745 if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base)
2746 return true;
2747
2748 // Check if we have a base destructor.
2749 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
2750 return true;
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002751
Peter Collingbourne66f82e62013-06-28 20:45:28 +00002752 return false;
2753}
David Majnemere2cb8d12014-07-07 06:20:47 +00002754
2755namespace {
2756class ItaniumRTTIBuilder {
2757 CodeGenModule &CGM; // Per-module state.
2758 llvm::LLVMContext &VMContext;
2759 const ItaniumCXXABI &CXXABI; // Per-module state.
2760
2761 /// Fields - The fields of the RTTI descriptor currently being built.
2762 SmallVector<llvm::Constant *, 16> Fields;
2763
2764 /// GetAddrOfTypeName - Returns the mangled type name of the given type.
2765 llvm::GlobalVariable *
2766 GetAddrOfTypeName(QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage);
2767
2768 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
2769 /// descriptor of the given type.
2770 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
2771
2772 /// BuildVTablePointer - Build the vtable pointer for the given type.
2773 void BuildVTablePointer(const Type *Ty);
2774
2775 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
2776 /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
2777 void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
2778
2779 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
2780 /// classes with bases that do not satisfy the abi::__si_class_type_info
2781 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
2782 void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
2783
2784 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
2785 /// for pointer types.
2786 void BuildPointerTypeInfo(QualType PointeeTy);
2787
2788 /// BuildObjCObjectTypeInfo - Build the appropriate kind of
2789 /// type_info for an object type.
2790 void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty);
2791
2792 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
2793 /// struct, used for member pointer types.
2794 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
2795
2796public:
2797 ItaniumRTTIBuilder(const ItaniumCXXABI &ABI)
2798 : CGM(ABI.CGM), VMContext(CGM.getModule().getContext()), CXXABI(ABI) {}
2799
2800 // Pointer type info flags.
2801 enum {
2802 /// PTI_Const - Type has const qualifier.
2803 PTI_Const = 0x1,
2804
2805 /// PTI_Volatile - Type has volatile qualifier.
2806 PTI_Volatile = 0x2,
2807
2808 /// PTI_Restrict - Type has restrict qualifier.
2809 PTI_Restrict = 0x4,
2810
2811 /// PTI_Incomplete - Type is incomplete.
2812 PTI_Incomplete = 0x8,
2813
2814 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
2815 /// (in pointer to member).
Richard Smitha7d93782016-12-01 03:32:42 +00002816 PTI_ContainingClassIncomplete = 0x10,
2817
2818 /// PTI_TransactionSafe - Pointee is transaction_safe function (C++ TM TS).
2819 //PTI_TransactionSafe = 0x20,
2820
2821 /// PTI_Noexcept - Pointee is noexcept function (C++1z).
2822 PTI_Noexcept = 0x40,
David Majnemere2cb8d12014-07-07 06:20:47 +00002823 };
2824
2825 // VMI type info flags.
2826 enum {
2827 /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
2828 VMI_NonDiamondRepeat = 0x1,
2829
2830 /// VMI_DiamondShaped - Class is diamond shaped.
2831 VMI_DiamondShaped = 0x2
2832 };
2833
2834 // Base class type info flags.
2835 enum {
2836 /// BCTI_Virtual - Base class is virtual.
2837 BCTI_Virtual = 0x1,
2838
2839 /// BCTI_Public - Base class is public.
2840 BCTI_Public = 0x2
2841 };
2842
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00002843 /// BuildTypeInfo - Build the RTTI type info struct for the given type, or
2844 /// link to an existing RTTI descriptor if one already exists.
2845 llvm::Constant *BuildTypeInfo(QualType Ty);
2846
David Majnemere2cb8d12014-07-07 06:20:47 +00002847 /// BuildTypeInfo - Build the RTTI type info struct for the given type.
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00002848 llvm::Constant *BuildTypeInfo(
2849 QualType Ty,
2850 llvm::GlobalVariable::LinkageTypes Linkage,
2851 llvm::GlobalValue::VisibilityTypes Visibility,
2852 llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass);
David Majnemere2cb8d12014-07-07 06:20:47 +00002853};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00002854}
David Majnemere2cb8d12014-07-07 06:20:47 +00002855
2856llvm::GlobalVariable *ItaniumRTTIBuilder::GetAddrOfTypeName(
2857 QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage) {
Yaron Kerene46f7ed2015-07-29 14:21:47 +00002858 SmallString<256> Name;
2859 llvm::raw_svector_ostream Out(Name);
David Majnemere2cb8d12014-07-07 06:20:47 +00002860 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
David Majnemere2cb8d12014-07-07 06:20:47 +00002861
2862 // We know that the mangled name of the type starts at index 4 of the
2863 // mangled name of the typename, so we can just index into it in order to
2864 // get the mangled name of the type.
2865 llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext,
2866 Name.substr(4));
David Greenbe0c5b62018-09-12 14:09:06 +00002867 auto Align = CGM.getContext().getTypeAlignInChars(CGM.getContext().CharTy);
David Majnemere2cb8d12014-07-07 06:20:47 +00002868
David Greenbe0c5b62018-09-12 14:09:06 +00002869 llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable(
2870 Name, Init->getType(), Linkage, Align.getQuantity());
David Majnemere2cb8d12014-07-07 06:20:47 +00002871
2872 GV->setInitializer(Init);
2873
2874 return GV;
2875}
2876
2877llvm::Constant *
2878ItaniumRTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
2879 // Mangle the RTTI name.
Yaron Kerene46f7ed2015-07-29 14:21:47 +00002880 SmallString<256> Name;
2881 llvm::raw_svector_ostream Out(Name);
David Majnemere2cb8d12014-07-07 06:20:47 +00002882 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
David Majnemere2cb8d12014-07-07 06:20:47 +00002883
2884 // Look for an existing global.
2885 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
2886
2887 if (!GV) {
2888 // Create a new global variable.
Piotr Padlewskid3b1cbd2017-06-01 08:04:05 +00002889 // Note for the future: If we would ever like to do deferred emission of
2890 // RTTI, check if emitting vtables opportunistically need any adjustment.
2891
David Majnemere2cb8d12014-07-07 06:20:47 +00002892 GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00002893 /*isConstant=*/true,
David Majnemere2cb8d12014-07-07 06:20:47 +00002894 llvm::GlobalValue::ExternalLinkage, nullptr,
2895 Name);
Rafael Espindola3f727a82018-03-14 18:14:46 +00002896 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
2897 CGM.setGVProperties(GV, RD);
David Majnemere2cb8d12014-07-07 06:20:47 +00002898 }
2899
2900 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
2901}
2902
2903/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
2904/// info for that type is defined in the standard library.
2905static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
2906 // Itanium C++ ABI 2.9.2:
2907 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
2908 // the run-time support library. Specifically, the run-time support
2909 // library should contain type_info objects for the types X, X* and
2910 // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char,
2911 // unsigned char, signed char, short, unsigned short, int, unsigned int,
2912 // long, unsigned long, long long, unsigned long long, float, double,
2913 // long double, char16_t, char32_t, and the IEEE 754r decimal and
2914 // half-precision floating point types.
Richard Smith4a382012016-02-03 01:32:42 +00002915 //
2916 // GCC also emits RTTI for __int128.
2917 // FIXME: We do not emit RTTI information for decimal types here.
2918
2919 // Types added here must also be added to EmitFundamentalRTTIDescriptors.
David Majnemere2cb8d12014-07-07 06:20:47 +00002920 switch (Ty->getKind()) {
2921 case BuiltinType::Void:
2922 case BuiltinType::NullPtr:
2923 case BuiltinType::Bool:
2924 case BuiltinType::WChar_S:
2925 case BuiltinType::WChar_U:
2926 case BuiltinType::Char_U:
2927 case BuiltinType::Char_S:
2928 case BuiltinType::UChar:
2929 case BuiltinType::SChar:
2930 case BuiltinType::Short:
2931 case BuiltinType::UShort:
2932 case BuiltinType::Int:
2933 case BuiltinType::UInt:
2934 case BuiltinType::Long:
2935 case BuiltinType::ULong:
2936 case BuiltinType::LongLong:
2937 case BuiltinType::ULongLong:
2938 case BuiltinType::Half:
2939 case BuiltinType::Float:
2940 case BuiltinType::Double:
2941 case BuiltinType::LongDouble:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00002942 case BuiltinType::Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00002943 case BuiltinType::Float128:
Richard Smith3a8244d2018-05-01 05:02:45 +00002944 case BuiltinType::Char8:
David Majnemere2cb8d12014-07-07 06:20:47 +00002945 case BuiltinType::Char16:
2946 case BuiltinType::Char32:
2947 case BuiltinType::Int128:
2948 case BuiltinType::UInt128:
Richard Smith4a382012016-02-03 01:32:42 +00002949 return true;
2950
Alexey Bader954ba212016-04-08 13:40:33 +00002951#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
2952 case BuiltinType::Id:
Alexey Baderb62f1442016-04-13 08:33:41 +00002953#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +00002954#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
2955 case BuiltinType::Id:
2956#include "clang/Basic/OpenCLExtensionTypes.def"
David Majnemere2cb8d12014-07-07 06:20:47 +00002957 case BuiltinType::OCLSampler:
2958 case BuiltinType::OCLEvent:
Alexey Bader9c8453f2015-09-15 11:18:52 +00002959 case BuiltinType::OCLClkEvent:
2960 case BuiltinType::OCLQueue:
Alexey Bader9c8453f2015-09-15 11:18:52 +00002961 case BuiltinType::OCLReserveID:
Richard Sandifordeb485fb2019-08-09 08:52:54 +00002962#define SVE_TYPE(Name, Id, SingletonId) \
2963 case BuiltinType::Id:
2964#include "clang/Basic/AArch64SVEACLETypes.def"
Leonard Chanf921d852018-06-04 16:07:52 +00002965 case BuiltinType::ShortAccum:
2966 case BuiltinType::Accum:
2967 case BuiltinType::LongAccum:
2968 case BuiltinType::UShortAccum:
2969 case BuiltinType::UAccum:
2970 case BuiltinType::ULongAccum:
Leonard Chanab80f3c2018-06-14 14:53:51 +00002971 case BuiltinType::ShortFract:
2972 case BuiltinType::Fract:
2973 case BuiltinType::LongFract:
2974 case BuiltinType::UShortFract:
2975 case BuiltinType::UFract:
2976 case BuiltinType::ULongFract:
2977 case BuiltinType::SatShortAccum:
2978 case BuiltinType::SatAccum:
2979 case BuiltinType::SatLongAccum:
2980 case BuiltinType::SatUShortAccum:
2981 case BuiltinType::SatUAccum:
2982 case BuiltinType::SatULongAccum:
2983 case BuiltinType::SatShortFract:
2984 case BuiltinType::SatFract:
2985 case BuiltinType::SatLongFract:
2986 case BuiltinType::SatUShortFract:
2987 case BuiltinType::SatUFract:
2988 case BuiltinType::SatULongFract:
Richard Smith4a382012016-02-03 01:32:42 +00002989 return false;
David Majnemere2cb8d12014-07-07 06:20:47 +00002990
2991 case BuiltinType::Dependent:
2992#define BUILTIN_TYPE(Id, SingletonId)
2993#define PLACEHOLDER_TYPE(Id, SingletonId) \
2994 case BuiltinType::Id:
2995#include "clang/AST/BuiltinTypes.def"
2996 llvm_unreachable("asking for RRTI for a placeholder type!");
2997
2998 case BuiltinType::ObjCId:
2999 case BuiltinType::ObjCClass:
3000 case BuiltinType::ObjCSel:
3001 llvm_unreachable("FIXME: Objective-C types are unsupported!");
3002 }
3003
3004 llvm_unreachable("Invalid BuiltinType Kind!");
3005}
3006
3007static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
3008 QualType PointeeTy = PointerTy->getPointeeType();
3009 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
3010 if (!BuiltinTy)
3011 return false;
3012
3013 // Check the qualifiers.
3014 Qualifiers Quals = PointeeTy.getQualifiers();
3015 Quals.removeConst();
3016
3017 if (!Quals.empty())
3018 return false;
3019
3020 return TypeInfoIsInStandardLibrary(BuiltinTy);
3021}
3022
3023/// IsStandardLibraryRTTIDescriptor - Returns whether the type
3024/// information for the given type exists in the standard library.
3025static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
3026 // Type info for builtin types is defined in the standard library.
3027 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
3028 return TypeInfoIsInStandardLibrary(BuiltinTy);
3029
3030 // Type info for some pointer types to builtin types is defined in the
3031 // standard library.
3032 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
3033 return TypeInfoIsInStandardLibrary(PointerTy);
3034
3035 return false;
3036}
3037
3038/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
3039/// the given type exists somewhere else, and that we should not emit the type
3040/// information in this translation unit. Assumes that it is not a
3041/// standard-library type.
3042static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM,
3043 QualType Ty) {
3044 ASTContext &Context = CGM.getContext();
3045
3046 // If RTTI is disabled, assume it might be disabled in the
3047 // translation unit that defines any potential key function, too.
3048 if (!Context.getLangOpts().RTTI) return false;
3049
3050 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
3051 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
3052 if (!RD->hasDefinition())
3053 return false;
3054
3055 if (!RD->isDynamicClass())
3056 return false;
3057
3058 // FIXME: this may need to be reconsidered if the key function
3059 // changes.
David Majnemerbe9022c2015-08-06 20:56:55 +00003060 // N.B. We must always emit the RTTI data ourselves if there exists a key
3061 // function.
3062 bool IsDLLImport = RD->hasAttr<DLLImportAttr>();
Martin Storsjo3b528942018-02-02 06:22:35 +00003063
3064 // Don't import the RTTI but emit it locally.
Martin Storsjo228ccd62019-04-26 19:31:51 +00003065 if (CGM.getTriple().isWindowsGNUEnvironment())
Martin Storsjo3b528942018-02-02 06:22:35 +00003066 return false;
3067
David Majnemer1fb1a042014-11-07 07:26:38 +00003068 if (CGM.getVTables().isVTableExternal(RD))
Shoaib Meenai61118e72017-07-04 01:02:19 +00003069 return IsDLLImport && !CGM.getTriple().isWindowsItaniumEnvironment()
3070 ? false
3071 : true;
David Majnemer1fb1a042014-11-07 07:26:38 +00003072
David Majnemerbe9022c2015-08-06 20:56:55 +00003073 if (IsDLLImport)
David Majnemer1fb1a042014-11-07 07:26:38 +00003074 return true;
David Majnemere2cb8d12014-07-07 06:20:47 +00003075 }
3076
3077 return false;
3078}
3079
3080/// IsIncompleteClassType - Returns whether the given record type is incomplete.
3081static bool IsIncompleteClassType(const RecordType *RecordTy) {
3082 return !RecordTy->getDecl()->isCompleteDefinition();
3083}
3084
3085/// ContainsIncompleteClassType - Returns whether the given type contains an
3086/// incomplete class type. This is true if
3087///
3088/// * The given type is an incomplete class type.
3089/// * The given type is a pointer type whose pointee type contains an
3090/// incomplete class type.
3091/// * The given type is a member pointer type whose class is an incomplete
3092/// class type.
3093/// * The given type is a member pointer type whoise pointee type contains an
3094/// incomplete class type.
3095/// is an indirect or direct pointer to an incomplete class type.
3096static bool ContainsIncompleteClassType(QualType Ty) {
3097 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
3098 if (IsIncompleteClassType(RecordTy))
3099 return true;
3100 }
3101
3102 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
3103 return ContainsIncompleteClassType(PointerTy->getPointeeType());
3104
3105 if (const MemberPointerType *MemberPointerTy =
3106 dyn_cast<MemberPointerType>(Ty)) {
3107 // Check if the class type is incomplete.
3108 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
3109 if (IsIncompleteClassType(ClassType))
3110 return true;
3111
3112 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
3113 }
3114
3115 return false;
3116}
3117
3118// CanUseSingleInheritance - Return whether the given record decl has a "single,
3119// public, non-virtual base at offset zero (i.e. the derived class is dynamic
3120// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
3121static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
3122 // Check the number of bases.
3123 if (RD->getNumBases() != 1)
3124 return false;
3125
3126 // Get the base.
3127 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
3128
3129 // Check that the base is not virtual.
3130 if (Base->isVirtual())
3131 return false;
3132
3133 // Check that the base is public.
3134 if (Base->getAccessSpecifier() != AS_public)
3135 return false;
3136
3137 // Check that the class is dynamic iff the base is.
Simon Pilgrimf2805472019-10-02 20:45:16 +00003138 auto *BaseDecl =
3139 cast<CXXRecordDecl>(Base->getType()->castAs<RecordType>()->getDecl());
David Majnemere2cb8d12014-07-07 06:20:47 +00003140 if (!BaseDecl->isEmpty() &&
3141 BaseDecl->isDynamicClass() != RD->isDynamicClass())
3142 return false;
3143
3144 return true;
3145}
3146
3147void ItaniumRTTIBuilder::BuildVTablePointer(const Type *Ty) {
3148 // abi::__class_type_info.
3149 static const char * const ClassTypeInfo =
3150 "_ZTVN10__cxxabiv117__class_type_infoE";
3151 // abi::__si_class_type_info.
3152 static const char * const SIClassTypeInfo =
3153 "_ZTVN10__cxxabiv120__si_class_type_infoE";
3154 // abi::__vmi_class_type_info.
3155 static const char * const VMIClassTypeInfo =
3156 "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
3157
3158 const char *VTableName = nullptr;
3159
3160 switch (Ty->getTypeClass()) {
3161#define TYPE(Class, Base)
3162#define ABSTRACT_TYPE(Class, Base)
3163#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
3164#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3165#define DEPENDENT_TYPE(Class, Base) case Type::Class:
John McCall36b12a82019-10-02 06:35:23 +00003166#include "clang/AST/TypeNodes.inc"
David Majnemere2cb8d12014-07-07 06:20:47 +00003167 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
3168
3169 case Type::LValueReference:
3170 case Type::RValueReference:
3171 llvm_unreachable("References shouldn't get here");
3172
3173 case Type::Auto:
Richard Smith600b5262017-01-26 20:40:47 +00003174 case Type::DeducedTemplateSpecialization:
3175 llvm_unreachable("Undeduced type shouldn't get here");
David Majnemere2cb8d12014-07-07 06:20:47 +00003176
Xiuli Pan9c14e282016-01-09 12:53:17 +00003177 case Type::Pipe:
3178 llvm_unreachable("Pipe types shouldn't get here");
3179
David Majnemere2cb8d12014-07-07 06:20:47 +00003180 case Type::Builtin:
3181 // GCC treats vector and complex types as fundamental types.
3182 case Type::Vector:
3183 case Type::ExtVector:
3184 case Type::Complex:
3185 case Type::Atomic:
3186 // FIXME: GCC treats block pointers as fundamental types?!
3187 case Type::BlockPointer:
3188 // abi::__fundamental_type_info.
3189 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
3190 break;
3191
3192 case Type::ConstantArray:
3193 case Type::IncompleteArray:
3194 case Type::VariableArray:
3195 // abi::__array_type_info.
3196 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
3197 break;
3198
3199 case Type::FunctionNoProto:
3200 case Type::FunctionProto:
Richard Smithb17d6fa2016-12-01 03:04:07 +00003201 // abi::__function_type_info.
3202 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
David Majnemere2cb8d12014-07-07 06:20:47 +00003203 break;
3204
3205 case Type::Enum:
3206 // abi::__enum_type_info.
3207 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
3208 break;
3209
3210 case Type::Record: {
Rafael Espindolaf6688122018-03-22 21:14:16 +00003211 const CXXRecordDecl *RD =
3212 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
David Majnemere2cb8d12014-07-07 06:20:47 +00003213
3214 if (!RD->hasDefinition() || !RD->getNumBases()) {
3215 VTableName = ClassTypeInfo;
3216 } else if (CanUseSingleInheritance(RD)) {
3217 VTableName = SIClassTypeInfo;
3218 } else {
3219 VTableName = VMIClassTypeInfo;
3220 }
3221
3222 break;
3223 }
3224
3225 case Type::ObjCObject:
3226 // Ignore protocol qualifiers.
3227 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
3228
3229 // Handle id and Class.
3230 if (isa<BuiltinType>(Ty)) {
3231 VTableName = ClassTypeInfo;
3232 break;
3233 }
3234
3235 assert(isa<ObjCInterfaceType>(Ty));
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +00003236 LLVM_FALLTHROUGH;
David Majnemere2cb8d12014-07-07 06:20:47 +00003237
3238 case Type::ObjCInterface:
3239 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
3240 VTableName = SIClassTypeInfo;
3241 } else {
3242 VTableName = ClassTypeInfo;
3243 }
3244 break;
3245
3246 case Type::ObjCObjectPointer:
3247 case Type::Pointer:
3248 // abi::__pointer_type_info.
3249 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
3250 break;
3251
3252 case Type::MemberPointer:
3253 // abi::__pointer_to_member_type_info.
3254 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
3255 break;
3256 }
3257
3258 llvm::Constant *VTable =
3259 CGM.getModule().getOrInsertGlobal(VTableName, CGM.Int8PtrTy);
Rafael Espindolafe9a55a2018-03-23 01:36:23 +00003260 CGM.setDSOLocal(cast<llvm::GlobalValue>(VTable->stripPointerCasts()));
David Majnemere2cb8d12014-07-07 06:20:47 +00003261
3262 llvm::Type *PtrDiffTy =
3263 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
3264
3265 // The vtable address point is 2.
3266 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
David Blaikiee3b172a2015-04-02 18:55:21 +00003267 VTable =
3268 llvm::ConstantExpr::getInBoundsGetElementPtr(CGM.Int8PtrTy, VTable, Two);
David Majnemere2cb8d12014-07-07 06:20:47 +00003269 VTable = llvm::ConstantExpr::getBitCast(VTable, CGM.Int8PtrTy);
3270
3271 Fields.push_back(VTable);
3272}
3273
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003274/// Return the linkage that the type info and type info name constants
David Majnemere2cb8d12014-07-07 06:20:47 +00003275/// should have for the given type.
Richard Smithbbb26552018-05-21 20:10:54 +00003276static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(CodeGenModule &CGM,
3277 QualType Ty) {
David Majnemere2cb8d12014-07-07 06:20:47 +00003278 // Itanium C++ ABI 2.9.5p7:
3279 // In addition, it and all of the intermediate abi::__pointer_type_info
3280 // structs in the chain down to the abi::__class_type_info for the
3281 // incomplete class type must be prevented from resolving to the
3282 // corresponding type_info structs for the complete class type, possibly
3283 // by making them local static objects. Finally, a dummy class RTTI is
3284 // generated for the incomplete type that will not resolve to the final
3285 // complete class RTTI (because the latter need not exist), possibly by
3286 // making it a local static object.
3287 if (ContainsIncompleteClassType(Ty))
Richard Smithbbb26552018-05-21 20:10:54 +00003288 return llvm::GlobalValue::InternalLinkage;
3289
3290 switch (Ty->getLinkage()) {
3291 case NoLinkage:
3292 case InternalLinkage:
3293 case UniqueExternalLinkage:
3294 return llvm::GlobalValue::InternalLinkage;
3295
3296 case VisibleNoLinkage:
3297 case ModuleInternalLinkage:
3298 case ModuleLinkage:
3299 case ExternalLinkage:
3300 // RTTI is not enabled, which means that this type info struct is going
3301 // to be used for exception handling. Give it linkonce_odr linkage.
3302 if (!CGM.getLangOpts().RTTI)
3303 return llvm::GlobalValue::LinkOnceODRLinkage;
3304
3305 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
3306 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
3307 if (RD->hasAttr<WeakAttr>())
3308 return llvm::GlobalValue::WeakODRLinkage;
3309 if (CGM.getTriple().isWindowsItaniumEnvironment())
3310 if (RD->hasAttr<DLLImportAttr>() &&
3311 ShouldUseExternalRTTIDescriptor(CGM, Ty))
3312 return llvm::GlobalValue::ExternalLinkage;
3313 // MinGW always uses LinkOnceODRLinkage for type info.
3314 if (RD->isDynamicClass() &&
3315 !CGM.getContext()
3316 .getTargetInfo()
3317 .getTriple()
3318 .isWindowsGNUEnvironment())
3319 return CGM.getVTableLinkage(RD);
3320 }
3321
3322 return llvm::GlobalValue::LinkOnceODRLinkage;
3323 }
3324
3325 llvm_unreachable("Invalid linkage!");
David Majnemere2cb8d12014-07-07 06:20:47 +00003326}
3327
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003328llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty) {
David Majnemere2cb8d12014-07-07 06:20:47 +00003329 // We want to operate on the canonical type.
Yaron Kerenebd14262016-03-16 12:14:43 +00003330 Ty = Ty.getCanonicalType();
David Majnemere2cb8d12014-07-07 06:20:47 +00003331
3332 // Check if we've already emitted an RTTI descriptor for this type.
Yaron Kerene46f7ed2015-07-29 14:21:47 +00003333 SmallString<256> Name;
3334 llvm::raw_svector_ostream Out(Name);
David Majnemere2cb8d12014-07-07 06:20:47 +00003335 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
David Majnemere2cb8d12014-07-07 06:20:47 +00003336
3337 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
3338 if (OldGV && !OldGV->isDeclaration()) {
3339 assert(!OldGV->hasAvailableExternallyLinkage() &&
3340 "available_externally typeinfos not yet implemented");
3341
3342 return llvm::ConstantExpr::getBitCast(OldGV, CGM.Int8PtrTy);
3343 }
3344
3345 // Check if there is already an external RTTI descriptor for this type.
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003346 if (IsStandardLibraryRTTIDescriptor(Ty) ||
3347 ShouldUseExternalRTTIDescriptor(CGM, Ty))
David Majnemere2cb8d12014-07-07 06:20:47 +00003348 return GetAddrOfExternalRTTIDescriptor(Ty);
3349
3350 // Emit the standard library with external linkage.
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003351 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(CGM, Ty);
Richard Smithbbb26552018-05-21 20:10:54 +00003352
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003353 // Give the type_info object and name the formal visibility of the
3354 // type itself.
3355 llvm::GlobalValue::VisibilityTypes llvmVisibility;
3356 if (llvm::GlobalValue::isLocalLinkage(Linkage))
3357 // If the linkage is local, only default visibility makes sense.
3358 llvmVisibility = llvm::GlobalValue::DefaultVisibility;
3359 else if (CXXABI.classifyRTTIUniqueness(Ty, Linkage) ==
3360 ItaniumCXXABI::RUK_NonUniqueHidden)
3361 llvmVisibility = llvm::GlobalValue::HiddenVisibility;
3362 else
3363 llvmVisibility = CodeGenModule::GetLLVMVisibility(Ty->getVisibility());
3364
3365 llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass =
3366 llvm::GlobalValue::DefaultStorageClass;
3367 if (CGM.getTriple().isWindowsItaniumEnvironment()) {
3368 auto RD = Ty->getAsCXXRecordDecl();
3369 if (RD && RD->hasAttr<DLLExportAttr>())
3370 DLLStorageClass = llvm::GlobalValue::DLLExportStorageClass;
3371 }
3372
3373 return BuildTypeInfo(Ty, Linkage, llvmVisibility, DLLStorageClass);
3374}
3375
3376llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(
3377 QualType Ty,
3378 llvm::GlobalVariable::LinkageTypes Linkage,
3379 llvm::GlobalValue::VisibilityTypes Visibility,
3380 llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass) {
David Majnemere2cb8d12014-07-07 06:20:47 +00003381 // Add the vtable pointer.
3382 BuildVTablePointer(cast<Type>(Ty));
3383
3384 // And the name.
Richard Smithbbb26552018-05-21 20:10:54 +00003385 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
David Majnemere2cb8d12014-07-07 06:20:47 +00003386 llvm::Constant *TypeNameField;
3387
3388 // If we're supposed to demote the visibility, be sure to set a flag
3389 // to use a string comparison for type_info comparisons.
3390 ItaniumCXXABI::RTTIUniquenessKind RTTIUniqueness =
Richard Smithbbb26552018-05-21 20:10:54 +00003391 CXXABI.classifyRTTIUniqueness(Ty, Linkage);
David Majnemere2cb8d12014-07-07 06:20:47 +00003392 if (RTTIUniqueness != ItaniumCXXABI::RUK_Unique) {
3393 // The flag is the sign bit, which on ARM64 is defined to be clear
3394 // for global pointers. This is very ARM64-specific.
3395 TypeNameField = llvm::ConstantExpr::getPtrToInt(TypeName, CGM.Int64Ty);
3396 llvm::Constant *flag =
3397 llvm::ConstantInt::get(CGM.Int64Ty, ((uint64_t)1) << 63);
3398 TypeNameField = llvm::ConstantExpr::getAdd(TypeNameField, flag);
3399 TypeNameField =
3400 llvm::ConstantExpr::getIntToPtr(TypeNameField, CGM.Int8PtrTy);
3401 } else {
3402 TypeNameField = llvm::ConstantExpr::getBitCast(TypeName, CGM.Int8PtrTy);
3403 }
3404 Fields.push_back(TypeNameField);
3405
3406 switch (Ty->getTypeClass()) {
3407#define TYPE(Class, Base)
3408#define ABSTRACT_TYPE(Class, Base)
3409#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
3410#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3411#define DEPENDENT_TYPE(Class, Base) case Type::Class:
John McCall36b12a82019-10-02 06:35:23 +00003412#include "clang/AST/TypeNodes.inc"
David Majnemere2cb8d12014-07-07 06:20:47 +00003413 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
3414
3415 // GCC treats vector types as fundamental types.
3416 case Type::Builtin:
3417 case Type::Vector:
3418 case Type::ExtVector:
3419 case Type::Complex:
3420 case Type::BlockPointer:
3421 // Itanium C++ ABI 2.9.5p4:
3422 // abi::__fundamental_type_info adds no data members to std::type_info.
3423 break;
3424
3425 case Type::LValueReference:
3426 case Type::RValueReference:
3427 llvm_unreachable("References shouldn't get here");
3428
3429 case Type::Auto:
Richard Smith600b5262017-01-26 20:40:47 +00003430 case Type::DeducedTemplateSpecialization:
3431 llvm_unreachable("Undeduced type shouldn't get here");
David Majnemere2cb8d12014-07-07 06:20:47 +00003432
Xiuli Pan9c14e282016-01-09 12:53:17 +00003433 case Type::Pipe:
3434 llvm_unreachable("Pipe type shouldn't get here");
3435
David Majnemere2cb8d12014-07-07 06:20:47 +00003436 case Type::ConstantArray:
3437 case Type::IncompleteArray:
3438 case Type::VariableArray:
3439 // Itanium C++ ABI 2.9.5p5:
3440 // abi::__array_type_info adds no data members to std::type_info.
3441 break;
3442
3443 case Type::FunctionNoProto:
Richard Smithb17d6fa2016-12-01 03:04:07 +00003444 case Type::FunctionProto:
David Majnemere2cb8d12014-07-07 06:20:47 +00003445 // Itanium C++ ABI 2.9.5p5:
3446 // abi::__function_type_info adds no data members to std::type_info.
3447 break;
3448
3449 case Type::Enum:
3450 // Itanium C++ ABI 2.9.5p5:
3451 // abi::__enum_type_info adds no data members to std::type_info.
3452 break;
3453
3454 case Type::Record: {
3455 const CXXRecordDecl *RD =
3456 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
3457 if (!RD->hasDefinition() || !RD->getNumBases()) {
3458 // We don't need to emit any fields.
3459 break;
3460 }
3461
3462 if (CanUseSingleInheritance(RD))
3463 BuildSIClassTypeInfo(RD);
3464 else
3465 BuildVMIClassTypeInfo(RD);
3466
3467 break;
3468 }
3469
3470 case Type::ObjCObject:
3471 case Type::ObjCInterface:
3472 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
3473 break;
3474
3475 case Type::ObjCObjectPointer:
3476 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
3477 break;
3478
3479 case Type::Pointer:
3480 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
3481 break;
3482
3483 case Type::MemberPointer:
3484 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
3485 break;
3486
3487 case Type::Atomic:
3488 // No fields, at least for the moment.
3489 break;
3490 }
3491
3492 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields);
3493
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003494 SmallString<256> Name;
3495 llvm::raw_svector_ostream Out(Name);
3496 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
Rafael Espindolacb92c192015-01-15 23:18:01 +00003497 llvm::Module &M = CGM.getModule();
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003498 llvm::GlobalVariable *OldGV = M.getNamedGlobal(Name);
David Majnemere2cb8d12014-07-07 06:20:47 +00003499 llvm::GlobalVariable *GV =
Rafael Espindolacb92c192015-01-15 23:18:01 +00003500 new llvm::GlobalVariable(M, Init->getType(),
Rui Ueyama49a3ad22019-07-16 04:46:31 +00003501 /*isConstant=*/true, Linkage, Init, Name);
Rafael Espindolacb92c192015-01-15 23:18:01 +00003502
David Majnemere2cb8d12014-07-07 06:20:47 +00003503 // If there's already an old global variable, replace it with the new one.
3504 if (OldGV) {
3505 GV->takeName(OldGV);
3506 llvm::Constant *NewPtr =
3507 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
3508 OldGV->replaceAllUsesWith(NewPtr);
3509 OldGV->eraseFromParent();
3510 }
3511
Yaron Keren04da2382015-07-29 15:42:28 +00003512 if (CGM.supportsCOMDAT() && GV->isWeakForLinker())
3513 GV->setComdat(M.getOrInsertComdat(GV->getName()));
3514
David Greenbe0c5b62018-09-12 14:09:06 +00003515 CharUnits Align =
3516 CGM.getContext().toCharUnitsFromBits(CGM.getTarget().getPointerAlign(0));
Guillaume Chateletc79099e2019-10-03 13:00:29 +00003517 GV->setAlignment(Align.getAsAlign());
David Greenbe0c5b62018-09-12 14:09:06 +00003518
David Majnemere2cb8d12014-07-07 06:20:47 +00003519 // The Itanium ABI specifies that type_info objects must be globally
3520 // unique, with one exception: if the type is an incomplete class
3521 // type or a (possibly indirect) pointer to one. That exception
3522 // affects the general case of comparing type_info objects produced
3523 // by the typeid operator, which is why the comparison operators on
3524 // std::type_info generally use the type_info name pointers instead
3525 // of the object addresses. However, the language's built-in uses
3526 // of RTTI generally require class types to be complete, even when
3527 // manipulating pointers to those class types. This allows the
3528 // implementation of dynamic_cast to rely on address equality tests,
3529 // which is much faster.
3530
3531 // All of this is to say that it's important that both the type_info
3532 // object and the type_info name be uniqued when weakly emitted.
3533
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003534 TypeName->setVisibility(Visibility);
Rafael Espindola3dd49812018-02-23 00:22:15 +00003535 CGM.setDSOLocal(TypeName);
Rafael Espindola699f5d62018-02-07 22:15:33 +00003536
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003537 GV->setVisibility(Visibility);
Rafael Espindola3dd49812018-02-23 00:22:15 +00003538 CGM.setDSOLocal(GV);
Saleem Abdulrasool18820022016-12-02 22:46:18 +00003539
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003540 TypeName->setDLLStorageClass(DLLStorageClass);
3541 GV->setDLLStorageClass(DLLStorageClass);
David Majnemere2cb8d12014-07-07 06:20:47 +00003542
Peter Collingbournee08e68d2019-06-07 19:10:08 +00003543 TypeName->setPartition(CGM.getCodeGenOpts().SymbolPartition);
3544 GV->setPartition(CGM.getCodeGenOpts().SymbolPartition);
3545
David Majnemere2cb8d12014-07-07 06:20:47 +00003546 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
3547}
3548
David Majnemere2cb8d12014-07-07 06:20:47 +00003549/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
3550/// for the given Objective-C object type.
3551void ItaniumRTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
3552 // Drop qualifiers.
3553 const Type *T = OT->getBaseType().getTypePtr();
3554 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
3555
3556 // The builtin types are abi::__class_type_infos and don't require
3557 // extra fields.
3558 if (isa<BuiltinType>(T)) return;
3559
3560 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
3561 ObjCInterfaceDecl *Super = Class->getSuperClass();
3562
3563 // Root classes are also __class_type_info.
3564 if (!Super) return;
3565
3566 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
3567
3568 // Everything else is single inheritance.
3569 llvm::Constant *BaseTypeInfo =
3570 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(SuperTy);
3571 Fields.push_back(BaseTypeInfo);
3572}
3573
3574/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
3575/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
3576void ItaniumRTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
3577 // Itanium C++ ABI 2.9.5p6b:
3578 // It adds to abi::__class_type_info a single member pointing to the
3579 // type_info structure for the base type,
3580 llvm::Constant *BaseTypeInfo =
3581 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(RD->bases_begin()->getType());
3582 Fields.push_back(BaseTypeInfo);
3583}
3584
3585namespace {
3586 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
3587 /// a class hierarchy.
3588 struct SeenBases {
3589 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
3590 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
3591 };
3592}
3593
3594/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
3595/// abi::__vmi_class_type_info.
3596///
3597static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
3598 SeenBases &Bases) {
3599
3600 unsigned Flags = 0;
3601
Simon Pilgrimf2805472019-10-02 20:45:16 +00003602 auto *BaseDecl =
3603 cast<CXXRecordDecl>(Base->getType()->castAs<RecordType>()->getDecl());
David Majnemere2cb8d12014-07-07 06:20:47 +00003604
3605 if (Base->isVirtual()) {
3606 // Mark the virtual base as seen.
David Blaikie82e95a32014-11-19 07:49:47 +00003607 if (!Bases.VirtualBases.insert(BaseDecl).second) {
David Majnemere2cb8d12014-07-07 06:20:47 +00003608 // If this virtual base has been seen before, then the class is diamond
3609 // shaped.
3610 Flags |= ItaniumRTTIBuilder::VMI_DiamondShaped;
3611 } else {
3612 if (Bases.NonVirtualBases.count(BaseDecl))
3613 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
3614 }
3615 } else {
3616 // Mark the non-virtual base as seen.
David Blaikie82e95a32014-11-19 07:49:47 +00003617 if (!Bases.NonVirtualBases.insert(BaseDecl).second) {
David Majnemere2cb8d12014-07-07 06:20:47 +00003618 // If this non-virtual base has been seen before, then the class has non-
3619 // diamond shaped repeated inheritance.
3620 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
3621 } else {
3622 if (Bases.VirtualBases.count(BaseDecl))
3623 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
3624 }
3625 }
3626
3627 // Walk all bases.
3628 for (const auto &I : BaseDecl->bases())
3629 Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases);
3630
3631 return Flags;
3632}
3633
3634static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
3635 unsigned Flags = 0;
3636 SeenBases Bases;
3637
3638 // Walk all bases.
3639 for (const auto &I : RD->bases())
3640 Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases);
3641
3642 return Flags;
3643}
3644
3645/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
3646/// classes with bases that do not satisfy the abi::__si_class_type_info
3647/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
3648void ItaniumRTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
3649 llvm::Type *UnsignedIntLTy =
3650 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
3651
3652 // Itanium C++ ABI 2.9.5p6c:
3653 // __flags is a word with flags describing details about the class
3654 // structure, which may be referenced by using the __flags_masks
3655 // enumeration. These flags refer to both direct and indirect bases.
3656 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
3657 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3658
3659 // Itanium C++ ABI 2.9.5p6c:
3660 // __base_count is a word with the number of direct proper base class
3661 // descriptions that follow.
3662 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
3663
3664 if (!RD->getNumBases())
3665 return;
3666
David Majnemere2cb8d12014-07-07 06:20:47 +00003667 // Now add the base class descriptions.
3668
3669 // Itanium C++ ABI 2.9.5p6c:
3670 // __base_info[] is an array of base class descriptions -- one for every
3671 // direct proper base. Each description is of the type:
3672 //
3673 // struct abi::__base_class_type_info {
3674 // public:
3675 // const __class_type_info *__base_type;
3676 // long __offset_flags;
3677 //
3678 // enum __offset_flags_masks {
3679 // __virtual_mask = 0x1,
3680 // __public_mask = 0x2,
3681 // __offset_shift = 8
3682 // };
3683 // };
Reid Klecknerd8b04662016-08-25 22:16:30 +00003684
3685 // If we're in mingw and 'long' isn't wide enough for a pointer, use 'long
3686 // long' instead of 'long' for __offset_flags. libstdc++abi uses long long on
3687 // LLP64 platforms.
3688 // FIXME: Consider updating libc++abi to match, and extend this logic to all
3689 // LLP64 platforms.
3690 QualType OffsetFlagsTy = CGM.getContext().LongTy;
3691 const TargetInfo &TI = CGM.getContext().getTargetInfo();
3692 if (TI.getTriple().isOSCygMing() && TI.getPointerWidth(0) > TI.getLongWidth())
3693 OffsetFlagsTy = CGM.getContext().LongLongTy;
3694 llvm::Type *OffsetFlagsLTy =
3695 CGM.getTypes().ConvertType(OffsetFlagsTy);
3696
David Majnemere2cb8d12014-07-07 06:20:47 +00003697 for (const auto &Base : RD->bases()) {
3698 // The __base_type member points to the RTTI for the base type.
3699 Fields.push_back(ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(Base.getType()));
3700
Simon Pilgrimf2805472019-10-02 20:45:16 +00003701 auto *BaseDecl =
3702 cast<CXXRecordDecl>(Base.getType()->castAs<RecordType>()->getDecl());
David Majnemere2cb8d12014-07-07 06:20:47 +00003703
3704 int64_t OffsetFlags = 0;
3705
3706 // All but the lower 8 bits of __offset_flags are a signed offset.
3707 // For a non-virtual base, this is the offset in the object of the base
3708 // subobject. For a virtual base, this is the offset in the virtual table of
3709 // the virtual base offset for the virtual base referenced (negative).
3710 CharUnits Offset;
3711 if (Base.isVirtual())
3712 Offset =
3713 CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(RD, BaseDecl);
3714 else {
3715 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
3716 Offset = Layout.getBaseClassOffset(BaseDecl);
3717 };
3718
3719 OffsetFlags = uint64_t(Offset.getQuantity()) << 8;
3720
3721 // The low-order byte of __offset_flags contains flags, as given by the
3722 // masks from the enumeration __offset_flags_masks.
3723 if (Base.isVirtual())
3724 OffsetFlags |= BCTI_Virtual;
3725 if (Base.getAccessSpecifier() == AS_public)
3726 OffsetFlags |= BCTI_Public;
3727
Reid Klecknerd8b04662016-08-25 22:16:30 +00003728 Fields.push_back(llvm::ConstantInt::get(OffsetFlagsLTy, OffsetFlags));
David Majnemere2cb8d12014-07-07 06:20:47 +00003729 }
3730}
3731
Richard Smitha7d93782016-12-01 03:32:42 +00003732/// Compute the flags for a __pbase_type_info, and remove the corresponding
3733/// pieces from \p Type.
3734static unsigned extractPBaseFlags(ASTContext &Ctx, QualType &Type) {
3735 unsigned Flags = 0;
David Majnemere2cb8d12014-07-07 06:20:47 +00003736
Richard Smitha7d93782016-12-01 03:32:42 +00003737 if (Type.isConstQualified())
3738 Flags |= ItaniumRTTIBuilder::PTI_Const;
3739 if (Type.isVolatileQualified())
3740 Flags |= ItaniumRTTIBuilder::PTI_Volatile;
3741 if (Type.isRestrictQualified())
3742 Flags |= ItaniumRTTIBuilder::PTI_Restrict;
3743 Type = Type.getUnqualifiedType();
David Majnemere2cb8d12014-07-07 06:20:47 +00003744
3745 // Itanium C++ ABI 2.9.5p7:
3746 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
3747 // incomplete class type, the incomplete target type flag is set.
Richard Smitha7d93782016-12-01 03:32:42 +00003748 if (ContainsIncompleteClassType(Type))
3749 Flags |= ItaniumRTTIBuilder::PTI_Incomplete;
3750
3751 if (auto *Proto = Type->getAs<FunctionProtoType>()) {
Richard Smitheaf11ad2018-05-03 03:58:32 +00003752 if (Proto->isNothrow()) {
Richard Smitha7d93782016-12-01 03:32:42 +00003753 Flags |= ItaniumRTTIBuilder::PTI_Noexcept;
Stephan Bergmann8c85bca2018-01-05 07:57:12 +00003754 Type = Ctx.getFunctionTypeWithExceptionSpec(Type, EST_None);
Richard Smitha7d93782016-12-01 03:32:42 +00003755 }
3756 }
3757
3758 return Flags;
3759}
3760
3761/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
3762/// used for pointer types.
3763void ItaniumRTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
3764 // Itanium C++ ABI 2.9.5p7:
3765 // __flags is a flag word describing the cv-qualification and other
3766 // attributes of the type pointed to
3767 unsigned Flags = extractPBaseFlags(CGM.getContext(), PointeeTy);
David Majnemere2cb8d12014-07-07 06:20:47 +00003768
3769 llvm::Type *UnsignedIntLTy =
3770 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
3771 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3772
3773 // Itanium C++ ABI 2.9.5p7:
3774 // __pointee is a pointer to the std::type_info derivation for the
3775 // unqualified type being pointed to.
3776 llvm::Constant *PointeeTypeInfo =
Richard Smitha7d93782016-12-01 03:32:42 +00003777 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(PointeeTy);
David Majnemere2cb8d12014-07-07 06:20:47 +00003778 Fields.push_back(PointeeTypeInfo);
3779}
3780
3781/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
3782/// struct, used for member pointer types.
3783void
3784ItaniumRTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
3785 QualType PointeeTy = Ty->getPointeeType();
3786
David Majnemere2cb8d12014-07-07 06:20:47 +00003787 // Itanium C++ ABI 2.9.5p7:
3788 // __flags is a flag word describing the cv-qualification and other
3789 // attributes of the type pointed to.
Richard Smitha7d93782016-12-01 03:32:42 +00003790 unsigned Flags = extractPBaseFlags(CGM.getContext(), PointeeTy);
David Majnemere2cb8d12014-07-07 06:20:47 +00003791
3792 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
David Majnemere2cb8d12014-07-07 06:20:47 +00003793 if (IsIncompleteClassType(ClassType))
3794 Flags |= PTI_ContainingClassIncomplete;
3795
3796 llvm::Type *UnsignedIntLTy =
3797 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
3798 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3799
3800 // Itanium C++ ABI 2.9.5p7:
3801 // __pointee is a pointer to the std::type_info derivation for the
3802 // unqualified type being pointed to.
3803 llvm::Constant *PointeeTypeInfo =
Richard Smitha7d93782016-12-01 03:32:42 +00003804 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(PointeeTy);
David Majnemere2cb8d12014-07-07 06:20:47 +00003805 Fields.push_back(PointeeTypeInfo);
3806
3807 // Itanium C++ ABI 2.9.5p9:
3808 // __context is a pointer to an abi::__class_type_info corresponding to the
3809 // class type containing the member pointed to
3810 // (e.g., the "A" in "int A::*").
3811 Fields.push_back(
3812 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(QualType(ClassType, 0)));
3813}
3814
David Majnemer443250f2015-03-17 20:35:00 +00003815llvm::Constant *ItaniumCXXABI::getAddrOfRTTIDescriptor(QualType Ty) {
David Majnemere2cb8d12014-07-07 06:20:47 +00003816 return ItaniumRTTIBuilder(*this).BuildTypeInfo(Ty);
3817}
3818
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003819void ItaniumCXXABI::EmitFundamentalRTTIDescriptors(const CXXRecordDecl *RD) {
Richard Smith4a382012016-02-03 01:32:42 +00003820 // Types added here must also be added to TypeInfoIsInStandardLibrary.
David Majnemere2cb8d12014-07-07 06:20:47 +00003821 QualType FundamentalTypes[] = {
3822 getContext().VoidTy, getContext().NullPtrTy,
3823 getContext().BoolTy, getContext().WCharTy,
3824 getContext().CharTy, getContext().UnsignedCharTy,
3825 getContext().SignedCharTy, getContext().ShortTy,
3826 getContext().UnsignedShortTy, getContext().IntTy,
3827 getContext().UnsignedIntTy, getContext().LongTy,
3828 getContext().UnsignedLongTy, getContext().LongLongTy,
Richard Smith4a382012016-02-03 01:32:42 +00003829 getContext().UnsignedLongLongTy, getContext().Int128Ty,
3830 getContext().UnsignedInt128Ty, getContext().HalfTy,
David Majnemere2cb8d12014-07-07 06:20:47 +00003831 getContext().FloatTy, getContext().DoubleTy,
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00003832 getContext().LongDoubleTy, getContext().Float128Ty,
Richard Smith3a8244d2018-05-01 05:02:45 +00003833 getContext().Char8Ty, getContext().Char16Ty,
3834 getContext().Char32Ty
David Majnemere2cb8d12014-07-07 06:20:47 +00003835 };
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003836 llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass =
3837 RD->hasAttr<DLLExportAttr>()
3838 ? llvm::GlobalValue::DLLExportStorageClass
3839 : llvm::GlobalValue::DefaultStorageClass;
3840 llvm::GlobalValue::VisibilityTypes Visibility =
3841 CodeGenModule::GetLLVMVisibility(RD->getVisibility());
3842 for (const QualType &FundamentalType : FundamentalTypes) {
3843 QualType PointerType = getContext().getPointerType(FundamentalType);
3844 QualType PointerTypeConst = getContext().getPointerType(
3845 FundamentalType.withConst());
3846 for (QualType Type : {FundamentalType, PointerType, PointerTypeConst})
3847 ItaniumRTTIBuilder(*this).BuildTypeInfo(
3848 Type, llvm::GlobalValue::ExternalLinkage,
3849 Visibility, DLLStorageClass);
3850 }
David Majnemere2cb8d12014-07-07 06:20:47 +00003851}
3852
3853/// What sort of uniqueness rules should we use for the RTTI for the
3854/// given type?
3855ItaniumCXXABI::RTTIUniquenessKind ItaniumCXXABI::classifyRTTIUniqueness(
3856 QualType CanTy, llvm::GlobalValue::LinkageTypes Linkage) const {
3857 if (shouldRTTIBeUnique())
3858 return RUK_Unique;
3859
3860 // It's only necessary for linkonce_odr or weak_odr linkage.
3861 if (Linkage != llvm::GlobalValue::LinkOnceODRLinkage &&
3862 Linkage != llvm::GlobalValue::WeakODRLinkage)
3863 return RUK_Unique;
3864
3865 // It's only necessary with default visibility.
3866 if (CanTy->getVisibility() != DefaultVisibility)
3867 return RUK_Unique;
3868
3869 // If we're not required to publish this symbol, hide it.
3870 if (Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
3871 return RUK_NonUniqueHidden;
3872
3873 // If we're required to publish this symbol, as we might be under an
3874 // explicit instantiation, leave it with default visibility but
3875 // enable string-comparisons.
3876 assert(Linkage == llvm::GlobalValue::WeakODRLinkage);
3877 return RUK_NonUniqueVisible;
3878}
Rafael Espindola91f68b42014-09-15 19:20:10 +00003879
Rafael Espindola1e4df922014-09-16 15:18:21 +00003880// Find out how to codegen the complete destructor and constructor
3881namespace {
3882enum class StructorCodegen { Emit, RAUW, Alias, COMDAT };
3883}
3884static StructorCodegen getCodegenToUse(CodeGenModule &CGM,
3885 const CXXMethodDecl *MD) {
3886 if (!CGM.getCodeGenOpts().CXXCtorDtorAliases)
3887 return StructorCodegen::Emit;
Rafael Espindola91f68b42014-09-15 19:20:10 +00003888
Rafael Espindola1e4df922014-09-16 15:18:21 +00003889 // The complete and base structors are not equivalent if there are any virtual
3890 // bases, so emit separate functions.
3891 if (MD->getParent()->getNumVBases())
3892 return StructorCodegen::Emit;
3893
3894 GlobalDecl AliasDecl;
3895 if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) {
3896 AliasDecl = GlobalDecl(DD, Dtor_Complete);
3897 } else {
3898 const auto *CD = cast<CXXConstructorDecl>(MD);
3899 AliasDecl = GlobalDecl(CD, Ctor_Complete);
3900 }
3901 llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl);
3902
Chandler Carruth1f82d9b2018-07-29 03:05:07 +00003903 if (llvm::GlobalValue::isDiscardableIfUnused(Linkage))
3904 return StructorCodegen::RAUW;
Rafael Espindola1e4df922014-09-16 15:18:21 +00003905
Pavel Labathc370f262018-05-14 11:35:44 +00003906 // FIXME: Should we allow available_externally aliases?
Chandler Carruth1f82d9b2018-07-29 03:05:07 +00003907 if (!llvm::GlobalAlias::isValidLinkage(Linkage))
3908 return StructorCodegen::RAUW;
Rafael Espindola1e4df922014-09-16 15:18:21 +00003909
Rafael Espindola0806f982014-09-16 20:19:43 +00003910 if (llvm::GlobalValue::isWeakForLinker(Linkage)) {
Dan Gohman839f2152017-01-17 21:46:38 +00003911 // Only ELF and wasm support COMDATs with arbitrary names (C5/D5).
3912 if (CGM.getTarget().getTriple().isOSBinFormatELF() ||
3913 CGM.getTarget().getTriple().isOSBinFormatWasm())
Rafael Espindola0806f982014-09-16 20:19:43 +00003914 return StructorCodegen::COMDAT;
3915 return StructorCodegen::Emit;
3916 }
Rafael Espindola1e4df922014-09-16 15:18:21 +00003917
3918 return StructorCodegen::Alias;
Rafael Espindola91f68b42014-09-15 19:20:10 +00003919}
3920
Rafael Espindola1e4df922014-09-16 15:18:21 +00003921static void emitConstructorDestructorAlias(CodeGenModule &CGM,
3922 GlobalDecl AliasDecl,
3923 GlobalDecl TargetDecl) {
3924 llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl);
3925
3926 StringRef MangledName = CGM.getMangledName(AliasDecl);
3927 llvm::GlobalValue *Entry = CGM.GetGlobalValue(MangledName);
3928 if (Entry && !Entry->isDeclaration())
3929 return;
3930
3931 auto *Aliasee = cast<llvm::GlobalValue>(CGM.GetAddrOfGlobal(TargetDecl));
Rafael Espindola1e4df922014-09-16 15:18:21 +00003932
3933 // Create the alias with no name.
David Blaikie2a791d72015-09-14 18:38:22 +00003934 auto *Alias = llvm::GlobalAlias::create(Linkage, "", Aliasee);
Rafael Espindola1e4df922014-09-16 15:18:21 +00003935
Peter Collingbourned914fd22018-06-18 20:58:54 +00003936 // Constructors and destructors are always unnamed_addr.
3937 Alias->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3938
Rafael Espindola1e4df922014-09-16 15:18:21 +00003939 // Switch any previous uses to the alias.
3940 if (Entry) {
NAKAMURA Takumie9621042015-09-15 01:39:27 +00003941 assert(Entry->getType() == Aliasee->getType() &&
Rafael Espindola1e4df922014-09-16 15:18:21 +00003942 "declaration exists with different type");
3943 Alias->takeName(Entry);
3944 Entry->replaceAllUsesWith(Alias);
3945 Entry->eraseFromParent();
3946 } else {
3947 Alias->setName(MangledName);
3948 }
3949
3950 // Finally, set up the alias with its proper name and attributes.
Rafael Espindolab7350042018-03-01 00:35:47 +00003951 CGM.SetCommonAttributes(AliasDecl, Alias);
Rafael Espindola1e4df922014-09-16 15:18:21 +00003952}
3953
Peter Collingbourned1c5b282019-03-22 23:05:10 +00003954void ItaniumCXXABI::emitCXXStructor(GlobalDecl GD) {
3955 auto *MD = cast<CXXMethodDecl>(GD.getDecl());
Rafael Espindola1e4df922014-09-16 15:18:21 +00003956 auto *CD = dyn_cast<CXXConstructorDecl>(MD);
3957 const CXXDestructorDecl *DD = CD ? nullptr : cast<CXXDestructorDecl>(MD);
3958
3959 StructorCodegen CGType = getCodegenToUse(CGM, MD);
3960
Peter Collingbourned1c5b282019-03-22 23:05:10 +00003961 if (CD ? GD.getCtorType() == Ctor_Complete
3962 : GD.getDtorType() == Dtor_Complete) {
Rafael Espindola1e4df922014-09-16 15:18:21 +00003963 GlobalDecl BaseDecl;
Peter Collingbourned1c5b282019-03-22 23:05:10 +00003964 if (CD)
3965 BaseDecl = GD.getWithCtorType(Ctor_Base);
3966 else
3967 BaseDecl = GD.getWithDtorType(Dtor_Base);
Rafael Espindola1e4df922014-09-16 15:18:21 +00003968
3969 if (CGType == StructorCodegen::Alias || CGType == StructorCodegen::COMDAT) {
Peter Collingbourned1c5b282019-03-22 23:05:10 +00003970 emitConstructorDestructorAlias(CGM, GD, BaseDecl);
Rafael Espindola1e4df922014-09-16 15:18:21 +00003971 return;
3972 }
3973
3974 if (CGType == StructorCodegen::RAUW) {
Peter Collingbourned1c5b282019-03-22 23:05:10 +00003975 StringRef MangledName = CGM.getMangledName(GD);
Andrey Bokhankocab58582015-08-31 13:20:44 +00003976 auto *Aliasee = CGM.GetAddrOfGlobal(BaseDecl);
Rafael Espindola1e4df922014-09-16 15:18:21 +00003977 CGM.addReplacement(MangledName, Aliasee);
3978 return;
Rafael Espindola91f68b42014-09-15 19:20:10 +00003979 }
3980 }
3981
3982 // The base destructor is equivalent to the base destructor of its
3983 // base class if there is exactly one non-virtual base class with a
3984 // non-trivial destructor, there are no fields with a non-trivial
3985 // destructor, and the body of the destructor is trivial.
Peter Collingbourned1c5b282019-03-22 23:05:10 +00003986 if (DD && GD.getDtorType() == Dtor_Base &&
3987 CGType != StructorCodegen::COMDAT &&
Rafael Espindola1e4df922014-09-16 15:18:21 +00003988 !CGM.TryEmitBaseDestructorAsAlias(DD))
Rafael Espindola91f68b42014-09-15 19:20:10 +00003989 return;
3990
Richard Smith5b349582017-10-13 01:55:36 +00003991 // FIXME: The deleting destructor is equivalent to the selected operator
3992 // delete if:
3993 // * either the delete is a destroying operator delete or the destructor
3994 // would be trivial if it weren't virtual,
3995 // * the conversion from the 'this' parameter to the first parameter of the
3996 // destructor is equivalent to a bitcast,
3997 // * the destructor does not have an implicit "this" return, and
3998 // * the operator delete has the same calling convention and IR function type
3999 // as the destructor.
4000 // In such cases we should try to emit the deleting dtor as an alias to the
4001 // selected 'operator delete'.
4002
Peter Collingbourned1c5b282019-03-22 23:05:10 +00004003 llvm::Function *Fn = CGM.codegenCXXStructor(GD);
Rafael Espindola91f68b42014-09-15 19:20:10 +00004004
Rafael Espindola1e4df922014-09-16 15:18:21 +00004005 if (CGType == StructorCodegen::COMDAT) {
4006 SmallString<256> Buffer;
4007 llvm::raw_svector_ostream Out(Buffer);
4008 if (DD)
4009 getMangleContext().mangleCXXDtorComdat(DD, Out);
4010 else
4011 getMangleContext().mangleCXXCtorComdat(CD, Out);
4012 llvm::Comdat *C = CGM.getModule().getOrInsertComdat(Out.str());
4013 Fn->setComdat(C);
Rafael Espindoladbee8a72015-01-15 21:36:08 +00004014 } else {
4015 CGM.maybeSetTrivialComdat(*MD, *Fn);
Rafael Espindola91f68b42014-09-15 19:20:10 +00004016 }
Rafael Espindola91f68b42014-09-15 19:20:10 +00004017}
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004018
James Y Knight9871db02019-02-05 16:42:33 +00004019static llvm::FunctionCallee getBeginCatchFn(CodeGenModule &CGM) {
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004020 // void *__cxa_begin_catch(void*);
4021 llvm::FunctionType *FTy = llvm::FunctionType::get(
Rui Ueyama49a3ad22019-07-16 04:46:31 +00004022 CGM.Int8PtrTy, CGM.Int8PtrTy, /*isVarArg=*/false);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004023
4024 return CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
4025}
4026
James Y Knight9871db02019-02-05 16:42:33 +00004027static llvm::FunctionCallee getEndCatchFn(CodeGenModule &CGM) {
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004028 // void __cxa_end_catch();
4029 llvm::FunctionType *FTy =
Rui Ueyama49a3ad22019-07-16 04:46:31 +00004030 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004031
4032 return CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch");
4033}
4034
James Y Knight9871db02019-02-05 16:42:33 +00004035static llvm::FunctionCallee getGetExceptionPtrFn(CodeGenModule &CGM) {
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004036 // void *__cxa_get_exception_ptr(void*);
4037 llvm::FunctionType *FTy = llvm::FunctionType::get(
Rui Ueyama49a3ad22019-07-16 04:46:31 +00004038 CGM.Int8PtrTy, CGM.Int8PtrTy, /*isVarArg=*/false);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004039
4040 return CGM.CreateRuntimeFunction(FTy, "__cxa_get_exception_ptr");
4041}
4042
4043namespace {
4044 /// A cleanup to call __cxa_end_catch. In many cases, the caught
4045 /// exception type lets us state definitively that the thrown exception
4046 /// type does not have a destructor. In particular:
4047 /// - Catch-alls tell us nothing, so we have to conservatively
4048 /// assume that the thrown exception might have a destructor.
4049 /// - Catches by reference behave according to their base types.
4050 /// - Catches of non-record types will only trigger for exceptions
4051 /// of non-record types, which never have destructors.
4052 /// - Catches of record types can trigger for arbitrary subclasses
4053 /// of the caught type, so we have to assume the actual thrown
4054 /// exception type might have a throwing destructor, even if the
4055 /// caught type's destructor is trivial or nothrow.
David Blaikie7e70d682015-08-18 22:40:54 +00004056 struct CallEndCatch final : EHScopeStack::Cleanup {
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004057 CallEndCatch(bool MightThrow) : MightThrow(MightThrow) {}
4058 bool MightThrow;
4059
4060 void Emit(CodeGenFunction &CGF, Flags flags) override {
4061 if (!MightThrow) {
4062 CGF.EmitNounwindRuntimeCall(getEndCatchFn(CGF.CGM));
4063 return;
4064 }
4065
4066 CGF.EmitRuntimeCallOrInvoke(getEndCatchFn(CGF.CGM));
4067 }
4068 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00004069}
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004070
4071/// Emits a call to __cxa_begin_catch and enters a cleanup to call
4072/// __cxa_end_catch.
4073///
4074/// \param EndMightThrow - true if __cxa_end_catch might throw
4075static llvm::Value *CallBeginCatch(CodeGenFunction &CGF,
4076 llvm::Value *Exn,
4077 bool EndMightThrow) {
4078 llvm::CallInst *call =
4079 CGF.EmitNounwindRuntimeCall(getBeginCatchFn(CGF.CGM), Exn);
4080
4081 CGF.EHStack.pushCleanup<CallEndCatch>(NormalAndEHCleanup, EndMightThrow);
4082
4083 return call;
4084}
4085
4086/// A "special initializer" callback for initializing a catch
4087/// parameter during catch initialization.
4088static void InitCatchParam(CodeGenFunction &CGF,
4089 const VarDecl &CatchParam,
John McCall7f416cc2015-09-08 08:05:57 +00004090 Address ParamAddr,
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004091 SourceLocation Loc) {
4092 // Load the exception from where the landing pad saved it.
4093 llvm::Value *Exn = CGF.getExceptionFromSlot();
4094
4095 CanQualType CatchType =
4096 CGF.CGM.getContext().getCanonicalType(CatchParam.getType());
4097 llvm::Type *LLVMCatchTy = CGF.ConvertTypeForMem(CatchType);
4098
4099 // If we're catching by reference, we can just cast the object
4100 // pointer to the appropriate pointer.
4101 if (isa<ReferenceType>(CatchType)) {
4102 QualType CaughtType = cast<ReferenceType>(CatchType)->getPointeeType();
4103 bool EndCatchMightThrow = CaughtType->isRecordType();
4104
4105 // __cxa_begin_catch returns the adjusted object pointer.
4106 llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, EndCatchMightThrow);
4107
4108 // We have no way to tell the personality function that we're
4109 // catching by reference, so if we're catching a pointer,
4110 // __cxa_begin_catch will actually return that pointer by value.
4111 if (const PointerType *PT = dyn_cast<PointerType>(CaughtType)) {
4112 QualType PointeeType = PT->getPointeeType();
4113
4114 // When catching by reference, generally we should just ignore
4115 // this by-value pointer and use the exception object instead.
4116 if (!PointeeType->isRecordType()) {
4117
4118 // Exn points to the struct _Unwind_Exception header, which
4119 // we have to skip past in order to reach the exception data.
4120 unsigned HeaderSize =
4121 CGF.CGM.getTargetCodeGenInfo().getSizeOfUnwindException();
4122 AdjustedExn = CGF.Builder.CreateConstGEP1_32(Exn, HeaderSize);
4123
4124 // However, if we're catching a pointer-to-record type that won't
4125 // work, because the personality function might have adjusted
4126 // the pointer. There's actually no way for us to fully satisfy
4127 // the language/ABI contract here: we can't use Exn because it
4128 // might have the wrong adjustment, but we can't use the by-value
4129 // pointer because it's off by a level of abstraction.
4130 //
4131 // The current solution is to dump the adjusted pointer into an
4132 // alloca, which breaks language semantics (because changing the
4133 // pointer doesn't change the exception) but at least works.
4134 // The better solution would be to filter out non-exact matches
4135 // and rethrow them, but this is tricky because the rethrow
4136 // really needs to be catchable by other sites at this landing
4137 // pad. The best solution is to fix the personality function.
4138 } else {
4139 // Pull the pointer for the reference type off.
4140 llvm::Type *PtrTy =
4141 cast<llvm::PointerType>(LLVMCatchTy)->getElementType();
4142
4143 // Create the temporary and write the adjusted pointer into it.
John McCall7f416cc2015-09-08 08:05:57 +00004144 Address ExnPtrTmp =
4145 CGF.CreateTempAlloca(PtrTy, CGF.getPointerAlign(), "exn.byref.tmp");
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004146 llvm::Value *Casted = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
4147 CGF.Builder.CreateStore(Casted, ExnPtrTmp);
4148
4149 // Bind the reference to the temporary.
John McCall7f416cc2015-09-08 08:05:57 +00004150 AdjustedExn = ExnPtrTmp.getPointer();
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004151 }
4152 }
4153
4154 llvm::Value *ExnCast =
4155 CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.byref");
4156 CGF.Builder.CreateStore(ExnCast, ParamAddr);
4157 return;
4158 }
4159
4160 // Scalars and complexes.
4161 TypeEvaluationKind TEK = CGF.getEvaluationKind(CatchType);
4162 if (TEK != TEK_Aggregate) {
4163 llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, false);
4164
4165 // If the catch type is a pointer type, __cxa_begin_catch returns
4166 // the pointer by value.
4167 if (CatchType->hasPointerRepresentation()) {
4168 llvm::Value *CastExn =
4169 CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.casted");
4170
4171 switch (CatchType.getQualifiers().getObjCLifetime()) {
4172 case Qualifiers::OCL_Strong:
4173 CastExn = CGF.EmitARCRetainNonBlock(CastExn);
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +00004174 LLVM_FALLTHROUGH;
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004175
4176 case Qualifiers::OCL_None:
4177 case Qualifiers::OCL_ExplicitNone:
4178 case Qualifiers::OCL_Autoreleasing:
4179 CGF.Builder.CreateStore(CastExn, ParamAddr);
4180 return;
4181
4182 case Qualifiers::OCL_Weak:
4183 CGF.EmitARCInitWeak(ParamAddr, CastExn);
4184 return;
4185 }
4186 llvm_unreachable("bad ownership qualifier!");
4187 }
4188
4189 // Otherwise, it returns a pointer into the exception object.
4190
4191 llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
4192 llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
4193
4194 LValue srcLV = CGF.MakeNaturalAlignAddrLValue(Cast, CatchType);
John McCall7f416cc2015-09-08 08:05:57 +00004195 LValue destLV = CGF.MakeAddrLValue(ParamAddr, CatchType);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004196 switch (TEK) {
4197 case TEK_Complex:
4198 CGF.EmitStoreOfComplex(CGF.EmitLoadOfComplex(srcLV, Loc), destLV,
4199 /*init*/ true);
4200 return;
4201 case TEK_Scalar: {
4202 llvm::Value *ExnLoad = CGF.EmitLoadOfScalar(srcLV, Loc);
4203 CGF.EmitStoreOfScalar(ExnLoad, destLV, /*init*/ true);
4204 return;
4205 }
4206 case TEK_Aggregate:
4207 llvm_unreachable("evaluation kind filtered out!");
4208 }
4209 llvm_unreachable("bad evaluation kind");
4210 }
4211
4212 assert(isa<RecordType>(CatchType) && "unexpected catch type!");
John McCall7f416cc2015-09-08 08:05:57 +00004213 auto catchRD = CatchType->getAsCXXRecordDecl();
4214 CharUnits caughtExnAlignment = CGF.CGM.getClassPointerAlignment(catchRD);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004215
4216 llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
4217
4218 // Check for a copy expression. If we don't have a copy expression,
4219 // that means a trivial copy is okay.
4220 const Expr *copyExpr = CatchParam.getInit();
4221 if (!copyExpr) {
4222 llvm::Value *rawAdjustedExn = CallBeginCatch(CGF, Exn, true);
John McCall7f416cc2015-09-08 08:05:57 +00004223 Address adjustedExn(CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy),
4224 caughtExnAlignment);
Ivan A. Kosarev1860b522018-01-25 14:21:55 +00004225 LValue Dest = CGF.MakeAddrLValue(ParamAddr, CatchType);
4226 LValue Src = CGF.MakeAddrLValue(adjustedExn, CatchType);
Richard Smithe78fac52018-04-05 20:52:58 +00004227 CGF.EmitAggregateCopy(Dest, Src, CatchType, AggValueSlot::DoesNotOverlap);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004228 return;
4229 }
4230
4231 // We have to call __cxa_get_exception_ptr to get the adjusted
4232 // pointer before copying.
4233 llvm::CallInst *rawAdjustedExn =
4234 CGF.EmitNounwindRuntimeCall(getGetExceptionPtrFn(CGF.CGM), Exn);
4235
4236 // Cast that to the appropriate type.
John McCall7f416cc2015-09-08 08:05:57 +00004237 Address adjustedExn(CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy),
4238 caughtExnAlignment);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004239
4240 // The copy expression is defined in terms of an OpaqueValueExpr.
4241 // Find it and map it to the adjusted expression.
4242 CodeGenFunction::OpaqueValueMapping
4243 opaque(CGF, OpaqueValueExpr::findInCopyConstruct(copyExpr),
4244 CGF.MakeAddrLValue(adjustedExn, CatchParam.getType()));
4245
4246 // Call the copy ctor in a terminate scope.
4247 CGF.EHStack.pushTerminate();
4248
4249 // Perform the copy construction.
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004250 CGF.EmitAggExpr(copyExpr,
John McCall7f416cc2015-09-08 08:05:57 +00004251 AggValueSlot::forAddr(ParamAddr, Qualifiers(),
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004252 AggValueSlot::IsNotDestructed,
4253 AggValueSlot::DoesNotNeedGCBarriers,
Richard Smithe78fac52018-04-05 20:52:58 +00004254 AggValueSlot::IsNotAliased,
4255 AggValueSlot::DoesNotOverlap));
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004256
4257 // Leave the terminate scope.
4258 CGF.EHStack.popTerminate();
4259
4260 // Undo the opaque value mapping.
4261 opaque.pop();
4262
4263 // Finally we can call __cxa_begin_catch.
4264 CallBeginCatch(CGF, Exn, true);
4265}
4266
4267/// Begins a catch statement by initializing the catch variable and
4268/// calling __cxa_begin_catch.
4269void ItaniumCXXABI::emitBeginCatch(CodeGenFunction &CGF,
4270 const CXXCatchStmt *S) {
4271 // We have to be very careful with the ordering of cleanups here:
4272 // C++ [except.throw]p4:
4273 // The destruction [of the exception temporary] occurs
4274 // immediately after the destruction of the object declared in
4275 // the exception-declaration in the handler.
4276 //
4277 // So the precise ordering is:
4278 // 1. Construct catch variable.
4279 // 2. __cxa_begin_catch
4280 // 3. Enter __cxa_end_catch cleanup
4281 // 4. Enter dtor cleanup
4282 //
4283 // We do this by using a slightly abnormal initialization process.
4284 // Delegation sequence:
4285 // - ExitCXXTryStmt opens a RunCleanupsScope
4286 // - EmitAutoVarAlloca creates the variable and debug info
4287 // - InitCatchParam initializes the variable from the exception
4288 // - CallBeginCatch calls __cxa_begin_catch
4289 // - CallBeginCatch enters the __cxa_end_catch cleanup
4290 // - EmitAutoVarCleanups enters the variable destructor cleanup
4291 // - EmitCXXTryStmt emits the code for the catch body
4292 // - EmitCXXTryStmt close the RunCleanupsScope
4293
4294 VarDecl *CatchParam = S->getExceptionDecl();
4295 if (!CatchParam) {
4296 llvm::Value *Exn = CGF.getExceptionFromSlot();
4297 CallBeginCatch(CGF, Exn, true);
4298 return;
4299 }
4300
4301 // Emit the local.
4302 CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004303 InitCatchParam(CGF, *CatchParam, var.getObjectAddress(CGF), S->getBeginLoc());
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004304 CGF.EmitAutoVarCleanups(var);
4305}
4306
4307/// Get or define the following function:
4308/// void @__clang_call_terminate(i8* %exn) nounwind noreturn
4309/// This code is used only in C++.
James Y Knight9871db02019-02-05 16:42:33 +00004310static llvm::FunctionCallee getClangCallTerminateFn(CodeGenModule &CGM) {
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004311 llvm::FunctionType *fnTy =
Rui Ueyama49a3ad22019-07-16 04:46:31 +00004312 llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*isVarArg=*/false);
James Y Knight9871db02019-02-05 16:42:33 +00004313 llvm::FunctionCallee fnRef = CGM.CreateRuntimeFunction(
Rui Ueyama49a3ad22019-07-16 04:46:31 +00004314 fnTy, "__clang_call_terminate", llvm::AttributeList(), /*Local=*/true);
James Y Knight9871db02019-02-05 16:42:33 +00004315 llvm::Function *fn =
4316 cast<llvm::Function>(fnRef.getCallee()->stripPointerCasts());
4317 if (fn->empty()) {
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004318 fn->setDoesNotThrow();
4319 fn->setDoesNotReturn();
4320
4321 // What we really want is to massively penalize inlining without
4322 // forbidding it completely. The difference between that and
4323 // 'noinline' is negligible.
4324 fn->addFnAttr(llvm::Attribute::NoInline);
4325
4326 // Allow this function to be shared across translation units, but
4327 // we don't want it to turn into an exported symbol.
4328 fn->setLinkage(llvm::Function::LinkOnceODRLinkage);
4329 fn->setVisibility(llvm::Function::HiddenVisibility);
NAKAMURA Takumic7da6da2015-05-09 21:10:07 +00004330 if (CGM.supportsCOMDAT())
4331 fn->setComdat(CGM.getModule().getOrInsertComdat(fn->getName()));
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004332
4333 // Set up the function.
4334 llvm::BasicBlock *entry =
James Y Knight9871db02019-02-05 16:42:33 +00004335 llvm::BasicBlock::Create(CGM.getLLVMContext(), "", fn);
John McCall7f416cc2015-09-08 08:05:57 +00004336 CGBuilderTy builder(CGM, entry);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004337
4338 // Pull the exception pointer out of the parameter list.
4339 llvm::Value *exn = &*fn->arg_begin();
4340
4341 // Call __cxa_begin_catch(exn).
4342 llvm::CallInst *catchCall = builder.CreateCall(getBeginCatchFn(CGM), exn);
4343 catchCall->setDoesNotThrow();
4344 catchCall->setCallingConv(CGM.getRuntimeCC());
4345
4346 // Call std::terminate().
David Blaikie4ba525b2015-07-14 17:27:39 +00004347 llvm::CallInst *termCall = builder.CreateCall(CGM.getTerminateFn());
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004348 termCall->setDoesNotThrow();
4349 termCall->setDoesNotReturn();
4350 termCall->setCallingConv(CGM.getRuntimeCC());
4351
4352 // std::terminate cannot return.
4353 builder.CreateUnreachable();
4354 }
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004355 return fnRef;
4356}
4357
4358llvm::CallInst *
4359ItaniumCXXABI::emitTerminateForUnexpectedException(CodeGenFunction &CGF,
4360 llvm::Value *Exn) {
4361 // In C++, we want to call __cxa_begin_catch() before terminating.
4362 if (Exn) {
4363 assert(CGF.CGM.getLangOpts().CPlusPlus);
4364 return CGF.EmitNounwindRuntimeCall(getClangCallTerminateFn(CGF.CGM), Exn);
4365 }
4366 return CGF.EmitNounwindRuntimeCall(CGF.CGM.getTerminateFn());
4367}
Peter Collingbourne60108802017-12-13 21:53:04 +00004368
4369std::pair<llvm::Value *, const CXXRecordDecl *>
4370ItaniumCXXABI::LoadVTablePtr(CodeGenFunction &CGF, Address This,
4371 const CXXRecordDecl *RD) {
4372 return {CGF.GetVTablePtr(This, CGM.Int8PtrTy, RD), RD};
4373}
Heejin Ahnc6479192018-05-31 22:18:13 +00004374
4375void WebAssemblyCXXABI::emitBeginCatch(CodeGenFunction &CGF,
4376 const CXXCatchStmt *C) {
Heejin Ahn1eb074d2018-06-01 01:01:37 +00004377 if (CGF.getTarget().hasFeature("exception-handling"))
4378 CGF.EHStack.pushCleanup<CatchRetScope>(
4379 NormalCleanup, cast<llvm::CatchPadInst>(CGF.CurrentFuncletPad));
Heejin Ahnc6479192018-05-31 22:18:13 +00004380 ItaniumCXXABI::emitBeginCatch(CGF, C);
4381}