blob: 0c4008f8ee78dfbd8c4f161a7d04a3df9d12725b [file] [log] [blame]
Charles Davis3a811f12010-05-25 19:52:27 +00001//===------- ItaniumCXXABI.cpp - Emit LLVM Code from ASTs for a Module ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000010// This provides C++ code generation targeting the Itanium C++ ABI. The class
Charles Davis3a811f12010-05-25 19:52:27 +000011// in this file generates structures that follow the Itanium C++ ABI, which is
12// documented at:
13// http://www.codesourcery.com/public/cxx-abi/abi.html
14// http://www.codesourcery.com/public/cxx-abi/abi-eh.html
John McCallee79a4c2010-08-21 22:46:04 +000015//
16// It also supports the closely-related ARM ABI, documented at:
17// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
18//
Charles Davis3a811f12010-05-25 19:52:27 +000019//===----------------------------------------------------------------------===//
20
21#include "CGCXXABI.h"
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070022#include "CGCleanup.h"
John McCall0bab0cd2010-08-23 01:21:21 +000023#include "CGRecordLayout.h"
Charles Davis9ee494f2012-06-23 23:44:00 +000024#include "CGVTables.h"
John McCall93d557b2010-08-22 00:05:51 +000025#include "CodeGenFunction.h"
Charles Davis3a811f12010-05-25 19:52:27 +000026#include "CodeGenModule.h"
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070027#include "TargetInfo.h"
Craig Topperba77cb92012-09-15 18:47:51 +000028#include "clang/AST/Mangle.h"
29#include "clang/AST/Type.h"
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070030#include "clang/AST/StmtCXX.h"
Stephen Hinesc568f1e2014-07-21 00:47:37 -070031#include "llvm/IR/CallSite.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000032#include "llvm/IR/DataLayout.h"
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070033#include "llvm/IR/Instructions.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000034#include "llvm/IR/Intrinsics.h"
35#include "llvm/IR/Value.h"
Charles Davis3a811f12010-05-25 19:52:27 +000036
37using namespace clang;
John McCall93d557b2010-08-22 00:05:51 +000038using namespace CodeGen;
Charles Davis3a811f12010-05-25 19:52:27 +000039
40namespace {
Charles Davis071cc7d2010-08-16 03:33:14 +000041class ItaniumCXXABI : public CodeGen::CGCXXABI {
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +000042 /// VTables - All the vtables which have been defined.
43 llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> VTables;
44
John McCall93d557b2010-08-22 00:05:51 +000045protected:
Mark Seaborn21fe4502013-07-24 16:25:13 +000046 bool UseARMMethodPtrABI;
47 bool UseARMGuardVarABI;
John McCall0bab0cd2010-08-23 01:21:21 +000048
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +000049 ItaniumMangleContext &getMangleContext() {
50 return cast<ItaniumMangleContext>(CodeGen::CGCXXABI::getMangleContext());
51 }
52
Charles Davis3a811f12010-05-25 19:52:27 +000053public:
Mark Seaborn21fe4502013-07-24 16:25:13 +000054 ItaniumCXXABI(CodeGen::CodeGenModule &CGM,
55 bool UseARMMethodPtrABI = false,
56 bool UseARMGuardVarABI = false) :
57 CGCXXABI(CGM), UseARMMethodPtrABI(UseARMMethodPtrABI),
58 UseARMGuardVarABI(UseARMGuardVarABI) { }
John McCall93d557b2010-08-22 00:05:51 +000059
Stephen Hines6bcf27b2014-05-29 04:14:42 -070060 bool classifyReturnType(CGFunctionInfo &FI) const override;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000061
Stephen Hines651f13c2014-04-23 16:59:28 -070062 RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const override {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000063 // Structures with either a non-trivial destructor or a non-trivial
64 // copy constructor are always indirect.
Stephen Hines6bcf27b2014-05-29 04:14:42 -070065 // FIXME: Use canCopyArgument() when it is fixed to handle lazily declared
66 // special members.
67 if (RD->hasNonTrivialDestructor() || RD->hasNonTrivialCopyConstructor())
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000068 return RAA_Indirect;
69 return RAA_Default;
70 }
71
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080072 bool isThisCompleteObject(GlobalDecl GD) const override {
73 // The Itanium ABI has separate complete-object vs. base-object
74 // variants of both constructors and destructors.
75 if (isa<CXXDestructorDecl>(GD.getDecl())) {
76 switch (GD.getDtorType()) {
77 case Dtor_Complete:
78 case Dtor_Deleting:
79 return true;
80
81 case Dtor_Base:
82 return false;
83
84 case Dtor_Comdat:
85 llvm_unreachable("emitting dtor comdat as function?");
86 }
87 llvm_unreachable("bad dtor kind");
88 }
89 if (isa<CXXConstructorDecl>(GD.getDecl())) {
90 switch (GD.getCtorType()) {
91 case Ctor_Complete:
92 return true;
93
94 case Ctor_Base:
95 return false;
96
97 case Ctor_CopyingClosure:
98 case Ctor_DefaultClosure:
99 llvm_unreachable("closure ctors in Itanium ABI?");
100
101 case Ctor_Comdat:
102 llvm_unreachable("emitting ctor comdat as function?");
103 }
104 llvm_unreachable("bad dtor kind");
105 }
106
107 // No other kinds.
108 return false;
109 }
110
Stephen Hines651f13c2014-04-23 16:59:28 -0700111 bool isZeroInitializable(const MemberPointerType *MPT) override;
John McCallcf2c85e2010-08-22 04:16:24 +0000112
Stephen Hines651f13c2014-04-23 16:59:28 -0700113 llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT) override;
John McCall0bab0cd2010-08-23 01:21:21 +0000114
Stephen Hines651f13c2014-04-23 16:59:28 -0700115 llvm::Value *
116 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
117 const Expr *E,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800118 Address This,
119 llvm::Value *&ThisPtrForCall,
Stephen Hines651f13c2014-04-23 16:59:28 -0700120 llvm::Value *MemFnPtr,
121 const MemberPointerType *MPT) override;
John McCall3023def2010-08-22 03:04:22 +0000122
Stephen Hines651f13c2014-04-23 16:59:28 -0700123 llvm::Value *
124 EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800125 Address Base,
Stephen Hines651f13c2014-04-23 16:59:28 -0700126 llvm::Value *MemPtr,
127 const MemberPointerType *MPT) override;
John McCall6c2ab1d2010-08-31 21:07:20 +0000128
John McCall0bab0cd2010-08-23 01:21:21 +0000129 llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
130 const CastExpr *E,
Stephen Hines651f13c2014-04-23 16:59:28 -0700131 llvm::Value *Src) override;
John McCall4d4e5c12012-02-15 01:22:51 +0000132 llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
Stephen Hines651f13c2014-04-23 16:59:28 -0700133 llvm::Constant *Src) override;
John McCallcf2c85e2010-08-22 04:16:24 +0000134
Stephen Hines651f13c2014-04-23 16:59:28 -0700135 llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT) override;
John McCallcf2c85e2010-08-22 04:16:24 +0000136
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800137 llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD) override;
John McCall5808ce42011-02-03 08:15:49 +0000138 llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
Stephen Hines651f13c2014-04-23 16:59:28 -0700139 CharUnits offset) override;
140 llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT) override;
Richard Smith2d6a5672012-01-14 04:30:29 +0000141 llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD,
142 CharUnits ThisAdjustment);
John McCall875ab102010-08-22 06:43:33 +0000143
John McCall0bab0cd2010-08-23 01:21:21 +0000144 llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
Stephen Hines651f13c2014-04-23 16:59:28 -0700145 llvm::Value *L, llvm::Value *R,
John McCall0bab0cd2010-08-23 01:21:21 +0000146 const MemberPointerType *MPT,
Stephen Hines651f13c2014-04-23 16:59:28 -0700147 bool Inequality) override;
John McCalle9fd7eb2010-08-22 08:30:07 +0000148
John McCall0bab0cd2010-08-23 01:21:21 +0000149 llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
Stephen Hines651f13c2014-04-23 16:59:28 -0700150 llvm::Value *Addr,
151 const MemberPointerType *MPT) override;
John McCall4c40d982010-08-31 07:33:07 +0000152
Stephen Hines176edba2014-12-01 14:53:08 -0800153 void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800154 Address Ptr, QualType ElementType,
Stephen Hines176edba2014-12-01 14:53:08 -0800155 const CXXDestructorDecl *Dtor) override;
John McCallecd03b42012-09-25 10:10:39 +0000156
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800157 /// Itanium says that an _Unwind_Exception has to be "double-word"
158 /// aligned (and thus the end of it is also so-aligned), meaning 16
159 /// bytes. Of course, that was written for the actual Itanium,
160 /// which is a 64-bit platform. Classically, the ABI doesn't really
161 /// specify the alignment on other platforms, but in practice
162 /// libUnwind declares the struct with __attribute__((aligned)), so
163 /// we assume that alignment here. (It's generally 16 bytes, but
164 /// some targets overwrite it.)
165 CharUnits getAlignmentOfExnObject() {
166 auto align = CGM.getContext().getTargetDefaultAlignForAttributeAligned();
167 return CGM.getContext().toCharUnitsFromBits(align);
168 }
169
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700170 void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700171 void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) override;
172
173 void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) override;
174
175 llvm::CallInst *
176 emitTerminateForUnexpectedException(CodeGenFunction &CGF,
177 llvm::Value *Exn) override;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700178
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700179 void EmitFundamentalRTTIDescriptor(QualType Type);
180 void EmitFundamentalRTTIDescriptors();
181 llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800182 CatchTypeInfo
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -0700183 getAddrOfCXXCatchHandlerType(QualType Ty,
184 QualType CatchHandlerType) override {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800185 return CatchTypeInfo{getAddrOfRTTIDescriptor(Ty), 0};
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700186 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700187
188 bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override;
189 void EmitBadTypeidCall(CodeGenFunction &CGF) override;
190 llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800191 Address ThisPtr,
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700192 llvm::Type *StdTypeInfoPtrTy) override;
193
194 bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
195 QualType SrcRecordTy) override;
196
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800197 llvm::Value *EmitDynamicCastCall(CodeGenFunction &CGF, Address Value,
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700198 QualType SrcRecordTy, QualType DestTy,
199 QualType DestRecordTy,
200 llvm::BasicBlock *CastEnd) override;
201
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800202 llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, Address Value,
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700203 QualType SrcRecordTy,
204 QualType DestTy) override;
205
206 bool EmitBadCastCall(CodeGenFunction &CGF) override;
207
Stephen Hines651f13c2014-04-23 16:59:28 -0700208 llvm::Value *
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800209 GetVirtualBaseClassOffset(CodeGenFunction &CGF, Address This,
Stephen Hines651f13c2014-04-23 16:59:28 -0700210 const CXXRecordDecl *ClassDecl,
211 const CXXRecordDecl *BaseClassDecl) override;
Reid Klecknerb0f533e2013-05-29 18:02:47 +0000212
Stephen Hines651f13c2014-04-23 16:59:28 -0700213 void EmitCXXConstructors(const CXXConstructorDecl *D) override;
Timur Iskhodzhanovbb1b7972013-08-04 17:30:04 +0000214
Stephen Hines176edba2014-12-01 14:53:08 -0800215 void buildStructorSignature(const CXXMethodDecl *MD, StructorType T,
216 SmallVectorImpl<CanQualType> &ArgTys) override;
John McCall4c40d982010-08-31 07:33:07 +0000217
Reid Klecknera4130ba2013-07-22 13:51:44 +0000218 bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
Stephen Hines651f13c2014-04-23 16:59:28 -0700219 CXXDtorType DT) const override {
Reid Klecknera4130ba2013-07-22 13:51:44 +0000220 // Itanium does not emit any destructor variant as an inline thunk.
221 // Delegating may occur as an optimization, but all variants are either
222 // emitted with external linkage or as linkonce if they are inline and used.
223 return false;
224 }
225
Stephen Hines651f13c2014-04-23 16:59:28 -0700226 void EmitCXXDestructors(const CXXDestructorDecl *D) override;
Reid Klecknera4130ba2013-07-22 13:51:44 +0000227
Stephen Hines651f13c2014-04-23 16:59:28 -0700228 void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy,
229 FunctionArgList &Params) override;
John McCall4c40d982010-08-31 07:33:07 +0000230
Stephen Hines651f13c2014-04-23 16:59:28 -0700231 void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override;
John McCall1e7fe752010-09-02 09:58:18 +0000232
Stephen Hines651f13c2014-04-23 16:59:28 -0700233 unsigned addImplicitConstructorArgs(CodeGenFunction &CGF,
234 const CXXConstructorDecl *D,
235 CXXCtorType Type, bool ForVirtualBase,
236 bool Delegating,
237 CallArgList &Args) override;
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000238
Stephen Hines651f13c2014-04-23 16:59:28 -0700239 void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD,
240 CXXDtorType Type, bool ForVirtualBase,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800241 bool Delegating, Address This) override;
Stephen Hines651f13c2014-04-23 16:59:28 -0700242
243 void emitVTableDefinitions(CodeGenVTables &CGVT,
244 const CXXRecordDecl *RD) override;
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +0000245
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800246 bool isVirtualOffsetNeededForVTableField(CodeGenFunction &CGF,
247 CodeGenFunction::VPtr Vptr) override;
248
249 bool doStructorsInitializeVPtrs(const CXXRecordDecl *VTableClass) override {
250 return true;
251 }
252
253 llvm::Constant *
254 getVTableAddressPoint(BaseSubobject Base,
255 const CXXRecordDecl *VTableClass) override;
256
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +0000257 llvm::Value *getVTableAddressPointInStructor(
258 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800259 BaseSubobject Base, const CXXRecordDecl *NearestVBase) override;
260
261 llvm::Value *getVTableAddressPointInStructorWithVTT(
262 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass,
263 BaseSubobject Base, const CXXRecordDecl *NearestVBase);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +0000264
265 llvm::Constant *
266 getVTableAddressPointForConstExpr(BaseSubobject Base,
Stephen Hines651f13c2014-04-23 16:59:28 -0700267 const CXXRecordDecl *VTableClass) override;
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +0000268
269 llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
Stephen Hines651f13c2014-04-23 16:59:28 -0700270 CharUnits VPtrOffset) override;
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +0000271
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000272 llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800273 Address This, llvm::Type *Ty,
274 SourceLocation Loc) override;
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000275
Stephen Hines176edba2014-12-01 14:53:08 -0800276 llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF,
277 const CXXDestructorDecl *Dtor,
278 CXXDtorType DtorType,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800279 Address This,
Stephen Hines176edba2014-12-01 14:53:08 -0800280 const CXXMemberCallExpr *CE) override;
Timur Iskhodzhanov0f9827f2013-02-15 14:45:22 +0000281
Stephen Hines651f13c2014-04-23 16:59:28 -0700282 void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override;
Reid Kleckner90633022013-06-19 15:20:38 +0000283
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800284 bool canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const override;
285
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700286 void setThunkLinkage(llvm::Function *Thunk, bool ForVTable, GlobalDecl GD,
287 bool ReturnAdjustment) override {
Timur Iskhodzhanov2cb17a02013-10-09 09:23:58 +0000288 // Allow inlining of thunks by emitting them with available_externally
289 // linkage together with vtables when needed.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800290 if (ForVTable && !Thunk->hasLocalLinkage())
Timur Iskhodzhanov2cb17a02013-10-09 09:23:58 +0000291 Thunk->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
292 }
293
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800294 llvm::Value *performThisAdjustment(CodeGenFunction &CGF, Address This,
Stephen Hines651f13c2014-04-23 16:59:28 -0700295 const ThisAdjustment &TA) override;
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +0000296
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800297 llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, Address Ret,
Stephen Hines651f13c2014-04-23 16:59:28 -0700298 const ReturnAdjustment &RA) override;
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +0000299
Stephen Hines176edba2014-12-01 14:53:08 -0800300 size_t getSrcArgforCopyCtor(const CXXConstructorDecl *,
301 FunctionArgList &Args) const override {
302 assert(!Args.empty() && "expected the arglist to not be empty!");
303 return Args.size() - 1;
304 }
305
Stephen Hines651f13c2014-04-23 16:59:28 -0700306 StringRef GetPureVirtualCallName() override { return "__cxa_pure_virtual"; }
307 StringRef GetDeletedVirtualCallName() override
308 { return "__cxa_deleted_virtual"; }
Joao Matos285baac2012-07-17 17:10:11 +0000309
Stephen Hines651f13c2014-04-23 16:59:28 -0700310 CharUnits getArrayCookieSizeImpl(QualType elementType) override;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800311 Address InitializeArrayCookie(CodeGenFunction &CGF,
312 Address NewPtr,
313 llvm::Value *NumElements,
314 const CXXNewExpr *expr,
315 QualType ElementType) override;
John McCalle2b45e22012-05-01 05:23:51 +0000316 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800317 Address allocPtr,
Stephen Hines651f13c2014-04-23 16:59:28 -0700318 CharUnits cookieSize) override;
John McCall5cd91b52010-09-08 01:44:27 +0000319
John McCall3030eb82010-11-06 09:44:32 +0000320 void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
Stephen Hines651f13c2014-04-23 16:59:28 -0700321 llvm::GlobalVariable *DeclPtr,
322 bool PerformInit) override;
Richard Smith04e51762013-04-14 23:01:42 +0000323 void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
Stephen Hines651f13c2014-04-23 16:59:28 -0700324 llvm::Constant *dtor, llvm::Constant *addr) override;
Richard Smithb80a16e2013-04-19 16:42:07 +0000325
326 llvm::Function *getOrCreateThreadLocalWrapper(const VarDecl *VD,
Stephen Hines176edba2014-12-01 14:53:08 -0800327 llvm::Value *Val);
Richard Smithb80a16e2013-04-19 16:42:07 +0000328 void EmitThreadLocalInitFuncs(
Stephen Hines176edba2014-12-01 14:53:08 -0800329 CodeGenModule &CGM,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800330 ArrayRef<const VarDecl *> CXXThreadLocals,
Stephen Hines176edba2014-12-01 14:53:08 -0800331 ArrayRef<llvm::Function *> CXXThreadLocalInits,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800332 ArrayRef<const VarDecl *> CXXThreadLocalInitVars) override;
Stephen Hines176edba2014-12-01 14:53:08 -0800333
334 bool usesThreadWrapperFunction() const override { return true; }
Stephen Hines651f13c2014-04-23 16:59:28 -0700335 LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD,
336 QualType LValType) override;
Peter Collingbournee1e35f72013-06-28 20:45:28 +0000337
Stephen Hines651f13c2014-04-23 16:59:28 -0700338 bool NeedsVTTParameter(GlobalDecl GD) override;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700339
340 /**************************** RTTI Uniqueness ******************************/
341
342protected:
343 /// Returns true if the ABI requires RTTI type_info objects to be unique
344 /// across a program.
345 virtual bool shouldRTTIBeUnique() const { return true; }
346
347public:
348 /// What sort of unique-RTTI behavior should we use?
349 enum RTTIUniquenessKind {
350 /// We are guaranteeing, or need to guarantee, that the RTTI string
351 /// is unique.
352 RUK_Unique,
353
354 /// We are not guaranteeing uniqueness for the RTTI string, so we
355 /// can demote to hidden visibility but must use string comparisons.
356 RUK_NonUniqueHidden,
357
358 /// We are not guaranteeing uniqueness for the RTTI string, so we
359 /// have to use string comparisons, but we also have to emit it with
360 /// non-hidden visibility.
361 RUK_NonUniqueVisible
362 };
363
364 /// Return the required visibility status for the given type and linkage in
365 /// the current ABI.
366 RTTIUniquenessKind
367 classifyRTTIUniqueness(QualType CanTy,
368 llvm::GlobalValue::LinkageTypes Linkage) const;
369 friend class ItaniumRTTIBuilder;
Stephen Hines176edba2014-12-01 14:53:08 -0800370
371 void emitCXXStructor(const CXXMethodDecl *MD, StructorType Type) override;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800372
373 private:
374 bool hasAnyUsedVirtualInlineFunction(const CXXRecordDecl *RD) const {
375 const auto &VtableLayout =
376 CGM.getItaniumVTableContext().getVTableLayout(RD);
377
378 for (const auto &VtableComponent : VtableLayout.vtable_components()) {
379 if (!VtableComponent.isUsedFunctionPointerKind())
380 continue;
381
382 const CXXMethodDecl *Method = VtableComponent.getFunctionDecl();
383 if (Method->getCanonicalDecl()->isInlined())
384 return true;
385 }
386 return false;
387 }
388
389 bool isVTableHidden(const CXXRecordDecl *RD) const {
390 const auto &VtableLayout =
391 CGM.getItaniumVTableContext().getVTableLayout(RD);
392
393 for (const auto &VtableComponent : VtableLayout.vtable_components()) {
394 if (VtableComponent.isRTTIKind()) {
395 const CXXRecordDecl *RTTIDecl = VtableComponent.getRTTIDecl();
396 if (RTTIDecl->getVisibility() == Visibility::HiddenVisibility)
397 return true;
398 } else if (VtableComponent.isUsedFunctionPointerKind()) {
399 const CXXMethodDecl *Method = VtableComponent.getFunctionDecl();
400 if (Method->getVisibility() == Visibility::HiddenVisibility &&
401 !Method->isDefined())
402 return true;
403 }
404 }
405 return false;
406 }
Charles Davis3a811f12010-05-25 19:52:27 +0000407};
John McCallee79a4c2010-08-21 22:46:04 +0000408
409class ARMCXXABI : public ItaniumCXXABI {
410public:
Mark Seaborn21fe4502013-07-24 16:25:13 +0000411 ARMCXXABI(CodeGen::CodeGenModule &CGM) :
412 ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
413 /* UseARMGuardVarABI = */ true) {}
John McCall4c40d982010-08-31 07:33:07 +0000414
Stephen Hines651f13c2014-04-23 16:59:28 -0700415 bool HasThisReturn(GlobalDecl GD) const override {
Stephen Lin3b50e8d2013-06-30 20:40:16 +0000416 return (isa<CXXConstructorDecl>(GD.getDecl()) || (
417 isa<CXXDestructorDecl>(GD.getDecl()) &&
418 GD.getDtorType() != Dtor_Deleting));
419 }
John McCall4c40d982010-08-31 07:33:07 +0000420
Stephen Hines651f13c2014-04-23 16:59:28 -0700421 void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV,
422 QualType ResTy) override;
John McCall4c40d982010-08-31 07:33:07 +0000423
Stephen Hines651f13c2014-04-23 16:59:28 -0700424 CharUnits getArrayCookieSizeImpl(QualType elementType) override;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800425 Address InitializeArrayCookie(CodeGenFunction &CGF,
426 Address NewPtr,
427 llvm::Value *NumElements,
428 const CXXNewExpr *expr,
429 QualType ElementType) override;
430 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, Address allocPtr,
Stephen Hines651f13c2014-04-23 16:59:28 -0700431 CharUnits cookieSize) override;
432};
433
434class iOS64CXXABI : public ARMCXXABI {
435public:
436 iOS64CXXABI(CodeGen::CodeGenModule &CGM) : ARMCXXABI(CGM) {}
437
438 // ARM64 libraries are prepared for non-unique RTTI.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700439 bool shouldRTTIBeUnique() const override { return false; }
John McCallee79a4c2010-08-21 22:46:04 +0000440};
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800441
442class WebAssemblyCXXABI final : public ItaniumCXXABI {
443public:
444 explicit WebAssemblyCXXABI(CodeGen::CodeGenModule &CGM)
445 : ItaniumCXXABI(CGM, /*UseARMMethodPtrABI=*/true,
446 /*UseARMGuardVarABI=*/true) {}
447
448private:
449 bool HasThisReturn(GlobalDecl GD) const override {
450 return isa<CXXConstructorDecl>(GD.getDecl()) ||
451 (isa<CXXDestructorDecl>(GD.getDecl()) &&
452 GD.getDtorType() != Dtor_Deleting);
453 }
454};
Charles Davis3a811f12010-05-25 19:52:27 +0000455}
456
Charles Davis071cc7d2010-08-16 03:33:14 +0000457CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
John McCall64aa4b32013-04-16 22:48:15 +0000458 switch (CGM.getTarget().getCXXABI().getKind()) {
John McCall96fcde02013-01-25 23:36:14 +0000459 // For IR-generation purposes, there's no significant difference
460 // between the ARM and iOS ABIs.
461 case TargetCXXABI::GenericARM:
462 case TargetCXXABI::iOS:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800463 case TargetCXXABI::WatchOS:
John McCall96fcde02013-01-25 23:36:14 +0000464 return new ARMCXXABI(CGM);
Charles Davis3a811f12010-05-25 19:52:27 +0000465
Stephen Hines651f13c2014-04-23 16:59:28 -0700466 case TargetCXXABI::iOS64:
467 return new iOS64CXXABI(CGM);
468
Tim Northoverc264e162013-01-31 12:13:10 +0000469 // Note that AArch64 uses the generic ItaniumCXXABI class since it doesn't
470 // include the other 32-bit ARM oddities: constructor/destructor return values
471 // and array cookies.
472 case TargetCXXABI::GenericAArch64:
Mark Seaborn21fe4502013-07-24 16:25:13 +0000473 return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
474 /* UseARMGuardVarABI = */ true);
Tim Northoverc264e162013-01-31 12:13:10 +0000475
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700476 case TargetCXXABI::GenericMIPS:
477 return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true);
478
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800479 case TargetCXXABI::WebAssembly:
480 return new WebAssemblyCXXABI(CGM);
481
John McCall96fcde02013-01-25 23:36:14 +0000482 case TargetCXXABI::GenericItanium:
Mark Seaborn21fe4502013-07-24 16:25:13 +0000483 if (CGM.getContext().getTargetInfo().getTriple().getArch()
484 == llvm::Triple::le32) {
485 // For PNaCl, use ARM-style method pointers so that PNaCl code
486 // does not assume anything about the alignment of function
487 // pointers.
488 return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
489 /* UseARMGuardVarABI = */ false);
490 }
John McCall96fcde02013-01-25 23:36:14 +0000491 return new ItaniumCXXABI(CGM);
492
493 case TargetCXXABI::Microsoft:
494 llvm_unreachable("Microsoft ABI is not Itanium-based");
495 }
496 llvm_unreachable("bad ABI kind");
John McCallee79a4c2010-08-21 22:46:04 +0000497}
498
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000499llvm::Type *
John McCall0bab0cd2010-08-23 01:21:21 +0000500ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
501 if (MPT->isMemberDataPointer())
Reid Kleckner92e44d92013-03-22 16:13:10 +0000502 return CGM.PtrDiffTy;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700503 return llvm::StructType::get(CGM.PtrDiffTy, CGM.PtrDiffTy, nullptr);
John McCall875ab102010-08-22 06:43:33 +0000504}
505
John McCallbabc9a92010-08-22 00:59:17 +0000506/// In the Itanium and ARM ABIs, method pointers have the form:
507/// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
508///
509/// In the Itanium ABI:
510/// - method pointers are virtual if (memptr.ptr & 1) is nonzero
511/// - the this-adjustment is (memptr.adj)
512/// - the virtual offset is (memptr.ptr - 1)
513///
514/// In the ARM ABI:
515/// - method pointers are virtual if (memptr.adj & 1) is nonzero
516/// - the this-adjustment is (memptr.adj >> 1)
517/// - the virtual offset is (memptr.ptr)
518/// ARM uses 'adj' for the virtual flag because Thumb functions
519/// may be only single-byte aligned.
520///
521/// If the member is virtual, the adjusted 'this' pointer points
522/// to a vtable pointer from which the virtual offset is applied.
523///
524/// If the member is non-virtual, memptr.ptr is the address of
525/// the function to call.
Stephen Hines651f13c2014-04-23 16:59:28 -0700526llvm::Value *ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800527 CodeGenFunction &CGF, const Expr *E, Address ThisAddr,
528 llvm::Value *&ThisPtrForCall,
Stephen Hines651f13c2014-04-23 16:59:28 -0700529 llvm::Value *MemFnPtr, const MemberPointerType *MPT) {
John McCall93d557b2010-08-22 00:05:51 +0000530 CGBuilderTy &Builder = CGF.Builder;
531
532 const FunctionProtoType *FPT =
533 MPT->getPointeeType()->getAs<FunctionProtoType>();
534 const CXXRecordDecl *RD =
535 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
536
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800537 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(
538 CGM.getTypes().arrangeCXXMethodType(RD, FPT, /*FD=*/nullptr));
John McCall93d557b2010-08-22 00:05:51 +0000539
Reid Kleckner92e44d92013-03-22 16:13:10 +0000540 llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(CGM.PtrDiffTy, 1);
John McCall93d557b2010-08-22 00:05:51 +0000541
John McCallbabc9a92010-08-22 00:59:17 +0000542 llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
543 llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
544 llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
545
John McCalld608cdb2010-08-22 10:59:02 +0000546 // Extract memptr.adj, which is in the second field.
547 llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
John McCallbabc9a92010-08-22 00:59:17 +0000548
549 // Compute the true adjustment.
550 llvm::Value *Adj = RawAdj;
Mark Seaborn21fe4502013-07-24 16:25:13 +0000551 if (UseARMMethodPtrABI)
John McCallbabc9a92010-08-22 00:59:17 +0000552 Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
John McCall93d557b2010-08-22 00:05:51 +0000553
554 // Apply the adjustment and cast back to the original struct type
555 // for consistency.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800556 llvm::Value *This = ThisAddr.getPointer();
John McCallbabc9a92010-08-22 00:59:17 +0000557 llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
558 Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
559 This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800560 ThisPtrForCall = This;
John McCall93d557b2010-08-22 00:05:51 +0000561
562 // Load the function pointer.
John McCalld608cdb2010-08-22 10:59:02 +0000563 llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
John McCall93d557b2010-08-22 00:05:51 +0000564
565 // If the LSB in the function pointer is 1, the function pointer points to
566 // a virtual function.
John McCallbabc9a92010-08-22 00:59:17 +0000567 llvm::Value *IsVirtual;
Mark Seaborn21fe4502013-07-24 16:25:13 +0000568 if (UseARMMethodPtrABI)
John McCallbabc9a92010-08-22 00:59:17 +0000569 IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
570 else
571 IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
572 IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
John McCall93d557b2010-08-22 00:05:51 +0000573 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
574
575 // In the virtual path, the adjustment left 'This' pointing to the
576 // vtable of the correct base subobject. The "function pointer" is an
John McCallbabc9a92010-08-22 00:59:17 +0000577 // offset within the vtable (+1 for the virtual flag on non-ARM).
John McCall93d557b2010-08-22 00:05:51 +0000578 CGF.EmitBlock(FnVirtual);
579
580 // Cast the adjusted this to a pointer to vtable pointer and load.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000581 llvm::Type *VTableTy = Builder.getInt8PtrTy();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800582 CharUnits VTablePtrAlign =
583 CGF.CGM.getDynamicOffsetAlignment(ThisAddr.getAlignment(), RD,
584 CGF.getPointerAlign());
585 llvm::Value *VTable =
586 CGF.GetVTablePtr(Address(This, VTablePtrAlign), VTableTy, RD);
John McCall93d557b2010-08-22 00:05:51 +0000587
588 // Apply the offset.
John McCallbabc9a92010-08-22 00:59:17 +0000589 llvm::Value *VTableOffset = FnAsInt;
Mark Seaborn21fe4502013-07-24 16:25:13 +0000590 if (!UseARMMethodPtrABI)
591 VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
John McCallbabc9a92010-08-22 00:59:17 +0000592 VTable = Builder.CreateGEP(VTable, VTableOffset);
John McCall93d557b2010-08-22 00:05:51 +0000593
594 // Load the virtual function to call.
595 VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800596 llvm::Value *VirtualFn =
597 Builder.CreateAlignedLoad(VTable, CGF.getPointerAlign(),
598 "memptr.virtualfn");
John McCall93d557b2010-08-22 00:05:51 +0000599 CGF.EmitBranch(FnEnd);
600
601 // In the non-virtual path, the function pointer is actually a
602 // function pointer.
603 CGF.EmitBlock(FnNonVirtual);
604 llvm::Value *NonVirtualFn =
John McCallbabc9a92010-08-22 00:59:17 +0000605 Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
John McCall93d557b2010-08-22 00:05:51 +0000606
607 // We're done.
608 CGF.EmitBlock(FnEnd);
Jay Foadbbf3bac2011-03-30 11:28:58 +0000609 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2);
John McCall93d557b2010-08-22 00:05:51 +0000610 Callee->addIncoming(VirtualFn, FnVirtual);
611 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
612 return Callee;
613}
John McCall3023def2010-08-22 03:04:22 +0000614
John McCall6c2ab1d2010-08-31 21:07:20 +0000615/// Compute an l-value by applying the given pointer-to-member to a
616/// base object.
Stephen Hines651f13c2014-04-23 16:59:28 -0700617llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800618 CodeGenFunction &CGF, const Expr *E, Address Base, llvm::Value *MemPtr,
Stephen Hines651f13c2014-04-23 16:59:28 -0700619 const MemberPointerType *MPT) {
Reid Kleckner92e44d92013-03-22 16:13:10 +0000620 assert(MemPtr->getType() == CGM.PtrDiffTy);
John McCall6c2ab1d2010-08-31 21:07:20 +0000621
622 CGBuilderTy &Builder = CGF.Builder;
623
John McCall6c2ab1d2010-08-31 21:07:20 +0000624 // Cast to char*.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800625 Base = Builder.CreateElementBitCast(Base, CGF.Int8Ty);
John McCall6c2ab1d2010-08-31 21:07:20 +0000626
627 // Apply the offset, which we assume is non-null.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800628 llvm::Value *Addr =
629 Builder.CreateInBoundsGEP(Base.getPointer(), MemPtr, "memptr.offset");
John McCall6c2ab1d2010-08-31 21:07:20 +0000630
631 // Cast the address to the appropriate pointer type, adopting the
632 // address space of the base pointer.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800633 llvm::Type *PType = CGF.ConvertTypeForMem(MPT->getPointeeType())
634 ->getPointerTo(Base.getAddressSpace());
John McCall6c2ab1d2010-08-31 21:07:20 +0000635 return Builder.CreateBitCast(Addr, PType);
636}
637
John McCall4d4e5c12012-02-15 01:22:51 +0000638/// Perform a bitcast, derived-to-base, or base-to-derived member pointer
639/// conversion.
640///
641/// Bitcast conversions are always a no-op under Itanium.
John McCall0bab0cd2010-08-23 01:21:21 +0000642///
643/// Obligatory offset/adjustment diagram:
644/// <-- offset --> <-- adjustment -->
645/// |--------------------------|----------------------|--------------------|
646/// ^Derived address point ^Base address point ^Member address point
647///
648/// So when converting a base member pointer to a derived member pointer,
649/// we add the offset to the adjustment because the address point has
650/// decreased; and conversely, when converting a derived MP to a base MP
651/// we subtract the offset from the adjustment because the address point
652/// has increased.
653///
654/// The standard forbids (at compile time) conversion to and from
655/// virtual bases, which is why we don't have to consider them here.
656///
657/// The standard forbids (at run time) casting a derived MP to a base
658/// MP when the derived MP does not point to a member of the base.
659/// This is why -1 is a reasonable choice for null data member
660/// pointers.
John McCalld608cdb2010-08-22 10:59:02 +0000661llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000662ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
663 const CastExpr *E,
John McCall4d4e5c12012-02-15 01:22:51 +0000664 llvm::Value *src) {
John McCall2de56d12010-08-25 11:45:40 +0000665 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
John McCall4d4e5c12012-02-15 01:22:51 +0000666 E->getCastKind() == CK_BaseToDerivedMemberPointer ||
667 E->getCastKind() == CK_ReinterpretMemberPointer);
668
669 // Under Itanium, reinterprets don't require any additional processing.
670 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
671
672 // Use constant emission if we can.
673 if (isa<llvm::Constant>(src))
674 return EmitMemberPointerConversion(E, cast<llvm::Constant>(src));
675
676 llvm::Constant *adj = getMemberPointerAdjustment(E);
677 if (!adj) return src;
John McCall3023def2010-08-22 03:04:22 +0000678
679 CGBuilderTy &Builder = CGF.Builder;
John McCall4d4e5c12012-02-15 01:22:51 +0000680 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
John McCall3023def2010-08-22 03:04:22 +0000681
John McCall4d4e5c12012-02-15 01:22:51 +0000682 const MemberPointerType *destTy =
683 E->getType()->castAs<MemberPointerType>();
John McCall875ab102010-08-22 06:43:33 +0000684
John McCall0bab0cd2010-08-23 01:21:21 +0000685 // For member data pointers, this is just a matter of adding the
686 // offset if the source is non-null.
John McCall4d4e5c12012-02-15 01:22:51 +0000687 if (destTy->isMemberDataPointer()) {
688 llvm::Value *dst;
689 if (isDerivedToBase)
690 dst = Builder.CreateNSWSub(src, adj, "adj");
John McCall0bab0cd2010-08-23 01:21:21 +0000691 else
John McCall4d4e5c12012-02-15 01:22:51 +0000692 dst = Builder.CreateNSWAdd(src, adj, "adj");
John McCall0bab0cd2010-08-23 01:21:21 +0000693
694 // Null check.
John McCall4d4e5c12012-02-15 01:22:51 +0000695 llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType());
696 llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull");
697 return Builder.CreateSelect(isNull, src, dst);
John McCall0bab0cd2010-08-23 01:21:21 +0000698 }
699
John McCalld608cdb2010-08-22 10:59:02 +0000700 // The this-adjustment is left-shifted by 1 on ARM.
Mark Seaborn21fe4502013-07-24 16:25:13 +0000701 if (UseARMMethodPtrABI) {
John McCall4d4e5c12012-02-15 01:22:51 +0000702 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
703 offset <<= 1;
704 adj = llvm::ConstantInt::get(adj->getType(), offset);
John McCalld608cdb2010-08-22 10:59:02 +0000705 }
706
John McCall4d4e5c12012-02-15 01:22:51 +0000707 llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj");
708 llvm::Value *dstAdj;
709 if (isDerivedToBase)
710 dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj");
John McCalld608cdb2010-08-22 10:59:02 +0000711 else
John McCall4d4e5c12012-02-15 01:22:51 +0000712 dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj");
John McCalld608cdb2010-08-22 10:59:02 +0000713
John McCall4d4e5c12012-02-15 01:22:51 +0000714 return Builder.CreateInsertValue(src, dstAdj, 1);
715}
716
717llvm::Constant *
718ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E,
719 llvm::Constant *src) {
720 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
721 E->getCastKind() == CK_BaseToDerivedMemberPointer ||
722 E->getCastKind() == CK_ReinterpretMemberPointer);
723
724 // Under Itanium, reinterprets don't require any additional processing.
725 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
726
727 // If the adjustment is trivial, we don't need to do anything.
728 llvm::Constant *adj = getMemberPointerAdjustment(E);
729 if (!adj) return src;
730
731 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
732
733 const MemberPointerType *destTy =
734 E->getType()->castAs<MemberPointerType>();
735
736 // For member data pointers, this is just a matter of adding the
737 // offset if the source is non-null.
738 if (destTy->isMemberDataPointer()) {
739 // null maps to null.
740 if (src->isAllOnesValue()) return src;
741
742 if (isDerivedToBase)
743 return llvm::ConstantExpr::getNSWSub(src, adj);
744 else
745 return llvm::ConstantExpr::getNSWAdd(src, adj);
746 }
747
748 // The this-adjustment is left-shifted by 1 on ARM.
Mark Seaborn21fe4502013-07-24 16:25:13 +0000749 if (UseARMMethodPtrABI) {
John McCall4d4e5c12012-02-15 01:22:51 +0000750 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
751 offset <<= 1;
752 adj = llvm::ConstantInt::get(adj->getType(), offset);
753 }
754
755 llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1);
756 llvm::Constant *dstAdj;
757 if (isDerivedToBase)
758 dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj);
759 else
760 dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj);
761
762 return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1);
John McCall3023def2010-08-22 03:04:22 +0000763}
John McCallcf2c85e2010-08-22 04:16:24 +0000764
765llvm::Constant *
John McCall0bab0cd2010-08-23 01:21:21 +0000766ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
John McCall0bab0cd2010-08-23 01:21:21 +0000767 // Itanium C++ ABI 2.3:
768 // A NULL pointer is represented as -1.
769 if (MPT->isMemberDataPointer())
Reid Kleckner92e44d92013-03-22 16:13:10 +0000770 return llvm::ConstantInt::get(CGM.PtrDiffTy, -1ULL, /*isSigned=*/true);
John McCalld608cdb2010-08-22 10:59:02 +0000771
Reid Kleckner92e44d92013-03-22 16:13:10 +0000772 llvm::Constant *Zero = llvm::ConstantInt::get(CGM.PtrDiffTy, 0);
John McCalld608cdb2010-08-22 10:59:02 +0000773 llvm::Constant *Values[2] = { Zero, Zero };
Chris Lattnerc5cbb902011-06-20 04:01:35 +0000774 return llvm::ConstantStruct::getAnon(Values);
John McCallcf2c85e2010-08-22 04:16:24 +0000775}
776
John McCall5808ce42011-02-03 08:15:49 +0000777llvm::Constant *
778ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
779 CharUnits offset) {
John McCall0bab0cd2010-08-23 01:21:21 +0000780 // Itanium C++ ABI 2.3:
781 // A pointer to data member is an offset from the base address of
782 // the class object containing it, represented as a ptrdiff_t
Reid Kleckner92e44d92013-03-22 16:13:10 +0000783 return llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity());
John McCall0bab0cd2010-08-23 01:21:21 +0000784}
785
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800786llvm::Constant *
787ItaniumCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) {
Richard Smith2d6a5672012-01-14 04:30:29 +0000788 return BuildMemberPointer(MD, CharUnits::Zero());
789}
790
791llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD,
792 CharUnits ThisAdjustment) {
John McCalld608cdb2010-08-22 10:59:02 +0000793 assert(MD->isInstance() && "Member function must not be static!");
794 MD = MD->getCanonicalDecl();
795
796 CodeGenTypes &Types = CGM.getTypes();
John McCalld608cdb2010-08-22 10:59:02 +0000797
798 // Get the function pointer (or index if this is a virtual function).
799 llvm::Constant *MemPtr[2];
800 if (MD->isVirtual()) {
Timur Iskhodzhanov5f0db582013-11-05 15:54:58 +0000801 uint64_t Index = CGM.getItaniumVTableContext().getMethodVTableIndex(MD);
John McCalld608cdb2010-08-22 10:59:02 +0000802
Ken Dyck1246ba62011-04-09 01:30:02 +0000803 const ASTContext &Context = getContext();
804 CharUnits PointerWidth =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000805 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Ken Dyck1246ba62011-04-09 01:30:02 +0000806 uint64_t VTableOffset = (Index * PointerWidth.getQuantity());
John McCalld608cdb2010-08-22 10:59:02 +0000807
Mark Seaborn21fe4502013-07-24 16:25:13 +0000808 if (UseARMMethodPtrABI) {
John McCalld608cdb2010-08-22 10:59:02 +0000809 // ARM C++ ABI 3.2.1:
810 // This ABI specifies that adj contains twice the this
811 // adjustment, plus 1 if the member function is virtual. The
812 // least significant bit of adj then makes exactly the same
813 // discrimination as the least significant bit of ptr does for
814 // Itanium.
Reid Kleckner92e44d92013-03-22 16:13:10 +0000815 MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset);
816 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
Richard Smith2d6a5672012-01-14 04:30:29 +0000817 2 * ThisAdjustment.getQuantity() + 1);
John McCalld608cdb2010-08-22 10:59:02 +0000818 } else {
819 // Itanium C++ ABI 2.3:
820 // For a virtual function, [the pointer field] is 1 plus the
821 // virtual table offset (in bytes) of the function,
822 // represented as a ptrdiff_t.
Reid Kleckner92e44d92013-03-22 16:13:10 +0000823 MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset + 1);
824 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
Richard Smith2d6a5672012-01-14 04:30:29 +0000825 ThisAdjustment.getQuantity());
John McCalld608cdb2010-08-22 10:59:02 +0000826 }
827 } else {
John McCall755d8492011-04-12 00:42:48 +0000828 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
Chris Lattner2acc6e32011-07-18 04:24:23 +0000829 llvm::Type *Ty;
John McCall755d8492011-04-12 00:42:48 +0000830 // Check whether the function has a computable LLVM signature.
Chris Lattnerf742eb02011-07-10 00:18:59 +0000831 if (Types.isFuncTypeConvertible(FPT)) {
John McCall755d8492011-04-12 00:42:48 +0000832 // The function has a computable LLVM signature; use the correct type.
John McCallde5d3c72012-02-17 03:33:10 +0000833 Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
John McCalld608cdb2010-08-22 10:59:02 +0000834 } else {
John McCall755d8492011-04-12 00:42:48 +0000835 // Use an arbitrary non-function type to tell GetAddrOfFunction that the
836 // function type is incomplete.
Reid Kleckner92e44d92013-03-22 16:13:10 +0000837 Ty = CGM.PtrDiffTy;
John McCalld608cdb2010-08-22 10:59:02 +0000838 }
John McCall755d8492011-04-12 00:42:48 +0000839 llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty);
John McCalld608cdb2010-08-22 10:59:02 +0000840
Reid Kleckner92e44d92013-03-22 16:13:10 +0000841 MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, CGM.PtrDiffTy);
Mark Seaborn21fe4502013-07-24 16:25:13 +0000842 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
843 (UseARMMethodPtrABI ? 2 : 1) *
Richard Smith2d6a5672012-01-14 04:30:29 +0000844 ThisAdjustment.getQuantity());
John McCalld608cdb2010-08-22 10:59:02 +0000845 }
John McCall875ab102010-08-22 06:43:33 +0000846
Chris Lattnerc5cbb902011-06-20 04:01:35 +0000847 return llvm::ConstantStruct::getAnon(MemPtr);
John McCall875ab102010-08-22 06:43:33 +0000848}
849
Richard Smith2d6a5672012-01-14 04:30:29 +0000850llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP,
851 QualType MPType) {
852 const MemberPointerType *MPT = MPType->castAs<MemberPointerType>();
853 const ValueDecl *MPD = MP.getMemberPointerDecl();
854 if (!MPD)
855 return EmitNullMemberPointer(MPT);
856
Reid Klecknerf6327302013-05-09 21:01:17 +0000857 CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP);
Richard Smith2d6a5672012-01-14 04:30:29 +0000858
859 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD))
860 return BuildMemberPointer(MD, ThisAdjustment);
861
862 CharUnits FieldOffset =
863 getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD));
864 return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset);
865}
866
John McCalle9fd7eb2010-08-22 08:30:07 +0000867/// The comparison algorithm is pretty easy: the member pointers are
868/// the same if they're either bitwise identical *or* both null.
869///
870/// ARM is different here only because null-ness is more complicated.
871llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000872ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
873 llvm::Value *L,
874 llvm::Value *R,
875 const MemberPointerType *MPT,
876 bool Inequality) {
John McCalle9fd7eb2010-08-22 08:30:07 +0000877 CGBuilderTy &Builder = CGF.Builder;
878
John McCalle9fd7eb2010-08-22 08:30:07 +0000879 llvm::ICmpInst::Predicate Eq;
880 llvm::Instruction::BinaryOps And, Or;
881 if (Inequality) {
882 Eq = llvm::ICmpInst::ICMP_NE;
883 And = llvm::Instruction::Or;
884 Or = llvm::Instruction::And;
885 } else {
886 Eq = llvm::ICmpInst::ICMP_EQ;
887 And = llvm::Instruction::And;
888 Or = llvm::Instruction::Or;
889 }
890
John McCall0bab0cd2010-08-23 01:21:21 +0000891 // Member data pointers are easy because there's a unique null
892 // value, so it just comes down to bitwise equality.
893 if (MPT->isMemberDataPointer())
894 return Builder.CreateICmp(Eq, L, R);
895
896 // For member function pointers, the tautologies are more complex.
897 // The Itanium tautology is:
John McCallde719f72010-08-23 06:56:36 +0000898 // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
John McCall0bab0cd2010-08-23 01:21:21 +0000899 // The ARM tautology is:
John McCallde719f72010-08-23 06:56:36 +0000900 // (L == R) <==> (L.ptr == R.ptr &&
901 // (L.adj == R.adj ||
902 // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
John McCall0bab0cd2010-08-23 01:21:21 +0000903 // The inequality tautologies have exactly the same structure, except
904 // applying De Morgan's laws.
905
906 llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
907 llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
908
John McCalle9fd7eb2010-08-22 08:30:07 +0000909 // This condition tests whether L.ptr == R.ptr. This must always be
910 // true for equality to hold.
911 llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
912
913 // This condition, together with the assumption that L.ptr == R.ptr,
914 // tests whether the pointers are both null. ARM imposes an extra
915 // condition.
916 llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
917 llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
918
919 // This condition tests whether L.adj == R.adj. If this isn't
920 // true, the pointers are unequal unless they're both null.
John McCalld608cdb2010-08-22 10:59:02 +0000921 llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
922 llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
John McCalle9fd7eb2010-08-22 08:30:07 +0000923 llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
924
925 // Null member function pointers on ARM clear the low bit of Adj,
926 // so the zero condition has to check that neither low bit is set.
Mark Seaborn21fe4502013-07-24 16:25:13 +0000927 if (UseARMMethodPtrABI) {
John McCalle9fd7eb2010-08-22 08:30:07 +0000928 llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
929
930 // Compute (l.adj | r.adj) & 1 and test it against zero.
931 llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
932 llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
933 llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
934 "cmp.or.adj");
935 EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
936 }
937
938 // Tie together all our conditions.
939 llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
940 Result = Builder.CreateBinOp(And, PtrEq, Result,
941 Inequality ? "memptr.ne" : "memptr.eq");
942 return Result;
943}
944
945llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000946ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
947 llvm::Value *MemPtr,
948 const MemberPointerType *MPT) {
John McCalle9fd7eb2010-08-22 08:30:07 +0000949 CGBuilderTy &Builder = CGF.Builder;
John McCall0bab0cd2010-08-23 01:21:21 +0000950
951 /// For member data pointers, this is just a check against -1.
952 if (MPT->isMemberDataPointer()) {
Reid Kleckner92e44d92013-03-22 16:13:10 +0000953 assert(MemPtr->getType() == CGM.PtrDiffTy);
John McCall0bab0cd2010-08-23 01:21:21 +0000954 llvm::Value *NegativeOne =
955 llvm::Constant::getAllOnesValue(MemPtr->getType());
956 return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
957 }
John McCalle9fd7eb2010-08-22 08:30:07 +0000958
Daniel Dunbardb27b5f2011-04-19 23:10:47 +0000959 // In Itanium, a member function pointer is not null if 'ptr' is not null.
John McCalld608cdb2010-08-22 10:59:02 +0000960 llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
John McCalle9fd7eb2010-08-22 08:30:07 +0000961
962 llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
963 llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
964
Daniel Dunbardb27b5f2011-04-19 23:10:47 +0000965 // On ARM, a member function pointer is also non-null if the low bit of 'adj'
966 // (the virtual bit) is set.
Mark Seaborn21fe4502013-07-24 16:25:13 +0000967 if (UseARMMethodPtrABI) {
John McCalle9fd7eb2010-08-22 08:30:07 +0000968 llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
John McCalld608cdb2010-08-22 10:59:02 +0000969 llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
John McCalle9fd7eb2010-08-22 08:30:07 +0000970 llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
Daniel Dunbardb27b5f2011-04-19 23:10:47 +0000971 llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero,
972 "memptr.isvirtual");
973 Result = Builder.CreateOr(Result, IsVirtual);
John McCalle9fd7eb2010-08-22 08:30:07 +0000974 }
975
976 return Result;
977}
John McCall875ab102010-08-22 06:43:33 +0000978
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700979bool ItaniumCXXABI::classifyReturnType(CGFunctionInfo &FI) const {
980 const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl();
981 if (!RD)
982 return false;
983
984 // Return indirectly if we have a non-trivial copy ctor or non-trivial dtor.
985 // FIXME: Use canCopyArgument() when it is fixed to handle lazily declared
986 // special members.
987 if (RD->hasNonTrivialDestructor() || RD->hasNonTrivialCopyConstructor()) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800988 auto Align = CGM.getContext().getTypeAlignInChars(FI.getReturnType());
989 FI.getReturnInfo() = ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700990 return true;
991 }
992 return false;
993}
994
John McCallf16aa102010-08-22 21:01:12 +0000995/// The Itanium ABI requires non-zero initialization only for data
996/// member pointers, for which '0' is a valid offset.
997bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700998 return MPT->isMemberFunctionPointer();
John McCallcf2c85e2010-08-22 04:16:24 +0000999}
John McCall4c40d982010-08-31 07:33:07 +00001000
John McCallecd03b42012-09-25 10:10:39 +00001001/// The Itanium ABI always places an offset to the complete object
1002/// at entry -2 in the vtable.
Stephen Hines176edba2014-12-01 14:53:08 -08001003void ItaniumCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF,
1004 const CXXDeleteExpr *DE,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001005 Address Ptr,
Stephen Hines176edba2014-12-01 14:53:08 -08001006 QualType ElementType,
1007 const CXXDestructorDecl *Dtor) {
1008 bool UseGlobalDelete = DE->isGlobalDelete();
1009 if (UseGlobalDelete) {
1010 // Derive the complete-object pointer, which is what we need
1011 // to pass to the deallocation function.
John McCallecd03b42012-09-25 10:10:39 +00001012
Stephen Hines176edba2014-12-01 14:53:08 -08001013 // Grab the vtable pointer as an intptr_t*.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001014 auto *ClassDecl =
1015 cast<CXXRecordDecl>(ElementType->getAs<RecordType>()->getDecl());
1016 llvm::Value *VTable =
1017 CGF.GetVTablePtr(Ptr, CGF.IntPtrTy->getPointerTo(), ClassDecl);
John McCallecd03b42012-09-25 10:10:39 +00001018
Stephen Hines176edba2014-12-01 14:53:08 -08001019 // Track back to entry -2 and pull out the offset there.
1020 llvm::Value *OffsetPtr = CGF.Builder.CreateConstInBoundsGEP1_64(
1021 VTable, -2, "complete-offset.ptr");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001022 llvm::Value *Offset =
1023 CGF.Builder.CreateAlignedLoad(OffsetPtr, CGF.getPointerAlign());
Stephen Hines176edba2014-12-01 14:53:08 -08001024
1025 // Apply the offset.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001026 llvm::Value *CompletePtr =
1027 CGF.Builder.CreateBitCast(Ptr.getPointer(), CGF.Int8PtrTy);
Stephen Hines176edba2014-12-01 14:53:08 -08001028 CompletePtr = CGF.Builder.CreateInBoundsGEP(CompletePtr, Offset);
1029
1030 // If we're supposed to call the global delete, make sure we do so
1031 // even if the destructor throws.
1032 CGF.pushCallObjectDeleteCleanup(DE->getOperatorDelete(), CompletePtr,
1033 ElementType);
1034 }
1035
1036 // FIXME: Provide a source location here even though there's no
1037 // CXXMemberCallExpr for dtor call.
1038 CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting;
1039 EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, /*CE=*/nullptr);
1040
1041 if (UseGlobalDelete)
1042 CGF.PopCleanupBlock();
John McCallecd03b42012-09-25 10:10:39 +00001043}
1044
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001045void ItaniumCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) {
1046 // void __cxa_rethrow();
1047
1048 llvm::FunctionType *FTy =
1049 llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false);
1050
1051 llvm::Constant *Fn = CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow");
1052
1053 if (isNoReturn)
1054 CGF.EmitNoreturnRuntimeCallOrInvoke(Fn, None);
1055 else
1056 CGF.EmitRuntimeCallOrInvoke(Fn);
1057}
1058
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001059static llvm::Constant *getAllocateExceptionFn(CodeGenModule &CGM) {
1060 // void *__cxa_allocate_exception(size_t thrown_size);
1061
1062 llvm::FunctionType *FTy =
1063 llvm::FunctionType::get(CGM.Int8PtrTy, CGM.SizeTy, /*IsVarArgs=*/false);
1064
1065 return CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception");
1066}
1067
1068static llvm::Constant *getThrowFn(CodeGenModule &CGM) {
1069 // void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
1070 // void (*dest) (void *));
1071
1072 llvm::Type *Args[3] = { CGM.Int8PtrTy, CGM.Int8PtrTy, CGM.Int8PtrTy };
1073 llvm::FunctionType *FTy =
1074 llvm::FunctionType::get(CGM.VoidTy, Args, /*IsVarArgs=*/false);
1075
1076 return CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
1077}
1078
1079void ItaniumCXXABI::emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) {
1080 QualType ThrowType = E->getSubExpr()->getType();
1081 // Now allocate the exception object.
1082 llvm::Type *SizeTy = CGF.ConvertType(getContext().getSizeType());
1083 uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity();
1084
1085 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(CGM);
1086 llvm::CallInst *ExceptionPtr = CGF.EmitNounwindRuntimeCall(
1087 AllocExceptionFn, llvm::ConstantInt::get(SizeTy, TypeSize), "exception");
1088
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001089 CharUnits ExnAlign = getAlignmentOfExnObject();
1090 CGF.EmitAnyExprToExn(E->getSubExpr(), Address(ExceptionPtr, ExnAlign));
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001091
1092 // Now throw the exception.
1093 llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType,
1094 /*ForEH=*/true);
1095
1096 // The address of the destructor. If the exception type has a
1097 // trivial destructor (or isn't a record), we just pass null.
1098 llvm::Constant *Dtor = nullptr;
1099 if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) {
1100 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
1101 if (!Record->hasTrivialDestructor()) {
1102 CXXDestructorDecl *DtorD = Record->getDestructor();
1103 Dtor = CGM.getAddrOfCXXStructor(DtorD, StructorType::Complete);
1104 Dtor = llvm::ConstantExpr::getBitCast(Dtor, CGM.Int8PtrTy);
1105 }
1106 }
1107 if (!Dtor) Dtor = llvm::Constant::getNullValue(CGM.Int8PtrTy);
1108
1109 llvm::Value *args[] = { ExceptionPtr, TypeInfo, Dtor };
1110 CGF.EmitNoreturnRuntimeCallOrInvoke(getThrowFn(CGM), args);
1111}
1112
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001113static llvm::Constant *getItaniumDynamicCastFn(CodeGenFunction &CGF) {
1114 // void *__dynamic_cast(const void *sub,
1115 // const abi::__class_type_info *src,
1116 // const abi::__class_type_info *dst,
1117 // std::ptrdiff_t src2dst_offset);
1118
1119 llvm::Type *Int8PtrTy = CGF.Int8PtrTy;
1120 llvm::Type *PtrDiffTy =
1121 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1122
1123 llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy };
1124
1125 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
1126
1127 // Mark the function as nounwind readonly.
1128 llvm::Attribute::AttrKind FuncAttrs[] = { llvm::Attribute::NoUnwind,
1129 llvm::Attribute::ReadOnly };
1130 llvm::AttributeSet Attrs = llvm::AttributeSet::get(
1131 CGF.getLLVMContext(), llvm::AttributeSet::FunctionIndex, FuncAttrs);
1132
1133 return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast", Attrs);
1134}
1135
1136static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) {
1137 // void __cxa_bad_cast();
1138 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
1139 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast");
1140}
1141
1142/// \brief Compute the src2dst_offset hint as described in the
1143/// Itanium C++ ABI [2.9.7]
1144static CharUnits computeOffsetHint(ASTContext &Context,
1145 const CXXRecordDecl *Src,
1146 const CXXRecordDecl *Dst) {
1147 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
1148 /*DetectVirtual=*/false);
1149
1150 // If Dst is not derived from Src we can skip the whole computation below and
1151 // return that Src is not a public base of Dst. Record all inheritance paths.
1152 if (!Dst->isDerivedFrom(Src, Paths))
1153 return CharUnits::fromQuantity(-2ULL);
1154
1155 unsigned NumPublicPaths = 0;
1156 CharUnits Offset;
1157
1158 // Now walk all possible inheritance paths.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001159 for (const CXXBasePath &Path : Paths) {
1160 if (Path.Access != AS_public) // Ignore non-public inheritance.
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001161 continue;
1162
1163 ++NumPublicPaths;
1164
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001165 for (const CXXBasePathElement &PathElement : Path) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001166 // If the path contains a virtual base class we can't give any hint.
1167 // -1: no hint.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001168 if (PathElement.Base->isVirtual())
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001169 return CharUnits::fromQuantity(-1ULL);
1170
1171 if (NumPublicPaths > 1) // Won't use offsets, skip computation.
1172 continue;
1173
1174 // Accumulate the base class offsets.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001175 const ASTRecordLayout &L = Context.getASTRecordLayout(PathElement.Class);
1176 Offset += L.getBaseClassOffset(
1177 PathElement.Base->getType()->getAsCXXRecordDecl());
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001178 }
1179 }
1180
1181 // -2: Src is not a public base of Dst.
1182 if (NumPublicPaths == 0)
1183 return CharUnits::fromQuantity(-2ULL);
1184
1185 // -3: Src is a multiple public base type but never a virtual base type.
1186 if (NumPublicPaths > 1)
1187 return CharUnits::fromQuantity(-3ULL);
1188
1189 // Otherwise, the Src type is a unique public nonvirtual base type of Dst.
1190 // Return the offset of Src from the origin of Dst.
1191 return Offset;
1192}
1193
1194static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) {
1195 // void __cxa_bad_typeid();
1196 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
1197
1198 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid");
1199}
1200
1201bool ItaniumCXXABI::shouldTypeidBeNullChecked(bool IsDeref,
1202 QualType SrcRecordTy) {
1203 return IsDeref;
1204}
1205
1206void ItaniumCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) {
1207 llvm::Value *Fn = getBadTypeidFn(CGF);
1208 CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn();
1209 CGF.Builder.CreateUnreachable();
1210}
1211
1212llvm::Value *ItaniumCXXABI::EmitTypeid(CodeGenFunction &CGF,
1213 QualType SrcRecordTy,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001214 Address ThisPtr,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001215 llvm::Type *StdTypeInfoPtrTy) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001216 auto *ClassDecl =
1217 cast<CXXRecordDecl>(SrcRecordTy->getAs<RecordType>()->getDecl());
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001218 llvm::Value *Value =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001219 CGF.GetVTablePtr(ThisPtr, StdTypeInfoPtrTy->getPointerTo(), ClassDecl);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001220
1221 // Load the type info.
1222 Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001223 return CGF.Builder.CreateAlignedLoad(Value, CGF.getPointerAlign());
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001224}
1225
1226bool ItaniumCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
1227 QualType SrcRecordTy) {
1228 return SrcIsPtr;
1229}
1230
1231llvm::Value *ItaniumCXXABI::EmitDynamicCastCall(
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001232 CodeGenFunction &CGF, Address ThisAddr, QualType SrcRecordTy,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001233 QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) {
1234 llvm::Type *PtrDiffLTy =
1235 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1236 llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1237
1238 llvm::Value *SrcRTTI =
1239 CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType());
1240 llvm::Value *DestRTTI =
1241 CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType());
1242
1243 // Compute the offset hint.
1244 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
1245 const CXXRecordDecl *DestDecl = DestRecordTy->getAsCXXRecordDecl();
1246 llvm::Value *OffsetHint = llvm::ConstantInt::get(
1247 PtrDiffLTy,
1248 computeOffsetHint(CGF.getContext(), SrcDecl, DestDecl).getQuantity());
1249
1250 // Emit the call to __dynamic_cast.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001251 llvm::Value *Value = ThisAddr.getPointer();
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001252 Value = CGF.EmitCastToVoidPtr(Value);
1253
1254 llvm::Value *args[] = {Value, SrcRTTI, DestRTTI, OffsetHint};
1255 Value = CGF.EmitNounwindRuntimeCall(getItaniumDynamicCastFn(CGF), args);
1256 Value = CGF.Builder.CreateBitCast(Value, DestLTy);
1257
1258 /// C++ [expr.dynamic.cast]p9:
1259 /// A failed cast to reference type throws std::bad_cast
1260 if (DestTy->isReferenceType()) {
1261 llvm::BasicBlock *BadCastBlock =
1262 CGF.createBasicBlock("dynamic_cast.bad_cast");
1263
1264 llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value);
1265 CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd);
1266
1267 CGF.EmitBlock(BadCastBlock);
1268 EmitBadCastCall(CGF);
1269 }
1270
1271 return Value;
1272}
1273
1274llvm::Value *ItaniumCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001275 Address ThisAddr,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001276 QualType SrcRecordTy,
1277 QualType DestTy) {
1278 llvm::Type *PtrDiffLTy =
1279 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1280 llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1281
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001282 auto *ClassDecl =
1283 cast<CXXRecordDecl>(SrcRecordTy->getAs<RecordType>()->getDecl());
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001284 // Get the vtable pointer.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001285 llvm::Value *VTable = CGF.GetVTablePtr(ThisAddr, PtrDiffLTy->getPointerTo(),
1286 ClassDecl);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001287
1288 // Get the offset-to-top from the vtable.
1289 llvm::Value *OffsetToTop =
1290 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001291 OffsetToTop =
1292 CGF.Builder.CreateAlignedLoad(OffsetToTop, CGF.getPointerAlign(),
1293 "offset.to.top");
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001294
1295 // Finally, add the offset to the pointer.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001296 llvm::Value *Value = ThisAddr.getPointer();
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001297 Value = CGF.EmitCastToVoidPtr(Value);
1298 Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop);
1299
1300 return CGF.Builder.CreateBitCast(Value, DestLTy);
1301}
1302
1303bool ItaniumCXXABI::EmitBadCastCall(CodeGenFunction &CGF) {
1304 llvm::Value *Fn = getBadCastFn(CGF);
1305 CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn();
1306 CGF.Builder.CreateUnreachable();
1307 return true;
1308}
1309
Reid Klecknerb0f533e2013-05-29 18:02:47 +00001310llvm::Value *
1311ItaniumCXXABI::GetVirtualBaseClassOffset(CodeGenFunction &CGF,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001312 Address This,
Reid Klecknerb0f533e2013-05-29 18:02:47 +00001313 const CXXRecordDecl *ClassDecl,
1314 const CXXRecordDecl *BaseClassDecl) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001315 llvm::Value *VTablePtr = CGF.GetVTablePtr(This, CGM.Int8PtrTy, ClassDecl);
Reid Klecknerb0f533e2013-05-29 18:02:47 +00001316 CharUnits VBaseOffsetOffset =
Timur Iskhodzhanov5f0db582013-11-05 15:54:58 +00001317 CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(ClassDecl,
1318 BaseClassDecl);
Reid Klecknerb0f533e2013-05-29 18:02:47 +00001319
1320 llvm::Value *VBaseOffsetPtr =
1321 CGF.Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(),
1322 "vbase.offset.ptr");
1323 VBaseOffsetPtr = CGF.Builder.CreateBitCast(VBaseOffsetPtr,
1324 CGM.PtrDiffTy->getPointerTo());
1325
1326 llvm::Value *VBaseOffset =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001327 CGF.Builder.CreateAlignedLoad(VBaseOffsetPtr, CGF.getPointerAlign(),
1328 "vbase.offset");
Reid Klecknerb0f533e2013-05-29 18:02:47 +00001329
1330 return VBaseOffset;
1331}
1332
Timur Iskhodzhanovbb1b7972013-08-04 17:30:04 +00001333void ItaniumCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) {
1334 // Just make sure we're in sync with TargetCXXABI.
1335 assert(CGM.getTarget().getCXXABI().hasConstructorVariants());
1336
Stephen Hines651f13c2014-04-23 16:59:28 -07001337 // The constructor used for constructing this as a base class;
1338 // ignores virtual bases.
1339 CGM.EmitGlobal(GlobalDecl(D, Ctor_Base));
1340
Timur Iskhodzhanovbb1b7972013-08-04 17:30:04 +00001341 // The constructor used for constructing this as a complete class;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001342 // constructs the virtual bases, then calls the base constructor.
Timur Iskhodzhanovbb1b7972013-08-04 17:30:04 +00001343 if (!D->getParent()->isAbstract()) {
1344 // We don't need to emit the complete ctor if the class is abstract.
1345 CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete));
1346 }
Timur Iskhodzhanovbb1b7972013-08-04 17:30:04 +00001347}
1348
Stephen Hines176edba2014-12-01 14:53:08 -08001349void
1350ItaniumCXXABI::buildStructorSignature(const CXXMethodDecl *MD, StructorType T,
1351 SmallVectorImpl<CanQualType> &ArgTys) {
John McCall9cb2cee2010-09-02 10:25:57 +00001352 ASTContext &Context = getContext();
John McCall4c40d982010-08-31 07:33:07 +00001353
Stephen Hines176edba2014-12-01 14:53:08 -08001354 // All parameters are already in place except VTT, which goes after 'this'.
1355 // These are Clang types, so we don't need to worry about sret yet.
John McCall4c40d982010-08-31 07:33:07 +00001356
1357 // Check if we need to add a VTT parameter (which has type void **).
Stephen Hines176edba2014-12-01 14:53:08 -08001358 if (T == StructorType::Base && MD->getParent()->getNumVBases() != 0)
1359 ArgTys.insert(ArgTys.begin() + 1,
1360 Context.getPointerType(Context.VoidPtrTy));
John McCall4c40d982010-08-31 07:33:07 +00001361}
1362
Reid Klecknera4130ba2013-07-22 13:51:44 +00001363void ItaniumCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001364 // The destructor used for destructing this as a base class; ignores
1365 // virtual bases.
1366 CGM.EmitGlobal(GlobalDecl(D, Dtor_Base));
Reid Klecknera4130ba2013-07-22 13:51:44 +00001367
1368 // The destructor used for destructing this as a most-derived class;
1369 // call the base destructor and then destructs any virtual bases.
1370 CGM.EmitGlobal(GlobalDecl(D, Dtor_Complete));
1371
Stephen Hines651f13c2014-04-23 16:59:28 -07001372 // The destructor in a virtual table is always a 'deleting'
1373 // destructor, which calls the complete destructor and then uses the
1374 // appropriate operator delete.
1375 if (D->isVirtual())
1376 CGM.EmitGlobal(GlobalDecl(D, Dtor_Deleting));
Reid Klecknera4130ba2013-07-22 13:51:44 +00001377}
1378
Stephen Hines651f13c2014-04-23 16:59:28 -07001379void ItaniumCXXABI::addImplicitStructorParams(CodeGenFunction &CGF,
1380 QualType &ResTy,
1381 FunctionArgList &Params) {
John McCall4c40d982010-08-31 07:33:07 +00001382 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
Stephen Hines651f13c2014-04-23 16:59:28 -07001383 assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD));
John McCall4c40d982010-08-31 07:33:07 +00001384
1385 // Check if we need a VTT parameter as well.
Peter Collingbournee1e35f72013-06-28 20:45:28 +00001386 if (NeedsVTTParameter(CGF.CurGD)) {
John McCall9cb2cee2010-09-02 10:25:57 +00001387 ASTContext &Context = getContext();
John McCall4c40d982010-08-31 07:33:07 +00001388
1389 // FIXME: avoid the fake decl
1390 QualType T = Context.getPointerType(Context.VoidPtrTy);
1391 ImplicitParamDecl *VTTDecl
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001392 = ImplicitParamDecl::Create(Context, nullptr, MD->getLocation(),
John McCall4c40d982010-08-31 07:33:07 +00001393 &Context.Idents.get("vtt"), T);
Stephen Hines651f13c2014-04-23 16:59:28 -07001394 Params.insert(Params.begin() + 1, VTTDecl);
1395 getStructorImplicitParamDecl(CGF) = VTTDecl;
John McCall4c40d982010-08-31 07:33:07 +00001396 }
1397}
1398
John McCall4c40d982010-08-31 07:33:07 +00001399void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
1400 /// Initialize the 'this' slot.
1401 EmitThisParam(CGF);
1402
1403 /// Initialize the 'vtt' slot if needed.
Stephen Hines651f13c2014-04-23 16:59:28 -07001404 if (getStructorImplicitParamDecl(CGF)) {
1405 getStructorImplicitParamValue(CGF) = CGF.Builder.CreateLoad(
1406 CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), "vtt");
John McCall4c40d982010-08-31 07:33:07 +00001407 }
John McCall4c40d982010-08-31 07:33:07 +00001408
Stephen Lin3b50e8d2013-06-30 20:40:16 +00001409 /// If this is a function that the ABI specifies returns 'this', initialize
1410 /// the return slot to 'this' at the start of the function.
1411 ///
1412 /// Unlike the setting of return types, this is done within the ABI
1413 /// implementation instead of by clients of CGCXXABI because:
1414 /// 1) getThisValue is currently protected
1415 /// 2) in theory, an ABI could implement 'this' returns some other way;
1416 /// HasThisReturn only specifies a contract, not the implementation
John McCall4c40d982010-08-31 07:33:07 +00001417 if (HasThisReturn(CGF.CurGD))
Eli Friedmancec5ebd2012-02-11 02:57:39 +00001418 CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
John McCall4c40d982010-08-31 07:33:07 +00001419}
1420
Stephen Hines651f13c2014-04-23 16:59:28 -07001421unsigned ItaniumCXXABI::addImplicitConstructorArgs(
1422 CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type,
1423 bool ForVirtualBase, bool Delegating, CallArgList &Args) {
1424 if (!NeedsVTTParameter(GlobalDecl(D, Type)))
1425 return 0;
1426
1427 // Insert the implicit 'vtt' argument as the second argument.
1428 llvm::Value *VTT =
1429 CGF.GetVTTParameter(GlobalDecl(D, Type), ForVirtualBase, Delegating);
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +00001430 QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07001431 Args.insert(Args.begin() + 1,
1432 CallArg(RValue::get(VTT), VTTTy, /*needscopy=*/false));
1433 return 1; // Added one arg.
1434}
1435
1436void ItaniumCXXABI::EmitDestructorCall(CodeGenFunction &CGF,
1437 const CXXDestructorDecl *DD,
1438 CXXDtorType Type, bool ForVirtualBase,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001439 bool Delegating, Address This) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001440 GlobalDecl GD(DD, Type);
1441 llvm::Value *VTT = CGF.GetVTTParameter(GD, ForVirtualBase, Delegating);
1442 QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy);
1443
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001444 llvm::Value *Callee = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001445 if (getContext().getLangOpts().AppleKext)
1446 Callee = CGF.BuildAppleKextVirtualDestructorCall(DD, Type, DD->getParent());
1447
1448 if (!Callee)
Stephen Hines176edba2014-12-01 14:53:08 -08001449 Callee = CGM.getAddrOfCXXStructor(DD, getFromDtorType(Type));
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +00001450
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001451 CGF.EmitCXXMemberOrOperatorCall(DD, Callee, ReturnValueSlot(),
1452 This.getPointer(), VTT, VTTTy, nullptr);
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +00001453}
1454
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001455void ItaniumCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT,
1456 const CXXRecordDecl *RD) {
1457 llvm::GlobalVariable *VTable = getAddrOfVTable(RD, CharUnits());
1458 if (VTable->hasInitializer())
1459 return;
1460
Timur Iskhodzhanov5f0db582013-11-05 15:54:58 +00001461 ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext();
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001462 const VTableLayout &VTLayout = VTContext.getVTableLayout(RD);
1463 llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001464 llvm::Constant *RTTI =
1465 CGM.GetAddrOfRTTIDescriptor(CGM.getContext().getTagDeclType(RD));
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001466
1467 // Create and set the initializer.
1468 llvm::Constant *Init = CGVT.CreateVTableInitializer(
1469 RD, VTLayout.vtable_component_begin(), VTLayout.getNumVTableComponents(),
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001470 VTLayout.vtable_thunk_begin(), VTLayout.getNumVTableThunks(), RTTI);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001471 VTable->setInitializer(Init);
1472
1473 // Set the correct linkage.
1474 VTable->setLinkage(Linkage);
1475
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001476 if (CGM.supportsCOMDAT() && VTable->isWeakForLinker())
1477 VTable->setComdat(CGM.getModule().getOrInsertComdat(VTable->getName()));
1478
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001479 // Set the right visibility.
Stephen Hines651f13c2014-04-23 16:59:28 -07001480 CGM.setGlobalVisibility(VTable, RD);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001481
Stephen Hines176edba2014-12-01 14:53:08 -08001482 // Use pointer alignment for the vtable. Otherwise we would align them based
1483 // on the size of the initializer which doesn't make sense as only single
1484 // values are read.
1485 unsigned PAlign = CGM.getTarget().getPointerAlign(0);
1486 VTable->setAlignment(getContext().toCharUnitsFromBits(PAlign).getQuantity());
1487
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001488 // If this is the magic class __cxxabiv1::__fundamental_type_info,
1489 // we will emit the typeinfo for the fundamental types. This is the
1490 // same behaviour as GCC.
1491 const DeclContext *DC = RD->getDeclContext();
1492 if (RD->getIdentifier() &&
1493 RD->getIdentifier()->isStr("__fundamental_type_info") &&
1494 isa<NamespaceDecl>(DC) && cast<NamespaceDecl>(DC)->getIdentifier() &&
1495 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") &&
1496 DC->getParent()->isTranslationUnit())
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001497 EmitFundamentalRTTIDescriptors();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001498
1499 CGM.EmitVTableBitSetEntries(VTable, VTLayout);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001500}
1501
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001502bool ItaniumCXXABI::isVirtualOffsetNeededForVTableField(
1503 CodeGenFunction &CGF, CodeGenFunction::VPtr Vptr) {
1504 if (Vptr.NearestVBase == nullptr)
1505 return false;
1506 return NeedsVTTParameter(CGF.CurGD);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001507}
1508
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001509llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructor(
1510 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base,
1511 const CXXRecordDecl *NearestVBase) {
1512
1513 if ((Base.getBase()->getNumVBases() || NearestVBase != nullptr) &&
1514 NeedsVTTParameter(CGF.CurGD)) {
1515 return getVTableAddressPointInStructorWithVTT(CGF, VTableClass, Base,
1516 NearestVBase);
1517 }
1518 return getVTableAddressPoint(Base, VTableClass);
1519}
1520
1521llvm::Constant *
1522ItaniumCXXABI::getVTableAddressPoint(BaseSubobject Base,
1523 const CXXRecordDecl *VTableClass) {
1524 llvm::GlobalValue *VTable = getAddrOfVTable(VTableClass, CharUnits());
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001525
1526 // Find the appropriate vtable within the vtable group.
Timur Iskhodzhanov5f0db582013-11-05 15:54:58 +00001527 uint64_t AddressPoint = CGM.getItaniumVTableContext()
1528 .getVTableLayout(VTableClass)
1529 .getAddressPoint(Base);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001530 llvm::Value *Indices[] = {
1531 llvm::ConstantInt::get(CGM.Int64Ty, 0),
1532 llvm::ConstantInt::get(CGM.Int64Ty, AddressPoint)
1533 };
1534
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07001535 return llvm::ConstantExpr::getInBoundsGetElementPtr(VTable->getValueType(),
1536 VTable, Indices);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001537}
1538
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001539llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructorWithVTT(
1540 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base,
1541 const CXXRecordDecl *NearestVBase) {
1542 assert((Base.getBase()->getNumVBases() || NearestVBase != nullptr) &&
1543 NeedsVTTParameter(CGF.CurGD) && "This class doesn't have VTT");
1544
1545 // Get the secondary vpointer index.
1546 uint64_t VirtualPointerIndex =
1547 CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base);
1548
1549 /// Load the VTT.
1550 llvm::Value *VTT = CGF.LoadCXXVTT();
1551 if (VirtualPointerIndex)
1552 VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex);
1553
1554 // And load the address point from the VTT.
1555 return CGF.Builder.CreateAlignedLoad(VTT, CGF.getPointerAlign());
1556}
1557
1558llvm::Constant *ItaniumCXXABI::getVTableAddressPointForConstExpr(
1559 BaseSubobject Base, const CXXRecordDecl *VTableClass) {
1560 return getVTableAddressPoint(Base, VTableClass);
1561}
1562
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001563llvm::GlobalVariable *ItaniumCXXABI::getAddrOfVTable(const CXXRecordDecl *RD,
1564 CharUnits VPtrOffset) {
1565 assert(VPtrOffset.isZero() && "Itanium ABI only supports zero vptr offsets");
1566
1567 llvm::GlobalVariable *&VTable = VTables[RD];
1568 if (VTable)
1569 return VTable;
1570
1571 // Queue up this v-table for possible deferred emission.
1572 CGM.addDeferredVTable(RD);
1573
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001574 SmallString<256> Name;
1575 llvm::raw_svector_ostream Out(Name);
Timur Iskhodzhanov11f22a32013-10-03 06:26:13 +00001576 getMangleContext().mangleCXXVTable(RD, Out);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001577
Timur Iskhodzhanov5f0db582013-11-05 15:54:58 +00001578 ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext();
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001579 llvm::ArrayType *ArrayType = llvm::ArrayType::get(
1580 CGM.Int8PtrTy, VTContext.getVTableLayout(RD).getNumVTableComponents());
1581
1582 VTable = CGM.CreateOrReplaceCXXRuntimeVariable(
1583 Name, ArrayType, llvm::GlobalValue::ExternalLinkage);
1584 VTable->setUnnamedAddr(true);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001585
1586 if (RD->hasAttr<DLLImportAttr>())
1587 VTable->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
1588 else if (RD->hasAttr<DLLExportAttr>())
1589 VTable->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
1590
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001591 return VTable;
1592}
1593
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00001594llvm::Value *ItaniumCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF,
1595 GlobalDecl GD,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001596 Address This,
1597 llvm::Type *Ty,
1598 SourceLocation Loc) {
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00001599 GD = GD.getCanonicalDecl();
1600 Ty = Ty->getPointerTo()->getPointerTo();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001601 auto *MethodDecl = cast<CXXMethodDecl>(GD.getDecl());
1602 llvm::Value *VTable = CGF.GetVTablePtr(This, Ty, MethodDecl->getParent());
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00001603
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07001604 if (CGF.SanOpts.has(SanitizerKind::CFIVCall))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001605 CGF.EmitVTablePtrCheckForCall(MethodDecl, VTable,
1606 CodeGenFunction::CFITCK_VCall, Loc);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001607
Timur Iskhodzhanov5f0db582013-11-05 15:54:58 +00001608 uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD);
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00001609 llvm::Value *VFuncPtr =
1610 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001611 return CGF.Builder.CreateAlignedLoad(VFuncPtr, CGF.getPointerAlign());
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00001612}
1613
Stephen Hines176edba2014-12-01 14:53:08 -08001614llvm::Value *ItaniumCXXABI::EmitVirtualDestructorCall(
1615 CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001616 Address This, const CXXMemberCallExpr *CE) {
Stephen Hines176edba2014-12-01 14:53:08 -08001617 assert(CE == nullptr || CE->arg_begin() == CE->arg_end());
Timur Iskhodzhanov0f9827f2013-02-15 14:45:22 +00001618 assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete);
1619
Stephen Hines176edba2014-12-01 14:53:08 -08001620 const CGFunctionInfo *FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration(
1621 Dtor, getFromDtorType(DtorType));
Timur Iskhodzhanov0f9827f2013-02-15 14:45:22 +00001622 llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo);
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +00001623 llvm::Value *Callee =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001624 getVirtualFunctionPointer(CGF, GlobalDecl(Dtor, DtorType), This, Ty,
1625 CE ? CE->getLocStart() : SourceLocation());
Timur Iskhodzhanov0f9827f2013-02-15 14:45:22 +00001626
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001627 CGF.EmitCXXMemberOrOperatorCall(Dtor, Callee, ReturnValueSlot(),
1628 This.getPointer(), /*ImplicitParam=*/nullptr,
1629 QualType(), CE);
Stephen Hines176edba2014-12-01 14:53:08 -08001630 return nullptr;
Timur Iskhodzhanov0f9827f2013-02-15 14:45:22 +00001631}
1632
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001633void ItaniumCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) {
Reid Kleckner90633022013-06-19 15:20:38 +00001634 CodeGenVTables &VTables = CGM.getVTables();
1635 llvm::GlobalVariable *VTT = VTables.GetAddrOfVTT(RD);
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +00001636 VTables.EmitVTTDefinition(VTT, CGM.getVTableLinkage(RD), RD);
Reid Kleckner90633022013-06-19 15:20:38 +00001637}
1638
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001639bool ItaniumCXXABI::canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const {
1640 // We don't emit available_externally vtables if we are in -fapple-kext mode
1641 // because kext mode does not permit devirtualization.
1642 if (CGM.getLangOpts().AppleKext)
1643 return false;
1644
1645 // If we don't have any inline virtual functions, and if vtable is not hidden,
1646 // then we are safe to emit available_externally copy of vtable.
1647 // FIXME we can still emit a copy of the vtable if we
1648 // can emit definition of the inline functions.
1649 return !hasAnyUsedVirtualInlineFunction(RD) && !isVTableHidden(RD);
1650}
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +00001651static llvm::Value *performTypeAdjustment(CodeGenFunction &CGF,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001652 Address InitialPtr,
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +00001653 int64_t NonVirtualAdjustment,
1654 int64_t VirtualAdjustment,
1655 bool IsReturnAdjustment) {
1656 if (!NonVirtualAdjustment && !VirtualAdjustment)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001657 return InitialPtr.getPointer();
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +00001658
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001659 Address V = CGF.Builder.CreateElementBitCast(InitialPtr, CGF.Int8Ty);
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +00001660
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001661 // In a base-to-derived cast, the non-virtual adjustment is applied first.
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +00001662 if (NonVirtualAdjustment && !IsReturnAdjustment) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001663 V = CGF.Builder.CreateConstInBoundsByteGEP(V,
1664 CharUnits::fromQuantity(NonVirtualAdjustment));
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +00001665 }
1666
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001667 // Perform the virtual adjustment if we have one.
1668 llvm::Value *ResultPtr;
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +00001669 if (VirtualAdjustment) {
1670 llvm::Type *PtrDiffTy =
1671 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1672
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001673 Address VTablePtrPtr = CGF.Builder.CreateElementBitCast(V, CGF.Int8PtrTy);
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +00001674 llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr);
1675
1676 llvm::Value *OffsetPtr =
1677 CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
1678
1679 OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo());
1680
1681 // Load the adjustment offset from the vtable.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001682 llvm::Value *Offset =
1683 CGF.Builder.CreateAlignedLoad(OffsetPtr, CGF.getPointerAlign());
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +00001684
1685 // Adjust our pointer.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001686 ResultPtr = CGF.Builder.CreateInBoundsGEP(V.getPointer(), Offset);
1687 } else {
1688 ResultPtr = V.getPointer();
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +00001689 }
1690
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001691 // In a derived-to-base conversion, the non-virtual adjustment is
1692 // applied second.
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +00001693 if (NonVirtualAdjustment && IsReturnAdjustment) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001694 ResultPtr = CGF.Builder.CreateConstInBoundsGEP1_64(ResultPtr,
1695 NonVirtualAdjustment);
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +00001696 }
1697
1698 // Cast back to the original type.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001699 return CGF.Builder.CreateBitCast(ResultPtr, InitialPtr.getType());
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +00001700}
1701
1702llvm::Value *ItaniumCXXABI::performThisAdjustment(CodeGenFunction &CGF,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001703 Address This,
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +00001704 const ThisAdjustment &TA) {
Timur Iskhodzhanov58b6db72013-11-06 06:24:31 +00001705 return performTypeAdjustment(CGF, This, TA.NonVirtual,
1706 TA.Virtual.Itanium.VCallOffsetOffset,
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +00001707 /*IsReturnAdjustment=*/false);
1708}
1709
1710llvm::Value *
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001711ItaniumCXXABI::performReturnAdjustment(CodeGenFunction &CGF, Address Ret,
Timur Iskhodzhanovc70cc5d2013-10-30 11:55:43 +00001712 const ReturnAdjustment &RA) {
1713 return performTypeAdjustment(CGF, Ret, RA.NonVirtual,
1714 RA.Virtual.Itanium.VBaseOffsetOffset,
1715 /*IsReturnAdjustment=*/true);
1716}
1717
John McCall4c40d982010-08-31 07:33:07 +00001718void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
1719 RValue RV, QualType ResultType) {
1720 if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
1721 return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
1722
1723 // Destructor thunks in the ARM ABI have indeterminate results.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001724 llvm::Type *T = CGF.ReturnValue.getElementType();
John McCall4c40d982010-08-31 07:33:07 +00001725 RValue Undef = RValue::get(llvm::UndefValue::get(T));
1726 return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
1727}
John McCall1e7fe752010-09-02 09:58:18 +00001728
1729/************************** Array allocation cookies **************************/
1730
John McCalle2b45e22012-05-01 05:23:51 +00001731CharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) {
1732 // The array cookie is a size_t; pad that up to the element alignment.
1733 // The cookie is actually right-justified in that space.
1734 return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes),
1735 CGM.getContext().getTypeAlignInChars(elementType));
John McCall1e7fe752010-09-02 09:58:18 +00001736}
1737
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001738Address ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
1739 Address NewPtr,
1740 llvm::Value *NumElements,
1741 const CXXNewExpr *expr,
1742 QualType ElementType) {
John McCalle2b45e22012-05-01 05:23:51 +00001743 assert(requiresArrayCookie(expr));
John McCall1e7fe752010-09-02 09:58:18 +00001744
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001745 unsigned AS = NewPtr.getAddressSpace();
John McCall1e7fe752010-09-02 09:58:18 +00001746
John McCall9cb2cee2010-09-02 10:25:57 +00001747 ASTContext &Ctx = getContext();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001748 CharUnits SizeSize = CGF.getSizeSize();
John McCall1e7fe752010-09-02 09:58:18 +00001749
1750 // The size of the cookie.
1751 CharUnits CookieSize =
1752 std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
John McCalle2b45e22012-05-01 05:23:51 +00001753 assert(CookieSize == getArrayCookieSizeImpl(ElementType));
John McCall1e7fe752010-09-02 09:58:18 +00001754
1755 // Compute an offset to the cookie.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001756 Address CookiePtr = NewPtr;
John McCall1e7fe752010-09-02 09:58:18 +00001757 CharUnits CookieOffset = CookieSize - SizeSize;
1758 if (!CookieOffset.isZero())
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001759 CookiePtr = CGF.Builder.CreateConstInBoundsByteGEP(CookiePtr, CookieOffset);
John McCall1e7fe752010-09-02 09:58:18 +00001760
1761 // Write the number of elements into the appropriate slot.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001762 Address NumElementsPtr =
1763 CGF.Builder.CreateElementBitCast(CookiePtr, CGF.SizeTy);
Stephen Hines176edba2014-12-01 14:53:08 -08001764 llvm::Instruction *SI = CGF.Builder.CreateStore(NumElements, NumElementsPtr);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001765
1766 // Handle the array cookie specially in ASan.
Stephen Hines176edba2014-12-01 14:53:08 -08001767 if (CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) && AS == 0 &&
1768 expr->getOperatorNew()->isReplaceableGlobalAllocationFunction()) {
1769 // The store to the CookiePtr does not need to be instrumented.
1770 CGM.getSanitizerMetadata()->disableSanitizerForInstruction(SI);
1771 llvm::FunctionType *FTy =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001772 llvm::FunctionType::get(CGM.VoidTy, NumElementsPtr.getType(), false);
Stephen Hines176edba2014-12-01 14:53:08 -08001773 llvm::Constant *F =
1774 CGM.CreateRuntimeFunction(FTy, "__asan_poison_cxx_array_cookie");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001775 CGF.Builder.CreateCall(F, NumElementsPtr.getPointer());
Stephen Hines176edba2014-12-01 14:53:08 -08001776 }
John McCall1e7fe752010-09-02 09:58:18 +00001777
1778 // Finally, compute a pointer to the actual data buffer by skipping
1779 // over the cookie completely.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001780 return CGF.Builder.CreateConstInBoundsByteGEP(NewPtr, CookieSize);
John McCall1e7fe752010-09-02 09:58:18 +00001781}
1782
John McCalle2b45e22012-05-01 05:23:51 +00001783llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001784 Address allocPtr,
John McCalle2b45e22012-05-01 05:23:51 +00001785 CharUnits cookieSize) {
1786 // The element size is right-justified in the cookie.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001787 Address numElementsPtr = allocPtr;
1788 CharUnits numElementsOffset = cookieSize - CGF.getSizeSize();
John McCalle2b45e22012-05-01 05:23:51 +00001789 if (!numElementsOffset.isZero())
1790 numElementsPtr =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001791 CGF.Builder.CreateConstInBoundsByteGEP(numElementsPtr, numElementsOffset);
John McCall1e7fe752010-09-02 09:58:18 +00001792
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001793 unsigned AS = allocPtr.getAddressSpace();
1794 numElementsPtr = CGF.Builder.CreateElementBitCast(numElementsPtr, CGF.SizeTy);
Stephen Hines176edba2014-12-01 14:53:08 -08001795 if (!CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) || AS != 0)
1796 return CGF.Builder.CreateLoad(numElementsPtr);
1797 // In asan mode emit a function call instead of a regular load and let the
1798 // run-time deal with it: if the shadow is properly poisoned return the
1799 // cookie, otherwise return 0 to avoid an infinite loop calling DTORs.
1800 // We can't simply ignore this load using nosanitize metadata because
1801 // the metadata may be lost.
1802 llvm::FunctionType *FTy =
1803 llvm::FunctionType::get(CGF.SizeTy, CGF.SizeTy->getPointerTo(0), false);
1804 llvm::Constant *F =
1805 CGM.CreateRuntimeFunction(FTy, "__asan_load_cxx_array_cookie");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001806 return CGF.Builder.CreateCall(F, numElementsPtr.getPointer());
John McCall1e7fe752010-09-02 09:58:18 +00001807}
1808
John McCalle2b45e22012-05-01 05:23:51 +00001809CharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) {
John McCallf3bbb152013-01-25 23:36:19 +00001810 // ARM says that the cookie is always:
John McCall1e7fe752010-09-02 09:58:18 +00001811 // struct array_cookie {
1812 // std::size_t element_size; // element_size != 0
1813 // std::size_t element_count;
1814 // };
John McCallf3bbb152013-01-25 23:36:19 +00001815 // But the base ABI doesn't give anything an alignment greater than
1816 // 8, so we can dismiss this as typical ABI-author blindness to
1817 // actual language complexity and round up to the element alignment.
1818 return std::max(CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes),
1819 CGM.getContext().getTypeAlignInChars(elementType));
John McCall1e7fe752010-09-02 09:58:18 +00001820}
1821
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001822Address ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
1823 Address newPtr,
1824 llvm::Value *numElements,
1825 const CXXNewExpr *expr,
1826 QualType elementType) {
John McCalle2b45e22012-05-01 05:23:51 +00001827 assert(requiresArrayCookie(expr));
John McCall1e7fe752010-09-02 09:58:18 +00001828
John McCall1e7fe752010-09-02 09:58:18 +00001829 // The cookie is always at the start of the buffer.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001830 Address cookie = newPtr;
John McCall1e7fe752010-09-02 09:58:18 +00001831
1832 // The first element is the element size.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001833 cookie = CGF.Builder.CreateElementBitCast(cookie, CGF.SizeTy);
John McCallf3bbb152013-01-25 23:36:19 +00001834 llvm::Value *elementSize = llvm::ConstantInt::get(CGF.SizeTy,
1835 getContext().getTypeSizeInChars(elementType).getQuantity());
1836 CGF.Builder.CreateStore(elementSize, cookie);
John McCall1e7fe752010-09-02 09:58:18 +00001837
1838 // The second element is the element count.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001839 cookie = CGF.Builder.CreateConstInBoundsGEP(cookie, 1, CGF.getSizeSize());
John McCallf3bbb152013-01-25 23:36:19 +00001840 CGF.Builder.CreateStore(numElements, cookie);
John McCall1e7fe752010-09-02 09:58:18 +00001841
1842 // Finally, compute a pointer to the actual data buffer by skipping
1843 // over the cookie completely.
John McCallf3bbb152013-01-25 23:36:19 +00001844 CharUnits cookieSize = ARMCXXABI::getArrayCookieSizeImpl(elementType);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001845 return CGF.Builder.CreateConstInBoundsByteGEP(newPtr, cookieSize);
John McCall1e7fe752010-09-02 09:58:18 +00001846}
1847
John McCalle2b45e22012-05-01 05:23:51 +00001848llvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001849 Address allocPtr,
John McCalle2b45e22012-05-01 05:23:51 +00001850 CharUnits cookieSize) {
1851 // The number of elements is at offset sizeof(size_t) relative to
1852 // the allocated pointer.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001853 Address numElementsPtr
1854 = CGF.Builder.CreateConstInBoundsByteGEP(allocPtr, CGF.getSizeSize());
John McCall1e7fe752010-09-02 09:58:18 +00001855
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001856 numElementsPtr = CGF.Builder.CreateElementBitCast(numElementsPtr, CGF.SizeTy);
John McCalle2b45e22012-05-01 05:23:51 +00001857 return CGF.Builder.CreateLoad(numElementsPtr);
John McCall1e7fe752010-09-02 09:58:18 +00001858}
1859
John McCall5cd91b52010-09-08 01:44:27 +00001860/*********************** Static local initialization **************************/
1861
1862static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001863 llvm::PointerType *GuardPtrTy) {
John McCall5cd91b52010-09-08 01:44:27 +00001864 // int __cxa_guard_acquire(__guard *guard_object);
Chris Lattner2acc6e32011-07-18 04:24:23 +00001865 llvm::FunctionType *FTy =
John McCall5cd91b52010-09-08 01:44:27 +00001866 llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy),
Jay Foadda549e82011-07-29 13:56:53 +00001867 GuardPtrTy, /*isVarArg=*/false);
Nick Lewyckye76872e2012-02-13 23:45:02 +00001868 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire",
Bill Wendlingc4c62fd2013-01-31 00:30:05 +00001869 llvm::AttributeSet::get(CGM.getLLVMContext(),
1870 llvm::AttributeSet::FunctionIndex,
Bill Wendling72390b32012-12-20 19:27:06 +00001871 llvm::Attribute::NoUnwind));
John McCall5cd91b52010-09-08 01:44:27 +00001872}
1873
1874static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001875 llvm::PointerType *GuardPtrTy) {
John McCall5cd91b52010-09-08 01:44:27 +00001876 // void __cxa_guard_release(__guard *guard_object);
Chris Lattner2acc6e32011-07-18 04:24:23 +00001877 llvm::FunctionType *FTy =
Chris Lattner8b418682012-02-07 00:39:47 +00001878 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
Nick Lewyckye76872e2012-02-13 23:45:02 +00001879 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release",
Bill Wendlingc4c62fd2013-01-31 00:30:05 +00001880 llvm::AttributeSet::get(CGM.getLLVMContext(),
1881 llvm::AttributeSet::FunctionIndex,
Bill Wendling72390b32012-12-20 19:27:06 +00001882 llvm::Attribute::NoUnwind));
John McCall5cd91b52010-09-08 01:44:27 +00001883}
1884
1885static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001886 llvm::PointerType *GuardPtrTy) {
John McCall5cd91b52010-09-08 01:44:27 +00001887 // void __cxa_guard_abort(__guard *guard_object);
Chris Lattner2acc6e32011-07-18 04:24:23 +00001888 llvm::FunctionType *FTy =
Chris Lattner8b418682012-02-07 00:39:47 +00001889 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
Nick Lewyckye76872e2012-02-13 23:45:02 +00001890 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort",
Bill Wendlingc4c62fd2013-01-31 00:30:05 +00001891 llvm::AttributeSet::get(CGM.getLLVMContext(),
1892 llvm::AttributeSet::FunctionIndex,
Bill Wendling72390b32012-12-20 19:27:06 +00001893 llvm::Attribute::NoUnwind));
John McCall5cd91b52010-09-08 01:44:27 +00001894}
1895
1896namespace {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001897 struct CallGuardAbort final : EHScopeStack::Cleanup {
John McCall5cd91b52010-09-08 01:44:27 +00001898 llvm::GlobalVariable *Guard;
Chandler Carruth0f30a122012-03-30 19:44:53 +00001899 CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
John McCall5cd91b52010-09-08 01:44:27 +00001900
Stephen Hines651f13c2014-04-23 16:59:28 -07001901 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCallbd7370a2013-02-28 19:01:20 +00001902 CGF.EmitNounwindRuntimeCall(getGuardAbortFn(CGF.CGM, Guard->getType()),
1903 Guard);
John McCall5cd91b52010-09-08 01:44:27 +00001904 }
1905 };
1906}
1907
1908/// The ARM code here follows the Itanium code closely enough that we
1909/// just special-case it at particular places.
John McCall3030eb82010-11-06 09:44:32 +00001910void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
1911 const VarDecl &D,
John McCall355bba72012-03-30 21:00:39 +00001912 llvm::GlobalVariable *var,
1913 bool shouldPerformInit) {
John McCall5cd91b52010-09-08 01:44:27 +00001914 CGBuilderTy &Builder = CGF.Builder;
John McCall3030eb82010-11-06 09:44:32 +00001915
Richard Smith04e51762013-04-14 23:01:42 +00001916 // We only need to use thread-safe statics for local non-TLS variables;
John McCall3030eb82010-11-06 09:44:32 +00001917 // global initialization is always single-threaded.
Richard Smith04e51762013-04-14 23:01:42 +00001918 bool threadsafe = getContext().getLangOpts().ThreadsafeStatics &&
1919 D.isLocalVarDecl() && !D.getTLSKind();
Anders Carlsson173d5122011-04-27 04:37:08 +00001920
Anders Carlsson173d5122011-04-27 04:37:08 +00001921 // If we have a global variable with internal linkage and thread-safe statics
1922 // are disabled, we can just let the guard variable be of type i8.
John McCall355bba72012-03-30 21:00:39 +00001923 bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage();
1924
1925 llvm::IntegerType *guardTy;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001926 CharUnits guardAlignment;
John McCall0502a222011-06-17 07:33:57 +00001927 if (useInt8GuardVariable) {
John McCall355bba72012-03-30 21:00:39 +00001928 guardTy = CGF.Int8Ty;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001929 guardAlignment = CharUnits::One();
John McCall0502a222011-06-17 07:33:57 +00001930 } else {
Tim Northoverc264e162013-01-31 12:13:10 +00001931 // Guard variables are 64 bits in the generic ABI and size width on ARM
1932 // (i.e. 32-bit on AArch32, 64-bit on AArch64).
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001933 if (UseARMGuardVarABI) {
1934 guardTy = CGF.SizeTy;
1935 guardAlignment = CGF.getSizeAlign();
1936 } else {
1937 guardTy = CGF.Int64Ty;
1938 guardAlignment = CharUnits::fromQuantity(
1939 CGM.getDataLayout().getABITypeAlignment(guardTy));
1940 }
Anders Carlsson173d5122011-04-27 04:37:08 +00001941 }
John McCall355bba72012-03-30 21:00:39 +00001942 llvm::PointerType *guardPtrTy = guardTy->getPointerTo();
John McCall5cd91b52010-09-08 01:44:27 +00001943
John McCall355bba72012-03-30 21:00:39 +00001944 // Create the guard variable if we don't already have it (as we
1945 // might if we're double-emitting this function body).
1946 llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D);
1947 if (!guard) {
1948 // Mangle the name for the guard.
1949 SmallString<256> guardName;
1950 {
1951 llvm::raw_svector_ostream out(guardName);
Reid Kleckner942f9fe2013-09-10 20:14:30 +00001952 getMangleContext().mangleStaticGuardVariable(&D, out);
John McCall355bba72012-03-30 21:00:39 +00001953 }
John McCall112c9672010-11-02 21:04:24 +00001954
John McCall355bba72012-03-30 21:00:39 +00001955 // Create the guard variable with a zero-initializer.
1956 // Just absorb linkage and visibility from the guarded variable.
1957 guard = new llvm::GlobalVariable(CGM.getModule(), guardTy,
1958 false, var->getLinkage(),
1959 llvm::ConstantInt::get(guardTy, 0),
1960 guardName.str());
1961 guard->setVisibility(var->getVisibility());
Richard Smith04e51762013-04-14 23:01:42 +00001962 // If the variable is thread-local, so is its guard variable.
1963 guard->setThreadLocalMode(var->getThreadLocalMode());
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001964 guard->setAlignment(guardAlignment.getQuantity());
John McCall355bba72012-03-30 21:00:39 +00001965
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001966 // The ABI says: "It is suggested that it be emitted in the same COMDAT
1967 // group as the associated data object." In practice, this doesn't work for
1968 // non-ELF object formats, so only do it for ELF.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001969 llvm::Comdat *C = var->getComdat();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001970 if (!D.isLocalVarDecl() && C &&
1971 CGM.getTarget().getTriple().isOSBinFormatELF()) {
Stephen Hines176edba2014-12-01 14:53:08 -08001972 guard->setComdat(C);
Stephen Hines176edba2014-12-01 14:53:08 -08001973 CGF.CurFn->setComdat(C);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001974 } else if (CGM.supportsCOMDAT() && guard->isWeakForLinker()) {
1975 guard->setComdat(CGM.getModule().getOrInsertComdat(guard->getName()));
Stephen Hines176edba2014-12-01 14:53:08 -08001976 }
1977
John McCall355bba72012-03-30 21:00:39 +00001978 CGM.setStaticLocalDeclGuardAddress(&D, guard);
1979 }
John McCall49d26d22012-03-30 07:09:50 +00001980
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001981 Address guardAddr = Address(guard, guardAlignment);
1982
John McCall5cd91b52010-09-08 01:44:27 +00001983 // Test whether the variable has completed initialization.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001984 //
John McCall5cd91b52010-09-08 01:44:27 +00001985 // Itanium C++ ABI 3.3.2:
1986 // The following is pseudo-code showing how these functions can be used:
1987 // if (obj_guard.first_byte == 0) {
1988 // if ( __cxa_guard_acquire (&obj_guard) ) {
1989 // try {
1990 // ... initialize the object ...;
1991 // } catch (...) {
1992 // __cxa_guard_abort (&obj_guard);
1993 // throw;
1994 // }
1995 // ... queue object destructor with __cxa_atexit() ...;
1996 // __cxa_guard_release (&obj_guard);
1997 // }
1998 // }
Stephen Hines651f13c2014-04-23 16:59:28 -07001999
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002000 // Load the first byte of the guard variable.
2001 llvm::LoadInst *LI =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002002 Builder.CreateLoad(Builder.CreateElementBitCast(guardAddr, CGM.Int8Ty));
John McCall5cd91b52010-09-08 01:44:27 +00002003
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002004 // Itanium ABI:
2005 // An implementation supporting thread-safety on multiprocessor
2006 // systems must also guarantee that references to the initialized
2007 // object do not occur before the load of the initialization flag.
2008 //
2009 // In LLVM, we do this by marking the load Acquire.
2010 if (threadsafe)
2011 LI->setAtomic(llvm::Acquire);
Eli Friedmaneb43f4a2011-09-13 22:21:56 +00002012
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002013 // For ARM, we should only check the first bit, rather than the entire byte:
2014 //
2015 // ARM C++ ABI 3.2.3.1:
2016 // To support the potential use of initialization guard variables
2017 // as semaphores that are the target of ARM SWP and LDREX/STREX
2018 // synchronizing instructions we define a static initialization
2019 // guard variable to be a 4-byte aligned, 4-byte word with the
2020 // following inline access protocol.
2021 // #define INITIALIZED 1
2022 // if ((obj_guard & INITIALIZED) != INITIALIZED) {
2023 // if (__cxa_guard_acquire(&obj_guard))
2024 // ...
2025 // }
2026 //
2027 // and similarly for ARM64:
2028 //
2029 // ARM64 C++ ABI 3.2.2:
2030 // This ABI instead only specifies the value bit 0 of the static guard
2031 // variable; all other bits are platform defined. Bit 0 shall be 0 when the
2032 // variable is not initialized and 1 when it is.
2033 llvm::Value *V =
2034 (UseARMGuardVarABI && !useInt8GuardVariable)
2035 ? Builder.CreateAnd(LI, llvm::ConstantInt::get(CGM.Int8Ty, 1))
2036 : LI;
2037 llvm::Value *isInitialized = Builder.CreateIsNull(V, "guard.uninitialized");
John McCall5cd91b52010-09-08 01:44:27 +00002038
2039 llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check");
2040 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
2041
2042 // Check if the first byte of the guard variable is zero.
John McCall355bba72012-03-30 21:00:39 +00002043 Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock);
John McCall5cd91b52010-09-08 01:44:27 +00002044
2045 CGF.EmitBlock(InitCheckBlock);
2046
2047 // Variables used when coping with thread-safe statics and exceptions.
John McCall0502a222011-06-17 07:33:57 +00002048 if (threadsafe) {
John McCall5cd91b52010-09-08 01:44:27 +00002049 // Call __cxa_guard_acquire.
2050 llvm::Value *V
John McCallbd7370a2013-02-28 19:01:20 +00002051 = CGF.EmitNounwindRuntimeCall(getGuardAcquireFn(CGM, guardPtrTy), guard);
John McCall5cd91b52010-09-08 01:44:27 +00002052
2053 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
2054
2055 Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
2056 InitBlock, EndBlock);
2057
2058 // Call __cxa_guard_abort along the exceptional edge.
John McCall355bba72012-03-30 21:00:39 +00002059 CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard);
John McCall5cd91b52010-09-08 01:44:27 +00002060
2061 CGF.EmitBlock(InitBlock);
2062 }
2063
2064 // Emit the initializer and add a global destructor if appropriate.
John McCall355bba72012-03-30 21:00:39 +00002065 CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit);
John McCall5cd91b52010-09-08 01:44:27 +00002066
John McCall0502a222011-06-17 07:33:57 +00002067 if (threadsafe) {
John McCall5cd91b52010-09-08 01:44:27 +00002068 // Pop the guard-abort cleanup if we pushed one.
2069 CGF.PopCleanupBlock();
2070
2071 // Call __cxa_guard_release. This cannot throw.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002072 CGF.EmitNounwindRuntimeCall(getGuardReleaseFn(CGM, guardPtrTy),
2073 guardAddr.getPointer());
John McCall5cd91b52010-09-08 01:44:27 +00002074 } else {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002075 Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guardAddr);
John McCall5cd91b52010-09-08 01:44:27 +00002076 }
2077
2078 CGF.EmitBlock(EndBlock);
2079}
John McCall20bb1752012-05-01 06:13:13 +00002080
2081/// Register a global destructor using __cxa_atexit.
2082static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF,
2083 llvm::Constant *dtor,
Richard Smith04e51762013-04-14 23:01:42 +00002084 llvm::Constant *addr,
2085 bool TLS) {
Bill Wendling4e3b54b2013-05-02 19:18:03 +00002086 const char *Name = "__cxa_atexit";
2087 if (TLS) {
2088 const llvm::Triple &T = CGF.getTarget().getTriple();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002089 Name = T.isOSDarwin() ? "_tlv_atexit" : "__cxa_thread_atexit";
Bill Wendling4e3b54b2013-05-02 19:18:03 +00002090 }
Richard Smith04e51762013-04-14 23:01:42 +00002091
John McCall20bb1752012-05-01 06:13:13 +00002092 // We're assuming that the destructor function is something we can
2093 // reasonably call with the default CC. Go ahead and cast it to the
2094 // right prototype.
2095 llvm::Type *dtorTy =
2096 llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo();
2097
2098 // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d);
2099 llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy };
2100 llvm::FunctionType *atexitTy =
2101 llvm::FunctionType::get(CGF.IntTy, paramTys, false);
2102
2103 // Fetch the actual function.
Richard Smith04e51762013-04-14 23:01:42 +00002104 llvm::Constant *atexit = CGF.CGM.CreateRuntimeFunction(atexitTy, Name);
John McCall20bb1752012-05-01 06:13:13 +00002105 if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit))
2106 fn->setDoesNotThrow();
2107
2108 // Create a variable that binds the atexit to this shared object.
2109 llvm::Constant *handle =
2110 CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle");
2111
2112 llvm::Value *args[] = {
2113 llvm::ConstantExpr::getBitCast(dtor, dtorTy),
2114 llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy),
2115 handle
2116 };
John McCallbd7370a2013-02-28 19:01:20 +00002117 CGF.EmitNounwindRuntimeCall(atexit, args);
John McCall20bb1752012-05-01 06:13:13 +00002118}
2119
2120/// Register a global destructor as best as we know how.
2121void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF,
Richard Smith04e51762013-04-14 23:01:42 +00002122 const VarDecl &D,
John McCall20bb1752012-05-01 06:13:13 +00002123 llvm::Constant *dtor,
2124 llvm::Constant *addr) {
2125 // Use __cxa_atexit if available.
Richard Smith04e51762013-04-14 23:01:42 +00002126 if (CGM.getCodeGenOpts().CXAAtExit)
2127 return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr, D.getTLSKind());
2128
2129 if (D.getTLSKind())
2130 CGM.ErrorUnsupported(&D, "non-trivial TLS destruction");
John McCall20bb1752012-05-01 06:13:13 +00002131
2132 // In Apple kexts, we want to add a global destructor entry.
2133 // FIXME: shouldn't this be guarded by some variable?
Richard Smith7edf9e32012-11-01 22:30:59 +00002134 if (CGM.getLangOpts().AppleKext) {
John McCall20bb1752012-05-01 06:13:13 +00002135 // Generate a global destructor entry.
2136 return CGM.AddCXXDtorEntry(dtor, addr);
2137 }
2138
David Blaikiec7971a92013-08-27 23:57:18 +00002139 CGF.registerGlobalDtorWithAtExit(D, dtor, addr);
John McCall20bb1752012-05-01 06:13:13 +00002140}
Richard Smithb80a16e2013-04-19 16:42:07 +00002141
Stephen Hines176edba2014-12-01 14:53:08 -08002142static bool isThreadWrapperReplaceable(const VarDecl *VD,
2143 CodeGen::CodeGenModule &CGM) {
2144 assert(!VD->isStaticLocal() && "static local VarDecls don't need wrappers!");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002145 // Darwin prefers to have references to thread local variables to go through
Stephen Hines176edba2014-12-01 14:53:08 -08002146 // the thread wrapper instead of directly referencing the backing variable.
2147 return VD->getTLSKind() == VarDecl::TLS_Dynamic &&
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002148 CGM.getTarget().getTriple().isOSDarwin();
Stephen Hines176edba2014-12-01 14:53:08 -08002149}
2150
Richard Smithb80a16e2013-04-19 16:42:07 +00002151/// Get the appropriate linkage for the wrapper function. This is essentially
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002152/// the weak form of the variable's linkage; every translation unit which needs
Richard Smithb80a16e2013-04-19 16:42:07 +00002153/// the wrapper emits a copy, and we want the linker to merge them.
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002154static llvm::GlobalValue::LinkageTypes
2155getThreadLocalWrapperLinkage(const VarDecl *VD, CodeGen::CodeGenModule &CGM) {
2156 llvm::GlobalValue::LinkageTypes VarLinkage =
2157 CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false);
2158
Richard Smithb80a16e2013-04-19 16:42:07 +00002159 // For internal linkage variables, we don't need an external or weak wrapper.
2160 if (llvm::GlobalValue::isLocalLinkage(VarLinkage))
2161 return VarLinkage;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002162
Stephen Hines176edba2014-12-01 14:53:08 -08002163 // If the thread wrapper is replaceable, give it appropriate linkage.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002164 if (isThreadWrapperReplaceable(VD, CGM))
2165 if (!llvm::GlobalVariable::isLinkOnceLinkage(VarLinkage) &&
2166 !llvm::GlobalVariable::isWeakODRLinkage(VarLinkage))
2167 return VarLinkage;
Richard Smithb80a16e2013-04-19 16:42:07 +00002168 return llvm::GlobalValue::WeakODRLinkage;
2169}
2170
2171llvm::Function *
2172ItaniumCXXABI::getOrCreateThreadLocalWrapper(const VarDecl *VD,
Stephen Hines176edba2014-12-01 14:53:08 -08002173 llvm::Value *Val) {
Richard Smithb80a16e2013-04-19 16:42:07 +00002174 // Mangle the name for the thread_local wrapper function.
2175 SmallString<256> WrapperName;
2176 {
2177 llvm::raw_svector_ostream Out(WrapperName);
2178 getMangleContext().mangleItaniumThreadLocalWrapper(VD, Out);
Richard Smithb80a16e2013-04-19 16:42:07 +00002179 }
2180
Stephen Hines176edba2014-12-01 14:53:08 -08002181 if (llvm::Value *V = CGM.getModule().getNamedValue(WrapperName))
Richard Smithb80a16e2013-04-19 16:42:07 +00002182 return cast<llvm::Function>(V);
2183
Stephen Hines176edba2014-12-01 14:53:08 -08002184 llvm::Type *RetTy = Val->getType();
Richard Smithb80a16e2013-04-19 16:42:07 +00002185 if (VD->getType()->isReferenceType())
2186 RetTy = RetTy->getPointerElementType();
2187
2188 llvm::FunctionType *FnTy = llvm::FunctionType::get(RetTy, false);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002189 llvm::Function *Wrapper =
2190 llvm::Function::Create(FnTy, getThreadLocalWrapperLinkage(VD, CGM),
2191 WrapperName.str(), &CGM.getModule());
Richard Smithb80a16e2013-04-19 16:42:07 +00002192 // Always resolve references to the wrapper at link time.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002193 if (!Wrapper->hasLocalLinkage() && !(isThreadWrapperReplaceable(VD, CGM) &&
2194 !llvm::GlobalVariable::isLinkOnceLinkage(Wrapper->getLinkage()) &&
2195 !llvm::GlobalVariable::isWeakODRLinkage(Wrapper->getLinkage())))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002196 Wrapper->setVisibility(llvm::GlobalValue::HiddenVisibility);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002197
2198 if (isThreadWrapperReplaceable(VD, CGM)) {
2199 Wrapper->setCallingConv(llvm::CallingConv::CXX_FAST_TLS);
2200 Wrapper->addFnAttr(llvm::Attribute::NoUnwind);
2201 }
Richard Smithb80a16e2013-04-19 16:42:07 +00002202 return Wrapper;
2203}
2204
2205void ItaniumCXXABI::EmitThreadLocalInitFuncs(
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002206 CodeGenModule &CGM, ArrayRef<const VarDecl *> CXXThreadLocals,
2207 ArrayRef<llvm::Function *> CXXThreadLocalInits,
2208 ArrayRef<const VarDecl *> CXXThreadLocalInitVars) {
Stephen Hines176edba2014-12-01 14:53:08 -08002209 llvm::Function *InitFunc = nullptr;
2210 if (!CXXThreadLocalInits.empty()) {
2211 // Generate a guarded initialization function.
2212 llvm::FunctionType *FTy =
2213 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002214 const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction();
2215 InitFunc = CGM.CreateGlobalInitOrDestructFunction(FTy, "__tls_init", FI,
Stephen Hines176edba2014-12-01 14:53:08 -08002216 SourceLocation(),
2217 /*TLS=*/true);
2218 llvm::GlobalVariable *Guard = new llvm::GlobalVariable(
2219 CGM.getModule(), CGM.Int8Ty, /*isConstant=*/false,
2220 llvm::GlobalVariable::InternalLinkage,
2221 llvm::ConstantInt::get(CGM.Int8Ty, 0), "__tls_guard");
2222 Guard->setThreadLocal(true);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002223
2224 CharUnits GuardAlign = CharUnits::One();
2225 Guard->setAlignment(GuardAlign.getQuantity());
2226
Stephen Hines176edba2014-12-01 14:53:08 -08002227 CodeGenFunction(CGM)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002228 .GenerateCXXGlobalInitFunc(InitFunc, CXXThreadLocalInits,
2229 Address(Guard, GuardAlign));
Stephen Hines176edba2014-12-01 14:53:08 -08002230 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002231 for (const VarDecl *VD : CXXThreadLocals) {
2232 llvm::GlobalVariable *Var =
2233 cast<llvm::GlobalVariable>(CGM.GetGlobalValue(CGM.getMangledName(VD)));
Stephen Hines176edba2014-12-01 14:53:08 -08002234
2235 // Some targets require that all access to thread local variables go through
2236 // the thread wrapper. This means that we cannot attempt to create a thread
2237 // wrapper or a thread helper.
2238 if (isThreadWrapperReplaceable(VD, CGM) && !VD->hasDefinition())
2239 continue;
Richard Smithb80a16e2013-04-19 16:42:07 +00002240
2241 // Mangle the name for the thread_local initialization function.
2242 SmallString<256> InitFnName;
2243 {
2244 llvm::raw_svector_ostream Out(InitFnName);
2245 getMangleContext().mangleItaniumThreadLocalInit(VD, Out);
Richard Smithb80a16e2013-04-19 16:42:07 +00002246 }
2247
2248 // If we have a definition for the variable, emit the initialization
2249 // function as an alias to the global Init function (if any). Otherwise,
2250 // produce a declaration of the initialization function.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002251 llvm::GlobalValue *Init = nullptr;
Richard Smithb80a16e2013-04-19 16:42:07 +00002252 bool InitIsInitFunc = false;
2253 if (VD->hasDefinition()) {
2254 InitIsInitFunc = true;
2255 if (InitFunc)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002256 Init = llvm::GlobalAlias::create(Var->getLinkage(), InitFnName.str(),
2257 InitFunc);
Richard Smithb80a16e2013-04-19 16:42:07 +00002258 } else {
2259 // Emit a weak global function referring to the initialization function.
2260 // This function will not exist if the TU defining the thread_local
2261 // variable in question does not need any dynamic initialization for
2262 // its thread_local variables.
2263 llvm::FunctionType *FnTy = llvm::FunctionType::get(CGM.VoidTy, false);
2264 Init = llvm::Function::Create(
2265 FnTy, llvm::GlobalVariable::ExternalWeakLinkage, InitFnName.str(),
2266 &CGM.getModule());
2267 }
2268
2269 if (Init)
2270 Init->setVisibility(Var->getVisibility());
2271
2272 llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Var);
2273 llvm::LLVMContext &Context = CGM.getModule().getContext();
2274 llvm::BasicBlock *Entry = llvm::BasicBlock::Create(Context, "", Wrapper);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002275 CGBuilderTy Builder(CGM, Entry);
Richard Smithb80a16e2013-04-19 16:42:07 +00002276 if (InitIsInitFunc) {
2277 if (Init)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002278 Builder.CreateCall(Init);
Richard Smithb80a16e2013-04-19 16:42:07 +00002279 } else {
2280 // Don't know whether we have an init function. Call it if it exists.
2281 llvm::Value *Have = Builder.CreateIsNotNull(Init);
2282 llvm::BasicBlock *InitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
2283 llvm::BasicBlock *ExitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
2284 Builder.CreateCondBr(Have, InitBB, ExitBB);
2285
2286 Builder.SetInsertPoint(InitBB);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002287 Builder.CreateCall(Init);
Richard Smithb80a16e2013-04-19 16:42:07 +00002288 Builder.CreateBr(ExitBB);
2289
2290 Builder.SetInsertPoint(ExitBB);
2291 }
2292
2293 // For a reference, the result of the wrapper function is a pointer to
2294 // the referenced object.
2295 llvm::Value *Val = Var;
2296 if (VD->getType()->isReferenceType()) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002297 CharUnits Align = CGM.getContext().getDeclAlign(VD);
2298 Val = Builder.CreateAlignedLoad(Val, Align);
Richard Smithb80a16e2013-04-19 16:42:07 +00002299 }
Stephen Hines176edba2014-12-01 14:53:08 -08002300 if (Val->getType() != Wrapper->getReturnType())
2301 Val = Builder.CreatePointerBitCastOrAddrSpaceCast(
2302 Val, Wrapper->getReturnType(), "");
Richard Smithb80a16e2013-04-19 16:42:07 +00002303 Builder.CreateRet(Val);
2304 }
2305}
2306
Stephen Hines651f13c2014-04-23 16:59:28 -07002307LValue ItaniumCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
2308 const VarDecl *VD,
2309 QualType LValType) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002310 llvm::Value *Val = CGF.CGM.GetAddrOfGlobalVar(VD);
Stephen Hines176edba2014-12-01 14:53:08 -08002311 llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Val);
Richard Smithb80a16e2013-04-19 16:42:07 +00002312
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002313 llvm::CallInst *CallVal = CGF.Builder.CreateCall(Wrapper);
2314 if (isThreadWrapperReplaceable(VD, CGF.CGM))
2315 CallVal->setCallingConv(llvm::CallingConv::CXX_FAST_TLS);
Richard Smithb80a16e2013-04-19 16:42:07 +00002316
2317 LValue LV;
2318 if (VD->getType()->isReferenceType())
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002319 LV = CGF.MakeNaturalAlignAddrLValue(CallVal, LValType);
Richard Smithb80a16e2013-04-19 16:42:07 +00002320 else
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002321 LV = CGF.MakeAddrLValue(CallVal, LValType,
2322 CGF.getContext().getDeclAlign(VD));
Richard Smithb80a16e2013-04-19 16:42:07 +00002323 // FIXME: need setObjCGCLValueClass?
2324 return LV;
2325}
Peter Collingbournee1e35f72013-06-28 20:45:28 +00002326
2327/// Return whether the given global decl needs a VTT parameter, which it does
2328/// if it's a base constructor or destructor with virtual bases.
2329bool ItaniumCXXABI::NeedsVTTParameter(GlobalDecl GD) {
2330 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2331
2332 // We don't have any virtual bases, just return early.
2333 if (!MD->getParent()->getNumVBases())
2334 return false;
2335
2336 // Check if we have a base constructor.
2337 if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base)
2338 return true;
2339
2340 // Check if we have a base destructor.
2341 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
2342 return true;
2343
2344 return false;
2345}
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002346
2347namespace {
2348class ItaniumRTTIBuilder {
2349 CodeGenModule &CGM; // Per-module state.
2350 llvm::LLVMContext &VMContext;
2351 const ItaniumCXXABI &CXXABI; // Per-module state.
2352
2353 /// Fields - The fields of the RTTI descriptor currently being built.
2354 SmallVector<llvm::Constant *, 16> Fields;
2355
2356 /// GetAddrOfTypeName - Returns the mangled type name of the given type.
2357 llvm::GlobalVariable *
2358 GetAddrOfTypeName(QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage);
2359
2360 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
2361 /// descriptor of the given type.
2362 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
2363
2364 /// BuildVTablePointer - Build the vtable pointer for the given type.
2365 void BuildVTablePointer(const Type *Ty);
2366
2367 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
2368 /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
2369 void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
2370
2371 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
2372 /// classes with bases that do not satisfy the abi::__si_class_type_info
2373 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
2374 void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
2375
2376 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
2377 /// for pointer types.
2378 void BuildPointerTypeInfo(QualType PointeeTy);
2379
2380 /// BuildObjCObjectTypeInfo - Build the appropriate kind of
2381 /// type_info for an object type.
2382 void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty);
2383
2384 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
2385 /// struct, used for member pointer types.
2386 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
2387
2388public:
2389 ItaniumRTTIBuilder(const ItaniumCXXABI &ABI)
2390 : CGM(ABI.CGM), VMContext(CGM.getModule().getContext()), CXXABI(ABI) {}
2391
2392 // Pointer type info flags.
2393 enum {
2394 /// PTI_Const - Type has const qualifier.
2395 PTI_Const = 0x1,
2396
2397 /// PTI_Volatile - Type has volatile qualifier.
2398 PTI_Volatile = 0x2,
2399
2400 /// PTI_Restrict - Type has restrict qualifier.
2401 PTI_Restrict = 0x4,
2402
2403 /// PTI_Incomplete - Type is incomplete.
2404 PTI_Incomplete = 0x8,
2405
2406 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
2407 /// (in pointer to member).
2408 PTI_ContainingClassIncomplete = 0x10
2409 };
2410
2411 // VMI type info flags.
2412 enum {
2413 /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
2414 VMI_NonDiamondRepeat = 0x1,
2415
2416 /// VMI_DiamondShaped - Class is diamond shaped.
2417 VMI_DiamondShaped = 0x2
2418 };
2419
2420 // Base class type info flags.
2421 enum {
2422 /// BCTI_Virtual - Base class is virtual.
2423 BCTI_Virtual = 0x1,
2424
2425 /// BCTI_Public - Base class is public.
2426 BCTI_Public = 0x2
2427 };
2428
2429 /// BuildTypeInfo - Build the RTTI type info struct for the given type.
2430 ///
2431 /// \param Force - true to force the creation of this RTTI value
2432 llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false);
2433};
2434}
2435
2436llvm::GlobalVariable *ItaniumRTTIBuilder::GetAddrOfTypeName(
2437 QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002438 SmallString<256> Name;
2439 llvm::raw_svector_ostream Out(Name);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002440 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002441
2442 // We know that the mangled name of the type starts at index 4 of the
2443 // mangled name of the typename, so we can just index into it in order to
2444 // get the mangled name of the type.
2445 llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext,
2446 Name.substr(4));
2447
2448 llvm::GlobalVariable *GV =
2449 CGM.CreateOrReplaceCXXRuntimeVariable(Name, Init->getType(), Linkage);
2450
2451 GV->setInitializer(Init);
2452
2453 return GV;
2454}
2455
2456llvm::Constant *
2457ItaniumRTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
2458 // Mangle the RTTI name.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002459 SmallString<256> Name;
2460 llvm::raw_svector_ostream Out(Name);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002461 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002462
2463 // Look for an existing global.
2464 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
2465
2466 if (!GV) {
2467 // Create a new global variable.
2468 GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy,
2469 /*Constant=*/true,
2470 llvm::GlobalValue::ExternalLinkage, nullptr,
2471 Name);
Stephen Hines176edba2014-12-01 14:53:08 -08002472 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
2473 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
2474 if (RD->hasAttr<DLLImportAttr>())
2475 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
2476 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002477 }
2478
2479 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
2480}
2481
2482/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
2483/// info for that type is defined in the standard library.
2484static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
2485 // Itanium C++ ABI 2.9.2:
2486 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
2487 // the run-time support library. Specifically, the run-time support
2488 // library should contain type_info objects for the types X, X* and
2489 // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char,
2490 // unsigned char, signed char, short, unsigned short, int, unsigned int,
2491 // long, unsigned long, long long, unsigned long long, float, double,
2492 // long double, char16_t, char32_t, and the IEEE 754r decimal and
2493 // half-precision floating point types.
2494 switch (Ty->getKind()) {
2495 case BuiltinType::Void:
2496 case BuiltinType::NullPtr:
2497 case BuiltinType::Bool:
2498 case BuiltinType::WChar_S:
2499 case BuiltinType::WChar_U:
2500 case BuiltinType::Char_U:
2501 case BuiltinType::Char_S:
2502 case BuiltinType::UChar:
2503 case BuiltinType::SChar:
2504 case BuiltinType::Short:
2505 case BuiltinType::UShort:
2506 case BuiltinType::Int:
2507 case BuiltinType::UInt:
2508 case BuiltinType::Long:
2509 case BuiltinType::ULong:
2510 case BuiltinType::LongLong:
2511 case BuiltinType::ULongLong:
2512 case BuiltinType::Half:
2513 case BuiltinType::Float:
2514 case BuiltinType::Double:
2515 case BuiltinType::LongDouble:
2516 case BuiltinType::Char16:
2517 case BuiltinType::Char32:
2518 case BuiltinType::Int128:
2519 case BuiltinType::UInt128:
2520 case BuiltinType::OCLImage1d:
2521 case BuiltinType::OCLImage1dArray:
2522 case BuiltinType::OCLImage1dBuffer:
2523 case BuiltinType::OCLImage2d:
2524 case BuiltinType::OCLImage2dArray:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002525 case BuiltinType::OCLImage2dDepth:
2526 case BuiltinType::OCLImage2dArrayDepth:
2527 case BuiltinType::OCLImage2dMSAA:
2528 case BuiltinType::OCLImage2dArrayMSAA:
2529 case BuiltinType::OCLImage2dMSAADepth:
2530 case BuiltinType::OCLImage2dArrayMSAADepth:
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002531 case BuiltinType::OCLImage3d:
2532 case BuiltinType::OCLSampler:
2533 case BuiltinType::OCLEvent:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002534 case BuiltinType::OCLClkEvent:
2535 case BuiltinType::OCLQueue:
2536 case BuiltinType::OCLNDRange:
2537 case BuiltinType::OCLReserveID:
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002538 return true;
2539
2540 case BuiltinType::Dependent:
2541#define BUILTIN_TYPE(Id, SingletonId)
2542#define PLACEHOLDER_TYPE(Id, SingletonId) \
2543 case BuiltinType::Id:
2544#include "clang/AST/BuiltinTypes.def"
2545 llvm_unreachable("asking for RRTI for a placeholder type!");
2546
2547 case BuiltinType::ObjCId:
2548 case BuiltinType::ObjCClass:
2549 case BuiltinType::ObjCSel:
2550 llvm_unreachable("FIXME: Objective-C types are unsupported!");
2551 }
2552
2553 llvm_unreachable("Invalid BuiltinType Kind!");
2554}
2555
2556static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
2557 QualType PointeeTy = PointerTy->getPointeeType();
2558 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
2559 if (!BuiltinTy)
2560 return false;
2561
2562 // Check the qualifiers.
2563 Qualifiers Quals = PointeeTy.getQualifiers();
2564 Quals.removeConst();
2565
2566 if (!Quals.empty())
2567 return false;
2568
2569 return TypeInfoIsInStandardLibrary(BuiltinTy);
2570}
2571
2572/// IsStandardLibraryRTTIDescriptor - Returns whether the type
2573/// information for the given type exists in the standard library.
2574static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
2575 // Type info for builtin types is defined in the standard library.
2576 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
2577 return TypeInfoIsInStandardLibrary(BuiltinTy);
2578
2579 // Type info for some pointer types to builtin types is defined in the
2580 // standard library.
2581 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
2582 return TypeInfoIsInStandardLibrary(PointerTy);
2583
2584 return false;
2585}
2586
2587/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
2588/// the given type exists somewhere else, and that we should not emit the type
2589/// information in this translation unit. Assumes that it is not a
2590/// standard-library type.
2591static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM,
2592 QualType Ty) {
2593 ASTContext &Context = CGM.getContext();
2594
2595 // If RTTI is disabled, assume it might be disabled in the
2596 // translation unit that defines any potential key function, too.
2597 if (!Context.getLangOpts().RTTI) return false;
2598
2599 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
2600 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
2601 if (!RD->hasDefinition())
2602 return false;
2603
2604 if (!RD->isDynamicClass())
2605 return false;
2606
2607 // FIXME: this may need to be reconsidered if the key function
2608 // changes.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002609 // N.B. We must always emit the RTTI data ourselves if there exists a key
2610 // function.
2611 bool IsDLLImport = RD->hasAttr<DLLImportAttr>();
Stephen Hines176edba2014-12-01 14:53:08 -08002612 if (CGM.getVTables().isVTableExternal(RD))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002613 return IsDLLImport ? false : true;
Stephen Hines176edba2014-12-01 14:53:08 -08002614
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002615 if (IsDLLImport)
Stephen Hines176edba2014-12-01 14:53:08 -08002616 return true;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002617 }
2618
2619 return false;
2620}
2621
2622/// IsIncompleteClassType - Returns whether the given record type is incomplete.
2623static bool IsIncompleteClassType(const RecordType *RecordTy) {
2624 return !RecordTy->getDecl()->isCompleteDefinition();
2625}
2626
2627/// ContainsIncompleteClassType - Returns whether the given type contains an
2628/// incomplete class type. This is true if
2629///
2630/// * The given type is an incomplete class type.
2631/// * The given type is a pointer type whose pointee type contains an
2632/// incomplete class type.
2633/// * The given type is a member pointer type whose class is an incomplete
2634/// class type.
2635/// * The given type is a member pointer type whoise pointee type contains an
2636/// incomplete class type.
2637/// is an indirect or direct pointer to an incomplete class type.
2638static bool ContainsIncompleteClassType(QualType Ty) {
2639 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
2640 if (IsIncompleteClassType(RecordTy))
2641 return true;
2642 }
2643
2644 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
2645 return ContainsIncompleteClassType(PointerTy->getPointeeType());
2646
2647 if (const MemberPointerType *MemberPointerTy =
2648 dyn_cast<MemberPointerType>(Ty)) {
2649 // Check if the class type is incomplete.
2650 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
2651 if (IsIncompleteClassType(ClassType))
2652 return true;
2653
2654 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
2655 }
2656
2657 return false;
2658}
2659
2660// CanUseSingleInheritance - Return whether the given record decl has a "single,
2661// public, non-virtual base at offset zero (i.e. the derived class is dynamic
2662// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
2663static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
2664 // Check the number of bases.
2665 if (RD->getNumBases() != 1)
2666 return false;
2667
2668 // Get the base.
2669 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
2670
2671 // Check that the base is not virtual.
2672 if (Base->isVirtual())
2673 return false;
2674
2675 // Check that the base is public.
2676 if (Base->getAccessSpecifier() != AS_public)
2677 return false;
2678
2679 // Check that the class is dynamic iff the base is.
2680 const CXXRecordDecl *BaseDecl =
2681 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
2682 if (!BaseDecl->isEmpty() &&
2683 BaseDecl->isDynamicClass() != RD->isDynamicClass())
2684 return false;
2685
2686 return true;
2687}
2688
2689void ItaniumRTTIBuilder::BuildVTablePointer(const Type *Ty) {
2690 // abi::__class_type_info.
2691 static const char * const ClassTypeInfo =
2692 "_ZTVN10__cxxabiv117__class_type_infoE";
2693 // abi::__si_class_type_info.
2694 static const char * const SIClassTypeInfo =
2695 "_ZTVN10__cxxabiv120__si_class_type_infoE";
2696 // abi::__vmi_class_type_info.
2697 static const char * const VMIClassTypeInfo =
2698 "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
2699
2700 const char *VTableName = nullptr;
2701
2702 switch (Ty->getTypeClass()) {
2703#define TYPE(Class, Base)
2704#define ABSTRACT_TYPE(Class, Base)
2705#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
2706#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2707#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2708#include "clang/AST/TypeNodes.def"
2709 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
2710
2711 case Type::LValueReference:
2712 case Type::RValueReference:
2713 llvm_unreachable("References shouldn't get here");
2714
2715 case Type::Auto:
2716 llvm_unreachable("Undeduced auto type shouldn't get here");
2717
2718 case Type::Builtin:
2719 // GCC treats vector and complex types as fundamental types.
2720 case Type::Vector:
2721 case Type::ExtVector:
2722 case Type::Complex:
2723 case Type::Atomic:
2724 // FIXME: GCC treats block pointers as fundamental types?!
2725 case Type::BlockPointer:
2726 // abi::__fundamental_type_info.
2727 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
2728 break;
2729
2730 case Type::ConstantArray:
2731 case Type::IncompleteArray:
2732 case Type::VariableArray:
2733 // abi::__array_type_info.
2734 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
2735 break;
2736
2737 case Type::FunctionNoProto:
2738 case Type::FunctionProto:
2739 // abi::__function_type_info.
2740 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
2741 break;
2742
2743 case Type::Enum:
2744 // abi::__enum_type_info.
2745 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
2746 break;
2747
2748 case Type::Record: {
2749 const CXXRecordDecl *RD =
2750 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
2751
2752 if (!RD->hasDefinition() || !RD->getNumBases()) {
2753 VTableName = ClassTypeInfo;
2754 } else if (CanUseSingleInheritance(RD)) {
2755 VTableName = SIClassTypeInfo;
2756 } else {
2757 VTableName = VMIClassTypeInfo;
2758 }
2759
2760 break;
2761 }
2762
2763 case Type::ObjCObject:
2764 // Ignore protocol qualifiers.
2765 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
2766
2767 // Handle id and Class.
2768 if (isa<BuiltinType>(Ty)) {
2769 VTableName = ClassTypeInfo;
2770 break;
2771 }
2772
2773 assert(isa<ObjCInterfaceType>(Ty));
2774 // Fall through.
2775
2776 case Type::ObjCInterface:
2777 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
2778 VTableName = SIClassTypeInfo;
2779 } else {
2780 VTableName = ClassTypeInfo;
2781 }
2782 break;
2783
2784 case Type::ObjCObjectPointer:
2785 case Type::Pointer:
2786 // abi::__pointer_type_info.
2787 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
2788 break;
2789
2790 case Type::MemberPointer:
2791 // abi::__pointer_to_member_type_info.
2792 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
2793 break;
2794 }
2795
2796 llvm::Constant *VTable =
2797 CGM.getModule().getOrInsertGlobal(VTableName, CGM.Int8PtrTy);
2798
2799 llvm::Type *PtrDiffTy =
2800 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
2801
2802 // The vtable address point is 2.
2803 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002804 VTable =
2805 llvm::ConstantExpr::getInBoundsGetElementPtr(CGM.Int8PtrTy, VTable, Two);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002806 VTable = llvm::ConstantExpr::getBitCast(VTable, CGM.Int8PtrTy);
2807
2808 Fields.push_back(VTable);
2809}
2810
2811/// \brief Return the linkage that the type info and type info name constants
2812/// should have for the given type.
2813static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(CodeGenModule &CGM,
2814 QualType Ty) {
2815 // Itanium C++ ABI 2.9.5p7:
2816 // In addition, it and all of the intermediate abi::__pointer_type_info
2817 // structs in the chain down to the abi::__class_type_info for the
2818 // incomplete class type must be prevented from resolving to the
2819 // corresponding type_info structs for the complete class type, possibly
2820 // by making them local static objects. Finally, a dummy class RTTI is
2821 // generated for the incomplete type that will not resolve to the final
2822 // complete class RTTI (because the latter need not exist), possibly by
2823 // making it a local static object.
2824 if (ContainsIncompleteClassType(Ty))
2825 return llvm::GlobalValue::InternalLinkage;
2826
2827 switch (Ty->getLinkage()) {
2828 case NoLinkage:
2829 case InternalLinkage:
2830 case UniqueExternalLinkage:
2831 return llvm::GlobalValue::InternalLinkage;
2832
2833 case VisibleNoLinkage:
2834 case ExternalLinkage:
2835 if (!CGM.getLangOpts().RTTI) {
2836 // RTTI is not enabled, which means that this type info struct is going
2837 // to be used for exception handling. Give it linkonce_odr linkage.
2838 return llvm::GlobalValue::LinkOnceODRLinkage;
2839 }
2840
2841 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
2842 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
2843 if (RD->hasAttr<WeakAttr>())
2844 return llvm::GlobalValue::WeakODRLinkage;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002845 if (RD->isDynamicClass()) {
2846 llvm::GlobalValue::LinkageTypes LT = CGM.getVTableLinkage(RD);
2847 // MinGW won't export the RTTI information when there is a key function.
2848 // Make sure we emit our own copy instead of attempting to dllimport it.
2849 if (RD->hasAttr<DLLImportAttr>() &&
2850 llvm::GlobalValue::isAvailableExternallyLinkage(LT))
2851 LT = llvm::GlobalValue::LinkOnceODRLinkage;
2852 return LT;
2853 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002854 }
2855
2856 return llvm::GlobalValue::LinkOnceODRLinkage;
2857 }
2858
2859 llvm_unreachable("Invalid linkage!");
2860}
2861
2862llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
2863 // We want to operate on the canonical type.
2864 Ty = CGM.getContext().getCanonicalType(Ty);
2865
2866 // Check if we've already emitted an RTTI descriptor for this type.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002867 SmallString<256> Name;
2868 llvm::raw_svector_ostream Out(Name);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002869 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002870
2871 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
2872 if (OldGV && !OldGV->isDeclaration()) {
2873 assert(!OldGV->hasAvailableExternallyLinkage() &&
2874 "available_externally typeinfos not yet implemented");
2875
2876 return llvm::ConstantExpr::getBitCast(OldGV, CGM.Int8PtrTy);
2877 }
2878
2879 // Check if there is already an external RTTI descriptor for this type.
2880 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
2881 if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
2882 return GetAddrOfExternalRTTIDescriptor(Ty);
2883
2884 // Emit the standard library with external linkage.
2885 llvm::GlobalVariable::LinkageTypes Linkage;
2886 if (IsStdLib)
2887 Linkage = llvm::GlobalValue::ExternalLinkage;
2888 else
2889 Linkage = getTypeInfoLinkage(CGM, Ty);
2890
2891 // Add the vtable pointer.
2892 BuildVTablePointer(cast<Type>(Ty));
2893
2894 // And the name.
2895 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
2896 llvm::Constant *TypeNameField;
2897
2898 // If we're supposed to demote the visibility, be sure to set a flag
2899 // to use a string comparison for type_info comparisons.
2900 ItaniumCXXABI::RTTIUniquenessKind RTTIUniqueness =
2901 CXXABI.classifyRTTIUniqueness(Ty, Linkage);
2902 if (RTTIUniqueness != ItaniumCXXABI::RUK_Unique) {
2903 // The flag is the sign bit, which on ARM64 is defined to be clear
2904 // for global pointers. This is very ARM64-specific.
2905 TypeNameField = llvm::ConstantExpr::getPtrToInt(TypeName, CGM.Int64Ty);
2906 llvm::Constant *flag =
2907 llvm::ConstantInt::get(CGM.Int64Ty, ((uint64_t)1) << 63);
2908 TypeNameField = llvm::ConstantExpr::getAdd(TypeNameField, flag);
2909 TypeNameField =
2910 llvm::ConstantExpr::getIntToPtr(TypeNameField, CGM.Int8PtrTy);
2911 } else {
2912 TypeNameField = llvm::ConstantExpr::getBitCast(TypeName, CGM.Int8PtrTy);
2913 }
2914 Fields.push_back(TypeNameField);
2915
2916 switch (Ty->getTypeClass()) {
2917#define TYPE(Class, Base)
2918#define ABSTRACT_TYPE(Class, Base)
2919#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
2920#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2921#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2922#include "clang/AST/TypeNodes.def"
2923 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
2924
2925 // GCC treats vector types as fundamental types.
2926 case Type::Builtin:
2927 case Type::Vector:
2928 case Type::ExtVector:
2929 case Type::Complex:
2930 case Type::BlockPointer:
2931 // Itanium C++ ABI 2.9.5p4:
2932 // abi::__fundamental_type_info adds no data members to std::type_info.
2933 break;
2934
2935 case Type::LValueReference:
2936 case Type::RValueReference:
2937 llvm_unreachable("References shouldn't get here");
2938
2939 case Type::Auto:
2940 llvm_unreachable("Undeduced auto type shouldn't get here");
2941
2942 case Type::ConstantArray:
2943 case Type::IncompleteArray:
2944 case Type::VariableArray:
2945 // Itanium C++ ABI 2.9.5p5:
2946 // abi::__array_type_info adds no data members to std::type_info.
2947 break;
2948
2949 case Type::FunctionNoProto:
2950 case Type::FunctionProto:
2951 // Itanium C++ ABI 2.9.5p5:
2952 // abi::__function_type_info adds no data members to std::type_info.
2953 break;
2954
2955 case Type::Enum:
2956 // Itanium C++ ABI 2.9.5p5:
2957 // abi::__enum_type_info adds no data members to std::type_info.
2958 break;
2959
2960 case Type::Record: {
2961 const CXXRecordDecl *RD =
2962 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
2963 if (!RD->hasDefinition() || !RD->getNumBases()) {
2964 // We don't need to emit any fields.
2965 break;
2966 }
2967
2968 if (CanUseSingleInheritance(RD))
2969 BuildSIClassTypeInfo(RD);
2970 else
2971 BuildVMIClassTypeInfo(RD);
2972
2973 break;
2974 }
2975
2976 case Type::ObjCObject:
2977 case Type::ObjCInterface:
2978 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
2979 break;
2980
2981 case Type::ObjCObjectPointer:
2982 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
2983 break;
2984
2985 case Type::Pointer:
2986 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
2987 break;
2988
2989 case Type::MemberPointer:
2990 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
2991 break;
2992
2993 case Type::Atomic:
2994 // No fields, at least for the moment.
2995 break;
2996 }
2997
2998 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields);
2999
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003000 llvm::Module &M = CGM.getModule();
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003001 llvm::GlobalVariable *GV =
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003002 new llvm::GlobalVariable(M, Init->getType(),
3003 /*Constant=*/true, Linkage, Init, Name);
3004
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003005 // If there's already an old global variable, replace it with the new one.
3006 if (OldGV) {
3007 GV->takeName(OldGV);
3008 llvm::Constant *NewPtr =
3009 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
3010 OldGV->replaceAllUsesWith(NewPtr);
3011 OldGV->eraseFromParent();
3012 }
3013
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003014 if (CGM.supportsCOMDAT() && GV->isWeakForLinker())
3015 GV->setComdat(M.getOrInsertComdat(GV->getName()));
3016
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003017 // The Itanium ABI specifies that type_info objects must be globally
3018 // unique, with one exception: if the type is an incomplete class
3019 // type or a (possibly indirect) pointer to one. That exception
3020 // affects the general case of comparing type_info objects produced
3021 // by the typeid operator, which is why the comparison operators on
3022 // std::type_info generally use the type_info name pointers instead
3023 // of the object addresses. However, the language's built-in uses
3024 // of RTTI generally require class types to be complete, even when
3025 // manipulating pointers to those class types. This allows the
3026 // implementation of dynamic_cast to rely on address equality tests,
3027 // which is much faster.
3028
3029 // All of this is to say that it's important that both the type_info
3030 // object and the type_info name be uniqued when weakly emitted.
3031
3032 // Give the type_info object and name the formal visibility of the
3033 // type itself.
3034 llvm::GlobalValue::VisibilityTypes llvmVisibility;
3035 if (llvm::GlobalValue::isLocalLinkage(Linkage))
3036 // If the linkage is local, only default visibility makes sense.
3037 llvmVisibility = llvm::GlobalValue::DefaultVisibility;
3038 else if (RTTIUniqueness == ItaniumCXXABI::RUK_NonUniqueHidden)
3039 llvmVisibility = llvm::GlobalValue::HiddenVisibility;
3040 else
3041 llvmVisibility = CodeGenModule::GetLLVMVisibility(Ty->getVisibility());
3042 TypeName->setVisibility(llvmVisibility);
3043 GV->setVisibility(llvmVisibility);
3044
3045 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
3046}
3047
3048/// ComputeQualifierFlags - Compute the pointer type info flags from the
3049/// given qualifier.
3050static unsigned ComputeQualifierFlags(Qualifiers Quals) {
3051 unsigned Flags = 0;
3052
3053 if (Quals.hasConst())
3054 Flags |= ItaniumRTTIBuilder::PTI_Const;
3055 if (Quals.hasVolatile())
3056 Flags |= ItaniumRTTIBuilder::PTI_Volatile;
3057 if (Quals.hasRestrict())
3058 Flags |= ItaniumRTTIBuilder::PTI_Restrict;
3059
3060 return Flags;
3061}
3062
3063/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
3064/// for the given Objective-C object type.
3065void ItaniumRTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
3066 // Drop qualifiers.
3067 const Type *T = OT->getBaseType().getTypePtr();
3068 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
3069
3070 // The builtin types are abi::__class_type_infos and don't require
3071 // extra fields.
3072 if (isa<BuiltinType>(T)) return;
3073
3074 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
3075 ObjCInterfaceDecl *Super = Class->getSuperClass();
3076
3077 // Root classes are also __class_type_info.
3078 if (!Super) return;
3079
3080 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
3081
3082 // Everything else is single inheritance.
3083 llvm::Constant *BaseTypeInfo =
3084 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(SuperTy);
3085 Fields.push_back(BaseTypeInfo);
3086}
3087
3088/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
3089/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
3090void ItaniumRTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
3091 // Itanium C++ ABI 2.9.5p6b:
3092 // It adds to abi::__class_type_info a single member pointing to the
3093 // type_info structure for the base type,
3094 llvm::Constant *BaseTypeInfo =
3095 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(RD->bases_begin()->getType());
3096 Fields.push_back(BaseTypeInfo);
3097}
3098
3099namespace {
3100 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
3101 /// a class hierarchy.
3102 struct SeenBases {
3103 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
3104 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
3105 };
3106}
3107
3108/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
3109/// abi::__vmi_class_type_info.
3110///
3111static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
3112 SeenBases &Bases) {
3113
3114 unsigned Flags = 0;
3115
3116 const CXXRecordDecl *BaseDecl =
3117 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
3118
3119 if (Base->isVirtual()) {
3120 // Mark the virtual base as seen.
Stephen Hines176edba2014-12-01 14:53:08 -08003121 if (!Bases.VirtualBases.insert(BaseDecl).second) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003122 // If this virtual base has been seen before, then the class is diamond
3123 // shaped.
3124 Flags |= ItaniumRTTIBuilder::VMI_DiamondShaped;
3125 } else {
3126 if (Bases.NonVirtualBases.count(BaseDecl))
3127 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
3128 }
3129 } else {
3130 // Mark the non-virtual base as seen.
Stephen Hines176edba2014-12-01 14:53:08 -08003131 if (!Bases.NonVirtualBases.insert(BaseDecl).second) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003132 // If this non-virtual base has been seen before, then the class has non-
3133 // diamond shaped repeated inheritance.
3134 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
3135 } else {
3136 if (Bases.VirtualBases.count(BaseDecl))
3137 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
3138 }
3139 }
3140
3141 // Walk all bases.
3142 for (const auto &I : BaseDecl->bases())
3143 Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases);
3144
3145 return Flags;
3146}
3147
3148static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
3149 unsigned Flags = 0;
3150 SeenBases Bases;
3151
3152 // Walk all bases.
3153 for (const auto &I : RD->bases())
3154 Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases);
3155
3156 return Flags;
3157}
3158
3159/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
3160/// classes with bases that do not satisfy the abi::__si_class_type_info
3161/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
3162void ItaniumRTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
3163 llvm::Type *UnsignedIntLTy =
3164 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
3165
3166 // Itanium C++ ABI 2.9.5p6c:
3167 // __flags is a word with flags describing details about the class
3168 // structure, which may be referenced by using the __flags_masks
3169 // enumeration. These flags refer to both direct and indirect bases.
3170 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
3171 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3172
3173 // Itanium C++ ABI 2.9.5p6c:
3174 // __base_count is a word with the number of direct proper base class
3175 // descriptions that follow.
3176 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
3177
3178 if (!RD->getNumBases())
3179 return;
3180
3181 llvm::Type *LongLTy =
3182 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
3183
3184 // Now add the base class descriptions.
3185
3186 // Itanium C++ ABI 2.9.5p6c:
3187 // __base_info[] is an array of base class descriptions -- one for every
3188 // direct proper base. Each description is of the type:
3189 //
3190 // struct abi::__base_class_type_info {
3191 // public:
3192 // const __class_type_info *__base_type;
3193 // long __offset_flags;
3194 //
3195 // enum __offset_flags_masks {
3196 // __virtual_mask = 0x1,
3197 // __public_mask = 0x2,
3198 // __offset_shift = 8
3199 // };
3200 // };
3201 for (const auto &Base : RD->bases()) {
3202 // The __base_type member points to the RTTI for the base type.
3203 Fields.push_back(ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(Base.getType()));
3204
3205 const CXXRecordDecl *BaseDecl =
3206 cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
3207
3208 int64_t OffsetFlags = 0;
3209
3210 // All but the lower 8 bits of __offset_flags are a signed offset.
3211 // For a non-virtual base, this is the offset in the object of the base
3212 // subobject. For a virtual base, this is the offset in the virtual table of
3213 // the virtual base offset for the virtual base referenced (negative).
3214 CharUnits Offset;
3215 if (Base.isVirtual())
3216 Offset =
3217 CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(RD, BaseDecl);
3218 else {
3219 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
3220 Offset = Layout.getBaseClassOffset(BaseDecl);
3221 };
3222
3223 OffsetFlags = uint64_t(Offset.getQuantity()) << 8;
3224
3225 // The low-order byte of __offset_flags contains flags, as given by the
3226 // masks from the enumeration __offset_flags_masks.
3227 if (Base.isVirtual())
3228 OffsetFlags |= BCTI_Virtual;
3229 if (Base.getAccessSpecifier() == AS_public)
3230 OffsetFlags |= BCTI_Public;
3231
3232 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
3233 }
3234}
3235
3236/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
3237/// used for pointer types.
3238void ItaniumRTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
3239 Qualifiers Quals;
3240 QualType UnqualifiedPointeeTy =
3241 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
3242
3243 // Itanium C++ ABI 2.9.5p7:
3244 // __flags is a flag word describing the cv-qualification and other
3245 // attributes of the type pointed to
3246 unsigned Flags = ComputeQualifierFlags(Quals);
3247
3248 // Itanium C++ ABI 2.9.5p7:
3249 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
3250 // incomplete class type, the incomplete target type flag is set.
3251 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
3252 Flags |= PTI_Incomplete;
3253
3254 llvm::Type *UnsignedIntLTy =
3255 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
3256 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3257
3258 // Itanium C++ ABI 2.9.5p7:
3259 // __pointee is a pointer to the std::type_info derivation for the
3260 // unqualified type being pointed to.
3261 llvm::Constant *PointeeTypeInfo =
3262 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(UnqualifiedPointeeTy);
3263 Fields.push_back(PointeeTypeInfo);
3264}
3265
3266/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
3267/// struct, used for member pointer types.
3268void
3269ItaniumRTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
3270 QualType PointeeTy = Ty->getPointeeType();
3271
3272 Qualifiers Quals;
3273 QualType UnqualifiedPointeeTy =
3274 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
3275
3276 // Itanium C++ ABI 2.9.5p7:
3277 // __flags is a flag word describing the cv-qualification and other
3278 // attributes of the type pointed to.
3279 unsigned Flags = ComputeQualifierFlags(Quals);
3280
3281 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
3282
3283 // Itanium C++ ABI 2.9.5p7:
3284 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
3285 // incomplete class type, the incomplete target type flag is set.
3286 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
3287 Flags |= PTI_Incomplete;
3288
3289 if (IsIncompleteClassType(ClassType))
3290 Flags |= PTI_ContainingClassIncomplete;
3291
3292 llvm::Type *UnsignedIntLTy =
3293 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
3294 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3295
3296 // Itanium C++ ABI 2.9.5p7:
3297 // __pointee is a pointer to the std::type_info derivation for the
3298 // unqualified type being pointed to.
3299 llvm::Constant *PointeeTypeInfo =
3300 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(UnqualifiedPointeeTy);
3301 Fields.push_back(PointeeTypeInfo);
3302
3303 // Itanium C++ ABI 2.9.5p9:
3304 // __context is a pointer to an abi::__class_type_info corresponding to the
3305 // class type containing the member pointed to
3306 // (e.g., the "A" in "int A::*").
3307 Fields.push_back(
3308 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(QualType(ClassType, 0)));
3309}
3310
3311llvm::Constant *ItaniumCXXABI::getAddrOfRTTIDescriptor(QualType Ty) {
3312 return ItaniumRTTIBuilder(*this).BuildTypeInfo(Ty);
3313}
3314
3315void ItaniumCXXABI::EmitFundamentalRTTIDescriptor(QualType Type) {
3316 QualType PointerType = getContext().getPointerType(Type);
3317 QualType PointerTypeConst = getContext().getPointerType(Type.withConst());
3318 ItaniumRTTIBuilder(*this).BuildTypeInfo(Type, true);
3319 ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerType, true);
3320 ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
3321}
3322
3323void ItaniumCXXABI::EmitFundamentalRTTIDescriptors() {
3324 QualType FundamentalTypes[] = {
3325 getContext().VoidTy, getContext().NullPtrTy,
3326 getContext().BoolTy, getContext().WCharTy,
3327 getContext().CharTy, getContext().UnsignedCharTy,
3328 getContext().SignedCharTy, getContext().ShortTy,
3329 getContext().UnsignedShortTy, getContext().IntTy,
3330 getContext().UnsignedIntTy, getContext().LongTy,
3331 getContext().UnsignedLongTy, getContext().LongLongTy,
3332 getContext().UnsignedLongLongTy, getContext().HalfTy,
3333 getContext().FloatTy, getContext().DoubleTy,
3334 getContext().LongDoubleTy, getContext().Char16Ty,
3335 getContext().Char32Ty,
3336 };
3337 for (const QualType &FundamentalType : FundamentalTypes)
3338 EmitFundamentalRTTIDescriptor(FundamentalType);
3339}
3340
3341/// What sort of uniqueness rules should we use for the RTTI for the
3342/// given type?
3343ItaniumCXXABI::RTTIUniquenessKind ItaniumCXXABI::classifyRTTIUniqueness(
3344 QualType CanTy, llvm::GlobalValue::LinkageTypes Linkage) const {
3345 if (shouldRTTIBeUnique())
3346 return RUK_Unique;
3347
3348 // It's only necessary for linkonce_odr or weak_odr linkage.
3349 if (Linkage != llvm::GlobalValue::LinkOnceODRLinkage &&
3350 Linkage != llvm::GlobalValue::WeakODRLinkage)
3351 return RUK_Unique;
3352
3353 // It's only necessary with default visibility.
3354 if (CanTy->getVisibility() != DefaultVisibility)
3355 return RUK_Unique;
3356
3357 // If we're not required to publish this symbol, hide it.
3358 if (Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
3359 return RUK_NonUniqueHidden;
3360
3361 // If we're required to publish this symbol, as we might be under an
3362 // explicit instantiation, leave it with default visibility but
3363 // enable string-comparisons.
3364 assert(Linkage == llvm::GlobalValue::WeakODRLinkage);
3365 return RUK_NonUniqueVisible;
3366}
Stephen Hines176edba2014-12-01 14:53:08 -08003367
3368// Find out how to codegen the complete destructor and constructor
3369namespace {
3370enum class StructorCodegen { Emit, RAUW, Alias, COMDAT };
3371}
3372static StructorCodegen getCodegenToUse(CodeGenModule &CGM,
3373 const CXXMethodDecl *MD) {
3374 if (!CGM.getCodeGenOpts().CXXCtorDtorAliases)
3375 return StructorCodegen::Emit;
3376
3377 // The complete and base structors are not equivalent if there are any virtual
3378 // bases, so emit separate functions.
3379 if (MD->getParent()->getNumVBases())
3380 return StructorCodegen::Emit;
3381
3382 GlobalDecl AliasDecl;
3383 if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) {
3384 AliasDecl = GlobalDecl(DD, Dtor_Complete);
3385 } else {
3386 const auto *CD = cast<CXXConstructorDecl>(MD);
3387 AliasDecl = GlobalDecl(CD, Ctor_Complete);
3388 }
3389 llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl);
3390
3391 if (llvm::GlobalValue::isDiscardableIfUnused(Linkage))
3392 return StructorCodegen::RAUW;
3393
3394 // FIXME: Should we allow available_externally aliases?
3395 if (!llvm::GlobalAlias::isValidLinkage(Linkage))
3396 return StructorCodegen::RAUW;
3397
3398 if (llvm::GlobalValue::isWeakForLinker(Linkage)) {
3399 // Only ELF supports COMDATs with arbitrary names (C5/D5).
3400 if (CGM.getTarget().getTriple().isOSBinFormatELF())
3401 return StructorCodegen::COMDAT;
3402 return StructorCodegen::Emit;
3403 }
3404
3405 return StructorCodegen::Alias;
3406}
3407
3408static void emitConstructorDestructorAlias(CodeGenModule &CGM,
3409 GlobalDecl AliasDecl,
3410 GlobalDecl TargetDecl) {
3411 llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl);
3412
3413 StringRef MangledName = CGM.getMangledName(AliasDecl);
3414 llvm::GlobalValue *Entry = CGM.GetGlobalValue(MangledName);
3415 if (Entry && !Entry->isDeclaration())
3416 return;
3417
3418 auto *Aliasee = cast<llvm::GlobalValue>(CGM.GetAddrOfGlobal(TargetDecl));
Stephen Hines176edba2014-12-01 14:53:08 -08003419
3420 // Create the alias with no name.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003421 auto *Alias = llvm::GlobalAlias::create(Linkage, "", Aliasee);
Stephen Hines176edba2014-12-01 14:53:08 -08003422
3423 // Switch any previous uses to the alias.
3424 if (Entry) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003425 assert(Entry->getType() == Aliasee->getType() &&
Stephen Hines176edba2014-12-01 14:53:08 -08003426 "declaration exists with different type");
3427 Alias->takeName(Entry);
3428 Entry->replaceAllUsesWith(Alias);
3429 Entry->eraseFromParent();
3430 } else {
3431 Alias->setName(MangledName);
3432 }
3433
3434 // Finally, set up the alias with its proper name and attributes.
3435 CGM.setAliasAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias);
3436}
3437
3438void ItaniumCXXABI::emitCXXStructor(const CXXMethodDecl *MD,
3439 StructorType Type) {
3440 auto *CD = dyn_cast<CXXConstructorDecl>(MD);
3441 const CXXDestructorDecl *DD = CD ? nullptr : cast<CXXDestructorDecl>(MD);
3442
3443 StructorCodegen CGType = getCodegenToUse(CGM, MD);
3444
3445 if (Type == StructorType::Complete) {
3446 GlobalDecl CompleteDecl;
3447 GlobalDecl BaseDecl;
3448 if (CD) {
3449 CompleteDecl = GlobalDecl(CD, Ctor_Complete);
3450 BaseDecl = GlobalDecl(CD, Ctor_Base);
3451 } else {
3452 CompleteDecl = GlobalDecl(DD, Dtor_Complete);
3453 BaseDecl = GlobalDecl(DD, Dtor_Base);
3454 }
3455
3456 if (CGType == StructorCodegen::Alias || CGType == StructorCodegen::COMDAT) {
3457 emitConstructorDestructorAlias(CGM, CompleteDecl, BaseDecl);
3458 return;
3459 }
3460
3461 if (CGType == StructorCodegen::RAUW) {
3462 StringRef MangledName = CGM.getMangledName(CompleteDecl);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003463 auto *Aliasee = CGM.GetAddrOfGlobal(BaseDecl);
Stephen Hines176edba2014-12-01 14:53:08 -08003464 CGM.addReplacement(MangledName, Aliasee);
3465 return;
3466 }
3467 }
3468
3469 // The base destructor is equivalent to the base destructor of its
3470 // base class if there is exactly one non-virtual base class with a
3471 // non-trivial destructor, there are no fields with a non-trivial
3472 // destructor, and the body of the destructor is trivial.
3473 if (DD && Type == StructorType::Base && CGType != StructorCodegen::COMDAT &&
3474 !CGM.TryEmitBaseDestructorAsAlias(DD))
3475 return;
3476
3477 llvm::Function *Fn = CGM.codegenCXXStructor(MD, Type);
3478
3479 if (CGType == StructorCodegen::COMDAT) {
3480 SmallString<256> Buffer;
3481 llvm::raw_svector_ostream Out(Buffer);
3482 if (DD)
3483 getMangleContext().mangleCXXDtorComdat(DD, Out);
3484 else
3485 getMangleContext().mangleCXXCtorComdat(CD, Out);
3486 llvm::Comdat *C = CGM.getModule().getOrInsertComdat(Out.str());
3487 Fn->setComdat(C);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003488 } else {
3489 CGM.maybeSetTrivialComdat(*MD, *Fn);
Stephen Hines176edba2014-12-01 14:53:08 -08003490 }
3491}
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003492
3493static llvm::Constant *getBeginCatchFn(CodeGenModule &CGM) {
3494 // void *__cxa_begin_catch(void*);
3495 llvm::FunctionType *FTy = llvm::FunctionType::get(
3496 CGM.Int8PtrTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
3497
3498 return CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
3499}
3500
3501static llvm::Constant *getEndCatchFn(CodeGenModule &CGM) {
3502 // void __cxa_end_catch();
3503 llvm::FunctionType *FTy =
3504 llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false);
3505
3506 return CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch");
3507}
3508
3509static llvm::Constant *getGetExceptionPtrFn(CodeGenModule &CGM) {
3510 // void *__cxa_get_exception_ptr(void*);
3511 llvm::FunctionType *FTy = llvm::FunctionType::get(
3512 CGM.Int8PtrTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
3513
3514 return CGM.CreateRuntimeFunction(FTy, "__cxa_get_exception_ptr");
3515}
3516
3517namespace {
3518 /// A cleanup to call __cxa_end_catch. In many cases, the caught
3519 /// exception type lets us state definitively that the thrown exception
3520 /// type does not have a destructor. In particular:
3521 /// - Catch-alls tell us nothing, so we have to conservatively
3522 /// assume that the thrown exception might have a destructor.
3523 /// - Catches by reference behave according to their base types.
3524 /// - Catches of non-record types will only trigger for exceptions
3525 /// of non-record types, which never have destructors.
3526 /// - Catches of record types can trigger for arbitrary subclasses
3527 /// of the caught type, so we have to assume the actual thrown
3528 /// exception type might have a throwing destructor, even if the
3529 /// caught type's destructor is trivial or nothrow.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003530 struct CallEndCatch final : EHScopeStack::Cleanup {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003531 CallEndCatch(bool MightThrow) : MightThrow(MightThrow) {}
3532 bool MightThrow;
3533
3534 void Emit(CodeGenFunction &CGF, Flags flags) override {
3535 if (!MightThrow) {
3536 CGF.EmitNounwindRuntimeCall(getEndCatchFn(CGF.CGM));
3537 return;
3538 }
3539
3540 CGF.EmitRuntimeCallOrInvoke(getEndCatchFn(CGF.CGM));
3541 }
3542 };
3543}
3544
3545/// Emits a call to __cxa_begin_catch and enters a cleanup to call
3546/// __cxa_end_catch.
3547///
3548/// \param EndMightThrow - true if __cxa_end_catch might throw
3549static llvm::Value *CallBeginCatch(CodeGenFunction &CGF,
3550 llvm::Value *Exn,
3551 bool EndMightThrow) {
3552 llvm::CallInst *call =
3553 CGF.EmitNounwindRuntimeCall(getBeginCatchFn(CGF.CGM), Exn);
3554
3555 CGF.EHStack.pushCleanup<CallEndCatch>(NormalAndEHCleanup, EndMightThrow);
3556
3557 return call;
3558}
3559
3560/// A "special initializer" callback for initializing a catch
3561/// parameter during catch initialization.
3562static void InitCatchParam(CodeGenFunction &CGF,
3563 const VarDecl &CatchParam,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003564 Address ParamAddr,
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003565 SourceLocation Loc) {
3566 // Load the exception from where the landing pad saved it.
3567 llvm::Value *Exn = CGF.getExceptionFromSlot();
3568
3569 CanQualType CatchType =
3570 CGF.CGM.getContext().getCanonicalType(CatchParam.getType());
3571 llvm::Type *LLVMCatchTy = CGF.ConvertTypeForMem(CatchType);
3572
3573 // If we're catching by reference, we can just cast the object
3574 // pointer to the appropriate pointer.
3575 if (isa<ReferenceType>(CatchType)) {
3576 QualType CaughtType = cast<ReferenceType>(CatchType)->getPointeeType();
3577 bool EndCatchMightThrow = CaughtType->isRecordType();
3578
3579 // __cxa_begin_catch returns the adjusted object pointer.
3580 llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, EndCatchMightThrow);
3581
3582 // We have no way to tell the personality function that we're
3583 // catching by reference, so if we're catching a pointer,
3584 // __cxa_begin_catch will actually return that pointer by value.
3585 if (const PointerType *PT = dyn_cast<PointerType>(CaughtType)) {
3586 QualType PointeeType = PT->getPointeeType();
3587
3588 // When catching by reference, generally we should just ignore
3589 // this by-value pointer and use the exception object instead.
3590 if (!PointeeType->isRecordType()) {
3591
3592 // Exn points to the struct _Unwind_Exception header, which
3593 // we have to skip past in order to reach the exception data.
3594 unsigned HeaderSize =
3595 CGF.CGM.getTargetCodeGenInfo().getSizeOfUnwindException();
3596 AdjustedExn = CGF.Builder.CreateConstGEP1_32(Exn, HeaderSize);
3597
3598 // However, if we're catching a pointer-to-record type that won't
3599 // work, because the personality function might have adjusted
3600 // the pointer. There's actually no way for us to fully satisfy
3601 // the language/ABI contract here: we can't use Exn because it
3602 // might have the wrong adjustment, but we can't use the by-value
3603 // pointer because it's off by a level of abstraction.
3604 //
3605 // The current solution is to dump the adjusted pointer into an
3606 // alloca, which breaks language semantics (because changing the
3607 // pointer doesn't change the exception) but at least works.
3608 // The better solution would be to filter out non-exact matches
3609 // and rethrow them, but this is tricky because the rethrow
3610 // really needs to be catchable by other sites at this landing
3611 // pad. The best solution is to fix the personality function.
3612 } else {
3613 // Pull the pointer for the reference type off.
3614 llvm::Type *PtrTy =
3615 cast<llvm::PointerType>(LLVMCatchTy)->getElementType();
3616
3617 // Create the temporary and write the adjusted pointer into it.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003618 Address ExnPtrTmp =
3619 CGF.CreateTempAlloca(PtrTy, CGF.getPointerAlign(), "exn.byref.tmp");
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003620 llvm::Value *Casted = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
3621 CGF.Builder.CreateStore(Casted, ExnPtrTmp);
3622
3623 // Bind the reference to the temporary.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003624 AdjustedExn = ExnPtrTmp.getPointer();
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003625 }
3626 }
3627
3628 llvm::Value *ExnCast =
3629 CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.byref");
3630 CGF.Builder.CreateStore(ExnCast, ParamAddr);
3631 return;
3632 }
3633
3634 // Scalars and complexes.
3635 TypeEvaluationKind TEK = CGF.getEvaluationKind(CatchType);
3636 if (TEK != TEK_Aggregate) {
3637 llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, false);
3638
3639 // If the catch type is a pointer type, __cxa_begin_catch returns
3640 // the pointer by value.
3641 if (CatchType->hasPointerRepresentation()) {
3642 llvm::Value *CastExn =
3643 CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.casted");
3644
3645 switch (CatchType.getQualifiers().getObjCLifetime()) {
3646 case Qualifiers::OCL_Strong:
3647 CastExn = CGF.EmitARCRetainNonBlock(CastExn);
3648 // fallthrough
3649
3650 case Qualifiers::OCL_None:
3651 case Qualifiers::OCL_ExplicitNone:
3652 case Qualifiers::OCL_Autoreleasing:
3653 CGF.Builder.CreateStore(CastExn, ParamAddr);
3654 return;
3655
3656 case Qualifiers::OCL_Weak:
3657 CGF.EmitARCInitWeak(ParamAddr, CastExn);
3658 return;
3659 }
3660 llvm_unreachable("bad ownership qualifier!");
3661 }
3662
3663 // Otherwise, it returns a pointer into the exception object.
3664
3665 llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
3666 llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
3667
3668 LValue srcLV = CGF.MakeNaturalAlignAddrLValue(Cast, CatchType);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003669 LValue destLV = CGF.MakeAddrLValue(ParamAddr, CatchType);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003670 switch (TEK) {
3671 case TEK_Complex:
3672 CGF.EmitStoreOfComplex(CGF.EmitLoadOfComplex(srcLV, Loc), destLV,
3673 /*init*/ true);
3674 return;
3675 case TEK_Scalar: {
3676 llvm::Value *ExnLoad = CGF.EmitLoadOfScalar(srcLV, Loc);
3677 CGF.EmitStoreOfScalar(ExnLoad, destLV, /*init*/ true);
3678 return;
3679 }
3680 case TEK_Aggregate:
3681 llvm_unreachable("evaluation kind filtered out!");
3682 }
3683 llvm_unreachable("bad evaluation kind");
3684 }
3685
3686 assert(isa<RecordType>(CatchType) && "unexpected catch type!");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003687 auto catchRD = CatchType->getAsCXXRecordDecl();
3688 CharUnits caughtExnAlignment = CGF.CGM.getClassPointerAlignment(catchRD);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003689
3690 llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
3691
3692 // Check for a copy expression. If we don't have a copy expression,
3693 // that means a trivial copy is okay.
3694 const Expr *copyExpr = CatchParam.getInit();
3695 if (!copyExpr) {
3696 llvm::Value *rawAdjustedExn = CallBeginCatch(CGF, Exn, true);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003697 Address adjustedExn(CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy),
3698 caughtExnAlignment);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003699 CGF.EmitAggregateCopy(ParamAddr, adjustedExn, CatchType);
3700 return;
3701 }
3702
3703 // We have to call __cxa_get_exception_ptr to get the adjusted
3704 // pointer before copying.
3705 llvm::CallInst *rawAdjustedExn =
3706 CGF.EmitNounwindRuntimeCall(getGetExceptionPtrFn(CGF.CGM), Exn);
3707
3708 // Cast that to the appropriate type.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003709 Address adjustedExn(CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy),
3710 caughtExnAlignment);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003711
3712 // The copy expression is defined in terms of an OpaqueValueExpr.
3713 // Find it and map it to the adjusted expression.
3714 CodeGenFunction::OpaqueValueMapping
3715 opaque(CGF, OpaqueValueExpr::findInCopyConstruct(copyExpr),
3716 CGF.MakeAddrLValue(adjustedExn, CatchParam.getType()));
3717
3718 // Call the copy ctor in a terminate scope.
3719 CGF.EHStack.pushTerminate();
3720
3721 // Perform the copy construction.
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003722 CGF.EmitAggExpr(copyExpr,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003723 AggValueSlot::forAddr(ParamAddr, Qualifiers(),
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003724 AggValueSlot::IsNotDestructed,
3725 AggValueSlot::DoesNotNeedGCBarriers,
3726 AggValueSlot::IsNotAliased));
3727
3728 // Leave the terminate scope.
3729 CGF.EHStack.popTerminate();
3730
3731 // Undo the opaque value mapping.
3732 opaque.pop();
3733
3734 // Finally we can call __cxa_begin_catch.
3735 CallBeginCatch(CGF, Exn, true);
3736}
3737
3738/// Begins a catch statement by initializing the catch variable and
3739/// calling __cxa_begin_catch.
3740void ItaniumCXXABI::emitBeginCatch(CodeGenFunction &CGF,
3741 const CXXCatchStmt *S) {
3742 // We have to be very careful with the ordering of cleanups here:
3743 // C++ [except.throw]p4:
3744 // The destruction [of the exception temporary] occurs
3745 // immediately after the destruction of the object declared in
3746 // the exception-declaration in the handler.
3747 //
3748 // So the precise ordering is:
3749 // 1. Construct catch variable.
3750 // 2. __cxa_begin_catch
3751 // 3. Enter __cxa_end_catch cleanup
3752 // 4. Enter dtor cleanup
3753 //
3754 // We do this by using a slightly abnormal initialization process.
3755 // Delegation sequence:
3756 // - ExitCXXTryStmt opens a RunCleanupsScope
3757 // - EmitAutoVarAlloca creates the variable and debug info
3758 // - InitCatchParam initializes the variable from the exception
3759 // - CallBeginCatch calls __cxa_begin_catch
3760 // - CallBeginCatch enters the __cxa_end_catch cleanup
3761 // - EmitAutoVarCleanups enters the variable destructor cleanup
3762 // - EmitCXXTryStmt emits the code for the catch body
3763 // - EmitCXXTryStmt close the RunCleanupsScope
3764
3765 VarDecl *CatchParam = S->getExceptionDecl();
3766 if (!CatchParam) {
3767 llvm::Value *Exn = CGF.getExceptionFromSlot();
3768 CallBeginCatch(CGF, Exn, true);
3769 return;
3770 }
3771
3772 // Emit the local.
3773 CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam);
3774 InitCatchParam(CGF, *CatchParam, var.getObjectAddress(CGF), S->getLocStart());
3775 CGF.EmitAutoVarCleanups(var);
3776}
3777
3778/// Get or define the following function:
3779/// void @__clang_call_terminate(i8* %exn) nounwind noreturn
3780/// This code is used only in C++.
3781static llvm::Constant *getClangCallTerminateFn(CodeGenModule &CGM) {
3782 llvm::FunctionType *fnTy =
3783 llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
3784 llvm::Constant *fnRef =
3785 CGM.CreateRuntimeFunction(fnTy, "__clang_call_terminate");
3786
3787 llvm::Function *fn = dyn_cast<llvm::Function>(fnRef);
3788 if (fn && fn->empty()) {
3789 fn->setDoesNotThrow();
3790 fn->setDoesNotReturn();
3791
3792 // What we really want is to massively penalize inlining without
3793 // forbidding it completely. The difference between that and
3794 // 'noinline' is negligible.
3795 fn->addFnAttr(llvm::Attribute::NoInline);
3796
3797 // Allow this function to be shared across translation units, but
3798 // we don't want it to turn into an exported symbol.
3799 fn->setLinkage(llvm::Function::LinkOnceODRLinkage);
3800 fn->setVisibility(llvm::Function::HiddenVisibility);
3801 if (CGM.supportsCOMDAT())
3802 fn->setComdat(CGM.getModule().getOrInsertComdat(fn->getName()));
3803
3804 // Set up the function.
3805 llvm::BasicBlock *entry =
3806 llvm::BasicBlock::Create(CGM.getLLVMContext(), "", fn);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003807 CGBuilderTy builder(CGM, entry);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003808
3809 // Pull the exception pointer out of the parameter list.
3810 llvm::Value *exn = &*fn->arg_begin();
3811
3812 // Call __cxa_begin_catch(exn).
3813 llvm::CallInst *catchCall = builder.CreateCall(getBeginCatchFn(CGM), exn);
3814 catchCall->setDoesNotThrow();
3815 catchCall->setCallingConv(CGM.getRuntimeCC());
3816
3817 // Call std::terminate().
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003818 llvm::CallInst *termCall = builder.CreateCall(CGM.getTerminateFn());
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003819 termCall->setDoesNotThrow();
3820 termCall->setDoesNotReturn();
3821 termCall->setCallingConv(CGM.getRuntimeCC());
3822
3823 // std::terminate cannot return.
3824 builder.CreateUnreachable();
3825 }
3826
3827 return fnRef;
3828}
3829
3830llvm::CallInst *
3831ItaniumCXXABI::emitTerminateForUnexpectedException(CodeGenFunction &CGF,
3832 llvm::Value *Exn) {
3833 // In C++, we want to call __cxa_begin_catch() before terminating.
3834 if (Exn) {
3835 assert(CGF.CGM.getLangOpts().CPlusPlus);
3836 return CGF.EmitNounwindRuntimeCall(getClangCallTerminateFn(CGF.CGM), Exn);
3837 }
3838 return CGF.EmitNounwindRuntimeCall(CGF.CGM.getTerminateFn());
3839}