blob: 453bd2faa7e663ba82c75e776ba57992cc24200e [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
John McCall475999d2010-08-22 00:05:51 +000046protected:
Mark Seabornedf0d382013-07-24 16:25:13 +000047 bool UseARMMethodPtrABI;
48 bool UseARMGuardVarABI;
John McCalld23b27e2016-09-16 02:40:45 +000049 bool Use32BitVTableOffsetABI;
John McCall7a9aac22010-08-23 01:21:21 +000050
Timur Iskhodzhanov67455222013-10-03 06:26:13 +000051 ItaniumMangleContext &getMangleContext() {
52 return cast<ItaniumMangleContext>(CodeGen::CGCXXABI::getMangleContext());
53 }
54
Charles Davis4e786dd2010-05-25 19:52:27 +000055public:
Mark Seabornedf0d382013-07-24 16:25:13 +000056 ItaniumCXXABI(CodeGen::CodeGenModule &CGM,
57 bool UseARMMethodPtrABI = false,
58 bool UseARMGuardVarABI = false) :
59 CGCXXABI(CGM), UseARMMethodPtrABI(UseARMMethodPtrABI),
John McCalld23b27e2016-09-16 02:40:45 +000060 UseARMGuardVarABI(UseARMGuardVarABI),
Richard Smithb17d6fa2016-12-01 03:04:07 +000061 Use32BitVTableOffsetABI(false) { }
John McCall475999d2010-08-22 00:05:51 +000062
Reid Kleckner40ca9132014-05-13 22:05:45 +000063 bool classifyReturnType(CGFunctionInfo &FI) const override;
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000064
Richard Smithf667ad52017-08-26 01:04:35 +000065 bool passClassIndirect(const CXXRecordDecl *RD) const {
Richard Smithf667ad52017-08-26 01:04:35 +000066 return !canCopyArgument(RD);
67 }
68
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.
Richard Smithf667ad52017-08-26 01:04:35 +000071 if (passClassIndirect(RD))
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
Akira Hatanakac47fcf02017-07-27 18:52:44 +0000161 /// Itanium says that an _Unwind_Exception has to be "double-word"
162 /// aligned (and thus the end of it is also so-aligned), meaning 16
163 /// bytes. Of course, that was written for the actual Itanium,
164 /// which is a 64-bit platform. Classically, the ABI doesn't really
165 /// specify the alignment on other platforms, but in practice
166 /// libUnwind declares the struct with __attribute__((aligned)), so
167 /// we assume that alignment here. (It's generally 16 bytes, but
168 /// some targets overwrite it.)
John McCall7f416cc2015-09-08 08:05:57 +0000169 CharUnits getAlignmentOfExnObject() {
Akira Hatanakac47fcf02017-07-27 18:52:44 +0000170 auto align = CGM.getContext().getTargetDefaultAlignForAttributeAligned();
171 return CGM.getContext().toCharUnitsFromBits(align);
John McCall7f416cc2015-09-08 08:05:57 +0000172 }
173
David Majnemer442d0a22014-11-25 07:20:20 +0000174 void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override;
David Majnemer7c237072015-03-05 00:46:22 +0000175 void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) override;
David Majnemer442d0a22014-11-25 07:20:20 +0000176
Reid Klecknerfff8e7f2015-03-03 19:21:04 +0000177 void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) override;
178
179 llvm::CallInst *
180 emitTerminateForUnexpectedException(CodeGenFunction &CGF,
181 llvm::Value *Exn) override;
182
Thomas Andersonb6d87cf2018-07-24 00:43:47 +0000183 void EmitFundamentalRTTIDescriptors(const CXXRecordDecl *RD);
David Majnemer443250f2015-03-17 20:35:00 +0000184 llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override;
Reid Kleckner10aa7702015-09-16 20:15:55 +0000185 CatchTypeInfo
David Majnemer37b417f2015-03-29 21:55:10 +0000186 getAddrOfCXXCatchHandlerType(QualType Ty,
187 QualType CatchHandlerType) override {
Reid Kleckner10aa7702015-09-16 20:15:55 +0000188 return CatchTypeInfo{getAddrOfRTTIDescriptor(Ty), 0};
David Majnemer443250f2015-03-17 20:35:00 +0000189 }
David Majnemere2cb8d12014-07-07 06:20:47 +0000190
David Majnemer1162d252014-06-22 19:05:33 +0000191 bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override;
192 void EmitBadTypeidCall(CodeGenFunction &CGF) override;
193 llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy,
John McCall7f416cc2015-09-08 08:05:57 +0000194 Address ThisPtr,
David Majnemer1162d252014-06-22 19:05:33 +0000195 llvm::Type *StdTypeInfoPtrTy) override;
196
197 bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
198 QualType SrcRecordTy) override;
199
John McCall7f416cc2015-09-08 08:05:57 +0000200 llvm::Value *EmitDynamicCastCall(CodeGenFunction &CGF, Address Value,
David Majnemer1162d252014-06-22 19:05:33 +0000201 QualType SrcRecordTy, QualType DestTy,
202 QualType DestRecordTy,
203 llvm::BasicBlock *CastEnd) override;
204
John McCall7f416cc2015-09-08 08:05:57 +0000205 llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, Address Value,
David Majnemer1162d252014-06-22 19:05:33 +0000206 QualType SrcRecordTy,
207 QualType DestTy) override;
208
209 bool EmitBadCastCall(CodeGenFunction &CGF) override;
210
Craig Topper4f12f102014-03-12 06:41:41 +0000211 llvm::Value *
John McCall7f416cc2015-09-08 08:05:57 +0000212 GetVirtualBaseClassOffset(CodeGenFunction &CGF, Address This,
Craig Topper4f12f102014-03-12 06:41:41 +0000213 const CXXRecordDecl *ClassDecl,
214 const CXXRecordDecl *BaseClassDecl) override;
Reid Klecknerd8cbeec2013-05-29 18:02:47 +0000215
Craig Topper4f12f102014-03-12 06:41:41 +0000216 void EmitCXXConstructors(const CXXConstructorDecl *D) override;
Timur Iskhodzhanov40f2fa92013-08-04 17:30:04 +0000217
George Burgess IVf203dbf2017-02-22 20:28:02 +0000218 AddedStructorArgs
219 buildStructorSignature(const CXXMethodDecl *MD, StructorType T,
220 SmallVectorImpl<CanQualType> &ArgTys) override;
John McCall5d865c322010-08-31 07:33:07 +0000221
Reid Klecknere7de47e2013-07-22 13:51:44 +0000222 bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
Craig Topper4f12f102014-03-12 06:41:41 +0000223 CXXDtorType DT) const override {
Reid Klecknere7de47e2013-07-22 13:51:44 +0000224 // Itanium does not emit any destructor variant as an inline thunk.
225 // Delegating may occur as an optimization, but all variants are either
226 // emitted with external linkage or as linkonce if they are inline and used.
227 return false;
228 }
229
Craig Topper4f12f102014-03-12 06:41:41 +0000230 void EmitCXXDestructors(const CXXDestructorDecl *D) override;
Reid Klecknere7de47e2013-07-22 13:51:44 +0000231
Reid Kleckner89077a12013-12-17 19:46:40 +0000232 void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy,
Craig Topper4f12f102014-03-12 06:41:41 +0000233 FunctionArgList &Params) override;
John McCall5d865c322010-08-31 07:33:07 +0000234
Craig Topper4f12f102014-03-12 06:41:41 +0000235 void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override;
John McCall8ed55a52010-09-02 09:58:18 +0000236
George Burgess IVf203dbf2017-02-22 20:28:02 +0000237 AddedStructorArgs
238 addImplicitConstructorArgs(CodeGenFunction &CGF, const CXXConstructorDecl *D,
239 CXXCtorType Type, bool ForVirtualBase,
240 bool Delegating, CallArgList &Args) override;
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000241
Reid Kleckner6fe771a2013-12-13 00:53:54 +0000242 void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD,
243 CXXDtorType Type, bool ForVirtualBase,
John McCall7f416cc2015-09-08 08:05:57 +0000244 bool Delegating, Address This) override;
Reid Kleckner6fe771a2013-12-13 00:53:54 +0000245
Craig Topper4f12f102014-03-12 06:41:41 +0000246 void emitVTableDefinitions(CodeGenVTables &CGVT,
247 const CXXRecordDecl *RD) override;
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000248
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000249 bool isVirtualOffsetNeededForVTableField(CodeGenFunction &CGF,
250 CodeGenFunction::VPtr Vptr) override;
251
252 bool doStructorsInitializeVPtrs(const CXXRecordDecl *VTableClass) override {
253 return true;
254 }
255
256 llvm::Constant *
257 getVTableAddressPoint(BaseSubobject Base,
258 const CXXRecordDecl *VTableClass) override;
259
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000260 llvm::Value *getVTableAddressPointInStructor(
261 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass,
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000262 BaseSubobject Base, const CXXRecordDecl *NearestVBase) override;
263
264 llvm::Value *getVTableAddressPointInStructorWithVTT(
265 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass,
266 BaseSubobject Base, const CXXRecordDecl *NearestVBase);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000267
268 llvm::Constant *
269 getVTableAddressPointForConstExpr(BaseSubobject Base,
Craig Topper4f12f102014-03-12 06:41:41 +0000270 const CXXRecordDecl *VTableClass) override;
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000271
272 llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
Craig Topper4f12f102014-03-12 06:41:41 +0000273 CharUnits VPtrOffset) override;
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000274
John McCall9831b842018-02-06 18:52:44 +0000275 CGCallee getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD,
276 Address This, llvm::Type *Ty,
277 SourceLocation Loc) override;
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000278
David Majnemer0c0b6d92014-10-31 20:09:12 +0000279 llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF,
280 const CXXDestructorDecl *Dtor,
281 CXXDtorType DtorType,
John McCall7f416cc2015-09-08 08:05:57 +0000282 Address This,
David Majnemer0c0b6d92014-10-31 20:09:12 +0000283 const CXXMemberCallExpr *CE) override;
Timur Iskhodzhanovd6197112013-02-15 14:45:22 +0000284
Craig Topper4f12f102014-03-12 06:41:41 +0000285 void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override;
Reid Kleckner7810af02013-06-19 15:20:38 +0000286
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000287 bool canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const override;
Richard Smithc195c252018-11-27 19:33:49 +0000288 bool canSpeculativelyEmitVTableAsBaseClass(const CXXRecordDecl *RD) const;
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000289
Hans Wennborgc94391d2014-06-06 20:04:01 +0000290 void setThunkLinkage(llvm::Function *Thunk, bool ForVTable, GlobalDecl GD,
291 bool ReturnAdjustment) override {
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +0000292 // Allow inlining of thunks by emitting them with available_externally
293 // linkage together with vtables when needed.
Peter Collingbourne8fabc1b2015-07-01 02:10:26 +0000294 if (ForVTable && !Thunk->hasLocalLinkage())
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +0000295 Thunk->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
Rafael Espindolab7350042018-03-01 00:35:47 +0000296 CGM.setGVProperties(Thunk, GD);
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +0000297 }
298
Rafael Espindolab7350042018-03-01 00:35:47 +0000299 bool exportThunk() override { return true; }
300
John McCall7f416cc2015-09-08 08:05:57 +0000301 llvm::Value *performThisAdjustment(CodeGenFunction &CGF, Address This,
Craig Topper4f12f102014-03-12 06:41:41 +0000302 const ThisAdjustment &TA) override;
Timur Iskhodzhanov02014322013-10-30 11:55:43 +0000303
John McCall7f416cc2015-09-08 08:05:57 +0000304 llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, Address Ret,
Craig Topper4f12f102014-03-12 06:41:41 +0000305 const ReturnAdjustment &RA) override;
Timur Iskhodzhanov02014322013-10-30 11:55:43 +0000306
David Majnemer196ac332014-09-11 23:05:02 +0000307 size_t getSrcArgforCopyCtor(const CXXConstructorDecl *,
308 FunctionArgList &Args) const override {
309 assert(!Args.empty() && "expected the arglist to not be empty!");
310 return Args.size() - 1;
311 }
312
Craig Topper4f12f102014-03-12 06:41:41 +0000313 StringRef GetPureVirtualCallName() override { return "__cxa_pure_virtual"; }
314 StringRef GetDeletedVirtualCallName() override
315 { return "__cxa_deleted_virtual"; }
Joao Matos2ce88ef2012-07-17 17:10:11 +0000316
Craig Topper4f12f102014-03-12 06:41:41 +0000317 CharUnits getArrayCookieSizeImpl(QualType elementType) override;
John McCall7f416cc2015-09-08 08:05:57 +0000318 Address InitializeArrayCookie(CodeGenFunction &CGF,
319 Address NewPtr,
320 llvm::Value *NumElements,
321 const CXXNewExpr *expr,
322 QualType ElementType) override;
John McCallb91cd662012-05-01 05:23:51 +0000323 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +0000324 Address allocPtr,
Craig Topper4f12f102014-03-12 06:41:41 +0000325 CharUnits cookieSize) override;
John McCall68ff0372010-09-08 01:44:27 +0000326
John McCallcdf7ef52010-11-06 09:44:32 +0000327 void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
Craig Topper4f12f102014-03-12 06:41:41 +0000328 llvm::GlobalVariable *DeclPtr,
329 bool PerformInit) override;
Richard Smithdbf74ba2013-04-14 23:01:42 +0000330 void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
Craig Topper4f12f102014-03-12 06:41:41 +0000331 llvm::Constant *dtor, llvm::Constant *addr) override;
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000332
333 llvm::Function *getOrCreateThreadLocalWrapper(const VarDecl *VD,
Alexander Musmanf94c3182014-09-26 06:28:25 +0000334 llvm::Value *Val);
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000335 void EmitThreadLocalInitFuncs(
David Majnemerb3341ea2014-10-05 05:05:40 +0000336 CodeGenModule &CGM,
Richard Smith5a99c492015-12-01 01:10:48 +0000337 ArrayRef<const VarDecl *> CXXThreadLocals,
David Majnemerb3341ea2014-10-05 05:05:40 +0000338 ArrayRef<llvm::Function *> CXXThreadLocalInits,
Richard Smith5a99c492015-12-01 01:10:48 +0000339 ArrayRef<const VarDecl *> CXXThreadLocalInitVars) override;
David Majnemerb3341ea2014-10-05 05:05:40 +0000340
341 bool usesThreadWrapperFunction() const override { return true; }
Richard Smith0f383742014-03-26 22:48:22 +0000342 LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD,
343 QualType LValType) override;
Peter Collingbourne66f82e62013-06-28 20:45:28 +0000344
Craig Topper4f12f102014-03-12 06:41:41 +0000345 bool NeedsVTTParameter(GlobalDecl GD) override;
David Majnemere2cb8d12014-07-07 06:20:47 +0000346
347 /**************************** RTTI Uniqueness ******************************/
348
349protected:
350 /// Returns true if the ABI requires RTTI type_info objects to be unique
351 /// across a program.
352 virtual bool shouldRTTIBeUnique() const { return true; }
353
354public:
355 /// What sort of unique-RTTI behavior should we use?
356 enum RTTIUniquenessKind {
357 /// We are guaranteeing, or need to guarantee, that the RTTI string
358 /// is unique.
359 RUK_Unique,
360
361 /// We are not guaranteeing uniqueness for the RTTI string, so we
362 /// can demote to hidden visibility but must use string comparisons.
363 RUK_NonUniqueHidden,
364
365 /// We are not guaranteeing uniqueness for the RTTI string, so we
366 /// have to use string comparisons, but we also have to emit it with
367 /// non-hidden visibility.
368 RUK_NonUniqueVisible
369 };
370
371 /// Return the required visibility status for the given type and linkage in
372 /// the current ABI.
373 RTTIUniquenessKind
374 classifyRTTIUniqueness(QualType CanTy,
375 llvm::GlobalValue::LinkageTypes Linkage) const;
376 friend class ItaniumRTTIBuilder;
Rafael Espindola91f68b42014-09-15 19:20:10 +0000377
378 void emitCXXStructor(const CXXMethodDecl *MD, StructorType Type) override;
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000379
Peter Collingbourne60108802017-12-13 21:53:04 +0000380 std::pair<llvm::Value *, const CXXRecordDecl *>
381 LoadVTablePtr(CodeGenFunction &CGF, Address This,
382 const CXXRecordDecl *RD) override;
383
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000384 private:
Piotr Padlewskid3b1cbd2017-06-01 08:04:05 +0000385 bool hasAnyUnusedVirtualInlineFunction(const CXXRecordDecl *RD) const {
386 const auto &VtableLayout =
387 CGM.getItaniumVTableContext().getVTableLayout(RD);
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000388
Piotr Padlewskid3b1cbd2017-06-01 08:04:05 +0000389 for (const auto &VtableComponent : VtableLayout.vtable_components()) {
390 // Skip empty slot.
391 if (!VtableComponent.isUsedFunctionPointerKind())
392 continue;
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000393
Piotr Padlewskid3b1cbd2017-06-01 08:04:05 +0000394 const CXXMethodDecl *Method = VtableComponent.getFunctionDecl();
395 if (!Method->getCanonicalDecl()->isInlined())
396 continue;
397
398 StringRef Name = CGM.getMangledName(VtableComponent.getGlobalDecl());
399 auto *Entry = CGM.GetGlobalValue(Name);
400 // This checks if virtual inline function has already been emitted.
401 // Note that it is possible that this inline function would be emitted
402 // after trying to emit vtable speculatively. Because of this we do
403 // an extra pass after emitting all deferred vtables to find and emit
404 // these vtables opportunistically.
405 if (!Entry || Entry->isDeclaration())
406 return true;
407 }
408 return false;
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000409 }
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000410
411 bool isVTableHidden(const CXXRecordDecl *RD) const {
412 const auto &VtableLayout =
413 CGM.getItaniumVTableContext().getVTableLayout(RD);
414
415 for (const auto &VtableComponent : VtableLayout.vtable_components()) {
416 if (VtableComponent.isRTTIKind()) {
417 const CXXRecordDecl *RTTIDecl = VtableComponent.getRTTIDecl();
418 if (RTTIDecl->getVisibility() == Visibility::HiddenVisibility)
419 return true;
420 } else if (VtableComponent.isUsedFunctionPointerKind()) {
421 const CXXMethodDecl *Method = VtableComponent.getFunctionDecl();
422 if (Method->getVisibility() == Visibility::HiddenVisibility &&
423 !Method->isDefined())
424 return true;
425 }
426 }
427 return false;
428 }
Charles Davis4e786dd2010-05-25 19:52:27 +0000429};
John McCall86353412010-08-21 22:46:04 +0000430
431class ARMCXXABI : public ItaniumCXXABI {
432public:
Mark Seabornedf0d382013-07-24 16:25:13 +0000433 ARMCXXABI(CodeGen::CodeGenModule &CGM) :
434 ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
435 /* UseARMGuardVarABI = */ true) {}
John McCall5d865c322010-08-31 07:33:07 +0000436
Craig Topper4f12f102014-03-12 06:41:41 +0000437 bool HasThisReturn(GlobalDecl GD) const override {
Stephen Lin9dc6eef2013-06-30 20:40:16 +0000438 return (isa<CXXConstructorDecl>(GD.getDecl()) || (
439 isa<CXXDestructorDecl>(GD.getDecl()) &&
440 GD.getDtorType() != Dtor_Deleting));
441 }
John McCall5d865c322010-08-31 07:33:07 +0000442
Craig Topper4f12f102014-03-12 06:41:41 +0000443 void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV,
444 QualType ResTy) override;
John McCall5d865c322010-08-31 07:33:07 +0000445
Craig Topper4f12f102014-03-12 06:41:41 +0000446 CharUnits getArrayCookieSizeImpl(QualType elementType) override;
John McCall7f416cc2015-09-08 08:05:57 +0000447 Address InitializeArrayCookie(CodeGenFunction &CGF,
448 Address NewPtr,
449 llvm::Value *NumElements,
450 const CXXNewExpr *expr,
451 QualType ElementType) override;
452 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, Address allocPtr,
Craig Topper4f12f102014-03-12 06:41:41 +0000453 CharUnits cookieSize) override;
John McCall86353412010-08-21 22:46:04 +0000454};
Tim Northovera2ee4332014-03-29 15:09:45 +0000455
456class iOS64CXXABI : public ARMCXXABI {
457public:
John McCalld23b27e2016-09-16 02:40:45 +0000458 iOS64CXXABI(CodeGen::CodeGenModule &CGM) : ARMCXXABI(CGM) {
459 Use32BitVTableOffsetABI = true;
460 }
Tim Northover65f582f2014-03-30 17:32:48 +0000461
462 // ARM64 libraries are prepared for non-unique RTTI.
David Majnemere2cb8d12014-07-07 06:20:47 +0000463 bool shouldRTTIBeUnique() const override { return false; }
Tim Northovera2ee4332014-03-29 15:09:45 +0000464};
Dan Gohmanc2853072015-09-03 22:51:53 +0000465
466class WebAssemblyCXXABI final : public ItaniumCXXABI {
467public:
468 explicit WebAssemblyCXXABI(CodeGen::CodeGenModule &CGM)
469 : ItaniumCXXABI(CGM, /*UseARMMethodPtrABI=*/true,
470 /*UseARMGuardVarABI=*/true) {}
Heejin Ahnc6479192018-05-31 22:18:13 +0000471 void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) override;
Dan Gohmanc2853072015-09-03 22:51:53 +0000472
473private:
474 bool HasThisReturn(GlobalDecl GD) const override {
475 return isa<CXXConstructorDecl>(GD.getDecl()) ||
476 (isa<CXXDestructorDecl>(GD.getDecl()) &&
477 GD.getDtorType() != Dtor_Deleting);
478 }
Derek Schuff8179be42016-05-10 17:44:55 +0000479 bool canCallMismatchedFunctionType() const override { return false; }
Dan Gohmanc2853072015-09-03 22:51:53 +0000480};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000481}
Charles Davis4e786dd2010-05-25 19:52:27 +0000482
Charles Davis53c59df2010-08-16 03:33:14 +0000483CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
John McCallc8e01702013-04-16 22:48:15 +0000484 switch (CGM.getTarget().getCXXABI().getKind()) {
John McCall57625922013-01-25 23:36:14 +0000485 // For IR-generation purposes, there's no significant difference
486 // between the ARM and iOS ABIs.
487 case TargetCXXABI::GenericARM:
488 case TargetCXXABI::iOS:
Tim Northover756447a2015-10-30 16:30:36 +0000489 case TargetCXXABI::WatchOS:
John McCall57625922013-01-25 23:36:14 +0000490 return new ARMCXXABI(CGM);
Charles Davis4e786dd2010-05-25 19:52:27 +0000491
Tim Northovera2ee4332014-03-29 15:09:45 +0000492 case TargetCXXABI::iOS64:
493 return new iOS64CXXABI(CGM);
494
Tim Northover9bb857a2013-01-31 12:13:10 +0000495 // Note that AArch64 uses the generic ItaniumCXXABI class since it doesn't
496 // include the other 32-bit ARM oddities: constructor/destructor return values
497 // and array cookies.
498 case TargetCXXABI::GenericAArch64:
Mark Seabornedf0d382013-07-24 16:25:13 +0000499 return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
500 /* UseARMGuardVarABI = */ true);
Tim Northover9bb857a2013-01-31 12:13:10 +0000501
Zoran Jovanovic26a12162015-02-18 15:21:35 +0000502 case TargetCXXABI::GenericMIPS:
503 return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true);
504
Dan Gohmanc2853072015-09-03 22:51:53 +0000505 case TargetCXXABI::WebAssembly:
506 return new WebAssemblyCXXABI(CGM);
507
John McCall57625922013-01-25 23:36:14 +0000508 case TargetCXXABI::GenericItanium:
Mark Seabornedf0d382013-07-24 16:25:13 +0000509 if (CGM.getContext().getTargetInfo().getTriple().getArch()
510 == llvm::Triple::le32) {
511 // For PNaCl, use ARM-style method pointers so that PNaCl code
512 // does not assume anything about the alignment of function
513 // pointers.
514 return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
515 /* UseARMGuardVarABI = */ false);
516 }
John McCall57625922013-01-25 23:36:14 +0000517 return new ItaniumCXXABI(CGM);
518
519 case TargetCXXABI::Microsoft:
520 llvm_unreachable("Microsoft ABI is not Itanium-based");
521 }
522 llvm_unreachable("bad ABI kind");
John McCall86353412010-08-21 22:46:04 +0000523}
524
Chris Lattnera5f58b02011-07-09 17:41:47 +0000525llvm::Type *
John McCall7a9aac22010-08-23 01:21:21 +0000526ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
527 if (MPT->isMemberDataPointer())
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000528 return CGM.PtrDiffTy;
Serge Guelton1d993272017-05-09 19:31:30 +0000529 return llvm::StructType::get(CGM.PtrDiffTy, CGM.PtrDiffTy);
John McCall1c456c82010-08-22 06:43:33 +0000530}
531
John McCalld9c6c0b2010-08-22 00:59:17 +0000532/// In the Itanium and ARM ABIs, method pointers have the form:
533/// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
534///
535/// In the Itanium ABI:
536/// - method pointers are virtual if (memptr.ptr & 1) is nonzero
537/// - the this-adjustment is (memptr.adj)
538/// - the virtual offset is (memptr.ptr - 1)
539///
540/// In the ARM ABI:
541/// - method pointers are virtual if (memptr.adj & 1) is nonzero
542/// - the this-adjustment is (memptr.adj >> 1)
543/// - the virtual offset is (memptr.ptr)
544/// ARM uses 'adj' for the virtual flag because Thumb functions
545/// may be only single-byte aligned.
546///
547/// If the member is virtual, the adjusted 'this' pointer points
548/// to a vtable pointer from which the virtual offset is applied.
549///
550/// If the member is non-virtual, memptr.ptr is the address of
551/// the function to call.
John McCallb92ab1a2016-10-26 23:46:34 +0000552CGCallee ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(
John McCall7f416cc2015-09-08 08:05:57 +0000553 CodeGenFunction &CGF, const Expr *E, Address ThisAddr,
554 llvm::Value *&ThisPtrForCall,
David Majnemer2b0d66d2014-02-20 23:22:07 +0000555 llvm::Value *MemFnPtr, const MemberPointerType *MPT) {
John McCall475999d2010-08-22 00:05:51 +0000556 CGBuilderTy &Builder = CGF.Builder;
557
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000558 const FunctionProtoType *FPT =
John McCall475999d2010-08-22 00:05:51 +0000559 MPT->getPointeeType()->getAs<FunctionProtoType>();
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000560 const CXXRecordDecl *RD =
John McCall475999d2010-08-22 00:05:51 +0000561 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
562
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000563 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(
564 CGM.getTypes().arrangeCXXMethodType(RD, FPT, /*FD=*/nullptr));
John McCall475999d2010-08-22 00:05:51 +0000565
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000566 llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(CGM.PtrDiffTy, 1);
John McCall475999d2010-08-22 00:05:51 +0000567
John McCalld9c6c0b2010-08-22 00:59:17 +0000568 llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
569 llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
570 llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
571
John McCalla1dee5302010-08-22 10:59:02 +0000572 // Extract memptr.adj, which is in the second field.
573 llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
John McCalld9c6c0b2010-08-22 00:59:17 +0000574
575 // Compute the true adjustment.
576 llvm::Value *Adj = RawAdj;
Mark Seabornedf0d382013-07-24 16:25:13 +0000577 if (UseARMMethodPtrABI)
John McCalld9c6c0b2010-08-22 00:59:17 +0000578 Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
John McCall475999d2010-08-22 00:05:51 +0000579
580 // Apply the adjustment and cast back to the original struct type
581 // for consistency.
John McCall7f416cc2015-09-08 08:05:57 +0000582 llvm::Value *This = ThisAddr.getPointer();
John McCalld9c6c0b2010-08-22 00:59:17 +0000583 llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
584 Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
585 This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
John McCall7f416cc2015-09-08 08:05:57 +0000586 ThisPtrForCall = This;
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000587
John McCall475999d2010-08-22 00:05:51 +0000588 // Load the function pointer.
John McCalla1dee5302010-08-22 10:59:02 +0000589 llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000590
John McCall475999d2010-08-22 00:05:51 +0000591 // If the LSB in the function pointer is 1, the function pointer points to
592 // a virtual function.
John McCalld9c6c0b2010-08-22 00:59:17 +0000593 llvm::Value *IsVirtual;
Mark Seabornedf0d382013-07-24 16:25:13 +0000594 if (UseARMMethodPtrABI)
John McCalld9c6c0b2010-08-22 00:59:17 +0000595 IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
596 else
597 IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
598 IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
John McCall475999d2010-08-22 00:05:51 +0000599 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
600
601 // In the virtual path, the adjustment left 'This' pointing to the
602 // vtable of the correct base subobject. The "function pointer" is an
John McCalld9c6c0b2010-08-22 00:59:17 +0000603 // offset within the vtable (+1 for the virtual flag on non-ARM).
John McCall475999d2010-08-22 00:05:51 +0000604 CGF.EmitBlock(FnVirtual);
605
606 // Cast the adjusted this to a pointer to vtable pointer and load.
Chris Lattner2192fe52011-07-18 04:24:23 +0000607 llvm::Type *VTableTy = Builder.getInt8PtrTy();
John McCall7f416cc2015-09-08 08:05:57 +0000608 CharUnits VTablePtrAlign =
609 CGF.CGM.getDynamicOffsetAlignment(ThisAddr.getAlignment(), RD,
610 CGF.getPointerAlign());
611 llvm::Value *VTable =
Piotr Padlewski4b1ac722015-09-15 21:46:55 +0000612 CGF.GetVTablePtr(Address(This, VTablePtrAlign), VTableTy, RD);
John McCall475999d2010-08-22 00:05:51 +0000613
614 // Apply the offset.
John McCalld23b27e2016-09-16 02:40:45 +0000615 // On ARM64, to reserve extra space in virtual member function pointers,
616 // we only pay attention to the low 32 bits of the offset.
John McCalld9c6c0b2010-08-22 00:59:17 +0000617 llvm::Value *VTableOffset = FnAsInt;
Mark Seabornedf0d382013-07-24 16:25:13 +0000618 if (!UseARMMethodPtrABI)
619 VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
John McCalld23b27e2016-09-16 02:40:45 +0000620 if (Use32BitVTableOffsetABI) {
621 VTableOffset = Builder.CreateTrunc(VTableOffset, CGF.Int32Ty);
622 VTableOffset = Builder.CreateZExt(VTableOffset, CGM.PtrDiffTy);
623 }
Peter Collingbournee44acad2018-06-26 02:15:47 +0000624 // Compute the address of the virtual function pointer.
625 llvm::Value *VFPAddr = Builder.CreateGEP(VTable, VTableOffset);
626
627 // Check the address of the function pointer if CFI on member function
628 // pointers is enabled.
629 llvm::Constant *CheckSourceLocation;
630 llvm::Constant *CheckTypeDesc;
631 bool ShouldEmitCFICheck = CGF.SanOpts.has(SanitizerKind::CFIMFCall) &&
632 CGM.HasHiddenLTOVisibility(RD);
633 if (ShouldEmitCFICheck) {
634 CodeGenFunction::SanitizerScope SanScope(&CGF);
635
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000636 CheckSourceLocation = CGF.EmitCheckSourceLocation(E->getBeginLoc());
Peter Collingbournee44acad2018-06-26 02:15:47 +0000637 CheckTypeDesc = CGF.EmitCheckTypeDescriptor(QualType(MPT, 0));
638 llvm::Constant *StaticData[] = {
639 llvm::ConstantInt::get(CGF.Int8Ty, CodeGenFunction::CFITCK_VMFCall),
640 CheckSourceLocation,
641 CheckTypeDesc,
642 };
643
644 llvm::Metadata *MD =
645 CGM.CreateMetadataIdentifierForVirtualMemPtrType(QualType(MPT, 0));
646 llvm::Value *TypeId = llvm::MetadataAsValue::get(CGF.getLLVMContext(), MD);
647
648 llvm::Value *TypeTest = Builder.CreateCall(
649 CGM.getIntrinsic(llvm::Intrinsic::type_test), {VFPAddr, TypeId});
650
651 if (CGM.getCodeGenOpts().SanitizeTrap.has(SanitizerKind::CFIMFCall)) {
652 CGF.EmitTrapCheck(TypeTest);
653 } else {
654 llvm::Value *AllVtables = llvm::MetadataAsValue::get(
655 CGM.getLLVMContext(),
656 llvm::MDString::get(CGM.getLLVMContext(), "all-vtables"));
657 llvm::Value *ValidVtable = Builder.CreateCall(
658 CGM.getIntrinsic(llvm::Intrinsic::type_test), {VTable, AllVtables});
659 CGF.EmitCheck(std::make_pair(TypeTest, SanitizerKind::CFIMFCall),
660 SanitizerHandler::CFICheckFail, StaticData,
661 {VTable, ValidVtable});
662 }
663
664 FnVirtual = Builder.GetInsertBlock();
665 }
John McCall475999d2010-08-22 00:05:51 +0000666
667 // Load the virtual function to call.
Peter Collingbournee44acad2018-06-26 02:15:47 +0000668 VFPAddr = Builder.CreateBitCast(VFPAddr, FTy->getPointerTo()->getPointerTo());
669 llvm::Value *VirtualFn = Builder.CreateAlignedLoad(
670 VFPAddr, CGF.getPointerAlign(), "memptr.virtualfn");
John McCall475999d2010-08-22 00:05:51 +0000671 CGF.EmitBranch(FnEnd);
672
673 // In the non-virtual path, the function pointer is actually a
674 // function pointer.
675 CGF.EmitBlock(FnNonVirtual);
676 llvm::Value *NonVirtualFn =
John McCalld9c6c0b2010-08-22 00:59:17 +0000677 Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000678
Peter Collingbournee44acad2018-06-26 02:15:47 +0000679 // Check the function pointer if CFI on member function pointers is enabled.
680 if (ShouldEmitCFICheck) {
681 CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl();
682 if (RD->hasDefinition()) {
683 CodeGenFunction::SanitizerScope SanScope(&CGF);
684
685 llvm::Constant *StaticData[] = {
686 llvm::ConstantInt::get(CGF.Int8Ty, CodeGenFunction::CFITCK_NVMFCall),
687 CheckSourceLocation,
688 CheckTypeDesc,
689 };
690
691 llvm::Value *Bit = Builder.getFalse();
692 llvm::Value *CastedNonVirtualFn =
693 Builder.CreateBitCast(NonVirtualFn, CGF.Int8PtrTy);
694 for (const CXXRecordDecl *Base : CGM.getMostBaseClasses(RD)) {
695 llvm::Metadata *MD = CGM.CreateMetadataIdentifierForType(
696 getContext().getMemberPointerType(
697 MPT->getPointeeType(),
698 getContext().getRecordType(Base).getTypePtr()));
699 llvm::Value *TypeId =
700 llvm::MetadataAsValue::get(CGF.getLLVMContext(), MD);
701
702 llvm::Value *TypeTest =
703 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::type_test),
704 {CastedNonVirtualFn, TypeId});
705 Bit = Builder.CreateOr(Bit, TypeTest);
706 }
707
708 CGF.EmitCheck(std::make_pair(Bit, SanitizerKind::CFIMFCall),
709 SanitizerHandler::CFICheckFail, StaticData,
710 {CastedNonVirtualFn, llvm::UndefValue::get(CGF.IntPtrTy)});
711
712 FnNonVirtual = Builder.GetInsertBlock();
713 }
714 }
715
John McCall475999d2010-08-22 00:05:51 +0000716 // We're done.
717 CGF.EmitBlock(FnEnd);
John McCallb92ab1a2016-10-26 23:46:34 +0000718 llvm::PHINode *CalleePtr = Builder.CreatePHI(FTy->getPointerTo(), 2);
719 CalleePtr->addIncoming(VirtualFn, FnVirtual);
720 CalleePtr->addIncoming(NonVirtualFn, FnNonVirtual);
721
722 CGCallee Callee(FPT, CalleePtr);
John McCall475999d2010-08-22 00:05:51 +0000723 return Callee;
724}
John McCalla8bbb822010-08-22 03:04:22 +0000725
John McCallc134eb52010-08-31 21:07:20 +0000726/// Compute an l-value by applying the given pointer-to-member to a
727/// base object.
David Majnemer2b0d66d2014-02-20 23:22:07 +0000728llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(
John McCall7f416cc2015-09-08 08:05:57 +0000729 CodeGenFunction &CGF, const Expr *E, Address Base, llvm::Value *MemPtr,
David Majnemer2b0d66d2014-02-20 23:22:07 +0000730 const MemberPointerType *MPT) {
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000731 assert(MemPtr->getType() == CGM.PtrDiffTy);
John McCallc134eb52010-08-31 21:07:20 +0000732
733 CGBuilderTy &Builder = CGF.Builder;
734
John McCallc134eb52010-08-31 21:07:20 +0000735 // Cast to char*.
John McCall7f416cc2015-09-08 08:05:57 +0000736 Base = Builder.CreateElementBitCast(Base, CGF.Int8Ty);
John McCallc134eb52010-08-31 21:07:20 +0000737
738 // Apply the offset, which we assume is non-null.
John McCall7f416cc2015-09-08 08:05:57 +0000739 llvm::Value *Addr =
740 Builder.CreateInBoundsGEP(Base.getPointer(), MemPtr, "memptr.offset");
John McCallc134eb52010-08-31 21:07:20 +0000741
742 // Cast the address to the appropriate pointer type, adopting the
743 // address space of the base pointer.
John McCall7f416cc2015-09-08 08:05:57 +0000744 llvm::Type *PType = CGF.ConvertTypeForMem(MPT->getPointeeType())
745 ->getPointerTo(Base.getAddressSpace());
John McCallc134eb52010-08-31 21:07:20 +0000746 return Builder.CreateBitCast(Addr, PType);
747}
748
John McCallc62bb392012-02-15 01:22:51 +0000749/// Perform a bitcast, derived-to-base, or base-to-derived member pointer
750/// conversion.
751///
752/// Bitcast conversions are always a no-op under Itanium.
John McCall7a9aac22010-08-23 01:21:21 +0000753///
754/// Obligatory offset/adjustment diagram:
755/// <-- offset --> <-- adjustment -->
756/// |--------------------------|----------------------|--------------------|
757/// ^Derived address point ^Base address point ^Member address point
758///
759/// So when converting a base member pointer to a derived member pointer,
760/// we add the offset to the adjustment because the address point has
761/// decreased; and conversely, when converting a derived MP to a base MP
762/// we subtract the offset from the adjustment because the address point
763/// has increased.
764///
765/// The standard forbids (at compile time) conversion to and from
766/// virtual bases, which is why we don't have to consider them here.
767///
768/// The standard forbids (at run time) casting a derived MP to a base
769/// MP when the derived MP does not point to a member of the base.
770/// This is why -1 is a reasonable choice for null data member
771/// pointers.
John McCalla1dee5302010-08-22 10:59:02 +0000772llvm::Value *
John McCall7a9aac22010-08-23 01:21:21 +0000773ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
774 const CastExpr *E,
John McCallc62bb392012-02-15 01:22:51 +0000775 llvm::Value *src) {
John McCalle3027922010-08-25 11:45:40 +0000776 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
John McCallc62bb392012-02-15 01:22:51 +0000777 E->getCastKind() == CK_BaseToDerivedMemberPointer ||
778 E->getCastKind() == CK_ReinterpretMemberPointer);
779
780 // Under Itanium, reinterprets don't require any additional processing.
781 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
782
783 // Use constant emission if we can.
784 if (isa<llvm::Constant>(src))
785 return EmitMemberPointerConversion(E, cast<llvm::Constant>(src));
786
787 llvm::Constant *adj = getMemberPointerAdjustment(E);
788 if (!adj) return src;
John McCalla8bbb822010-08-22 03:04:22 +0000789
790 CGBuilderTy &Builder = CGF.Builder;
John McCallc62bb392012-02-15 01:22:51 +0000791 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
John McCalla8bbb822010-08-22 03:04:22 +0000792
John McCallc62bb392012-02-15 01:22:51 +0000793 const MemberPointerType *destTy =
794 E->getType()->castAs<MemberPointerType>();
John McCall1c456c82010-08-22 06:43:33 +0000795
John McCall7a9aac22010-08-23 01:21:21 +0000796 // For member data pointers, this is just a matter of adding the
797 // offset if the source is non-null.
John McCallc62bb392012-02-15 01:22:51 +0000798 if (destTy->isMemberDataPointer()) {
799 llvm::Value *dst;
800 if (isDerivedToBase)
801 dst = Builder.CreateNSWSub(src, adj, "adj");
John McCall7a9aac22010-08-23 01:21:21 +0000802 else
John McCallc62bb392012-02-15 01:22:51 +0000803 dst = Builder.CreateNSWAdd(src, adj, "adj");
John McCall7a9aac22010-08-23 01:21:21 +0000804
805 // Null check.
John McCallc62bb392012-02-15 01:22:51 +0000806 llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType());
807 llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull");
808 return Builder.CreateSelect(isNull, src, dst);
John McCall7a9aac22010-08-23 01:21:21 +0000809 }
810
John McCalla1dee5302010-08-22 10:59:02 +0000811 // The this-adjustment is left-shifted by 1 on ARM.
Mark Seabornedf0d382013-07-24 16:25:13 +0000812 if (UseARMMethodPtrABI) {
John McCallc62bb392012-02-15 01:22:51 +0000813 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
814 offset <<= 1;
815 adj = llvm::ConstantInt::get(adj->getType(), offset);
John McCalla1dee5302010-08-22 10:59:02 +0000816 }
817
John McCallc62bb392012-02-15 01:22:51 +0000818 llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj");
819 llvm::Value *dstAdj;
820 if (isDerivedToBase)
821 dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj");
John McCalla1dee5302010-08-22 10:59:02 +0000822 else
John McCallc62bb392012-02-15 01:22:51 +0000823 dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj");
John McCalla1dee5302010-08-22 10:59:02 +0000824
John McCallc62bb392012-02-15 01:22:51 +0000825 return Builder.CreateInsertValue(src, dstAdj, 1);
826}
827
828llvm::Constant *
829ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E,
830 llvm::Constant *src) {
831 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
832 E->getCastKind() == CK_BaseToDerivedMemberPointer ||
833 E->getCastKind() == CK_ReinterpretMemberPointer);
834
835 // Under Itanium, reinterprets don't require any additional processing.
836 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
837
838 // If the adjustment is trivial, we don't need to do anything.
839 llvm::Constant *adj = getMemberPointerAdjustment(E);
840 if (!adj) return src;
841
842 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
843
844 const MemberPointerType *destTy =
845 E->getType()->castAs<MemberPointerType>();
846
847 // For member data pointers, this is just a matter of adding the
848 // offset if the source is non-null.
849 if (destTy->isMemberDataPointer()) {
850 // null maps to null.
851 if (src->isAllOnesValue()) return src;
852
853 if (isDerivedToBase)
854 return llvm::ConstantExpr::getNSWSub(src, adj);
855 else
856 return llvm::ConstantExpr::getNSWAdd(src, adj);
857 }
858
859 // The this-adjustment is left-shifted by 1 on ARM.
Mark Seabornedf0d382013-07-24 16:25:13 +0000860 if (UseARMMethodPtrABI) {
John McCallc62bb392012-02-15 01:22:51 +0000861 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
862 offset <<= 1;
863 adj = llvm::ConstantInt::get(adj->getType(), offset);
864 }
865
866 llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1);
867 llvm::Constant *dstAdj;
868 if (isDerivedToBase)
869 dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj);
870 else
871 dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj);
872
873 return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1);
John McCalla8bbb822010-08-22 03:04:22 +0000874}
John McCall84fa5102010-08-22 04:16:24 +0000875
876llvm::Constant *
John McCall7a9aac22010-08-23 01:21:21 +0000877ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
John McCall7a9aac22010-08-23 01:21:21 +0000878 // Itanium C++ ABI 2.3:
879 // A NULL pointer is represented as -1.
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000880 if (MPT->isMemberDataPointer())
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000881 return llvm::ConstantInt::get(CGM.PtrDiffTy, -1ULL, /*isSigned=*/true);
John McCalla1dee5302010-08-22 10:59:02 +0000882
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000883 llvm::Constant *Zero = llvm::ConstantInt::get(CGM.PtrDiffTy, 0);
John McCalla1dee5302010-08-22 10:59:02 +0000884 llvm::Constant *Values[2] = { Zero, Zero };
Chris Lattnere64d7ba2011-06-20 04:01:35 +0000885 return llvm::ConstantStruct::getAnon(Values);
John McCall84fa5102010-08-22 04:16:24 +0000886}
887
John McCallf3a88602011-02-03 08:15:49 +0000888llvm::Constant *
889ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
890 CharUnits offset) {
John McCall7a9aac22010-08-23 01:21:21 +0000891 // Itanium C++ ABI 2.3:
892 // A pointer to data member is an offset from the base address of
893 // the class object containing it, represented as a ptrdiff_t
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000894 return llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity());
John McCall7a9aac22010-08-23 01:21:21 +0000895}
896
David Majnemere2be95b2015-06-23 07:31:01 +0000897llvm::Constant *
898ItaniumCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) {
Richard Smithdafff942012-01-14 04:30:29 +0000899 return BuildMemberPointer(MD, CharUnits::Zero());
900}
901
902llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD,
903 CharUnits ThisAdjustment) {
John McCalla1dee5302010-08-22 10:59:02 +0000904 assert(MD->isInstance() && "Member function must not be static!");
John McCalla1dee5302010-08-22 10:59:02 +0000905
906 CodeGenTypes &Types = CGM.getTypes();
John McCalla1dee5302010-08-22 10:59:02 +0000907
908 // Get the function pointer (or index if this is a virtual function).
909 llvm::Constant *MemPtr[2];
910 if (MD->isVirtual()) {
Timur Iskhodzhanov58776632013-11-05 15:54:58 +0000911 uint64_t Index = CGM.getItaniumVTableContext().getMethodVTableIndex(MD);
John McCalla1dee5302010-08-22 10:59:02 +0000912
Ken Dyckdf016282011-04-09 01:30:02 +0000913 const ASTContext &Context = getContext();
914 CharUnits PointerWidth =
Douglas Gregore8bbc122011-09-02 00:18:52 +0000915 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Ken Dyckdf016282011-04-09 01:30:02 +0000916 uint64_t VTableOffset = (Index * PointerWidth.getQuantity());
John McCalla1dee5302010-08-22 10:59:02 +0000917
Mark Seabornedf0d382013-07-24 16:25:13 +0000918 if (UseARMMethodPtrABI) {
John McCalla1dee5302010-08-22 10:59:02 +0000919 // ARM C++ ABI 3.2.1:
920 // This ABI specifies that adj contains twice the this
921 // adjustment, plus 1 if the member function is virtual. The
922 // least significant bit of adj then makes exactly the same
923 // discrimination as the least significant bit of ptr does for
924 // Itanium.
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000925 MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset);
926 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
Richard Smithdafff942012-01-14 04:30:29 +0000927 2 * ThisAdjustment.getQuantity() + 1);
John McCalla1dee5302010-08-22 10:59:02 +0000928 } else {
929 // Itanium C++ ABI 2.3:
930 // For a virtual function, [the pointer field] is 1 plus the
931 // virtual table offset (in bytes) of the function,
932 // represented as a ptrdiff_t.
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000933 MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset + 1);
934 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
Richard Smithdafff942012-01-14 04:30:29 +0000935 ThisAdjustment.getQuantity());
John McCalla1dee5302010-08-22 10:59:02 +0000936 }
937 } else {
John McCall2979fe02011-04-12 00:42:48 +0000938 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
Chris Lattner2192fe52011-07-18 04:24:23 +0000939 llvm::Type *Ty;
John McCall2979fe02011-04-12 00:42:48 +0000940 // Check whether the function has a computable LLVM signature.
Chris Lattner8806e322011-07-10 00:18:59 +0000941 if (Types.isFuncTypeConvertible(FPT)) {
John McCall2979fe02011-04-12 00:42:48 +0000942 // The function has a computable LLVM signature; use the correct type.
John McCalla729c622012-02-17 03:33:10 +0000943 Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
John McCalla1dee5302010-08-22 10:59:02 +0000944 } else {
John McCall2979fe02011-04-12 00:42:48 +0000945 // Use an arbitrary non-function type to tell GetAddrOfFunction that the
946 // function type is incomplete.
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000947 Ty = CGM.PtrDiffTy;
John McCalla1dee5302010-08-22 10:59:02 +0000948 }
John McCall2979fe02011-04-12 00:42:48 +0000949 llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty);
John McCalla1dee5302010-08-22 10:59:02 +0000950
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000951 MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, CGM.PtrDiffTy);
Mark Seabornedf0d382013-07-24 16:25:13 +0000952 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
953 (UseARMMethodPtrABI ? 2 : 1) *
Richard Smithdafff942012-01-14 04:30:29 +0000954 ThisAdjustment.getQuantity());
John McCalla1dee5302010-08-22 10:59:02 +0000955 }
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000956
Chris Lattnere64d7ba2011-06-20 04:01:35 +0000957 return llvm::ConstantStruct::getAnon(MemPtr);
John McCall1c456c82010-08-22 06:43:33 +0000958}
959
Richard Smithdafff942012-01-14 04:30:29 +0000960llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP,
961 QualType MPType) {
962 const MemberPointerType *MPT = MPType->castAs<MemberPointerType>();
963 const ValueDecl *MPD = MP.getMemberPointerDecl();
964 if (!MPD)
965 return EmitNullMemberPointer(MPT);
966
Reid Kleckner452abac2013-05-09 21:01:17 +0000967 CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP);
Richard Smithdafff942012-01-14 04:30:29 +0000968
969 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD))
970 return BuildMemberPointer(MD, ThisAdjustment);
971
972 CharUnits FieldOffset =
973 getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD));
974 return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset);
975}
976
John McCall131d97d2010-08-22 08:30:07 +0000977/// The comparison algorithm is pretty easy: the member pointers are
978/// the same if they're either bitwise identical *or* both null.
979///
980/// ARM is different here only because null-ness is more complicated.
981llvm::Value *
John McCall7a9aac22010-08-23 01:21:21 +0000982ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
983 llvm::Value *L,
984 llvm::Value *R,
985 const MemberPointerType *MPT,
986 bool Inequality) {
John McCall131d97d2010-08-22 08:30:07 +0000987 CGBuilderTy &Builder = CGF.Builder;
988
John McCall131d97d2010-08-22 08:30:07 +0000989 llvm::ICmpInst::Predicate Eq;
990 llvm::Instruction::BinaryOps And, Or;
991 if (Inequality) {
992 Eq = llvm::ICmpInst::ICMP_NE;
993 And = llvm::Instruction::Or;
994 Or = llvm::Instruction::And;
995 } else {
996 Eq = llvm::ICmpInst::ICMP_EQ;
997 And = llvm::Instruction::And;
998 Or = llvm::Instruction::Or;
999 }
1000
John McCall7a9aac22010-08-23 01:21:21 +00001001 // Member data pointers are easy because there's a unique null
1002 // value, so it just comes down to bitwise equality.
1003 if (MPT->isMemberDataPointer())
1004 return Builder.CreateICmp(Eq, L, R);
1005
1006 // For member function pointers, the tautologies are more complex.
1007 // The Itanium tautology is:
John McCall61a14882010-08-23 06:56:36 +00001008 // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
John McCall7a9aac22010-08-23 01:21:21 +00001009 // The ARM tautology is:
John McCall61a14882010-08-23 06:56:36 +00001010 // (L == R) <==> (L.ptr == R.ptr &&
1011 // (L.adj == R.adj ||
1012 // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
John McCall7a9aac22010-08-23 01:21:21 +00001013 // The inequality tautologies have exactly the same structure, except
1014 // applying De Morgan's laws.
Jake Ehrlichc451cf22017-11-11 01:15:41 +00001015
John McCall7a9aac22010-08-23 01:21:21 +00001016 llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
1017 llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
1018
John McCall131d97d2010-08-22 08:30:07 +00001019 // This condition tests whether L.ptr == R.ptr. This must always be
1020 // true for equality to hold.
1021 llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
1022
1023 // This condition, together with the assumption that L.ptr == R.ptr,
1024 // tests whether the pointers are both null. ARM imposes an extra
1025 // condition.
1026 llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
1027 llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
1028
1029 // This condition tests whether L.adj == R.adj. If this isn't
1030 // true, the pointers are unequal unless they're both null.
John McCalla1dee5302010-08-22 10:59:02 +00001031 llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
1032 llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
John McCall131d97d2010-08-22 08:30:07 +00001033 llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
1034
1035 // Null member function pointers on ARM clear the low bit of Adj,
1036 // so the zero condition has to check that neither low bit is set.
Mark Seabornedf0d382013-07-24 16:25:13 +00001037 if (UseARMMethodPtrABI) {
John McCall131d97d2010-08-22 08:30:07 +00001038 llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
1039
1040 // Compute (l.adj | r.adj) & 1 and test it against zero.
1041 llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
1042 llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
1043 llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
1044 "cmp.or.adj");
1045 EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
1046 }
1047
1048 // Tie together all our conditions.
1049 llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
1050 Result = Builder.CreateBinOp(And, PtrEq, Result,
1051 Inequality ? "memptr.ne" : "memptr.eq");
1052 return Result;
1053}
1054
1055llvm::Value *
John McCall7a9aac22010-08-23 01:21:21 +00001056ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
1057 llvm::Value *MemPtr,
1058 const MemberPointerType *MPT) {
John McCall131d97d2010-08-22 08:30:07 +00001059 CGBuilderTy &Builder = CGF.Builder;
John McCall7a9aac22010-08-23 01:21:21 +00001060
1061 /// For member data pointers, this is just a check against -1.
1062 if (MPT->isMemberDataPointer()) {
Reid Kleckner9cffbc12013-03-22 16:13:10 +00001063 assert(MemPtr->getType() == CGM.PtrDiffTy);
John McCall7a9aac22010-08-23 01:21:21 +00001064 llvm::Value *NegativeOne =
1065 llvm::Constant::getAllOnesValue(MemPtr->getType());
1066 return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
1067 }
Jake Ehrlichc451cf22017-11-11 01:15:41 +00001068
Daniel Dunbar914bc412011-04-19 23:10:47 +00001069 // In Itanium, a member function pointer is not null if 'ptr' is not null.
John McCalla1dee5302010-08-22 10:59:02 +00001070 llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
John McCall131d97d2010-08-22 08:30:07 +00001071
1072 llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
1073 llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
1074
Daniel Dunbar914bc412011-04-19 23:10:47 +00001075 // On ARM, a member function pointer is also non-null if the low bit of 'adj'
1076 // (the virtual bit) is set.
Mark Seabornedf0d382013-07-24 16:25:13 +00001077 if (UseARMMethodPtrABI) {
John McCall131d97d2010-08-22 08:30:07 +00001078 llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
John McCalla1dee5302010-08-22 10:59:02 +00001079 llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
John McCall131d97d2010-08-22 08:30:07 +00001080 llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
Daniel Dunbar914bc412011-04-19 23:10:47 +00001081 llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero,
1082 "memptr.isvirtual");
1083 Result = Builder.CreateOr(Result, IsVirtual);
John McCall131d97d2010-08-22 08:30:07 +00001084 }
1085
1086 return Result;
1087}
John McCall1c456c82010-08-22 06:43:33 +00001088
Reid Kleckner40ca9132014-05-13 22:05:45 +00001089bool ItaniumCXXABI::classifyReturnType(CGFunctionInfo &FI) const {
1090 const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl();
1091 if (!RD)
1092 return false;
1093
Richard Smith96cd6712017-08-16 01:49:53 +00001094 // If C++ prohibits us from making a copy, return by address.
Richard Smithf667ad52017-08-26 01:04:35 +00001095 if (passClassIndirect(RD)) {
John McCall7f416cc2015-09-08 08:05:57 +00001096 auto Align = CGM.getContext().getTypeAlignInChars(FI.getReturnType());
1097 FI.getReturnInfo() = ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
Reid Kleckner40ca9132014-05-13 22:05:45 +00001098 return true;
1099 }
Reid Kleckner40ca9132014-05-13 22:05:45 +00001100 return false;
1101}
1102
John McCall614dbdc2010-08-22 21:01:12 +00001103/// The Itanium ABI requires non-zero initialization only for data
1104/// member pointers, for which '0' is a valid offset.
1105bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
David Majnemer5fd33e02015-04-24 01:25:08 +00001106 return MPT->isMemberFunctionPointer();
John McCall84fa5102010-08-22 04:16:24 +00001107}
John McCall5d865c322010-08-31 07:33:07 +00001108
John McCall82fb8922012-09-25 10:10:39 +00001109/// The Itanium ABI always places an offset to the complete object
1110/// at entry -2 in the vtable.
David Majnemer08681372014-11-01 07:37:17 +00001111void ItaniumCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF,
1112 const CXXDeleteExpr *DE,
John McCall7f416cc2015-09-08 08:05:57 +00001113 Address Ptr,
David Majnemer08681372014-11-01 07:37:17 +00001114 QualType ElementType,
1115 const CXXDestructorDecl *Dtor) {
1116 bool UseGlobalDelete = DE->isGlobalDelete();
David Majnemer0c0b6d92014-10-31 20:09:12 +00001117 if (UseGlobalDelete) {
1118 // Derive the complete-object pointer, which is what we need
1119 // to pass to the deallocation function.
John McCall82fb8922012-09-25 10:10:39 +00001120
David Majnemer0c0b6d92014-10-31 20:09:12 +00001121 // Grab the vtable pointer as an intptr_t*.
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001122 auto *ClassDecl =
1123 cast<CXXRecordDecl>(ElementType->getAs<RecordType>()->getDecl());
1124 llvm::Value *VTable =
1125 CGF.GetVTablePtr(Ptr, CGF.IntPtrTy->getPointerTo(), ClassDecl);
John McCall82fb8922012-09-25 10:10:39 +00001126
David Majnemer0c0b6d92014-10-31 20:09:12 +00001127 // Track back to entry -2 and pull out the offset there.
1128 llvm::Value *OffsetPtr = CGF.Builder.CreateConstInBoundsGEP1_64(
1129 VTable, -2, "complete-offset.ptr");
John McCall7f416cc2015-09-08 08:05:57 +00001130 llvm::Value *Offset =
1131 CGF.Builder.CreateAlignedLoad(OffsetPtr, CGF.getPointerAlign());
David Majnemer0c0b6d92014-10-31 20:09:12 +00001132
1133 // Apply the offset.
John McCall7f416cc2015-09-08 08:05:57 +00001134 llvm::Value *CompletePtr =
1135 CGF.Builder.CreateBitCast(Ptr.getPointer(), CGF.Int8PtrTy);
David Majnemer0c0b6d92014-10-31 20:09:12 +00001136 CompletePtr = CGF.Builder.CreateInBoundsGEP(CompletePtr, Offset);
1137
1138 // If we're supposed to call the global delete, make sure we do so
1139 // even if the destructor throws.
David Majnemer08681372014-11-01 07:37:17 +00001140 CGF.pushCallObjectDeleteCleanup(DE->getOperatorDelete(), CompletePtr,
1141 ElementType);
David Majnemer0c0b6d92014-10-31 20:09:12 +00001142 }
1143
1144 // FIXME: Provide a source location here even though there's no
1145 // CXXMemberCallExpr for dtor call.
1146 CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting;
1147 EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, /*CE=*/nullptr);
1148
1149 if (UseGlobalDelete)
1150 CGF.PopCleanupBlock();
John McCall82fb8922012-09-25 10:10:39 +00001151}
1152
David Majnemer442d0a22014-11-25 07:20:20 +00001153void ItaniumCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) {
1154 // void __cxa_rethrow();
1155
1156 llvm::FunctionType *FTy =
1157 llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false);
1158
1159 llvm::Constant *Fn = CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow");
1160
1161 if (isNoReturn)
1162 CGF.EmitNoreturnRuntimeCallOrInvoke(Fn, None);
1163 else
1164 CGF.EmitRuntimeCallOrInvoke(Fn);
1165}
1166
David Majnemer7c237072015-03-05 00:46:22 +00001167static llvm::Constant *getAllocateExceptionFn(CodeGenModule &CGM) {
1168 // void *__cxa_allocate_exception(size_t thrown_size);
1169
1170 llvm::FunctionType *FTy =
1171 llvm::FunctionType::get(CGM.Int8PtrTy, CGM.SizeTy, /*IsVarArgs=*/false);
1172
1173 return CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception");
1174}
1175
1176static llvm::Constant *getThrowFn(CodeGenModule &CGM) {
1177 // void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
1178 // void (*dest) (void *));
1179
1180 llvm::Type *Args[3] = { CGM.Int8PtrTy, CGM.Int8PtrTy, CGM.Int8PtrTy };
1181 llvm::FunctionType *FTy =
1182 llvm::FunctionType::get(CGM.VoidTy, Args, /*IsVarArgs=*/false);
1183
1184 return CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
1185}
1186
1187void ItaniumCXXABI::emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) {
1188 QualType ThrowType = E->getSubExpr()->getType();
1189 // Now allocate the exception object.
1190 llvm::Type *SizeTy = CGF.ConvertType(getContext().getSizeType());
1191 uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity();
1192
1193 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(CGM);
1194 llvm::CallInst *ExceptionPtr = CGF.EmitNounwindRuntimeCall(
1195 AllocExceptionFn, llvm::ConstantInt::get(SizeTy, TypeSize), "exception");
1196
John McCall7f416cc2015-09-08 08:05:57 +00001197 CharUnits ExnAlign = getAlignmentOfExnObject();
1198 CGF.EmitAnyExprToExn(E->getSubExpr(), Address(ExceptionPtr, ExnAlign));
David Majnemer7c237072015-03-05 00:46:22 +00001199
1200 // Now throw the exception.
1201 llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType,
1202 /*ForEH=*/true);
1203
1204 // The address of the destructor. If the exception type has a
1205 // trivial destructor (or isn't a record), we just pass null.
1206 llvm::Constant *Dtor = nullptr;
1207 if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) {
1208 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
1209 if (!Record->hasTrivialDestructor()) {
1210 CXXDestructorDecl *DtorD = Record->getDestructor();
1211 Dtor = CGM.getAddrOfCXXStructor(DtorD, StructorType::Complete);
1212 Dtor = llvm::ConstantExpr::getBitCast(Dtor, CGM.Int8PtrTy);
1213 }
1214 }
1215 if (!Dtor) Dtor = llvm::Constant::getNullValue(CGM.Int8PtrTy);
1216
1217 llvm::Value *args[] = { ExceptionPtr, TypeInfo, Dtor };
1218 CGF.EmitNoreturnRuntimeCallOrInvoke(getThrowFn(CGM), args);
1219}
1220
David Majnemer1162d252014-06-22 19:05:33 +00001221static llvm::Constant *getItaniumDynamicCastFn(CodeGenFunction &CGF) {
1222 // void *__dynamic_cast(const void *sub,
1223 // const abi::__class_type_info *src,
1224 // const abi::__class_type_info *dst,
1225 // std::ptrdiff_t src2dst_offset);
Jake Ehrlichc451cf22017-11-11 01:15:41 +00001226
David Majnemer1162d252014-06-22 19:05:33 +00001227 llvm::Type *Int8PtrTy = CGF.Int8PtrTy;
Jake Ehrlichc451cf22017-11-11 01:15:41 +00001228 llvm::Type *PtrDiffTy =
David Majnemer1162d252014-06-22 19:05:33 +00001229 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1230
1231 llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy };
1232
1233 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
1234
1235 // Mark the function as nounwind readonly.
1236 llvm::Attribute::AttrKind FuncAttrs[] = { llvm::Attribute::NoUnwind,
1237 llvm::Attribute::ReadOnly };
Reid Klecknerde864822017-03-21 16:57:30 +00001238 llvm::AttributeList Attrs = llvm::AttributeList::get(
1239 CGF.getLLVMContext(), llvm::AttributeList::FunctionIndex, FuncAttrs);
David Majnemer1162d252014-06-22 19:05:33 +00001240
1241 return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast", Attrs);
1242}
1243
1244static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) {
1245 // void __cxa_bad_cast();
1246 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
1247 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast");
1248}
1249
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001250/// Compute the src2dst_offset hint as described in the
David Majnemer1162d252014-06-22 19:05:33 +00001251/// Itanium C++ ABI [2.9.7]
1252static CharUnits computeOffsetHint(ASTContext &Context,
1253 const CXXRecordDecl *Src,
1254 const CXXRecordDecl *Dst) {
1255 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
1256 /*DetectVirtual=*/false);
1257
1258 // If Dst is not derived from Src we can skip the whole computation below and
1259 // return that Src is not a public base of Dst. Record all inheritance paths.
1260 if (!Dst->isDerivedFrom(Src, Paths))
1261 return CharUnits::fromQuantity(-2ULL);
1262
1263 unsigned NumPublicPaths = 0;
1264 CharUnits Offset;
1265
1266 // Now walk all possible inheritance paths.
Piotr Padlewski44b4ce82015-07-28 16:10:58 +00001267 for (const CXXBasePath &Path : Paths) {
1268 if (Path.Access != AS_public) // Ignore non-public inheritance.
David Majnemer1162d252014-06-22 19:05:33 +00001269 continue;
1270
1271 ++NumPublicPaths;
1272
Piotr Padlewski44b4ce82015-07-28 16:10:58 +00001273 for (const CXXBasePathElement &PathElement : Path) {
David Majnemer1162d252014-06-22 19:05:33 +00001274 // If the path contains a virtual base class we can't give any hint.
1275 // -1: no hint.
Piotr Padlewski44b4ce82015-07-28 16:10:58 +00001276 if (PathElement.Base->isVirtual())
David Majnemer1162d252014-06-22 19:05:33 +00001277 return CharUnits::fromQuantity(-1ULL);
1278
1279 if (NumPublicPaths > 1) // Won't use offsets, skip computation.
1280 continue;
1281
1282 // Accumulate the base class offsets.
Piotr Padlewski44b4ce82015-07-28 16:10:58 +00001283 const ASTRecordLayout &L = Context.getASTRecordLayout(PathElement.Class);
1284 Offset += L.getBaseClassOffset(
1285 PathElement.Base->getType()->getAsCXXRecordDecl());
David Majnemer1162d252014-06-22 19:05:33 +00001286 }
1287 }
1288
1289 // -2: Src is not a public base of Dst.
1290 if (NumPublicPaths == 0)
1291 return CharUnits::fromQuantity(-2ULL);
1292
1293 // -3: Src is a multiple public base type but never a virtual base type.
1294 if (NumPublicPaths > 1)
1295 return CharUnits::fromQuantity(-3ULL);
1296
1297 // Otherwise, the Src type is a unique public nonvirtual base type of Dst.
1298 // Return the offset of Src from the origin of Dst.
1299 return Offset;
1300}
1301
1302static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) {
1303 // void __cxa_bad_typeid();
1304 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
1305
1306 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid");
1307}
1308
1309bool ItaniumCXXABI::shouldTypeidBeNullChecked(bool IsDeref,
1310 QualType SrcRecordTy) {
1311 return IsDeref;
1312}
1313
1314void ItaniumCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) {
1315 llvm::Value *Fn = getBadTypeidFn(CGF);
James Y Knight3933add2019-01-30 02:54:28 +00001316 llvm::CallBase *Call = CGF.EmitRuntimeCallOrInvoke(Fn);
1317 Call->setDoesNotReturn();
David Majnemer1162d252014-06-22 19:05:33 +00001318 CGF.Builder.CreateUnreachable();
1319}
1320
1321llvm::Value *ItaniumCXXABI::EmitTypeid(CodeGenFunction &CGF,
1322 QualType SrcRecordTy,
John McCall7f416cc2015-09-08 08:05:57 +00001323 Address ThisPtr,
David Majnemer1162d252014-06-22 19:05:33 +00001324 llvm::Type *StdTypeInfoPtrTy) {
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001325 auto *ClassDecl =
1326 cast<CXXRecordDecl>(SrcRecordTy->getAs<RecordType>()->getDecl());
David Majnemer1162d252014-06-22 19:05:33 +00001327 llvm::Value *Value =
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001328 CGF.GetVTablePtr(ThisPtr, StdTypeInfoPtrTy->getPointerTo(), ClassDecl);
David Majnemer1162d252014-06-22 19:05:33 +00001329
1330 // Load the type info.
1331 Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL);
John McCall7f416cc2015-09-08 08:05:57 +00001332 return CGF.Builder.CreateAlignedLoad(Value, CGF.getPointerAlign());
David Majnemer1162d252014-06-22 19:05:33 +00001333}
1334
1335bool ItaniumCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
1336 QualType SrcRecordTy) {
1337 return SrcIsPtr;
1338}
1339
1340llvm::Value *ItaniumCXXABI::EmitDynamicCastCall(
John McCall7f416cc2015-09-08 08:05:57 +00001341 CodeGenFunction &CGF, Address ThisAddr, QualType SrcRecordTy,
David Majnemer1162d252014-06-22 19:05:33 +00001342 QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) {
1343 llvm::Type *PtrDiffLTy =
1344 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1345 llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1346
1347 llvm::Value *SrcRTTI =
1348 CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType());
1349 llvm::Value *DestRTTI =
1350 CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType());
1351
1352 // Compute the offset hint.
1353 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
1354 const CXXRecordDecl *DestDecl = DestRecordTy->getAsCXXRecordDecl();
1355 llvm::Value *OffsetHint = llvm::ConstantInt::get(
1356 PtrDiffLTy,
1357 computeOffsetHint(CGF.getContext(), SrcDecl, DestDecl).getQuantity());
1358
1359 // Emit the call to __dynamic_cast.
John McCall7f416cc2015-09-08 08:05:57 +00001360 llvm::Value *Value = ThisAddr.getPointer();
David Majnemer1162d252014-06-22 19:05:33 +00001361 Value = CGF.EmitCastToVoidPtr(Value);
1362
1363 llvm::Value *args[] = {Value, SrcRTTI, DestRTTI, OffsetHint};
1364 Value = CGF.EmitNounwindRuntimeCall(getItaniumDynamicCastFn(CGF), args);
1365 Value = CGF.Builder.CreateBitCast(Value, DestLTy);
1366
1367 /// C++ [expr.dynamic.cast]p9:
1368 /// A failed cast to reference type throws std::bad_cast
1369 if (DestTy->isReferenceType()) {
1370 llvm::BasicBlock *BadCastBlock =
1371 CGF.createBasicBlock("dynamic_cast.bad_cast");
1372
1373 llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value);
1374 CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd);
1375
1376 CGF.EmitBlock(BadCastBlock);
1377 EmitBadCastCall(CGF);
1378 }
1379
1380 return Value;
1381}
1382
1383llvm::Value *ItaniumCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00001384 Address ThisAddr,
David Majnemer1162d252014-06-22 19:05:33 +00001385 QualType SrcRecordTy,
1386 QualType DestTy) {
1387 llvm::Type *PtrDiffLTy =
1388 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1389 llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1390
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001391 auto *ClassDecl =
1392 cast<CXXRecordDecl>(SrcRecordTy->getAs<RecordType>()->getDecl());
David Majnemer1162d252014-06-22 19:05:33 +00001393 // Get the vtable pointer.
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001394 llvm::Value *VTable = CGF.GetVTablePtr(ThisAddr, PtrDiffLTy->getPointerTo(),
1395 ClassDecl);
David Majnemer1162d252014-06-22 19:05:33 +00001396
1397 // Get the offset-to-top from the vtable.
1398 llvm::Value *OffsetToTop =
1399 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL);
John McCall7f416cc2015-09-08 08:05:57 +00001400 OffsetToTop =
1401 CGF.Builder.CreateAlignedLoad(OffsetToTop, CGF.getPointerAlign(),
1402 "offset.to.top");
David Majnemer1162d252014-06-22 19:05:33 +00001403
1404 // Finally, add the offset to the pointer.
John McCall7f416cc2015-09-08 08:05:57 +00001405 llvm::Value *Value = ThisAddr.getPointer();
David Majnemer1162d252014-06-22 19:05:33 +00001406 Value = CGF.EmitCastToVoidPtr(Value);
1407 Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop);
1408
1409 return CGF.Builder.CreateBitCast(Value, DestLTy);
1410}
1411
1412bool ItaniumCXXABI::EmitBadCastCall(CodeGenFunction &CGF) {
1413 llvm::Value *Fn = getBadCastFn(CGF);
James Y Knight3933add2019-01-30 02:54:28 +00001414 llvm::CallBase *Call = CGF.EmitRuntimeCallOrInvoke(Fn);
1415 Call->setDoesNotReturn();
David Majnemer1162d252014-06-22 19:05:33 +00001416 CGF.Builder.CreateUnreachable();
1417 return true;
1418}
1419
Reid Klecknerd8cbeec2013-05-29 18:02:47 +00001420llvm::Value *
1421ItaniumCXXABI::GetVirtualBaseClassOffset(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00001422 Address This,
Reid Klecknerd8cbeec2013-05-29 18:02:47 +00001423 const CXXRecordDecl *ClassDecl,
1424 const CXXRecordDecl *BaseClassDecl) {
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001425 llvm::Value *VTablePtr = CGF.GetVTablePtr(This, CGM.Int8PtrTy, ClassDecl);
Reid Klecknerd8cbeec2013-05-29 18:02:47 +00001426 CharUnits VBaseOffsetOffset =
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001427 CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(ClassDecl,
1428 BaseClassDecl);
Reid Klecknerd8cbeec2013-05-29 18:02:47 +00001429
1430 llvm::Value *VBaseOffsetPtr =
1431 CGF.Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(),
1432 "vbase.offset.ptr");
1433 VBaseOffsetPtr = CGF.Builder.CreateBitCast(VBaseOffsetPtr,
1434 CGM.PtrDiffTy->getPointerTo());
1435
1436 llvm::Value *VBaseOffset =
John McCall7f416cc2015-09-08 08:05:57 +00001437 CGF.Builder.CreateAlignedLoad(VBaseOffsetPtr, CGF.getPointerAlign(),
1438 "vbase.offset");
Reid Klecknerd8cbeec2013-05-29 18:02:47 +00001439
1440 return VBaseOffset;
1441}
1442
Timur Iskhodzhanov40f2fa92013-08-04 17:30:04 +00001443void ItaniumCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) {
1444 // Just make sure we're in sync with TargetCXXABI.
1445 assert(CGM.getTarget().getCXXABI().hasConstructorVariants());
1446
Rafael Espindolac3cde362013-12-09 14:51:17 +00001447 // The constructor used for constructing this as a base class;
1448 // ignores virtual bases.
1449 CGM.EmitGlobal(GlobalDecl(D, Ctor_Base));
1450
Timur Iskhodzhanov40f2fa92013-08-04 17:30:04 +00001451 // The constructor used for constructing this as a complete class;
Nico Weber4c2ffb22015-01-07 05:25:05 +00001452 // constructs the virtual bases, then calls the base constructor.
Timur Iskhodzhanov40f2fa92013-08-04 17:30:04 +00001453 if (!D->getParent()->isAbstract()) {
1454 // We don't need to emit the complete ctor if the class is abstract.
1455 CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete));
1456 }
Timur Iskhodzhanov40f2fa92013-08-04 17:30:04 +00001457}
1458
George Burgess IVf203dbf2017-02-22 20:28:02 +00001459CGCXXABI::AddedStructorArgs
Rafael Espindola8d2a19b2014-09-08 16:01:27 +00001460ItaniumCXXABI::buildStructorSignature(const CXXMethodDecl *MD, StructorType T,
1461 SmallVectorImpl<CanQualType> &ArgTys) {
John McCall9bca9232010-09-02 10:25:57 +00001462 ASTContext &Context = getContext();
John McCall5d865c322010-08-31 07:33:07 +00001463
Rafael Espindola8d2a19b2014-09-08 16:01:27 +00001464 // All parameters are already in place except VTT, which goes after 'this'.
1465 // These are Clang types, so we don't need to worry about sret yet.
John McCall5d865c322010-08-31 07:33:07 +00001466
1467 // Check if we need to add a VTT parameter (which has type void **).
George Burgess IVf203dbf2017-02-22 20:28:02 +00001468 if (T == StructorType::Base && MD->getParent()->getNumVBases() != 0) {
Rafael Espindola8d2a19b2014-09-08 16:01:27 +00001469 ArgTys.insert(ArgTys.begin() + 1,
1470 Context.getPointerType(Context.VoidPtrTy));
George Burgess IVf203dbf2017-02-22 20:28:02 +00001471 return AddedStructorArgs::prefix(1);
1472 }
1473 return AddedStructorArgs{};
John McCall5d865c322010-08-31 07:33:07 +00001474}
1475
Reid Klecknere7de47e2013-07-22 13:51:44 +00001476void ItaniumCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) {
Rafael Espindolac3cde362013-12-09 14:51:17 +00001477 // The destructor used for destructing this as a base class; ignores
1478 // virtual bases.
1479 CGM.EmitGlobal(GlobalDecl(D, Dtor_Base));
Reid Klecknere7de47e2013-07-22 13:51:44 +00001480
1481 // The destructor used for destructing this as a most-derived class;
1482 // call the base destructor and then destructs any virtual bases.
1483 CGM.EmitGlobal(GlobalDecl(D, Dtor_Complete));
1484
Rafael Espindolac3cde362013-12-09 14:51:17 +00001485 // The destructor in a virtual table is always a 'deleting'
1486 // destructor, which calls the complete destructor and then uses the
1487 // appropriate operator delete.
1488 if (D->isVirtual())
1489 CGM.EmitGlobal(GlobalDecl(D, Dtor_Deleting));
Reid Klecknere7de47e2013-07-22 13:51:44 +00001490}
1491
Reid Kleckner89077a12013-12-17 19:46:40 +00001492void ItaniumCXXABI::addImplicitStructorParams(CodeGenFunction &CGF,
1493 QualType &ResTy,
1494 FunctionArgList &Params) {
John McCall5d865c322010-08-31 07:33:07 +00001495 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
Reid Kleckner89077a12013-12-17 19:46:40 +00001496 assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD));
John McCall5d865c322010-08-31 07:33:07 +00001497
1498 // Check if we need a VTT parameter as well.
Peter Collingbourne66f82e62013-06-28 20:45:28 +00001499 if (NeedsVTTParameter(CGF.CurGD)) {
John McCall9bca9232010-09-02 10:25:57 +00001500 ASTContext &Context = getContext();
John McCall5d865c322010-08-31 07:33:07 +00001501
1502 // FIXME: avoid the fake decl
1503 QualType T = Context.getPointerType(Context.VoidPtrTy);
Alexey Bataev56223232017-06-09 13:40:18 +00001504 auto *VTTDecl = ImplicitParamDecl::Create(
1505 Context, /*DC=*/nullptr, MD->getLocation(), &Context.Idents.get("vtt"),
1506 T, ImplicitParamDecl::CXXVTT);
Reid Kleckner89077a12013-12-17 19:46:40 +00001507 Params.insert(Params.begin() + 1, VTTDecl);
Reid Kleckner2af6d732013-12-13 00:09:59 +00001508 getStructorImplicitParamDecl(CGF) = VTTDecl;
John McCall5d865c322010-08-31 07:33:07 +00001509 }
1510}
1511
John McCall5d865c322010-08-31 07:33:07 +00001512void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
Justin Lebared4f1722016-07-27 22:04:24 +00001513 // Naked functions have no prolog.
1514 if (CGF.CurFuncDecl && CGF.CurFuncDecl->hasAttr<NakedAttr>())
1515 return;
1516
Reid Kleckner06239e42017-11-16 19:09:36 +00001517 /// Initialize the 'this' slot. In the Itanium C++ ABI, no prologue
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001518 /// adjustments are required, because they are all handled by thunks.
Reid Kleckner06239e42017-11-16 19:09:36 +00001519 setCXXABIThisValue(CGF, loadIncomingCXXThis(CGF));
John McCall5d865c322010-08-31 07:33:07 +00001520
1521 /// Initialize the 'vtt' slot if needed.
Reid Kleckner2af6d732013-12-13 00:09:59 +00001522 if (getStructorImplicitParamDecl(CGF)) {
1523 getStructorImplicitParamValue(CGF) = CGF.Builder.CreateLoad(
1524 CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), "vtt");
John McCall5d865c322010-08-31 07:33:07 +00001525 }
John McCall5d865c322010-08-31 07:33:07 +00001526
Stephen Lin9dc6eef2013-06-30 20:40:16 +00001527 /// If this is a function that the ABI specifies returns 'this', initialize
1528 /// the return slot to 'this' at the start of the function.
1529 ///
1530 /// Unlike the setting of return types, this is done within the ABI
1531 /// implementation instead of by clients of CGCXXABI because:
1532 /// 1) getThisValue is currently protected
1533 /// 2) in theory, an ABI could implement 'this' returns some other way;
1534 /// HasThisReturn only specifies a contract, not the implementation
John McCall5d865c322010-08-31 07:33:07 +00001535 if (HasThisReturn(CGF.CurGD))
Eli Friedman9fbeba02012-02-11 02:57:39 +00001536 CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
John McCall5d865c322010-08-31 07:33:07 +00001537}
1538
George Burgess IVf203dbf2017-02-22 20:28:02 +00001539CGCXXABI::AddedStructorArgs ItaniumCXXABI::addImplicitConstructorArgs(
Reid Kleckner89077a12013-12-17 19:46:40 +00001540 CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type,
1541 bool ForVirtualBase, bool Delegating, CallArgList &Args) {
1542 if (!NeedsVTTParameter(GlobalDecl(D, Type)))
George Burgess IVf203dbf2017-02-22 20:28:02 +00001543 return AddedStructorArgs{};
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001544
Reid Kleckner89077a12013-12-17 19:46:40 +00001545 // Insert the implicit 'vtt' argument as the second argument.
1546 llvm::Value *VTT =
1547 CGF.GetVTTParameter(GlobalDecl(D, Type), ForVirtualBase, Delegating);
1548 QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy);
Yaxun Liu5b330e82018-03-15 15:25:19 +00001549 Args.insert(Args.begin() + 1, CallArg(RValue::get(VTT), VTTTy));
George Burgess IVf203dbf2017-02-22 20:28:02 +00001550 return AddedStructorArgs::prefix(1); // Added one arg.
Reid Kleckner6fe771a2013-12-13 00:53:54 +00001551}
1552
1553void ItaniumCXXABI::EmitDestructorCall(CodeGenFunction &CGF,
1554 const CXXDestructorDecl *DD,
1555 CXXDtorType Type, bool ForVirtualBase,
John McCall7f416cc2015-09-08 08:05:57 +00001556 bool Delegating, Address This) {
Reid Kleckner6fe771a2013-12-13 00:53:54 +00001557 GlobalDecl GD(DD, Type);
1558 llvm::Value *VTT = CGF.GetVTTParameter(GD, ForVirtualBase, Delegating);
1559 QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy);
1560
John McCallb92ab1a2016-10-26 23:46:34 +00001561 CGCallee Callee;
1562 if (getContext().getLangOpts().AppleKext &&
1563 Type != Dtor_Base && DD->isVirtual())
Reid Kleckner6fe771a2013-12-13 00:53:54 +00001564 Callee = CGF.BuildAppleKextVirtualDestructorCall(DD, Type, DD->getParent());
John McCallb92ab1a2016-10-26 23:46:34 +00001565 else
Erich Keanede6480a32018-11-13 15:48:08 +00001566 Callee = CGCallee::forDirect(
1567 CGM.getAddrOfCXXStructor(DD, getFromDtorType(Type)), GD);
Reid Kleckner6fe771a2013-12-13 00:53:54 +00001568
John McCall7f416cc2015-09-08 08:05:57 +00001569 CGF.EmitCXXMemberOrOperatorCall(DD, Callee, ReturnValueSlot(),
Richard Smith762672a2016-09-28 19:09:10 +00001570 This.getPointer(), VTT, VTTTy,
1571 nullptr, nullptr);
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001572}
1573
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001574void ItaniumCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT,
1575 const CXXRecordDecl *RD) {
1576 llvm::GlobalVariable *VTable = getAddrOfVTable(RD, CharUnits());
1577 if (VTable->hasInitializer())
1578 return;
1579
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001580 ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext();
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001581 const VTableLayout &VTLayout = VTContext.getVTableLayout(RD);
1582 llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD);
David Majnemerd905da42014-07-01 20:30:31 +00001583 llvm::Constant *RTTI =
1584 CGM.GetAddrOfRTTIDescriptor(CGM.getContext().getTagDeclType(RD));
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001585
1586 // Create and set the initializer.
John McCall9c6cb762016-11-28 22:18:33 +00001587 ConstantInitBuilder Builder(CGM);
Peter Collingbourne2849c4e2016-12-13 20:40:39 +00001588 auto Components = Builder.beginStruct();
John McCall9c6cb762016-11-28 22:18:33 +00001589 CGVT.createVTableInitializer(Components, VTLayout, RTTI);
1590 Components.finishAndSetAsInitializer(VTable);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001591
1592 // Set the correct linkage.
1593 VTable->setLinkage(Linkage);
1594
NAKAMURA Takumic7da6da2015-05-09 21:10:07 +00001595 if (CGM.supportsCOMDAT() && VTable->isWeakForLinker())
1596 VTable->setComdat(CGM.getModule().getOrInsertComdat(VTable->getName()));
Rafael Espindolacb92c192015-01-15 23:18:01 +00001597
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001598 // Set the right visibility.
Rafael Espindola699f5d62018-02-07 22:15:33 +00001599 CGM.setGVProperties(VTable, RD);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001600
1601 // If this is the magic class __cxxabiv1::__fundamental_type_info,
1602 // we will emit the typeinfo for the fundamental types. This is the
1603 // same behaviour as GCC.
1604 const DeclContext *DC = RD->getDeclContext();
1605 if (RD->getIdentifier() &&
1606 RD->getIdentifier()->isStr("__fundamental_type_info") &&
1607 isa<NamespaceDecl>(DC) && cast<NamespaceDecl>(DC)->getIdentifier() &&
1608 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") &&
1609 DC->getParent()->isTranslationUnit())
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00001610 EmitFundamentalRTTIDescriptors(RD);
Peter Collingbournea4ccff32015-02-20 20:30:56 +00001611
Evgeniy Stepanov93987df2016-01-23 01:20:18 +00001612 if (!VTable->isDeclarationForLinker())
Peter Collingbourne8dd14da2016-06-24 21:21:46 +00001613 CGM.EmitVTableTypeMetadata(VTable, VTLayout);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001614}
1615
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00001616bool ItaniumCXXABI::isVirtualOffsetNeededForVTableField(
1617 CodeGenFunction &CGF, CodeGenFunction::VPtr Vptr) {
1618 if (Vptr.NearestVBase == nullptr)
1619 return false;
1620 return NeedsVTTParameter(CGF.CurGD);
Piotr Padlewski255652e2015-09-09 22:20:28 +00001621}
1622
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00001623llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructor(
1624 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base,
1625 const CXXRecordDecl *NearestVBase) {
1626
1627 if ((Base.getBase()->getNumVBases() || NearestVBase != nullptr) &&
1628 NeedsVTTParameter(CGF.CurGD)) {
1629 return getVTableAddressPointInStructorWithVTT(CGF, VTableClass, Base,
1630 NearestVBase);
1631 }
1632 return getVTableAddressPoint(Base, VTableClass);
1633}
1634
1635llvm::Constant *
1636ItaniumCXXABI::getVTableAddressPoint(BaseSubobject Base,
1637 const CXXRecordDecl *VTableClass) {
1638 llvm::GlobalValue *VTable = getAddrOfVTable(VTableClass, CharUnits());
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001639
Peter Collingbourne2849c4e2016-12-13 20:40:39 +00001640 // Find the appropriate vtable within the vtable group, and the address point
1641 // within that vtable.
1642 VTableLayout::AddressPointLocation AddressPoint =
1643 CGM.getItaniumVTableContext()
1644 .getVTableLayout(VTableClass)
1645 .getAddressPoint(Base);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001646 llvm::Value *Indices[] = {
Peter Collingbourne4e6a5402016-03-14 19:07:10 +00001647 llvm::ConstantInt::get(CGM.Int32Ty, 0),
Peter Collingbourne2849c4e2016-12-13 20:40:39 +00001648 llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.VTableIndex),
1649 llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.AddressPointIndex),
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001650 };
1651
Peter Collingbourne25a2b702016-12-13 20:50:44 +00001652 return llvm::ConstantExpr::getGetElementPtr(VTable->getValueType(), VTable,
1653 Indices, /*InBounds=*/true,
1654 /*InRangeIndex=*/1);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001655}
1656
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00001657llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructorWithVTT(
1658 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base,
1659 const CXXRecordDecl *NearestVBase) {
1660 assert((Base.getBase()->getNumVBases() || NearestVBase != nullptr) &&
1661 NeedsVTTParameter(CGF.CurGD) && "This class doesn't have VTT");
1662
1663 // Get the secondary vpointer index.
1664 uint64_t VirtualPointerIndex =
1665 CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base);
1666
1667 /// Load the VTT.
1668 llvm::Value *VTT = CGF.LoadCXXVTT();
1669 if (VirtualPointerIndex)
1670 VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex);
1671
1672 // And load the address point from the VTT.
1673 return CGF.Builder.CreateAlignedLoad(VTT, CGF.getPointerAlign());
1674}
1675
1676llvm::Constant *ItaniumCXXABI::getVTableAddressPointForConstExpr(
1677 BaseSubobject Base, const CXXRecordDecl *VTableClass) {
1678 return getVTableAddressPoint(Base, VTableClass);
1679}
1680
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001681llvm::GlobalVariable *ItaniumCXXABI::getAddrOfVTable(const CXXRecordDecl *RD,
1682 CharUnits VPtrOffset) {
1683 assert(VPtrOffset.isZero() && "Itanium ABI only supports zero vptr offsets");
1684
1685 llvm::GlobalVariable *&VTable = VTables[RD];
1686 if (VTable)
1687 return VTable;
1688
Eric Christopherd160c502016-01-29 01:35:53 +00001689 // Queue up this vtable for possible deferred emission.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001690 CGM.addDeferredVTable(RD);
1691
Yaron Kerene46f7ed2015-07-29 14:21:47 +00001692 SmallString<256> Name;
1693 llvm::raw_svector_ostream Out(Name);
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00001694 getMangleContext().mangleCXXVTable(RD, Out);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001695
Peter Collingbourne2849c4e2016-12-13 20:40:39 +00001696 const VTableLayout &VTLayout =
1697 CGM.getItaniumVTableContext().getVTableLayout(RD);
1698 llvm::Type *VTableType = CGM.getVTables().getVTableType(VTLayout);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001699
David Greenbe0c5b62018-09-12 14:09:06 +00001700 // Use pointer alignment for the vtable. Otherwise we would align them based
1701 // on the size of the initializer which doesn't make sense as only single
1702 // values are read.
1703 unsigned PAlign = CGM.getTarget().getPointerAlign(0);
1704
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001705 VTable = CGM.CreateOrReplaceCXXRuntimeVariable(
David Greenbe0c5b62018-09-12 14:09:06 +00001706 Name, VTableType, llvm::GlobalValue::ExternalLinkage,
1707 getContext().toCharUnitsFromBits(PAlign).getQuantity());
Peter Collingbournebcf909d2016-06-14 21:02:05 +00001708 VTable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
Hans Wennborgda24e9c2014-06-02 23:13:03 +00001709
Rafael Espindola922f2aa2018-02-23 19:30:48 +00001710 CGM.setGVProperties(VTable, RD);
1711
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001712 return VTable;
1713}
1714
John McCall9831b842018-02-06 18:52:44 +00001715CGCallee ItaniumCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF,
1716 GlobalDecl GD,
1717 Address This,
1718 llvm::Type *Ty,
1719 SourceLocation Loc) {
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +00001720 Ty = Ty->getPointerTo()->getPointerTo();
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001721 auto *MethodDecl = cast<CXXMethodDecl>(GD.getDecl());
1722 llvm::Value *VTable = CGF.GetVTablePtr(This, Ty, MethodDecl->getParent());
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +00001723
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001724 uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD);
John McCall9831b842018-02-06 18:52:44 +00001725 llvm::Value *VFunc;
1726 if (CGF.ShouldEmitVTableTypeCheckedLoad(MethodDecl->getParent())) {
1727 VFunc = CGF.EmitVTableTypeCheckedLoad(
Peter Collingbourne0ca03632016-06-25 00:24:06 +00001728 MethodDecl->getParent(), VTable,
1729 VTableIndex * CGM.getContext().getTargetInfo().getPointerWidth(0) / 8);
John McCall9831b842018-02-06 18:52:44 +00001730 } else {
1731 CGF.EmitTypeMetadataCodeForVCall(MethodDecl->getParent(), VTable, Loc);
Peter Collingbourne0ca03632016-06-25 00:24:06 +00001732
John McCall9831b842018-02-06 18:52:44 +00001733 llvm::Value *VFuncPtr =
1734 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn");
1735 auto *VFuncLoad =
1736 CGF.Builder.CreateAlignedLoad(VFuncPtr, CGF.getPointerAlign());
Piotr Padlewski77cc9622016-10-29 15:28:30 +00001737
John McCall9831b842018-02-06 18:52:44 +00001738 // Add !invariant.load md to virtual function load to indicate that
1739 // function didn't change inside vtable.
1740 // It's safe to add it without -fstrict-vtable-pointers, but it would not
1741 // help in devirtualization because it will only matter if we will have 2
1742 // the same virtual function loads from the same vtable load, which won't
1743 // happen without enabled devirtualization with -fstrict-vtable-pointers.
1744 if (CGM.getCodeGenOpts().OptimizationLevel > 0 &&
1745 CGM.getCodeGenOpts().StrictVTablePointers)
1746 VFuncLoad->setMetadata(
1747 llvm::LLVMContext::MD_invariant_load,
1748 llvm::MDNode::get(CGM.getLLVMContext(),
1749 llvm::ArrayRef<llvm::Metadata *>()));
1750 VFunc = VFuncLoad;
1751 }
John McCallb92ab1a2016-10-26 23:46:34 +00001752
Erich Keanede6480a32018-11-13 15:48:08 +00001753 CGCallee Callee(GD, VFunc);
John McCall9831b842018-02-06 18:52:44 +00001754 return Callee;
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +00001755}
1756
David Majnemer0c0b6d92014-10-31 20:09:12 +00001757llvm::Value *ItaniumCXXABI::EmitVirtualDestructorCall(
1758 CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType,
John McCall7f416cc2015-09-08 08:05:57 +00001759 Address This, const CXXMemberCallExpr *CE) {
Alexey Samsonova5bf76b2014-08-25 20:17:35 +00001760 assert(CE == nullptr || CE->arg_begin() == CE->arg_end());
Timur Iskhodzhanovd6197112013-02-15 14:45:22 +00001761 assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete);
1762
Rafael Espindola8d2a19b2014-09-08 16:01:27 +00001763 const CGFunctionInfo *FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration(
1764 Dtor, getFromDtorType(DtorType));
George Burgess IV00f70bd2018-03-01 05:43:23 +00001765 llvm::FunctionType *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo);
John McCallb92ab1a2016-10-26 23:46:34 +00001766 CGCallee Callee =
Peter Collingbourneea211002018-02-05 23:09:13 +00001767 CGCallee::forVirtual(CE, GlobalDecl(Dtor, DtorType), This, Ty);
Timur Iskhodzhanovd6197112013-02-15 14:45:22 +00001768
John McCall7f416cc2015-09-08 08:05:57 +00001769 CGF.EmitCXXMemberOrOperatorCall(Dtor, Callee, ReturnValueSlot(),
1770 This.getPointer(), /*ImplicitParam=*/nullptr,
Richard Smith762672a2016-09-28 19:09:10 +00001771 QualType(), CE, nullptr);
David Majnemer0c0b6d92014-10-31 20:09:12 +00001772 return nullptr;
Timur Iskhodzhanovd6197112013-02-15 14:45:22 +00001773}
1774
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001775void ItaniumCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) {
Reid Kleckner7810af02013-06-19 15:20:38 +00001776 CodeGenVTables &VTables = CGM.getVTables();
1777 llvm::GlobalVariable *VTT = VTables.GetAddrOfVTT(RD);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001778 VTables.EmitVTTDefinition(VTT, CGM.getVTableLinkage(RD), RD);
Reid Kleckner7810af02013-06-19 15:20:38 +00001779}
1780
Richard Smithc195c252018-11-27 19:33:49 +00001781bool ItaniumCXXABI::canSpeculativelyEmitVTableAsBaseClass(
1782 const CXXRecordDecl *RD) const {
Piotr Padlewskia68a7872015-07-24 04:04:49 +00001783 // We don't emit available_externally vtables if we are in -fapple-kext mode
1784 // because kext mode does not permit devirtualization.
1785 if (CGM.getLangOpts().AppleKext)
1786 return false;
1787
Piotr Padlewskie368de32018-06-13 13:55:42 +00001788 // If the vtable is hidden then it is not safe to emit an available_externally
1789 // copy of vtable.
1790 if (isVTableHidden(RD))
1791 return false;
1792
1793 if (CGM.getCodeGenOpts().ForceEmitVTables)
1794 return true;
1795
1796 // If we don't have any not emitted inline virtual function then we are safe
1797 // to emit an available_externally copy of vtable.
Piotr Padlewskia68a7872015-07-24 04:04:49 +00001798 // FIXME we can still emit a copy of the vtable if we
1799 // can emit definition of the inline functions.
Richard Smithc195c252018-11-27 19:33:49 +00001800 if (hasAnyUnusedVirtualInlineFunction(RD))
1801 return false;
1802
1803 // For a class with virtual bases, we must also be able to speculatively
1804 // emit the VTT, because CodeGen doesn't have separate notions of "can emit
1805 // the vtable" and "can emit the VTT". For a base subobject, this means we
1806 // need to be able to emit non-virtual base vtables.
1807 if (RD->getNumVBases()) {
1808 for (const auto &B : RD->bases()) {
1809 auto *BRD = B.getType()->getAsCXXRecordDecl();
1810 assert(BRD && "no class for base specifier");
1811 if (B.isVirtual() || !BRD->isDynamicClass())
1812 continue;
1813 if (!canSpeculativelyEmitVTableAsBaseClass(BRD))
1814 return false;
1815 }
1816 }
1817
1818 return true;
1819}
1820
1821bool ItaniumCXXABI::canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const {
1822 if (!canSpeculativelyEmitVTableAsBaseClass(RD))
1823 return false;
1824
1825 // For a complete-object vtable (or more specifically, for the VTT), we need
1826 // to be able to speculatively emit the vtables of all dynamic virtual bases.
1827 for (const auto &B : RD->vbases()) {
1828 auto *BRD = B.getType()->getAsCXXRecordDecl();
1829 assert(BRD && "no class for base specifier");
1830 if (!BRD->isDynamicClass())
1831 continue;
1832 if (!canSpeculativelyEmitVTableAsBaseClass(BRD))
1833 return false;
1834 }
1835
1836 return true;
Piotr Padlewskia68a7872015-07-24 04:04:49 +00001837}
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001838static llvm::Value *performTypeAdjustment(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00001839 Address InitialPtr,
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001840 int64_t NonVirtualAdjustment,
1841 int64_t VirtualAdjustment,
1842 bool IsReturnAdjustment) {
1843 if (!NonVirtualAdjustment && !VirtualAdjustment)
John McCall7f416cc2015-09-08 08:05:57 +00001844 return InitialPtr.getPointer();
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001845
John McCall7f416cc2015-09-08 08:05:57 +00001846 Address V = CGF.Builder.CreateElementBitCast(InitialPtr, CGF.Int8Ty);
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001847
John McCall7f416cc2015-09-08 08:05:57 +00001848 // In a base-to-derived cast, the non-virtual adjustment is applied first.
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001849 if (NonVirtualAdjustment && !IsReturnAdjustment) {
John McCall7f416cc2015-09-08 08:05:57 +00001850 V = CGF.Builder.CreateConstInBoundsByteGEP(V,
1851 CharUnits::fromQuantity(NonVirtualAdjustment));
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001852 }
1853
John McCall7f416cc2015-09-08 08:05:57 +00001854 // Perform the virtual adjustment if we have one.
1855 llvm::Value *ResultPtr;
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001856 if (VirtualAdjustment) {
1857 llvm::Type *PtrDiffTy =
1858 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1859
John McCall7f416cc2015-09-08 08:05:57 +00001860 Address VTablePtrPtr = CGF.Builder.CreateElementBitCast(V, CGF.Int8PtrTy);
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001861 llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr);
1862
1863 llvm::Value *OffsetPtr =
1864 CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
1865
1866 OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo());
1867
1868 // Load the adjustment offset from the vtable.
John McCall7f416cc2015-09-08 08:05:57 +00001869 llvm::Value *Offset =
1870 CGF.Builder.CreateAlignedLoad(OffsetPtr, CGF.getPointerAlign());
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001871
1872 // Adjust our pointer.
John McCall7f416cc2015-09-08 08:05:57 +00001873 ResultPtr = CGF.Builder.CreateInBoundsGEP(V.getPointer(), Offset);
1874 } else {
1875 ResultPtr = V.getPointer();
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001876 }
1877
John McCall7f416cc2015-09-08 08:05:57 +00001878 // In a derived-to-base conversion, the non-virtual adjustment is
1879 // applied second.
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001880 if (NonVirtualAdjustment && IsReturnAdjustment) {
John McCall7f416cc2015-09-08 08:05:57 +00001881 ResultPtr = CGF.Builder.CreateConstInBoundsGEP1_64(ResultPtr,
1882 NonVirtualAdjustment);
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001883 }
1884
1885 // Cast back to the original type.
John McCall7f416cc2015-09-08 08:05:57 +00001886 return CGF.Builder.CreateBitCast(ResultPtr, InitialPtr.getType());
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001887}
1888
1889llvm::Value *ItaniumCXXABI::performThisAdjustment(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00001890 Address This,
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001891 const ThisAdjustment &TA) {
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00001892 return performTypeAdjustment(CGF, This, TA.NonVirtual,
1893 TA.Virtual.Itanium.VCallOffsetOffset,
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001894 /*IsReturnAdjustment=*/false);
1895}
1896
1897llvm::Value *
John McCall7f416cc2015-09-08 08:05:57 +00001898ItaniumCXXABI::performReturnAdjustment(CodeGenFunction &CGF, Address Ret,
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001899 const ReturnAdjustment &RA) {
1900 return performTypeAdjustment(CGF, Ret, RA.NonVirtual,
1901 RA.Virtual.Itanium.VBaseOffsetOffset,
1902 /*IsReturnAdjustment=*/true);
1903}
1904
John McCall5d865c322010-08-31 07:33:07 +00001905void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
1906 RValue RV, QualType ResultType) {
1907 if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
1908 return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
1909
1910 // Destructor thunks in the ARM ABI have indeterminate results.
John McCall7f416cc2015-09-08 08:05:57 +00001911 llvm::Type *T = CGF.ReturnValue.getElementType();
John McCall5d865c322010-08-31 07:33:07 +00001912 RValue Undef = RValue::get(llvm::UndefValue::get(T));
1913 return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
1914}
John McCall8ed55a52010-09-02 09:58:18 +00001915
1916/************************** Array allocation cookies **************************/
1917
John McCallb91cd662012-05-01 05:23:51 +00001918CharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) {
1919 // The array cookie is a size_t; pad that up to the element alignment.
1920 // The cookie is actually right-justified in that space.
1921 return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes),
1922 CGM.getContext().getTypeAlignInChars(elementType));
John McCall8ed55a52010-09-02 09:58:18 +00001923}
1924
John McCall7f416cc2015-09-08 08:05:57 +00001925Address ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
1926 Address NewPtr,
1927 llvm::Value *NumElements,
1928 const CXXNewExpr *expr,
1929 QualType ElementType) {
John McCallb91cd662012-05-01 05:23:51 +00001930 assert(requiresArrayCookie(expr));
John McCall8ed55a52010-09-02 09:58:18 +00001931
John McCall7f416cc2015-09-08 08:05:57 +00001932 unsigned AS = NewPtr.getAddressSpace();
John McCall8ed55a52010-09-02 09:58:18 +00001933
John McCall9bca9232010-09-02 10:25:57 +00001934 ASTContext &Ctx = getContext();
John McCall7f416cc2015-09-08 08:05:57 +00001935 CharUnits SizeSize = CGF.getSizeSize();
John McCall8ed55a52010-09-02 09:58:18 +00001936
1937 // The size of the cookie.
1938 CharUnits CookieSize =
1939 std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
John McCallb91cd662012-05-01 05:23:51 +00001940 assert(CookieSize == getArrayCookieSizeImpl(ElementType));
John McCall8ed55a52010-09-02 09:58:18 +00001941
1942 // Compute an offset to the cookie.
John McCall7f416cc2015-09-08 08:05:57 +00001943 Address CookiePtr = NewPtr;
John McCall8ed55a52010-09-02 09:58:18 +00001944 CharUnits CookieOffset = CookieSize - SizeSize;
1945 if (!CookieOffset.isZero())
John McCall7f416cc2015-09-08 08:05:57 +00001946 CookiePtr = CGF.Builder.CreateConstInBoundsByteGEP(CookiePtr, CookieOffset);
John McCall8ed55a52010-09-02 09:58:18 +00001947
1948 // Write the number of elements into the appropriate slot.
John McCall7f416cc2015-09-08 08:05:57 +00001949 Address NumElementsPtr =
1950 CGF.Builder.CreateElementBitCast(CookiePtr, CGF.SizeTy);
Kostya Serebryany4ee69042014-08-26 02:29:59 +00001951 llvm::Instruction *SI = CGF.Builder.CreateStore(NumElements, NumElementsPtr);
John McCall7f416cc2015-09-08 08:05:57 +00001952
1953 // Handle the array cookie specially in ASan.
Filipe Cabecinhas6f83fa92018-01-02 13:46:12 +00001954 if (CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) && AS == 0 &&
Filipe Cabecinhas4ba58172018-02-12 11:49:02 +00001955 (expr->getOperatorNew()->isReplaceableGlobalAllocationFunction() ||
Filipe Cabecinhas0eb50082018-11-02 17:29:04 +00001956 CGM.getCodeGenOpts().SanitizeAddressPoisonCustomArrayCookie)) {
Kostya Serebryany4a9187a2014-08-29 01:01:32 +00001957 // The store to the CookiePtr does not need to be instrumented.
Kostya Serebryany4ee69042014-08-26 02:29:59 +00001958 CGM.getSanitizerMetadata()->disableSanitizerForInstruction(SI);
1959 llvm::FunctionType *FTy =
John McCall7f416cc2015-09-08 08:05:57 +00001960 llvm::FunctionType::get(CGM.VoidTy, NumElementsPtr.getType(), false);
Kostya Serebryany4ee69042014-08-26 02:29:59 +00001961 llvm::Constant *F =
1962 CGM.CreateRuntimeFunction(FTy, "__asan_poison_cxx_array_cookie");
John McCall7f416cc2015-09-08 08:05:57 +00001963 CGF.Builder.CreateCall(F, NumElementsPtr.getPointer());
Kostya Serebryany4ee69042014-08-26 02:29:59 +00001964 }
John McCall8ed55a52010-09-02 09:58:18 +00001965
1966 // Finally, compute a pointer to the actual data buffer by skipping
1967 // over the cookie completely.
John McCall7f416cc2015-09-08 08:05:57 +00001968 return CGF.Builder.CreateConstInBoundsByteGEP(NewPtr, CookieSize);
John McCall8ed55a52010-09-02 09:58:18 +00001969}
1970
John McCallb91cd662012-05-01 05:23:51 +00001971llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00001972 Address allocPtr,
John McCallb91cd662012-05-01 05:23:51 +00001973 CharUnits cookieSize) {
1974 // The element size is right-justified in the cookie.
John McCall7f416cc2015-09-08 08:05:57 +00001975 Address numElementsPtr = allocPtr;
1976 CharUnits numElementsOffset = cookieSize - CGF.getSizeSize();
John McCallb91cd662012-05-01 05:23:51 +00001977 if (!numElementsOffset.isZero())
1978 numElementsPtr =
John McCall7f416cc2015-09-08 08:05:57 +00001979 CGF.Builder.CreateConstInBoundsByteGEP(numElementsPtr, numElementsOffset);
John McCall8ed55a52010-09-02 09:58:18 +00001980
John McCall7f416cc2015-09-08 08:05:57 +00001981 unsigned AS = allocPtr.getAddressSpace();
1982 numElementsPtr = CGF.Builder.CreateElementBitCast(numElementsPtr, CGF.SizeTy);
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001983 if (!CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) || AS != 0)
Kostya Serebryany4a9187a2014-08-29 01:01:32 +00001984 return CGF.Builder.CreateLoad(numElementsPtr);
1985 // In asan mode emit a function call instead of a regular load and let the
1986 // run-time deal with it: if the shadow is properly poisoned return the
1987 // cookie, otherwise return 0 to avoid an infinite loop calling DTORs.
1988 // We can't simply ignore this load using nosanitize metadata because
1989 // the metadata may be lost.
1990 llvm::FunctionType *FTy =
1991 llvm::FunctionType::get(CGF.SizeTy, CGF.SizeTy->getPointerTo(0), false);
1992 llvm::Constant *F =
1993 CGM.CreateRuntimeFunction(FTy, "__asan_load_cxx_array_cookie");
John McCall7f416cc2015-09-08 08:05:57 +00001994 return CGF.Builder.CreateCall(F, numElementsPtr.getPointer());
John McCall8ed55a52010-09-02 09:58:18 +00001995}
1996
John McCallb91cd662012-05-01 05:23:51 +00001997CharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) {
John McCallc19c7062013-01-25 23:36:19 +00001998 // ARM says that the cookie is always:
John McCall8ed55a52010-09-02 09:58:18 +00001999 // struct array_cookie {
2000 // std::size_t element_size; // element_size != 0
2001 // std::size_t element_count;
2002 // };
John McCallc19c7062013-01-25 23:36:19 +00002003 // But the base ABI doesn't give anything an alignment greater than
2004 // 8, so we can dismiss this as typical ABI-author blindness to
2005 // actual language complexity and round up to the element alignment.
2006 return std::max(CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes),
2007 CGM.getContext().getTypeAlignInChars(elementType));
John McCall8ed55a52010-09-02 09:58:18 +00002008}
2009
John McCall7f416cc2015-09-08 08:05:57 +00002010Address ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
2011 Address newPtr,
2012 llvm::Value *numElements,
2013 const CXXNewExpr *expr,
2014 QualType elementType) {
John McCallb91cd662012-05-01 05:23:51 +00002015 assert(requiresArrayCookie(expr));
John McCall8ed55a52010-09-02 09:58:18 +00002016
John McCall8ed55a52010-09-02 09:58:18 +00002017 // The cookie is always at the start of the buffer.
John McCall7f416cc2015-09-08 08:05:57 +00002018 Address cookie = newPtr;
John McCall8ed55a52010-09-02 09:58:18 +00002019
2020 // The first element is the element size.
John McCall7f416cc2015-09-08 08:05:57 +00002021 cookie = CGF.Builder.CreateElementBitCast(cookie, CGF.SizeTy);
John McCallc19c7062013-01-25 23:36:19 +00002022 llvm::Value *elementSize = llvm::ConstantInt::get(CGF.SizeTy,
2023 getContext().getTypeSizeInChars(elementType).getQuantity());
2024 CGF.Builder.CreateStore(elementSize, cookie);
John McCall8ed55a52010-09-02 09:58:18 +00002025
2026 // The second element is the element count.
John McCall7f416cc2015-09-08 08:05:57 +00002027 cookie = CGF.Builder.CreateConstInBoundsGEP(cookie, 1, CGF.getSizeSize());
John McCallc19c7062013-01-25 23:36:19 +00002028 CGF.Builder.CreateStore(numElements, cookie);
John McCall8ed55a52010-09-02 09:58:18 +00002029
2030 // Finally, compute a pointer to the actual data buffer by skipping
2031 // over the cookie completely.
John McCallc19c7062013-01-25 23:36:19 +00002032 CharUnits cookieSize = ARMCXXABI::getArrayCookieSizeImpl(elementType);
John McCall7f416cc2015-09-08 08:05:57 +00002033 return CGF.Builder.CreateConstInBoundsByteGEP(newPtr, cookieSize);
John McCall8ed55a52010-09-02 09:58:18 +00002034}
2035
John McCallb91cd662012-05-01 05:23:51 +00002036llvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00002037 Address allocPtr,
John McCallb91cd662012-05-01 05:23:51 +00002038 CharUnits cookieSize) {
2039 // The number of elements is at offset sizeof(size_t) relative to
2040 // the allocated pointer.
John McCall7f416cc2015-09-08 08:05:57 +00002041 Address numElementsPtr
2042 = CGF.Builder.CreateConstInBoundsByteGEP(allocPtr, CGF.getSizeSize());
John McCall8ed55a52010-09-02 09:58:18 +00002043
John McCall7f416cc2015-09-08 08:05:57 +00002044 numElementsPtr = CGF.Builder.CreateElementBitCast(numElementsPtr, CGF.SizeTy);
John McCallb91cd662012-05-01 05:23:51 +00002045 return CGF.Builder.CreateLoad(numElementsPtr);
John McCall8ed55a52010-09-02 09:58:18 +00002046}
2047
John McCall68ff0372010-09-08 01:44:27 +00002048/*********************** Static local initialization **************************/
2049
2050static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM,
Chris Lattnera5f58b02011-07-09 17:41:47 +00002051 llvm::PointerType *GuardPtrTy) {
John McCall68ff0372010-09-08 01:44:27 +00002052 // int __cxa_guard_acquire(__guard *guard_object);
Chris Lattner2192fe52011-07-18 04:24:23 +00002053 llvm::FunctionType *FTy =
John McCall68ff0372010-09-08 01:44:27 +00002054 llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy),
Jay Foad5709f7c2011-07-29 13:56:53 +00002055 GuardPtrTy, /*isVarArg=*/false);
Reid Klecknerde864822017-03-21 16:57:30 +00002056 return CGM.CreateRuntimeFunction(
2057 FTy, "__cxa_guard_acquire",
2058 llvm::AttributeList::get(CGM.getLLVMContext(),
2059 llvm::AttributeList::FunctionIndex,
2060 llvm::Attribute::NoUnwind));
John McCall68ff0372010-09-08 01:44:27 +00002061}
2062
2063static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM,
Chris Lattnera5f58b02011-07-09 17:41:47 +00002064 llvm::PointerType *GuardPtrTy) {
John McCall68ff0372010-09-08 01:44:27 +00002065 // void __cxa_guard_release(__guard *guard_object);
Chris Lattner2192fe52011-07-18 04:24:23 +00002066 llvm::FunctionType *FTy =
Chris Lattnerece04092012-02-07 00:39:47 +00002067 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
Reid Klecknerde864822017-03-21 16:57:30 +00002068 return CGM.CreateRuntimeFunction(
2069 FTy, "__cxa_guard_release",
2070 llvm::AttributeList::get(CGM.getLLVMContext(),
2071 llvm::AttributeList::FunctionIndex,
2072 llvm::Attribute::NoUnwind));
John McCall68ff0372010-09-08 01:44:27 +00002073}
2074
2075static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM,
Chris Lattnera5f58b02011-07-09 17:41:47 +00002076 llvm::PointerType *GuardPtrTy) {
John McCall68ff0372010-09-08 01:44:27 +00002077 // void __cxa_guard_abort(__guard *guard_object);
Chris Lattner2192fe52011-07-18 04:24:23 +00002078 llvm::FunctionType *FTy =
Chris Lattnerece04092012-02-07 00:39:47 +00002079 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
Reid Klecknerde864822017-03-21 16:57:30 +00002080 return CGM.CreateRuntimeFunction(
2081 FTy, "__cxa_guard_abort",
2082 llvm::AttributeList::get(CGM.getLLVMContext(),
2083 llvm::AttributeList::FunctionIndex,
2084 llvm::Attribute::NoUnwind));
John McCall68ff0372010-09-08 01:44:27 +00002085}
2086
2087namespace {
David Blaikie7e70d682015-08-18 22:40:54 +00002088 struct CallGuardAbort final : EHScopeStack::Cleanup {
John McCall68ff0372010-09-08 01:44:27 +00002089 llvm::GlobalVariable *Guard;
Chandler Carruth84537952012-03-30 19:44:53 +00002090 CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
John McCall68ff0372010-09-08 01:44:27 +00002091
Craig Topper4f12f102014-03-12 06:41:41 +00002092 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCall882987f2013-02-28 19:01:20 +00002093 CGF.EmitNounwindRuntimeCall(getGuardAbortFn(CGF.CGM, Guard->getType()),
2094 Guard);
John McCall68ff0372010-09-08 01:44:27 +00002095 }
2096 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00002097}
John McCall68ff0372010-09-08 01:44:27 +00002098
2099/// The ARM code here follows the Itanium code closely enough that we
2100/// just special-case it at particular places.
John McCallcdf7ef52010-11-06 09:44:32 +00002101void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
2102 const VarDecl &D,
John McCallb88a5662012-03-30 21:00:39 +00002103 llvm::GlobalVariable *var,
2104 bool shouldPerformInit) {
John McCall68ff0372010-09-08 01:44:27 +00002105 CGBuilderTy &Builder = CGF.Builder;
John McCallcdf7ef52010-11-06 09:44:32 +00002106
Richard Smith62f19e72016-06-25 00:15:56 +00002107 // Inline variables that weren't instantiated from variable templates have
2108 // partially-ordered initialization within their translation unit.
2109 bool NonTemplateInline =
2110 D.isInline() &&
2111 !isTemplateInstantiation(D.getTemplateSpecializationKind());
2112
2113 // We only need to use thread-safe statics for local non-TLS variables and
2114 // inline variables; other global initialization is always single-threaded
2115 // or (through lazy dynamic loading in multiple threads) unsequenced.
Richard Smithdbf74ba2013-04-14 23:01:42 +00002116 bool threadsafe = getContext().getLangOpts().ThreadsafeStatics &&
Richard Smith62f19e72016-06-25 00:15:56 +00002117 (D.isLocalVarDecl() || NonTemplateInline) &&
2118 !D.getTLSKind();
Anders Carlssonc5d3ba12011-04-27 04:37:08 +00002119
Anders Carlssonc5d3ba12011-04-27 04:37:08 +00002120 // If we have a global variable with internal linkage and thread-safe statics
2121 // are disabled, we can just let the guard variable be of type i8.
John McCallb88a5662012-03-30 21:00:39 +00002122 bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage();
2123
2124 llvm::IntegerType *guardTy;
John McCall7f416cc2015-09-08 08:05:57 +00002125 CharUnits guardAlignment;
John McCall5aa52592011-06-17 07:33:57 +00002126 if (useInt8GuardVariable) {
John McCallb88a5662012-03-30 21:00:39 +00002127 guardTy = CGF.Int8Ty;
John McCall7f416cc2015-09-08 08:05:57 +00002128 guardAlignment = CharUnits::One();
John McCall5aa52592011-06-17 07:33:57 +00002129 } else {
Tim Northover9bb857a2013-01-31 12:13:10 +00002130 // Guard variables are 64 bits in the generic ABI and size width on ARM
2131 // (i.e. 32-bit on AArch32, 64-bit on AArch64).
John McCall7f416cc2015-09-08 08:05:57 +00002132 if (UseARMGuardVarABI) {
2133 guardTy = CGF.SizeTy;
2134 guardAlignment = CGF.getSizeAlign();
2135 } else {
2136 guardTy = CGF.Int64Ty;
2137 guardAlignment = CharUnits::fromQuantity(
2138 CGM.getDataLayout().getABITypeAlignment(guardTy));
2139 }
Anders Carlssonc5d3ba12011-04-27 04:37:08 +00002140 }
John McCallb88a5662012-03-30 21:00:39 +00002141 llvm::PointerType *guardPtrTy = guardTy->getPointerTo();
John McCall68ff0372010-09-08 01:44:27 +00002142
John McCallb88a5662012-03-30 21:00:39 +00002143 // Create the guard variable if we don't already have it (as we
2144 // might if we're double-emitting this function body).
2145 llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D);
2146 if (!guard) {
2147 // Mangle the name for the guard.
2148 SmallString<256> guardName;
2149 {
2150 llvm::raw_svector_ostream out(guardName);
Reid Klecknerd8110b62013-09-10 20:14:30 +00002151 getMangleContext().mangleStaticGuardVariable(&D, out);
John McCallb88a5662012-03-30 21:00:39 +00002152 }
John McCall8e7cb6d2010-11-02 21:04:24 +00002153
John McCallb88a5662012-03-30 21:00:39 +00002154 // Create the guard variable with a zero-initializer.
2155 // Just absorb linkage and visibility from the guarded variable.
2156 guard = new llvm::GlobalVariable(CGM.getModule(), guardTy,
2157 false, var->getLinkage(),
2158 llvm::ConstantInt::get(guardTy, 0),
2159 guardName.str());
Rafael Espindola699f5d62018-02-07 22:15:33 +00002160 guard->setDSOLocal(var->isDSOLocal());
John McCallb88a5662012-03-30 21:00:39 +00002161 guard->setVisibility(var->getVisibility());
Richard Smithdbf74ba2013-04-14 23:01:42 +00002162 // If the variable is thread-local, so is its guard variable.
2163 guard->setThreadLocalMode(var->getThreadLocalMode());
John McCall7f416cc2015-09-08 08:05:57 +00002164 guard->setAlignment(guardAlignment.getQuantity());
John McCallb88a5662012-03-30 21:00:39 +00002165
Yaron Keren5bfa1082015-09-03 20:33:29 +00002166 // The ABI says: "It is suggested that it be emitted in the same COMDAT
2167 // group as the associated data object." In practice, this doesn't work for
Dan Gohman839f2152017-01-17 21:46:38 +00002168 // non-ELF and non-Wasm object formats, so only do it for ELF and Wasm.
Rafael Espindola0d4fb982015-01-12 22:13:53 +00002169 llvm::Comdat *C = var->getComdat();
Yaron Keren5bfa1082015-09-03 20:33:29 +00002170 if (!D.isLocalVarDecl() && C &&
Dan Gohman839f2152017-01-17 21:46:38 +00002171 (CGM.getTarget().getTriple().isOSBinFormatELF() ||
2172 CGM.getTarget().getTriple().isOSBinFormatWasm())) {
Rafael Espindola2ae4b632014-09-19 19:43:18 +00002173 guard->setComdat(C);
Richard Smith62f19e72016-06-25 00:15:56 +00002174 // An inline variable's guard function is run from the per-TU
2175 // initialization function, not via a dedicated global ctor function, so
2176 // we can't put it in a comdat.
2177 if (!NonTemplateInline)
2178 CGF.CurFn->setComdat(C);
NAKAMURA Takumic7da6da2015-05-09 21:10:07 +00002179 } else if (CGM.supportsCOMDAT() && guard->isWeakForLinker()) {
2180 guard->setComdat(CGM.getModule().getOrInsertComdat(guard->getName()));
Rafael Espindola2ae4b632014-09-19 19:43:18 +00002181 }
2182
John McCallb88a5662012-03-30 21:00:39 +00002183 CGM.setStaticLocalDeclGuardAddress(&D, guard);
2184 }
John McCall87590e62012-03-30 07:09:50 +00002185
John McCall7f416cc2015-09-08 08:05:57 +00002186 Address guardAddr = Address(guard, guardAlignment);
2187
John McCall68ff0372010-09-08 01:44:27 +00002188 // Test whether the variable has completed initialization.
Justin Bogner0cbb6d82014-04-23 01:50:10 +00002189 //
John McCall68ff0372010-09-08 01:44:27 +00002190 // Itanium C++ ABI 3.3.2:
2191 // The following is pseudo-code showing how these functions can be used:
2192 // if (obj_guard.first_byte == 0) {
2193 // if ( __cxa_guard_acquire (&obj_guard) ) {
2194 // try {
2195 // ... initialize the object ...;
2196 // } catch (...) {
2197 // __cxa_guard_abort (&obj_guard);
2198 // throw;
2199 // }
2200 // ... queue object destructor with __cxa_atexit() ...;
2201 // __cxa_guard_release (&obj_guard);
2202 // }
2203 // }
Tim Northovera2ee4332014-03-29 15:09:45 +00002204
Justin Bogner0cbb6d82014-04-23 01:50:10 +00002205 // Load the first byte of the guard variable.
2206 llvm::LoadInst *LI =
John McCall7f416cc2015-09-08 08:05:57 +00002207 Builder.CreateLoad(Builder.CreateElementBitCast(guardAddr, CGM.Int8Ty));
John McCall68ff0372010-09-08 01:44:27 +00002208
Justin Bogner0cbb6d82014-04-23 01:50:10 +00002209 // Itanium ABI:
2210 // An implementation supporting thread-safety on multiprocessor
2211 // systems must also guarantee that references to the initialized
2212 // object do not occur before the load of the initialization flag.
2213 //
2214 // In LLVM, we do this by marking the load Acquire.
2215 if (threadsafe)
JF Bastien92f4ef12016-04-06 17:26:42 +00002216 LI->setAtomic(llvm::AtomicOrdering::Acquire);
Eli Friedman84d28122011-09-13 22:21:56 +00002217
Justin Bogner0cbb6d82014-04-23 01:50:10 +00002218 // For ARM, we should only check the first bit, rather than the entire byte:
2219 //
2220 // ARM C++ ABI 3.2.3.1:
2221 // To support the potential use of initialization guard variables
2222 // as semaphores that are the target of ARM SWP and LDREX/STREX
2223 // synchronizing instructions we define a static initialization
2224 // guard variable to be a 4-byte aligned, 4-byte word with the
2225 // following inline access protocol.
2226 // #define INITIALIZED 1
2227 // if ((obj_guard & INITIALIZED) != INITIALIZED) {
2228 // if (__cxa_guard_acquire(&obj_guard))
2229 // ...
2230 // }
2231 //
2232 // and similarly for ARM64:
2233 //
2234 // ARM64 C++ ABI 3.2.2:
2235 // This ABI instead only specifies the value bit 0 of the static guard
2236 // variable; all other bits are platform defined. Bit 0 shall be 0 when the
2237 // variable is not initialized and 1 when it is.
2238 llvm::Value *V =
2239 (UseARMGuardVarABI && !useInt8GuardVariable)
2240 ? Builder.CreateAnd(LI, llvm::ConstantInt::get(CGM.Int8Ty, 1))
2241 : LI;
Richard Smithae8d62c2017-07-26 22:01:09 +00002242 llvm::Value *NeedsInit = Builder.CreateIsNull(V, "guard.uninitialized");
John McCall68ff0372010-09-08 01:44:27 +00002243
2244 llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check");
2245 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
2246
2247 // Check if the first byte of the guard variable is zero.
Richard Smithae8d62c2017-07-26 22:01:09 +00002248 CGF.EmitCXXGuardedInitBranch(NeedsInit, InitCheckBlock, EndBlock,
2249 CodeGenFunction::GuardKind::VariableGuard, &D);
John McCall68ff0372010-09-08 01:44:27 +00002250
2251 CGF.EmitBlock(InitCheckBlock);
2252
2253 // Variables used when coping with thread-safe statics and exceptions.
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002254 if (threadsafe) {
John McCall68ff0372010-09-08 01:44:27 +00002255 // Call __cxa_guard_acquire.
2256 llvm::Value *V
John McCall882987f2013-02-28 19:01:20 +00002257 = CGF.EmitNounwindRuntimeCall(getGuardAcquireFn(CGM, guardPtrTy), guard);
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002258
John McCall68ff0372010-09-08 01:44:27 +00002259 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002260
John McCall68ff0372010-09-08 01:44:27 +00002261 Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
2262 InitBlock, EndBlock);
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002263
John McCall68ff0372010-09-08 01:44:27 +00002264 // Call __cxa_guard_abort along the exceptional edge.
John McCallb88a5662012-03-30 21:00:39 +00002265 CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard);
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002266
John McCall68ff0372010-09-08 01:44:27 +00002267 CGF.EmitBlock(InitBlock);
2268 }
2269
2270 // Emit the initializer and add a global destructor if appropriate.
John McCallb88a5662012-03-30 21:00:39 +00002271 CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit);
John McCall68ff0372010-09-08 01:44:27 +00002272
John McCall5aa52592011-06-17 07:33:57 +00002273 if (threadsafe) {
John McCall68ff0372010-09-08 01:44:27 +00002274 // Pop the guard-abort cleanup if we pushed one.
2275 CGF.PopCleanupBlock();
2276
2277 // Call __cxa_guard_release. This cannot throw.
John McCall7f416cc2015-09-08 08:05:57 +00002278 CGF.EmitNounwindRuntimeCall(getGuardReleaseFn(CGM, guardPtrTy),
2279 guardAddr.getPointer());
John McCall68ff0372010-09-08 01:44:27 +00002280 } else {
John McCall7f416cc2015-09-08 08:05:57 +00002281 Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guardAddr);
John McCall68ff0372010-09-08 01:44:27 +00002282 }
2283
2284 CGF.EmitBlock(EndBlock);
2285}
John McCallc84ed6a2012-05-01 06:13:13 +00002286
2287/// Register a global destructor using __cxa_atexit.
2288static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF,
2289 llvm::Constant *dtor,
Richard Smithdbf74ba2013-04-14 23:01:42 +00002290 llvm::Constant *addr,
2291 bool TLS) {
Bill Wendling95cae882013-05-02 19:18:03 +00002292 const char *Name = "__cxa_atexit";
2293 if (TLS) {
2294 const llvm::Triple &T = CGF.getTarget().getTriple();
Manman Renf93fff22015-11-11 23:08:18 +00002295 Name = T.isOSDarwin() ? "_tlv_atexit" : "__cxa_thread_atexit";
Bill Wendling95cae882013-05-02 19:18:03 +00002296 }
Richard Smithdbf74ba2013-04-14 23:01:42 +00002297
John McCallc84ed6a2012-05-01 06:13:13 +00002298 // We're assuming that the destructor function is something we can
2299 // reasonably call with the default CC. Go ahead and cast it to the
2300 // right prototype.
2301 llvm::Type *dtorTy =
2302 llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo();
2303
2304 // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d);
2305 llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy };
2306 llvm::FunctionType *atexitTy =
2307 llvm::FunctionType::get(CGF.IntTy, paramTys, false);
2308
2309 // Fetch the actual function.
Richard Smithdbf74ba2013-04-14 23:01:42 +00002310 llvm::Constant *atexit = CGF.CGM.CreateRuntimeFunction(atexitTy, Name);
John McCallc84ed6a2012-05-01 06:13:13 +00002311 if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit))
2312 fn->setDoesNotThrow();
2313
2314 // Create a variable that binds the atexit to this shared object.
2315 llvm::Constant *handle =
Reid Kleckner9de92142017-02-13 18:49:21 +00002316 CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle");
2317 auto *GV = cast<llvm::GlobalValue>(handle->stripPointerCasts());
2318 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
John McCallc84ed6a2012-05-01 06:13:13 +00002319
Akira Hatanaka617e2612018-04-17 18:41:52 +00002320 if (!addr)
2321 // addr is null when we are trying to register a dtor annotated with
2322 // __attribute__((destructor)) in a constructor function. Using null here is
2323 // okay because this argument is just passed back to the destructor
2324 // function.
2325 addr = llvm::Constant::getNullValue(CGF.Int8PtrTy);
2326
John McCallc84ed6a2012-05-01 06:13:13 +00002327 llvm::Value *args[] = {
2328 llvm::ConstantExpr::getBitCast(dtor, dtorTy),
2329 llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy),
2330 handle
2331 };
John McCall882987f2013-02-28 19:01:20 +00002332 CGF.EmitNounwindRuntimeCall(atexit, args);
John McCallc84ed6a2012-05-01 06:13:13 +00002333}
2334
Akira Hatanaka617e2612018-04-17 18:41:52 +00002335void CodeGenModule::registerGlobalDtorsWithAtExit() {
2336 for (const auto I : DtorsUsingAtExit) {
2337 int Priority = I.first;
2338 const llvm::TinyPtrVector<llvm::Function *> &Dtors = I.second;
2339
2340 // Create a function that registers destructors that have the same priority.
2341 //
2342 // Since constructor functions are run in non-descending order of their
2343 // priorities, destructors are registered in non-descending order of their
2344 // priorities, and since destructor functions are run in the reverse order
2345 // of their registration, destructor functions are run in non-ascending
2346 // order of their priorities.
2347 CodeGenFunction CGF(*this);
2348 std::string GlobalInitFnName =
2349 std::string("__GLOBAL_init_") + llvm::to_string(Priority);
2350 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
2351 llvm::Function *GlobalInitFn = CreateGlobalInitOrDestructFunction(
2352 FTy, GlobalInitFnName, getTypes().arrangeNullaryFunction(),
2353 SourceLocation());
2354 ASTContext &Ctx = getContext();
Jonas Devlieghere64a26302018-11-11 00:56:15 +00002355 QualType ReturnTy = Ctx.VoidTy;
2356 QualType FunctionTy = Ctx.getFunctionType(ReturnTy, llvm::None, {});
Akira Hatanaka617e2612018-04-17 18:41:52 +00002357 FunctionDecl *FD = FunctionDecl::Create(
2358 Ctx, Ctx.getTranslationUnitDecl(), SourceLocation(), SourceLocation(),
Jonas Devlieghere64a26302018-11-11 00:56:15 +00002359 &Ctx.Idents.get(GlobalInitFnName), FunctionTy, nullptr, SC_Static,
Akira Hatanaka617e2612018-04-17 18:41:52 +00002360 false, false);
Jonas Devlieghere64a26302018-11-11 00:56:15 +00002361 CGF.StartFunction(GlobalDecl(FD), ReturnTy, GlobalInitFn,
Akira Hatanaka617e2612018-04-17 18:41:52 +00002362 getTypes().arrangeNullaryFunction(), FunctionArgList(),
2363 SourceLocation(), SourceLocation());
2364
2365 for (auto *Dtor : Dtors) {
2366 // Register the destructor function calling __cxa_atexit if it is
2367 // available. Otherwise fall back on calling atexit.
2368 if (getCodeGenOpts().CXAAtExit)
2369 emitGlobalDtorWithCXAAtExit(CGF, Dtor, nullptr, false);
2370 else
2371 CGF.registerGlobalDtorWithAtExit(Dtor);
2372 }
2373
2374 CGF.FinishFunction();
2375 AddGlobalCtor(GlobalInitFn, Priority, nullptr);
2376 }
2377}
2378
John McCallc84ed6a2012-05-01 06:13:13 +00002379/// Register a global destructor as best as we know how.
2380void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF,
Richard Smithdbf74ba2013-04-14 23:01:42 +00002381 const VarDecl &D,
John McCallc84ed6a2012-05-01 06:13:13 +00002382 llvm::Constant *dtor,
2383 llvm::Constant *addr) {
Erik Pilkington5a559e62018-08-21 17:24:06 +00002384 if (D.isNoDestroy(CGM.getContext()))
2385 return;
2386
John McCallc84ed6a2012-05-01 06:13:13 +00002387 // Use __cxa_atexit if available.
Richard Smithdbf74ba2013-04-14 23:01:42 +00002388 if (CGM.getCodeGenOpts().CXAAtExit)
2389 return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr, D.getTLSKind());
2390
2391 if (D.getTLSKind())
2392 CGM.ErrorUnsupported(&D, "non-trivial TLS destruction");
John McCallc84ed6a2012-05-01 06:13:13 +00002393
2394 // In Apple kexts, we want to add a global destructor entry.
2395 // FIXME: shouldn't this be guarded by some variable?
Richard Smith9c6890a2012-11-01 22:30:59 +00002396 if (CGM.getLangOpts().AppleKext) {
John McCallc84ed6a2012-05-01 06:13:13 +00002397 // Generate a global destructor entry.
2398 return CGM.AddCXXDtorEntry(dtor, addr);
2399 }
2400
David Blaikieebe87e12013-08-27 23:57:18 +00002401 CGF.registerGlobalDtorWithAtExit(D, dtor, addr);
John McCallc84ed6a2012-05-01 06:13:13 +00002402}
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002403
David Majnemer9b21c332014-07-11 20:28:10 +00002404static bool isThreadWrapperReplaceable(const VarDecl *VD,
2405 CodeGen::CodeGenModule &CGM) {
2406 assert(!VD->isStaticLocal() && "static local VarDecls don't need wrappers!");
Manman Renf93fff22015-11-11 23:08:18 +00002407 // Darwin prefers to have references to thread local variables to go through
David Majnemer9b21c332014-07-11 20:28:10 +00002408 // the thread wrapper instead of directly referencing the backing variable.
2409 return VD->getTLSKind() == VarDecl::TLS_Dynamic &&
Manman Renf93fff22015-11-11 23:08:18 +00002410 CGM.getTarget().getTriple().isOSDarwin();
David Majnemer9b21c332014-07-11 20:28:10 +00002411}
2412
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002413/// Get the appropriate linkage for the wrapper function. This is essentially
David Majnemer4632e1e2014-06-27 16:56:27 +00002414/// the weak form of the variable's linkage; every translation unit which needs
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002415/// the wrapper emits a copy, and we want the linker to merge them.
David Majnemer35ab3282014-06-11 04:08:55 +00002416static llvm::GlobalValue::LinkageTypes
2417getThreadLocalWrapperLinkage(const VarDecl *VD, CodeGen::CodeGenModule &CGM) {
2418 llvm::GlobalValue::LinkageTypes VarLinkage =
2419 CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false);
2420
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002421 // For internal linkage variables, we don't need an external or weak wrapper.
2422 if (llvm::GlobalValue::isLocalLinkage(VarLinkage))
2423 return VarLinkage;
David Majnemer35ab3282014-06-11 04:08:55 +00002424
David Majnemer9b21c332014-07-11 20:28:10 +00002425 // If the thread wrapper is replaceable, give it appropriate linkage.
Manman Ren68150262015-11-11 22:42:31 +00002426 if (isThreadWrapperReplaceable(VD, CGM))
2427 if (!llvm::GlobalVariable::isLinkOnceLinkage(VarLinkage) &&
2428 !llvm::GlobalVariable::isWeakODRLinkage(VarLinkage))
2429 return VarLinkage;
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002430 return llvm::GlobalValue::WeakODRLinkage;
2431}
2432
2433llvm::Function *
2434ItaniumCXXABI::getOrCreateThreadLocalWrapper(const VarDecl *VD,
Alexander Musmanf94c3182014-09-26 06:28:25 +00002435 llvm::Value *Val) {
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002436 // Mangle the name for the thread_local wrapper function.
2437 SmallString<256> WrapperName;
2438 {
2439 llvm::raw_svector_ostream Out(WrapperName);
2440 getMangleContext().mangleItaniumThreadLocalWrapper(VD, Out);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002441 }
2442
Akira Hatanaka26907f92016-01-15 03:34:06 +00002443 // FIXME: If VD is a definition, we should regenerate the function attributes
2444 // before returning.
Alexander Musmanf94c3182014-09-26 06:28:25 +00002445 if (llvm::Value *V = CGM.getModule().getNamedValue(WrapperName))
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002446 return cast<llvm::Function>(V);
2447
Akira Hatanaka26907f92016-01-15 03:34:06 +00002448 QualType RetQT = VD->getType();
2449 if (RetQT->isReferenceType())
2450 RetQT = RetQT.getNonReferenceType();
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002451
John McCallc56a8b32016-03-11 04:30:31 +00002452 const CGFunctionInfo &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(
2453 getContext().getPointerType(RetQT), FunctionArgList());
Akira Hatanaka26907f92016-01-15 03:34:06 +00002454
2455 llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FI);
David Majnemer35ab3282014-06-11 04:08:55 +00002456 llvm::Function *Wrapper =
2457 llvm::Function::Create(FnTy, getThreadLocalWrapperLinkage(VD, CGM),
2458 WrapperName.str(), &CGM.getModule());
Akira Hatanaka26907f92016-01-15 03:34:06 +00002459
Erich Keanede6480a32018-11-13 15:48:08 +00002460 CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, Wrapper);
Akira Hatanaka26907f92016-01-15 03:34:06 +00002461
2462 if (VD->hasDefinition())
2463 CGM.SetLLVMFunctionAttributesForDefinition(nullptr, Wrapper);
2464
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002465 // Always resolve references to the wrapper at link time.
Vlad Tsyrklevichc93390b2019-01-17 17:53:45 +00002466 if (!Wrapper->hasLocalLinkage())
2467 if (!isThreadWrapperReplaceable(VD, CGM) ||
2468 llvm::GlobalVariable::isLinkOnceLinkage(Wrapper->getLinkage()) ||
2469 llvm::GlobalVariable::isWeakODRLinkage(Wrapper->getLinkage()) ||
2470 VD->getVisibility() == HiddenVisibility)
2471 Wrapper->setVisibility(llvm::GlobalValue::HiddenVisibility);
Manman Renb0b3af72015-12-17 00:42:36 +00002472
2473 if (isThreadWrapperReplaceable(VD, CGM)) {
2474 Wrapper->setCallingConv(llvm::CallingConv::CXX_FAST_TLS);
2475 Wrapper->addFnAttr(llvm::Attribute::NoUnwind);
2476 }
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002477 return Wrapper;
2478}
2479
2480void ItaniumCXXABI::EmitThreadLocalInitFuncs(
Richard Smith5a99c492015-12-01 01:10:48 +00002481 CodeGenModule &CGM, ArrayRef<const VarDecl *> CXXThreadLocals,
2482 ArrayRef<llvm::Function *> CXXThreadLocalInits,
2483 ArrayRef<const VarDecl *> CXXThreadLocalInitVars) {
David Majnemerb3341ea2014-10-05 05:05:40 +00002484 llvm::Function *InitFunc = nullptr;
Richard Smithfbe23692017-01-13 00:43:31 +00002485
2486 // Separate initializers into those with ordered (or partially-ordered)
2487 // initialization and those with unordered initialization.
2488 llvm::SmallVector<llvm::Function *, 8> OrderedInits;
2489 llvm::SmallDenseMap<const VarDecl *, llvm::Function *> UnorderedInits;
2490 for (unsigned I = 0; I != CXXThreadLocalInits.size(); ++I) {
2491 if (isTemplateInstantiation(
2492 CXXThreadLocalInitVars[I]->getTemplateSpecializationKind()))
2493 UnorderedInits[CXXThreadLocalInitVars[I]->getCanonicalDecl()] =
2494 CXXThreadLocalInits[I];
2495 else
2496 OrderedInits.push_back(CXXThreadLocalInits[I]);
2497 }
2498
2499 if (!OrderedInits.empty()) {
David Majnemerb3341ea2014-10-05 05:05:40 +00002500 // Generate a guarded initialization function.
2501 llvm::FunctionType *FTy =
2502 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
Akira Hatanaka7791f1a42015-10-31 01:28:07 +00002503 const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction();
2504 InitFunc = CGM.CreateGlobalInitOrDestructFunction(FTy, "__tls_init", FI,
Alexey Samsonov1444bb92014-10-17 00:20:19 +00002505 SourceLocation(),
David Majnemerb3341ea2014-10-05 05:05:40 +00002506 /*TLS=*/true);
2507 llvm::GlobalVariable *Guard = new llvm::GlobalVariable(
2508 CGM.getModule(), CGM.Int8Ty, /*isConstant=*/false,
2509 llvm::GlobalVariable::InternalLinkage,
2510 llvm::ConstantInt::get(CGM.Int8Ty, 0), "__tls_guard");
2511 Guard->setThreadLocal(true);
John McCall7f416cc2015-09-08 08:05:57 +00002512
2513 CharUnits GuardAlign = CharUnits::One();
2514 Guard->setAlignment(GuardAlign.getQuantity());
2515
Richard Smith3ad06362018-10-31 20:39:26 +00002516 CodeGenFunction(CGM).GenerateCXXGlobalInitFunc(
2517 InitFunc, OrderedInits, ConstantAddress(Guard, GuardAlign));
Manman Ren5e5d0462016-03-18 23:35:21 +00002518 // On Darwin platforms, use CXX_FAST_TLS calling convention.
2519 if (CGM.getTarget().getTriple().isOSDarwin()) {
2520 InitFunc->setCallingConv(llvm::CallingConv::CXX_FAST_TLS);
2521 InitFunc->addFnAttr(llvm::Attribute::NoUnwind);
2522 }
David Majnemerb3341ea2014-10-05 05:05:40 +00002523 }
Richard Smithfbe23692017-01-13 00:43:31 +00002524
2525 // Emit thread wrappers.
Richard Smith5a99c492015-12-01 01:10:48 +00002526 for (const VarDecl *VD : CXXThreadLocals) {
2527 llvm::GlobalVariable *Var =
2528 cast<llvm::GlobalVariable>(CGM.GetGlobalValue(CGM.getMangledName(VD)));
Richard Smithfbe23692017-01-13 00:43:31 +00002529 llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Var);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002530
David Majnemer9b21c332014-07-11 20:28:10 +00002531 // Some targets require that all access to thread local variables go through
2532 // the thread wrapper. This means that we cannot attempt to create a thread
2533 // wrapper or a thread helper.
Richard Smithfbe23692017-01-13 00:43:31 +00002534 if (isThreadWrapperReplaceable(VD, CGM) && !VD->hasDefinition()) {
2535 Wrapper->setLinkage(llvm::Function::ExternalLinkage);
David Majnemer9b21c332014-07-11 20:28:10 +00002536 continue;
Richard Smithfbe23692017-01-13 00:43:31 +00002537 }
David Majnemer9b21c332014-07-11 20:28:10 +00002538
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002539 // Mangle the name for the thread_local initialization function.
2540 SmallString<256> InitFnName;
2541 {
2542 llvm::raw_svector_ostream Out(InitFnName);
2543 getMangleContext().mangleItaniumThreadLocalInit(VD, Out);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002544 }
2545
2546 // If we have a definition for the variable, emit the initialization
2547 // function as an alias to the global Init function (if any). Otherwise,
2548 // produce a declaration of the initialization function.
Craig Topper8a13c412014-05-21 05:09:00 +00002549 llvm::GlobalValue *Init = nullptr;
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002550 bool InitIsInitFunc = false;
2551 if (VD->hasDefinition()) {
2552 InitIsInitFunc = true;
Richard Smithfbe23692017-01-13 00:43:31 +00002553 llvm::Function *InitFuncToUse = InitFunc;
2554 if (isTemplateInstantiation(VD->getTemplateSpecializationKind()))
2555 InitFuncToUse = UnorderedInits.lookup(VD->getCanonicalDecl());
2556 if (InitFuncToUse)
Rafael Espindola234405b2014-05-17 21:30:14 +00002557 Init = llvm::GlobalAlias::create(Var->getLinkage(), InitFnName.str(),
Richard Smithfbe23692017-01-13 00:43:31 +00002558 InitFuncToUse);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002559 } else {
2560 // Emit a weak global function referring to the initialization function.
2561 // This function will not exist if the TU defining the thread_local
2562 // variable in question does not need any dynamic initialization for
2563 // its thread_local variables.
2564 llvm::FunctionType *FnTy = llvm::FunctionType::get(CGM.VoidTy, false);
Richard Smithfbe23692017-01-13 00:43:31 +00002565 Init = llvm::Function::Create(FnTy,
2566 llvm::GlobalVariable::ExternalWeakLinkage,
2567 InitFnName.str(), &CGM.getModule());
John McCallc56a8b32016-03-11 04:30:31 +00002568 const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction();
Erich Keanede6480a32018-11-13 15:48:08 +00002569 CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI,
2570 cast<llvm::Function>(Init));
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002571 }
2572
Rafael Espindolaabdb3222018-03-07 23:18:06 +00002573 if (Init) {
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002574 Init->setVisibility(Var->getVisibility());
Rafael Espindolaabdb3222018-03-07 23:18:06 +00002575 Init->setDSOLocal(Var->isDSOLocal());
2576 }
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002577
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002578 llvm::LLVMContext &Context = CGM.getModule().getContext();
2579 llvm::BasicBlock *Entry = llvm::BasicBlock::Create(Context, "", Wrapper);
John McCall7f416cc2015-09-08 08:05:57 +00002580 CGBuilderTy Builder(CGM, Entry);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002581 if (InitIsInitFunc) {
Manman Ren5e5d0462016-03-18 23:35:21 +00002582 if (Init) {
2583 llvm::CallInst *CallVal = Builder.CreateCall(Init);
Akira Hatanaka1da9dbb2018-05-29 18:28:49 +00002584 if (isThreadWrapperReplaceable(VD, CGM)) {
Manman Ren5e5d0462016-03-18 23:35:21 +00002585 CallVal->setCallingConv(llvm::CallingConv::CXX_FAST_TLS);
Akira Hatanaka1da9dbb2018-05-29 18:28:49 +00002586 llvm::Function *Fn =
2587 cast<llvm::Function>(cast<llvm::GlobalAlias>(Init)->getAliasee());
2588 Fn->setCallingConv(llvm::CallingConv::CXX_FAST_TLS);
2589 }
Manman Ren5e5d0462016-03-18 23:35:21 +00002590 }
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002591 } else {
2592 // Don't know whether we have an init function. Call it if it exists.
2593 llvm::Value *Have = Builder.CreateIsNotNull(Init);
2594 llvm::BasicBlock *InitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
2595 llvm::BasicBlock *ExitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
2596 Builder.CreateCondBr(Have, InitBB, ExitBB);
2597
2598 Builder.SetInsertPoint(InitBB);
David Blaikie4ba525b2015-07-14 17:27:39 +00002599 Builder.CreateCall(Init);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002600 Builder.CreateBr(ExitBB);
2601
2602 Builder.SetInsertPoint(ExitBB);
2603 }
2604
2605 // For a reference, the result of the wrapper function is a pointer to
2606 // the referenced object.
2607 llvm::Value *Val = Var;
2608 if (VD->getType()->isReferenceType()) {
John McCall7f416cc2015-09-08 08:05:57 +00002609 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2610 Val = Builder.CreateAlignedLoad(Val, Align);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002611 }
Alexander Musmanf94c3182014-09-26 06:28:25 +00002612 if (Val->getType() != Wrapper->getReturnType())
2613 Val = Builder.CreatePointerBitCastOrAddrSpaceCast(
2614 Val, Wrapper->getReturnType(), "");
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002615 Builder.CreateRet(Val);
2616 }
2617}
2618
Richard Smith0f383742014-03-26 22:48:22 +00002619LValue ItaniumCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
2620 const VarDecl *VD,
2621 QualType LValType) {
Richard Smith5a99c492015-12-01 01:10:48 +00002622 llvm::Value *Val = CGF.CGM.GetAddrOfGlobalVar(VD);
Alexander Musmanf94c3182014-09-26 06:28:25 +00002623 llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Val);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002624
Manman Renb0b3af72015-12-17 00:42:36 +00002625 llvm::CallInst *CallVal = CGF.Builder.CreateCall(Wrapper);
Saleem Abdulrasool4a7130a2016-08-01 21:31:24 +00002626 CallVal->setCallingConv(Wrapper->getCallingConv());
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002627
2628 LValue LV;
2629 if (VD->getType()->isReferenceType())
Manman Renb0b3af72015-12-17 00:42:36 +00002630 LV = CGF.MakeNaturalAlignAddrLValue(CallVal, LValType);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002631 else
Manman Renb0b3af72015-12-17 00:42:36 +00002632 LV = CGF.MakeAddrLValue(CallVal, LValType,
2633 CGF.getContext().getDeclAlign(VD));
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002634 // FIXME: need setObjCGCLValueClass?
2635 return LV;
2636}
Peter Collingbourne66f82e62013-06-28 20:45:28 +00002637
2638/// Return whether the given global decl needs a VTT parameter, which it does
2639/// if it's a base constructor or destructor with virtual bases.
2640bool ItaniumCXXABI::NeedsVTTParameter(GlobalDecl GD) {
2641 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002642
Peter Collingbourne66f82e62013-06-28 20:45:28 +00002643 // We don't have any virtual bases, just return early.
2644 if (!MD->getParent()->getNumVBases())
2645 return false;
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002646
Peter Collingbourne66f82e62013-06-28 20:45:28 +00002647 // Check if we have a base constructor.
2648 if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base)
2649 return true;
2650
2651 // Check if we have a base destructor.
2652 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
2653 return true;
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002654
Peter Collingbourne66f82e62013-06-28 20:45:28 +00002655 return false;
2656}
David Majnemere2cb8d12014-07-07 06:20:47 +00002657
2658namespace {
2659class ItaniumRTTIBuilder {
2660 CodeGenModule &CGM; // Per-module state.
2661 llvm::LLVMContext &VMContext;
2662 const ItaniumCXXABI &CXXABI; // Per-module state.
2663
2664 /// Fields - The fields of the RTTI descriptor currently being built.
2665 SmallVector<llvm::Constant *, 16> Fields;
2666
2667 /// GetAddrOfTypeName - Returns the mangled type name of the given type.
2668 llvm::GlobalVariable *
2669 GetAddrOfTypeName(QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage);
2670
2671 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
2672 /// descriptor of the given type.
2673 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
2674
2675 /// BuildVTablePointer - Build the vtable pointer for the given type.
2676 void BuildVTablePointer(const Type *Ty);
2677
2678 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
2679 /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
2680 void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
2681
2682 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
2683 /// classes with bases that do not satisfy the abi::__si_class_type_info
2684 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
2685 void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
2686
2687 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
2688 /// for pointer types.
2689 void BuildPointerTypeInfo(QualType PointeeTy);
2690
2691 /// BuildObjCObjectTypeInfo - Build the appropriate kind of
2692 /// type_info for an object type.
2693 void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty);
2694
2695 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
2696 /// struct, used for member pointer types.
2697 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
2698
2699public:
2700 ItaniumRTTIBuilder(const ItaniumCXXABI &ABI)
2701 : CGM(ABI.CGM), VMContext(CGM.getModule().getContext()), CXXABI(ABI) {}
2702
2703 // Pointer type info flags.
2704 enum {
2705 /// PTI_Const - Type has const qualifier.
2706 PTI_Const = 0x1,
2707
2708 /// PTI_Volatile - Type has volatile qualifier.
2709 PTI_Volatile = 0x2,
2710
2711 /// PTI_Restrict - Type has restrict qualifier.
2712 PTI_Restrict = 0x4,
2713
2714 /// PTI_Incomplete - Type is incomplete.
2715 PTI_Incomplete = 0x8,
2716
2717 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
2718 /// (in pointer to member).
Richard Smitha7d93782016-12-01 03:32:42 +00002719 PTI_ContainingClassIncomplete = 0x10,
2720
2721 /// PTI_TransactionSafe - Pointee is transaction_safe function (C++ TM TS).
2722 //PTI_TransactionSafe = 0x20,
2723
2724 /// PTI_Noexcept - Pointee is noexcept function (C++1z).
2725 PTI_Noexcept = 0x40,
David Majnemere2cb8d12014-07-07 06:20:47 +00002726 };
2727
2728 // VMI type info flags.
2729 enum {
2730 /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
2731 VMI_NonDiamondRepeat = 0x1,
2732
2733 /// VMI_DiamondShaped - Class is diamond shaped.
2734 VMI_DiamondShaped = 0x2
2735 };
2736
2737 // Base class type info flags.
2738 enum {
2739 /// BCTI_Virtual - Base class is virtual.
2740 BCTI_Virtual = 0x1,
2741
2742 /// BCTI_Public - Base class is public.
2743 BCTI_Public = 0x2
2744 };
2745
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00002746 /// BuildTypeInfo - Build the RTTI type info struct for the given type, or
2747 /// link to an existing RTTI descriptor if one already exists.
2748 llvm::Constant *BuildTypeInfo(QualType Ty);
2749
David Majnemere2cb8d12014-07-07 06:20:47 +00002750 /// BuildTypeInfo - Build the RTTI type info struct for the given type.
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00002751 llvm::Constant *BuildTypeInfo(
2752 QualType Ty,
2753 llvm::GlobalVariable::LinkageTypes Linkage,
2754 llvm::GlobalValue::VisibilityTypes Visibility,
2755 llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass);
David Majnemere2cb8d12014-07-07 06:20:47 +00002756};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00002757}
David Majnemere2cb8d12014-07-07 06:20:47 +00002758
2759llvm::GlobalVariable *ItaniumRTTIBuilder::GetAddrOfTypeName(
2760 QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage) {
Yaron Kerene46f7ed2015-07-29 14:21:47 +00002761 SmallString<256> Name;
2762 llvm::raw_svector_ostream Out(Name);
David Majnemere2cb8d12014-07-07 06:20:47 +00002763 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
David Majnemere2cb8d12014-07-07 06:20:47 +00002764
2765 // We know that the mangled name of the type starts at index 4 of the
2766 // mangled name of the typename, so we can just index into it in order to
2767 // get the mangled name of the type.
2768 llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext,
2769 Name.substr(4));
David Greenbe0c5b62018-09-12 14:09:06 +00002770 auto Align = CGM.getContext().getTypeAlignInChars(CGM.getContext().CharTy);
David Majnemere2cb8d12014-07-07 06:20:47 +00002771
David Greenbe0c5b62018-09-12 14:09:06 +00002772 llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable(
2773 Name, Init->getType(), Linkage, Align.getQuantity());
David Majnemere2cb8d12014-07-07 06:20:47 +00002774
2775 GV->setInitializer(Init);
2776
2777 return GV;
2778}
2779
2780llvm::Constant *
2781ItaniumRTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
2782 // Mangle the RTTI name.
Yaron Kerene46f7ed2015-07-29 14:21:47 +00002783 SmallString<256> Name;
2784 llvm::raw_svector_ostream Out(Name);
David Majnemere2cb8d12014-07-07 06:20:47 +00002785 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
David Majnemere2cb8d12014-07-07 06:20:47 +00002786
2787 // Look for an existing global.
2788 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
2789
2790 if (!GV) {
2791 // Create a new global variable.
Piotr Padlewskid3b1cbd2017-06-01 08:04:05 +00002792 // Note for the future: If we would ever like to do deferred emission of
2793 // RTTI, check if emitting vtables opportunistically need any adjustment.
2794
David Majnemere2cb8d12014-07-07 06:20:47 +00002795 GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy,
2796 /*Constant=*/true,
2797 llvm::GlobalValue::ExternalLinkage, nullptr,
2798 Name);
Rafael Espindola3f727a82018-03-14 18:14:46 +00002799 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
2800 CGM.setGVProperties(GV, RD);
David Majnemere2cb8d12014-07-07 06:20:47 +00002801 }
2802
2803 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
2804}
2805
2806/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
2807/// info for that type is defined in the standard library.
2808static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
2809 // Itanium C++ ABI 2.9.2:
2810 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
2811 // the run-time support library. Specifically, the run-time support
2812 // library should contain type_info objects for the types X, X* and
2813 // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char,
2814 // unsigned char, signed char, short, unsigned short, int, unsigned int,
2815 // long, unsigned long, long long, unsigned long long, float, double,
2816 // long double, char16_t, char32_t, and the IEEE 754r decimal and
2817 // half-precision floating point types.
Richard Smith4a382012016-02-03 01:32:42 +00002818 //
2819 // GCC also emits RTTI for __int128.
2820 // FIXME: We do not emit RTTI information for decimal types here.
2821
2822 // Types added here must also be added to EmitFundamentalRTTIDescriptors.
David Majnemere2cb8d12014-07-07 06:20:47 +00002823 switch (Ty->getKind()) {
2824 case BuiltinType::Void:
2825 case BuiltinType::NullPtr:
2826 case BuiltinType::Bool:
2827 case BuiltinType::WChar_S:
2828 case BuiltinType::WChar_U:
2829 case BuiltinType::Char_U:
2830 case BuiltinType::Char_S:
2831 case BuiltinType::UChar:
2832 case BuiltinType::SChar:
2833 case BuiltinType::Short:
2834 case BuiltinType::UShort:
2835 case BuiltinType::Int:
2836 case BuiltinType::UInt:
2837 case BuiltinType::Long:
2838 case BuiltinType::ULong:
2839 case BuiltinType::LongLong:
2840 case BuiltinType::ULongLong:
2841 case BuiltinType::Half:
2842 case BuiltinType::Float:
2843 case BuiltinType::Double:
2844 case BuiltinType::LongDouble:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00002845 case BuiltinType::Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00002846 case BuiltinType::Float128:
Richard Smith3a8244d2018-05-01 05:02:45 +00002847 case BuiltinType::Char8:
David Majnemere2cb8d12014-07-07 06:20:47 +00002848 case BuiltinType::Char16:
2849 case BuiltinType::Char32:
2850 case BuiltinType::Int128:
2851 case BuiltinType::UInt128:
Richard Smith4a382012016-02-03 01:32:42 +00002852 return true;
2853
Alexey Bader954ba212016-04-08 13:40:33 +00002854#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
2855 case BuiltinType::Id:
Alexey Baderb62f1442016-04-13 08:33:41 +00002856#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +00002857#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
2858 case BuiltinType::Id:
2859#include "clang/Basic/OpenCLExtensionTypes.def"
David Majnemere2cb8d12014-07-07 06:20:47 +00002860 case BuiltinType::OCLSampler:
2861 case BuiltinType::OCLEvent:
Alexey Bader9c8453f2015-09-15 11:18:52 +00002862 case BuiltinType::OCLClkEvent:
2863 case BuiltinType::OCLQueue:
Alexey Bader9c8453f2015-09-15 11:18:52 +00002864 case BuiltinType::OCLReserveID:
Leonard Chanf921d852018-06-04 16:07:52 +00002865 case BuiltinType::ShortAccum:
2866 case BuiltinType::Accum:
2867 case BuiltinType::LongAccum:
2868 case BuiltinType::UShortAccum:
2869 case BuiltinType::UAccum:
2870 case BuiltinType::ULongAccum:
Leonard Chanab80f3c2018-06-14 14:53:51 +00002871 case BuiltinType::ShortFract:
2872 case BuiltinType::Fract:
2873 case BuiltinType::LongFract:
2874 case BuiltinType::UShortFract:
2875 case BuiltinType::UFract:
2876 case BuiltinType::ULongFract:
2877 case BuiltinType::SatShortAccum:
2878 case BuiltinType::SatAccum:
2879 case BuiltinType::SatLongAccum:
2880 case BuiltinType::SatUShortAccum:
2881 case BuiltinType::SatUAccum:
2882 case BuiltinType::SatULongAccum:
2883 case BuiltinType::SatShortFract:
2884 case BuiltinType::SatFract:
2885 case BuiltinType::SatLongFract:
2886 case BuiltinType::SatUShortFract:
2887 case BuiltinType::SatUFract:
2888 case BuiltinType::SatULongFract:
Richard Smith4a382012016-02-03 01:32:42 +00002889 return false;
David Majnemere2cb8d12014-07-07 06:20:47 +00002890
2891 case BuiltinType::Dependent:
2892#define BUILTIN_TYPE(Id, SingletonId)
2893#define PLACEHOLDER_TYPE(Id, SingletonId) \
2894 case BuiltinType::Id:
2895#include "clang/AST/BuiltinTypes.def"
2896 llvm_unreachable("asking for RRTI for a placeholder type!");
2897
2898 case BuiltinType::ObjCId:
2899 case BuiltinType::ObjCClass:
2900 case BuiltinType::ObjCSel:
2901 llvm_unreachable("FIXME: Objective-C types are unsupported!");
2902 }
2903
2904 llvm_unreachable("Invalid BuiltinType Kind!");
2905}
2906
2907static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
2908 QualType PointeeTy = PointerTy->getPointeeType();
2909 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
2910 if (!BuiltinTy)
2911 return false;
2912
2913 // Check the qualifiers.
2914 Qualifiers Quals = PointeeTy.getQualifiers();
2915 Quals.removeConst();
2916
2917 if (!Quals.empty())
2918 return false;
2919
2920 return TypeInfoIsInStandardLibrary(BuiltinTy);
2921}
2922
2923/// IsStandardLibraryRTTIDescriptor - Returns whether the type
2924/// information for the given type exists in the standard library.
2925static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
2926 // Type info for builtin types is defined in the standard library.
2927 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
2928 return TypeInfoIsInStandardLibrary(BuiltinTy);
2929
2930 // Type info for some pointer types to builtin types is defined in the
2931 // standard library.
2932 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
2933 return TypeInfoIsInStandardLibrary(PointerTy);
2934
2935 return false;
2936}
2937
2938/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
2939/// the given type exists somewhere else, and that we should not emit the type
2940/// information in this translation unit. Assumes that it is not a
2941/// standard-library type.
2942static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM,
2943 QualType Ty) {
2944 ASTContext &Context = CGM.getContext();
2945
2946 // If RTTI is disabled, assume it might be disabled in the
2947 // translation unit that defines any potential key function, too.
2948 if (!Context.getLangOpts().RTTI) return false;
2949
2950 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
2951 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
2952 if (!RD->hasDefinition())
2953 return false;
2954
2955 if (!RD->isDynamicClass())
2956 return false;
2957
2958 // FIXME: this may need to be reconsidered if the key function
2959 // changes.
David Majnemerbe9022c2015-08-06 20:56:55 +00002960 // N.B. We must always emit the RTTI data ourselves if there exists a key
2961 // function.
2962 bool IsDLLImport = RD->hasAttr<DLLImportAttr>();
Martin Storsjo3b528942018-02-02 06:22:35 +00002963
2964 // Don't import the RTTI but emit it locally.
2965 if (CGM.getTriple().isWindowsGNUEnvironment() && IsDLLImport)
2966 return false;
2967
David Majnemer1fb1a042014-11-07 07:26:38 +00002968 if (CGM.getVTables().isVTableExternal(RD))
Shoaib Meenai61118e72017-07-04 01:02:19 +00002969 return IsDLLImport && !CGM.getTriple().isWindowsItaniumEnvironment()
2970 ? false
2971 : true;
David Majnemer1fb1a042014-11-07 07:26:38 +00002972
David Majnemerbe9022c2015-08-06 20:56:55 +00002973 if (IsDLLImport)
David Majnemer1fb1a042014-11-07 07:26:38 +00002974 return true;
David Majnemere2cb8d12014-07-07 06:20:47 +00002975 }
2976
2977 return false;
2978}
2979
2980/// IsIncompleteClassType - Returns whether the given record type is incomplete.
2981static bool IsIncompleteClassType(const RecordType *RecordTy) {
2982 return !RecordTy->getDecl()->isCompleteDefinition();
2983}
2984
2985/// ContainsIncompleteClassType - Returns whether the given type contains an
2986/// incomplete class type. This is true if
2987///
2988/// * The given type is an incomplete class type.
2989/// * The given type is a pointer type whose pointee type contains an
2990/// incomplete class type.
2991/// * The given type is a member pointer type whose class is an incomplete
2992/// class type.
2993/// * The given type is a member pointer type whoise pointee type contains an
2994/// incomplete class type.
2995/// is an indirect or direct pointer to an incomplete class type.
2996static bool ContainsIncompleteClassType(QualType Ty) {
2997 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
2998 if (IsIncompleteClassType(RecordTy))
2999 return true;
3000 }
3001
3002 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
3003 return ContainsIncompleteClassType(PointerTy->getPointeeType());
3004
3005 if (const MemberPointerType *MemberPointerTy =
3006 dyn_cast<MemberPointerType>(Ty)) {
3007 // Check if the class type is incomplete.
3008 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
3009 if (IsIncompleteClassType(ClassType))
3010 return true;
3011
3012 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
3013 }
3014
3015 return false;
3016}
3017
3018// CanUseSingleInheritance - Return whether the given record decl has a "single,
3019// public, non-virtual base at offset zero (i.e. the derived class is dynamic
3020// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
3021static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
3022 // Check the number of bases.
3023 if (RD->getNumBases() != 1)
3024 return false;
3025
3026 // Get the base.
3027 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
3028
3029 // Check that the base is not virtual.
3030 if (Base->isVirtual())
3031 return false;
3032
3033 // Check that the base is public.
3034 if (Base->getAccessSpecifier() != AS_public)
3035 return false;
3036
3037 // Check that the class is dynamic iff the base is.
3038 const CXXRecordDecl *BaseDecl =
3039 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
3040 if (!BaseDecl->isEmpty() &&
3041 BaseDecl->isDynamicClass() != RD->isDynamicClass())
3042 return false;
3043
3044 return true;
3045}
3046
3047void ItaniumRTTIBuilder::BuildVTablePointer(const Type *Ty) {
3048 // abi::__class_type_info.
3049 static const char * const ClassTypeInfo =
3050 "_ZTVN10__cxxabiv117__class_type_infoE";
3051 // abi::__si_class_type_info.
3052 static const char * const SIClassTypeInfo =
3053 "_ZTVN10__cxxabiv120__si_class_type_infoE";
3054 // abi::__vmi_class_type_info.
3055 static const char * const VMIClassTypeInfo =
3056 "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
3057
3058 const char *VTableName = nullptr;
3059
3060 switch (Ty->getTypeClass()) {
3061#define TYPE(Class, Base)
3062#define ABSTRACT_TYPE(Class, Base)
3063#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
3064#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3065#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3066#include "clang/AST/TypeNodes.def"
3067 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
3068
3069 case Type::LValueReference:
3070 case Type::RValueReference:
3071 llvm_unreachable("References shouldn't get here");
3072
3073 case Type::Auto:
Richard Smith600b5262017-01-26 20:40:47 +00003074 case Type::DeducedTemplateSpecialization:
3075 llvm_unreachable("Undeduced type shouldn't get here");
David Majnemere2cb8d12014-07-07 06:20:47 +00003076
Xiuli Pan9c14e282016-01-09 12:53:17 +00003077 case Type::Pipe:
3078 llvm_unreachable("Pipe types shouldn't get here");
3079
David Majnemere2cb8d12014-07-07 06:20:47 +00003080 case Type::Builtin:
3081 // GCC treats vector and complex types as fundamental types.
3082 case Type::Vector:
3083 case Type::ExtVector:
3084 case Type::Complex:
3085 case Type::Atomic:
3086 // FIXME: GCC treats block pointers as fundamental types?!
3087 case Type::BlockPointer:
3088 // abi::__fundamental_type_info.
3089 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
3090 break;
3091
3092 case Type::ConstantArray:
3093 case Type::IncompleteArray:
3094 case Type::VariableArray:
3095 // abi::__array_type_info.
3096 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
3097 break;
3098
3099 case Type::FunctionNoProto:
3100 case Type::FunctionProto:
Richard Smithb17d6fa2016-12-01 03:04:07 +00003101 // abi::__function_type_info.
3102 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
David Majnemere2cb8d12014-07-07 06:20:47 +00003103 break;
3104
3105 case Type::Enum:
3106 // abi::__enum_type_info.
3107 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
3108 break;
3109
3110 case Type::Record: {
Rafael Espindolaf6688122018-03-22 21:14:16 +00003111 const CXXRecordDecl *RD =
3112 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
David Majnemere2cb8d12014-07-07 06:20:47 +00003113
3114 if (!RD->hasDefinition() || !RD->getNumBases()) {
3115 VTableName = ClassTypeInfo;
3116 } else if (CanUseSingleInheritance(RD)) {
3117 VTableName = SIClassTypeInfo;
3118 } else {
3119 VTableName = VMIClassTypeInfo;
3120 }
3121
3122 break;
3123 }
3124
3125 case Type::ObjCObject:
3126 // Ignore protocol qualifiers.
3127 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
3128
3129 // Handle id and Class.
3130 if (isa<BuiltinType>(Ty)) {
3131 VTableName = ClassTypeInfo;
3132 break;
3133 }
3134
3135 assert(isa<ObjCInterfaceType>(Ty));
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +00003136 LLVM_FALLTHROUGH;
David Majnemere2cb8d12014-07-07 06:20:47 +00003137
3138 case Type::ObjCInterface:
3139 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
3140 VTableName = SIClassTypeInfo;
3141 } else {
3142 VTableName = ClassTypeInfo;
3143 }
3144 break;
3145
3146 case Type::ObjCObjectPointer:
3147 case Type::Pointer:
3148 // abi::__pointer_type_info.
3149 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
3150 break;
3151
3152 case Type::MemberPointer:
3153 // abi::__pointer_to_member_type_info.
3154 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
3155 break;
3156 }
3157
3158 llvm::Constant *VTable =
3159 CGM.getModule().getOrInsertGlobal(VTableName, CGM.Int8PtrTy);
Rafael Espindolafe9a55a2018-03-23 01:36:23 +00003160 CGM.setDSOLocal(cast<llvm::GlobalValue>(VTable->stripPointerCasts()));
David Majnemere2cb8d12014-07-07 06:20:47 +00003161
3162 llvm::Type *PtrDiffTy =
3163 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
3164
3165 // The vtable address point is 2.
3166 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
David Blaikiee3b172a2015-04-02 18:55:21 +00003167 VTable =
3168 llvm::ConstantExpr::getInBoundsGetElementPtr(CGM.Int8PtrTy, VTable, Two);
David Majnemere2cb8d12014-07-07 06:20:47 +00003169 VTable = llvm::ConstantExpr::getBitCast(VTable, CGM.Int8PtrTy);
3170
3171 Fields.push_back(VTable);
3172}
3173
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003174/// Return the linkage that the type info and type info name constants
David Majnemere2cb8d12014-07-07 06:20:47 +00003175/// should have for the given type.
Richard Smithbbb26552018-05-21 20:10:54 +00003176static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(CodeGenModule &CGM,
3177 QualType Ty) {
David Majnemere2cb8d12014-07-07 06:20:47 +00003178 // Itanium C++ ABI 2.9.5p7:
3179 // In addition, it and all of the intermediate abi::__pointer_type_info
3180 // structs in the chain down to the abi::__class_type_info for the
3181 // incomplete class type must be prevented from resolving to the
3182 // corresponding type_info structs for the complete class type, possibly
3183 // by making them local static objects. Finally, a dummy class RTTI is
3184 // generated for the incomplete type that will not resolve to the final
3185 // complete class RTTI (because the latter need not exist), possibly by
3186 // making it a local static object.
3187 if (ContainsIncompleteClassType(Ty))
Richard Smithbbb26552018-05-21 20:10:54 +00003188 return llvm::GlobalValue::InternalLinkage;
3189
3190 switch (Ty->getLinkage()) {
3191 case NoLinkage:
3192 case InternalLinkage:
3193 case UniqueExternalLinkage:
3194 return llvm::GlobalValue::InternalLinkage;
3195
3196 case VisibleNoLinkage:
3197 case ModuleInternalLinkage:
3198 case ModuleLinkage:
3199 case ExternalLinkage:
3200 // RTTI is not enabled, which means that this type info struct is going
3201 // to be used for exception handling. Give it linkonce_odr linkage.
3202 if (!CGM.getLangOpts().RTTI)
3203 return llvm::GlobalValue::LinkOnceODRLinkage;
3204
3205 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
3206 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
3207 if (RD->hasAttr<WeakAttr>())
3208 return llvm::GlobalValue::WeakODRLinkage;
3209 if (CGM.getTriple().isWindowsItaniumEnvironment())
3210 if (RD->hasAttr<DLLImportAttr>() &&
3211 ShouldUseExternalRTTIDescriptor(CGM, Ty))
3212 return llvm::GlobalValue::ExternalLinkage;
3213 // MinGW always uses LinkOnceODRLinkage for type info.
3214 if (RD->isDynamicClass() &&
3215 !CGM.getContext()
3216 .getTargetInfo()
3217 .getTriple()
3218 .isWindowsGNUEnvironment())
3219 return CGM.getVTableLinkage(RD);
3220 }
3221
3222 return llvm::GlobalValue::LinkOnceODRLinkage;
3223 }
3224
3225 llvm_unreachable("Invalid linkage!");
David Majnemere2cb8d12014-07-07 06:20:47 +00003226}
3227
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003228llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty) {
David Majnemere2cb8d12014-07-07 06:20:47 +00003229 // We want to operate on the canonical type.
Yaron Kerenebd14262016-03-16 12:14:43 +00003230 Ty = Ty.getCanonicalType();
David Majnemere2cb8d12014-07-07 06:20:47 +00003231
3232 // Check if we've already emitted an RTTI descriptor for this type.
Yaron Kerene46f7ed2015-07-29 14:21:47 +00003233 SmallString<256> Name;
3234 llvm::raw_svector_ostream Out(Name);
David Majnemere2cb8d12014-07-07 06:20:47 +00003235 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
David Majnemere2cb8d12014-07-07 06:20:47 +00003236
3237 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
3238 if (OldGV && !OldGV->isDeclaration()) {
3239 assert(!OldGV->hasAvailableExternallyLinkage() &&
3240 "available_externally typeinfos not yet implemented");
3241
3242 return llvm::ConstantExpr::getBitCast(OldGV, CGM.Int8PtrTy);
3243 }
3244
3245 // Check if there is already an external RTTI descriptor for this type.
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003246 if (IsStandardLibraryRTTIDescriptor(Ty) ||
3247 ShouldUseExternalRTTIDescriptor(CGM, Ty))
David Majnemere2cb8d12014-07-07 06:20:47 +00003248 return GetAddrOfExternalRTTIDescriptor(Ty);
3249
3250 // Emit the standard library with external linkage.
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003251 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(CGM, Ty);
Richard Smithbbb26552018-05-21 20:10:54 +00003252
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003253 // Give the type_info object and name the formal visibility of the
3254 // type itself.
3255 llvm::GlobalValue::VisibilityTypes llvmVisibility;
3256 if (llvm::GlobalValue::isLocalLinkage(Linkage))
3257 // If the linkage is local, only default visibility makes sense.
3258 llvmVisibility = llvm::GlobalValue::DefaultVisibility;
3259 else if (CXXABI.classifyRTTIUniqueness(Ty, Linkage) ==
3260 ItaniumCXXABI::RUK_NonUniqueHidden)
3261 llvmVisibility = llvm::GlobalValue::HiddenVisibility;
3262 else
3263 llvmVisibility = CodeGenModule::GetLLVMVisibility(Ty->getVisibility());
3264
3265 llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass =
3266 llvm::GlobalValue::DefaultStorageClass;
3267 if (CGM.getTriple().isWindowsItaniumEnvironment()) {
3268 auto RD = Ty->getAsCXXRecordDecl();
3269 if (RD && RD->hasAttr<DLLExportAttr>())
3270 DLLStorageClass = llvm::GlobalValue::DLLExportStorageClass;
3271 }
3272
3273 return BuildTypeInfo(Ty, Linkage, llvmVisibility, DLLStorageClass);
3274}
3275
3276llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(
3277 QualType Ty,
3278 llvm::GlobalVariable::LinkageTypes Linkage,
3279 llvm::GlobalValue::VisibilityTypes Visibility,
3280 llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass) {
David Majnemere2cb8d12014-07-07 06:20:47 +00003281 // Add the vtable pointer.
3282 BuildVTablePointer(cast<Type>(Ty));
3283
3284 // And the name.
Richard Smithbbb26552018-05-21 20:10:54 +00003285 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
David Majnemere2cb8d12014-07-07 06:20:47 +00003286 llvm::Constant *TypeNameField;
3287
3288 // If we're supposed to demote the visibility, be sure to set a flag
3289 // to use a string comparison for type_info comparisons.
3290 ItaniumCXXABI::RTTIUniquenessKind RTTIUniqueness =
Richard Smithbbb26552018-05-21 20:10:54 +00003291 CXXABI.classifyRTTIUniqueness(Ty, Linkage);
David Majnemere2cb8d12014-07-07 06:20:47 +00003292 if (RTTIUniqueness != ItaniumCXXABI::RUK_Unique) {
3293 // The flag is the sign bit, which on ARM64 is defined to be clear
3294 // for global pointers. This is very ARM64-specific.
3295 TypeNameField = llvm::ConstantExpr::getPtrToInt(TypeName, CGM.Int64Ty);
3296 llvm::Constant *flag =
3297 llvm::ConstantInt::get(CGM.Int64Ty, ((uint64_t)1) << 63);
3298 TypeNameField = llvm::ConstantExpr::getAdd(TypeNameField, flag);
3299 TypeNameField =
3300 llvm::ConstantExpr::getIntToPtr(TypeNameField, CGM.Int8PtrTy);
3301 } else {
3302 TypeNameField = llvm::ConstantExpr::getBitCast(TypeName, CGM.Int8PtrTy);
3303 }
3304 Fields.push_back(TypeNameField);
3305
3306 switch (Ty->getTypeClass()) {
3307#define TYPE(Class, Base)
3308#define ABSTRACT_TYPE(Class, Base)
3309#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
3310#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3311#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3312#include "clang/AST/TypeNodes.def"
3313 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
3314
3315 // GCC treats vector types as fundamental types.
3316 case Type::Builtin:
3317 case Type::Vector:
3318 case Type::ExtVector:
3319 case Type::Complex:
3320 case Type::BlockPointer:
3321 // Itanium C++ ABI 2.9.5p4:
3322 // abi::__fundamental_type_info adds no data members to std::type_info.
3323 break;
3324
3325 case Type::LValueReference:
3326 case Type::RValueReference:
3327 llvm_unreachable("References shouldn't get here");
3328
3329 case Type::Auto:
Richard Smith600b5262017-01-26 20:40:47 +00003330 case Type::DeducedTemplateSpecialization:
3331 llvm_unreachable("Undeduced type shouldn't get here");
David Majnemere2cb8d12014-07-07 06:20:47 +00003332
Xiuli Pan9c14e282016-01-09 12:53:17 +00003333 case Type::Pipe:
3334 llvm_unreachable("Pipe type shouldn't get here");
3335
David Majnemere2cb8d12014-07-07 06:20:47 +00003336 case Type::ConstantArray:
3337 case Type::IncompleteArray:
3338 case Type::VariableArray:
3339 // Itanium C++ ABI 2.9.5p5:
3340 // abi::__array_type_info adds no data members to std::type_info.
3341 break;
3342
3343 case Type::FunctionNoProto:
Richard Smithb17d6fa2016-12-01 03:04:07 +00003344 case Type::FunctionProto:
David Majnemere2cb8d12014-07-07 06:20:47 +00003345 // Itanium C++ ABI 2.9.5p5:
3346 // abi::__function_type_info adds no data members to std::type_info.
3347 break;
3348
3349 case Type::Enum:
3350 // Itanium C++ ABI 2.9.5p5:
3351 // abi::__enum_type_info adds no data members to std::type_info.
3352 break;
3353
3354 case Type::Record: {
3355 const CXXRecordDecl *RD =
3356 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
3357 if (!RD->hasDefinition() || !RD->getNumBases()) {
3358 // We don't need to emit any fields.
3359 break;
3360 }
3361
3362 if (CanUseSingleInheritance(RD))
3363 BuildSIClassTypeInfo(RD);
3364 else
3365 BuildVMIClassTypeInfo(RD);
3366
3367 break;
3368 }
3369
3370 case Type::ObjCObject:
3371 case Type::ObjCInterface:
3372 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
3373 break;
3374
3375 case Type::ObjCObjectPointer:
3376 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
3377 break;
3378
3379 case Type::Pointer:
3380 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
3381 break;
3382
3383 case Type::MemberPointer:
3384 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
3385 break;
3386
3387 case Type::Atomic:
3388 // No fields, at least for the moment.
3389 break;
3390 }
3391
3392 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields);
3393
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003394 SmallString<256> Name;
3395 llvm::raw_svector_ostream Out(Name);
3396 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
Rafael Espindolacb92c192015-01-15 23:18:01 +00003397 llvm::Module &M = CGM.getModule();
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003398 llvm::GlobalVariable *OldGV = M.getNamedGlobal(Name);
David Majnemere2cb8d12014-07-07 06:20:47 +00003399 llvm::GlobalVariable *GV =
Rafael Espindolacb92c192015-01-15 23:18:01 +00003400 new llvm::GlobalVariable(M, Init->getType(),
Richard Smithbbb26552018-05-21 20:10:54 +00003401 /*Constant=*/true, Linkage, Init, Name);
Rafael Espindolacb92c192015-01-15 23:18:01 +00003402
David Majnemere2cb8d12014-07-07 06:20:47 +00003403 // If there's already an old global variable, replace it with the new one.
3404 if (OldGV) {
3405 GV->takeName(OldGV);
3406 llvm::Constant *NewPtr =
3407 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
3408 OldGV->replaceAllUsesWith(NewPtr);
3409 OldGV->eraseFromParent();
3410 }
3411
Yaron Keren04da2382015-07-29 15:42:28 +00003412 if (CGM.supportsCOMDAT() && GV->isWeakForLinker())
3413 GV->setComdat(M.getOrInsertComdat(GV->getName()));
3414
David Greenbe0c5b62018-09-12 14:09:06 +00003415 CharUnits Align =
3416 CGM.getContext().toCharUnitsFromBits(CGM.getTarget().getPointerAlign(0));
3417 GV->setAlignment(Align.getQuantity());
3418
David Majnemere2cb8d12014-07-07 06:20:47 +00003419 // The Itanium ABI specifies that type_info objects must be globally
3420 // unique, with one exception: if the type is an incomplete class
3421 // type or a (possibly indirect) pointer to one. That exception
3422 // affects the general case of comparing type_info objects produced
3423 // by the typeid operator, which is why the comparison operators on
3424 // std::type_info generally use the type_info name pointers instead
3425 // of the object addresses. However, the language's built-in uses
3426 // of RTTI generally require class types to be complete, even when
3427 // manipulating pointers to those class types. This allows the
3428 // implementation of dynamic_cast to rely on address equality tests,
3429 // which is much faster.
3430
3431 // All of this is to say that it's important that both the type_info
3432 // object and the type_info name be uniqued when weakly emitted.
3433
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003434 TypeName->setVisibility(Visibility);
Rafael Espindola3dd49812018-02-23 00:22:15 +00003435 CGM.setDSOLocal(TypeName);
Rafael Espindola699f5d62018-02-07 22:15:33 +00003436
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003437 GV->setVisibility(Visibility);
Rafael Espindola3dd49812018-02-23 00:22:15 +00003438 CGM.setDSOLocal(GV);
Saleem Abdulrasool18820022016-12-02 22:46:18 +00003439
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003440 TypeName->setDLLStorageClass(DLLStorageClass);
3441 GV->setDLLStorageClass(DLLStorageClass);
David Majnemere2cb8d12014-07-07 06:20:47 +00003442
3443 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
3444}
3445
David Majnemere2cb8d12014-07-07 06:20:47 +00003446/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
3447/// for the given Objective-C object type.
3448void ItaniumRTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
3449 // Drop qualifiers.
3450 const Type *T = OT->getBaseType().getTypePtr();
3451 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
3452
3453 // The builtin types are abi::__class_type_infos and don't require
3454 // extra fields.
3455 if (isa<BuiltinType>(T)) return;
3456
3457 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
3458 ObjCInterfaceDecl *Super = Class->getSuperClass();
3459
3460 // Root classes are also __class_type_info.
3461 if (!Super) return;
3462
3463 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
3464
3465 // Everything else is single inheritance.
3466 llvm::Constant *BaseTypeInfo =
3467 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(SuperTy);
3468 Fields.push_back(BaseTypeInfo);
3469}
3470
3471/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
3472/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
3473void ItaniumRTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
3474 // Itanium C++ ABI 2.9.5p6b:
3475 // It adds to abi::__class_type_info a single member pointing to the
3476 // type_info structure for the base type,
3477 llvm::Constant *BaseTypeInfo =
3478 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(RD->bases_begin()->getType());
3479 Fields.push_back(BaseTypeInfo);
3480}
3481
3482namespace {
3483 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
3484 /// a class hierarchy.
3485 struct SeenBases {
3486 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
3487 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
3488 };
3489}
3490
3491/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
3492/// abi::__vmi_class_type_info.
3493///
3494static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
3495 SeenBases &Bases) {
3496
3497 unsigned Flags = 0;
3498
3499 const CXXRecordDecl *BaseDecl =
3500 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
3501
3502 if (Base->isVirtual()) {
3503 // Mark the virtual base as seen.
David Blaikie82e95a32014-11-19 07:49:47 +00003504 if (!Bases.VirtualBases.insert(BaseDecl).second) {
David Majnemere2cb8d12014-07-07 06:20:47 +00003505 // If this virtual base has been seen before, then the class is diamond
3506 // shaped.
3507 Flags |= ItaniumRTTIBuilder::VMI_DiamondShaped;
3508 } else {
3509 if (Bases.NonVirtualBases.count(BaseDecl))
3510 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
3511 }
3512 } else {
3513 // Mark the non-virtual base as seen.
David Blaikie82e95a32014-11-19 07:49:47 +00003514 if (!Bases.NonVirtualBases.insert(BaseDecl).second) {
David Majnemere2cb8d12014-07-07 06:20:47 +00003515 // If this non-virtual base has been seen before, then the class has non-
3516 // diamond shaped repeated inheritance.
3517 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
3518 } else {
3519 if (Bases.VirtualBases.count(BaseDecl))
3520 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
3521 }
3522 }
3523
3524 // Walk all bases.
3525 for (const auto &I : BaseDecl->bases())
3526 Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases);
3527
3528 return Flags;
3529}
3530
3531static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
3532 unsigned Flags = 0;
3533 SeenBases Bases;
3534
3535 // Walk all bases.
3536 for (const auto &I : RD->bases())
3537 Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases);
3538
3539 return Flags;
3540}
3541
3542/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
3543/// classes with bases that do not satisfy the abi::__si_class_type_info
3544/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
3545void ItaniumRTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
3546 llvm::Type *UnsignedIntLTy =
3547 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
3548
3549 // Itanium C++ ABI 2.9.5p6c:
3550 // __flags is a word with flags describing details about the class
3551 // structure, which may be referenced by using the __flags_masks
3552 // enumeration. These flags refer to both direct and indirect bases.
3553 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
3554 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3555
3556 // Itanium C++ ABI 2.9.5p6c:
3557 // __base_count is a word with the number of direct proper base class
3558 // descriptions that follow.
3559 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
3560
3561 if (!RD->getNumBases())
3562 return;
3563
David Majnemere2cb8d12014-07-07 06:20:47 +00003564 // Now add the base class descriptions.
3565
3566 // Itanium C++ ABI 2.9.5p6c:
3567 // __base_info[] is an array of base class descriptions -- one for every
3568 // direct proper base. Each description is of the type:
3569 //
3570 // struct abi::__base_class_type_info {
3571 // public:
3572 // const __class_type_info *__base_type;
3573 // long __offset_flags;
3574 //
3575 // enum __offset_flags_masks {
3576 // __virtual_mask = 0x1,
3577 // __public_mask = 0x2,
3578 // __offset_shift = 8
3579 // };
3580 // };
Reid Klecknerd8b04662016-08-25 22:16:30 +00003581
3582 // If we're in mingw and 'long' isn't wide enough for a pointer, use 'long
3583 // long' instead of 'long' for __offset_flags. libstdc++abi uses long long on
3584 // LLP64 platforms.
3585 // FIXME: Consider updating libc++abi to match, and extend this logic to all
3586 // LLP64 platforms.
3587 QualType OffsetFlagsTy = CGM.getContext().LongTy;
3588 const TargetInfo &TI = CGM.getContext().getTargetInfo();
3589 if (TI.getTriple().isOSCygMing() && TI.getPointerWidth(0) > TI.getLongWidth())
3590 OffsetFlagsTy = CGM.getContext().LongLongTy;
3591 llvm::Type *OffsetFlagsLTy =
3592 CGM.getTypes().ConvertType(OffsetFlagsTy);
3593
David Majnemere2cb8d12014-07-07 06:20:47 +00003594 for (const auto &Base : RD->bases()) {
3595 // The __base_type member points to the RTTI for the base type.
3596 Fields.push_back(ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(Base.getType()));
3597
3598 const CXXRecordDecl *BaseDecl =
3599 cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
3600
3601 int64_t OffsetFlags = 0;
3602
3603 // All but the lower 8 bits of __offset_flags are a signed offset.
3604 // For a non-virtual base, this is the offset in the object of the base
3605 // subobject. For a virtual base, this is the offset in the virtual table of
3606 // the virtual base offset for the virtual base referenced (negative).
3607 CharUnits Offset;
3608 if (Base.isVirtual())
3609 Offset =
3610 CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(RD, BaseDecl);
3611 else {
3612 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
3613 Offset = Layout.getBaseClassOffset(BaseDecl);
3614 };
3615
3616 OffsetFlags = uint64_t(Offset.getQuantity()) << 8;
3617
3618 // The low-order byte of __offset_flags contains flags, as given by the
3619 // masks from the enumeration __offset_flags_masks.
3620 if (Base.isVirtual())
3621 OffsetFlags |= BCTI_Virtual;
3622 if (Base.getAccessSpecifier() == AS_public)
3623 OffsetFlags |= BCTI_Public;
3624
Reid Klecknerd8b04662016-08-25 22:16:30 +00003625 Fields.push_back(llvm::ConstantInt::get(OffsetFlagsLTy, OffsetFlags));
David Majnemere2cb8d12014-07-07 06:20:47 +00003626 }
3627}
3628
Richard Smitha7d93782016-12-01 03:32:42 +00003629/// Compute the flags for a __pbase_type_info, and remove the corresponding
3630/// pieces from \p Type.
3631static unsigned extractPBaseFlags(ASTContext &Ctx, QualType &Type) {
3632 unsigned Flags = 0;
David Majnemere2cb8d12014-07-07 06:20:47 +00003633
Richard Smitha7d93782016-12-01 03:32:42 +00003634 if (Type.isConstQualified())
3635 Flags |= ItaniumRTTIBuilder::PTI_Const;
3636 if (Type.isVolatileQualified())
3637 Flags |= ItaniumRTTIBuilder::PTI_Volatile;
3638 if (Type.isRestrictQualified())
3639 Flags |= ItaniumRTTIBuilder::PTI_Restrict;
3640 Type = Type.getUnqualifiedType();
David Majnemere2cb8d12014-07-07 06:20:47 +00003641
3642 // Itanium C++ ABI 2.9.5p7:
3643 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
3644 // incomplete class type, the incomplete target type flag is set.
Richard Smitha7d93782016-12-01 03:32:42 +00003645 if (ContainsIncompleteClassType(Type))
3646 Flags |= ItaniumRTTIBuilder::PTI_Incomplete;
3647
3648 if (auto *Proto = Type->getAs<FunctionProtoType>()) {
Richard Smitheaf11ad2018-05-03 03:58:32 +00003649 if (Proto->isNothrow()) {
Richard Smitha7d93782016-12-01 03:32:42 +00003650 Flags |= ItaniumRTTIBuilder::PTI_Noexcept;
Stephan Bergmann8c85bca2018-01-05 07:57:12 +00003651 Type = Ctx.getFunctionTypeWithExceptionSpec(Type, EST_None);
Richard Smitha7d93782016-12-01 03:32:42 +00003652 }
3653 }
3654
3655 return Flags;
3656}
3657
3658/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
3659/// used for pointer types.
3660void ItaniumRTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
3661 // Itanium C++ ABI 2.9.5p7:
3662 // __flags is a flag word describing the cv-qualification and other
3663 // attributes of the type pointed to
3664 unsigned Flags = extractPBaseFlags(CGM.getContext(), PointeeTy);
David Majnemere2cb8d12014-07-07 06:20:47 +00003665
3666 llvm::Type *UnsignedIntLTy =
3667 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
3668 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3669
3670 // Itanium C++ ABI 2.9.5p7:
3671 // __pointee is a pointer to the std::type_info derivation for the
3672 // unqualified type being pointed to.
3673 llvm::Constant *PointeeTypeInfo =
Richard Smitha7d93782016-12-01 03:32:42 +00003674 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(PointeeTy);
David Majnemere2cb8d12014-07-07 06:20:47 +00003675 Fields.push_back(PointeeTypeInfo);
3676}
3677
3678/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
3679/// struct, used for member pointer types.
3680void
3681ItaniumRTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
3682 QualType PointeeTy = Ty->getPointeeType();
3683
David Majnemere2cb8d12014-07-07 06:20:47 +00003684 // Itanium C++ ABI 2.9.5p7:
3685 // __flags is a flag word describing the cv-qualification and other
3686 // attributes of the type pointed to.
Richard Smitha7d93782016-12-01 03:32:42 +00003687 unsigned Flags = extractPBaseFlags(CGM.getContext(), PointeeTy);
David Majnemere2cb8d12014-07-07 06:20:47 +00003688
3689 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
David Majnemere2cb8d12014-07-07 06:20:47 +00003690 if (IsIncompleteClassType(ClassType))
3691 Flags |= PTI_ContainingClassIncomplete;
3692
3693 llvm::Type *UnsignedIntLTy =
3694 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
3695 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3696
3697 // Itanium C++ ABI 2.9.5p7:
3698 // __pointee is a pointer to the std::type_info derivation for the
3699 // unqualified type being pointed to.
3700 llvm::Constant *PointeeTypeInfo =
Richard Smitha7d93782016-12-01 03:32:42 +00003701 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(PointeeTy);
David Majnemere2cb8d12014-07-07 06:20:47 +00003702 Fields.push_back(PointeeTypeInfo);
3703
3704 // Itanium C++ ABI 2.9.5p9:
3705 // __context is a pointer to an abi::__class_type_info corresponding to the
3706 // class type containing the member pointed to
3707 // (e.g., the "A" in "int A::*").
3708 Fields.push_back(
3709 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(QualType(ClassType, 0)));
3710}
3711
David Majnemer443250f2015-03-17 20:35:00 +00003712llvm::Constant *ItaniumCXXABI::getAddrOfRTTIDescriptor(QualType Ty) {
David Majnemere2cb8d12014-07-07 06:20:47 +00003713 return ItaniumRTTIBuilder(*this).BuildTypeInfo(Ty);
3714}
3715
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003716void ItaniumCXXABI::EmitFundamentalRTTIDescriptors(const CXXRecordDecl *RD) {
Richard Smith4a382012016-02-03 01:32:42 +00003717 // Types added here must also be added to TypeInfoIsInStandardLibrary.
David Majnemere2cb8d12014-07-07 06:20:47 +00003718 QualType FundamentalTypes[] = {
3719 getContext().VoidTy, getContext().NullPtrTy,
3720 getContext().BoolTy, getContext().WCharTy,
3721 getContext().CharTy, getContext().UnsignedCharTy,
3722 getContext().SignedCharTy, getContext().ShortTy,
3723 getContext().UnsignedShortTy, getContext().IntTy,
3724 getContext().UnsignedIntTy, getContext().LongTy,
3725 getContext().UnsignedLongTy, getContext().LongLongTy,
Richard Smith4a382012016-02-03 01:32:42 +00003726 getContext().UnsignedLongLongTy, getContext().Int128Ty,
3727 getContext().UnsignedInt128Ty, getContext().HalfTy,
David Majnemere2cb8d12014-07-07 06:20:47 +00003728 getContext().FloatTy, getContext().DoubleTy,
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00003729 getContext().LongDoubleTy, getContext().Float128Ty,
Richard Smith3a8244d2018-05-01 05:02:45 +00003730 getContext().Char8Ty, getContext().Char16Ty,
3731 getContext().Char32Ty
David Majnemere2cb8d12014-07-07 06:20:47 +00003732 };
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003733 llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass =
3734 RD->hasAttr<DLLExportAttr>()
3735 ? llvm::GlobalValue::DLLExportStorageClass
3736 : llvm::GlobalValue::DefaultStorageClass;
3737 llvm::GlobalValue::VisibilityTypes Visibility =
3738 CodeGenModule::GetLLVMVisibility(RD->getVisibility());
3739 for (const QualType &FundamentalType : FundamentalTypes) {
3740 QualType PointerType = getContext().getPointerType(FundamentalType);
3741 QualType PointerTypeConst = getContext().getPointerType(
3742 FundamentalType.withConst());
3743 for (QualType Type : {FundamentalType, PointerType, PointerTypeConst})
3744 ItaniumRTTIBuilder(*this).BuildTypeInfo(
3745 Type, llvm::GlobalValue::ExternalLinkage,
3746 Visibility, DLLStorageClass);
3747 }
David Majnemere2cb8d12014-07-07 06:20:47 +00003748}
3749
3750/// What sort of uniqueness rules should we use for the RTTI for the
3751/// given type?
3752ItaniumCXXABI::RTTIUniquenessKind ItaniumCXXABI::classifyRTTIUniqueness(
3753 QualType CanTy, llvm::GlobalValue::LinkageTypes Linkage) const {
3754 if (shouldRTTIBeUnique())
3755 return RUK_Unique;
3756
3757 // It's only necessary for linkonce_odr or weak_odr linkage.
3758 if (Linkage != llvm::GlobalValue::LinkOnceODRLinkage &&
3759 Linkage != llvm::GlobalValue::WeakODRLinkage)
3760 return RUK_Unique;
3761
3762 // It's only necessary with default visibility.
3763 if (CanTy->getVisibility() != DefaultVisibility)
3764 return RUK_Unique;
3765
3766 // If we're not required to publish this symbol, hide it.
3767 if (Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
3768 return RUK_NonUniqueHidden;
3769
3770 // If we're required to publish this symbol, as we might be under an
3771 // explicit instantiation, leave it with default visibility but
3772 // enable string-comparisons.
3773 assert(Linkage == llvm::GlobalValue::WeakODRLinkage);
3774 return RUK_NonUniqueVisible;
3775}
Rafael Espindola91f68b42014-09-15 19:20:10 +00003776
Rafael Espindola1e4df922014-09-16 15:18:21 +00003777// Find out how to codegen the complete destructor and constructor
3778namespace {
3779enum class StructorCodegen { Emit, RAUW, Alias, COMDAT };
3780}
3781static StructorCodegen getCodegenToUse(CodeGenModule &CGM,
3782 const CXXMethodDecl *MD) {
3783 if (!CGM.getCodeGenOpts().CXXCtorDtorAliases)
3784 return StructorCodegen::Emit;
Rafael Espindola91f68b42014-09-15 19:20:10 +00003785
Rafael Espindola1e4df922014-09-16 15:18:21 +00003786 // The complete and base structors are not equivalent if there are any virtual
3787 // bases, so emit separate functions.
3788 if (MD->getParent()->getNumVBases())
3789 return StructorCodegen::Emit;
3790
3791 GlobalDecl AliasDecl;
3792 if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) {
3793 AliasDecl = GlobalDecl(DD, Dtor_Complete);
3794 } else {
3795 const auto *CD = cast<CXXConstructorDecl>(MD);
3796 AliasDecl = GlobalDecl(CD, Ctor_Complete);
3797 }
3798 llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl);
3799
Chandler Carruth1f82d9b2018-07-29 03:05:07 +00003800 if (llvm::GlobalValue::isDiscardableIfUnused(Linkage))
3801 return StructorCodegen::RAUW;
Rafael Espindola1e4df922014-09-16 15:18:21 +00003802
Pavel Labathc370f262018-05-14 11:35:44 +00003803 // FIXME: Should we allow available_externally aliases?
Chandler Carruth1f82d9b2018-07-29 03:05:07 +00003804 if (!llvm::GlobalAlias::isValidLinkage(Linkage))
3805 return StructorCodegen::RAUW;
Rafael Espindola1e4df922014-09-16 15:18:21 +00003806
Rafael Espindola0806f982014-09-16 20:19:43 +00003807 if (llvm::GlobalValue::isWeakForLinker(Linkage)) {
Dan Gohman839f2152017-01-17 21:46:38 +00003808 // Only ELF and wasm support COMDATs with arbitrary names (C5/D5).
3809 if (CGM.getTarget().getTriple().isOSBinFormatELF() ||
3810 CGM.getTarget().getTriple().isOSBinFormatWasm())
Rafael Espindola0806f982014-09-16 20:19:43 +00003811 return StructorCodegen::COMDAT;
3812 return StructorCodegen::Emit;
3813 }
Rafael Espindola1e4df922014-09-16 15:18:21 +00003814
3815 return StructorCodegen::Alias;
Rafael Espindola91f68b42014-09-15 19:20:10 +00003816}
3817
Rafael Espindola1e4df922014-09-16 15:18:21 +00003818static void emitConstructorDestructorAlias(CodeGenModule &CGM,
3819 GlobalDecl AliasDecl,
3820 GlobalDecl TargetDecl) {
3821 llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl);
3822
3823 StringRef MangledName = CGM.getMangledName(AliasDecl);
3824 llvm::GlobalValue *Entry = CGM.GetGlobalValue(MangledName);
3825 if (Entry && !Entry->isDeclaration())
3826 return;
3827
3828 auto *Aliasee = cast<llvm::GlobalValue>(CGM.GetAddrOfGlobal(TargetDecl));
Rafael Espindola1e4df922014-09-16 15:18:21 +00003829
3830 // Create the alias with no name.
David Blaikie2a791d72015-09-14 18:38:22 +00003831 auto *Alias = llvm::GlobalAlias::create(Linkage, "", Aliasee);
Rafael Espindola1e4df922014-09-16 15:18:21 +00003832
Peter Collingbourned914fd22018-06-18 20:58:54 +00003833 // Constructors and destructors are always unnamed_addr.
3834 Alias->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3835
Rafael Espindola1e4df922014-09-16 15:18:21 +00003836 // Switch any previous uses to the alias.
3837 if (Entry) {
NAKAMURA Takumie9621042015-09-15 01:39:27 +00003838 assert(Entry->getType() == Aliasee->getType() &&
Rafael Espindola1e4df922014-09-16 15:18:21 +00003839 "declaration exists with different type");
3840 Alias->takeName(Entry);
3841 Entry->replaceAllUsesWith(Alias);
3842 Entry->eraseFromParent();
3843 } else {
3844 Alias->setName(MangledName);
3845 }
3846
3847 // Finally, set up the alias with its proper name and attributes.
Rafael Espindolab7350042018-03-01 00:35:47 +00003848 CGM.SetCommonAttributes(AliasDecl, Alias);
Rafael Espindola1e4df922014-09-16 15:18:21 +00003849}
3850
3851void ItaniumCXXABI::emitCXXStructor(const CXXMethodDecl *MD,
3852 StructorType Type) {
3853 auto *CD = dyn_cast<CXXConstructorDecl>(MD);
3854 const CXXDestructorDecl *DD = CD ? nullptr : cast<CXXDestructorDecl>(MD);
3855
3856 StructorCodegen CGType = getCodegenToUse(CGM, MD);
3857
3858 if (Type == StructorType::Complete) {
3859 GlobalDecl CompleteDecl;
3860 GlobalDecl BaseDecl;
3861 if (CD) {
3862 CompleteDecl = GlobalDecl(CD, Ctor_Complete);
3863 BaseDecl = GlobalDecl(CD, Ctor_Base);
3864 } else {
3865 CompleteDecl = GlobalDecl(DD, Dtor_Complete);
3866 BaseDecl = GlobalDecl(DD, Dtor_Base);
3867 }
3868
3869 if (CGType == StructorCodegen::Alias || CGType == StructorCodegen::COMDAT) {
3870 emitConstructorDestructorAlias(CGM, CompleteDecl, BaseDecl);
3871 return;
3872 }
3873
3874 if (CGType == StructorCodegen::RAUW) {
3875 StringRef MangledName = CGM.getMangledName(CompleteDecl);
Andrey Bokhankocab58582015-08-31 13:20:44 +00003876 auto *Aliasee = CGM.GetAddrOfGlobal(BaseDecl);
Rafael Espindola1e4df922014-09-16 15:18:21 +00003877 CGM.addReplacement(MangledName, Aliasee);
3878 return;
Rafael Espindola91f68b42014-09-15 19:20:10 +00003879 }
3880 }
3881
3882 // The base destructor is equivalent to the base destructor of its
3883 // base class if there is exactly one non-virtual base class with a
3884 // non-trivial destructor, there are no fields with a non-trivial
3885 // destructor, and the body of the destructor is trivial.
Rafael Espindola1e4df922014-09-16 15:18:21 +00003886 if (DD && Type == StructorType::Base && CGType != StructorCodegen::COMDAT &&
3887 !CGM.TryEmitBaseDestructorAsAlias(DD))
Rafael Espindola91f68b42014-09-15 19:20:10 +00003888 return;
3889
Richard Smith5b349582017-10-13 01:55:36 +00003890 // FIXME: The deleting destructor is equivalent to the selected operator
3891 // delete if:
3892 // * either the delete is a destroying operator delete or the destructor
3893 // would be trivial if it weren't virtual,
3894 // * the conversion from the 'this' parameter to the first parameter of the
3895 // destructor is equivalent to a bitcast,
3896 // * the destructor does not have an implicit "this" return, and
3897 // * the operator delete has the same calling convention and IR function type
3898 // as the destructor.
3899 // In such cases we should try to emit the deleting dtor as an alias to the
3900 // selected 'operator delete'.
3901
Rafael Espindola1e4df922014-09-16 15:18:21 +00003902 llvm::Function *Fn = CGM.codegenCXXStructor(MD, Type);
Rafael Espindola91f68b42014-09-15 19:20:10 +00003903
Rafael Espindola1e4df922014-09-16 15:18:21 +00003904 if (CGType == StructorCodegen::COMDAT) {
3905 SmallString<256> Buffer;
3906 llvm::raw_svector_ostream Out(Buffer);
3907 if (DD)
3908 getMangleContext().mangleCXXDtorComdat(DD, Out);
3909 else
3910 getMangleContext().mangleCXXCtorComdat(CD, Out);
3911 llvm::Comdat *C = CGM.getModule().getOrInsertComdat(Out.str());
3912 Fn->setComdat(C);
Rafael Espindoladbee8a72015-01-15 21:36:08 +00003913 } else {
3914 CGM.maybeSetTrivialComdat(*MD, *Fn);
Rafael Espindola91f68b42014-09-15 19:20:10 +00003915 }
Rafael Espindola91f68b42014-09-15 19:20:10 +00003916}
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00003917
3918static llvm::Constant *getBeginCatchFn(CodeGenModule &CGM) {
3919 // void *__cxa_begin_catch(void*);
3920 llvm::FunctionType *FTy = llvm::FunctionType::get(
3921 CGM.Int8PtrTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
3922
3923 return CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
3924}
3925
3926static llvm::Constant *getEndCatchFn(CodeGenModule &CGM) {
3927 // void __cxa_end_catch();
3928 llvm::FunctionType *FTy =
3929 llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false);
3930
3931 return CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch");
3932}
3933
3934static llvm::Constant *getGetExceptionPtrFn(CodeGenModule &CGM) {
3935 // void *__cxa_get_exception_ptr(void*);
3936 llvm::FunctionType *FTy = llvm::FunctionType::get(
3937 CGM.Int8PtrTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
3938
3939 return CGM.CreateRuntimeFunction(FTy, "__cxa_get_exception_ptr");
3940}
3941
3942namespace {
3943 /// A cleanup to call __cxa_end_catch. In many cases, the caught
3944 /// exception type lets us state definitively that the thrown exception
3945 /// type does not have a destructor. In particular:
3946 /// - Catch-alls tell us nothing, so we have to conservatively
3947 /// assume that the thrown exception might have a destructor.
3948 /// - Catches by reference behave according to their base types.
3949 /// - Catches of non-record types will only trigger for exceptions
3950 /// of non-record types, which never have destructors.
3951 /// - Catches of record types can trigger for arbitrary subclasses
3952 /// of the caught type, so we have to assume the actual thrown
3953 /// exception type might have a throwing destructor, even if the
3954 /// caught type's destructor is trivial or nothrow.
David Blaikie7e70d682015-08-18 22:40:54 +00003955 struct CallEndCatch final : EHScopeStack::Cleanup {
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00003956 CallEndCatch(bool MightThrow) : MightThrow(MightThrow) {}
3957 bool MightThrow;
3958
3959 void Emit(CodeGenFunction &CGF, Flags flags) override {
3960 if (!MightThrow) {
3961 CGF.EmitNounwindRuntimeCall(getEndCatchFn(CGF.CGM));
3962 return;
3963 }
3964
3965 CGF.EmitRuntimeCallOrInvoke(getEndCatchFn(CGF.CGM));
3966 }
3967 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00003968}
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00003969
3970/// Emits a call to __cxa_begin_catch and enters a cleanup to call
3971/// __cxa_end_catch.
3972///
3973/// \param EndMightThrow - true if __cxa_end_catch might throw
3974static llvm::Value *CallBeginCatch(CodeGenFunction &CGF,
3975 llvm::Value *Exn,
3976 bool EndMightThrow) {
3977 llvm::CallInst *call =
3978 CGF.EmitNounwindRuntimeCall(getBeginCatchFn(CGF.CGM), Exn);
3979
3980 CGF.EHStack.pushCleanup<CallEndCatch>(NormalAndEHCleanup, EndMightThrow);
3981
3982 return call;
3983}
3984
3985/// A "special initializer" callback for initializing a catch
3986/// parameter during catch initialization.
3987static void InitCatchParam(CodeGenFunction &CGF,
3988 const VarDecl &CatchParam,
John McCall7f416cc2015-09-08 08:05:57 +00003989 Address ParamAddr,
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00003990 SourceLocation Loc) {
3991 // Load the exception from where the landing pad saved it.
3992 llvm::Value *Exn = CGF.getExceptionFromSlot();
3993
3994 CanQualType CatchType =
3995 CGF.CGM.getContext().getCanonicalType(CatchParam.getType());
3996 llvm::Type *LLVMCatchTy = CGF.ConvertTypeForMem(CatchType);
3997
3998 // If we're catching by reference, we can just cast the object
3999 // pointer to the appropriate pointer.
4000 if (isa<ReferenceType>(CatchType)) {
4001 QualType CaughtType = cast<ReferenceType>(CatchType)->getPointeeType();
4002 bool EndCatchMightThrow = CaughtType->isRecordType();
4003
4004 // __cxa_begin_catch returns the adjusted object pointer.
4005 llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, EndCatchMightThrow);
4006
4007 // We have no way to tell the personality function that we're
4008 // catching by reference, so if we're catching a pointer,
4009 // __cxa_begin_catch will actually return that pointer by value.
4010 if (const PointerType *PT = dyn_cast<PointerType>(CaughtType)) {
4011 QualType PointeeType = PT->getPointeeType();
4012
4013 // When catching by reference, generally we should just ignore
4014 // this by-value pointer and use the exception object instead.
4015 if (!PointeeType->isRecordType()) {
4016
4017 // Exn points to the struct _Unwind_Exception header, which
4018 // we have to skip past in order to reach the exception data.
4019 unsigned HeaderSize =
4020 CGF.CGM.getTargetCodeGenInfo().getSizeOfUnwindException();
4021 AdjustedExn = CGF.Builder.CreateConstGEP1_32(Exn, HeaderSize);
4022
4023 // However, if we're catching a pointer-to-record type that won't
4024 // work, because the personality function might have adjusted
4025 // the pointer. There's actually no way for us to fully satisfy
4026 // the language/ABI contract here: we can't use Exn because it
4027 // might have the wrong adjustment, but we can't use the by-value
4028 // pointer because it's off by a level of abstraction.
4029 //
4030 // The current solution is to dump the adjusted pointer into an
4031 // alloca, which breaks language semantics (because changing the
4032 // pointer doesn't change the exception) but at least works.
4033 // The better solution would be to filter out non-exact matches
4034 // and rethrow them, but this is tricky because the rethrow
4035 // really needs to be catchable by other sites at this landing
4036 // pad. The best solution is to fix the personality function.
4037 } else {
4038 // Pull the pointer for the reference type off.
4039 llvm::Type *PtrTy =
4040 cast<llvm::PointerType>(LLVMCatchTy)->getElementType();
4041
4042 // Create the temporary and write the adjusted pointer into it.
John McCall7f416cc2015-09-08 08:05:57 +00004043 Address ExnPtrTmp =
4044 CGF.CreateTempAlloca(PtrTy, CGF.getPointerAlign(), "exn.byref.tmp");
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004045 llvm::Value *Casted = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
4046 CGF.Builder.CreateStore(Casted, ExnPtrTmp);
4047
4048 // Bind the reference to the temporary.
John McCall7f416cc2015-09-08 08:05:57 +00004049 AdjustedExn = ExnPtrTmp.getPointer();
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004050 }
4051 }
4052
4053 llvm::Value *ExnCast =
4054 CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.byref");
4055 CGF.Builder.CreateStore(ExnCast, ParamAddr);
4056 return;
4057 }
4058
4059 // Scalars and complexes.
4060 TypeEvaluationKind TEK = CGF.getEvaluationKind(CatchType);
4061 if (TEK != TEK_Aggregate) {
4062 llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, false);
4063
4064 // If the catch type is a pointer type, __cxa_begin_catch returns
4065 // the pointer by value.
4066 if (CatchType->hasPointerRepresentation()) {
4067 llvm::Value *CastExn =
4068 CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.casted");
4069
4070 switch (CatchType.getQualifiers().getObjCLifetime()) {
4071 case Qualifiers::OCL_Strong:
4072 CastExn = CGF.EmitARCRetainNonBlock(CastExn);
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +00004073 LLVM_FALLTHROUGH;
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004074
4075 case Qualifiers::OCL_None:
4076 case Qualifiers::OCL_ExplicitNone:
4077 case Qualifiers::OCL_Autoreleasing:
4078 CGF.Builder.CreateStore(CastExn, ParamAddr);
4079 return;
4080
4081 case Qualifiers::OCL_Weak:
4082 CGF.EmitARCInitWeak(ParamAddr, CastExn);
4083 return;
4084 }
4085 llvm_unreachable("bad ownership qualifier!");
4086 }
4087
4088 // Otherwise, it returns a pointer into the exception object.
4089
4090 llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
4091 llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
4092
4093 LValue srcLV = CGF.MakeNaturalAlignAddrLValue(Cast, CatchType);
John McCall7f416cc2015-09-08 08:05:57 +00004094 LValue destLV = CGF.MakeAddrLValue(ParamAddr, CatchType);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004095 switch (TEK) {
4096 case TEK_Complex:
4097 CGF.EmitStoreOfComplex(CGF.EmitLoadOfComplex(srcLV, Loc), destLV,
4098 /*init*/ true);
4099 return;
4100 case TEK_Scalar: {
4101 llvm::Value *ExnLoad = CGF.EmitLoadOfScalar(srcLV, Loc);
4102 CGF.EmitStoreOfScalar(ExnLoad, destLV, /*init*/ true);
4103 return;
4104 }
4105 case TEK_Aggregate:
4106 llvm_unreachable("evaluation kind filtered out!");
4107 }
4108 llvm_unreachable("bad evaluation kind");
4109 }
4110
4111 assert(isa<RecordType>(CatchType) && "unexpected catch type!");
John McCall7f416cc2015-09-08 08:05:57 +00004112 auto catchRD = CatchType->getAsCXXRecordDecl();
4113 CharUnits caughtExnAlignment = CGF.CGM.getClassPointerAlignment(catchRD);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004114
4115 llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
4116
4117 // Check for a copy expression. If we don't have a copy expression,
4118 // that means a trivial copy is okay.
4119 const Expr *copyExpr = CatchParam.getInit();
4120 if (!copyExpr) {
4121 llvm::Value *rawAdjustedExn = CallBeginCatch(CGF, Exn, true);
John McCall7f416cc2015-09-08 08:05:57 +00004122 Address adjustedExn(CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy),
4123 caughtExnAlignment);
Ivan A. Kosarev1860b522018-01-25 14:21:55 +00004124 LValue Dest = CGF.MakeAddrLValue(ParamAddr, CatchType);
4125 LValue Src = CGF.MakeAddrLValue(adjustedExn, CatchType);
Richard Smithe78fac52018-04-05 20:52:58 +00004126 CGF.EmitAggregateCopy(Dest, Src, CatchType, AggValueSlot::DoesNotOverlap);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004127 return;
4128 }
4129
4130 // We have to call __cxa_get_exception_ptr to get the adjusted
4131 // pointer before copying.
4132 llvm::CallInst *rawAdjustedExn =
4133 CGF.EmitNounwindRuntimeCall(getGetExceptionPtrFn(CGF.CGM), Exn);
4134
4135 // Cast that to the appropriate type.
John McCall7f416cc2015-09-08 08:05:57 +00004136 Address adjustedExn(CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy),
4137 caughtExnAlignment);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004138
4139 // The copy expression is defined in terms of an OpaqueValueExpr.
4140 // Find it and map it to the adjusted expression.
4141 CodeGenFunction::OpaqueValueMapping
4142 opaque(CGF, OpaqueValueExpr::findInCopyConstruct(copyExpr),
4143 CGF.MakeAddrLValue(adjustedExn, CatchParam.getType()));
4144
4145 // Call the copy ctor in a terminate scope.
4146 CGF.EHStack.pushTerminate();
4147
4148 // Perform the copy construction.
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004149 CGF.EmitAggExpr(copyExpr,
John McCall7f416cc2015-09-08 08:05:57 +00004150 AggValueSlot::forAddr(ParamAddr, Qualifiers(),
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004151 AggValueSlot::IsNotDestructed,
4152 AggValueSlot::DoesNotNeedGCBarriers,
Richard Smithe78fac52018-04-05 20:52:58 +00004153 AggValueSlot::IsNotAliased,
4154 AggValueSlot::DoesNotOverlap));
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004155
4156 // Leave the terminate scope.
4157 CGF.EHStack.popTerminate();
4158
4159 // Undo the opaque value mapping.
4160 opaque.pop();
4161
4162 // Finally we can call __cxa_begin_catch.
4163 CallBeginCatch(CGF, Exn, true);
4164}
4165
4166/// Begins a catch statement by initializing the catch variable and
4167/// calling __cxa_begin_catch.
4168void ItaniumCXXABI::emitBeginCatch(CodeGenFunction &CGF,
4169 const CXXCatchStmt *S) {
4170 // We have to be very careful with the ordering of cleanups here:
4171 // C++ [except.throw]p4:
4172 // The destruction [of the exception temporary] occurs
4173 // immediately after the destruction of the object declared in
4174 // the exception-declaration in the handler.
4175 //
4176 // So the precise ordering is:
4177 // 1. Construct catch variable.
4178 // 2. __cxa_begin_catch
4179 // 3. Enter __cxa_end_catch cleanup
4180 // 4. Enter dtor cleanup
4181 //
4182 // We do this by using a slightly abnormal initialization process.
4183 // Delegation sequence:
4184 // - ExitCXXTryStmt opens a RunCleanupsScope
4185 // - EmitAutoVarAlloca creates the variable and debug info
4186 // - InitCatchParam initializes the variable from the exception
4187 // - CallBeginCatch calls __cxa_begin_catch
4188 // - CallBeginCatch enters the __cxa_end_catch cleanup
4189 // - EmitAutoVarCleanups enters the variable destructor cleanup
4190 // - EmitCXXTryStmt emits the code for the catch body
4191 // - EmitCXXTryStmt close the RunCleanupsScope
4192
4193 VarDecl *CatchParam = S->getExceptionDecl();
4194 if (!CatchParam) {
4195 llvm::Value *Exn = CGF.getExceptionFromSlot();
4196 CallBeginCatch(CGF, Exn, true);
4197 return;
4198 }
4199
4200 // Emit the local.
4201 CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004202 InitCatchParam(CGF, *CatchParam, var.getObjectAddress(CGF), S->getBeginLoc());
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004203 CGF.EmitAutoVarCleanups(var);
4204}
4205
4206/// Get or define the following function:
4207/// void @__clang_call_terminate(i8* %exn) nounwind noreturn
4208/// This code is used only in C++.
4209static llvm::Constant *getClangCallTerminateFn(CodeGenModule &CGM) {
4210 llvm::FunctionType *fnTy =
4211 llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
Reid Klecknerde864822017-03-21 16:57:30 +00004212 llvm::Constant *fnRef = CGM.CreateRuntimeFunction(
4213 fnTy, "__clang_call_terminate", llvm::AttributeList(), /*Local=*/true);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004214
4215 llvm::Function *fn = dyn_cast<llvm::Function>(fnRef);
4216 if (fn && fn->empty()) {
4217 fn->setDoesNotThrow();
4218 fn->setDoesNotReturn();
4219
4220 // What we really want is to massively penalize inlining without
4221 // forbidding it completely. The difference between that and
4222 // 'noinline' is negligible.
4223 fn->addFnAttr(llvm::Attribute::NoInline);
4224
4225 // Allow this function to be shared across translation units, but
4226 // we don't want it to turn into an exported symbol.
4227 fn->setLinkage(llvm::Function::LinkOnceODRLinkage);
4228 fn->setVisibility(llvm::Function::HiddenVisibility);
NAKAMURA Takumic7da6da2015-05-09 21:10:07 +00004229 if (CGM.supportsCOMDAT())
4230 fn->setComdat(CGM.getModule().getOrInsertComdat(fn->getName()));
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004231
4232 // Set up the function.
4233 llvm::BasicBlock *entry =
4234 llvm::BasicBlock::Create(CGM.getLLVMContext(), "", fn);
John McCall7f416cc2015-09-08 08:05:57 +00004235 CGBuilderTy builder(CGM, entry);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004236
4237 // Pull the exception pointer out of the parameter list.
4238 llvm::Value *exn = &*fn->arg_begin();
4239
4240 // Call __cxa_begin_catch(exn).
4241 llvm::CallInst *catchCall = builder.CreateCall(getBeginCatchFn(CGM), exn);
4242 catchCall->setDoesNotThrow();
4243 catchCall->setCallingConv(CGM.getRuntimeCC());
4244
4245 // Call std::terminate().
David Blaikie4ba525b2015-07-14 17:27:39 +00004246 llvm::CallInst *termCall = builder.CreateCall(CGM.getTerminateFn());
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004247 termCall->setDoesNotThrow();
4248 termCall->setDoesNotReturn();
4249 termCall->setCallingConv(CGM.getRuntimeCC());
4250
4251 // std::terminate cannot return.
4252 builder.CreateUnreachable();
4253 }
4254
4255 return fnRef;
4256}
4257
4258llvm::CallInst *
4259ItaniumCXXABI::emitTerminateForUnexpectedException(CodeGenFunction &CGF,
4260 llvm::Value *Exn) {
4261 // In C++, we want to call __cxa_begin_catch() before terminating.
4262 if (Exn) {
4263 assert(CGF.CGM.getLangOpts().CPlusPlus);
4264 return CGF.EmitNounwindRuntimeCall(getClangCallTerminateFn(CGF.CGM), Exn);
4265 }
4266 return CGF.EmitNounwindRuntimeCall(CGF.CGM.getTerminateFn());
4267}
Peter Collingbourne60108802017-12-13 21:53:04 +00004268
4269std::pair<llvm::Value *, const CXXRecordDecl *>
4270ItaniumCXXABI::LoadVTablePtr(CodeGenFunction &CGF, Address This,
4271 const CXXRecordDecl *RD) {
4272 return {CGF.GetVTablePtr(This, CGM.Int8PtrTy, RD), RD};
4273}
Heejin Ahnc6479192018-05-31 22:18:13 +00004274
4275void WebAssemblyCXXABI::emitBeginCatch(CodeGenFunction &CGF,
4276 const CXXCatchStmt *C) {
Heejin Ahn1eb074d2018-06-01 01:01:37 +00004277 if (CGF.getTarget().hasFeature("exception-handling"))
4278 CGF.EHStack.pushCleanup<CatchRetScope>(
4279 NormalCleanup, cast<llvm::CatchPadInst>(CGF.CurrentFuncletPad));
Heejin Ahnc6479192018-05-31 22:18:13 +00004280 ItaniumCXXABI::emitBeginCatch(CGF, C);
4281}