blob: 43affc6cf5534beae7487b238ddf676fb73baf18 [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"
David Majnemer1162d252014-06-22 19:05:33 +000031#include "llvm/IR/CallSite.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000032#include "llvm/IR/DataLayout.h"
Thomas Andersonb6d87cf2018-07-24 00:43:47 +000033#include "llvm/IR/GlobalValue.h"
Reid Klecknerfff8e7f2015-03-03 19:21:04 +000034#include "llvm/IR/Instructions.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000035#include "llvm/IR/Intrinsics.h"
36#include "llvm/IR/Value.h"
Akira Hatanaka617e2612018-04-17 18:41:52 +000037#include "llvm/Support/ScopedPrinter.h"
Charles Davis4e786dd2010-05-25 19:52:27 +000038
39using namespace clang;
John McCall475999d2010-08-22 00:05:51 +000040using namespace CodeGen;
Charles Davis4e786dd2010-05-25 19:52:27 +000041
42namespace {
Charles Davis53c59df2010-08-16 03:33:14 +000043class ItaniumCXXABI : public CodeGen::CGCXXABI {
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +000044 /// VTables - All the vtables which have been defined.
45 llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> VTables;
46
John McCall475999d2010-08-22 00:05:51 +000047protected:
Mark Seabornedf0d382013-07-24 16:25:13 +000048 bool UseARMMethodPtrABI;
49 bool UseARMGuardVarABI;
John McCalld23b27e2016-09-16 02:40:45 +000050 bool Use32BitVTableOffsetABI;
John McCall7a9aac22010-08-23 01:21:21 +000051
Timur Iskhodzhanov67455222013-10-03 06:26:13 +000052 ItaniumMangleContext &getMangleContext() {
53 return cast<ItaniumMangleContext>(CodeGen::CGCXXABI::getMangleContext());
54 }
55
Charles Davis4e786dd2010-05-25 19:52:27 +000056public:
Mark Seabornedf0d382013-07-24 16:25:13 +000057 ItaniumCXXABI(CodeGen::CodeGenModule &CGM,
58 bool UseARMMethodPtrABI = false,
59 bool UseARMGuardVarABI = false) :
60 CGCXXABI(CGM), UseARMMethodPtrABI(UseARMMethodPtrABI),
John McCalld23b27e2016-09-16 02:40:45 +000061 UseARMGuardVarABI(UseARMGuardVarABI),
Richard Smithb17d6fa2016-12-01 03:04:07 +000062 Use32BitVTableOffsetABI(false) { }
John McCall475999d2010-08-22 00:05:51 +000063
Reid Kleckner40ca9132014-05-13 22:05:45 +000064 bool classifyReturnType(CGFunctionInfo &FI) const override;
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000065
Richard Smithf667ad52017-08-26 01:04:35 +000066 bool passClassIndirect(const CXXRecordDecl *RD) const {
Richard Smithf667ad52017-08-26 01:04:35 +000067 return !canCopyArgument(RD);
68 }
69
Craig Topper4f12f102014-03-12 06:41:41 +000070 RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const override {
Richard Smith96cd6712017-08-16 01:49:53 +000071 // If C++ prohibits us from making a copy, pass by address.
Richard Smithf667ad52017-08-26 01:04:35 +000072 if (passClassIndirect(RD))
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000073 return RAA_Indirect;
74 return RAA_Default;
75 }
76
John McCall7f416cc2015-09-08 08:05:57 +000077 bool isThisCompleteObject(GlobalDecl GD) const override {
78 // The Itanium ABI has separate complete-object vs. base-object
79 // variants of both constructors and destructors.
80 if (isa<CXXDestructorDecl>(GD.getDecl())) {
81 switch (GD.getDtorType()) {
82 case Dtor_Complete:
83 case Dtor_Deleting:
84 return true;
85
86 case Dtor_Base:
87 return false;
88
89 case Dtor_Comdat:
90 llvm_unreachable("emitting dtor comdat as function?");
91 }
92 llvm_unreachable("bad dtor kind");
93 }
94 if (isa<CXXConstructorDecl>(GD.getDecl())) {
95 switch (GD.getCtorType()) {
96 case Ctor_Complete:
97 return true;
98
99 case Ctor_Base:
100 return false;
101
102 case Ctor_CopyingClosure:
103 case Ctor_DefaultClosure:
104 llvm_unreachable("closure ctors in Itanium ABI?");
105
106 case Ctor_Comdat:
107 llvm_unreachable("emitting ctor comdat as function?");
108 }
109 llvm_unreachable("bad dtor kind");
110 }
111
112 // No other kinds.
113 return false;
114 }
115
Craig Topper4f12f102014-03-12 06:41:41 +0000116 bool isZeroInitializable(const MemberPointerType *MPT) override;
John McCall84fa5102010-08-22 04:16:24 +0000117
Craig Topper4f12f102014-03-12 06:41:41 +0000118 llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT) override;
John McCall7a9aac22010-08-23 01:21:21 +0000119
John McCallb92ab1a2016-10-26 23:46:34 +0000120 CGCallee
Craig Topper4f12f102014-03-12 06:41:41 +0000121 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
122 const Expr *E,
John McCall7f416cc2015-09-08 08:05:57 +0000123 Address This,
124 llvm::Value *&ThisPtrForCall,
Craig Topper4f12f102014-03-12 06:41:41 +0000125 llvm::Value *MemFnPtr,
126 const MemberPointerType *MPT) override;
John McCalla8bbb822010-08-22 03:04:22 +0000127
Craig Topper4f12f102014-03-12 06:41:41 +0000128 llvm::Value *
129 EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
John McCall7f416cc2015-09-08 08:05:57 +0000130 Address Base,
Craig Topper4f12f102014-03-12 06:41:41 +0000131 llvm::Value *MemPtr,
132 const MemberPointerType *MPT) override;
John McCallc134eb52010-08-31 21:07:20 +0000133
John McCall7a9aac22010-08-23 01:21:21 +0000134 llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
135 const CastExpr *E,
Craig Topper4f12f102014-03-12 06:41:41 +0000136 llvm::Value *Src) override;
John McCallc62bb392012-02-15 01:22:51 +0000137 llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
Craig Topper4f12f102014-03-12 06:41:41 +0000138 llvm::Constant *Src) override;
John McCall84fa5102010-08-22 04:16:24 +0000139
Craig Topper4f12f102014-03-12 06:41:41 +0000140 llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT) override;
John McCall84fa5102010-08-22 04:16:24 +0000141
David Majnemere2be95b2015-06-23 07:31:01 +0000142 llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD) override;
John McCallf3a88602011-02-03 08:15:49 +0000143 llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
Craig Topper4f12f102014-03-12 06:41:41 +0000144 CharUnits offset) override;
145 llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT) override;
Richard Smithdafff942012-01-14 04:30:29 +0000146 llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD,
147 CharUnits ThisAdjustment);
John McCall1c456c82010-08-22 06:43:33 +0000148
John McCall7a9aac22010-08-23 01:21:21 +0000149 llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +0000150 llvm::Value *L, llvm::Value *R,
John McCall7a9aac22010-08-23 01:21:21 +0000151 const MemberPointerType *MPT,
Craig Topper4f12f102014-03-12 06:41:41 +0000152 bool Inequality) override;
John McCall131d97d2010-08-22 08:30:07 +0000153
John McCall7a9aac22010-08-23 01:21:21 +0000154 llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +0000155 llvm::Value *Addr,
156 const MemberPointerType *MPT) override;
John McCall5d865c322010-08-31 07:33:07 +0000157
David Majnemer08681372014-11-01 07:37:17 +0000158 void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE,
John McCall7f416cc2015-09-08 08:05:57 +0000159 Address Ptr, QualType ElementType,
David Majnemer0c0b6d92014-10-31 20:09:12 +0000160 const CXXDestructorDecl *Dtor) override;
John McCall82fb8922012-09-25 10:10:39 +0000161
Akira Hatanakac47fcf02017-07-27 18:52:44 +0000162 /// Itanium says that an _Unwind_Exception has to be "double-word"
163 /// aligned (and thus the end of it is also so-aligned), meaning 16
164 /// bytes. Of course, that was written for the actual Itanium,
165 /// which is a 64-bit platform. Classically, the ABI doesn't really
166 /// specify the alignment on other platforms, but in practice
167 /// libUnwind declares the struct with __attribute__((aligned)), so
168 /// we assume that alignment here. (It's generally 16 bytes, but
169 /// some targets overwrite it.)
John McCall7f416cc2015-09-08 08:05:57 +0000170 CharUnits getAlignmentOfExnObject() {
Akira Hatanakac47fcf02017-07-27 18:52:44 +0000171 auto align = CGM.getContext().getTargetDefaultAlignForAttributeAligned();
172 return CGM.getContext().toCharUnitsFromBits(align);
John McCall7f416cc2015-09-08 08:05:57 +0000173 }
174
David Majnemer442d0a22014-11-25 07:20:20 +0000175 void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override;
David Majnemer7c237072015-03-05 00:46:22 +0000176 void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) override;
David Majnemer442d0a22014-11-25 07:20:20 +0000177
Reid Klecknerfff8e7f2015-03-03 19:21:04 +0000178 void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) override;
179
180 llvm::CallInst *
181 emitTerminateForUnexpectedException(CodeGenFunction &CGF,
182 llvm::Value *Exn) override;
183
Thomas Andersonb6d87cf2018-07-24 00:43:47 +0000184 void EmitFundamentalRTTIDescriptors(const CXXRecordDecl *RD);
David Majnemer443250f2015-03-17 20:35:00 +0000185 llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override;
Reid Kleckner10aa7702015-09-16 20:15:55 +0000186 CatchTypeInfo
David Majnemer37b417f2015-03-29 21:55:10 +0000187 getAddrOfCXXCatchHandlerType(QualType Ty,
188 QualType CatchHandlerType) override {
Reid Kleckner10aa7702015-09-16 20:15:55 +0000189 return CatchTypeInfo{getAddrOfRTTIDescriptor(Ty), 0};
David Majnemer443250f2015-03-17 20:35:00 +0000190 }
David Majnemere2cb8d12014-07-07 06:20:47 +0000191
David Majnemer1162d252014-06-22 19:05:33 +0000192 bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override;
193 void EmitBadTypeidCall(CodeGenFunction &CGF) override;
194 llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy,
John McCall7f416cc2015-09-08 08:05:57 +0000195 Address ThisPtr,
David Majnemer1162d252014-06-22 19:05:33 +0000196 llvm::Type *StdTypeInfoPtrTy) override;
197
198 bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
199 QualType SrcRecordTy) override;
200
John McCall7f416cc2015-09-08 08:05:57 +0000201 llvm::Value *EmitDynamicCastCall(CodeGenFunction &CGF, Address Value,
David Majnemer1162d252014-06-22 19:05:33 +0000202 QualType SrcRecordTy, QualType DestTy,
203 QualType DestRecordTy,
204 llvm::BasicBlock *CastEnd) override;
205
John McCall7f416cc2015-09-08 08:05:57 +0000206 llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, Address Value,
David Majnemer1162d252014-06-22 19:05:33 +0000207 QualType SrcRecordTy,
208 QualType DestTy) override;
209
210 bool EmitBadCastCall(CodeGenFunction &CGF) override;
211
Craig Topper4f12f102014-03-12 06:41:41 +0000212 llvm::Value *
John McCall7f416cc2015-09-08 08:05:57 +0000213 GetVirtualBaseClassOffset(CodeGenFunction &CGF, Address This,
Craig Topper4f12f102014-03-12 06:41:41 +0000214 const CXXRecordDecl *ClassDecl,
215 const CXXRecordDecl *BaseClassDecl) override;
Reid Klecknerd8cbeec2013-05-29 18:02:47 +0000216
Craig Topper4f12f102014-03-12 06:41:41 +0000217 void EmitCXXConstructors(const CXXConstructorDecl *D) override;
Timur Iskhodzhanov40f2fa92013-08-04 17:30:04 +0000218
George Burgess IVf203dbf2017-02-22 20:28:02 +0000219 AddedStructorArgs
220 buildStructorSignature(const CXXMethodDecl *MD, StructorType T,
221 SmallVectorImpl<CanQualType> &ArgTys) override;
John McCall5d865c322010-08-31 07:33:07 +0000222
Reid Klecknere7de47e2013-07-22 13:51:44 +0000223 bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
Craig Topper4f12f102014-03-12 06:41:41 +0000224 CXXDtorType DT) const override {
Reid Klecknere7de47e2013-07-22 13:51:44 +0000225 // Itanium does not emit any destructor variant as an inline thunk.
226 // Delegating may occur as an optimization, but all variants are either
227 // emitted with external linkage or as linkonce if they are inline and used.
228 return false;
229 }
230
Craig Topper4f12f102014-03-12 06:41:41 +0000231 void EmitCXXDestructors(const CXXDestructorDecl *D) override;
Reid Klecknere7de47e2013-07-22 13:51:44 +0000232
Reid Kleckner89077a12013-12-17 19:46:40 +0000233 void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy,
Craig Topper4f12f102014-03-12 06:41:41 +0000234 FunctionArgList &Params) override;
John McCall5d865c322010-08-31 07:33:07 +0000235
Craig Topper4f12f102014-03-12 06:41:41 +0000236 void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override;
John McCall8ed55a52010-09-02 09:58:18 +0000237
George Burgess IVf203dbf2017-02-22 20:28:02 +0000238 AddedStructorArgs
239 addImplicitConstructorArgs(CodeGenFunction &CGF, const CXXConstructorDecl *D,
240 CXXCtorType Type, bool ForVirtualBase,
241 bool Delegating, CallArgList &Args) override;
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000242
Reid Kleckner6fe771a2013-12-13 00:53:54 +0000243 void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD,
244 CXXDtorType Type, bool ForVirtualBase,
John McCall7f416cc2015-09-08 08:05:57 +0000245 bool Delegating, Address This) override;
Reid Kleckner6fe771a2013-12-13 00:53:54 +0000246
Craig Topper4f12f102014-03-12 06:41:41 +0000247 void emitVTableDefinitions(CodeGenVTables &CGVT,
248 const CXXRecordDecl *RD) override;
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000249
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000250 bool isVirtualOffsetNeededForVTableField(CodeGenFunction &CGF,
251 CodeGenFunction::VPtr Vptr) override;
252
253 bool doStructorsInitializeVPtrs(const CXXRecordDecl *VTableClass) override {
254 return true;
255 }
256
257 llvm::Constant *
258 getVTableAddressPoint(BaseSubobject Base,
259 const CXXRecordDecl *VTableClass) override;
260
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000261 llvm::Value *getVTableAddressPointInStructor(
262 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass,
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000263 BaseSubobject Base, const CXXRecordDecl *NearestVBase) override;
264
265 llvm::Value *getVTableAddressPointInStructorWithVTT(
266 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass,
267 BaseSubobject Base, const CXXRecordDecl *NearestVBase);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000268
269 llvm::Constant *
270 getVTableAddressPointForConstExpr(BaseSubobject Base,
Craig Topper4f12f102014-03-12 06:41:41 +0000271 const CXXRecordDecl *VTableClass) override;
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000272
273 llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
Craig Topper4f12f102014-03-12 06:41:41 +0000274 CharUnits VPtrOffset) override;
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000275
John McCall9831b842018-02-06 18:52:44 +0000276 CGCallee getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD,
277 Address This, llvm::Type *Ty,
278 SourceLocation Loc) override;
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000279
David Majnemer0c0b6d92014-10-31 20:09:12 +0000280 llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF,
281 const CXXDestructorDecl *Dtor,
282 CXXDtorType DtorType,
John McCall7f416cc2015-09-08 08:05:57 +0000283 Address This,
David Majnemer0c0b6d92014-10-31 20:09:12 +0000284 const CXXMemberCallExpr *CE) override;
Timur Iskhodzhanovd6197112013-02-15 14:45:22 +0000285
Craig Topper4f12f102014-03-12 06:41:41 +0000286 void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override;
Reid Kleckner7810af02013-06-19 15:20:38 +0000287
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000288 bool canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const override;
Richard Smithc195c252018-11-27 19:33:49 +0000289 bool canSpeculativelyEmitVTableAsBaseClass(const CXXRecordDecl *RD) const;
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000290
Hans Wennborgc94391d2014-06-06 20:04:01 +0000291 void setThunkLinkage(llvm::Function *Thunk, bool ForVTable, GlobalDecl GD,
292 bool ReturnAdjustment) override {
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +0000293 // Allow inlining of thunks by emitting them with available_externally
294 // linkage together with vtables when needed.
Peter Collingbourne8fabc1b2015-07-01 02:10:26 +0000295 if (ForVTable && !Thunk->hasLocalLinkage())
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +0000296 Thunk->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
Rafael Espindolab7350042018-03-01 00:35:47 +0000297 CGM.setGVProperties(Thunk, GD);
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +0000298 }
299
Rafael Espindolab7350042018-03-01 00:35:47 +0000300 bool exportThunk() override { return true; }
301
John McCall7f416cc2015-09-08 08:05:57 +0000302 llvm::Value *performThisAdjustment(CodeGenFunction &CGF, Address This,
Craig Topper4f12f102014-03-12 06:41:41 +0000303 const ThisAdjustment &TA) override;
Timur Iskhodzhanov02014322013-10-30 11:55:43 +0000304
John McCall7f416cc2015-09-08 08:05:57 +0000305 llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, Address Ret,
Craig Topper4f12f102014-03-12 06:41:41 +0000306 const ReturnAdjustment &RA) override;
Timur Iskhodzhanov02014322013-10-30 11:55:43 +0000307
David Majnemer196ac332014-09-11 23:05:02 +0000308 size_t getSrcArgforCopyCtor(const CXXConstructorDecl *,
309 FunctionArgList &Args) const override {
310 assert(!Args.empty() && "expected the arglist to not be empty!");
311 return Args.size() - 1;
312 }
313
Craig Topper4f12f102014-03-12 06:41:41 +0000314 StringRef GetPureVirtualCallName() override { return "__cxa_pure_virtual"; }
315 StringRef GetDeletedVirtualCallName() override
316 { return "__cxa_deleted_virtual"; }
Joao Matos2ce88ef2012-07-17 17:10:11 +0000317
Craig Topper4f12f102014-03-12 06:41:41 +0000318 CharUnits getArrayCookieSizeImpl(QualType elementType) override;
John McCall7f416cc2015-09-08 08:05:57 +0000319 Address InitializeArrayCookie(CodeGenFunction &CGF,
320 Address NewPtr,
321 llvm::Value *NumElements,
322 const CXXNewExpr *expr,
323 QualType ElementType) override;
John McCallb91cd662012-05-01 05:23:51 +0000324 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +0000325 Address allocPtr,
Craig Topper4f12f102014-03-12 06:41:41 +0000326 CharUnits cookieSize) override;
John McCall68ff0372010-09-08 01:44:27 +0000327
John McCallcdf7ef52010-11-06 09:44:32 +0000328 void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
Craig Topper4f12f102014-03-12 06:41:41 +0000329 llvm::GlobalVariable *DeclPtr,
330 bool PerformInit) override;
Richard Smithdbf74ba2013-04-14 23:01:42 +0000331 void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
Craig Topper4f12f102014-03-12 06:41:41 +0000332 llvm::Constant *dtor, llvm::Constant *addr) override;
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000333
334 llvm::Function *getOrCreateThreadLocalWrapper(const VarDecl *VD,
Alexander Musmanf94c3182014-09-26 06:28:25 +0000335 llvm::Value *Val);
Richard Smith2fd1d7a2013-04-19 16:42:07 +0000336 void EmitThreadLocalInitFuncs(
David Majnemerb3341ea2014-10-05 05:05:40 +0000337 CodeGenModule &CGM,
Richard Smith5a99c492015-12-01 01:10:48 +0000338 ArrayRef<const VarDecl *> CXXThreadLocals,
David Majnemerb3341ea2014-10-05 05:05:40 +0000339 ArrayRef<llvm::Function *> CXXThreadLocalInits,
Richard Smith5a99c492015-12-01 01:10:48 +0000340 ArrayRef<const VarDecl *> CXXThreadLocalInitVars) override;
David Majnemerb3341ea2014-10-05 05:05:40 +0000341
342 bool usesThreadWrapperFunction() const override { return true; }
Richard Smith0f383742014-03-26 22:48:22 +0000343 LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD,
344 QualType LValType) override;
Peter Collingbourne66f82e62013-06-28 20:45:28 +0000345
Craig Topper4f12f102014-03-12 06:41:41 +0000346 bool NeedsVTTParameter(GlobalDecl GD) override;
David Majnemere2cb8d12014-07-07 06:20:47 +0000347
348 /**************************** RTTI Uniqueness ******************************/
349
350protected:
351 /// Returns true if the ABI requires RTTI type_info objects to be unique
352 /// across a program.
353 virtual bool shouldRTTIBeUnique() const { return true; }
354
355public:
356 /// What sort of unique-RTTI behavior should we use?
357 enum RTTIUniquenessKind {
358 /// We are guaranteeing, or need to guarantee, that the RTTI string
359 /// is unique.
360 RUK_Unique,
361
362 /// We are not guaranteeing uniqueness for the RTTI string, so we
363 /// can demote to hidden visibility but must use string comparisons.
364 RUK_NonUniqueHidden,
365
366 /// We are not guaranteeing uniqueness for the RTTI string, so we
367 /// have to use string comparisons, but we also have to emit it with
368 /// non-hidden visibility.
369 RUK_NonUniqueVisible
370 };
371
372 /// Return the required visibility status for the given type and linkage in
373 /// the current ABI.
374 RTTIUniquenessKind
375 classifyRTTIUniqueness(QualType CanTy,
376 llvm::GlobalValue::LinkageTypes Linkage) const;
377 friend class ItaniumRTTIBuilder;
Rafael Espindola91f68b42014-09-15 19:20:10 +0000378
379 void emitCXXStructor(const CXXMethodDecl *MD, StructorType Type) override;
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000380
Peter Collingbourne60108802017-12-13 21:53:04 +0000381 std::pair<llvm::Value *, const CXXRecordDecl *>
382 LoadVTablePtr(CodeGenFunction &CGF, Address This,
383 const CXXRecordDecl *RD) override;
384
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000385 private:
Piotr Padlewskid3b1cbd2017-06-01 08:04:05 +0000386 bool hasAnyUnusedVirtualInlineFunction(const CXXRecordDecl *RD) const {
387 const auto &VtableLayout =
388 CGM.getItaniumVTableContext().getVTableLayout(RD);
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000389
Piotr Padlewskid3b1cbd2017-06-01 08:04:05 +0000390 for (const auto &VtableComponent : VtableLayout.vtable_components()) {
391 // Skip empty slot.
392 if (!VtableComponent.isUsedFunctionPointerKind())
393 continue;
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000394
Piotr Padlewskid3b1cbd2017-06-01 08:04:05 +0000395 const CXXMethodDecl *Method = VtableComponent.getFunctionDecl();
396 if (!Method->getCanonicalDecl()->isInlined())
397 continue;
398
399 StringRef Name = CGM.getMangledName(VtableComponent.getGlobalDecl());
400 auto *Entry = CGM.GetGlobalValue(Name);
401 // This checks if virtual inline function has already been emitted.
402 // Note that it is possible that this inline function would be emitted
403 // after trying to emit vtable speculatively. Because of this we do
404 // an extra pass after emitting all deferred vtables to find and emit
405 // these vtables opportunistically.
406 if (!Entry || Entry->isDeclaration())
407 return true;
408 }
409 return false;
Piotr Padlewskia68a7872015-07-24 04:04:49 +0000410 }
Piotr Padlewskid679d7e2015-09-15 00:37:06 +0000411
412 bool isVTableHidden(const CXXRecordDecl *RD) const {
413 const auto &VtableLayout =
414 CGM.getItaniumVTableContext().getVTableLayout(RD);
415
416 for (const auto &VtableComponent : VtableLayout.vtable_components()) {
417 if (VtableComponent.isRTTIKind()) {
418 const CXXRecordDecl *RTTIDecl = VtableComponent.getRTTIDecl();
419 if (RTTIDecl->getVisibility() == Visibility::HiddenVisibility)
420 return true;
421 } else if (VtableComponent.isUsedFunctionPointerKind()) {
422 const CXXMethodDecl *Method = VtableComponent.getFunctionDecl();
423 if (Method->getVisibility() == Visibility::HiddenVisibility &&
424 !Method->isDefined())
425 return true;
426 }
427 }
428 return false;
429 }
Charles Davis4e786dd2010-05-25 19:52:27 +0000430};
John McCall86353412010-08-21 22:46:04 +0000431
432class ARMCXXABI : public ItaniumCXXABI {
433public:
Mark Seabornedf0d382013-07-24 16:25:13 +0000434 ARMCXXABI(CodeGen::CodeGenModule &CGM) :
435 ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
436 /* UseARMGuardVarABI = */ true) {}
John McCall5d865c322010-08-31 07:33:07 +0000437
Craig Topper4f12f102014-03-12 06:41:41 +0000438 bool HasThisReturn(GlobalDecl GD) const override {
Stephen Lin9dc6eef2013-06-30 20:40:16 +0000439 return (isa<CXXConstructorDecl>(GD.getDecl()) || (
440 isa<CXXDestructorDecl>(GD.getDecl()) &&
441 GD.getDtorType() != Dtor_Deleting));
442 }
John McCall5d865c322010-08-31 07:33:07 +0000443
Craig Topper4f12f102014-03-12 06:41:41 +0000444 void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV,
445 QualType ResTy) override;
John McCall5d865c322010-08-31 07:33:07 +0000446
Craig Topper4f12f102014-03-12 06:41:41 +0000447 CharUnits getArrayCookieSizeImpl(QualType elementType) override;
John McCall7f416cc2015-09-08 08:05:57 +0000448 Address InitializeArrayCookie(CodeGenFunction &CGF,
449 Address NewPtr,
450 llvm::Value *NumElements,
451 const CXXNewExpr *expr,
452 QualType ElementType) override;
453 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, Address allocPtr,
Craig Topper4f12f102014-03-12 06:41:41 +0000454 CharUnits cookieSize) override;
John McCall86353412010-08-21 22:46:04 +0000455};
Tim Northovera2ee4332014-03-29 15:09:45 +0000456
457class iOS64CXXABI : public ARMCXXABI {
458public:
John McCalld23b27e2016-09-16 02:40:45 +0000459 iOS64CXXABI(CodeGen::CodeGenModule &CGM) : ARMCXXABI(CGM) {
460 Use32BitVTableOffsetABI = true;
461 }
Tim Northover65f582f2014-03-30 17:32:48 +0000462
463 // ARM64 libraries are prepared for non-unique RTTI.
David Majnemere2cb8d12014-07-07 06:20:47 +0000464 bool shouldRTTIBeUnique() const override { return false; }
Tim Northovera2ee4332014-03-29 15:09:45 +0000465};
Dan Gohmanc2853072015-09-03 22:51:53 +0000466
467class WebAssemblyCXXABI final : public ItaniumCXXABI {
468public:
469 explicit WebAssemblyCXXABI(CodeGen::CodeGenModule &CGM)
470 : ItaniumCXXABI(CGM, /*UseARMMethodPtrABI=*/true,
471 /*UseARMGuardVarABI=*/true) {}
Heejin Ahnc6479192018-05-31 22:18:13 +0000472 void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) override;
Dan Gohmanc2853072015-09-03 22:51:53 +0000473
474private:
475 bool HasThisReturn(GlobalDecl GD) const override {
476 return isa<CXXConstructorDecl>(GD.getDecl()) ||
477 (isa<CXXDestructorDecl>(GD.getDecl()) &&
478 GD.getDtorType() != Dtor_Deleting);
479 }
Derek Schuff8179be42016-05-10 17:44:55 +0000480 bool canCallMismatchedFunctionType() const override { return false; }
Dan Gohmanc2853072015-09-03 22:51:53 +0000481};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000482}
Charles Davis4e786dd2010-05-25 19:52:27 +0000483
Charles Davis53c59df2010-08-16 03:33:14 +0000484CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
John McCallc8e01702013-04-16 22:48:15 +0000485 switch (CGM.getTarget().getCXXABI().getKind()) {
John McCall57625922013-01-25 23:36:14 +0000486 // For IR-generation purposes, there's no significant difference
487 // between the ARM and iOS ABIs.
488 case TargetCXXABI::GenericARM:
489 case TargetCXXABI::iOS:
Tim Northover756447a2015-10-30 16:30:36 +0000490 case TargetCXXABI::WatchOS:
John McCall57625922013-01-25 23:36:14 +0000491 return new ARMCXXABI(CGM);
Charles Davis4e786dd2010-05-25 19:52:27 +0000492
Tim Northovera2ee4332014-03-29 15:09:45 +0000493 case TargetCXXABI::iOS64:
494 return new iOS64CXXABI(CGM);
495
Tim Northover9bb857a2013-01-31 12:13:10 +0000496 // Note that AArch64 uses the generic ItaniumCXXABI class since it doesn't
497 // include the other 32-bit ARM oddities: constructor/destructor return values
498 // and array cookies.
499 case TargetCXXABI::GenericAArch64:
Mark Seabornedf0d382013-07-24 16:25:13 +0000500 return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
501 /* UseARMGuardVarABI = */ true);
Tim Northover9bb857a2013-01-31 12:13:10 +0000502
Zoran Jovanovic26a12162015-02-18 15:21:35 +0000503 case TargetCXXABI::GenericMIPS:
504 return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true);
505
Dan Gohmanc2853072015-09-03 22:51:53 +0000506 case TargetCXXABI::WebAssembly:
507 return new WebAssemblyCXXABI(CGM);
508
John McCall57625922013-01-25 23:36:14 +0000509 case TargetCXXABI::GenericItanium:
Mark Seabornedf0d382013-07-24 16:25:13 +0000510 if (CGM.getContext().getTargetInfo().getTriple().getArch()
511 == llvm::Triple::le32) {
512 // For PNaCl, use ARM-style method pointers so that PNaCl code
513 // does not assume anything about the alignment of function
514 // pointers.
515 return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
516 /* UseARMGuardVarABI = */ false);
517 }
John McCall57625922013-01-25 23:36:14 +0000518 return new ItaniumCXXABI(CGM);
519
520 case TargetCXXABI::Microsoft:
521 llvm_unreachable("Microsoft ABI is not Itanium-based");
522 }
523 llvm_unreachable("bad ABI kind");
John McCall86353412010-08-21 22:46:04 +0000524}
525
Chris Lattnera5f58b02011-07-09 17:41:47 +0000526llvm::Type *
John McCall7a9aac22010-08-23 01:21:21 +0000527ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
528 if (MPT->isMemberDataPointer())
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000529 return CGM.PtrDiffTy;
Serge Guelton1d993272017-05-09 19:31:30 +0000530 return llvm::StructType::get(CGM.PtrDiffTy, CGM.PtrDiffTy);
John McCall1c456c82010-08-22 06:43:33 +0000531}
532
John McCalld9c6c0b2010-08-22 00:59:17 +0000533/// In the Itanium and ARM ABIs, method pointers have the form:
534/// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
535///
536/// In the Itanium ABI:
537/// - method pointers are virtual if (memptr.ptr & 1) is nonzero
538/// - the this-adjustment is (memptr.adj)
539/// - the virtual offset is (memptr.ptr - 1)
540///
541/// In the ARM ABI:
542/// - method pointers are virtual if (memptr.adj & 1) is nonzero
543/// - the this-adjustment is (memptr.adj >> 1)
544/// - the virtual offset is (memptr.ptr)
545/// ARM uses 'adj' for the virtual flag because Thumb functions
546/// may be only single-byte aligned.
547///
548/// If the member is virtual, the adjusted 'this' pointer points
549/// to a vtable pointer from which the virtual offset is applied.
550///
551/// If the member is non-virtual, memptr.ptr is the address of
552/// the function to call.
John McCallb92ab1a2016-10-26 23:46:34 +0000553CGCallee ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(
John McCall7f416cc2015-09-08 08:05:57 +0000554 CodeGenFunction &CGF, const Expr *E, Address ThisAddr,
555 llvm::Value *&ThisPtrForCall,
David Majnemer2b0d66d2014-02-20 23:22:07 +0000556 llvm::Value *MemFnPtr, const MemberPointerType *MPT) {
John McCall475999d2010-08-22 00:05:51 +0000557 CGBuilderTy &Builder = CGF.Builder;
558
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000559 const FunctionProtoType *FPT =
John McCall475999d2010-08-22 00:05:51 +0000560 MPT->getPointeeType()->getAs<FunctionProtoType>();
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000561 const CXXRecordDecl *RD =
John McCall475999d2010-08-22 00:05:51 +0000562 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
563
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000564 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(
565 CGM.getTypes().arrangeCXXMethodType(RD, FPT, /*FD=*/nullptr));
John McCall475999d2010-08-22 00:05:51 +0000566
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000567 llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(CGM.PtrDiffTy, 1);
John McCall475999d2010-08-22 00:05:51 +0000568
John McCalld9c6c0b2010-08-22 00:59:17 +0000569 llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
570 llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
571 llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
572
John McCalla1dee5302010-08-22 10:59:02 +0000573 // Extract memptr.adj, which is in the second field.
574 llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
John McCalld9c6c0b2010-08-22 00:59:17 +0000575
576 // Compute the true adjustment.
577 llvm::Value *Adj = RawAdj;
Mark Seabornedf0d382013-07-24 16:25:13 +0000578 if (UseARMMethodPtrABI)
John McCalld9c6c0b2010-08-22 00:59:17 +0000579 Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
John McCall475999d2010-08-22 00:05:51 +0000580
581 // Apply the adjustment and cast back to the original struct type
582 // for consistency.
John McCall7f416cc2015-09-08 08:05:57 +0000583 llvm::Value *This = ThisAddr.getPointer();
John McCalld9c6c0b2010-08-22 00:59:17 +0000584 llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
585 Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
586 This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
John McCall7f416cc2015-09-08 08:05:57 +0000587 ThisPtrForCall = This;
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000588
John McCall475999d2010-08-22 00:05:51 +0000589 // Load the function pointer.
John McCalla1dee5302010-08-22 10:59:02 +0000590 llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000591
John McCall475999d2010-08-22 00:05:51 +0000592 // If the LSB in the function pointer is 1, the function pointer points to
593 // a virtual function.
John McCalld9c6c0b2010-08-22 00:59:17 +0000594 llvm::Value *IsVirtual;
Mark Seabornedf0d382013-07-24 16:25:13 +0000595 if (UseARMMethodPtrABI)
John McCalld9c6c0b2010-08-22 00:59:17 +0000596 IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
597 else
598 IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
599 IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
John McCall475999d2010-08-22 00:05:51 +0000600 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
601
602 // In the virtual path, the adjustment left 'This' pointing to the
603 // vtable of the correct base subobject. The "function pointer" is an
John McCalld9c6c0b2010-08-22 00:59:17 +0000604 // offset within the vtable (+1 for the virtual flag on non-ARM).
John McCall475999d2010-08-22 00:05:51 +0000605 CGF.EmitBlock(FnVirtual);
606
607 // Cast the adjusted this to a pointer to vtable pointer and load.
Chris Lattner2192fe52011-07-18 04:24:23 +0000608 llvm::Type *VTableTy = Builder.getInt8PtrTy();
John McCall7f416cc2015-09-08 08:05:57 +0000609 CharUnits VTablePtrAlign =
610 CGF.CGM.getDynamicOffsetAlignment(ThisAddr.getAlignment(), RD,
611 CGF.getPointerAlign());
612 llvm::Value *VTable =
Piotr Padlewski4b1ac722015-09-15 21:46:55 +0000613 CGF.GetVTablePtr(Address(This, VTablePtrAlign), VTableTy, RD);
John McCall475999d2010-08-22 00:05:51 +0000614
615 // Apply the offset.
John McCalld23b27e2016-09-16 02:40:45 +0000616 // On ARM64, to reserve extra space in virtual member function pointers,
617 // we only pay attention to the low 32 bits of the offset.
John McCalld9c6c0b2010-08-22 00:59:17 +0000618 llvm::Value *VTableOffset = FnAsInt;
Mark Seabornedf0d382013-07-24 16:25:13 +0000619 if (!UseARMMethodPtrABI)
620 VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
John McCalld23b27e2016-09-16 02:40:45 +0000621 if (Use32BitVTableOffsetABI) {
622 VTableOffset = Builder.CreateTrunc(VTableOffset, CGF.Int32Ty);
623 VTableOffset = Builder.CreateZExt(VTableOffset, CGM.PtrDiffTy);
624 }
Peter Collingbournee44acad2018-06-26 02:15:47 +0000625 // Compute the address of the virtual function pointer.
626 llvm::Value *VFPAddr = Builder.CreateGEP(VTable, VTableOffset);
627
628 // Check the address of the function pointer if CFI on member function
629 // pointers is enabled.
630 llvm::Constant *CheckSourceLocation;
631 llvm::Constant *CheckTypeDesc;
632 bool ShouldEmitCFICheck = CGF.SanOpts.has(SanitizerKind::CFIMFCall) &&
633 CGM.HasHiddenLTOVisibility(RD);
634 if (ShouldEmitCFICheck) {
635 CodeGenFunction::SanitizerScope SanScope(&CGF);
636
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000637 CheckSourceLocation = CGF.EmitCheckSourceLocation(E->getBeginLoc());
Peter Collingbournee44acad2018-06-26 02:15:47 +0000638 CheckTypeDesc = CGF.EmitCheckTypeDescriptor(QualType(MPT, 0));
639 llvm::Constant *StaticData[] = {
640 llvm::ConstantInt::get(CGF.Int8Ty, CodeGenFunction::CFITCK_VMFCall),
641 CheckSourceLocation,
642 CheckTypeDesc,
643 };
644
645 llvm::Metadata *MD =
646 CGM.CreateMetadataIdentifierForVirtualMemPtrType(QualType(MPT, 0));
647 llvm::Value *TypeId = llvm::MetadataAsValue::get(CGF.getLLVMContext(), MD);
648
649 llvm::Value *TypeTest = Builder.CreateCall(
650 CGM.getIntrinsic(llvm::Intrinsic::type_test), {VFPAddr, TypeId});
651
652 if (CGM.getCodeGenOpts().SanitizeTrap.has(SanitizerKind::CFIMFCall)) {
653 CGF.EmitTrapCheck(TypeTest);
654 } else {
655 llvm::Value *AllVtables = llvm::MetadataAsValue::get(
656 CGM.getLLVMContext(),
657 llvm::MDString::get(CGM.getLLVMContext(), "all-vtables"));
658 llvm::Value *ValidVtable = Builder.CreateCall(
659 CGM.getIntrinsic(llvm::Intrinsic::type_test), {VTable, AllVtables});
660 CGF.EmitCheck(std::make_pair(TypeTest, SanitizerKind::CFIMFCall),
661 SanitizerHandler::CFICheckFail, StaticData,
662 {VTable, ValidVtable});
663 }
664
665 FnVirtual = Builder.GetInsertBlock();
666 }
John McCall475999d2010-08-22 00:05:51 +0000667
668 // Load the virtual function to call.
Peter Collingbournee44acad2018-06-26 02:15:47 +0000669 VFPAddr = Builder.CreateBitCast(VFPAddr, FTy->getPointerTo()->getPointerTo());
670 llvm::Value *VirtualFn = Builder.CreateAlignedLoad(
671 VFPAddr, CGF.getPointerAlign(), "memptr.virtualfn");
John McCall475999d2010-08-22 00:05:51 +0000672 CGF.EmitBranch(FnEnd);
673
674 // In the non-virtual path, the function pointer is actually a
675 // function pointer.
676 CGF.EmitBlock(FnNonVirtual);
677 llvm::Value *NonVirtualFn =
John McCalld9c6c0b2010-08-22 00:59:17 +0000678 Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000679
Peter Collingbournee44acad2018-06-26 02:15:47 +0000680 // Check the function pointer if CFI on member function pointers is enabled.
681 if (ShouldEmitCFICheck) {
682 CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl();
683 if (RD->hasDefinition()) {
684 CodeGenFunction::SanitizerScope SanScope(&CGF);
685
686 llvm::Constant *StaticData[] = {
687 llvm::ConstantInt::get(CGF.Int8Ty, CodeGenFunction::CFITCK_NVMFCall),
688 CheckSourceLocation,
689 CheckTypeDesc,
690 };
691
692 llvm::Value *Bit = Builder.getFalse();
693 llvm::Value *CastedNonVirtualFn =
694 Builder.CreateBitCast(NonVirtualFn, CGF.Int8PtrTy);
695 for (const CXXRecordDecl *Base : CGM.getMostBaseClasses(RD)) {
696 llvm::Metadata *MD = CGM.CreateMetadataIdentifierForType(
697 getContext().getMemberPointerType(
698 MPT->getPointeeType(),
699 getContext().getRecordType(Base).getTypePtr()));
700 llvm::Value *TypeId =
701 llvm::MetadataAsValue::get(CGF.getLLVMContext(), MD);
702
703 llvm::Value *TypeTest =
704 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::type_test),
705 {CastedNonVirtualFn, TypeId});
706 Bit = Builder.CreateOr(Bit, TypeTest);
707 }
708
709 CGF.EmitCheck(std::make_pair(Bit, SanitizerKind::CFIMFCall),
710 SanitizerHandler::CFICheckFail, StaticData,
711 {CastedNonVirtualFn, llvm::UndefValue::get(CGF.IntPtrTy)});
712
713 FnNonVirtual = Builder.GetInsertBlock();
714 }
715 }
716
John McCall475999d2010-08-22 00:05:51 +0000717 // We're done.
718 CGF.EmitBlock(FnEnd);
John McCallb92ab1a2016-10-26 23:46:34 +0000719 llvm::PHINode *CalleePtr = Builder.CreatePHI(FTy->getPointerTo(), 2);
720 CalleePtr->addIncoming(VirtualFn, FnVirtual);
721 CalleePtr->addIncoming(NonVirtualFn, FnNonVirtual);
722
723 CGCallee Callee(FPT, CalleePtr);
John McCall475999d2010-08-22 00:05:51 +0000724 return Callee;
725}
John McCalla8bbb822010-08-22 03:04:22 +0000726
John McCallc134eb52010-08-31 21:07:20 +0000727/// Compute an l-value by applying the given pointer-to-member to a
728/// base object.
David Majnemer2b0d66d2014-02-20 23:22:07 +0000729llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(
John McCall7f416cc2015-09-08 08:05:57 +0000730 CodeGenFunction &CGF, const Expr *E, Address Base, llvm::Value *MemPtr,
David Majnemer2b0d66d2014-02-20 23:22:07 +0000731 const MemberPointerType *MPT) {
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000732 assert(MemPtr->getType() == CGM.PtrDiffTy);
John McCallc134eb52010-08-31 21:07:20 +0000733
734 CGBuilderTy &Builder = CGF.Builder;
735
John McCallc134eb52010-08-31 21:07:20 +0000736 // Cast to char*.
John McCall7f416cc2015-09-08 08:05:57 +0000737 Base = Builder.CreateElementBitCast(Base, CGF.Int8Ty);
John McCallc134eb52010-08-31 21:07:20 +0000738
739 // Apply the offset, which we assume is non-null.
John McCall7f416cc2015-09-08 08:05:57 +0000740 llvm::Value *Addr =
741 Builder.CreateInBoundsGEP(Base.getPointer(), MemPtr, "memptr.offset");
John McCallc134eb52010-08-31 21:07:20 +0000742
743 // Cast the address to the appropriate pointer type, adopting the
744 // address space of the base pointer.
John McCall7f416cc2015-09-08 08:05:57 +0000745 llvm::Type *PType = CGF.ConvertTypeForMem(MPT->getPointeeType())
746 ->getPointerTo(Base.getAddressSpace());
John McCallc134eb52010-08-31 21:07:20 +0000747 return Builder.CreateBitCast(Addr, PType);
748}
749
John McCallc62bb392012-02-15 01:22:51 +0000750/// Perform a bitcast, derived-to-base, or base-to-derived member pointer
751/// conversion.
752///
753/// Bitcast conversions are always a no-op under Itanium.
John McCall7a9aac22010-08-23 01:21:21 +0000754///
755/// Obligatory offset/adjustment diagram:
756/// <-- offset --> <-- adjustment -->
757/// |--------------------------|----------------------|--------------------|
758/// ^Derived address point ^Base address point ^Member address point
759///
760/// So when converting a base member pointer to a derived member pointer,
761/// we add the offset to the adjustment because the address point has
762/// decreased; and conversely, when converting a derived MP to a base MP
763/// we subtract the offset from the adjustment because the address point
764/// has increased.
765///
766/// The standard forbids (at compile time) conversion to and from
767/// virtual bases, which is why we don't have to consider them here.
768///
769/// The standard forbids (at run time) casting a derived MP to a base
770/// MP when the derived MP does not point to a member of the base.
771/// This is why -1 is a reasonable choice for null data member
772/// pointers.
John McCalla1dee5302010-08-22 10:59:02 +0000773llvm::Value *
John McCall7a9aac22010-08-23 01:21:21 +0000774ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
775 const CastExpr *E,
John McCallc62bb392012-02-15 01:22:51 +0000776 llvm::Value *src) {
John McCalle3027922010-08-25 11:45:40 +0000777 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
John McCallc62bb392012-02-15 01:22:51 +0000778 E->getCastKind() == CK_BaseToDerivedMemberPointer ||
779 E->getCastKind() == CK_ReinterpretMemberPointer);
780
781 // Under Itanium, reinterprets don't require any additional processing.
782 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
783
784 // Use constant emission if we can.
785 if (isa<llvm::Constant>(src))
786 return EmitMemberPointerConversion(E, cast<llvm::Constant>(src));
787
788 llvm::Constant *adj = getMemberPointerAdjustment(E);
789 if (!adj) return src;
John McCalla8bbb822010-08-22 03:04:22 +0000790
791 CGBuilderTy &Builder = CGF.Builder;
John McCallc62bb392012-02-15 01:22:51 +0000792 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
John McCalla8bbb822010-08-22 03:04:22 +0000793
John McCallc62bb392012-02-15 01:22:51 +0000794 const MemberPointerType *destTy =
795 E->getType()->castAs<MemberPointerType>();
John McCall1c456c82010-08-22 06:43:33 +0000796
John McCall7a9aac22010-08-23 01:21:21 +0000797 // For member data pointers, this is just a matter of adding the
798 // offset if the source is non-null.
John McCallc62bb392012-02-15 01:22:51 +0000799 if (destTy->isMemberDataPointer()) {
800 llvm::Value *dst;
801 if (isDerivedToBase)
802 dst = Builder.CreateNSWSub(src, adj, "adj");
John McCall7a9aac22010-08-23 01:21:21 +0000803 else
John McCallc62bb392012-02-15 01:22:51 +0000804 dst = Builder.CreateNSWAdd(src, adj, "adj");
John McCall7a9aac22010-08-23 01:21:21 +0000805
806 // Null check.
John McCallc62bb392012-02-15 01:22:51 +0000807 llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType());
808 llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull");
809 return Builder.CreateSelect(isNull, src, dst);
John McCall7a9aac22010-08-23 01:21:21 +0000810 }
811
John McCalla1dee5302010-08-22 10:59:02 +0000812 // The this-adjustment is left-shifted by 1 on ARM.
Mark Seabornedf0d382013-07-24 16:25:13 +0000813 if (UseARMMethodPtrABI) {
John McCallc62bb392012-02-15 01:22:51 +0000814 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
815 offset <<= 1;
816 adj = llvm::ConstantInt::get(adj->getType(), offset);
John McCalla1dee5302010-08-22 10:59:02 +0000817 }
818
John McCallc62bb392012-02-15 01:22:51 +0000819 llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj");
820 llvm::Value *dstAdj;
821 if (isDerivedToBase)
822 dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj");
John McCalla1dee5302010-08-22 10:59:02 +0000823 else
John McCallc62bb392012-02-15 01:22:51 +0000824 dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj");
John McCalla1dee5302010-08-22 10:59:02 +0000825
John McCallc62bb392012-02-15 01:22:51 +0000826 return Builder.CreateInsertValue(src, dstAdj, 1);
827}
828
829llvm::Constant *
830ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E,
831 llvm::Constant *src) {
832 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
833 E->getCastKind() == CK_BaseToDerivedMemberPointer ||
834 E->getCastKind() == CK_ReinterpretMemberPointer);
835
836 // Under Itanium, reinterprets don't require any additional processing.
837 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
838
839 // If the adjustment is trivial, we don't need to do anything.
840 llvm::Constant *adj = getMemberPointerAdjustment(E);
841 if (!adj) return src;
842
843 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
844
845 const MemberPointerType *destTy =
846 E->getType()->castAs<MemberPointerType>();
847
848 // For member data pointers, this is just a matter of adding the
849 // offset if the source is non-null.
850 if (destTy->isMemberDataPointer()) {
851 // null maps to null.
852 if (src->isAllOnesValue()) return src;
853
854 if (isDerivedToBase)
855 return llvm::ConstantExpr::getNSWSub(src, adj);
856 else
857 return llvm::ConstantExpr::getNSWAdd(src, adj);
858 }
859
860 // The this-adjustment is left-shifted by 1 on ARM.
Mark Seabornedf0d382013-07-24 16:25:13 +0000861 if (UseARMMethodPtrABI) {
John McCallc62bb392012-02-15 01:22:51 +0000862 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
863 offset <<= 1;
864 adj = llvm::ConstantInt::get(adj->getType(), offset);
865 }
866
867 llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1);
868 llvm::Constant *dstAdj;
869 if (isDerivedToBase)
870 dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj);
871 else
872 dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj);
873
874 return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1);
John McCalla8bbb822010-08-22 03:04:22 +0000875}
John McCall84fa5102010-08-22 04:16:24 +0000876
877llvm::Constant *
John McCall7a9aac22010-08-23 01:21:21 +0000878ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
John McCall7a9aac22010-08-23 01:21:21 +0000879 // Itanium C++ ABI 2.3:
880 // A NULL pointer is represented as -1.
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000881 if (MPT->isMemberDataPointer())
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000882 return llvm::ConstantInt::get(CGM.PtrDiffTy, -1ULL, /*isSigned=*/true);
John McCalla1dee5302010-08-22 10:59:02 +0000883
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000884 llvm::Constant *Zero = llvm::ConstantInt::get(CGM.PtrDiffTy, 0);
John McCalla1dee5302010-08-22 10:59:02 +0000885 llvm::Constant *Values[2] = { Zero, Zero };
Chris Lattnere64d7ba2011-06-20 04:01:35 +0000886 return llvm::ConstantStruct::getAnon(Values);
John McCall84fa5102010-08-22 04:16:24 +0000887}
888
John McCallf3a88602011-02-03 08:15:49 +0000889llvm::Constant *
890ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
891 CharUnits offset) {
John McCall7a9aac22010-08-23 01:21:21 +0000892 // Itanium C++ ABI 2.3:
893 // A pointer to data member is an offset from the base address of
894 // the class object containing it, represented as a ptrdiff_t
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000895 return llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity());
John McCall7a9aac22010-08-23 01:21:21 +0000896}
897
David Majnemere2be95b2015-06-23 07:31:01 +0000898llvm::Constant *
899ItaniumCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) {
Richard Smithdafff942012-01-14 04:30:29 +0000900 return BuildMemberPointer(MD, CharUnits::Zero());
901}
902
903llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD,
904 CharUnits ThisAdjustment) {
John McCalla1dee5302010-08-22 10:59:02 +0000905 assert(MD->isInstance() && "Member function must not be static!");
John McCalla1dee5302010-08-22 10:59:02 +0000906
907 CodeGenTypes &Types = CGM.getTypes();
John McCalla1dee5302010-08-22 10:59:02 +0000908
909 // Get the function pointer (or index if this is a virtual function).
910 llvm::Constant *MemPtr[2];
911 if (MD->isVirtual()) {
Timur Iskhodzhanov58776632013-11-05 15:54:58 +0000912 uint64_t Index = CGM.getItaniumVTableContext().getMethodVTableIndex(MD);
John McCalla1dee5302010-08-22 10:59:02 +0000913
Ken Dyckdf016282011-04-09 01:30:02 +0000914 const ASTContext &Context = getContext();
915 CharUnits PointerWidth =
Douglas Gregore8bbc122011-09-02 00:18:52 +0000916 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Ken Dyckdf016282011-04-09 01:30:02 +0000917 uint64_t VTableOffset = (Index * PointerWidth.getQuantity());
John McCalla1dee5302010-08-22 10:59:02 +0000918
Mark Seabornedf0d382013-07-24 16:25:13 +0000919 if (UseARMMethodPtrABI) {
John McCalla1dee5302010-08-22 10:59:02 +0000920 // ARM C++ ABI 3.2.1:
921 // This ABI specifies that adj contains twice the this
922 // adjustment, plus 1 if the member function is virtual. The
923 // least significant bit of adj then makes exactly the same
924 // discrimination as the least significant bit of ptr does for
925 // Itanium.
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000926 MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset);
927 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
Richard Smithdafff942012-01-14 04:30:29 +0000928 2 * ThisAdjustment.getQuantity() + 1);
John McCalla1dee5302010-08-22 10:59:02 +0000929 } else {
930 // Itanium C++ ABI 2.3:
931 // For a virtual function, [the pointer field] is 1 plus the
932 // virtual table offset (in bytes) of the function,
933 // represented as a ptrdiff_t.
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000934 MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset + 1);
935 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
Richard Smithdafff942012-01-14 04:30:29 +0000936 ThisAdjustment.getQuantity());
John McCalla1dee5302010-08-22 10:59:02 +0000937 }
938 } else {
John McCall2979fe02011-04-12 00:42:48 +0000939 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
Chris Lattner2192fe52011-07-18 04:24:23 +0000940 llvm::Type *Ty;
John McCall2979fe02011-04-12 00:42:48 +0000941 // Check whether the function has a computable LLVM signature.
Chris Lattner8806e322011-07-10 00:18:59 +0000942 if (Types.isFuncTypeConvertible(FPT)) {
John McCall2979fe02011-04-12 00:42:48 +0000943 // The function has a computable LLVM signature; use the correct type.
John McCalla729c622012-02-17 03:33:10 +0000944 Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
John McCalla1dee5302010-08-22 10:59:02 +0000945 } else {
John McCall2979fe02011-04-12 00:42:48 +0000946 // Use an arbitrary non-function type to tell GetAddrOfFunction that the
947 // function type is incomplete.
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000948 Ty = CGM.PtrDiffTy;
John McCalla1dee5302010-08-22 10:59:02 +0000949 }
John McCall2979fe02011-04-12 00:42:48 +0000950 llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty);
John McCalla1dee5302010-08-22 10:59:02 +0000951
Reid Kleckner9cffbc12013-03-22 16:13:10 +0000952 MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, CGM.PtrDiffTy);
Mark Seabornedf0d382013-07-24 16:25:13 +0000953 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
954 (UseARMMethodPtrABI ? 2 : 1) *
Richard Smithdafff942012-01-14 04:30:29 +0000955 ThisAdjustment.getQuantity());
John McCalla1dee5302010-08-22 10:59:02 +0000956 }
Jake Ehrlichc451cf22017-11-11 01:15:41 +0000957
Chris Lattnere64d7ba2011-06-20 04:01:35 +0000958 return llvm::ConstantStruct::getAnon(MemPtr);
John McCall1c456c82010-08-22 06:43:33 +0000959}
960
Richard Smithdafff942012-01-14 04:30:29 +0000961llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP,
962 QualType MPType) {
963 const MemberPointerType *MPT = MPType->castAs<MemberPointerType>();
964 const ValueDecl *MPD = MP.getMemberPointerDecl();
965 if (!MPD)
966 return EmitNullMemberPointer(MPT);
967
Reid Kleckner452abac2013-05-09 21:01:17 +0000968 CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP);
Richard Smithdafff942012-01-14 04:30:29 +0000969
970 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD))
971 return BuildMemberPointer(MD, ThisAdjustment);
972
973 CharUnits FieldOffset =
974 getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD));
975 return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset);
976}
977
John McCall131d97d2010-08-22 08:30:07 +0000978/// The comparison algorithm is pretty easy: the member pointers are
979/// the same if they're either bitwise identical *or* both null.
980///
981/// ARM is different here only because null-ness is more complicated.
982llvm::Value *
John McCall7a9aac22010-08-23 01:21:21 +0000983ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
984 llvm::Value *L,
985 llvm::Value *R,
986 const MemberPointerType *MPT,
987 bool Inequality) {
John McCall131d97d2010-08-22 08:30:07 +0000988 CGBuilderTy &Builder = CGF.Builder;
989
John McCall131d97d2010-08-22 08:30:07 +0000990 llvm::ICmpInst::Predicate Eq;
991 llvm::Instruction::BinaryOps And, Or;
992 if (Inequality) {
993 Eq = llvm::ICmpInst::ICMP_NE;
994 And = llvm::Instruction::Or;
995 Or = llvm::Instruction::And;
996 } else {
997 Eq = llvm::ICmpInst::ICMP_EQ;
998 And = llvm::Instruction::And;
999 Or = llvm::Instruction::Or;
1000 }
1001
John McCall7a9aac22010-08-23 01:21:21 +00001002 // Member data pointers are easy because there's a unique null
1003 // value, so it just comes down to bitwise equality.
1004 if (MPT->isMemberDataPointer())
1005 return Builder.CreateICmp(Eq, L, R);
1006
1007 // For member function pointers, the tautologies are more complex.
1008 // The Itanium tautology is:
John McCall61a14882010-08-23 06:56:36 +00001009 // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
John McCall7a9aac22010-08-23 01:21:21 +00001010 // The ARM tautology is:
John McCall61a14882010-08-23 06:56:36 +00001011 // (L == R) <==> (L.ptr == R.ptr &&
1012 // (L.adj == R.adj ||
1013 // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
John McCall7a9aac22010-08-23 01:21:21 +00001014 // The inequality tautologies have exactly the same structure, except
1015 // applying De Morgan's laws.
Jake Ehrlichc451cf22017-11-11 01:15:41 +00001016
John McCall7a9aac22010-08-23 01:21:21 +00001017 llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
1018 llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
1019
John McCall131d97d2010-08-22 08:30:07 +00001020 // This condition tests whether L.ptr == R.ptr. This must always be
1021 // true for equality to hold.
1022 llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
1023
1024 // This condition, together with the assumption that L.ptr == R.ptr,
1025 // tests whether the pointers are both null. ARM imposes an extra
1026 // condition.
1027 llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
1028 llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
1029
1030 // This condition tests whether L.adj == R.adj. If this isn't
1031 // true, the pointers are unequal unless they're both null.
John McCalla1dee5302010-08-22 10:59:02 +00001032 llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
1033 llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
John McCall131d97d2010-08-22 08:30:07 +00001034 llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
1035
1036 // Null member function pointers on ARM clear the low bit of Adj,
1037 // so the zero condition has to check that neither low bit is set.
Mark Seabornedf0d382013-07-24 16:25:13 +00001038 if (UseARMMethodPtrABI) {
John McCall131d97d2010-08-22 08:30:07 +00001039 llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
1040
1041 // Compute (l.adj | r.adj) & 1 and test it against zero.
1042 llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
1043 llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
1044 llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
1045 "cmp.or.adj");
1046 EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
1047 }
1048
1049 // Tie together all our conditions.
1050 llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
1051 Result = Builder.CreateBinOp(And, PtrEq, Result,
1052 Inequality ? "memptr.ne" : "memptr.eq");
1053 return Result;
1054}
1055
1056llvm::Value *
John McCall7a9aac22010-08-23 01:21:21 +00001057ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
1058 llvm::Value *MemPtr,
1059 const MemberPointerType *MPT) {
John McCall131d97d2010-08-22 08:30:07 +00001060 CGBuilderTy &Builder = CGF.Builder;
John McCall7a9aac22010-08-23 01:21:21 +00001061
1062 /// For member data pointers, this is just a check against -1.
1063 if (MPT->isMemberDataPointer()) {
Reid Kleckner9cffbc12013-03-22 16:13:10 +00001064 assert(MemPtr->getType() == CGM.PtrDiffTy);
John McCall7a9aac22010-08-23 01:21:21 +00001065 llvm::Value *NegativeOne =
1066 llvm::Constant::getAllOnesValue(MemPtr->getType());
1067 return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
1068 }
Jake Ehrlichc451cf22017-11-11 01:15:41 +00001069
Daniel Dunbar914bc412011-04-19 23:10:47 +00001070 // In Itanium, a member function pointer is not null if 'ptr' is not null.
John McCalla1dee5302010-08-22 10:59:02 +00001071 llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
John McCall131d97d2010-08-22 08:30:07 +00001072
1073 llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
1074 llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
1075
Daniel Dunbar914bc412011-04-19 23:10:47 +00001076 // On ARM, a member function pointer is also non-null if the low bit of 'adj'
1077 // (the virtual bit) is set.
Mark Seabornedf0d382013-07-24 16:25:13 +00001078 if (UseARMMethodPtrABI) {
John McCall131d97d2010-08-22 08:30:07 +00001079 llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
John McCalla1dee5302010-08-22 10:59:02 +00001080 llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
John McCall131d97d2010-08-22 08:30:07 +00001081 llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
Daniel Dunbar914bc412011-04-19 23:10:47 +00001082 llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero,
1083 "memptr.isvirtual");
1084 Result = Builder.CreateOr(Result, IsVirtual);
John McCall131d97d2010-08-22 08:30:07 +00001085 }
1086
1087 return Result;
1088}
John McCall1c456c82010-08-22 06:43:33 +00001089
Reid Kleckner40ca9132014-05-13 22:05:45 +00001090bool ItaniumCXXABI::classifyReturnType(CGFunctionInfo &FI) const {
1091 const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl();
1092 if (!RD)
1093 return false;
1094
Richard Smith96cd6712017-08-16 01:49:53 +00001095 // If C++ prohibits us from making a copy, return by address.
Richard Smithf667ad52017-08-26 01:04:35 +00001096 if (passClassIndirect(RD)) {
John McCall7f416cc2015-09-08 08:05:57 +00001097 auto Align = CGM.getContext().getTypeAlignInChars(FI.getReturnType());
1098 FI.getReturnInfo() = ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
Reid Kleckner40ca9132014-05-13 22:05:45 +00001099 return true;
1100 }
Reid Kleckner40ca9132014-05-13 22:05:45 +00001101 return false;
1102}
1103
John McCall614dbdc2010-08-22 21:01:12 +00001104/// The Itanium ABI requires non-zero initialization only for data
1105/// member pointers, for which '0' is a valid offset.
1106bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
David Majnemer5fd33e02015-04-24 01:25:08 +00001107 return MPT->isMemberFunctionPointer();
John McCall84fa5102010-08-22 04:16:24 +00001108}
John McCall5d865c322010-08-31 07:33:07 +00001109
John McCall82fb8922012-09-25 10:10:39 +00001110/// The Itanium ABI always places an offset to the complete object
1111/// at entry -2 in the vtable.
David Majnemer08681372014-11-01 07:37:17 +00001112void ItaniumCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF,
1113 const CXXDeleteExpr *DE,
John McCall7f416cc2015-09-08 08:05:57 +00001114 Address Ptr,
David Majnemer08681372014-11-01 07:37:17 +00001115 QualType ElementType,
1116 const CXXDestructorDecl *Dtor) {
1117 bool UseGlobalDelete = DE->isGlobalDelete();
David Majnemer0c0b6d92014-10-31 20:09:12 +00001118 if (UseGlobalDelete) {
1119 // Derive the complete-object pointer, which is what we need
1120 // to pass to the deallocation function.
John McCall82fb8922012-09-25 10:10:39 +00001121
David Majnemer0c0b6d92014-10-31 20:09:12 +00001122 // Grab the vtable pointer as an intptr_t*.
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001123 auto *ClassDecl =
1124 cast<CXXRecordDecl>(ElementType->getAs<RecordType>()->getDecl());
1125 llvm::Value *VTable =
1126 CGF.GetVTablePtr(Ptr, CGF.IntPtrTy->getPointerTo(), ClassDecl);
John McCall82fb8922012-09-25 10:10:39 +00001127
David Majnemer0c0b6d92014-10-31 20:09:12 +00001128 // Track back to entry -2 and pull out the offset there.
1129 llvm::Value *OffsetPtr = CGF.Builder.CreateConstInBoundsGEP1_64(
1130 VTable, -2, "complete-offset.ptr");
John McCall7f416cc2015-09-08 08:05:57 +00001131 llvm::Value *Offset =
1132 CGF.Builder.CreateAlignedLoad(OffsetPtr, CGF.getPointerAlign());
David Majnemer0c0b6d92014-10-31 20:09:12 +00001133
1134 // Apply the offset.
John McCall7f416cc2015-09-08 08:05:57 +00001135 llvm::Value *CompletePtr =
1136 CGF.Builder.CreateBitCast(Ptr.getPointer(), CGF.Int8PtrTy);
David Majnemer0c0b6d92014-10-31 20:09:12 +00001137 CompletePtr = CGF.Builder.CreateInBoundsGEP(CompletePtr, Offset);
1138
1139 // If we're supposed to call the global delete, make sure we do so
1140 // even if the destructor throws.
David Majnemer08681372014-11-01 07:37:17 +00001141 CGF.pushCallObjectDeleteCleanup(DE->getOperatorDelete(), CompletePtr,
1142 ElementType);
David Majnemer0c0b6d92014-10-31 20:09:12 +00001143 }
1144
1145 // FIXME: Provide a source location here even though there's no
1146 // CXXMemberCallExpr for dtor call.
1147 CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting;
1148 EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, /*CE=*/nullptr);
1149
1150 if (UseGlobalDelete)
1151 CGF.PopCleanupBlock();
John McCall82fb8922012-09-25 10:10:39 +00001152}
1153
David Majnemer442d0a22014-11-25 07:20:20 +00001154void ItaniumCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) {
1155 // void __cxa_rethrow();
1156
1157 llvm::FunctionType *FTy =
1158 llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false);
1159
1160 llvm::Constant *Fn = CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow");
1161
1162 if (isNoReturn)
1163 CGF.EmitNoreturnRuntimeCallOrInvoke(Fn, None);
1164 else
1165 CGF.EmitRuntimeCallOrInvoke(Fn);
1166}
1167
David Majnemer7c237072015-03-05 00:46:22 +00001168static llvm::Constant *getAllocateExceptionFn(CodeGenModule &CGM) {
1169 // void *__cxa_allocate_exception(size_t thrown_size);
1170
1171 llvm::FunctionType *FTy =
1172 llvm::FunctionType::get(CGM.Int8PtrTy, CGM.SizeTy, /*IsVarArgs=*/false);
1173
1174 return CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception");
1175}
1176
1177static llvm::Constant *getThrowFn(CodeGenModule &CGM) {
1178 // void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
1179 // void (*dest) (void *));
1180
1181 llvm::Type *Args[3] = { CGM.Int8PtrTy, CGM.Int8PtrTy, CGM.Int8PtrTy };
1182 llvm::FunctionType *FTy =
1183 llvm::FunctionType::get(CGM.VoidTy, Args, /*IsVarArgs=*/false);
1184
1185 return CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
1186}
1187
1188void ItaniumCXXABI::emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) {
1189 QualType ThrowType = E->getSubExpr()->getType();
1190 // Now allocate the exception object.
1191 llvm::Type *SizeTy = CGF.ConvertType(getContext().getSizeType());
1192 uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity();
1193
1194 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(CGM);
1195 llvm::CallInst *ExceptionPtr = CGF.EmitNounwindRuntimeCall(
1196 AllocExceptionFn, llvm::ConstantInt::get(SizeTy, TypeSize), "exception");
1197
John McCall7f416cc2015-09-08 08:05:57 +00001198 CharUnits ExnAlign = getAlignmentOfExnObject();
1199 CGF.EmitAnyExprToExn(E->getSubExpr(), Address(ExceptionPtr, ExnAlign));
David Majnemer7c237072015-03-05 00:46:22 +00001200
1201 // Now throw the exception.
1202 llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType,
1203 /*ForEH=*/true);
1204
1205 // The address of the destructor. If the exception type has a
1206 // trivial destructor (or isn't a record), we just pass null.
1207 llvm::Constant *Dtor = nullptr;
1208 if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) {
1209 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
1210 if (!Record->hasTrivialDestructor()) {
1211 CXXDestructorDecl *DtorD = Record->getDestructor();
1212 Dtor = CGM.getAddrOfCXXStructor(DtorD, StructorType::Complete);
1213 Dtor = llvm::ConstantExpr::getBitCast(Dtor, CGM.Int8PtrTy);
1214 }
1215 }
1216 if (!Dtor) Dtor = llvm::Constant::getNullValue(CGM.Int8PtrTy);
1217
1218 llvm::Value *args[] = { ExceptionPtr, TypeInfo, Dtor };
1219 CGF.EmitNoreturnRuntimeCallOrInvoke(getThrowFn(CGM), args);
1220}
1221
David Majnemer1162d252014-06-22 19:05:33 +00001222static llvm::Constant *getItaniumDynamicCastFn(CodeGenFunction &CGF) {
1223 // void *__dynamic_cast(const void *sub,
1224 // const abi::__class_type_info *src,
1225 // const abi::__class_type_info *dst,
1226 // std::ptrdiff_t src2dst_offset);
Jake Ehrlichc451cf22017-11-11 01:15:41 +00001227
David Majnemer1162d252014-06-22 19:05:33 +00001228 llvm::Type *Int8PtrTy = CGF.Int8PtrTy;
Jake Ehrlichc451cf22017-11-11 01:15:41 +00001229 llvm::Type *PtrDiffTy =
David Majnemer1162d252014-06-22 19:05:33 +00001230 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1231
1232 llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy };
1233
1234 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
1235
1236 // Mark the function as nounwind readonly.
1237 llvm::Attribute::AttrKind FuncAttrs[] = { llvm::Attribute::NoUnwind,
1238 llvm::Attribute::ReadOnly };
Reid Klecknerde864822017-03-21 16:57:30 +00001239 llvm::AttributeList Attrs = llvm::AttributeList::get(
1240 CGF.getLLVMContext(), llvm::AttributeList::FunctionIndex, FuncAttrs);
David Majnemer1162d252014-06-22 19:05:33 +00001241
1242 return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast", Attrs);
1243}
1244
1245static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) {
1246 // void __cxa_bad_cast();
1247 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
1248 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast");
1249}
1250
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001251/// Compute the src2dst_offset hint as described in the
David Majnemer1162d252014-06-22 19:05:33 +00001252/// Itanium C++ ABI [2.9.7]
1253static CharUnits computeOffsetHint(ASTContext &Context,
1254 const CXXRecordDecl *Src,
1255 const CXXRecordDecl *Dst) {
1256 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
1257 /*DetectVirtual=*/false);
1258
1259 // If Dst is not derived from Src we can skip the whole computation below and
1260 // return that Src is not a public base of Dst. Record all inheritance paths.
1261 if (!Dst->isDerivedFrom(Src, Paths))
1262 return CharUnits::fromQuantity(-2ULL);
1263
1264 unsigned NumPublicPaths = 0;
1265 CharUnits Offset;
1266
1267 // Now walk all possible inheritance paths.
Piotr Padlewski44b4ce82015-07-28 16:10:58 +00001268 for (const CXXBasePath &Path : Paths) {
1269 if (Path.Access != AS_public) // Ignore non-public inheritance.
David Majnemer1162d252014-06-22 19:05:33 +00001270 continue;
1271
1272 ++NumPublicPaths;
1273
Piotr Padlewski44b4ce82015-07-28 16:10:58 +00001274 for (const CXXBasePathElement &PathElement : Path) {
David Majnemer1162d252014-06-22 19:05:33 +00001275 // If the path contains a virtual base class we can't give any hint.
1276 // -1: no hint.
Piotr Padlewski44b4ce82015-07-28 16:10:58 +00001277 if (PathElement.Base->isVirtual())
David Majnemer1162d252014-06-22 19:05:33 +00001278 return CharUnits::fromQuantity(-1ULL);
1279
1280 if (NumPublicPaths > 1) // Won't use offsets, skip computation.
1281 continue;
1282
1283 // Accumulate the base class offsets.
Piotr Padlewski44b4ce82015-07-28 16:10:58 +00001284 const ASTRecordLayout &L = Context.getASTRecordLayout(PathElement.Class);
1285 Offset += L.getBaseClassOffset(
1286 PathElement.Base->getType()->getAsCXXRecordDecl());
David Majnemer1162d252014-06-22 19:05:33 +00001287 }
1288 }
1289
1290 // -2: Src is not a public base of Dst.
1291 if (NumPublicPaths == 0)
1292 return CharUnits::fromQuantity(-2ULL);
1293
1294 // -3: Src is a multiple public base type but never a virtual base type.
1295 if (NumPublicPaths > 1)
1296 return CharUnits::fromQuantity(-3ULL);
1297
1298 // Otherwise, the Src type is a unique public nonvirtual base type of Dst.
1299 // Return the offset of Src from the origin of Dst.
1300 return Offset;
1301}
1302
1303static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) {
1304 // void __cxa_bad_typeid();
1305 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
1306
1307 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid");
1308}
1309
1310bool ItaniumCXXABI::shouldTypeidBeNullChecked(bool IsDeref,
1311 QualType SrcRecordTy) {
1312 return IsDeref;
1313}
1314
1315void ItaniumCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) {
1316 llvm::Value *Fn = getBadTypeidFn(CGF);
1317 CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn();
1318 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);
1414 CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn();
1415 CGF.Builder.CreateUnreachable();
1416 return true;
1417}
1418
Reid Klecknerd8cbeec2013-05-29 18:02:47 +00001419llvm::Value *
1420ItaniumCXXABI::GetVirtualBaseClassOffset(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00001421 Address This,
Reid Klecknerd8cbeec2013-05-29 18:02:47 +00001422 const CXXRecordDecl *ClassDecl,
1423 const CXXRecordDecl *BaseClassDecl) {
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001424 llvm::Value *VTablePtr = CGF.GetVTablePtr(This, CGM.Int8PtrTy, ClassDecl);
Reid Klecknerd8cbeec2013-05-29 18:02:47 +00001425 CharUnits VBaseOffsetOffset =
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001426 CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(ClassDecl,
1427 BaseClassDecl);
Reid Klecknerd8cbeec2013-05-29 18:02:47 +00001428
1429 llvm::Value *VBaseOffsetPtr =
1430 CGF.Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(),
1431 "vbase.offset.ptr");
1432 VBaseOffsetPtr = CGF.Builder.CreateBitCast(VBaseOffsetPtr,
1433 CGM.PtrDiffTy->getPointerTo());
1434
1435 llvm::Value *VBaseOffset =
John McCall7f416cc2015-09-08 08:05:57 +00001436 CGF.Builder.CreateAlignedLoad(VBaseOffsetPtr, CGF.getPointerAlign(),
1437 "vbase.offset");
Reid Klecknerd8cbeec2013-05-29 18:02:47 +00001438
1439 return VBaseOffset;
1440}
1441
Timur Iskhodzhanov40f2fa92013-08-04 17:30:04 +00001442void ItaniumCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) {
1443 // Just make sure we're in sync with TargetCXXABI.
1444 assert(CGM.getTarget().getCXXABI().hasConstructorVariants());
1445
Rafael Espindolac3cde362013-12-09 14:51:17 +00001446 // The constructor used for constructing this as a base class;
1447 // ignores virtual bases.
1448 CGM.EmitGlobal(GlobalDecl(D, Ctor_Base));
1449
Timur Iskhodzhanov40f2fa92013-08-04 17:30:04 +00001450 // The constructor used for constructing this as a complete class;
Nico Weber4c2ffb22015-01-07 05:25:05 +00001451 // constructs the virtual bases, then calls the base constructor.
Timur Iskhodzhanov40f2fa92013-08-04 17:30:04 +00001452 if (!D->getParent()->isAbstract()) {
1453 // We don't need to emit the complete ctor if the class is abstract.
1454 CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete));
1455 }
Timur Iskhodzhanov40f2fa92013-08-04 17:30:04 +00001456}
1457
George Burgess IVf203dbf2017-02-22 20:28:02 +00001458CGCXXABI::AddedStructorArgs
Rafael Espindola8d2a19b2014-09-08 16:01:27 +00001459ItaniumCXXABI::buildStructorSignature(const CXXMethodDecl *MD, StructorType T,
1460 SmallVectorImpl<CanQualType> &ArgTys) {
John McCall9bca9232010-09-02 10:25:57 +00001461 ASTContext &Context = getContext();
John McCall5d865c322010-08-31 07:33:07 +00001462
Rafael Espindola8d2a19b2014-09-08 16:01:27 +00001463 // All parameters are already in place except VTT, which goes after 'this'.
1464 // These are Clang types, so we don't need to worry about sret yet.
John McCall5d865c322010-08-31 07:33:07 +00001465
1466 // Check if we need to add a VTT parameter (which has type void **).
George Burgess IVf203dbf2017-02-22 20:28:02 +00001467 if (T == StructorType::Base && MD->getParent()->getNumVBases() != 0) {
Rafael Espindola8d2a19b2014-09-08 16:01:27 +00001468 ArgTys.insert(ArgTys.begin() + 1,
1469 Context.getPointerType(Context.VoidPtrTy));
George Burgess IVf203dbf2017-02-22 20:28:02 +00001470 return AddedStructorArgs::prefix(1);
1471 }
1472 return AddedStructorArgs{};
John McCall5d865c322010-08-31 07:33:07 +00001473}
1474
Reid Klecknere7de47e2013-07-22 13:51:44 +00001475void ItaniumCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) {
Rafael Espindolac3cde362013-12-09 14:51:17 +00001476 // The destructor used for destructing this as a base class; ignores
1477 // virtual bases.
1478 CGM.EmitGlobal(GlobalDecl(D, Dtor_Base));
Reid Klecknere7de47e2013-07-22 13:51:44 +00001479
1480 // The destructor used for destructing this as a most-derived class;
1481 // call the base destructor and then destructs any virtual bases.
1482 CGM.EmitGlobal(GlobalDecl(D, Dtor_Complete));
1483
Rafael Espindolac3cde362013-12-09 14:51:17 +00001484 // The destructor in a virtual table is always a 'deleting'
1485 // destructor, which calls the complete destructor and then uses the
1486 // appropriate operator delete.
1487 if (D->isVirtual())
1488 CGM.EmitGlobal(GlobalDecl(D, Dtor_Deleting));
Reid Klecknere7de47e2013-07-22 13:51:44 +00001489}
1490
Reid Kleckner89077a12013-12-17 19:46:40 +00001491void ItaniumCXXABI::addImplicitStructorParams(CodeGenFunction &CGF,
1492 QualType &ResTy,
1493 FunctionArgList &Params) {
John McCall5d865c322010-08-31 07:33:07 +00001494 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
Reid Kleckner89077a12013-12-17 19:46:40 +00001495 assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD));
John McCall5d865c322010-08-31 07:33:07 +00001496
1497 // Check if we need a VTT parameter as well.
Peter Collingbourne66f82e62013-06-28 20:45:28 +00001498 if (NeedsVTTParameter(CGF.CurGD)) {
John McCall9bca9232010-09-02 10:25:57 +00001499 ASTContext &Context = getContext();
John McCall5d865c322010-08-31 07:33:07 +00001500
1501 // FIXME: avoid the fake decl
1502 QualType T = Context.getPointerType(Context.VoidPtrTy);
Alexey Bataev56223232017-06-09 13:40:18 +00001503 auto *VTTDecl = ImplicitParamDecl::Create(
1504 Context, /*DC=*/nullptr, MD->getLocation(), &Context.Idents.get("vtt"),
1505 T, ImplicitParamDecl::CXXVTT);
Reid Kleckner89077a12013-12-17 19:46:40 +00001506 Params.insert(Params.begin() + 1, VTTDecl);
Reid Kleckner2af6d732013-12-13 00:09:59 +00001507 getStructorImplicitParamDecl(CGF) = VTTDecl;
John McCall5d865c322010-08-31 07:33:07 +00001508 }
1509}
1510
John McCall5d865c322010-08-31 07:33:07 +00001511void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
Justin Lebared4f1722016-07-27 22:04:24 +00001512 // Naked functions have no prolog.
1513 if (CGF.CurFuncDecl && CGF.CurFuncDecl->hasAttr<NakedAttr>())
1514 return;
1515
Reid Kleckner06239e42017-11-16 19:09:36 +00001516 /// Initialize the 'this' slot. In the Itanium C++ ABI, no prologue
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001517 /// adjustments are required, because they are all handled by thunks.
Reid Kleckner06239e42017-11-16 19:09:36 +00001518 setCXXABIThisValue(CGF, loadIncomingCXXThis(CGF));
John McCall5d865c322010-08-31 07:33:07 +00001519
1520 /// Initialize the 'vtt' slot if needed.
Reid Kleckner2af6d732013-12-13 00:09:59 +00001521 if (getStructorImplicitParamDecl(CGF)) {
1522 getStructorImplicitParamValue(CGF) = CGF.Builder.CreateLoad(
1523 CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), "vtt");
John McCall5d865c322010-08-31 07:33:07 +00001524 }
John McCall5d865c322010-08-31 07:33:07 +00001525
Stephen Lin9dc6eef2013-06-30 20:40:16 +00001526 /// If this is a function that the ABI specifies returns 'this', initialize
1527 /// the return slot to 'this' at the start of the function.
1528 ///
1529 /// Unlike the setting of return types, this is done within the ABI
1530 /// implementation instead of by clients of CGCXXABI because:
1531 /// 1) getThisValue is currently protected
1532 /// 2) in theory, an ABI could implement 'this' returns some other way;
1533 /// HasThisReturn only specifies a contract, not the implementation
John McCall5d865c322010-08-31 07:33:07 +00001534 if (HasThisReturn(CGF.CurGD))
Eli Friedman9fbeba02012-02-11 02:57:39 +00001535 CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
John McCall5d865c322010-08-31 07:33:07 +00001536}
1537
George Burgess IVf203dbf2017-02-22 20:28:02 +00001538CGCXXABI::AddedStructorArgs ItaniumCXXABI::addImplicitConstructorArgs(
Reid Kleckner89077a12013-12-17 19:46:40 +00001539 CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type,
1540 bool ForVirtualBase, bool Delegating, CallArgList &Args) {
1541 if (!NeedsVTTParameter(GlobalDecl(D, Type)))
George Burgess IVf203dbf2017-02-22 20:28:02 +00001542 return AddedStructorArgs{};
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001543
Reid Kleckner89077a12013-12-17 19:46:40 +00001544 // Insert the implicit 'vtt' argument as the second argument.
1545 llvm::Value *VTT =
1546 CGF.GetVTTParameter(GlobalDecl(D, Type), ForVirtualBase, Delegating);
1547 QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy);
Yaxun Liu5b330e82018-03-15 15:25:19 +00001548 Args.insert(Args.begin() + 1, CallArg(RValue::get(VTT), VTTTy));
George Burgess IVf203dbf2017-02-22 20:28:02 +00001549 return AddedStructorArgs::prefix(1); // Added one arg.
Reid Kleckner6fe771a2013-12-13 00:53:54 +00001550}
1551
1552void ItaniumCXXABI::EmitDestructorCall(CodeGenFunction &CGF,
1553 const CXXDestructorDecl *DD,
1554 CXXDtorType Type, bool ForVirtualBase,
John McCall7f416cc2015-09-08 08:05:57 +00001555 bool Delegating, Address This) {
Reid Kleckner6fe771a2013-12-13 00:53:54 +00001556 GlobalDecl GD(DD, Type);
1557 llvm::Value *VTT = CGF.GetVTTParameter(GD, ForVirtualBase, Delegating);
1558 QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy);
1559
John McCallb92ab1a2016-10-26 23:46:34 +00001560 CGCallee Callee;
1561 if (getContext().getLangOpts().AppleKext &&
1562 Type != Dtor_Base && DD->isVirtual())
Reid Kleckner6fe771a2013-12-13 00:53:54 +00001563 Callee = CGF.BuildAppleKextVirtualDestructorCall(DD, Type, DD->getParent());
John McCallb92ab1a2016-10-26 23:46:34 +00001564 else
Erich Keanede6480a32018-11-13 15:48:08 +00001565 Callee = CGCallee::forDirect(
1566 CGM.getAddrOfCXXStructor(DD, getFromDtorType(Type)), GD);
Reid Kleckner6fe771a2013-12-13 00:53:54 +00001567
John McCall7f416cc2015-09-08 08:05:57 +00001568 CGF.EmitCXXMemberOrOperatorCall(DD, Callee, ReturnValueSlot(),
Richard Smith762672a2016-09-28 19:09:10 +00001569 This.getPointer(), VTT, VTTTy,
1570 nullptr, nullptr);
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001571}
1572
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001573void ItaniumCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT,
1574 const CXXRecordDecl *RD) {
1575 llvm::GlobalVariable *VTable = getAddrOfVTable(RD, CharUnits());
1576 if (VTable->hasInitializer())
1577 return;
1578
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001579 ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext();
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001580 const VTableLayout &VTLayout = VTContext.getVTableLayout(RD);
1581 llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD);
David Majnemerd905da42014-07-01 20:30:31 +00001582 llvm::Constant *RTTI =
1583 CGM.GetAddrOfRTTIDescriptor(CGM.getContext().getTagDeclType(RD));
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001584
1585 // Create and set the initializer.
John McCall9c6cb762016-11-28 22:18:33 +00001586 ConstantInitBuilder Builder(CGM);
Peter Collingbourne2849c4e2016-12-13 20:40:39 +00001587 auto Components = Builder.beginStruct();
John McCall9c6cb762016-11-28 22:18:33 +00001588 CGVT.createVTableInitializer(Components, VTLayout, RTTI);
1589 Components.finishAndSetAsInitializer(VTable);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001590
1591 // Set the correct linkage.
1592 VTable->setLinkage(Linkage);
1593
NAKAMURA Takumic7da6da2015-05-09 21:10:07 +00001594 if (CGM.supportsCOMDAT() && VTable->isWeakForLinker())
1595 VTable->setComdat(CGM.getModule().getOrInsertComdat(VTable->getName()));
Rafael Espindolacb92c192015-01-15 23:18:01 +00001596
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001597 // Set the right visibility.
Rafael Espindola699f5d62018-02-07 22:15:33 +00001598 CGM.setGVProperties(VTable, RD);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001599
1600 // If this is the magic class __cxxabiv1::__fundamental_type_info,
1601 // we will emit the typeinfo for the fundamental types. This is the
1602 // same behaviour as GCC.
1603 const DeclContext *DC = RD->getDeclContext();
1604 if (RD->getIdentifier() &&
1605 RD->getIdentifier()->isStr("__fundamental_type_info") &&
1606 isa<NamespaceDecl>(DC) && cast<NamespaceDecl>(DC)->getIdentifier() &&
1607 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") &&
1608 DC->getParent()->isTranslationUnit())
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00001609 EmitFundamentalRTTIDescriptors(RD);
Peter Collingbournea4ccff32015-02-20 20:30:56 +00001610
Evgeniy Stepanov93987df2016-01-23 01:20:18 +00001611 if (!VTable->isDeclarationForLinker())
Peter Collingbourne8dd14da2016-06-24 21:21:46 +00001612 CGM.EmitVTableTypeMetadata(VTable, VTLayout);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001613}
1614
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00001615bool ItaniumCXXABI::isVirtualOffsetNeededForVTableField(
1616 CodeGenFunction &CGF, CodeGenFunction::VPtr Vptr) {
1617 if (Vptr.NearestVBase == nullptr)
1618 return false;
1619 return NeedsVTTParameter(CGF.CurGD);
Piotr Padlewski255652e2015-09-09 22:20:28 +00001620}
1621
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00001622llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructor(
1623 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base,
1624 const CXXRecordDecl *NearestVBase) {
1625
1626 if ((Base.getBase()->getNumVBases() || NearestVBase != nullptr) &&
1627 NeedsVTTParameter(CGF.CurGD)) {
1628 return getVTableAddressPointInStructorWithVTT(CGF, VTableClass, Base,
1629 NearestVBase);
1630 }
1631 return getVTableAddressPoint(Base, VTableClass);
1632}
1633
1634llvm::Constant *
1635ItaniumCXXABI::getVTableAddressPoint(BaseSubobject Base,
1636 const CXXRecordDecl *VTableClass) {
1637 llvm::GlobalValue *VTable = getAddrOfVTable(VTableClass, CharUnits());
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001638
Peter Collingbourne2849c4e2016-12-13 20:40:39 +00001639 // Find the appropriate vtable within the vtable group, and the address point
1640 // within that vtable.
1641 VTableLayout::AddressPointLocation AddressPoint =
1642 CGM.getItaniumVTableContext()
1643 .getVTableLayout(VTableClass)
1644 .getAddressPoint(Base);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001645 llvm::Value *Indices[] = {
Peter Collingbourne4e6a5402016-03-14 19:07:10 +00001646 llvm::ConstantInt::get(CGM.Int32Ty, 0),
Peter Collingbourne2849c4e2016-12-13 20:40:39 +00001647 llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.VTableIndex),
1648 llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.AddressPointIndex),
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001649 };
1650
Peter Collingbourne25a2b702016-12-13 20:50:44 +00001651 return llvm::ConstantExpr::getGetElementPtr(VTable->getValueType(), VTable,
1652 Indices, /*InBounds=*/true,
1653 /*InRangeIndex=*/1);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001654}
1655
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00001656llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructorWithVTT(
1657 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base,
1658 const CXXRecordDecl *NearestVBase) {
1659 assert((Base.getBase()->getNumVBases() || NearestVBase != nullptr) &&
1660 NeedsVTTParameter(CGF.CurGD) && "This class doesn't have VTT");
1661
1662 // Get the secondary vpointer index.
1663 uint64_t VirtualPointerIndex =
1664 CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base);
1665
1666 /// Load the VTT.
1667 llvm::Value *VTT = CGF.LoadCXXVTT();
1668 if (VirtualPointerIndex)
1669 VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex);
1670
1671 // And load the address point from the VTT.
1672 return CGF.Builder.CreateAlignedLoad(VTT, CGF.getPointerAlign());
1673}
1674
1675llvm::Constant *ItaniumCXXABI::getVTableAddressPointForConstExpr(
1676 BaseSubobject Base, const CXXRecordDecl *VTableClass) {
1677 return getVTableAddressPoint(Base, VTableClass);
1678}
1679
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001680llvm::GlobalVariable *ItaniumCXXABI::getAddrOfVTable(const CXXRecordDecl *RD,
1681 CharUnits VPtrOffset) {
1682 assert(VPtrOffset.isZero() && "Itanium ABI only supports zero vptr offsets");
1683
1684 llvm::GlobalVariable *&VTable = VTables[RD];
1685 if (VTable)
1686 return VTable;
1687
Eric Christopherd160c502016-01-29 01:35:53 +00001688 // Queue up this vtable for possible deferred emission.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001689 CGM.addDeferredVTable(RD);
1690
Yaron Kerene46f7ed2015-07-29 14:21:47 +00001691 SmallString<256> Name;
1692 llvm::raw_svector_ostream Out(Name);
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00001693 getMangleContext().mangleCXXVTable(RD, Out);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001694
Peter Collingbourne2849c4e2016-12-13 20:40:39 +00001695 const VTableLayout &VTLayout =
1696 CGM.getItaniumVTableContext().getVTableLayout(RD);
1697 llvm::Type *VTableType = CGM.getVTables().getVTableType(VTLayout);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001698
David Greenbe0c5b62018-09-12 14:09:06 +00001699 // Use pointer alignment for the vtable. Otherwise we would align them based
1700 // on the size of the initializer which doesn't make sense as only single
1701 // values are read.
1702 unsigned PAlign = CGM.getTarget().getPointerAlign(0);
1703
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001704 VTable = CGM.CreateOrReplaceCXXRuntimeVariable(
David Greenbe0c5b62018-09-12 14:09:06 +00001705 Name, VTableType, llvm::GlobalValue::ExternalLinkage,
1706 getContext().toCharUnitsFromBits(PAlign).getQuantity());
Peter Collingbournebcf909d2016-06-14 21:02:05 +00001707 VTable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
Hans Wennborgda24e9c2014-06-02 23:13:03 +00001708
Rafael Espindola922f2aa2018-02-23 19:30:48 +00001709 CGM.setGVProperties(VTable, RD);
1710
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001711 return VTable;
1712}
1713
John McCall9831b842018-02-06 18:52:44 +00001714CGCallee ItaniumCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF,
1715 GlobalDecl GD,
1716 Address This,
1717 llvm::Type *Ty,
1718 SourceLocation Loc) {
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +00001719 Ty = Ty->getPointerTo()->getPointerTo();
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00001720 auto *MethodDecl = cast<CXXMethodDecl>(GD.getDecl());
1721 llvm::Value *VTable = CGF.GetVTablePtr(This, Ty, MethodDecl->getParent());
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +00001722
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00001723 uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD);
John McCall9831b842018-02-06 18:52:44 +00001724 llvm::Value *VFunc;
1725 if (CGF.ShouldEmitVTableTypeCheckedLoad(MethodDecl->getParent())) {
1726 VFunc = CGF.EmitVTableTypeCheckedLoad(
Peter Collingbourne0ca03632016-06-25 00:24:06 +00001727 MethodDecl->getParent(), VTable,
1728 VTableIndex * CGM.getContext().getTargetInfo().getPointerWidth(0) / 8);
John McCall9831b842018-02-06 18:52:44 +00001729 } else {
1730 CGF.EmitTypeMetadataCodeForVCall(MethodDecl->getParent(), VTable, Loc);
Peter Collingbourne0ca03632016-06-25 00:24:06 +00001731
John McCall9831b842018-02-06 18:52:44 +00001732 llvm::Value *VFuncPtr =
1733 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn");
1734 auto *VFuncLoad =
1735 CGF.Builder.CreateAlignedLoad(VFuncPtr, CGF.getPointerAlign());
Piotr Padlewski77cc9622016-10-29 15:28:30 +00001736
John McCall9831b842018-02-06 18:52:44 +00001737 // Add !invariant.load md to virtual function load to indicate that
1738 // function didn't change inside vtable.
1739 // It's safe to add it without -fstrict-vtable-pointers, but it would not
1740 // help in devirtualization because it will only matter if we will have 2
1741 // the same virtual function loads from the same vtable load, which won't
1742 // happen without enabled devirtualization with -fstrict-vtable-pointers.
1743 if (CGM.getCodeGenOpts().OptimizationLevel > 0 &&
1744 CGM.getCodeGenOpts().StrictVTablePointers)
1745 VFuncLoad->setMetadata(
1746 llvm::LLVMContext::MD_invariant_load,
1747 llvm::MDNode::get(CGM.getLLVMContext(),
1748 llvm::ArrayRef<llvm::Metadata *>()));
1749 VFunc = VFuncLoad;
1750 }
John McCallb92ab1a2016-10-26 23:46:34 +00001751
Erich Keanede6480a32018-11-13 15:48:08 +00001752 CGCallee Callee(GD, VFunc);
John McCall9831b842018-02-06 18:52:44 +00001753 return Callee;
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +00001754}
1755
David Majnemer0c0b6d92014-10-31 20:09:12 +00001756llvm::Value *ItaniumCXXABI::EmitVirtualDestructorCall(
1757 CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType,
John McCall7f416cc2015-09-08 08:05:57 +00001758 Address This, const CXXMemberCallExpr *CE) {
Alexey Samsonova5bf76b2014-08-25 20:17:35 +00001759 assert(CE == nullptr || CE->arg_begin() == CE->arg_end());
Timur Iskhodzhanovd6197112013-02-15 14:45:22 +00001760 assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete);
1761
Rafael Espindola8d2a19b2014-09-08 16:01:27 +00001762 const CGFunctionInfo *FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration(
1763 Dtor, getFromDtorType(DtorType));
George Burgess IV00f70bd2018-03-01 05:43:23 +00001764 llvm::FunctionType *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo);
John McCallb92ab1a2016-10-26 23:46:34 +00001765 CGCallee Callee =
Peter Collingbourneea211002018-02-05 23:09:13 +00001766 CGCallee::forVirtual(CE, GlobalDecl(Dtor, DtorType), This, Ty);
Timur Iskhodzhanovd6197112013-02-15 14:45:22 +00001767
John McCall7f416cc2015-09-08 08:05:57 +00001768 CGF.EmitCXXMemberOrOperatorCall(Dtor, Callee, ReturnValueSlot(),
1769 This.getPointer(), /*ImplicitParam=*/nullptr,
Richard Smith762672a2016-09-28 19:09:10 +00001770 QualType(), CE, nullptr);
David Majnemer0c0b6d92014-10-31 20:09:12 +00001771 return nullptr;
Timur Iskhodzhanovd6197112013-02-15 14:45:22 +00001772}
1773
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001774void ItaniumCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) {
Reid Kleckner7810af02013-06-19 15:20:38 +00001775 CodeGenVTables &VTables = CGM.getVTables();
1776 llvm::GlobalVariable *VTT = VTables.GetAddrOfVTT(RD);
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001777 VTables.EmitVTTDefinition(VTT, CGM.getVTableLinkage(RD), RD);
Reid Kleckner7810af02013-06-19 15:20:38 +00001778}
1779
Richard Smithc195c252018-11-27 19:33:49 +00001780bool ItaniumCXXABI::canSpeculativelyEmitVTableAsBaseClass(
1781 const CXXRecordDecl *RD) const {
Piotr Padlewskia68a7872015-07-24 04:04:49 +00001782 // We don't emit available_externally vtables if we are in -fapple-kext mode
1783 // because kext mode does not permit devirtualization.
1784 if (CGM.getLangOpts().AppleKext)
1785 return false;
1786
Piotr Padlewskie368de32018-06-13 13:55:42 +00001787 // If the vtable is hidden then it is not safe to emit an available_externally
1788 // copy of vtable.
1789 if (isVTableHidden(RD))
1790 return false;
1791
1792 if (CGM.getCodeGenOpts().ForceEmitVTables)
1793 return true;
1794
1795 // If we don't have any not emitted inline virtual function then we are safe
1796 // to emit an available_externally copy of vtable.
Piotr Padlewskia68a7872015-07-24 04:04:49 +00001797 // FIXME we can still emit a copy of the vtable if we
1798 // can emit definition of the inline functions.
Richard Smithc195c252018-11-27 19:33:49 +00001799 if (hasAnyUnusedVirtualInlineFunction(RD))
1800 return false;
1801
1802 // For a class with virtual bases, we must also be able to speculatively
1803 // emit the VTT, because CodeGen doesn't have separate notions of "can emit
1804 // the vtable" and "can emit the VTT". For a base subobject, this means we
1805 // need to be able to emit non-virtual base vtables.
1806 if (RD->getNumVBases()) {
1807 for (const auto &B : RD->bases()) {
1808 auto *BRD = B.getType()->getAsCXXRecordDecl();
1809 assert(BRD && "no class for base specifier");
1810 if (B.isVirtual() || !BRD->isDynamicClass())
1811 continue;
1812 if (!canSpeculativelyEmitVTableAsBaseClass(BRD))
1813 return false;
1814 }
1815 }
1816
1817 return true;
1818}
1819
1820bool ItaniumCXXABI::canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const {
1821 if (!canSpeculativelyEmitVTableAsBaseClass(RD))
1822 return false;
1823
1824 // For a complete-object vtable (or more specifically, for the VTT), we need
1825 // to be able to speculatively emit the vtables of all dynamic virtual bases.
1826 for (const auto &B : RD->vbases()) {
1827 auto *BRD = B.getType()->getAsCXXRecordDecl();
1828 assert(BRD && "no class for base specifier");
1829 if (!BRD->isDynamicClass())
1830 continue;
1831 if (!canSpeculativelyEmitVTableAsBaseClass(BRD))
1832 return false;
1833 }
1834
1835 return true;
Piotr Padlewskia68a7872015-07-24 04:04:49 +00001836}
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001837static llvm::Value *performTypeAdjustment(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00001838 Address InitialPtr,
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001839 int64_t NonVirtualAdjustment,
1840 int64_t VirtualAdjustment,
1841 bool IsReturnAdjustment) {
1842 if (!NonVirtualAdjustment && !VirtualAdjustment)
John McCall7f416cc2015-09-08 08:05:57 +00001843 return InitialPtr.getPointer();
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001844
John McCall7f416cc2015-09-08 08:05:57 +00001845 Address V = CGF.Builder.CreateElementBitCast(InitialPtr, CGF.Int8Ty);
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001846
John McCall7f416cc2015-09-08 08:05:57 +00001847 // In a base-to-derived cast, the non-virtual adjustment is applied first.
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001848 if (NonVirtualAdjustment && !IsReturnAdjustment) {
John McCall7f416cc2015-09-08 08:05:57 +00001849 V = CGF.Builder.CreateConstInBoundsByteGEP(V,
1850 CharUnits::fromQuantity(NonVirtualAdjustment));
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001851 }
1852
John McCall7f416cc2015-09-08 08:05:57 +00001853 // Perform the virtual adjustment if we have one.
1854 llvm::Value *ResultPtr;
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001855 if (VirtualAdjustment) {
1856 llvm::Type *PtrDiffTy =
1857 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1858
John McCall7f416cc2015-09-08 08:05:57 +00001859 Address VTablePtrPtr = CGF.Builder.CreateElementBitCast(V, CGF.Int8PtrTy);
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001860 llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr);
1861
1862 llvm::Value *OffsetPtr =
1863 CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
1864
1865 OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo());
1866
1867 // Load the adjustment offset from the vtable.
John McCall7f416cc2015-09-08 08:05:57 +00001868 llvm::Value *Offset =
1869 CGF.Builder.CreateAlignedLoad(OffsetPtr, CGF.getPointerAlign());
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001870
1871 // Adjust our pointer.
John McCall7f416cc2015-09-08 08:05:57 +00001872 ResultPtr = CGF.Builder.CreateInBoundsGEP(V.getPointer(), Offset);
1873 } else {
1874 ResultPtr = V.getPointer();
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001875 }
1876
John McCall7f416cc2015-09-08 08:05:57 +00001877 // In a derived-to-base conversion, the non-virtual adjustment is
1878 // applied second.
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001879 if (NonVirtualAdjustment && IsReturnAdjustment) {
John McCall7f416cc2015-09-08 08:05:57 +00001880 ResultPtr = CGF.Builder.CreateConstInBoundsGEP1_64(ResultPtr,
1881 NonVirtualAdjustment);
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001882 }
1883
1884 // Cast back to the original type.
John McCall7f416cc2015-09-08 08:05:57 +00001885 return CGF.Builder.CreateBitCast(ResultPtr, InitialPtr.getType());
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001886}
1887
1888llvm::Value *ItaniumCXXABI::performThisAdjustment(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00001889 Address This,
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001890 const ThisAdjustment &TA) {
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00001891 return performTypeAdjustment(CGF, This, TA.NonVirtual,
1892 TA.Virtual.Itanium.VCallOffsetOffset,
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001893 /*IsReturnAdjustment=*/false);
1894}
1895
1896llvm::Value *
John McCall7f416cc2015-09-08 08:05:57 +00001897ItaniumCXXABI::performReturnAdjustment(CodeGenFunction &CGF, Address Ret,
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001898 const ReturnAdjustment &RA) {
1899 return performTypeAdjustment(CGF, Ret, RA.NonVirtual,
1900 RA.Virtual.Itanium.VBaseOffsetOffset,
1901 /*IsReturnAdjustment=*/true);
1902}
1903
John McCall5d865c322010-08-31 07:33:07 +00001904void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
1905 RValue RV, QualType ResultType) {
1906 if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
1907 return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
1908
1909 // Destructor thunks in the ARM ABI have indeterminate results.
John McCall7f416cc2015-09-08 08:05:57 +00001910 llvm::Type *T = CGF.ReturnValue.getElementType();
John McCall5d865c322010-08-31 07:33:07 +00001911 RValue Undef = RValue::get(llvm::UndefValue::get(T));
1912 return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
1913}
John McCall8ed55a52010-09-02 09:58:18 +00001914
1915/************************** Array allocation cookies **************************/
1916
John McCallb91cd662012-05-01 05:23:51 +00001917CharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) {
1918 // The array cookie is a size_t; pad that up to the element alignment.
1919 // The cookie is actually right-justified in that space.
1920 return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes),
1921 CGM.getContext().getTypeAlignInChars(elementType));
John McCall8ed55a52010-09-02 09:58:18 +00001922}
1923
John McCall7f416cc2015-09-08 08:05:57 +00001924Address ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
1925 Address NewPtr,
1926 llvm::Value *NumElements,
1927 const CXXNewExpr *expr,
1928 QualType ElementType) {
John McCallb91cd662012-05-01 05:23:51 +00001929 assert(requiresArrayCookie(expr));
John McCall8ed55a52010-09-02 09:58:18 +00001930
John McCall7f416cc2015-09-08 08:05:57 +00001931 unsigned AS = NewPtr.getAddressSpace();
John McCall8ed55a52010-09-02 09:58:18 +00001932
John McCall9bca9232010-09-02 10:25:57 +00001933 ASTContext &Ctx = getContext();
John McCall7f416cc2015-09-08 08:05:57 +00001934 CharUnits SizeSize = CGF.getSizeSize();
John McCall8ed55a52010-09-02 09:58:18 +00001935
1936 // The size of the cookie.
1937 CharUnits CookieSize =
1938 std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
John McCallb91cd662012-05-01 05:23:51 +00001939 assert(CookieSize == getArrayCookieSizeImpl(ElementType));
John McCall8ed55a52010-09-02 09:58:18 +00001940
1941 // Compute an offset to the cookie.
John McCall7f416cc2015-09-08 08:05:57 +00001942 Address CookiePtr = NewPtr;
John McCall8ed55a52010-09-02 09:58:18 +00001943 CharUnits CookieOffset = CookieSize - SizeSize;
1944 if (!CookieOffset.isZero())
John McCall7f416cc2015-09-08 08:05:57 +00001945 CookiePtr = CGF.Builder.CreateConstInBoundsByteGEP(CookiePtr, CookieOffset);
John McCall8ed55a52010-09-02 09:58:18 +00001946
1947 // Write the number of elements into the appropriate slot.
John McCall7f416cc2015-09-08 08:05:57 +00001948 Address NumElementsPtr =
1949 CGF.Builder.CreateElementBitCast(CookiePtr, CGF.SizeTy);
Kostya Serebryany4ee69042014-08-26 02:29:59 +00001950 llvm::Instruction *SI = CGF.Builder.CreateStore(NumElements, NumElementsPtr);
John McCall7f416cc2015-09-08 08:05:57 +00001951
1952 // Handle the array cookie specially in ASan.
Filipe Cabecinhas6f83fa92018-01-02 13:46:12 +00001953 if (CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) && AS == 0 &&
Filipe Cabecinhas4ba58172018-02-12 11:49:02 +00001954 (expr->getOperatorNew()->isReplaceableGlobalAllocationFunction() ||
Filipe Cabecinhas0eb50082018-11-02 17:29:04 +00001955 CGM.getCodeGenOpts().SanitizeAddressPoisonCustomArrayCookie)) {
Kostya Serebryany4a9187a2014-08-29 01:01:32 +00001956 // The store to the CookiePtr does not need to be instrumented.
Kostya Serebryany4ee69042014-08-26 02:29:59 +00001957 CGM.getSanitizerMetadata()->disableSanitizerForInstruction(SI);
1958 llvm::FunctionType *FTy =
John McCall7f416cc2015-09-08 08:05:57 +00001959 llvm::FunctionType::get(CGM.VoidTy, NumElementsPtr.getType(), false);
Kostya Serebryany4ee69042014-08-26 02:29:59 +00001960 llvm::Constant *F =
1961 CGM.CreateRuntimeFunction(FTy, "__asan_poison_cxx_array_cookie");
John McCall7f416cc2015-09-08 08:05:57 +00001962 CGF.Builder.CreateCall(F, NumElementsPtr.getPointer());
Kostya Serebryany4ee69042014-08-26 02:29:59 +00001963 }
John McCall8ed55a52010-09-02 09:58:18 +00001964
1965 // Finally, compute a pointer to the actual data buffer by skipping
1966 // over the cookie completely.
John McCall7f416cc2015-09-08 08:05:57 +00001967 return CGF.Builder.CreateConstInBoundsByteGEP(NewPtr, CookieSize);
John McCall8ed55a52010-09-02 09:58:18 +00001968}
1969
John McCallb91cd662012-05-01 05:23:51 +00001970llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00001971 Address allocPtr,
John McCallb91cd662012-05-01 05:23:51 +00001972 CharUnits cookieSize) {
1973 // The element size is right-justified in the cookie.
John McCall7f416cc2015-09-08 08:05:57 +00001974 Address numElementsPtr = allocPtr;
1975 CharUnits numElementsOffset = cookieSize - CGF.getSizeSize();
John McCallb91cd662012-05-01 05:23:51 +00001976 if (!numElementsOffset.isZero())
1977 numElementsPtr =
John McCall7f416cc2015-09-08 08:05:57 +00001978 CGF.Builder.CreateConstInBoundsByteGEP(numElementsPtr, numElementsOffset);
John McCall8ed55a52010-09-02 09:58:18 +00001979
John McCall7f416cc2015-09-08 08:05:57 +00001980 unsigned AS = allocPtr.getAddressSpace();
1981 numElementsPtr = CGF.Builder.CreateElementBitCast(numElementsPtr, CGF.SizeTy);
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001982 if (!CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) || AS != 0)
Kostya Serebryany4a9187a2014-08-29 01:01:32 +00001983 return CGF.Builder.CreateLoad(numElementsPtr);
1984 // In asan mode emit a function call instead of a regular load and let the
1985 // run-time deal with it: if the shadow is properly poisoned return the
1986 // cookie, otherwise return 0 to avoid an infinite loop calling DTORs.
1987 // We can't simply ignore this load using nosanitize metadata because
1988 // the metadata may be lost.
1989 llvm::FunctionType *FTy =
1990 llvm::FunctionType::get(CGF.SizeTy, CGF.SizeTy->getPointerTo(0), false);
1991 llvm::Constant *F =
1992 CGM.CreateRuntimeFunction(FTy, "__asan_load_cxx_array_cookie");
John McCall7f416cc2015-09-08 08:05:57 +00001993 return CGF.Builder.CreateCall(F, numElementsPtr.getPointer());
John McCall8ed55a52010-09-02 09:58:18 +00001994}
1995
John McCallb91cd662012-05-01 05:23:51 +00001996CharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) {
John McCallc19c7062013-01-25 23:36:19 +00001997 // ARM says that the cookie is always:
John McCall8ed55a52010-09-02 09:58:18 +00001998 // struct array_cookie {
1999 // std::size_t element_size; // element_size != 0
2000 // std::size_t element_count;
2001 // };
John McCallc19c7062013-01-25 23:36:19 +00002002 // But the base ABI doesn't give anything an alignment greater than
2003 // 8, so we can dismiss this as typical ABI-author blindness to
2004 // actual language complexity and round up to the element alignment.
2005 return std::max(CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes),
2006 CGM.getContext().getTypeAlignInChars(elementType));
John McCall8ed55a52010-09-02 09:58:18 +00002007}
2008
John McCall7f416cc2015-09-08 08:05:57 +00002009Address ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
2010 Address newPtr,
2011 llvm::Value *numElements,
2012 const CXXNewExpr *expr,
2013 QualType elementType) {
John McCallb91cd662012-05-01 05:23:51 +00002014 assert(requiresArrayCookie(expr));
John McCall8ed55a52010-09-02 09:58:18 +00002015
John McCall8ed55a52010-09-02 09:58:18 +00002016 // The cookie is always at the start of the buffer.
John McCall7f416cc2015-09-08 08:05:57 +00002017 Address cookie = newPtr;
John McCall8ed55a52010-09-02 09:58:18 +00002018
2019 // The first element is the element size.
John McCall7f416cc2015-09-08 08:05:57 +00002020 cookie = CGF.Builder.CreateElementBitCast(cookie, CGF.SizeTy);
John McCallc19c7062013-01-25 23:36:19 +00002021 llvm::Value *elementSize = llvm::ConstantInt::get(CGF.SizeTy,
2022 getContext().getTypeSizeInChars(elementType).getQuantity());
2023 CGF.Builder.CreateStore(elementSize, cookie);
John McCall8ed55a52010-09-02 09:58:18 +00002024
2025 // The second element is the element count.
John McCall7f416cc2015-09-08 08:05:57 +00002026 cookie = CGF.Builder.CreateConstInBoundsGEP(cookie, 1, CGF.getSizeSize());
John McCallc19c7062013-01-25 23:36:19 +00002027 CGF.Builder.CreateStore(numElements, cookie);
John McCall8ed55a52010-09-02 09:58:18 +00002028
2029 // Finally, compute a pointer to the actual data buffer by skipping
2030 // over the cookie completely.
John McCallc19c7062013-01-25 23:36:19 +00002031 CharUnits cookieSize = ARMCXXABI::getArrayCookieSizeImpl(elementType);
John McCall7f416cc2015-09-08 08:05:57 +00002032 return CGF.Builder.CreateConstInBoundsByteGEP(newPtr, cookieSize);
John McCall8ed55a52010-09-02 09:58:18 +00002033}
2034
John McCallb91cd662012-05-01 05:23:51 +00002035llvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00002036 Address allocPtr,
John McCallb91cd662012-05-01 05:23:51 +00002037 CharUnits cookieSize) {
2038 // The number of elements is at offset sizeof(size_t) relative to
2039 // the allocated pointer.
John McCall7f416cc2015-09-08 08:05:57 +00002040 Address numElementsPtr
2041 = CGF.Builder.CreateConstInBoundsByteGEP(allocPtr, CGF.getSizeSize());
John McCall8ed55a52010-09-02 09:58:18 +00002042
John McCall7f416cc2015-09-08 08:05:57 +00002043 numElementsPtr = CGF.Builder.CreateElementBitCast(numElementsPtr, CGF.SizeTy);
John McCallb91cd662012-05-01 05:23:51 +00002044 return CGF.Builder.CreateLoad(numElementsPtr);
John McCall8ed55a52010-09-02 09:58:18 +00002045}
2046
John McCall68ff0372010-09-08 01:44:27 +00002047/*********************** Static local initialization **************************/
2048
2049static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM,
Chris Lattnera5f58b02011-07-09 17:41:47 +00002050 llvm::PointerType *GuardPtrTy) {
John McCall68ff0372010-09-08 01:44:27 +00002051 // int __cxa_guard_acquire(__guard *guard_object);
Chris Lattner2192fe52011-07-18 04:24:23 +00002052 llvm::FunctionType *FTy =
John McCall68ff0372010-09-08 01:44:27 +00002053 llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy),
Jay Foad5709f7c2011-07-29 13:56:53 +00002054 GuardPtrTy, /*isVarArg=*/false);
Reid Klecknerde864822017-03-21 16:57:30 +00002055 return CGM.CreateRuntimeFunction(
2056 FTy, "__cxa_guard_acquire",
2057 llvm::AttributeList::get(CGM.getLLVMContext(),
2058 llvm::AttributeList::FunctionIndex,
2059 llvm::Attribute::NoUnwind));
John McCall68ff0372010-09-08 01:44:27 +00002060}
2061
2062static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM,
Chris Lattnera5f58b02011-07-09 17:41:47 +00002063 llvm::PointerType *GuardPtrTy) {
John McCall68ff0372010-09-08 01:44:27 +00002064 // void __cxa_guard_release(__guard *guard_object);
Chris Lattner2192fe52011-07-18 04:24:23 +00002065 llvm::FunctionType *FTy =
Chris Lattnerece04092012-02-07 00:39:47 +00002066 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
Reid Klecknerde864822017-03-21 16:57:30 +00002067 return CGM.CreateRuntimeFunction(
2068 FTy, "__cxa_guard_release",
2069 llvm::AttributeList::get(CGM.getLLVMContext(),
2070 llvm::AttributeList::FunctionIndex,
2071 llvm::Attribute::NoUnwind));
John McCall68ff0372010-09-08 01:44:27 +00002072}
2073
2074static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM,
Chris Lattnera5f58b02011-07-09 17:41:47 +00002075 llvm::PointerType *GuardPtrTy) {
John McCall68ff0372010-09-08 01:44:27 +00002076 // void __cxa_guard_abort(__guard *guard_object);
Chris Lattner2192fe52011-07-18 04:24:23 +00002077 llvm::FunctionType *FTy =
Chris Lattnerece04092012-02-07 00:39:47 +00002078 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
Reid Klecknerde864822017-03-21 16:57:30 +00002079 return CGM.CreateRuntimeFunction(
2080 FTy, "__cxa_guard_abort",
2081 llvm::AttributeList::get(CGM.getLLVMContext(),
2082 llvm::AttributeList::FunctionIndex,
2083 llvm::Attribute::NoUnwind));
John McCall68ff0372010-09-08 01:44:27 +00002084}
2085
2086namespace {
David Blaikie7e70d682015-08-18 22:40:54 +00002087 struct CallGuardAbort final : EHScopeStack::Cleanup {
John McCall68ff0372010-09-08 01:44:27 +00002088 llvm::GlobalVariable *Guard;
Chandler Carruth84537952012-03-30 19:44:53 +00002089 CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
John McCall68ff0372010-09-08 01:44:27 +00002090
Craig Topper4f12f102014-03-12 06:41:41 +00002091 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCall882987f2013-02-28 19:01:20 +00002092 CGF.EmitNounwindRuntimeCall(getGuardAbortFn(CGF.CGM, Guard->getType()),
2093 Guard);
John McCall68ff0372010-09-08 01:44:27 +00002094 }
2095 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00002096}
John McCall68ff0372010-09-08 01:44:27 +00002097
2098/// The ARM code here follows the Itanium code closely enough that we
2099/// just special-case it at particular places.
John McCallcdf7ef52010-11-06 09:44:32 +00002100void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
2101 const VarDecl &D,
John McCallb88a5662012-03-30 21:00:39 +00002102 llvm::GlobalVariable *var,
2103 bool shouldPerformInit) {
John McCall68ff0372010-09-08 01:44:27 +00002104 CGBuilderTy &Builder = CGF.Builder;
John McCallcdf7ef52010-11-06 09:44:32 +00002105
Richard Smith62f19e72016-06-25 00:15:56 +00002106 // Inline variables that weren't instantiated from variable templates have
2107 // partially-ordered initialization within their translation unit.
2108 bool NonTemplateInline =
2109 D.isInline() &&
2110 !isTemplateInstantiation(D.getTemplateSpecializationKind());
2111
2112 // We only need to use thread-safe statics for local non-TLS variables and
2113 // inline variables; other global initialization is always single-threaded
2114 // or (through lazy dynamic loading in multiple threads) unsequenced.
Richard Smithdbf74ba2013-04-14 23:01:42 +00002115 bool threadsafe = getContext().getLangOpts().ThreadsafeStatics &&
Richard Smith62f19e72016-06-25 00:15:56 +00002116 (D.isLocalVarDecl() || NonTemplateInline) &&
2117 !D.getTLSKind();
Anders Carlssonc5d3ba12011-04-27 04:37:08 +00002118
Anders Carlssonc5d3ba12011-04-27 04:37:08 +00002119 // If we have a global variable with internal linkage and thread-safe statics
2120 // are disabled, we can just let the guard variable be of type i8.
John McCallb88a5662012-03-30 21:00:39 +00002121 bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage();
2122
2123 llvm::IntegerType *guardTy;
John McCall7f416cc2015-09-08 08:05:57 +00002124 CharUnits guardAlignment;
John McCall5aa52592011-06-17 07:33:57 +00002125 if (useInt8GuardVariable) {
John McCallb88a5662012-03-30 21:00:39 +00002126 guardTy = CGF.Int8Ty;
John McCall7f416cc2015-09-08 08:05:57 +00002127 guardAlignment = CharUnits::One();
John McCall5aa52592011-06-17 07:33:57 +00002128 } else {
Tim Northover9bb857a2013-01-31 12:13:10 +00002129 // Guard variables are 64 bits in the generic ABI and size width on ARM
2130 // (i.e. 32-bit on AArch32, 64-bit on AArch64).
John McCall7f416cc2015-09-08 08:05:57 +00002131 if (UseARMGuardVarABI) {
2132 guardTy = CGF.SizeTy;
2133 guardAlignment = CGF.getSizeAlign();
2134 } else {
2135 guardTy = CGF.Int64Ty;
2136 guardAlignment = CharUnits::fromQuantity(
2137 CGM.getDataLayout().getABITypeAlignment(guardTy));
2138 }
Anders Carlssonc5d3ba12011-04-27 04:37:08 +00002139 }
John McCallb88a5662012-03-30 21:00:39 +00002140 llvm::PointerType *guardPtrTy = guardTy->getPointerTo();
John McCall68ff0372010-09-08 01:44:27 +00002141
John McCallb88a5662012-03-30 21:00:39 +00002142 // Create the guard variable if we don't already have it (as we
2143 // might if we're double-emitting this function body).
2144 llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D);
2145 if (!guard) {
2146 // Mangle the name for the guard.
2147 SmallString<256> guardName;
2148 {
2149 llvm::raw_svector_ostream out(guardName);
Reid Klecknerd8110b62013-09-10 20:14:30 +00002150 getMangleContext().mangleStaticGuardVariable(&D, out);
John McCallb88a5662012-03-30 21:00:39 +00002151 }
John McCall8e7cb6d2010-11-02 21:04:24 +00002152
John McCallb88a5662012-03-30 21:00:39 +00002153 // Create the guard variable with a zero-initializer.
2154 // Just absorb linkage and visibility from the guarded variable.
2155 guard = new llvm::GlobalVariable(CGM.getModule(), guardTy,
2156 false, var->getLinkage(),
2157 llvm::ConstantInt::get(guardTy, 0),
2158 guardName.str());
Rafael Espindola699f5d62018-02-07 22:15:33 +00002159 guard->setDSOLocal(var->isDSOLocal());
John McCallb88a5662012-03-30 21:00:39 +00002160 guard->setVisibility(var->getVisibility());
Richard Smithdbf74ba2013-04-14 23:01:42 +00002161 // If the variable is thread-local, so is its guard variable.
2162 guard->setThreadLocalMode(var->getThreadLocalMode());
John McCall7f416cc2015-09-08 08:05:57 +00002163 guard->setAlignment(guardAlignment.getQuantity());
John McCallb88a5662012-03-30 21:00:39 +00002164
Yaron Keren5bfa1082015-09-03 20:33:29 +00002165 // The ABI says: "It is suggested that it be emitted in the same COMDAT
2166 // group as the associated data object." In practice, this doesn't work for
Dan Gohman839f2152017-01-17 21:46:38 +00002167 // non-ELF and non-Wasm object formats, so only do it for ELF and Wasm.
Rafael Espindola0d4fb982015-01-12 22:13:53 +00002168 llvm::Comdat *C = var->getComdat();
Yaron Keren5bfa1082015-09-03 20:33:29 +00002169 if (!D.isLocalVarDecl() && C &&
Dan Gohman839f2152017-01-17 21:46:38 +00002170 (CGM.getTarget().getTriple().isOSBinFormatELF() ||
2171 CGM.getTarget().getTriple().isOSBinFormatWasm())) {
Rafael Espindola2ae4b632014-09-19 19:43:18 +00002172 guard->setComdat(C);
Richard Smith62f19e72016-06-25 00:15:56 +00002173 // An inline variable's guard function is run from the per-TU
2174 // initialization function, not via a dedicated global ctor function, so
2175 // we can't put it in a comdat.
2176 if (!NonTemplateInline)
2177 CGF.CurFn->setComdat(C);
NAKAMURA Takumic7da6da2015-05-09 21:10:07 +00002178 } else if (CGM.supportsCOMDAT() && guard->isWeakForLinker()) {
2179 guard->setComdat(CGM.getModule().getOrInsertComdat(guard->getName()));
Rafael Espindola2ae4b632014-09-19 19:43:18 +00002180 }
2181
John McCallb88a5662012-03-30 21:00:39 +00002182 CGM.setStaticLocalDeclGuardAddress(&D, guard);
2183 }
John McCall87590e62012-03-30 07:09:50 +00002184
John McCall7f416cc2015-09-08 08:05:57 +00002185 Address guardAddr = Address(guard, guardAlignment);
2186
John McCall68ff0372010-09-08 01:44:27 +00002187 // Test whether the variable has completed initialization.
Justin Bogner0cbb6d82014-04-23 01:50:10 +00002188 //
John McCall68ff0372010-09-08 01:44:27 +00002189 // Itanium C++ ABI 3.3.2:
2190 // The following is pseudo-code showing how these functions can be used:
2191 // if (obj_guard.first_byte == 0) {
2192 // if ( __cxa_guard_acquire (&obj_guard) ) {
2193 // try {
2194 // ... initialize the object ...;
2195 // } catch (...) {
2196 // __cxa_guard_abort (&obj_guard);
2197 // throw;
2198 // }
2199 // ... queue object destructor with __cxa_atexit() ...;
2200 // __cxa_guard_release (&obj_guard);
2201 // }
2202 // }
Tim Northovera2ee4332014-03-29 15:09:45 +00002203
Justin Bogner0cbb6d82014-04-23 01:50:10 +00002204 // Load the first byte of the guard variable.
2205 llvm::LoadInst *LI =
John McCall7f416cc2015-09-08 08:05:57 +00002206 Builder.CreateLoad(Builder.CreateElementBitCast(guardAddr, CGM.Int8Ty));
John McCall68ff0372010-09-08 01:44:27 +00002207
Justin Bogner0cbb6d82014-04-23 01:50:10 +00002208 // Itanium ABI:
2209 // An implementation supporting thread-safety on multiprocessor
2210 // systems must also guarantee that references to the initialized
2211 // object do not occur before the load of the initialization flag.
2212 //
2213 // In LLVM, we do this by marking the load Acquire.
2214 if (threadsafe)
JF Bastien92f4ef12016-04-06 17:26:42 +00002215 LI->setAtomic(llvm::AtomicOrdering::Acquire);
Eli Friedman84d28122011-09-13 22:21:56 +00002216
Justin Bogner0cbb6d82014-04-23 01:50:10 +00002217 // For ARM, we should only check the first bit, rather than the entire byte:
2218 //
2219 // ARM C++ ABI 3.2.3.1:
2220 // To support the potential use of initialization guard variables
2221 // as semaphores that are the target of ARM SWP and LDREX/STREX
2222 // synchronizing instructions we define a static initialization
2223 // guard variable to be a 4-byte aligned, 4-byte word with the
2224 // following inline access protocol.
2225 // #define INITIALIZED 1
2226 // if ((obj_guard & INITIALIZED) != INITIALIZED) {
2227 // if (__cxa_guard_acquire(&obj_guard))
2228 // ...
2229 // }
2230 //
2231 // and similarly for ARM64:
2232 //
2233 // ARM64 C++ ABI 3.2.2:
2234 // This ABI instead only specifies the value bit 0 of the static guard
2235 // variable; all other bits are platform defined. Bit 0 shall be 0 when the
2236 // variable is not initialized and 1 when it is.
2237 llvm::Value *V =
2238 (UseARMGuardVarABI && !useInt8GuardVariable)
2239 ? Builder.CreateAnd(LI, llvm::ConstantInt::get(CGM.Int8Ty, 1))
2240 : LI;
Richard Smithae8d62c2017-07-26 22:01:09 +00002241 llvm::Value *NeedsInit = Builder.CreateIsNull(V, "guard.uninitialized");
John McCall68ff0372010-09-08 01:44:27 +00002242
2243 llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check");
2244 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
2245
2246 // Check if the first byte of the guard variable is zero.
Richard Smithae8d62c2017-07-26 22:01:09 +00002247 CGF.EmitCXXGuardedInitBranch(NeedsInit, InitCheckBlock, EndBlock,
2248 CodeGenFunction::GuardKind::VariableGuard, &D);
John McCall68ff0372010-09-08 01:44:27 +00002249
2250 CGF.EmitBlock(InitCheckBlock);
2251
2252 // Variables used when coping with thread-safe statics and exceptions.
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002253 if (threadsafe) {
John McCall68ff0372010-09-08 01:44:27 +00002254 // Call __cxa_guard_acquire.
2255 llvm::Value *V
John McCall882987f2013-02-28 19:01:20 +00002256 = CGF.EmitNounwindRuntimeCall(getGuardAcquireFn(CGM, guardPtrTy), guard);
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002257
John McCall68ff0372010-09-08 01:44:27 +00002258 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002259
John McCall68ff0372010-09-08 01:44:27 +00002260 Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
2261 InitBlock, EndBlock);
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002262
John McCall68ff0372010-09-08 01:44:27 +00002263 // Call __cxa_guard_abort along the exceptional edge.
John McCallb88a5662012-03-30 21:00:39 +00002264 CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard);
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002265
John McCall68ff0372010-09-08 01:44:27 +00002266 CGF.EmitBlock(InitBlock);
2267 }
2268
2269 // Emit the initializer and add a global destructor if appropriate.
John McCallb88a5662012-03-30 21:00:39 +00002270 CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit);
John McCall68ff0372010-09-08 01:44:27 +00002271
John McCall5aa52592011-06-17 07:33:57 +00002272 if (threadsafe) {
John McCall68ff0372010-09-08 01:44:27 +00002273 // Pop the guard-abort cleanup if we pushed one.
2274 CGF.PopCleanupBlock();
2275
2276 // Call __cxa_guard_release. This cannot throw.
John McCall7f416cc2015-09-08 08:05:57 +00002277 CGF.EmitNounwindRuntimeCall(getGuardReleaseFn(CGM, guardPtrTy),
2278 guardAddr.getPointer());
John McCall68ff0372010-09-08 01:44:27 +00002279 } else {
John McCall7f416cc2015-09-08 08:05:57 +00002280 Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guardAddr);
John McCall68ff0372010-09-08 01:44:27 +00002281 }
2282
2283 CGF.EmitBlock(EndBlock);
2284}
John McCallc84ed6a2012-05-01 06:13:13 +00002285
2286/// Register a global destructor using __cxa_atexit.
2287static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF,
2288 llvm::Constant *dtor,
Richard Smithdbf74ba2013-04-14 23:01:42 +00002289 llvm::Constant *addr,
2290 bool TLS) {
Bill Wendling95cae882013-05-02 19:18:03 +00002291 const char *Name = "__cxa_atexit";
2292 if (TLS) {
2293 const llvm::Triple &T = CGF.getTarget().getTriple();
Manman Renf93fff22015-11-11 23:08:18 +00002294 Name = T.isOSDarwin() ? "_tlv_atexit" : "__cxa_thread_atexit";
Bill Wendling95cae882013-05-02 19:18:03 +00002295 }
Richard Smithdbf74ba2013-04-14 23:01:42 +00002296
John McCallc84ed6a2012-05-01 06:13:13 +00002297 // We're assuming that the destructor function is something we can
2298 // reasonably call with the default CC. Go ahead and cast it to the
2299 // right prototype.
2300 llvm::Type *dtorTy =
2301 llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo();
2302
2303 // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d);
2304 llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy };
2305 llvm::FunctionType *atexitTy =
2306 llvm::FunctionType::get(CGF.IntTy, paramTys, false);
2307
2308 // Fetch the actual function.
Richard Smithdbf74ba2013-04-14 23:01:42 +00002309 llvm::Constant *atexit = CGF.CGM.CreateRuntimeFunction(atexitTy, Name);
John McCallc84ed6a2012-05-01 06:13:13 +00002310 if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit))
2311 fn->setDoesNotThrow();
2312
2313 // Create a variable that binds the atexit to this shared object.
2314 llvm::Constant *handle =
Reid Kleckner9de92142017-02-13 18:49:21 +00002315 CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle");
2316 auto *GV = cast<llvm::GlobalValue>(handle->stripPointerCasts());
2317 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
John McCallc84ed6a2012-05-01 06:13:13 +00002318
Akira Hatanaka617e2612018-04-17 18:41:52 +00002319 if (!addr)
2320 // addr is null when we are trying to register a dtor annotated with
2321 // __attribute__((destructor)) in a constructor function. Using null here is
2322 // okay because this argument is just passed back to the destructor
2323 // function.
2324 addr = llvm::Constant::getNullValue(CGF.Int8PtrTy);
2325
John McCallc84ed6a2012-05-01 06:13:13 +00002326 llvm::Value *args[] = {
2327 llvm::ConstantExpr::getBitCast(dtor, dtorTy),
2328 llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy),
2329 handle
2330 };
John McCall882987f2013-02-28 19:01:20 +00002331 CGF.EmitNounwindRuntimeCall(atexit, args);
John McCallc84ed6a2012-05-01 06:13:13 +00002332}
2333
Akira Hatanaka617e2612018-04-17 18:41:52 +00002334void CodeGenModule::registerGlobalDtorsWithAtExit() {
2335 for (const auto I : DtorsUsingAtExit) {
2336 int Priority = I.first;
2337 const llvm::TinyPtrVector<llvm::Function *> &Dtors = I.second;
2338
2339 // Create a function that registers destructors that have the same priority.
2340 //
2341 // Since constructor functions are run in non-descending order of their
2342 // priorities, destructors are registered in non-descending order of their
2343 // priorities, and since destructor functions are run in the reverse order
2344 // of their registration, destructor functions are run in non-ascending
2345 // order of their priorities.
2346 CodeGenFunction CGF(*this);
2347 std::string GlobalInitFnName =
2348 std::string("__GLOBAL_init_") + llvm::to_string(Priority);
2349 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
2350 llvm::Function *GlobalInitFn = CreateGlobalInitOrDestructFunction(
2351 FTy, GlobalInitFnName, getTypes().arrangeNullaryFunction(),
2352 SourceLocation());
2353 ASTContext &Ctx = getContext();
Jonas Devlieghere64a26302018-11-11 00:56:15 +00002354 QualType ReturnTy = Ctx.VoidTy;
2355 QualType FunctionTy = Ctx.getFunctionType(ReturnTy, llvm::None, {});
Akira Hatanaka617e2612018-04-17 18:41:52 +00002356 FunctionDecl *FD = FunctionDecl::Create(
2357 Ctx, Ctx.getTranslationUnitDecl(), SourceLocation(), SourceLocation(),
Jonas Devlieghere64a26302018-11-11 00:56:15 +00002358 &Ctx.Idents.get(GlobalInitFnName), FunctionTy, nullptr, SC_Static,
Akira Hatanaka617e2612018-04-17 18:41:52 +00002359 false, false);
Jonas Devlieghere64a26302018-11-11 00:56:15 +00002360 CGF.StartFunction(GlobalDecl(FD), ReturnTy, GlobalInitFn,
Akira Hatanaka617e2612018-04-17 18:41:52 +00002361 getTypes().arrangeNullaryFunction(), FunctionArgList(),
2362 SourceLocation(), SourceLocation());
2363
2364 for (auto *Dtor : Dtors) {
2365 // Register the destructor function calling __cxa_atexit if it is
2366 // available. Otherwise fall back on calling atexit.
2367 if (getCodeGenOpts().CXAAtExit)
2368 emitGlobalDtorWithCXAAtExit(CGF, Dtor, nullptr, false);
2369 else
2370 CGF.registerGlobalDtorWithAtExit(Dtor);
2371 }
2372
2373 CGF.FinishFunction();
2374 AddGlobalCtor(GlobalInitFn, Priority, nullptr);
2375 }
2376}
2377
John McCallc84ed6a2012-05-01 06:13:13 +00002378/// Register a global destructor as best as we know how.
2379void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF,
Richard Smithdbf74ba2013-04-14 23:01:42 +00002380 const VarDecl &D,
John McCallc84ed6a2012-05-01 06:13:13 +00002381 llvm::Constant *dtor,
2382 llvm::Constant *addr) {
Erik Pilkington5a559e62018-08-21 17:24:06 +00002383 if (D.isNoDestroy(CGM.getContext()))
2384 return;
2385
John McCallc84ed6a2012-05-01 06:13:13 +00002386 // Use __cxa_atexit if available.
Richard Smithdbf74ba2013-04-14 23:01:42 +00002387 if (CGM.getCodeGenOpts().CXAAtExit)
2388 return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr, D.getTLSKind());
2389
2390 if (D.getTLSKind())
2391 CGM.ErrorUnsupported(&D, "non-trivial TLS destruction");
John McCallc84ed6a2012-05-01 06:13:13 +00002392
2393 // In Apple kexts, we want to add a global destructor entry.
2394 // FIXME: shouldn't this be guarded by some variable?
Richard Smith9c6890a2012-11-01 22:30:59 +00002395 if (CGM.getLangOpts().AppleKext) {
John McCallc84ed6a2012-05-01 06:13:13 +00002396 // Generate a global destructor entry.
2397 return CGM.AddCXXDtorEntry(dtor, addr);
2398 }
2399
David Blaikieebe87e12013-08-27 23:57:18 +00002400 CGF.registerGlobalDtorWithAtExit(D, dtor, addr);
John McCallc84ed6a2012-05-01 06:13:13 +00002401}
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002402
David Majnemer9b21c332014-07-11 20:28:10 +00002403static bool isThreadWrapperReplaceable(const VarDecl *VD,
2404 CodeGen::CodeGenModule &CGM) {
2405 assert(!VD->isStaticLocal() && "static local VarDecls don't need wrappers!");
Manman Renf93fff22015-11-11 23:08:18 +00002406 // Darwin prefers to have references to thread local variables to go through
David Majnemer9b21c332014-07-11 20:28:10 +00002407 // the thread wrapper instead of directly referencing the backing variable.
2408 return VD->getTLSKind() == VarDecl::TLS_Dynamic &&
Manman Renf93fff22015-11-11 23:08:18 +00002409 CGM.getTarget().getTriple().isOSDarwin();
David Majnemer9b21c332014-07-11 20:28:10 +00002410}
2411
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002412/// Get the appropriate linkage for the wrapper function. This is essentially
David Majnemer4632e1e2014-06-27 16:56:27 +00002413/// the weak form of the variable's linkage; every translation unit which needs
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002414/// the wrapper emits a copy, and we want the linker to merge them.
David Majnemer35ab3282014-06-11 04:08:55 +00002415static llvm::GlobalValue::LinkageTypes
2416getThreadLocalWrapperLinkage(const VarDecl *VD, CodeGen::CodeGenModule &CGM) {
2417 llvm::GlobalValue::LinkageTypes VarLinkage =
2418 CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false);
2419
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002420 // For internal linkage variables, we don't need an external or weak wrapper.
2421 if (llvm::GlobalValue::isLocalLinkage(VarLinkage))
2422 return VarLinkage;
David Majnemer35ab3282014-06-11 04:08:55 +00002423
David Majnemer9b21c332014-07-11 20:28:10 +00002424 // If the thread wrapper is replaceable, give it appropriate linkage.
Manman Ren68150262015-11-11 22:42:31 +00002425 if (isThreadWrapperReplaceable(VD, CGM))
2426 if (!llvm::GlobalVariable::isLinkOnceLinkage(VarLinkage) &&
2427 !llvm::GlobalVariable::isWeakODRLinkage(VarLinkage))
2428 return VarLinkage;
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002429 return llvm::GlobalValue::WeakODRLinkage;
2430}
2431
2432llvm::Function *
2433ItaniumCXXABI::getOrCreateThreadLocalWrapper(const VarDecl *VD,
Alexander Musmanf94c3182014-09-26 06:28:25 +00002434 llvm::Value *Val) {
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002435 // Mangle the name for the thread_local wrapper function.
2436 SmallString<256> WrapperName;
2437 {
2438 llvm::raw_svector_ostream Out(WrapperName);
2439 getMangleContext().mangleItaniumThreadLocalWrapper(VD, Out);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002440 }
2441
Akira Hatanaka26907f92016-01-15 03:34:06 +00002442 // FIXME: If VD is a definition, we should regenerate the function attributes
2443 // before returning.
Alexander Musmanf94c3182014-09-26 06:28:25 +00002444 if (llvm::Value *V = CGM.getModule().getNamedValue(WrapperName))
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002445 return cast<llvm::Function>(V);
2446
Akira Hatanaka26907f92016-01-15 03:34:06 +00002447 QualType RetQT = VD->getType();
2448 if (RetQT->isReferenceType())
2449 RetQT = RetQT.getNonReferenceType();
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002450
John McCallc56a8b32016-03-11 04:30:31 +00002451 const CGFunctionInfo &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(
2452 getContext().getPointerType(RetQT), FunctionArgList());
Akira Hatanaka26907f92016-01-15 03:34:06 +00002453
2454 llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FI);
David Majnemer35ab3282014-06-11 04:08:55 +00002455 llvm::Function *Wrapper =
2456 llvm::Function::Create(FnTy, getThreadLocalWrapperLinkage(VD, CGM),
2457 WrapperName.str(), &CGM.getModule());
Akira Hatanaka26907f92016-01-15 03:34:06 +00002458
Erich Keanede6480a32018-11-13 15:48:08 +00002459 CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, Wrapper);
Akira Hatanaka26907f92016-01-15 03:34:06 +00002460
2461 if (VD->hasDefinition())
2462 CGM.SetLLVMFunctionAttributesForDefinition(nullptr, Wrapper);
2463
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002464 // Always resolve references to the wrapper at link time.
Vlad Tsyrklevichc93390b2019-01-17 17:53:45 +00002465 if (!Wrapper->hasLocalLinkage())
2466 if (!isThreadWrapperReplaceable(VD, CGM) ||
2467 llvm::GlobalVariable::isLinkOnceLinkage(Wrapper->getLinkage()) ||
2468 llvm::GlobalVariable::isWeakODRLinkage(Wrapper->getLinkage()) ||
2469 VD->getVisibility() == HiddenVisibility)
2470 Wrapper->setVisibility(llvm::GlobalValue::HiddenVisibility);
Manman Renb0b3af72015-12-17 00:42:36 +00002471
2472 if (isThreadWrapperReplaceable(VD, CGM)) {
2473 Wrapper->setCallingConv(llvm::CallingConv::CXX_FAST_TLS);
2474 Wrapper->addFnAttr(llvm::Attribute::NoUnwind);
2475 }
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002476 return Wrapper;
2477}
2478
2479void ItaniumCXXABI::EmitThreadLocalInitFuncs(
Richard Smith5a99c492015-12-01 01:10:48 +00002480 CodeGenModule &CGM, ArrayRef<const VarDecl *> CXXThreadLocals,
2481 ArrayRef<llvm::Function *> CXXThreadLocalInits,
2482 ArrayRef<const VarDecl *> CXXThreadLocalInitVars) {
David Majnemerb3341ea2014-10-05 05:05:40 +00002483 llvm::Function *InitFunc = nullptr;
Richard Smithfbe23692017-01-13 00:43:31 +00002484
2485 // Separate initializers into those with ordered (or partially-ordered)
2486 // initialization and those with unordered initialization.
2487 llvm::SmallVector<llvm::Function *, 8> OrderedInits;
2488 llvm::SmallDenseMap<const VarDecl *, llvm::Function *> UnorderedInits;
2489 for (unsigned I = 0; I != CXXThreadLocalInits.size(); ++I) {
2490 if (isTemplateInstantiation(
2491 CXXThreadLocalInitVars[I]->getTemplateSpecializationKind()))
2492 UnorderedInits[CXXThreadLocalInitVars[I]->getCanonicalDecl()] =
2493 CXXThreadLocalInits[I];
2494 else
2495 OrderedInits.push_back(CXXThreadLocalInits[I]);
2496 }
2497
2498 if (!OrderedInits.empty()) {
David Majnemerb3341ea2014-10-05 05:05:40 +00002499 // Generate a guarded initialization function.
2500 llvm::FunctionType *FTy =
2501 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
Akira Hatanaka7791f1a42015-10-31 01:28:07 +00002502 const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction();
2503 InitFunc = CGM.CreateGlobalInitOrDestructFunction(FTy, "__tls_init", FI,
Alexey Samsonov1444bb92014-10-17 00:20:19 +00002504 SourceLocation(),
David Majnemerb3341ea2014-10-05 05:05:40 +00002505 /*TLS=*/true);
2506 llvm::GlobalVariable *Guard = new llvm::GlobalVariable(
2507 CGM.getModule(), CGM.Int8Ty, /*isConstant=*/false,
2508 llvm::GlobalVariable::InternalLinkage,
2509 llvm::ConstantInt::get(CGM.Int8Ty, 0), "__tls_guard");
2510 Guard->setThreadLocal(true);
John McCall7f416cc2015-09-08 08:05:57 +00002511
2512 CharUnits GuardAlign = CharUnits::One();
2513 Guard->setAlignment(GuardAlign.getQuantity());
2514
Richard Smith3ad06362018-10-31 20:39:26 +00002515 CodeGenFunction(CGM).GenerateCXXGlobalInitFunc(
2516 InitFunc, OrderedInits, ConstantAddress(Guard, GuardAlign));
Manman Ren5e5d0462016-03-18 23:35:21 +00002517 // On Darwin platforms, use CXX_FAST_TLS calling convention.
2518 if (CGM.getTarget().getTriple().isOSDarwin()) {
2519 InitFunc->setCallingConv(llvm::CallingConv::CXX_FAST_TLS);
2520 InitFunc->addFnAttr(llvm::Attribute::NoUnwind);
2521 }
David Majnemerb3341ea2014-10-05 05:05:40 +00002522 }
Richard Smithfbe23692017-01-13 00:43:31 +00002523
2524 // Emit thread wrappers.
Richard Smith5a99c492015-12-01 01:10:48 +00002525 for (const VarDecl *VD : CXXThreadLocals) {
2526 llvm::GlobalVariable *Var =
2527 cast<llvm::GlobalVariable>(CGM.GetGlobalValue(CGM.getMangledName(VD)));
Richard Smithfbe23692017-01-13 00:43:31 +00002528 llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Var);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002529
David Majnemer9b21c332014-07-11 20:28:10 +00002530 // Some targets require that all access to thread local variables go through
2531 // the thread wrapper. This means that we cannot attempt to create a thread
2532 // wrapper or a thread helper.
Richard Smithfbe23692017-01-13 00:43:31 +00002533 if (isThreadWrapperReplaceable(VD, CGM) && !VD->hasDefinition()) {
2534 Wrapper->setLinkage(llvm::Function::ExternalLinkage);
David Majnemer9b21c332014-07-11 20:28:10 +00002535 continue;
Richard Smithfbe23692017-01-13 00:43:31 +00002536 }
David Majnemer9b21c332014-07-11 20:28:10 +00002537
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002538 // Mangle the name for the thread_local initialization function.
2539 SmallString<256> InitFnName;
2540 {
2541 llvm::raw_svector_ostream Out(InitFnName);
2542 getMangleContext().mangleItaniumThreadLocalInit(VD, Out);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002543 }
2544
2545 // If we have a definition for the variable, emit the initialization
2546 // function as an alias to the global Init function (if any). Otherwise,
2547 // produce a declaration of the initialization function.
Craig Topper8a13c412014-05-21 05:09:00 +00002548 llvm::GlobalValue *Init = nullptr;
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002549 bool InitIsInitFunc = false;
2550 if (VD->hasDefinition()) {
2551 InitIsInitFunc = true;
Richard Smithfbe23692017-01-13 00:43:31 +00002552 llvm::Function *InitFuncToUse = InitFunc;
2553 if (isTemplateInstantiation(VD->getTemplateSpecializationKind()))
2554 InitFuncToUse = UnorderedInits.lookup(VD->getCanonicalDecl());
2555 if (InitFuncToUse)
Rafael Espindola234405b2014-05-17 21:30:14 +00002556 Init = llvm::GlobalAlias::create(Var->getLinkage(), InitFnName.str(),
Richard Smithfbe23692017-01-13 00:43:31 +00002557 InitFuncToUse);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002558 } else {
2559 // Emit a weak global function referring to the initialization function.
2560 // This function will not exist if the TU defining the thread_local
2561 // variable in question does not need any dynamic initialization for
2562 // its thread_local variables.
2563 llvm::FunctionType *FnTy = llvm::FunctionType::get(CGM.VoidTy, false);
Richard Smithfbe23692017-01-13 00:43:31 +00002564 Init = llvm::Function::Create(FnTy,
2565 llvm::GlobalVariable::ExternalWeakLinkage,
2566 InitFnName.str(), &CGM.getModule());
John McCallc56a8b32016-03-11 04:30:31 +00002567 const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction();
Erich Keanede6480a32018-11-13 15:48:08 +00002568 CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI,
2569 cast<llvm::Function>(Init));
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002570 }
2571
Rafael Espindolaabdb3222018-03-07 23:18:06 +00002572 if (Init) {
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002573 Init->setVisibility(Var->getVisibility());
Rafael Espindolaabdb3222018-03-07 23:18:06 +00002574 Init->setDSOLocal(Var->isDSOLocal());
2575 }
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002576
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002577 llvm::LLVMContext &Context = CGM.getModule().getContext();
2578 llvm::BasicBlock *Entry = llvm::BasicBlock::Create(Context, "", Wrapper);
John McCall7f416cc2015-09-08 08:05:57 +00002579 CGBuilderTy Builder(CGM, Entry);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002580 if (InitIsInitFunc) {
Manman Ren5e5d0462016-03-18 23:35:21 +00002581 if (Init) {
2582 llvm::CallInst *CallVal = Builder.CreateCall(Init);
Akira Hatanaka1da9dbb2018-05-29 18:28:49 +00002583 if (isThreadWrapperReplaceable(VD, CGM)) {
Manman Ren5e5d0462016-03-18 23:35:21 +00002584 CallVal->setCallingConv(llvm::CallingConv::CXX_FAST_TLS);
Akira Hatanaka1da9dbb2018-05-29 18:28:49 +00002585 llvm::Function *Fn =
2586 cast<llvm::Function>(cast<llvm::GlobalAlias>(Init)->getAliasee());
2587 Fn->setCallingConv(llvm::CallingConv::CXX_FAST_TLS);
2588 }
Manman Ren5e5d0462016-03-18 23:35:21 +00002589 }
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002590 } else {
2591 // Don't know whether we have an init function. Call it if it exists.
2592 llvm::Value *Have = Builder.CreateIsNotNull(Init);
2593 llvm::BasicBlock *InitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
2594 llvm::BasicBlock *ExitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
2595 Builder.CreateCondBr(Have, InitBB, ExitBB);
2596
2597 Builder.SetInsertPoint(InitBB);
David Blaikie4ba525b2015-07-14 17:27:39 +00002598 Builder.CreateCall(Init);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002599 Builder.CreateBr(ExitBB);
2600
2601 Builder.SetInsertPoint(ExitBB);
2602 }
2603
2604 // For a reference, the result of the wrapper function is a pointer to
2605 // the referenced object.
2606 llvm::Value *Val = Var;
2607 if (VD->getType()->isReferenceType()) {
John McCall7f416cc2015-09-08 08:05:57 +00002608 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2609 Val = Builder.CreateAlignedLoad(Val, Align);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002610 }
Alexander Musmanf94c3182014-09-26 06:28:25 +00002611 if (Val->getType() != Wrapper->getReturnType())
2612 Val = Builder.CreatePointerBitCastOrAddrSpaceCast(
2613 Val, Wrapper->getReturnType(), "");
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002614 Builder.CreateRet(Val);
2615 }
2616}
2617
Richard Smith0f383742014-03-26 22:48:22 +00002618LValue ItaniumCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
2619 const VarDecl *VD,
2620 QualType LValType) {
Richard Smith5a99c492015-12-01 01:10:48 +00002621 llvm::Value *Val = CGF.CGM.GetAddrOfGlobalVar(VD);
Alexander Musmanf94c3182014-09-26 06:28:25 +00002622 llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Val);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002623
Manman Renb0b3af72015-12-17 00:42:36 +00002624 llvm::CallInst *CallVal = CGF.Builder.CreateCall(Wrapper);
Saleem Abdulrasool4a7130a2016-08-01 21:31:24 +00002625 CallVal->setCallingConv(Wrapper->getCallingConv());
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002626
2627 LValue LV;
2628 if (VD->getType()->isReferenceType())
Manman Renb0b3af72015-12-17 00:42:36 +00002629 LV = CGF.MakeNaturalAlignAddrLValue(CallVal, LValType);
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002630 else
Manman Renb0b3af72015-12-17 00:42:36 +00002631 LV = CGF.MakeAddrLValue(CallVal, LValType,
2632 CGF.getContext().getDeclAlign(VD));
Richard Smith2fd1d7a2013-04-19 16:42:07 +00002633 // FIXME: need setObjCGCLValueClass?
2634 return LV;
2635}
Peter Collingbourne66f82e62013-06-28 20:45:28 +00002636
2637/// Return whether the given global decl needs a VTT parameter, which it does
2638/// if it's a base constructor or destructor with virtual bases.
2639bool ItaniumCXXABI::NeedsVTTParameter(GlobalDecl GD) {
2640 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002641
Peter Collingbourne66f82e62013-06-28 20:45:28 +00002642 // We don't have any virtual bases, just return early.
2643 if (!MD->getParent()->getNumVBases())
2644 return false;
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002645
Peter Collingbourne66f82e62013-06-28 20:45:28 +00002646 // Check if we have a base constructor.
2647 if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base)
2648 return true;
2649
2650 // Check if we have a base destructor.
2651 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
2652 return true;
Jake Ehrlichc451cf22017-11-11 01:15:41 +00002653
Peter Collingbourne66f82e62013-06-28 20:45:28 +00002654 return false;
2655}
David Majnemere2cb8d12014-07-07 06:20:47 +00002656
2657namespace {
2658class ItaniumRTTIBuilder {
2659 CodeGenModule &CGM; // Per-module state.
2660 llvm::LLVMContext &VMContext;
2661 const ItaniumCXXABI &CXXABI; // Per-module state.
2662
2663 /// Fields - The fields of the RTTI descriptor currently being built.
2664 SmallVector<llvm::Constant *, 16> Fields;
2665
2666 /// GetAddrOfTypeName - Returns the mangled type name of the given type.
2667 llvm::GlobalVariable *
2668 GetAddrOfTypeName(QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage);
2669
2670 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
2671 /// descriptor of the given type.
2672 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
2673
2674 /// BuildVTablePointer - Build the vtable pointer for the given type.
2675 void BuildVTablePointer(const Type *Ty);
2676
2677 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
2678 /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
2679 void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
2680
2681 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
2682 /// classes with bases that do not satisfy the abi::__si_class_type_info
2683 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
2684 void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
2685
2686 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
2687 /// for pointer types.
2688 void BuildPointerTypeInfo(QualType PointeeTy);
2689
2690 /// BuildObjCObjectTypeInfo - Build the appropriate kind of
2691 /// type_info for an object type.
2692 void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty);
2693
2694 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
2695 /// struct, used for member pointer types.
2696 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
2697
2698public:
2699 ItaniumRTTIBuilder(const ItaniumCXXABI &ABI)
2700 : CGM(ABI.CGM), VMContext(CGM.getModule().getContext()), CXXABI(ABI) {}
2701
2702 // Pointer type info flags.
2703 enum {
2704 /// PTI_Const - Type has const qualifier.
2705 PTI_Const = 0x1,
2706
2707 /// PTI_Volatile - Type has volatile qualifier.
2708 PTI_Volatile = 0x2,
2709
2710 /// PTI_Restrict - Type has restrict qualifier.
2711 PTI_Restrict = 0x4,
2712
2713 /// PTI_Incomplete - Type is incomplete.
2714 PTI_Incomplete = 0x8,
2715
2716 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
2717 /// (in pointer to member).
Richard Smitha7d93782016-12-01 03:32:42 +00002718 PTI_ContainingClassIncomplete = 0x10,
2719
2720 /// PTI_TransactionSafe - Pointee is transaction_safe function (C++ TM TS).
2721 //PTI_TransactionSafe = 0x20,
2722
2723 /// PTI_Noexcept - Pointee is noexcept function (C++1z).
2724 PTI_Noexcept = 0x40,
David Majnemere2cb8d12014-07-07 06:20:47 +00002725 };
2726
2727 // VMI type info flags.
2728 enum {
2729 /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
2730 VMI_NonDiamondRepeat = 0x1,
2731
2732 /// VMI_DiamondShaped - Class is diamond shaped.
2733 VMI_DiamondShaped = 0x2
2734 };
2735
2736 // Base class type info flags.
2737 enum {
2738 /// BCTI_Virtual - Base class is virtual.
2739 BCTI_Virtual = 0x1,
2740
2741 /// BCTI_Public - Base class is public.
2742 BCTI_Public = 0x2
2743 };
2744
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00002745 /// BuildTypeInfo - Build the RTTI type info struct for the given type, or
2746 /// link to an existing RTTI descriptor if one already exists.
2747 llvm::Constant *BuildTypeInfo(QualType Ty);
2748
David Majnemere2cb8d12014-07-07 06:20:47 +00002749 /// BuildTypeInfo - Build the RTTI type info struct for the given type.
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00002750 llvm::Constant *BuildTypeInfo(
2751 QualType Ty,
2752 llvm::GlobalVariable::LinkageTypes Linkage,
2753 llvm::GlobalValue::VisibilityTypes Visibility,
2754 llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass);
David Majnemere2cb8d12014-07-07 06:20:47 +00002755};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00002756}
David Majnemere2cb8d12014-07-07 06:20:47 +00002757
2758llvm::GlobalVariable *ItaniumRTTIBuilder::GetAddrOfTypeName(
2759 QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage) {
Yaron Kerene46f7ed2015-07-29 14:21:47 +00002760 SmallString<256> Name;
2761 llvm::raw_svector_ostream Out(Name);
David Majnemere2cb8d12014-07-07 06:20:47 +00002762 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
David Majnemere2cb8d12014-07-07 06:20:47 +00002763
2764 // We know that the mangled name of the type starts at index 4 of the
2765 // mangled name of the typename, so we can just index into it in order to
2766 // get the mangled name of the type.
2767 llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext,
2768 Name.substr(4));
David Greenbe0c5b62018-09-12 14:09:06 +00002769 auto Align = CGM.getContext().getTypeAlignInChars(CGM.getContext().CharTy);
David Majnemere2cb8d12014-07-07 06:20:47 +00002770
David Greenbe0c5b62018-09-12 14:09:06 +00002771 llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable(
2772 Name, Init->getType(), Linkage, Align.getQuantity());
David Majnemere2cb8d12014-07-07 06:20:47 +00002773
2774 GV->setInitializer(Init);
2775
2776 return GV;
2777}
2778
2779llvm::Constant *
2780ItaniumRTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
2781 // Mangle the RTTI name.
Yaron Kerene46f7ed2015-07-29 14:21:47 +00002782 SmallString<256> Name;
2783 llvm::raw_svector_ostream Out(Name);
David Majnemere2cb8d12014-07-07 06:20:47 +00002784 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
David Majnemere2cb8d12014-07-07 06:20:47 +00002785
2786 // Look for an existing global.
2787 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
2788
2789 if (!GV) {
2790 // Create a new global variable.
Piotr Padlewskid3b1cbd2017-06-01 08:04:05 +00002791 // Note for the future: If we would ever like to do deferred emission of
2792 // RTTI, check if emitting vtables opportunistically need any adjustment.
2793
David Majnemere2cb8d12014-07-07 06:20:47 +00002794 GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy,
2795 /*Constant=*/true,
2796 llvm::GlobalValue::ExternalLinkage, nullptr,
2797 Name);
Rafael Espindola3f727a82018-03-14 18:14:46 +00002798 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
2799 CGM.setGVProperties(GV, RD);
David Majnemere2cb8d12014-07-07 06:20:47 +00002800 }
2801
2802 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
2803}
2804
2805/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
2806/// info for that type is defined in the standard library.
2807static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
2808 // Itanium C++ ABI 2.9.2:
2809 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
2810 // the run-time support library. Specifically, the run-time support
2811 // library should contain type_info objects for the types X, X* and
2812 // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char,
2813 // unsigned char, signed char, short, unsigned short, int, unsigned int,
2814 // long, unsigned long, long long, unsigned long long, float, double,
2815 // long double, char16_t, char32_t, and the IEEE 754r decimal and
2816 // half-precision floating point types.
Richard Smith4a382012016-02-03 01:32:42 +00002817 //
2818 // GCC also emits RTTI for __int128.
2819 // FIXME: We do not emit RTTI information for decimal types here.
2820
2821 // Types added here must also be added to EmitFundamentalRTTIDescriptors.
David Majnemere2cb8d12014-07-07 06:20:47 +00002822 switch (Ty->getKind()) {
2823 case BuiltinType::Void:
2824 case BuiltinType::NullPtr:
2825 case BuiltinType::Bool:
2826 case BuiltinType::WChar_S:
2827 case BuiltinType::WChar_U:
2828 case BuiltinType::Char_U:
2829 case BuiltinType::Char_S:
2830 case BuiltinType::UChar:
2831 case BuiltinType::SChar:
2832 case BuiltinType::Short:
2833 case BuiltinType::UShort:
2834 case BuiltinType::Int:
2835 case BuiltinType::UInt:
2836 case BuiltinType::Long:
2837 case BuiltinType::ULong:
2838 case BuiltinType::LongLong:
2839 case BuiltinType::ULongLong:
2840 case BuiltinType::Half:
2841 case BuiltinType::Float:
2842 case BuiltinType::Double:
2843 case BuiltinType::LongDouble:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00002844 case BuiltinType::Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00002845 case BuiltinType::Float128:
Richard Smith3a8244d2018-05-01 05:02:45 +00002846 case BuiltinType::Char8:
David Majnemere2cb8d12014-07-07 06:20:47 +00002847 case BuiltinType::Char16:
2848 case BuiltinType::Char32:
2849 case BuiltinType::Int128:
2850 case BuiltinType::UInt128:
Richard Smith4a382012016-02-03 01:32:42 +00002851 return true;
2852
Alexey Bader954ba212016-04-08 13:40:33 +00002853#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
2854 case BuiltinType::Id:
Alexey Baderb62f1442016-04-13 08:33:41 +00002855#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +00002856#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
2857 case BuiltinType::Id:
2858#include "clang/Basic/OpenCLExtensionTypes.def"
David Majnemere2cb8d12014-07-07 06:20:47 +00002859 case BuiltinType::OCLSampler:
2860 case BuiltinType::OCLEvent:
Alexey Bader9c8453f2015-09-15 11:18:52 +00002861 case BuiltinType::OCLClkEvent:
2862 case BuiltinType::OCLQueue:
Alexey Bader9c8453f2015-09-15 11:18:52 +00002863 case BuiltinType::OCLReserveID:
Leonard Chanf921d852018-06-04 16:07:52 +00002864 case BuiltinType::ShortAccum:
2865 case BuiltinType::Accum:
2866 case BuiltinType::LongAccum:
2867 case BuiltinType::UShortAccum:
2868 case BuiltinType::UAccum:
2869 case BuiltinType::ULongAccum:
Leonard Chanab80f3c2018-06-14 14:53:51 +00002870 case BuiltinType::ShortFract:
2871 case BuiltinType::Fract:
2872 case BuiltinType::LongFract:
2873 case BuiltinType::UShortFract:
2874 case BuiltinType::UFract:
2875 case BuiltinType::ULongFract:
2876 case BuiltinType::SatShortAccum:
2877 case BuiltinType::SatAccum:
2878 case BuiltinType::SatLongAccum:
2879 case BuiltinType::SatUShortAccum:
2880 case BuiltinType::SatUAccum:
2881 case BuiltinType::SatULongAccum:
2882 case BuiltinType::SatShortFract:
2883 case BuiltinType::SatFract:
2884 case BuiltinType::SatLongFract:
2885 case BuiltinType::SatUShortFract:
2886 case BuiltinType::SatUFract:
2887 case BuiltinType::SatULongFract:
Richard Smith4a382012016-02-03 01:32:42 +00002888 return false;
David Majnemere2cb8d12014-07-07 06:20:47 +00002889
2890 case BuiltinType::Dependent:
2891#define BUILTIN_TYPE(Id, SingletonId)
2892#define PLACEHOLDER_TYPE(Id, SingletonId) \
2893 case BuiltinType::Id:
2894#include "clang/AST/BuiltinTypes.def"
2895 llvm_unreachable("asking for RRTI for a placeholder type!");
2896
2897 case BuiltinType::ObjCId:
2898 case BuiltinType::ObjCClass:
2899 case BuiltinType::ObjCSel:
2900 llvm_unreachable("FIXME: Objective-C types are unsupported!");
2901 }
2902
2903 llvm_unreachable("Invalid BuiltinType Kind!");
2904}
2905
2906static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
2907 QualType PointeeTy = PointerTy->getPointeeType();
2908 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
2909 if (!BuiltinTy)
2910 return false;
2911
2912 // Check the qualifiers.
2913 Qualifiers Quals = PointeeTy.getQualifiers();
2914 Quals.removeConst();
2915
2916 if (!Quals.empty())
2917 return false;
2918
2919 return TypeInfoIsInStandardLibrary(BuiltinTy);
2920}
2921
2922/// IsStandardLibraryRTTIDescriptor - Returns whether the type
2923/// information for the given type exists in the standard library.
2924static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
2925 // Type info for builtin types is defined in the standard library.
2926 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
2927 return TypeInfoIsInStandardLibrary(BuiltinTy);
2928
2929 // Type info for some pointer types to builtin types is defined in the
2930 // standard library.
2931 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
2932 return TypeInfoIsInStandardLibrary(PointerTy);
2933
2934 return false;
2935}
2936
2937/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
2938/// the given type exists somewhere else, and that we should not emit the type
2939/// information in this translation unit. Assumes that it is not a
2940/// standard-library type.
2941static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM,
2942 QualType Ty) {
2943 ASTContext &Context = CGM.getContext();
2944
2945 // If RTTI is disabled, assume it might be disabled in the
2946 // translation unit that defines any potential key function, too.
2947 if (!Context.getLangOpts().RTTI) return false;
2948
2949 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
2950 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
2951 if (!RD->hasDefinition())
2952 return false;
2953
2954 if (!RD->isDynamicClass())
2955 return false;
2956
2957 // FIXME: this may need to be reconsidered if the key function
2958 // changes.
David Majnemerbe9022c2015-08-06 20:56:55 +00002959 // N.B. We must always emit the RTTI data ourselves if there exists a key
2960 // function.
2961 bool IsDLLImport = RD->hasAttr<DLLImportAttr>();
Martin Storsjo3b528942018-02-02 06:22:35 +00002962
2963 // Don't import the RTTI but emit it locally.
2964 if (CGM.getTriple().isWindowsGNUEnvironment() && IsDLLImport)
2965 return false;
2966
David Majnemer1fb1a042014-11-07 07:26:38 +00002967 if (CGM.getVTables().isVTableExternal(RD))
Shoaib Meenai61118e72017-07-04 01:02:19 +00002968 return IsDLLImport && !CGM.getTriple().isWindowsItaniumEnvironment()
2969 ? false
2970 : true;
David Majnemer1fb1a042014-11-07 07:26:38 +00002971
David Majnemerbe9022c2015-08-06 20:56:55 +00002972 if (IsDLLImport)
David Majnemer1fb1a042014-11-07 07:26:38 +00002973 return true;
David Majnemere2cb8d12014-07-07 06:20:47 +00002974 }
2975
2976 return false;
2977}
2978
2979/// IsIncompleteClassType - Returns whether the given record type is incomplete.
2980static bool IsIncompleteClassType(const RecordType *RecordTy) {
2981 return !RecordTy->getDecl()->isCompleteDefinition();
2982}
2983
2984/// ContainsIncompleteClassType - Returns whether the given type contains an
2985/// incomplete class type. This is true if
2986///
2987/// * The given type is an incomplete class type.
2988/// * The given type is a pointer type whose pointee type contains an
2989/// incomplete class type.
2990/// * The given type is a member pointer type whose class is an incomplete
2991/// class type.
2992/// * The given type is a member pointer type whoise pointee type contains an
2993/// incomplete class type.
2994/// is an indirect or direct pointer to an incomplete class type.
2995static bool ContainsIncompleteClassType(QualType Ty) {
2996 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
2997 if (IsIncompleteClassType(RecordTy))
2998 return true;
2999 }
3000
3001 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
3002 return ContainsIncompleteClassType(PointerTy->getPointeeType());
3003
3004 if (const MemberPointerType *MemberPointerTy =
3005 dyn_cast<MemberPointerType>(Ty)) {
3006 // Check if the class type is incomplete.
3007 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
3008 if (IsIncompleteClassType(ClassType))
3009 return true;
3010
3011 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
3012 }
3013
3014 return false;
3015}
3016
3017// CanUseSingleInheritance - Return whether the given record decl has a "single,
3018// public, non-virtual base at offset zero (i.e. the derived class is dynamic
3019// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
3020static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
3021 // Check the number of bases.
3022 if (RD->getNumBases() != 1)
3023 return false;
3024
3025 // Get the base.
3026 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
3027
3028 // Check that the base is not virtual.
3029 if (Base->isVirtual())
3030 return false;
3031
3032 // Check that the base is public.
3033 if (Base->getAccessSpecifier() != AS_public)
3034 return false;
3035
3036 // Check that the class is dynamic iff the base is.
3037 const CXXRecordDecl *BaseDecl =
3038 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
3039 if (!BaseDecl->isEmpty() &&
3040 BaseDecl->isDynamicClass() != RD->isDynamicClass())
3041 return false;
3042
3043 return true;
3044}
3045
3046void ItaniumRTTIBuilder::BuildVTablePointer(const Type *Ty) {
3047 // abi::__class_type_info.
3048 static const char * const ClassTypeInfo =
3049 "_ZTVN10__cxxabiv117__class_type_infoE";
3050 // abi::__si_class_type_info.
3051 static const char * const SIClassTypeInfo =
3052 "_ZTVN10__cxxabiv120__si_class_type_infoE";
3053 // abi::__vmi_class_type_info.
3054 static const char * const VMIClassTypeInfo =
3055 "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
3056
3057 const char *VTableName = nullptr;
3058
3059 switch (Ty->getTypeClass()) {
3060#define TYPE(Class, Base)
3061#define ABSTRACT_TYPE(Class, Base)
3062#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
3063#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3064#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3065#include "clang/AST/TypeNodes.def"
3066 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
3067
3068 case Type::LValueReference:
3069 case Type::RValueReference:
3070 llvm_unreachable("References shouldn't get here");
3071
3072 case Type::Auto:
Richard Smith600b5262017-01-26 20:40:47 +00003073 case Type::DeducedTemplateSpecialization:
3074 llvm_unreachable("Undeduced type shouldn't get here");
David Majnemere2cb8d12014-07-07 06:20:47 +00003075
Xiuli Pan9c14e282016-01-09 12:53:17 +00003076 case Type::Pipe:
3077 llvm_unreachable("Pipe types shouldn't get here");
3078
David Majnemere2cb8d12014-07-07 06:20:47 +00003079 case Type::Builtin:
3080 // GCC treats vector and complex types as fundamental types.
3081 case Type::Vector:
3082 case Type::ExtVector:
3083 case Type::Complex:
3084 case Type::Atomic:
3085 // FIXME: GCC treats block pointers as fundamental types?!
3086 case Type::BlockPointer:
3087 // abi::__fundamental_type_info.
3088 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
3089 break;
3090
3091 case Type::ConstantArray:
3092 case Type::IncompleteArray:
3093 case Type::VariableArray:
3094 // abi::__array_type_info.
3095 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
3096 break;
3097
3098 case Type::FunctionNoProto:
3099 case Type::FunctionProto:
Richard Smithb17d6fa2016-12-01 03:04:07 +00003100 // abi::__function_type_info.
3101 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
David Majnemere2cb8d12014-07-07 06:20:47 +00003102 break;
3103
3104 case Type::Enum:
3105 // abi::__enum_type_info.
3106 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
3107 break;
3108
3109 case Type::Record: {
Rafael Espindolaf6688122018-03-22 21:14:16 +00003110 const CXXRecordDecl *RD =
3111 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
David Majnemere2cb8d12014-07-07 06:20:47 +00003112
3113 if (!RD->hasDefinition() || !RD->getNumBases()) {
3114 VTableName = ClassTypeInfo;
3115 } else if (CanUseSingleInheritance(RD)) {
3116 VTableName = SIClassTypeInfo;
3117 } else {
3118 VTableName = VMIClassTypeInfo;
3119 }
3120
3121 break;
3122 }
3123
3124 case Type::ObjCObject:
3125 // Ignore protocol qualifiers.
3126 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
3127
3128 // Handle id and Class.
3129 if (isa<BuiltinType>(Ty)) {
3130 VTableName = ClassTypeInfo;
3131 break;
3132 }
3133
3134 assert(isa<ObjCInterfaceType>(Ty));
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +00003135 LLVM_FALLTHROUGH;
David Majnemere2cb8d12014-07-07 06:20:47 +00003136
3137 case Type::ObjCInterface:
3138 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
3139 VTableName = SIClassTypeInfo;
3140 } else {
3141 VTableName = ClassTypeInfo;
3142 }
3143 break;
3144
3145 case Type::ObjCObjectPointer:
3146 case Type::Pointer:
3147 // abi::__pointer_type_info.
3148 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
3149 break;
3150
3151 case Type::MemberPointer:
3152 // abi::__pointer_to_member_type_info.
3153 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
3154 break;
3155 }
3156
3157 llvm::Constant *VTable =
3158 CGM.getModule().getOrInsertGlobal(VTableName, CGM.Int8PtrTy);
Rafael Espindolafe9a55a2018-03-23 01:36:23 +00003159 CGM.setDSOLocal(cast<llvm::GlobalValue>(VTable->stripPointerCasts()));
David Majnemere2cb8d12014-07-07 06:20:47 +00003160
3161 llvm::Type *PtrDiffTy =
3162 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
3163
3164 // The vtable address point is 2.
3165 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
David Blaikiee3b172a2015-04-02 18:55:21 +00003166 VTable =
3167 llvm::ConstantExpr::getInBoundsGetElementPtr(CGM.Int8PtrTy, VTable, Two);
David Majnemere2cb8d12014-07-07 06:20:47 +00003168 VTable = llvm::ConstantExpr::getBitCast(VTable, CGM.Int8PtrTy);
3169
3170 Fields.push_back(VTable);
3171}
3172
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003173/// Return the linkage that the type info and type info name constants
David Majnemere2cb8d12014-07-07 06:20:47 +00003174/// should have for the given type.
Richard Smithbbb26552018-05-21 20:10:54 +00003175static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(CodeGenModule &CGM,
3176 QualType Ty) {
David Majnemere2cb8d12014-07-07 06:20:47 +00003177 // Itanium C++ ABI 2.9.5p7:
3178 // In addition, it and all of the intermediate abi::__pointer_type_info
3179 // structs in the chain down to the abi::__class_type_info for the
3180 // incomplete class type must be prevented from resolving to the
3181 // corresponding type_info structs for the complete class type, possibly
3182 // by making them local static objects. Finally, a dummy class RTTI is
3183 // generated for the incomplete type that will not resolve to the final
3184 // complete class RTTI (because the latter need not exist), possibly by
3185 // making it a local static object.
3186 if (ContainsIncompleteClassType(Ty))
Richard Smithbbb26552018-05-21 20:10:54 +00003187 return llvm::GlobalValue::InternalLinkage;
3188
3189 switch (Ty->getLinkage()) {
3190 case NoLinkage:
3191 case InternalLinkage:
3192 case UniqueExternalLinkage:
3193 return llvm::GlobalValue::InternalLinkage;
3194
3195 case VisibleNoLinkage:
3196 case ModuleInternalLinkage:
3197 case ModuleLinkage:
3198 case ExternalLinkage:
3199 // RTTI is not enabled, which means that this type info struct is going
3200 // to be used for exception handling. Give it linkonce_odr linkage.
3201 if (!CGM.getLangOpts().RTTI)
3202 return llvm::GlobalValue::LinkOnceODRLinkage;
3203
3204 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
3205 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
3206 if (RD->hasAttr<WeakAttr>())
3207 return llvm::GlobalValue::WeakODRLinkage;
3208 if (CGM.getTriple().isWindowsItaniumEnvironment())
3209 if (RD->hasAttr<DLLImportAttr>() &&
3210 ShouldUseExternalRTTIDescriptor(CGM, Ty))
3211 return llvm::GlobalValue::ExternalLinkage;
3212 // MinGW always uses LinkOnceODRLinkage for type info.
3213 if (RD->isDynamicClass() &&
3214 !CGM.getContext()
3215 .getTargetInfo()
3216 .getTriple()
3217 .isWindowsGNUEnvironment())
3218 return CGM.getVTableLinkage(RD);
3219 }
3220
3221 return llvm::GlobalValue::LinkOnceODRLinkage;
3222 }
3223
3224 llvm_unreachable("Invalid linkage!");
David Majnemere2cb8d12014-07-07 06:20:47 +00003225}
3226
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003227llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty) {
David Majnemere2cb8d12014-07-07 06:20:47 +00003228 // We want to operate on the canonical type.
Yaron Kerenebd14262016-03-16 12:14:43 +00003229 Ty = Ty.getCanonicalType();
David Majnemere2cb8d12014-07-07 06:20:47 +00003230
3231 // Check if we've already emitted an RTTI descriptor for this type.
Yaron Kerene46f7ed2015-07-29 14:21:47 +00003232 SmallString<256> Name;
3233 llvm::raw_svector_ostream Out(Name);
David Majnemere2cb8d12014-07-07 06:20:47 +00003234 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
David Majnemere2cb8d12014-07-07 06:20:47 +00003235
3236 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
3237 if (OldGV && !OldGV->isDeclaration()) {
3238 assert(!OldGV->hasAvailableExternallyLinkage() &&
3239 "available_externally typeinfos not yet implemented");
3240
3241 return llvm::ConstantExpr::getBitCast(OldGV, CGM.Int8PtrTy);
3242 }
3243
3244 // Check if there is already an external RTTI descriptor for this type.
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003245 if (IsStandardLibraryRTTIDescriptor(Ty) ||
3246 ShouldUseExternalRTTIDescriptor(CGM, Ty))
David Majnemere2cb8d12014-07-07 06:20:47 +00003247 return GetAddrOfExternalRTTIDescriptor(Ty);
3248
3249 // Emit the standard library with external linkage.
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003250 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(CGM, Ty);
Richard Smithbbb26552018-05-21 20:10:54 +00003251
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003252 // Give the type_info object and name the formal visibility of the
3253 // type itself.
3254 llvm::GlobalValue::VisibilityTypes llvmVisibility;
3255 if (llvm::GlobalValue::isLocalLinkage(Linkage))
3256 // If the linkage is local, only default visibility makes sense.
3257 llvmVisibility = llvm::GlobalValue::DefaultVisibility;
3258 else if (CXXABI.classifyRTTIUniqueness(Ty, Linkage) ==
3259 ItaniumCXXABI::RUK_NonUniqueHidden)
3260 llvmVisibility = llvm::GlobalValue::HiddenVisibility;
3261 else
3262 llvmVisibility = CodeGenModule::GetLLVMVisibility(Ty->getVisibility());
3263
3264 llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass =
3265 llvm::GlobalValue::DefaultStorageClass;
3266 if (CGM.getTriple().isWindowsItaniumEnvironment()) {
3267 auto RD = Ty->getAsCXXRecordDecl();
3268 if (RD && RD->hasAttr<DLLExportAttr>())
3269 DLLStorageClass = llvm::GlobalValue::DLLExportStorageClass;
3270 }
3271
3272 return BuildTypeInfo(Ty, Linkage, llvmVisibility, DLLStorageClass);
3273}
3274
3275llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(
3276 QualType Ty,
3277 llvm::GlobalVariable::LinkageTypes Linkage,
3278 llvm::GlobalValue::VisibilityTypes Visibility,
3279 llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass) {
David Majnemere2cb8d12014-07-07 06:20:47 +00003280 // Add the vtable pointer.
3281 BuildVTablePointer(cast<Type>(Ty));
3282
3283 // And the name.
Richard Smithbbb26552018-05-21 20:10:54 +00003284 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
David Majnemere2cb8d12014-07-07 06:20:47 +00003285 llvm::Constant *TypeNameField;
3286
3287 // If we're supposed to demote the visibility, be sure to set a flag
3288 // to use a string comparison for type_info comparisons.
3289 ItaniumCXXABI::RTTIUniquenessKind RTTIUniqueness =
Richard Smithbbb26552018-05-21 20:10:54 +00003290 CXXABI.classifyRTTIUniqueness(Ty, Linkage);
David Majnemere2cb8d12014-07-07 06:20:47 +00003291 if (RTTIUniqueness != ItaniumCXXABI::RUK_Unique) {
3292 // The flag is the sign bit, which on ARM64 is defined to be clear
3293 // for global pointers. This is very ARM64-specific.
3294 TypeNameField = llvm::ConstantExpr::getPtrToInt(TypeName, CGM.Int64Ty);
3295 llvm::Constant *flag =
3296 llvm::ConstantInt::get(CGM.Int64Ty, ((uint64_t)1) << 63);
3297 TypeNameField = llvm::ConstantExpr::getAdd(TypeNameField, flag);
3298 TypeNameField =
3299 llvm::ConstantExpr::getIntToPtr(TypeNameField, CGM.Int8PtrTy);
3300 } else {
3301 TypeNameField = llvm::ConstantExpr::getBitCast(TypeName, CGM.Int8PtrTy);
3302 }
3303 Fields.push_back(TypeNameField);
3304
3305 switch (Ty->getTypeClass()) {
3306#define TYPE(Class, Base)
3307#define ABSTRACT_TYPE(Class, Base)
3308#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
3309#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3310#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3311#include "clang/AST/TypeNodes.def"
3312 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
3313
3314 // GCC treats vector types as fundamental types.
3315 case Type::Builtin:
3316 case Type::Vector:
3317 case Type::ExtVector:
3318 case Type::Complex:
3319 case Type::BlockPointer:
3320 // Itanium C++ ABI 2.9.5p4:
3321 // abi::__fundamental_type_info adds no data members to std::type_info.
3322 break;
3323
3324 case Type::LValueReference:
3325 case Type::RValueReference:
3326 llvm_unreachable("References shouldn't get here");
3327
3328 case Type::Auto:
Richard Smith600b5262017-01-26 20:40:47 +00003329 case Type::DeducedTemplateSpecialization:
3330 llvm_unreachable("Undeduced type shouldn't get here");
David Majnemere2cb8d12014-07-07 06:20:47 +00003331
Xiuli Pan9c14e282016-01-09 12:53:17 +00003332 case Type::Pipe:
3333 llvm_unreachable("Pipe type shouldn't get here");
3334
David Majnemere2cb8d12014-07-07 06:20:47 +00003335 case Type::ConstantArray:
3336 case Type::IncompleteArray:
3337 case Type::VariableArray:
3338 // Itanium C++ ABI 2.9.5p5:
3339 // abi::__array_type_info adds no data members to std::type_info.
3340 break;
3341
3342 case Type::FunctionNoProto:
Richard Smithb17d6fa2016-12-01 03:04:07 +00003343 case Type::FunctionProto:
David Majnemere2cb8d12014-07-07 06:20:47 +00003344 // Itanium C++ ABI 2.9.5p5:
3345 // abi::__function_type_info adds no data members to std::type_info.
3346 break;
3347
3348 case Type::Enum:
3349 // Itanium C++ ABI 2.9.5p5:
3350 // abi::__enum_type_info adds no data members to std::type_info.
3351 break;
3352
3353 case Type::Record: {
3354 const CXXRecordDecl *RD =
3355 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
3356 if (!RD->hasDefinition() || !RD->getNumBases()) {
3357 // We don't need to emit any fields.
3358 break;
3359 }
3360
3361 if (CanUseSingleInheritance(RD))
3362 BuildSIClassTypeInfo(RD);
3363 else
3364 BuildVMIClassTypeInfo(RD);
3365
3366 break;
3367 }
3368
3369 case Type::ObjCObject:
3370 case Type::ObjCInterface:
3371 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
3372 break;
3373
3374 case Type::ObjCObjectPointer:
3375 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
3376 break;
3377
3378 case Type::Pointer:
3379 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
3380 break;
3381
3382 case Type::MemberPointer:
3383 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
3384 break;
3385
3386 case Type::Atomic:
3387 // No fields, at least for the moment.
3388 break;
3389 }
3390
3391 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields);
3392
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003393 SmallString<256> Name;
3394 llvm::raw_svector_ostream Out(Name);
3395 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
Rafael Espindolacb92c192015-01-15 23:18:01 +00003396 llvm::Module &M = CGM.getModule();
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003397 llvm::GlobalVariable *OldGV = M.getNamedGlobal(Name);
David Majnemere2cb8d12014-07-07 06:20:47 +00003398 llvm::GlobalVariable *GV =
Rafael Espindolacb92c192015-01-15 23:18:01 +00003399 new llvm::GlobalVariable(M, Init->getType(),
Richard Smithbbb26552018-05-21 20:10:54 +00003400 /*Constant=*/true, Linkage, Init, Name);
Rafael Espindolacb92c192015-01-15 23:18:01 +00003401
David Majnemere2cb8d12014-07-07 06:20:47 +00003402 // If there's already an old global variable, replace it with the new one.
3403 if (OldGV) {
3404 GV->takeName(OldGV);
3405 llvm::Constant *NewPtr =
3406 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
3407 OldGV->replaceAllUsesWith(NewPtr);
3408 OldGV->eraseFromParent();
3409 }
3410
Yaron Keren04da2382015-07-29 15:42:28 +00003411 if (CGM.supportsCOMDAT() && GV->isWeakForLinker())
3412 GV->setComdat(M.getOrInsertComdat(GV->getName()));
3413
David Greenbe0c5b62018-09-12 14:09:06 +00003414 CharUnits Align =
3415 CGM.getContext().toCharUnitsFromBits(CGM.getTarget().getPointerAlign(0));
3416 GV->setAlignment(Align.getQuantity());
3417
David Majnemere2cb8d12014-07-07 06:20:47 +00003418 // The Itanium ABI specifies that type_info objects must be globally
3419 // unique, with one exception: if the type is an incomplete class
3420 // type or a (possibly indirect) pointer to one. That exception
3421 // affects the general case of comparing type_info objects produced
3422 // by the typeid operator, which is why the comparison operators on
3423 // std::type_info generally use the type_info name pointers instead
3424 // of the object addresses. However, the language's built-in uses
3425 // of RTTI generally require class types to be complete, even when
3426 // manipulating pointers to those class types. This allows the
3427 // implementation of dynamic_cast to rely on address equality tests,
3428 // which is much faster.
3429
3430 // All of this is to say that it's important that both the type_info
3431 // object and the type_info name be uniqued when weakly emitted.
3432
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003433 TypeName->setVisibility(Visibility);
Rafael Espindola3dd49812018-02-23 00:22:15 +00003434 CGM.setDSOLocal(TypeName);
Rafael Espindola699f5d62018-02-07 22:15:33 +00003435
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003436 GV->setVisibility(Visibility);
Rafael Espindola3dd49812018-02-23 00:22:15 +00003437 CGM.setDSOLocal(GV);
Saleem Abdulrasool18820022016-12-02 22:46:18 +00003438
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003439 TypeName->setDLLStorageClass(DLLStorageClass);
3440 GV->setDLLStorageClass(DLLStorageClass);
David Majnemere2cb8d12014-07-07 06:20:47 +00003441
3442 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
3443}
3444
David Majnemere2cb8d12014-07-07 06:20:47 +00003445/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
3446/// for the given Objective-C object type.
3447void ItaniumRTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
3448 // Drop qualifiers.
3449 const Type *T = OT->getBaseType().getTypePtr();
3450 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
3451
3452 // The builtin types are abi::__class_type_infos and don't require
3453 // extra fields.
3454 if (isa<BuiltinType>(T)) return;
3455
3456 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
3457 ObjCInterfaceDecl *Super = Class->getSuperClass();
3458
3459 // Root classes are also __class_type_info.
3460 if (!Super) return;
3461
3462 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
3463
3464 // Everything else is single inheritance.
3465 llvm::Constant *BaseTypeInfo =
3466 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(SuperTy);
3467 Fields.push_back(BaseTypeInfo);
3468}
3469
3470/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
3471/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
3472void ItaniumRTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
3473 // Itanium C++ ABI 2.9.5p6b:
3474 // It adds to abi::__class_type_info a single member pointing to the
3475 // type_info structure for the base type,
3476 llvm::Constant *BaseTypeInfo =
3477 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(RD->bases_begin()->getType());
3478 Fields.push_back(BaseTypeInfo);
3479}
3480
3481namespace {
3482 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
3483 /// a class hierarchy.
3484 struct SeenBases {
3485 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
3486 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
3487 };
3488}
3489
3490/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
3491/// abi::__vmi_class_type_info.
3492///
3493static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
3494 SeenBases &Bases) {
3495
3496 unsigned Flags = 0;
3497
3498 const CXXRecordDecl *BaseDecl =
3499 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
3500
3501 if (Base->isVirtual()) {
3502 // Mark the virtual base as seen.
David Blaikie82e95a32014-11-19 07:49:47 +00003503 if (!Bases.VirtualBases.insert(BaseDecl).second) {
David Majnemere2cb8d12014-07-07 06:20:47 +00003504 // If this virtual base has been seen before, then the class is diamond
3505 // shaped.
3506 Flags |= ItaniumRTTIBuilder::VMI_DiamondShaped;
3507 } else {
3508 if (Bases.NonVirtualBases.count(BaseDecl))
3509 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
3510 }
3511 } else {
3512 // Mark the non-virtual base as seen.
David Blaikie82e95a32014-11-19 07:49:47 +00003513 if (!Bases.NonVirtualBases.insert(BaseDecl).second) {
David Majnemere2cb8d12014-07-07 06:20:47 +00003514 // If this non-virtual base has been seen before, then the class has non-
3515 // diamond shaped repeated inheritance.
3516 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
3517 } else {
3518 if (Bases.VirtualBases.count(BaseDecl))
3519 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
3520 }
3521 }
3522
3523 // Walk all bases.
3524 for (const auto &I : BaseDecl->bases())
3525 Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases);
3526
3527 return Flags;
3528}
3529
3530static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
3531 unsigned Flags = 0;
3532 SeenBases Bases;
3533
3534 // Walk all bases.
3535 for (const auto &I : RD->bases())
3536 Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases);
3537
3538 return Flags;
3539}
3540
3541/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
3542/// classes with bases that do not satisfy the abi::__si_class_type_info
3543/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
3544void ItaniumRTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
3545 llvm::Type *UnsignedIntLTy =
3546 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
3547
3548 // Itanium C++ ABI 2.9.5p6c:
3549 // __flags is a word with flags describing details about the class
3550 // structure, which may be referenced by using the __flags_masks
3551 // enumeration. These flags refer to both direct and indirect bases.
3552 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
3553 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3554
3555 // Itanium C++ ABI 2.9.5p6c:
3556 // __base_count is a word with the number of direct proper base class
3557 // descriptions that follow.
3558 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
3559
3560 if (!RD->getNumBases())
3561 return;
3562
David Majnemere2cb8d12014-07-07 06:20:47 +00003563 // Now add the base class descriptions.
3564
3565 // Itanium C++ ABI 2.9.5p6c:
3566 // __base_info[] is an array of base class descriptions -- one for every
3567 // direct proper base. Each description is of the type:
3568 //
3569 // struct abi::__base_class_type_info {
3570 // public:
3571 // const __class_type_info *__base_type;
3572 // long __offset_flags;
3573 //
3574 // enum __offset_flags_masks {
3575 // __virtual_mask = 0x1,
3576 // __public_mask = 0x2,
3577 // __offset_shift = 8
3578 // };
3579 // };
Reid Klecknerd8b04662016-08-25 22:16:30 +00003580
3581 // If we're in mingw and 'long' isn't wide enough for a pointer, use 'long
3582 // long' instead of 'long' for __offset_flags. libstdc++abi uses long long on
3583 // LLP64 platforms.
3584 // FIXME: Consider updating libc++abi to match, and extend this logic to all
3585 // LLP64 platforms.
3586 QualType OffsetFlagsTy = CGM.getContext().LongTy;
3587 const TargetInfo &TI = CGM.getContext().getTargetInfo();
3588 if (TI.getTriple().isOSCygMing() && TI.getPointerWidth(0) > TI.getLongWidth())
3589 OffsetFlagsTy = CGM.getContext().LongLongTy;
3590 llvm::Type *OffsetFlagsLTy =
3591 CGM.getTypes().ConvertType(OffsetFlagsTy);
3592
David Majnemere2cb8d12014-07-07 06:20:47 +00003593 for (const auto &Base : RD->bases()) {
3594 // The __base_type member points to the RTTI for the base type.
3595 Fields.push_back(ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(Base.getType()));
3596
3597 const CXXRecordDecl *BaseDecl =
3598 cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
3599
3600 int64_t OffsetFlags = 0;
3601
3602 // All but the lower 8 bits of __offset_flags are a signed offset.
3603 // For a non-virtual base, this is the offset in the object of the base
3604 // subobject. For a virtual base, this is the offset in the virtual table of
3605 // the virtual base offset for the virtual base referenced (negative).
3606 CharUnits Offset;
3607 if (Base.isVirtual())
3608 Offset =
3609 CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(RD, BaseDecl);
3610 else {
3611 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
3612 Offset = Layout.getBaseClassOffset(BaseDecl);
3613 };
3614
3615 OffsetFlags = uint64_t(Offset.getQuantity()) << 8;
3616
3617 // The low-order byte of __offset_flags contains flags, as given by the
3618 // masks from the enumeration __offset_flags_masks.
3619 if (Base.isVirtual())
3620 OffsetFlags |= BCTI_Virtual;
3621 if (Base.getAccessSpecifier() == AS_public)
3622 OffsetFlags |= BCTI_Public;
3623
Reid Klecknerd8b04662016-08-25 22:16:30 +00003624 Fields.push_back(llvm::ConstantInt::get(OffsetFlagsLTy, OffsetFlags));
David Majnemere2cb8d12014-07-07 06:20:47 +00003625 }
3626}
3627
Richard Smitha7d93782016-12-01 03:32:42 +00003628/// Compute the flags for a __pbase_type_info, and remove the corresponding
3629/// pieces from \p Type.
3630static unsigned extractPBaseFlags(ASTContext &Ctx, QualType &Type) {
3631 unsigned Flags = 0;
David Majnemere2cb8d12014-07-07 06:20:47 +00003632
Richard Smitha7d93782016-12-01 03:32:42 +00003633 if (Type.isConstQualified())
3634 Flags |= ItaniumRTTIBuilder::PTI_Const;
3635 if (Type.isVolatileQualified())
3636 Flags |= ItaniumRTTIBuilder::PTI_Volatile;
3637 if (Type.isRestrictQualified())
3638 Flags |= ItaniumRTTIBuilder::PTI_Restrict;
3639 Type = Type.getUnqualifiedType();
David Majnemere2cb8d12014-07-07 06:20:47 +00003640
3641 // Itanium C++ ABI 2.9.5p7:
3642 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
3643 // incomplete class type, the incomplete target type flag is set.
Richard Smitha7d93782016-12-01 03:32:42 +00003644 if (ContainsIncompleteClassType(Type))
3645 Flags |= ItaniumRTTIBuilder::PTI_Incomplete;
3646
3647 if (auto *Proto = Type->getAs<FunctionProtoType>()) {
Richard Smitheaf11ad2018-05-03 03:58:32 +00003648 if (Proto->isNothrow()) {
Richard Smitha7d93782016-12-01 03:32:42 +00003649 Flags |= ItaniumRTTIBuilder::PTI_Noexcept;
Stephan Bergmann8c85bca2018-01-05 07:57:12 +00003650 Type = Ctx.getFunctionTypeWithExceptionSpec(Type, EST_None);
Richard Smitha7d93782016-12-01 03:32:42 +00003651 }
3652 }
3653
3654 return Flags;
3655}
3656
3657/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
3658/// used for pointer types.
3659void ItaniumRTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
3660 // Itanium C++ ABI 2.9.5p7:
3661 // __flags is a flag word describing the cv-qualification and other
3662 // attributes of the type pointed to
3663 unsigned Flags = extractPBaseFlags(CGM.getContext(), PointeeTy);
David Majnemere2cb8d12014-07-07 06:20:47 +00003664
3665 llvm::Type *UnsignedIntLTy =
3666 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
3667 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3668
3669 // Itanium C++ ABI 2.9.5p7:
3670 // __pointee is a pointer to the std::type_info derivation for the
3671 // unqualified type being pointed to.
3672 llvm::Constant *PointeeTypeInfo =
Richard Smitha7d93782016-12-01 03:32:42 +00003673 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(PointeeTy);
David Majnemere2cb8d12014-07-07 06:20:47 +00003674 Fields.push_back(PointeeTypeInfo);
3675}
3676
3677/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
3678/// struct, used for member pointer types.
3679void
3680ItaniumRTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
3681 QualType PointeeTy = Ty->getPointeeType();
3682
David Majnemere2cb8d12014-07-07 06:20:47 +00003683 // Itanium C++ ABI 2.9.5p7:
3684 // __flags is a flag word describing the cv-qualification and other
3685 // attributes of the type pointed to.
Richard Smitha7d93782016-12-01 03:32:42 +00003686 unsigned Flags = extractPBaseFlags(CGM.getContext(), PointeeTy);
David Majnemere2cb8d12014-07-07 06:20:47 +00003687
3688 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
David Majnemere2cb8d12014-07-07 06:20:47 +00003689 if (IsIncompleteClassType(ClassType))
3690 Flags |= PTI_ContainingClassIncomplete;
3691
3692 llvm::Type *UnsignedIntLTy =
3693 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
3694 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3695
3696 // Itanium C++ ABI 2.9.5p7:
3697 // __pointee is a pointer to the std::type_info derivation for the
3698 // unqualified type being pointed to.
3699 llvm::Constant *PointeeTypeInfo =
Richard Smitha7d93782016-12-01 03:32:42 +00003700 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(PointeeTy);
David Majnemere2cb8d12014-07-07 06:20:47 +00003701 Fields.push_back(PointeeTypeInfo);
3702
3703 // Itanium C++ ABI 2.9.5p9:
3704 // __context is a pointer to an abi::__class_type_info corresponding to the
3705 // class type containing the member pointed to
3706 // (e.g., the "A" in "int A::*").
3707 Fields.push_back(
3708 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(QualType(ClassType, 0)));
3709}
3710
David Majnemer443250f2015-03-17 20:35:00 +00003711llvm::Constant *ItaniumCXXABI::getAddrOfRTTIDescriptor(QualType Ty) {
David Majnemere2cb8d12014-07-07 06:20:47 +00003712 return ItaniumRTTIBuilder(*this).BuildTypeInfo(Ty);
3713}
3714
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003715void ItaniumCXXABI::EmitFundamentalRTTIDescriptors(const CXXRecordDecl *RD) {
Richard Smith4a382012016-02-03 01:32:42 +00003716 // Types added here must also be added to TypeInfoIsInStandardLibrary.
David Majnemere2cb8d12014-07-07 06:20:47 +00003717 QualType FundamentalTypes[] = {
3718 getContext().VoidTy, getContext().NullPtrTy,
3719 getContext().BoolTy, getContext().WCharTy,
3720 getContext().CharTy, getContext().UnsignedCharTy,
3721 getContext().SignedCharTy, getContext().ShortTy,
3722 getContext().UnsignedShortTy, getContext().IntTy,
3723 getContext().UnsignedIntTy, getContext().LongTy,
3724 getContext().UnsignedLongTy, getContext().LongLongTy,
Richard Smith4a382012016-02-03 01:32:42 +00003725 getContext().UnsignedLongLongTy, getContext().Int128Ty,
3726 getContext().UnsignedInt128Ty, getContext().HalfTy,
David Majnemere2cb8d12014-07-07 06:20:47 +00003727 getContext().FloatTy, getContext().DoubleTy,
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00003728 getContext().LongDoubleTy, getContext().Float128Ty,
Richard Smith3a8244d2018-05-01 05:02:45 +00003729 getContext().Char8Ty, getContext().Char16Ty,
3730 getContext().Char32Ty
David Majnemere2cb8d12014-07-07 06:20:47 +00003731 };
Thomas Andersonb6d87cf2018-07-24 00:43:47 +00003732 llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass =
3733 RD->hasAttr<DLLExportAttr>()
3734 ? llvm::GlobalValue::DLLExportStorageClass
3735 : llvm::GlobalValue::DefaultStorageClass;
3736 llvm::GlobalValue::VisibilityTypes Visibility =
3737 CodeGenModule::GetLLVMVisibility(RD->getVisibility());
3738 for (const QualType &FundamentalType : FundamentalTypes) {
3739 QualType PointerType = getContext().getPointerType(FundamentalType);
3740 QualType PointerTypeConst = getContext().getPointerType(
3741 FundamentalType.withConst());
3742 for (QualType Type : {FundamentalType, PointerType, PointerTypeConst})
3743 ItaniumRTTIBuilder(*this).BuildTypeInfo(
3744 Type, llvm::GlobalValue::ExternalLinkage,
3745 Visibility, DLLStorageClass);
3746 }
David Majnemere2cb8d12014-07-07 06:20:47 +00003747}
3748
3749/// What sort of uniqueness rules should we use for the RTTI for the
3750/// given type?
3751ItaniumCXXABI::RTTIUniquenessKind ItaniumCXXABI::classifyRTTIUniqueness(
3752 QualType CanTy, llvm::GlobalValue::LinkageTypes Linkage) const {
3753 if (shouldRTTIBeUnique())
3754 return RUK_Unique;
3755
3756 // It's only necessary for linkonce_odr or weak_odr linkage.
3757 if (Linkage != llvm::GlobalValue::LinkOnceODRLinkage &&
3758 Linkage != llvm::GlobalValue::WeakODRLinkage)
3759 return RUK_Unique;
3760
3761 // It's only necessary with default visibility.
3762 if (CanTy->getVisibility() != DefaultVisibility)
3763 return RUK_Unique;
3764
3765 // If we're not required to publish this symbol, hide it.
3766 if (Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
3767 return RUK_NonUniqueHidden;
3768
3769 // If we're required to publish this symbol, as we might be under an
3770 // explicit instantiation, leave it with default visibility but
3771 // enable string-comparisons.
3772 assert(Linkage == llvm::GlobalValue::WeakODRLinkage);
3773 return RUK_NonUniqueVisible;
3774}
Rafael Espindola91f68b42014-09-15 19:20:10 +00003775
Rafael Espindola1e4df922014-09-16 15:18:21 +00003776// Find out how to codegen the complete destructor and constructor
3777namespace {
3778enum class StructorCodegen { Emit, RAUW, Alias, COMDAT };
3779}
3780static StructorCodegen getCodegenToUse(CodeGenModule &CGM,
3781 const CXXMethodDecl *MD) {
3782 if (!CGM.getCodeGenOpts().CXXCtorDtorAliases)
3783 return StructorCodegen::Emit;
Rafael Espindola91f68b42014-09-15 19:20:10 +00003784
Rafael Espindola1e4df922014-09-16 15:18:21 +00003785 // The complete and base structors are not equivalent if there are any virtual
3786 // bases, so emit separate functions.
3787 if (MD->getParent()->getNumVBases())
3788 return StructorCodegen::Emit;
3789
3790 GlobalDecl AliasDecl;
3791 if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) {
3792 AliasDecl = GlobalDecl(DD, Dtor_Complete);
3793 } else {
3794 const auto *CD = cast<CXXConstructorDecl>(MD);
3795 AliasDecl = GlobalDecl(CD, Ctor_Complete);
3796 }
3797 llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl);
3798
Chandler Carruth1f82d9b2018-07-29 03:05:07 +00003799 if (llvm::GlobalValue::isDiscardableIfUnused(Linkage))
3800 return StructorCodegen::RAUW;
Rafael Espindola1e4df922014-09-16 15:18:21 +00003801
Pavel Labathc370f262018-05-14 11:35:44 +00003802 // FIXME: Should we allow available_externally aliases?
Chandler Carruth1f82d9b2018-07-29 03:05:07 +00003803 if (!llvm::GlobalAlias::isValidLinkage(Linkage))
3804 return StructorCodegen::RAUW;
Rafael Espindola1e4df922014-09-16 15:18:21 +00003805
Rafael Espindola0806f982014-09-16 20:19:43 +00003806 if (llvm::GlobalValue::isWeakForLinker(Linkage)) {
Dan Gohman839f2152017-01-17 21:46:38 +00003807 // Only ELF and wasm support COMDATs with arbitrary names (C5/D5).
3808 if (CGM.getTarget().getTriple().isOSBinFormatELF() ||
3809 CGM.getTarget().getTriple().isOSBinFormatWasm())
Rafael Espindola0806f982014-09-16 20:19:43 +00003810 return StructorCodegen::COMDAT;
3811 return StructorCodegen::Emit;
3812 }
Rafael Espindola1e4df922014-09-16 15:18:21 +00003813
3814 return StructorCodegen::Alias;
Rafael Espindola91f68b42014-09-15 19:20:10 +00003815}
3816
Rafael Espindola1e4df922014-09-16 15:18:21 +00003817static void emitConstructorDestructorAlias(CodeGenModule &CGM,
3818 GlobalDecl AliasDecl,
3819 GlobalDecl TargetDecl) {
3820 llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl);
3821
3822 StringRef MangledName = CGM.getMangledName(AliasDecl);
3823 llvm::GlobalValue *Entry = CGM.GetGlobalValue(MangledName);
3824 if (Entry && !Entry->isDeclaration())
3825 return;
3826
3827 auto *Aliasee = cast<llvm::GlobalValue>(CGM.GetAddrOfGlobal(TargetDecl));
Rafael Espindola1e4df922014-09-16 15:18:21 +00003828
3829 // Create the alias with no name.
David Blaikie2a791d72015-09-14 18:38:22 +00003830 auto *Alias = llvm::GlobalAlias::create(Linkage, "", Aliasee);
Rafael Espindola1e4df922014-09-16 15:18:21 +00003831
Peter Collingbourned914fd22018-06-18 20:58:54 +00003832 // Constructors and destructors are always unnamed_addr.
3833 Alias->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3834
Rafael Espindola1e4df922014-09-16 15:18:21 +00003835 // Switch any previous uses to the alias.
3836 if (Entry) {
NAKAMURA Takumie9621042015-09-15 01:39:27 +00003837 assert(Entry->getType() == Aliasee->getType() &&
Rafael Espindola1e4df922014-09-16 15:18:21 +00003838 "declaration exists with different type");
3839 Alias->takeName(Entry);
3840 Entry->replaceAllUsesWith(Alias);
3841 Entry->eraseFromParent();
3842 } else {
3843 Alias->setName(MangledName);
3844 }
3845
3846 // Finally, set up the alias with its proper name and attributes.
Rafael Espindolab7350042018-03-01 00:35:47 +00003847 CGM.SetCommonAttributes(AliasDecl, Alias);
Rafael Espindola1e4df922014-09-16 15:18:21 +00003848}
3849
3850void ItaniumCXXABI::emitCXXStructor(const CXXMethodDecl *MD,
3851 StructorType Type) {
3852 auto *CD = dyn_cast<CXXConstructorDecl>(MD);
3853 const CXXDestructorDecl *DD = CD ? nullptr : cast<CXXDestructorDecl>(MD);
3854
3855 StructorCodegen CGType = getCodegenToUse(CGM, MD);
3856
3857 if (Type == StructorType::Complete) {
3858 GlobalDecl CompleteDecl;
3859 GlobalDecl BaseDecl;
3860 if (CD) {
3861 CompleteDecl = GlobalDecl(CD, Ctor_Complete);
3862 BaseDecl = GlobalDecl(CD, Ctor_Base);
3863 } else {
3864 CompleteDecl = GlobalDecl(DD, Dtor_Complete);
3865 BaseDecl = GlobalDecl(DD, Dtor_Base);
3866 }
3867
3868 if (CGType == StructorCodegen::Alias || CGType == StructorCodegen::COMDAT) {
3869 emitConstructorDestructorAlias(CGM, CompleteDecl, BaseDecl);
3870 return;
3871 }
3872
3873 if (CGType == StructorCodegen::RAUW) {
3874 StringRef MangledName = CGM.getMangledName(CompleteDecl);
Andrey Bokhankocab58582015-08-31 13:20:44 +00003875 auto *Aliasee = CGM.GetAddrOfGlobal(BaseDecl);
Rafael Espindola1e4df922014-09-16 15:18:21 +00003876 CGM.addReplacement(MangledName, Aliasee);
3877 return;
Rafael Espindola91f68b42014-09-15 19:20:10 +00003878 }
3879 }
3880
3881 // The base destructor is equivalent to the base destructor of its
3882 // base class if there is exactly one non-virtual base class with a
3883 // non-trivial destructor, there are no fields with a non-trivial
3884 // destructor, and the body of the destructor is trivial.
Rafael Espindola1e4df922014-09-16 15:18:21 +00003885 if (DD && Type == StructorType::Base && CGType != StructorCodegen::COMDAT &&
3886 !CGM.TryEmitBaseDestructorAsAlias(DD))
Rafael Espindola91f68b42014-09-15 19:20:10 +00003887 return;
3888
Richard Smith5b349582017-10-13 01:55:36 +00003889 // FIXME: The deleting destructor is equivalent to the selected operator
3890 // delete if:
3891 // * either the delete is a destroying operator delete or the destructor
3892 // would be trivial if it weren't virtual,
3893 // * the conversion from the 'this' parameter to the first parameter of the
3894 // destructor is equivalent to a bitcast,
3895 // * the destructor does not have an implicit "this" return, and
3896 // * the operator delete has the same calling convention and IR function type
3897 // as the destructor.
3898 // In such cases we should try to emit the deleting dtor as an alias to the
3899 // selected 'operator delete'.
3900
Rafael Espindola1e4df922014-09-16 15:18:21 +00003901 llvm::Function *Fn = CGM.codegenCXXStructor(MD, Type);
Rafael Espindola91f68b42014-09-15 19:20:10 +00003902
Rafael Espindola1e4df922014-09-16 15:18:21 +00003903 if (CGType == StructorCodegen::COMDAT) {
3904 SmallString<256> Buffer;
3905 llvm::raw_svector_ostream Out(Buffer);
3906 if (DD)
3907 getMangleContext().mangleCXXDtorComdat(DD, Out);
3908 else
3909 getMangleContext().mangleCXXCtorComdat(CD, Out);
3910 llvm::Comdat *C = CGM.getModule().getOrInsertComdat(Out.str());
3911 Fn->setComdat(C);
Rafael Espindoladbee8a72015-01-15 21:36:08 +00003912 } else {
3913 CGM.maybeSetTrivialComdat(*MD, *Fn);
Rafael Espindola91f68b42014-09-15 19:20:10 +00003914 }
Rafael Espindola91f68b42014-09-15 19:20:10 +00003915}
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00003916
3917static llvm::Constant *getBeginCatchFn(CodeGenModule &CGM) {
3918 // void *__cxa_begin_catch(void*);
3919 llvm::FunctionType *FTy = llvm::FunctionType::get(
3920 CGM.Int8PtrTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
3921
3922 return CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
3923}
3924
3925static llvm::Constant *getEndCatchFn(CodeGenModule &CGM) {
3926 // void __cxa_end_catch();
3927 llvm::FunctionType *FTy =
3928 llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false);
3929
3930 return CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch");
3931}
3932
3933static llvm::Constant *getGetExceptionPtrFn(CodeGenModule &CGM) {
3934 // void *__cxa_get_exception_ptr(void*);
3935 llvm::FunctionType *FTy = llvm::FunctionType::get(
3936 CGM.Int8PtrTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
3937
3938 return CGM.CreateRuntimeFunction(FTy, "__cxa_get_exception_ptr");
3939}
3940
3941namespace {
3942 /// A cleanup to call __cxa_end_catch. In many cases, the caught
3943 /// exception type lets us state definitively that the thrown exception
3944 /// type does not have a destructor. In particular:
3945 /// - Catch-alls tell us nothing, so we have to conservatively
3946 /// assume that the thrown exception might have a destructor.
3947 /// - Catches by reference behave according to their base types.
3948 /// - Catches of non-record types will only trigger for exceptions
3949 /// of non-record types, which never have destructors.
3950 /// - Catches of record types can trigger for arbitrary subclasses
3951 /// of the caught type, so we have to assume the actual thrown
3952 /// exception type might have a throwing destructor, even if the
3953 /// caught type's destructor is trivial or nothrow.
David Blaikie7e70d682015-08-18 22:40:54 +00003954 struct CallEndCatch final : EHScopeStack::Cleanup {
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00003955 CallEndCatch(bool MightThrow) : MightThrow(MightThrow) {}
3956 bool MightThrow;
3957
3958 void Emit(CodeGenFunction &CGF, Flags flags) override {
3959 if (!MightThrow) {
3960 CGF.EmitNounwindRuntimeCall(getEndCatchFn(CGF.CGM));
3961 return;
3962 }
3963
3964 CGF.EmitRuntimeCallOrInvoke(getEndCatchFn(CGF.CGM));
3965 }
3966 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00003967}
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00003968
3969/// Emits a call to __cxa_begin_catch and enters a cleanup to call
3970/// __cxa_end_catch.
3971///
3972/// \param EndMightThrow - true if __cxa_end_catch might throw
3973static llvm::Value *CallBeginCatch(CodeGenFunction &CGF,
3974 llvm::Value *Exn,
3975 bool EndMightThrow) {
3976 llvm::CallInst *call =
3977 CGF.EmitNounwindRuntimeCall(getBeginCatchFn(CGF.CGM), Exn);
3978
3979 CGF.EHStack.pushCleanup<CallEndCatch>(NormalAndEHCleanup, EndMightThrow);
3980
3981 return call;
3982}
3983
3984/// A "special initializer" callback for initializing a catch
3985/// parameter during catch initialization.
3986static void InitCatchParam(CodeGenFunction &CGF,
3987 const VarDecl &CatchParam,
John McCall7f416cc2015-09-08 08:05:57 +00003988 Address ParamAddr,
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00003989 SourceLocation Loc) {
3990 // Load the exception from where the landing pad saved it.
3991 llvm::Value *Exn = CGF.getExceptionFromSlot();
3992
3993 CanQualType CatchType =
3994 CGF.CGM.getContext().getCanonicalType(CatchParam.getType());
3995 llvm::Type *LLVMCatchTy = CGF.ConvertTypeForMem(CatchType);
3996
3997 // If we're catching by reference, we can just cast the object
3998 // pointer to the appropriate pointer.
3999 if (isa<ReferenceType>(CatchType)) {
4000 QualType CaughtType = cast<ReferenceType>(CatchType)->getPointeeType();
4001 bool EndCatchMightThrow = CaughtType->isRecordType();
4002
4003 // __cxa_begin_catch returns the adjusted object pointer.
4004 llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, EndCatchMightThrow);
4005
4006 // We have no way to tell the personality function that we're
4007 // catching by reference, so if we're catching a pointer,
4008 // __cxa_begin_catch will actually return that pointer by value.
4009 if (const PointerType *PT = dyn_cast<PointerType>(CaughtType)) {
4010 QualType PointeeType = PT->getPointeeType();
4011
4012 // When catching by reference, generally we should just ignore
4013 // this by-value pointer and use the exception object instead.
4014 if (!PointeeType->isRecordType()) {
4015
4016 // Exn points to the struct _Unwind_Exception header, which
4017 // we have to skip past in order to reach the exception data.
4018 unsigned HeaderSize =
4019 CGF.CGM.getTargetCodeGenInfo().getSizeOfUnwindException();
4020 AdjustedExn = CGF.Builder.CreateConstGEP1_32(Exn, HeaderSize);
4021
4022 // However, if we're catching a pointer-to-record type that won't
4023 // work, because the personality function might have adjusted
4024 // the pointer. There's actually no way for us to fully satisfy
4025 // the language/ABI contract here: we can't use Exn because it
4026 // might have the wrong adjustment, but we can't use the by-value
4027 // pointer because it's off by a level of abstraction.
4028 //
4029 // The current solution is to dump the adjusted pointer into an
4030 // alloca, which breaks language semantics (because changing the
4031 // pointer doesn't change the exception) but at least works.
4032 // The better solution would be to filter out non-exact matches
4033 // and rethrow them, but this is tricky because the rethrow
4034 // really needs to be catchable by other sites at this landing
4035 // pad. The best solution is to fix the personality function.
4036 } else {
4037 // Pull the pointer for the reference type off.
4038 llvm::Type *PtrTy =
4039 cast<llvm::PointerType>(LLVMCatchTy)->getElementType();
4040
4041 // Create the temporary and write the adjusted pointer into it.
John McCall7f416cc2015-09-08 08:05:57 +00004042 Address ExnPtrTmp =
4043 CGF.CreateTempAlloca(PtrTy, CGF.getPointerAlign(), "exn.byref.tmp");
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004044 llvm::Value *Casted = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
4045 CGF.Builder.CreateStore(Casted, ExnPtrTmp);
4046
4047 // Bind the reference to the temporary.
John McCall7f416cc2015-09-08 08:05:57 +00004048 AdjustedExn = ExnPtrTmp.getPointer();
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004049 }
4050 }
4051
4052 llvm::Value *ExnCast =
4053 CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.byref");
4054 CGF.Builder.CreateStore(ExnCast, ParamAddr);
4055 return;
4056 }
4057
4058 // Scalars and complexes.
4059 TypeEvaluationKind TEK = CGF.getEvaluationKind(CatchType);
4060 if (TEK != TEK_Aggregate) {
4061 llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, false);
4062
4063 // If the catch type is a pointer type, __cxa_begin_catch returns
4064 // the pointer by value.
4065 if (CatchType->hasPointerRepresentation()) {
4066 llvm::Value *CastExn =
4067 CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.casted");
4068
4069 switch (CatchType.getQualifiers().getObjCLifetime()) {
4070 case Qualifiers::OCL_Strong:
4071 CastExn = CGF.EmitARCRetainNonBlock(CastExn);
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +00004072 LLVM_FALLTHROUGH;
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004073
4074 case Qualifiers::OCL_None:
4075 case Qualifiers::OCL_ExplicitNone:
4076 case Qualifiers::OCL_Autoreleasing:
4077 CGF.Builder.CreateStore(CastExn, ParamAddr);
4078 return;
4079
4080 case Qualifiers::OCL_Weak:
4081 CGF.EmitARCInitWeak(ParamAddr, CastExn);
4082 return;
4083 }
4084 llvm_unreachable("bad ownership qualifier!");
4085 }
4086
4087 // Otherwise, it returns a pointer into the exception object.
4088
4089 llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
4090 llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
4091
4092 LValue srcLV = CGF.MakeNaturalAlignAddrLValue(Cast, CatchType);
John McCall7f416cc2015-09-08 08:05:57 +00004093 LValue destLV = CGF.MakeAddrLValue(ParamAddr, CatchType);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004094 switch (TEK) {
4095 case TEK_Complex:
4096 CGF.EmitStoreOfComplex(CGF.EmitLoadOfComplex(srcLV, Loc), destLV,
4097 /*init*/ true);
4098 return;
4099 case TEK_Scalar: {
4100 llvm::Value *ExnLoad = CGF.EmitLoadOfScalar(srcLV, Loc);
4101 CGF.EmitStoreOfScalar(ExnLoad, destLV, /*init*/ true);
4102 return;
4103 }
4104 case TEK_Aggregate:
4105 llvm_unreachable("evaluation kind filtered out!");
4106 }
4107 llvm_unreachable("bad evaluation kind");
4108 }
4109
4110 assert(isa<RecordType>(CatchType) && "unexpected catch type!");
John McCall7f416cc2015-09-08 08:05:57 +00004111 auto catchRD = CatchType->getAsCXXRecordDecl();
4112 CharUnits caughtExnAlignment = CGF.CGM.getClassPointerAlignment(catchRD);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004113
4114 llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
4115
4116 // Check for a copy expression. If we don't have a copy expression,
4117 // that means a trivial copy is okay.
4118 const Expr *copyExpr = CatchParam.getInit();
4119 if (!copyExpr) {
4120 llvm::Value *rawAdjustedExn = CallBeginCatch(CGF, Exn, true);
John McCall7f416cc2015-09-08 08:05:57 +00004121 Address adjustedExn(CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy),
4122 caughtExnAlignment);
Ivan A. Kosarev1860b522018-01-25 14:21:55 +00004123 LValue Dest = CGF.MakeAddrLValue(ParamAddr, CatchType);
4124 LValue Src = CGF.MakeAddrLValue(adjustedExn, CatchType);
Richard Smithe78fac52018-04-05 20:52:58 +00004125 CGF.EmitAggregateCopy(Dest, Src, CatchType, AggValueSlot::DoesNotOverlap);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004126 return;
4127 }
4128
4129 // We have to call __cxa_get_exception_ptr to get the adjusted
4130 // pointer before copying.
4131 llvm::CallInst *rawAdjustedExn =
4132 CGF.EmitNounwindRuntimeCall(getGetExceptionPtrFn(CGF.CGM), Exn);
4133
4134 // Cast that to the appropriate type.
John McCall7f416cc2015-09-08 08:05:57 +00004135 Address adjustedExn(CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy),
4136 caughtExnAlignment);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004137
4138 // The copy expression is defined in terms of an OpaqueValueExpr.
4139 // Find it and map it to the adjusted expression.
4140 CodeGenFunction::OpaqueValueMapping
4141 opaque(CGF, OpaqueValueExpr::findInCopyConstruct(copyExpr),
4142 CGF.MakeAddrLValue(adjustedExn, CatchParam.getType()));
4143
4144 // Call the copy ctor in a terminate scope.
4145 CGF.EHStack.pushTerminate();
4146
4147 // Perform the copy construction.
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004148 CGF.EmitAggExpr(copyExpr,
John McCall7f416cc2015-09-08 08:05:57 +00004149 AggValueSlot::forAddr(ParamAddr, Qualifiers(),
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004150 AggValueSlot::IsNotDestructed,
4151 AggValueSlot::DoesNotNeedGCBarriers,
Richard Smithe78fac52018-04-05 20:52:58 +00004152 AggValueSlot::IsNotAliased,
4153 AggValueSlot::DoesNotOverlap));
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004154
4155 // Leave the terminate scope.
4156 CGF.EHStack.popTerminate();
4157
4158 // Undo the opaque value mapping.
4159 opaque.pop();
4160
4161 // Finally we can call __cxa_begin_catch.
4162 CallBeginCatch(CGF, Exn, true);
4163}
4164
4165/// Begins a catch statement by initializing the catch variable and
4166/// calling __cxa_begin_catch.
4167void ItaniumCXXABI::emitBeginCatch(CodeGenFunction &CGF,
4168 const CXXCatchStmt *S) {
4169 // We have to be very careful with the ordering of cleanups here:
4170 // C++ [except.throw]p4:
4171 // The destruction [of the exception temporary] occurs
4172 // immediately after the destruction of the object declared in
4173 // the exception-declaration in the handler.
4174 //
4175 // So the precise ordering is:
4176 // 1. Construct catch variable.
4177 // 2. __cxa_begin_catch
4178 // 3. Enter __cxa_end_catch cleanup
4179 // 4. Enter dtor cleanup
4180 //
4181 // We do this by using a slightly abnormal initialization process.
4182 // Delegation sequence:
4183 // - ExitCXXTryStmt opens a RunCleanupsScope
4184 // - EmitAutoVarAlloca creates the variable and debug info
4185 // - InitCatchParam initializes the variable from the exception
4186 // - CallBeginCatch calls __cxa_begin_catch
4187 // - CallBeginCatch enters the __cxa_end_catch cleanup
4188 // - EmitAutoVarCleanups enters the variable destructor cleanup
4189 // - EmitCXXTryStmt emits the code for the catch body
4190 // - EmitCXXTryStmt close the RunCleanupsScope
4191
4192 VarDecl *CatchParam = S->getExceptionDecl();
4193 if (!CatchParam) {
4194 llvm::Value *Exn = CGF.getExceptionFromSlot();
4195 CallBeginCatch(CGF, Exn, true);
4196 return;
4197 }
4198
4199 // Emit the local.
4200 CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004201 InitCatchParam(CGF, *CatchParam, var.getObjectAddress(CGF), S->getBeginLoc());
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004202 CGF.EmitAutoVarCleanups(var);
4203}
4204
4205/// Get or define the following function:
4206/// void @__clang_call_terminate(i8* %exn) nounwind noreturn
4207/// This code is used only in C++.
4208static llvm::Constant *getClangCallTerminateFn(CodeGenModule &CGM) {
4209 llvm::FunctionType *fnTy =
4210 llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
Reid Klecknerde864822017-03-21 16:57:30 +00004211 llvm::Constant *fnRef = CGM.CreateRuntimeFunction(
4212 fnTy, "__clang_call_terminate", llvm::AttributeList(), /*Local=*/true);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004213
4214 llvm::Function *fn = dyn_cast<llvm::Function>(fnRef);
4215 if (fn && fn->empty()) {
4216 fn->setDoesNotThrow();
4217 fn->setDoesNotReturn();
4218
4219 // What we really want is to massively penalize inlining without
4220 // forbidding it completely. The difference between that and
4221 // 'noinline' is negligible.
4222 fn->addFnAttr(llvm::Attribute::NoInline);
4223
4224 // Allow this function to be shared across translation units, but
4225 // we don't want it to turn into an exported symbol.
4226 fn->setLinkage(llvm::Function::LinkOnceODRLinkage);
4227 fn->setVisibility(llvm::Function::HiddenVisibility);
NAKAMURA Takumic7da6da2015-05-09 21:10:07 +00004228 if (CGM.supportsCOMDAT())
4229 fn->setComdat(CGM.getModule().getOrInsertComdat(fn->getName()));
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004230
4231 // Set up the function.
4232 llvm::BasicBlock *entry =
4233 llvm::BasicBlock::Create(CGM.getLLVMContext(), "", fn);
John McCall7f416cc2015-09-08 08:05:57 +00004234 CGBuilderTy builder(CGM, entry);
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004235
4236 // Pull the exception pointer out of the parameter list.
4237 llvm::Value *exn = &*fn->arg_begin();
4238
4239 // Call __cxa_begin_catch(exn).
4240 llvm::CallInst *catchCall = builder.CreateCall(getBeginCatchFn(CGM), exn);
4241 catchCall->setDoesNotThrow();
4242 catchCall->setCallingConv(CGM.getRuntimeCC());
4243
4244 // Call std::terminate().
David Blaikie4ba525b2015-07-14 17:27:39 +00004245 llvm::CallInst *termCall = builder.CreateCall(CGM.getTerminateFn());
Reid Klecknerfff8e7f2015-03-03 19:21:04 +00004246 termCall->setDoesNotThrow();
4247 termCall->setDoesNotReturn();
4248 termCall->setCallingConv(CGM.getRuntimeCC());
4249
4250 // std::terminate cannot return.
4251 builder.CreateUnreachable();
4252 }
4253
4254 return fnRef;
4255}
4256
4257llvm::CallInst *
4258ItaniumCXXABI::emitTerminateForUnexpectedException(CodeGenFunction &CGF,
4259 llvm::Value *Exn) {
4260 // In C++, we want to call __cxa_begin_catch() before terminating.
4261 if (Exn) {
4262 assert(CGF.CGM.getLangOpts().CPlusPlus);
4263 return CGF.EmitNounwindRuntimeCall(getClangCallTerminateFn(CGF.CGM), Exn);
4264 }
4265 return CGF.EmitNounwindRuntimeCall(CGF.CGM.getTerminateFn());
4266}
Peter Collingbourne60108802017-12-13 21:53:04 +00004267
4268std::pair<llvm::Value *, const CXXRecordDecl *>
4269ItaniumCXXABI::LoadVTablePtr(CodeGenFunction &CGF, Address This,
4270 const CXXRecordDecl *RD) {
4271 return {CGF.GetVTablePtr(This, CGM.Int8PtrTy, RD), RD};
4272}
Heejin Ahnc6479192018-05-31 22:18:13 +00004273
4274void WebAssemblyCXXABI::emitBeginCatch(CodeGenFunction &CGF,
4275 const CXXCatchStmt *C) {
Heejin Ahn1eb074d2018-06-01 01:01:37 +00004276 if (CGF.getTarget().hasFeature("exception-handling"))
4277 CGF.EHStack.pushCleanup<CatchRetScope>(
4278 NormalCleanup, cast<llvm::CatchPadInst>(CGF.CurrentFuncletPad));
Heejin Ahnc6479192018-05-31 22:18:13 +00004279 ItaniumCXXABI::emitBeginCatch(CGF, C);
4280}